update metadata queries to include edits

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

@ -86,9 +86,8 @@ var handleCommand = function (meta, line) {
Meta.createLineHandler = function (ref, errorHandler) {
ref.meta = {};
errorHandler = errorHandler;
return function (err, line, index) {
var index = 0;
return function (err, line) {
if (err) {
return void errorHandler('METADATA_HANDLER_LINE_ERR', {
error: err,
@ -98,11 +97,12 @@ Meta.createLineHandler = function (ref, errorHandler) {
}
if (Array.isArray(line)) {
index++;
try {
handleCommand(ref.meta, line);
} catch (err) {
} catch (err2) {
errorHandler("METADATA_COMMAND_ERR", {
error: err.stack,
error: err2.stack,
line: line,
});
}
@ -110,6 +110,7 @@ Meta.createLineHandler = function (ref, errorHandler) {
}
if (index === 0 && typeof(line) === 'object') {
index++;
// special case!
ref.meta = line;
return;
@ -117,7 +118,7 @@ Meta.createLineHandler = function (ref, errorHandler) {
errorHandler("METADATA_HANDLER_WEIRDLINE", {
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) {
if (!isValidId(channel)) { return void cb('INVALID_CHAN'); }
if (channel.length === 32) {
if (typeof(Env.msgStore.getChannelMetadata) !== 'function') {
return cb('GET_CHANNEL_METADATA_UNSUPPORTED');
if (channel.length !== 32) { return cb("INVALID_CHAN"); }
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);
});
return void Env.msgStore.getChannelMetadata(channel, function (e, data) {
if (e) {
if (e.code === 'INVALID_METADATA') { return void cb(void 0, {}); }
return void cb(e.code);
}
cb(void 0, data);
});
}
/*
// FIXME METADATA
return void Env.msgStore.getChannelMetadata(channel, function (e, data) {
if (e) {
if (e.code === 'INVALID_METADATA') { return void cb(void 0, {}); }
return void cb(e.code);
}
cb(void 0, data);
});*/
};
var getMultipleFileSize = function (Env, channels, cb) {
@ -802,10 +813,12 @@ var clearOwnedChannel = function (Env, channelId, unsafeKey, cb) {
return cb('INVALID_ARGUMENTS');
}
// FIXME METADATA
if (!(Env.msgStore && Env.msgStore.getChannelMetadata)) {
return cb('E_NOT_IMPLEMENTED');
}
// FIXME METADATA
Env.msgStore.getChannelMetadata(channelId, function (e, metadata) {
if (e) { return cb(e); }
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) {
// FIXME METADATA
var safeKey = escapeKeyCharacters(unsafeKey);
var safeKeyPrefix = safeKey.slice(0,3);
var blobPrefix = blobId.slice(0,2);
@ -891,10 +905,12 @@ var removeOwnedChannel = function (Env, channelId, unsafeKey, cb) {
return void removeOwnedBlob(Env, channelId, unsafeKey, cb);
}
// FIXME METADATA
if (!(Env.msgStore && Env.msgStore.removeChannel && Env.msgStore.getChannelMetadata)) {
return cb("E_NOT_IMPLEMENTED");
}
// FIXME METADATA
Env.msgStore.getChannelMetadata(channelId, function (e, metadata) {
if (e) { return cb(e); }
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);
};
// 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) {
/*
@ -172,9 +194,6 @@ How to proceed
*/
var CB = Once(cb);
var index = 0;
nThen(function (w) {
// returns the first line of a channel, parsed...
getChannelMetadata(env, channelId, w(function (err, data) {
@ -182,33 +201,22 @@ How to proceed
// 'INVALID_METADATA' if it can't parse
// stream errors if anything goes wrong at a lower level
// ENOENT (no channel here)
return void handler(err, undefined, index++);
return void handler(err);
}
// disregard anything that isn't a map
if (!data || typeof(data) !== 'object' || Array.isArray(data)) { return; }
// otherwise it's good.
handler(null, data, index++);
handler(null, data);
}));
}).nThen(function (w) {
var metadataPath = mkMetadataPath(env, channelId);
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) {
}).nThen(function () {
getDedicatedMetadata(env, channelId, handler, function (err) {
if (err) {
// ENOENT => there is no metadata log
if (err.code === 'ENOENT') { return void CB(); }
// otherwise stream errors?
CB(err);
// stream errors?
return void cb(err);
}
CB();
}));
cb();
});
});
};
@ -841,6 +849,12 @@ module.exports.create = function (
if (!isValidChannelId(channelName)) { return void cb(new Error('EINVAL')); }
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
readChannelMetadata: function (channelName, handler, cb) {
if (!isValidChannelId(channelName)) { return void cb(new Error('EINVAL')); }

Loading…
Cancel
Save