Remove offset code

pull/1/head
yflory 4 years ago
parent fe9d39c66b
commit ea4c529e39

@ -367,8 +367,8 @@ const storeMessage = function (Env, channel, msg, isCp, optionalMessageHash, cb)
} }
} }
// call back with the offset of the message we just stored // Message stored, call back
cb(void 0, index.size); cb();
index.size += msgBin.length; index.size += msgBin.length;
@ -508,22 +508,9 @@ const getHistoryAsync = (Env, channelName, lastKnownHash, beforeHash, handler, c
return void cb(new Error('EUNKNOWN')); return void cb(new Error('EUNKNOWN'));
} }
const start = (beforeHash) ? 0 : offset; const start = (beforeHash) ? 0 : offset;
let msgOffset = start;
store.readMessagesBin(channelName, start, (msgObj, readMore, abort) => { store.readMessagesBin(channelName, start, (msgObj, readMore, abort) => {
if (beforeHash && msgObj.offset >= offset) { return void abort(); } if (beforeHash && msgObj.offset >= offset) { return void abort(); }
const str = msgObj.buff.toString('utf8'); const parsed = tryParse(Env, msgObj.buff.toString('utf8'));
var parsed = tryParse(Env, str);
// Extract the time and put it in the new opts object
let time;
if (Array.isArray(parsed)) {
if (typeof(parsed[parsed.length - 1]) === "number") { time = parsed.pop(); }
parsed.push({
time: time,
offset: msgObj.offset
});
}
if (!parsed) { return void readMore(); } if (!parsed) { return void readMore(); }
handler(parsed, readMore); handler(parsed, readMore);
}, waitFor(function (err) { }, waitFor(function (err) {
@ -718,20 +705,7 @@ const handleGetHistoryRange = function (Env, Server, seq, userId, parsed) {
} }
if (Array.isArray(toSend)) { if (Array.isArray(toSend)) {
// toSend: array of objects {msg, hash, offset} toSend.forEach(function (msg) {
toSend.forEach(function (obj) {
let msg = obj.msg;
// Extract the time and put it in the new opts object
let time;
if (Array.isArray(msg)) {
if (typeof(msg[msg.length - 1]) === "number") { time = msg.pop(); }
msg.push({
time: time,
offset: obj.offset
});
}
Server.send(userId, [0, HISTORY_KEEPER_ID, 'MSG', userId, Server.send(userId, [0, HISTORY_KEEPER_ID, 'MSG', userId,
JSON.stringify(['HISTORY_RANGE', txid, msg])]); JSON.stringify(['HISTORY_RANGE', txid, msg])]);
}); });

@ -263,12 +263,9 @@ var readMessages = function (path, msgHandler, _cb) {
var collector = createIdleStreamCollector(stream); var collector = createIdleStreamCollector(stream);
var cb = Util.once(Util.mkAsync(Util.both(_cb, collector))); var cb = Util.once(Util.mkAsync(Util.both(_cb, collector)));
let offset = 0;
return readFileBin(stream, function (msgObj, readMore) { return readFileBin(stream, function (msgObj, readMore) {
collector.keepAlive(); collector.keepAlive();
const str = msgObj.buff.toString('utf8'); msgHandler(msgObj.buff.toString('utf8'));
msgHandler(str, offset);
offset += str.length + 1; // +1 because of line break
readMore(); readMore();
}, function (err) { }, function (err) {
cb(err); cb(err);
@ -849,10 +846,10 @@ var message = function (env, chanName, msg, cb) {
var getMessages = function (env, chanName, handler, cb) { var getMessages = function (env, chanName, handler, cb) {
var errorState = false; var errorState = false;
var path = mkPath(env, chanName); var path = mkPath(env, chanName);
readMessages(path, function (msg, offset) { readMessages(path, function (msg) {
if (!msg || errorState) { return; } if (!msg || errorState) { return; }
try { try {
handler(msg, offset); handler(msg);
} catch (e) { } catch (e) {
errorState = true; errorState = true;
return void cb(e); return void cb(e);

@ -329,9 +329,7 @@ const getOlderHistory = function (data, cb) {
var messages = []; var messages = [];
var found = false; var found = false;
// XXX use readMessagesBin ? store.getMessages(channelName, function (msgStr) {
// But we don't know the "start" offset if we use "desiredMessages" or "desiredCheckpoint"
store.getMessages(channelName, function (msgStr, offset) {
if (found) { return; } if (found) { return; }
let parsed = HK.tryParse(Env, msgStr); let parsed = HK.tryParse(Env, msgStr);
@ -353,7 +351,6 @@ const getOlderHistory = function (data, cb) {
messages.push({ messages.push({
msg: parsed, msg: parsed,
hash: hash, hash: hash,
offset: offset
}); });
}, function (err) { }, function (err) {
var toSend = []; var toSend = [];

@ -2065,18 +2065,12 @@ define([
if (msg) { if (msg) {
msg = msg.replace(/cp\|(([A-Za-z0-9+\/=]+)\|)?/, ''); msg = msg.replace(/cp\|(([A-Za-z0-9+\/=]+)\|)?/, '');
//var decryptedMsg = crypto.decrypt(msg, true); //var decryptedMsg = crypto.decrypt(msg, true);
var opts = {};
if (parsed[1][5] && typeof(parsed[1][5]) === "object") {
opts = parsed[1][5];
console.error(opts);
}
if (data.debug) { if (data.debug) {
msgs.push({ msgs.push({
serverHash: msg.slice(0,64), serverHash: msg.slice(0,64),
msg: msg, msg: msg,
author: parsed[1][1], author: parsed[1][1],
time: opts.time, time: parsed[1][5]
offset: opts.offset
}); });
} else { } else {
msgs.push(msg); msgs.push(msg);
@ -2206,17 +2200,12 @@ define([
lastKnownHash = msg.slice(0,64); lastKnownHash = msg.slice(0,64);
first = false; first = false;
} }
var opts = {};
if (parsed[2][5] && typeof(parsed[2][5]) === "object") {
opts = parsed[2][5];
}
msg = msg.replace(/cp\|(([A-Za-z0-9+\/=]+)\|)?/, ''); msg = msg.replace(/cp\|(([A-Za-z0-9+\/=]+)\|)?/, '');
msgs.push({ msgs.push({
serverHash: msg.slice(0,64), serverHash: msg.slice(0,64),
msg: msg, msg: msg,
author: parsed[2][1], author: parsed[2][1],
time: opts.time, time: parsed[2][5]
offset: opts.offset
}); });
} }
}; };

@ -396,14 +396,11 @@ proxy.mailboxes = {
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
var opts = {};
if (_msg[5] && typeof(_msg[5]) === "object") { opts = _msg[5]; }
ctx.emit('HISTORY', { ctx.emit('HISTORY', {
txid: txid, txid: txid,
time: opts.time, time: _msg[5],
message: message, message: message,
hash: _msg[4].slice(0,64), hash: _msg[4].slice(0,64)
offset: opts.offset
}, [req.cId]); }, [req.cId]);
} else if (type === 'HISTORY_RANGE_END') { } else if (type === 'HISTORY_RANGE_END') {
ctx.emit('HISTORY', { ctx.emit('HISTORY', {

@ -207,7 +207,6 @@ define([
if (historyState) { return void cb("ALREADY_CALLED"); } if (historyState) { return void cb("ALREADY_CALLED"); }
historyState = true; historyState = true;
var txid = Util.uid(); var txid = Util.uid();
// XXX OFFSET: no change? offsets are useless in get_history_range
execCommand('LOAD_HISTORY', { execCommand('LOAD_HISTORY', {
type: type, type: type,
count: lastKnownHash ? count + 1 : count, count: lastKnownHash ? count + 1 : count,

Loading…
Cancel
Save