organize dbal sorted code

This commit is contained in:
barisusakli
2016-12-23 14:12:00 +03:00
parent 3935e35f5c
commit b71ab64cf4
10 changed files with 555 additions and 514 deletions

View File

@@ -6,113 +6,10 @@ var utils = require('../../../public/src/utils');
module.exports = function (db, module) {
var helpers = module.helpers.mongo;
module.sortedSetAdd = function (key, score, value, callback) {
callback = callback || helpers.noop;
if (!key) {
return callback();
}
if (Array.isArray(score) && Array.isArray(value)) {
return sortedSetAddBulk(key, score, value, callback);
}
value = helpers.valueToString(value);
db.collection('objects').update({_key: key, value: value}, {$set: {score: parseFloat(score)}}, {upsert:true, w: 1}, function (err) {
if (err && err.message.startsWith('E11000 duplicate key error')) {
return process.nextTick(module.sortedSetAdd, key, score, value, callback);
}
callback(err);
});
};
function sortedSetAddBulk(key, scores, values, callback) {
if (!scores.length || !values.length) {
return callback();
}
if (scores.length !== values.length) {
return callback(new Error('[[error:invalid-data]]'));
}
values = values.map(helpers.valueToString);
var bulk = db.collection('objects').initializeUnorderedBulkOp();
for(var i = 0; i < scores.length; ++i) {
bulk.find({_key: key, value: values[i]}).upsert().updateOne({$set: {score: parseFloat(scores[i])}});
}
bulk.execute(function (err) {
callback(err);
});
}
module.sortedSetsAdd = function (keys, score, value, callback) {
callback = callback || helpers.noop;
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
value = helpers.valueToString(value);
var bulk = db.collection('objects').initializeUnorderedBulkOp();
for(var i = 0; i < keys.length; ++i) {
bulk.find({_key: keys[i], value: value}).upsert().updateOne({$set: {score: parseFloat(score)}});
}
bulk.execute(function (err) {
callback(err);
});
};
module.sortedSetRemove = function (key, value, callback) {
function done(err) {
callback(err);
}
callback = callback || helpers.noop;
if (!key) {
return callback();
}
if (Array.isArray(value)) {
value = value.map(helpers.valueToString);
db.collection('objects').remove({_key: key, value: {$in: value}}, done);
} else {
value = helpers.valueToString(value);
db.collection('objects').remove({_key: key, value: value}, done);
}
};
module.sortedSetsRemove = function (keys, value, callback) {
callback = callback || helpers.noop;
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
value = helpers.valueToString(value);
db.collection('objects').remove({_key: {$in: keys}, value: value}, function (err) {
callback(err);
});
};
module.sortedSetsRemoveRangeByScore = function (keys, min, max, callback) {
callback = callback || helpers.noop;
if (!Array.isArray(keys) || !keys.length) {
return callback();
}
var query = {_key: {$in: keys}};
if (min !== '-inf') {
query.score = {$gte: min};
}
if (max !== '+inf') {
query.score = query.score || {};
query.score.$lte = max;
}
db.collection('objects').remove(query, function (err) {
callback(err);
});
};
require('./sorted/add')(db, module);
require('./sorted/remove')(db, module);
require('./sorted/union')(db, module);
require('./sorted/intersect')(db, module);
module.getSortedSetRange = function (key, start, stop, callback) {
getSortedSetRange(key, start, stop, 1, false, callback);
@@ -188,7 +85,7 @@ module.exports = function (db, module) {
if (!key) {
return callback();
}
if(parseInt(count, 10) === -1) {
if (parseInt(count, 10) === -1) {
count = 0;
}
@@ -212,7 +109,7 @@ module.exports = function (db, module) {
.skip(start)
.sort({score: sort})
.toArray(function (err, data) {
if(err) {
if (err) {
return callback(err);
}
@@ -477,86 +374,6 @@ module.exports = function (db, module) {
});
};
module.sortedSetUnionCard = function (keys, callback) {
if (!Array.isArray(keys) || !keys.length) {
return callback(null, 0);
}
var pipeline = [
{ $match: { _key: {$in: keys} } },
{ $group: { _id: {value: '$value' } } },
{ $group: { _id: null, count: { $sum: 1 } } }
];
var project = { _id: 0, count: '$count' };
pipeline.push({ $project: project });
db.collection('objects').aggregate(pipeline, function (err, data) {
callback(err, Array.isArray(data) && data.length ? data[0].count : 0);
});
};
module.getSortedSetUnion = function (params, callback) {
params.sort = 1;
getSortedSetUnion(params, callback);
};
module.getSortedSetRevUnion = function (params, callback) {
params.sort = -1;
getSortedSetUnion(params, callback);
};
function getSortedSetUnion(params, callback) {
if (!Array.isArray(params.sets) || !params.sets.length) {
return callback();
}
var limit = params.stop - params.start + 1;
if (limit <= 0) {
limit = 0;
}
var aggregate = {};
if (params.aggregate) {
aggregate['$' + params.aggregate.toLowerCase()] = '$score';
} else {
aggregate.$sum = '$score';
}
var pipeline = [
{ $match: { _key: {$in: params.sets}} },
{ $group: { _id: {value: '$value'}, totalScore: aggregate} },
{ $sort: { totalScore: params.sort} }
];
if (params.start) {
pipeline.push({ $skip: params.start });
}
if (limit > 0) {
pipeline.push({ $limit: limit });
}
var project = { _id: 0, value: '$_id.value' };
if (params.withScores) {
project.score = '$totalScore';
}
pipeline.push({ $project: project });
db.collection('objects').aggregate(pipeline, function (err, data) {
if (err || !data) {
return callback(err);
}
if (!params.withScores) {
data = data.map(function (item) {
return item.value;
});
}
callback(null, data);
});
}
module.sortedSetIncrBy = function (key, increment, value, callback) {
callback = callback || helpers.noop;
if (!key) {
@@ -600,26 +417,7 @@ module.exports = function (db, module) {
}
var query = {_key: key};
if (min !== '-') {
if (min.match(/^\(/)) {
query.value = {$gt: min.slice(1)};
} else if (min.match(/^\[/)) {
query.value = {$gte: min.slice(1)};
} else {
query.value = {$gte: min};
}
}
if (max !== '+') {
query.value = query.value || {};
if (max.match(/^\(/)) {
query.value.$lt = max.slice(1);
} else if (max.match(/^\[/)) {
query.value.$lte = max.slice(1);
} else {
query.value.$lte = max;
}
}
buildLexQuery(query, min, max);
db.collection('objects').find(query, {_id: 0, value: 1})
.sort({value: sort})
@@ -640,7 +438,14 @@ module.exports = function (db, module) {
callback = callback || helpers.noop;
var query = {_key: key};
buildLexQuery(query, min, max);
db.collection('objects').remove(query, function (err) {
callback(err);
});
};
function buildLexQuery(query, min, max) {
if (min !== '-') {
if (min.match(/^\(/)) {
query.value = {$gt: min.slice(1)};
@@ -660,11 +465,7 @@ module.exports = function (db, module) {
query.value.$lte = max;
}
}
db.collection('objects').remove(query, function (err) {
callback(err);
});
};
}
module.processSortedSet = function (setKey, process, batch, callback) {
var done = false;
@@ -703,97 +504,4 @@ module.exports = function (db, module) {
);
};
module.sortedSetIntersectCard = function (keys, callback) {
if (!Array.isArray(keys) || !keys.length) {
return callback(null, 0);
}
var pipeline = [
{ $match: { _key: {$in: keys}} },
{ $group: { _id: {value: '$value'}, count: {$sum: 1}} },
{ $match: { count: keys.length} },
{ $group: { _id: null, count: { $sum: 1 } } }
];
db.collection('objects').aggregate(pipeline, function (err, data) {
callback(err, Array.isArray(data) && data.length ? data[0].count : 0);
});
};
module.getSortedSetIntersect = function (params, callback) {
params.sort = 1;
getSortedSetRevIntersect(params, callback);
};
module.getSortedSetRevIntersect = function (params, callback) {
params.sort = -1;
getSortedSetRevIntersect(params, callback);
};
function getSortedSetRevIntersect(params, callback) {
var sets = params.sets;
var start = params.hasOwnProperty('start') ? params.start : 0;
var stop = params.hasOwnProperty('stop') ? params.stop : -1;
var weights = params.weights || [];
var aggregate = {};
if (params.aggregate) {
aggregate['$' + params.aggregate.toLowerCase()] = '$score';
} else {
aggregate.$sum = '$score';
}
var limit = stop - start + 1;
if (limit <= 0) {
limit = 0;
}
var pipeline = [{ $match: { _key: {$in: sets}} }];
weights.forEach(function (weight, index) {
if (weight !== 1) {
pipeline.push({
$project: {
value: 1,
score: {
$cond: { if: { $eq: [ "$_key", sets[index] ] }, then: { $multiply: [ '$score', weight ] }, else: '$score' }
}
}
});
}
});
pipeline.push({ $group: { _id: {value: '$value'}, totalScore: aggregate, count: {$sum: 1}} });
pipeline.push({ $match: { count: sets.length} });
pipeline.push({ $sort: { totalScore: params.sort} });
if (start) {
pipeline.push({ $skip: start });
}
if (limit > 0) {
pipeline.push({ $limit: limit });
}
var project = { _id: 0, value: '$_id.value'};
if (params.withScores) {
project.score = '$totalScore';
}
pipeline.push({ $project: project });
db.collection('objects').aggregate(pipeline, function (err, data) {
if (err || !data) {
return callback(err);
}
if (!params.withScores) {
data = data.map(function (item) {
return item.value;
});
}
callback(null, data);
});
}
};