diff --git a/src/database/level.js b/src/database/level.js index b1e80b16c1..e7828e7ffa 100644 --- a/src/database/level.js +++ b/src/database/level.js @@ -2,9 +2,9 @@ (function(module) { /* - * Okay, so LevelDB was made by Google. Therefore its skalable. + * Okay, so LevelDB was made by Google. Therefore it's skalable. * BUT, I created 99% of the rest of NodeBB's expected functionality out of just simple get and set commands. - * Therefore, it is unskalable. + * Therefore, it is unskalable. I totally should have read the docs before starting. * * With much <3, psychobunny. */ @@ -58,15 +58,15 @@ // Exported functions // module.searchIndex = function(key, content, id) { - + // o.O }; module.search = function(key, term, limit, callback) { - + // O.o }; module.searchRemove = function(key, id, callback) { - + // o___O }; module.flushdb = function(callback) { @@ -78,7 +78,8 @@ }; module.info = function(callback) { - + // O____O GIEF FOOD + // v v }; // key @@ -100,19 +101,28 @@ }; module.set = function(key, value, callback) { - db.put(key, value, callback); + if (value === '') { + callback(false); + } else { + db.put(key, value, function(err) { + // uh, err is {}.. why?? + if (typeof callback === 'function') { + callback(null); + } + }); + } }; module.rename = function(oldKey, newKey, callback) { - //db.rename(oldKey, newKey, callback); + // G__G }; module.expire = function(key, seconds, callback) { - //db.expire(key, seconds, callback); + // >__> }; module.expireAt = function(key, timestamp, callback) { - //db.expireat(key, timestamp, callback); + // <__< }; //hashes @@ -132,7 +142,11 @@ }; module.setObjectField = function(key, field, value, callback) { - module.set(key + ':' + field, value, callback); + // can be improved. + module.getObject(key, function(err, obj) { + obj[key] = field; + module.setObject(key, obj, callback); + }); }; module.getObject = function(key, callback) { @@ -225,6 +239,7 @@ module.delete(key + ':' + field, callback); }; + // BEGIN FAILURE: if the obj doesn't exist like redis, it does not create it! module.incrObjectField = function(key, field, callback) { module.get(key + ':' + field, function(err, val) { module.set(key + ':' + field, val + 1, callback); @@ -248,66 +263,76 @@ module.set(key + ':' + field, val - value, callback); }); }; + // END FAILURE: if the obj doesn't exist like redis, it does not create it! // sets module.setAdd = function(key, value, callback) { - db.sadd(key, value, callback); + module.getListRange(key, 0, -1, function(err, set) { + if (!set.indexOf(value)) { + module.listAppend(set, value, callback); + } else { + callback(null, true); // verify if it sends back true on redis? + } + }); }; module.setRemove = function(key, value, callback) { - db.srem(key, value, callback); + module.getListRange(key, 0, -1, function(err, set) { + module.set(key, set.splice(set.indexOf(value), 1), callback); + }); }; module.isSetMember = function(key, value, callback) { - db.sismember(key, value, function(err, result) { - if(err) { - return callback(err); - } - - callback(null, result === 1); + module.getListRange(key, 0, -1, function(err, set) { + callback(err, !!set.indexOf(value)); }); }; module.isSetMembers = function(key, values, callback) { - var multi = db.multi(); - for (var i=0; i= 0 ? stop : list.length + stop)); + } else { + callback(null, []); + } + }); }; }(exports)); diff --git a/tests/database.js b/tests/database.js index 6de00d9013..c28fd8e125 100644 --- a/tests/database.js +++ b/tests/database.js @@ -189,7 +189,7 @@ describe('Test database', function() { ]; async.series(listTasks, function(err, results) { - assert.equal(err, null, 'error in list methods'); + assert.equal(err, null, 'error in list methods: ' + err); assert.ok(results); done(); @@ -197,77 +197,7 @@ describe('Test database', function() { }); -/* it('should not throw err', function(done) { - - function sortedSetAdd(callback) { - db.sortedSetAdd('sortedSet3', 12, 5, function(err, data) { - callback(err, {'sortedSetAdd': data}); - }); - } - - function sortedSetRemove(callback) { - db.sortedSetRemove('sortedSet3', 12, function(err, data) { - callback(err, {'sortedSetRemove': data}); - }); - } - - function getSortedSetRange(callback) { - db.getSortedSetRevRange('sortedSet3', 0, -1, function(err, data) { - callback(err, {'getSortedSetRange': data}); - }); - } - - function getSortedSetRevRangeByScore(callback) { - var args = ['sortedSet2', '+inf', 100, 'LIMIT', 0, 10]; - db.getSortedSetRevRangeByScore(args, function(err, data) { - callback(err, {'getSortedSetRevRangeByScore': data}); - }); - } - - function sortedSetCount(callback) { - db.sortedSetCount('sortedSet3', -Infinity, Infinity, function(err, data) { - callback(err, {'sortedSetCount': data}); - }); - } - - function sortedSetScore(callback) { - db.sortedSetScore('users:joindate', 1, function(err, data) { - callback(err, {'sortedSetScore': data}); - }); - } - - function sortedSetsScore(callback) { - db.sortedSetsScore(['users:joindate', 'users:derp', 'users:postcount'], 1, function(err, data) { - callback(err, {'sortedSetsScore': data}); - }); - } - - var sortedSetTasks = [ - sortedSetAdd, - getSortedSetRange, - sortedSetAdd, - getSortedSetRange, - sortedSetRemove, - getSortedSetRange, - sortedSetCount, - sortedSetScore, - sortedSetsScore, - getSortedSetRevRangeByScore - ]; - - async.series(sortedSetTasks, function(err, results) { - assert.equal(err, null, 'error in sorted set methods'); - assert.ok(results); - - done(); - }); - - }); - - it('should not throw err', function(done) { - - function setAdd(callback) { db.setAdd('myTestSet', 15, function(err, data) { callback(err, {'setAdd': data}); @@ -335,5 +265,71 @@ describe('Test database', function() { done(); }); }); -*/ + + + it('should not throw err', function(done) { + function sortedSetAdd(callback) { + db.sortedSetAdd('sortedSet3', 12, 5, function(err, data) { + callback(err, {'sortedSetAdd': data}); + }); + } + + function sortedSetRemove(callback) { + db.sortedSetRemove('sortedSet3', 12, function(err, data) { + callback(err, {'sortedSetRemove': data}); + }); + } + + function getSortedSetRange(callback) { + db.getSortedSetRevRange('sortedSet3', 0, -1, function(err, data) { + callback(err, {'getSortedSetRange': data}); + }); + } + + function getSortedSetRevRangeByScore(callback) { + var args = ['sortedSet2', '+inf', 100, 'LIMIT', 0, 10]; + db.getSortedSetRevRangeByScore(args, function(err, data) { + callback(err, {'getSortedSetRevRangeByScore': data}); + }); + } + + function sortedSetCount(callback) { + db.sortedSetCount('sortedSet3', -Infinity, Infinity, function(err, data) { + callback(err, {'sortedSetCount': data}); + }); + } + + function sortedSetScore(callback) { + db.sortedSetScore('users:joindate', 1, function(err, data) { + callback(err, {'sortedSetScore': data}); + }); + } + + function sortedSetsScore(callback) { + db.sortedSetsScore(['users:joindate', 'users:derp', 'users:postcount'], 1, function(err, data) { + callback(err, {'sortedSetsScore': data}); + }); + } + + var sortedSetTasks = [ + sortedSetAdd, + getSortedSetRange, + sortedSetAdd, + getSortedSetRange, + sortedSetRemove, + getSortedSetRange, + sortedSetCount, + sortedSetScore, + sortedSetsScore, + getSortedSetRevRangeByScore + ]; + + async.series(sortedSetTasks, function(err, results) { + assert.equal(err, null, 'error in sorted set methods'); + assert.ok(results); + + done(); + }); + + }); });