Fix the lvl storage

This commit is contained in:
Caleb James DeLisle 2016-02-11 12:28:05 +01:00
parent 350c9e6c76
commit dff8143fad
2 changed files with 48 additions and 75 deletions

View File

@ -4,6 +4,8 @@
"version": "0.1.0", "version": "0.1.0",
"dependencies": { "dependencies": {
"express": "~4.10.1", "express": "~4.10.1",
"ws": "^1.0.1" "ws": "^1.0.1",
"level": "~1.4.0",
"nthen": "~0.1.0"
} }
} }

View File

@ -1,77 +1,48 @@
var level=require("level"); var Level = require("level");
var nThen = require('nthen');
module.exports.create = function(conf,cb){ var getIndex = function(db, cName, cb) {
console.log("Loading leveldb"); db.get(cName+'=>index', function(e, out){
if (e) { throw e; }
var db=level(conf.levelPath||'./test.level.db'), cb(parseInt(out));
indices={}, });
Channel={}; };
var makeChannel=function(cName){ var insert = function (db, channelName, content, cb) {
Channel[cName]={ var index;
lastModified:0, nThen(function (waitFor) {
messages:[], getIndex(db, channelName, waitFor(function (i) { index = i; }));
}; }).nThen(function (waitFor) {
}, db.put(channelName+'=>index', ''+index, waitFor(function (e) { if (e) { throw e; } }));
makeIndex=function(cName){ }).nThen(function (waitFor) {
// initializing to negative one means we can increment on inserts db.put(channelName+'=>'+index, content, waitFor(function (e) { if (e) { throw e; } }));
// so we always start from zero. }).nThen(cb);
indices[cName]=-1; };
},
loadIndex=function(cName, out){ var getMessages = function (db, channelName, msgHandler) {
indices[cName]=parseInt(out); var index;
typeof indices[cName] !== 'number' && nThen(function (waitFor) {
console.error("FOUND A NON-NUMERIC INDEX for channel: %s", cName); getIndex(db, channelName, waitFor(function (i) { index = i; }));
}, }).nThen(function (waitFor) {
getIndex=function(cName,f){ var again = function (i) {
if(typeof indices[cName] !== 'undefined'){ db.get(channelName + '=>' + i, waitFor(function (e, out) {
f(indices[cName]); if (e) { throw e; }
}else{ msgHandler(out);
// get and increment the channelIndex if (i < index) { again(i+1); }
db.get(cName+'=>index',function(e,out){ }));
if(e){ };
// it doesn't exist, so initialize it again(0);
makeIndex(cName); });
}else{ };
// it exists. parse it
loadIndex(cName,out); module.exports.create = function (conf, cb) {
} var db = Level(conf.levelPath || './test.level.db');
f(indices[cName]); cb({
}); message: function (channelName, content, cb) {
} insert(db, channelName, content, cb);
}; },
getMessages: function (channelName, msgHandler) {
cb({ getMessages(db, channelName, msgHandler);
message: function(cName,content,cb){ }
getIndex(cName,function(index){
var index = ++indices[cName];
db.put(cName+'=>index', ''+index,function(e){
if(e) console.error(e);
});
db.put(cName+'=>'+index, content, function(err){
if(err){
console.log(err);
}
cb();
});
});
},
getMessages: function(cName, cb){
/* get all messages relating to a channel */
getIndex(cName, function(index){
var last = index,
i = 0,
next = function () {
db.get(cName+'=>'+i, function (e,out) {
if(e) return console.error(e);
cb(out);
if (++i <= last) {
next();
}
});
};
next();
});
},
}); });
}; };