使用 rus
我在我的 node.js Express 应用程序中使用 来比较 SQL Server 数据库中的两个表。
但是由于我的表可能很大,我想使用“分块”方法来避免OOM,所以我重构了我的函数
return require('rus-diff').rusDiff(first_array_of_objects, second_array_of_objects);
看起来像这样,所以一次只比较 500 行。
compareTwoArrayOfObjects: function (first_array_of_objects, second_array_of_objects, batch_size = 500) {
const rusDiff = require('rus-diff');
const diff = {$set: {}};
let rows_processed = 0;
// Loop over the arrays in batches
while (rows_processed < Math.max(first_array_of_objects.length, second_array_of_objects.length)) {
const first_batch = first_array_of_objects.slice(rows_processed, rows_processed + batch_size);
const second_batch = second_array_of_objects.slice(rows_processed, rows_processed + batch_size);
// Compare the batches using rus-diff
const batch_diff = rusDiff.rusDiff(first_batch, second_batch);
// Merge the batch differences into the overall differences
Object.assign(diff.$set, batch_diff.$set);
// Update the number of rows processed
rows_processed += batch_size;
// Log the percentage completed
const percentage_completed = Math.round(rows_processed / Math.max(first_array_of_objects.length, second_array_of_objects.length) * 100);
console.log(`Processed ${rows_processed} rows (${percentage_completed}%)`);
}
return diff;
}
这个 compareTwoArrayOfObjects 函数在我的路线中是这样使用的:
compareRows: async function (req, res) {
console.log('compare-rows endpoint called');
const {
sourceDB,
destDB,
....
} = req.body;
//console.log(runID, useRunId)
const commonCols = findCommonStrings(columns1, columns2);
try {
const sourcePool = await GetCreateIfNotExistPool(sourceDB);
const destPool = await GetCreateIfNotExistPool(destDB);
const destRows = await getDestRows(destPool, destDB, commonCols, table2, joinKeys, pageSize, schema2, runID, useRunId);
const sourceRows = await getSourceRows(sourcePool, sourceDB, table1, joinKeys, commonCols, destRows, parallelism, schema1);
let tableDiffs = compareTwoArrayOfObjects(sourceRows, destRows);
res.json({
sourceData: sourceRows,
destData: destRows,
isSameObject: Object.keys(tableDiffs).length === 0 ? false : tableDiffs
})
} catch (err) {
console.error(err);
res.status(500).send(err.message);
}
}
我遇到的问题是现在我的“compareTwoArrayOfObjects”只比较第一个块。当我调用 compare rows 当它应该遍历我的两个数组并重新返回所有块的差异时。有人可以帮我弄清楚这是否是我的代码的问题?还是“rus-diff”不允许“分块”?
回答如下:更多推荐
使用 rus
发布评论