node.js - Node.js 缓冲区异或意外行为
问题描述
设想:
用户上传一个 X 大小的文件,该文件被分成 12 个相等的块;如果除法的结果不是无符号整数,我会在上限上对其进行四舍五入,除了最后一个块外,所有块的长度都相同
预处理 12 个块并创建 6 个擦除块(4 个局部块和 2 个全局块),以恢复文件;我选择了这种方法来减少所需的磁盘大小并优化恢复并增加一些容错能力
工作场景代码
function erasureChunkCreate(chunks,callback) {
let erasureChunks = {};
let locale = undefined;
let global = undefined;
erasureChunks = {'local': {}, 'global': {}};
async.eachSeries(chunks, (item, callback_times) => {
if (locale === undefined)
locale = item.chunk.slice();
else
locale = xor(locale, item.chunk);
if (item.seq_no % 3 === 2) {
erasureChunks['local'][Math.ceil(item.seq_no / 3)] = locale;
if (global === undefined)
global = locale.slice();
else
global = xor(global, locale);
locale = undefined;
}
callback_times(null);
}, (err_times) => {
if (err_times)
return callback(err_times, null);
erasureChunks['global'] = global;
return callback(null, erasureChunks);
});
}
问题:
- 如果全局变量是在计算语言环境 xor 之后放置的,我会得到下一个意外行为,不同的块哈希
意外场景引导代码部分
if (locale === undefined)
locale = item.chunk.slice();
else
locale = xor(locale, item.chunk);
if (global === undefined)
global = locale.slice();
else
global = xor(global, locale);
if (item.seq_no % 3 === 2) {
erasureChunks['local'][Math.ceil(item.seq_no / 3)] = locale;
locale = undefined;
}
我尝试了什么:
- 调试并手动检查缓冲区和异或结果,看到这两种情况产生不同的异或值
- 试图改变代码的逻辑(导致工作场景代码)
使用的模块:
- 异步
- 缓冲异或
解决方案
推荐阅读
- .net-core - .net core 3.1 Console App 找不到配置文件“appsettings.json”
- r - 根据时间戳识别 R data.frame 中 Remark 列的更改位置
- javascript - 从哈希数组返回平均值
- c++ - 检测球体和三角形之间的碰撞
- swift - 使用 Swift 包构建期间来自 IB 的非常奇怪的错误,使用 IBDesignable
- python - 从熊猫数据框中更新一行
- c - 两次对指针变量进行类型转换,为什么?
- sql - 从由特殊字符分隔的字符串数组中删除特定后缀
- docker - 如何在 Cloud Build 中禁用 `docker pull` 输出?
- python - ALTER COLUMN "active" TYPE boolean USING "active"::boolean