使用 rus

编程入门 行业动态 更新时间:2024-10-06 14:23:20

使用 rus

使用 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

本文发布于:2024-05-13 16:57:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760189.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:rus

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!