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
module.setAdd = function(key, value, callback) {
var data = {
value:value
};
data.setName = key;
module.setObject(key + ':' + value, data, callback);
db.collection('objects').update({_key:key}, {$addToSet: { members: value }}, {upsert:true, w: 1}, function(err, result) {
if(callback) {
callback(err, result);
}
});
}
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) {
callback(err, result);
}
@ -285,7 +283,7 @@
}
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);
});
}
@ -302,7 +300,7 @@
}
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) {
return callback(err);
}
@ -310,29 +308,26 @@
if(!data) {
callback(null, []);
} else {
data = data.map(function(item) {
return item.value;
});
callback(null, data);
callback(null, data.members);
}
});
}
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) {
return callback(err);
}
if(!count) {
if(!data) {
return callback(null, 0);
}
callback(null,count);
callback(null, data.members.length);
});
}
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(callback) {
return callback(err);
@ -346,11 +341,11 @@
callback(null, 0);
}
} else {
var randomIndex = Math.floor(Math.random() * data.length);
var item = data[randomIndex];
module.setRemove(item.setName, item.value, function(err, result) {
var randomIndex = Math.floor(Math.random() * data.members.length);
var value = data.members[randomIndex];
module.setRemove(data._key, value, function(err, result) {
if(callback) {
callback(err, item.value);
callback(err, value);
}
});
}

@ -250,14 +250,14 @@ var DebugRoute = function(app) {
function setAdd(callback) {
db.setAdd('myTestSet', 5, function(err, data) {
db.setAdd('myTestSet', 15, function(err, data) {
console.log('setAdd return', data);
callback(err, {'setAdd': data});
});
}
function setRemove(callback) {
db.setRemove('myTestSet', 11, function(err, data) {
db.setRemove('myTestSet', 15, function(err, data) {
console.log('setRemove return', data);
callback(err, {'setRemove': data});
});
@ -271,14 +271,14 @@ var DebugRoute = function(app) {
}
function isSetMember(callback) {
db.isSetMember('myTestSet', 5, function(err, data) {
db.isSetMember('myTestSet', 15, function(err, data) {
console.log('isSetMember return', data);
callback(err, {'isSetMember': data});
});
}
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);
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 = [
@ -345,8 +352,15 @@ var DebugRoute = function(app) {
getSetMembers,
setAdd,
getSetMembers,
setRemove,
getSetMembers,
isSetMember,
setAdd,
getSetMembers,
isSetMember,
setRemoveRandom,
getSetMembers,
setCount,
isMemberOfSets
];

Loading…
Cancel
Save