better sets in mongo, should change sorted sets too using sort

v1.18.x
Baris Soner Usakli 11 years ago
parent 7c4347736c
commit 9b557cafd8

@ -267,17 +267,15 @@
// sets // sets
module.setAdd = function(key, value, callback) { module.setAdd = function(key, value, callback) {
db.collection('objects').update({_key:key}, {$addToSet: { members: value }}, {upsert:true, w: 1}, function(err, result) {
var data = { if(callback) {
value:value callback(err, result);
}; }
});
data.setName = key;
module.setObject(key + ':' + value, data, callback);
} }
module.setRemove = function(key, value, callback) { module.setRemove = function(key, value, callback) {
db.collection('objects').remove({setName:key, value:value}, function(err, result) { db.collection('objects').update({_key:key, members: value}, {$pull : {members: value}}, function(err, result) {
if(callback) { if(callback) {
callback(err, result); callback(err, result);
} }
@ -285,7 +283,7 @@
} }
module.isSetMember = function(key, value, callback) { module.isSetMember = function(key, value, callback) {
db.collection('objects').findOne({setName:key, value:value}, function(err, item) { db.collection('objects').findOne({_key:key, members: value}, function(err, item) {
callback(err, item !== null && item !== undefined); callback(err, item !== null && item !== undefined);
}); });
} }
@ -302,7 +300,7 @@
} }
module.getSetMembers = function(key, callback) { module.getSetMembers = function(key, callback) {
db.collection('objects').find({setName:key}).toArray(function(err, data) { db.collection('objects').findOne({_key:key}, {members:1}, function(err, data) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
@ -310,29 +308,26 @@
if(!data) { if(!data) {
callback(null, []); callback(null, []);
} else { } else {
data = data.map(function(item) { callback(null, data.members);
return item.value;
});
callback(null, data);
} }
}); });
} }
module.setCount = function(key, callback) { module.setCount = function(key, callback) {
db.collection('objects').count({setName:key}, function(err, count) { db.collection('objects').findOne({_key:key}, function(err, data) {
if(err) { if(err) {
return callback(err); return callback(err);
} }
if(!data) {
if(!count) {
return callback(null, 0); return callback(null, 0);
} }
callback(null,count);
callback(null, data.members.length);
}); });
} }
module.setRemoveRandom = function(key, callback) { module.setRemoveRandom = function(key, callback) {
db.collection('objects').find({setName:key}).toArray(function(err, data) { db.collection('objects').findOne({_key:key}, function(err, data) {
if(err) { if(err) {
if(callback) { if(callback) {
return callback(err); return callback(err);
@ -346,11 +341,11 @@
callback(null, 0); callback(null, 0);
} }
} else { } else {
var randomIndex = Math.floor(Math.random() * data.length); var randomIndex = Math.floor(Math.random() * data.members.length);
var item = data[randomIndex]; var value = data.members[randomIndex];
module.setRemove(item.setName, item.value, function(err, result) { module.setRemove(data._key, value, function(err, result) {
if(callback) { if(callback) {
callback(err, item.value); callback(err, value);
} }
}); });
} }

@ -250,14 +250,14 @@ var DebugRoute = function(app) {
function setAdd(callback) { function setAdd(callback) {
db.setAdd('myTestSet', 5, function(err, data) { db.setAdd('myTestSet', 15, function(err, data) {
console.log('setAdd return', data); console.log('setAdd return', data);
callback(err, {'setAdd': data}); callback(err, {'setAdd': data});
}); });
} }
function setRemove(callback) { function setRemove(callback) {
db.setRemove('myTestSet', 11, function(err, data) { db.setRemove('myTestSet', 15, function(err, data) {
console.log('setRemove return', data); console.log('setRemove return', data);
callback(err, {'setRemove': data}); callback(err, {'setRemove': data});
}); });
@ -271,14 +271,14 @@ var DebugRoute = function(app) {
} }
function isSetMember(callback) { function isSetMember(callback) {
db.isSetMember('myTestSet', 5, function(err, data) { db.isSetMember('myTestSet', 15, function(err, data) {
console.log('isSetMember return', data); console.log('isSetMember return', data);
callback(err, {'isSetMember': data}); callback(err, {'isSetMember': data});
}); });
} }
function isMemberOfSets(callback) { function isMemberOfSets(callback) {
db.isMemberOfSets(['doesntexist', 'myTestSet', 'nonexistingSet'], 5, function(err, data) { db.isMemberOfSets(['doesntexist', 'myTestSet', 'nonexistingSet'], 15, function(err, data) {
console.log('isMemberOfSets return', data); console.log('isMemberOfSets return', data);
callback(err, {'isMemberOfSets': data}); callback(err, {'isMemberOfSets': data});
}); });
@ -291,6 +291,13 @@ var DebugRoute = function(app) {
}); });
} }
function setCount(callback) {
db.setCount('myTestSet', function(err, data) {
console.log('setCount return', data);
callback(err, {'setCount': data});
});
}
var objectTasks = [ var objectTasks = [
@ -345,8 +352,15 @@ var DebugRoute = function(app) {
getSetMembers, getSetMembers,
setAdd, setAdd,
getSetMembers, getSetMembers,
setRemove,
getSetMembers,
isSetMember,
setAdd,
getSetMembers,
isSetMember, isSetMember,
setRemoveRandom,
getSetMembers, getSetMembers,
setCount,
isMemberOfSets isMemberOfSets
]; ];

Loading…
Cancel
Save