avoid incrementing index size twice for a single message

This commit is contained in:
ansuz 2021-03-01 18:23:24 +05:30
parent 3be2ef9d61
commit c9d1996e0d

View file

@ -300,6 +300,7 @@ var trimMapByOffset = function (map, offset) {
* the fix is to use callbacks and implement queueing for writes
* to guarantee that offset computation is always atomic with writes
*/
// FIXME 'optionalMessageHash' is always supplied, so we could consider renaming it for clarity
const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash, cb) {
const id = channel.id;
const Log = Env.Log;
@ -338,6 +339,12 @@ const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash, cb)
cb();
return void next();
}
if (optionalMessageHash && typeof(index.offsetByHash[optionalMessageHash]) === 'number') {
cb();
return void next();
}
if (typeof (index.line) === "number") { index.line++; }
if (isCp) {
index.cpIndex = sliceCpIndex(index.cpIndex, index.line || 0);
@ -352,7 +359,7 @@ const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash, cb)
can actually cause it to become incorrect, leading to incorrect behaviour when clients connect
with a lastKnownHash. We avoid this by only assigning new offsets to the map.
*/
if (optionalMessageHash && typeof(index.offsetByHash[optionalMessageHash]) === 'undefined') {
if (optionalMessageHash /* && typeof(index.offsetByHash[optionalMessageHash]) === 'undefined' */) {
index.offsetByHash[optionalMessageHash] = index.size;
index.offsets++;
}