update metadata queries to include edits

pull/1/head
ansuz 5 years ago
parent efd0efede4
commit b36f50f2c9

@ -86,9 +86,8 @@ var handleCommand = function (meta, line) {
Meta.createLineHandler = function (ref, errorHandler) { Meta.createLineHandler = function (ref, errorHandler) {
ref.meta = {}; ref.meta = {};
errorHandler = errorHandler; var index = 0;
return function (err, line) {
return function (err, line, index) {
if (err) { if (err) {
return void errorHandler('METADATA_HANDLER_LINE_ERR', { return void errorHandler('METADATA_HANDLER_LINE_ERR', {
error: err, error: err,
@ -98,11 +97,12 @@ Meta.createLineHandler = function (ref, errorHandler) {
} }
if (Array.isArray(line)) { if (Array.isArray(line)) {
index++;
try { try {
handleCommand(ref.meta, line); handleCommand(ref.meta, line);
} catch (err) { } catch (err2) {
errorHandler("METADATA_COMMAND_ERR", { errorHandler("METADATA_COMMAND_ERR", {
error: err.stack, error: err2.stack,
line: line, line: line,
}); });
} }
@ -110,6 +110,7 @@ Meta.createLineHandler = function (ref, errorHandler) {
} }
if (index === 0 && typeof(line) === 'object') { if (index === 0 && typeof(line) === 'object') {
index++;
// special case! // special case!
ref.meta = line; ref.meta = line;
return; return;
@ -117,7 +118,7 @@ Meta.createLineHandler = function (ref, errorHandler) {
errorHandler("METADATA_HANDLER_WEIRDLINE", { errorHandler("METADATA_HANDLER_WEIRDLINE", {
line: line, line: line,
index: index index: index++,
}); });
}; };
}; };

@ -313,22 +313,33 @@ var getFileSize = function (Env, channel, cb) {
}); });
}; };
var Meta = require("./lib/metadata");
var getMetadata = function (Env, channel, cb) { var getMetadata = function (Env, channel, cb) {
if (!isValidId(channel)) { return void cb('INVALID_CHAN'); } if (!isValidId(channel)) { return void cb('INVALID_CHAN'); }
if (channel.length === 32) { if (channel.length !== 32) { return cb("INVALID_CHAN"); }
if (typeof(Env.msgStore.getChannelMetadata) !== 'function') {
return cb('GET_CHANNEL_METADATA_UNSUPPORTED'); var ref = {};
var lineHandler = Meta.createLineHandler(ref, Log.error);
return void Env.msgStore.readChannelMetadata(channel, lineHandler, function (err) {
if (err) {
// stream errors?
return void cb(err);
} }
cb(void 0, ref.meta);
});
/*
// FIXME METADATA
return void Env.msgStore.getChannelMetadata(channel, function (e, data) { return void Env.msgStore.getChannelMetadata(channel, function (e, data) {
if (e) { if (e) {
if (e.code === 'INVALID_METADATA') { return void cb(void 0, {}); } if (e.code === 'INVALID_METADATA') { return void cb(void 0, {}); }
return void cb(e.code); return void cb(e.code);
} }
cb(void 0, data); cb(void 0, data);
}); });*/
}
}; };
var getMultipleFileSize = function (Env, channels, cb) { var getMultipleFileSize = function (Env, channels, cb) {
@ -802,10 +813,12 @@ var clearOwnedChannel = function (Env, channelId, unsafeKey, cb) {
return cb('INVALID_ARGUMENTS'); return cb('INVALID_ARGUMENTS');
} }
// FIXME METADATA
if (!(Env.msgStore && Env.msgStore.getChannelMetadata)) { if (!(Env.msgStore && Env.msgStore.getChannelMetadata)) {
return cb('E_NOT_IMPLEMENTED'); return cb('E_NOT_IMPLEMENTED');
} }
// FIXME METADATA
Env.msgStore.getChannelMetadata(channelId, function (e, metadata) { Env.msgStore.getChannelMetadata(channelId, function (e, metadata) {
if (e) { return cb(e); } if (e) { return cb(e); }
if (!(metadata && Array.isArray(metadata.owners))) { return void cb('E_NO_OWNERS'); } if (!(metadata && Array.isArray(metadata.owners))) { return void cb('E_NO_OWNERS'); }
@ -822,6 +835,7 @@ var clearOwnedChannel = function (Env, channelId, unsafeKey, cb) {
}; };
var removeOwnedBlob = function (Env, blobId, unsafeKey, cb) { var removeOwnedBlob = function (Env, blobId, unsafeKey, cb) {
// FIXME METADATA
var safeKey = escapeKeyCharacters(unsafeKey); var safeKey = escapeKeyCharacters(unsafeKey);
var safeKeyPrefix = safeKey.slice(0,3); var safeKeyPrefix = safeKey.slice(0,3);
var blobPrefix = blobId.slice(0,2); var blobPrefix = blobId.slice(0,2);
@ -891,10 +905,12 @@ var removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
return void removeOwnedBlob(Env, channelId, unsafeKey, cb); return void removeOwnedBlob(Env, channelId, unsafeKey, cb);
} }
// FIXME METADATA
if (!(Env.msgStore && Env.msgStore.removeChannel && Env.msgStore.getChannelMetadata)) { if (!(Env.msgStore && Env.msgStore.removeChannel && Env.msgStore.getChannelMetadata)) {
return cb("E_NOT_IMPLEMENTED"); return cb("E_NOT_IMPLEMENTED");
} }
// FIXME METADATA
Env.msgStore.getChannelMetadata(channelId, function (e, metadata) { Env.msgStore.getChannelMetadata(channelId, function (e, metadata) {
if (e) { return cb(e); } if (e) { return cb(e); }
if (!(metadata && Array.isArray(metadata.owners))) { return void cb('E_NO_OWNERS'); } if (!(metadata && Array.isArray(metadata.owners))) { return void cb('E_NO_OWNERS'); }

@ -155,6 +155,28 @@ var getChannelMetadata = function (Env, channelId, cb) {
getMetadataAtPath(Env, path, cb); getMetadataAtPath(Env, path, cb);
}; };
// low level method for getting just the dedicated metadata channel
var getDedicatedMetadata = function (env, channelId, handler, cb) {
var metadataPath = mkMetadataPath(env, channelId);
readMessages(metadataPath, function (line) {
if (!line) { return; }
try {
var parsed = JSON.parse(line);
handler(null, parsed);
} catch (e) {
handler(e, line);
}
}, function (err) {
if (err) {
// ENOENT => there is no metadata log
if (err.code === 'ENOENT') { return void cb(); }
// otherwise stream errors?
cb(err);
}
cb();
});
};
var readMetadata = function (env, channelId, handler, cb) { var readMetadata = function (env, channelId, handler, cb) {
/* /*
@ -172,9 +194,6 @@ How to proceed
*/ */
var CB = Once(cb);
var index = 0;
nThen(function (w) { nThen(function (w) {
// returns the first line of a channel, parsed... // returns the first line of a channel, parsed...
getChannelMetadata(env, channelId, w(function (err, data) { getChannelMetadata(env, channelId, w(function (err, data) {
@ -182,33 +201,22 @@ How to proceed
// 'INVALID_METADATA' if it can't parse // 'INVALID_METADATA' if it can't parse
// stream errors if anything goes wrong at a lower level // stream errors if anything goes wrong at a lower level
// ENOENT (no channel here) // ENOENT (no channel here)
return void handler(err, undefined, index++); return void handler(err);
} }
// disregard anything that isn't a map // disregard anything that isn't a map
if (!data || typeof(data) !== 'object' || Array.isArray(data)) { return; } if (!data || typeof(data) !== 'object' || Array.isArray(data)) { return; }
// otherwise it's good. // otherwise it's good.
handler(null, data, index++); handler(null, data);
})); }));
}).nThen(function (w) { }).nThen(function () {
var metadataPath = mkMetadataPath(env, channelId); getDedicatedMetadata(env, channelId, handler, function (err) {
readMessages(metadataPath, function (line) {
if (!line) { return; }
try {
var parsed = JSON.parse(line);
handler(null, parsed, index++);
} catch (e) {
handler(e, line, index++);
}
}, w(function (err) {
if (err) { if (err) {
// ENOENT => there is no metadata log // stream errors?
if (err.code === 'ENOENT') { return void CB(); } return void cb(err);
// otherwise stream errors?
CB(err);
} }
CB(); cb();
})); });
}); });
}; };
@ -841,6 +849,12 @@ module.exports.create = function (
if (!isValidChannelId(channelName)) { return void cb(new Error('EINVAL')); } if (!isValidChannelId(channelName)) { return void cb(new Error('EINVAL')); }
getChannelMetadata(env, channelName, cb); getChannelMetadata(env, channelName, cb);
}, },
// iterate over lines of metadata changes from a dedicated log
readDedicatedMetadata: function (channelName, handler, cb) {
if (!isValidChannelId(channelName)) { return void cb(new Error('EINVAL')); }
getDedicatedMetadata(env, channelName, handler, cb);
},
// iterate over multiple lines of metadata changes // iterate over multiple lines of metadata changes
readChannelMetadata: function (channelName, handler, cb) { readChannelMetadata: function (channelName, handler, cb) {
if (!isValidChannelId(channelName)) { return void cb(new Error('EINVAL')); } if (!isValidChannelId(channelName)) { return void cb(new Error('EINVAL')); }

Loading…
Cancel
Save