trim lookup tables for channels without checkpoints

This commit is contained in:
ansuz
2020-03-30 18:03:37 -04:00
parent a6943b12b4
commit 77961e3954
2 changed files with 71 additions and 6 deletions

View File

@@ -118,6 +118,7 @@ const computeIndex = function (data, cb) {
const CB = Util.once(cb);
const offsetByHash = {};
let offsetCount = 0;
let size = 0;
nThen(function (w) {
// iterate over all messages in the channel log
@@ -151,6 +152,8 @@ const computeIndex = function (data, cb) {
// so clear the buffer every time you see a new one
messageBuf = [];
}
} else if (messageBuf.length > 100 && cpIndex.length === 0) {
messageBuf = messageBuf.slice(0, 50);
}
// if it's not metadata or a checkpoint then it should be a regular message
// store it in the buffer
@@ -163,6 +166,7 @@ const computeIndex = function (data, cb) {
}
// once indexing is complete you should have a buffer of messages since the latest checkpoint
// or the 50-100 latest messages if the channel is of a type without checkpoints.
// map the 'hash' of each message to its byte offset in the log, to be used for reconnecting clients
messageBuf.forEach((msgObj) => {
const msg = HK.tryParse(Env, msgObj.buff.toString('utf8'));
@@ -171,6 +175,7 @@ const computeIndex = function (data, cb) {
// msgObj.offset is API guaranteed by our storage module
// it should always be a valid positive integer
offsetByHash[HK.getHash(msg[4])] = msgObj.offset;
offsetCount++;
}
// There is a trailing \n at the end of the file
size = msgObj.offset + msgObj.buff.length + 1;
@@ -182,6 +187,7 @@ const computeIndex = function (data, cb) {
// Only keep the checkpoints included in the last 100 messages
cpIndex: HK.sliceCpIndex(cpIndex, i),
offsetByHash: offsetByHash,
offsets: offsetCount,
size: size,
//metadata: metadata,
line: i