mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
This commit is contained in:
61
src/batch.js
Normal file
61
src/batch.js
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var async = require('async'),
|
||||||
|
db = require('./database'),
|
||||||
|
utils = require('../public/src/utils');
|
||||||
|
|
||||||
|
(function(Batch) {
|
||||||
|
|
||||||
|
var DEFAULT_BATCH_SIZE = 100;
|
||||||
|
|
||||||
|
Batch.processSortedSet = function(setKey, process, options, callback) {
|
||||||
|
if (typeof options === 'function') {
|
||||||
|
callback = options;
|
||||||
|
options = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
callback = typeof callback === 'function' ? callback : function(){};
|
||||||
|
options = options || {};
|
||||||
|
|
||||||
|
if (typeof process !== 'function') {
|
||||||
|
return callback(new Error('[[error:process-not-a-function]]'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// custom done condition
|
||||||
|
options.doneIf = typeof options.doneIf === 'function' ? options.doneIf : function(){};
|
||||||
|
|
||||||
|
var batch = options.batch || DEFAULT_BATCH_SIZE;
|
||||||
|
var start = 0;
|
||||||
|
var end = batch;
|
||||||
|
var done = false;
|
||||||
|
|
||||||
|
async.whilst(
|
||||||
|
function() {
|
||||||
|
return !done;
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
|
db.getSortedSetRange(setKey, start, end, function(err, ids) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
if (!ids.length || options.doneIf(start, end, ids)) {
|
||||||
|
done = true;
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
process(err, ids, function(err) {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
start += utils.isNumber(options.alwaysStartAt) ? options.alwaysStartAt : batch + 1;
|
||||||
|
end = start + batch;
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
callback
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
}(exports));
|
||||||
@@ -2,27 +2,23 @@
|
|||||||
|
|
||||||
var async = require('async'),
|
var async = require('async'),
|
||||||
db = require('../database'),
|
db = require('../database'),
|
||||||
|
batch = require('../batch'),
|
||||||
threadTools = require('../threadTools');
|
threadTools = require('../threadTools');
|
||||||
|
|
||||||
|
|
||||||
module.exports = function(Categories) {
|
module.exports = function(Categories) {
|
||||||
|
|
||||||
Categories.purge = function(cid, callback) {
|
Categories.purge = function(cid, callback) {
|
||||||
|
batch.processSortedSet('categories:' + cid + ':tid', function(err, tids, next) {
|
||||||
Categories.getTopicIds(cid, 0, -1, function(err, tids) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.eachLimit(tids, 10, function(tid, next) {
|
async.eachLimit(tids, 10, function(tid, next) {
|
||||||
threadTools.purge(tid, 0, next);
|
threadTools.purge(tid, 0, next);
|
||||||
}, function(err) {
|
}, next);
|
||||||
if (err) {
|
}, {alwaysStartAt: 0}, function(err) {
|
||||||
return callback(err);
|
purgeCategory(cid, callback);
|
||||||
}
|
|
||||||
|
|
||||||
purgeCategory(cid, callback);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -32,13 +28,7 @@ module.exports = function(Categories) {
|
|||||||
db.sortedSetRemove('categories:cid', cid, next);
|
db.sortedSetRemove('categories:cid', cid, next);
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
db.delete('categories:' + cid + ':tid', next);
|
db.deleteAll(['categories:' + cid + ':tid', 'categories:recent_posts:cid:' + cid, 'category:' + cid], next);
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.delete('categories:recent_posts:cid:' + cid, next);
|
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.delete('category:' + cid, next);
|
|
||||||
}
|
}
|
||||||
], callback);
|
], callback);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,17 +40,11 @@ module.exports = function(Posts) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel([
|
db.sortedSetsRemove([
|
||||||
function(next) {
|
'tid:' + postData.tid + ':posts',
|
||||||
db.sortedSetRemove('tid:' + postData.tid + ':posts', pid, next);
|
'tid:' + postData.tid + ':posts:votes',
|
||||||
},
|
'uid:' + postData.uid + ':posts'
|
||||||
function(next) {
|
], pid, function(err) {
|
||||||
db.sortedSetRemove('tid:' + postData.tid + ':posts:votes', pid, next);
|
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.sortedSetRemove('uid:' + postData.uid + ':posts', pid, next);
|
|
||||||
}
|
|
||||||
], function(err) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
@@ -101,7 +95,7 @@ module.exports = function(Posts) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var sets = uids.map(function(uid) {
|
var sets = uids.map(function(uid) {
|
||||||
return 'uid:' + uid + ':favourites'
|
return 'uid:' + uid + ':favourites';
|
||||||
});
|
});
|
||||||
|
|
||||||
db.sortedSetsRemove(sets, pid, function(err) {
|
db.sortedSetsRemove(sets, pid, function(err) {
|
||||||
@@ -143,14 +137,10 @@ module.exports = function(Posts) {
|
|||||||
db.sortedSetsRemove(downvoterSets, pid, next);
|
db.sortedSetsRemove(downvoterSets, pid, next);
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
db.delete('pid:' + pid + ':upvote', next);
|
db.deleteAll(['pid:' + pid + ':upvote', 'pid:' + pid + ':downvote'], next);
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.delete('pid:' + pid + ':downvote', next);
|
|
||||||
}
|
}
|
||||||
], callback);
|
], callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -13,7 +13,8 @@ var winston = require('winston'),
|
|||||||
meta = require('./meta'),
|
meta = require('./meta'),
|
||||||
websockets = require('./socket.io'),
|
websockets = require('./socket.io'),
|
||||||
events = require('./events'),
|
events = require('./events'),
|
||||||
plugins = require('./plugins');
|
plugins = require('./plugins'),
|
||||||
|
batch = require('./batch');
|
||||||
|
|
||||||
|
|
||||||
(function(ThreadTools) {
|
(function(ThreadTools) {
|
||||||
@@ -71,26 +72,24 @@ var winston = require('winston'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
ThreadTools.purge = function(tid, uid, callback) {
|
ThreadTools.purge = function(tid, uid, callback) {
|
||||||
async.parallel({
|
batch.processSortedSet('tid:' + tid + ':posts', function(err, pids, next) {
|
||||||
topic: function(next) {
|
async.eachLimit(pids, 10, posts.purge, next);
|
||||||
topics.getTopicFields(tid, ['cid'], next);
|
}, {alwaysStartAt: 0}, function(err) {
|
||||||
},
|
|
||||||
pids: function(next) {
|
|
||||||
topics.getPids(tid, next);
|
|
||||||
}
|
|
||||||
}, function(err, results) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel([
|
topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
|
||||||
function(next) {
|
if (err) {
|
||||||
async.eachLimit(results.pids, 10, posts.purge, next);
|
return callback(err);
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
topics.purge(tid, next);
|
|
||||||
}
|
}
|
||||||
], callback);
|
posts.purge(mainPid, function(err) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
topics.purge(tid, callback);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -44,10 +44,7 @@ module.exports = function(Topics) {
|
|||||||
Topics.removeRecent(tid, next);
|
Topics.removeRecent(tid, next);
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
db.sortedSetRemove('topics:posts', tid, next);
|
db.sortedSetsRemove(['topics:posts', 'topics:views'], tid, next);
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.sortedSetRemove('topics:views', tid, next);
|
|
||||||
}
|
}
|
||||||
], function(err) {
|
], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -90,22 +87,10 @@ module.exports = function(Topics) {
|
|||||||
Topics.purge = function(tid, callback) {
|
Topics.purge = function(tid, callback) {
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function(next) {
|
function(next) {
|
||||||
db.delete('tid:' + tid + ':followers', next);
|
db.deleteAll(['tid:' + tid + ':followers', 'tid:' + tid + ':read_by_uid'], next);
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
db.delete('tid:' + tid + ':read_by_uid', next);
|
db.sortedSetsRemove(['topics:tid', 'topics:recent', 'topics:posts', 'topics:views'], tid, next);
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.sortedSetRemove('topics:tid', tid, next);
|
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
Topics.removeRecent(tid, next);
|
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.sortedSetRemove('topics:posts', tid, next);
|
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.sortedSetRemove('topics:views', tid, next);
|
|
||||||
},
|
},
|
||||||
function(next) {
|
function(next) {
|
||||||
deleteTopicFromCategoryAndUser(tid, next);
|
deleteTopicFromCategoryAndUser(tid, next);
|
||||||
@@ -128,14 +113,7 @@ module.exports = function(Topics) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel([
|
db.sortedSetsRemove(['categories:' + topicData.cid + ':tid', 'uid:' + topicData.uid + ':topics'], tid, function(err) {
|
||||||
function(next) {
|
|
||||||
db.sortedSetRemove('categories:' + topicData.cid + ':tid', tid, next);
|
|
||||||
},
|
|
||||||
function(next) {
|
|
||||||
db.sortedSetRemove('uid:' + topicData.uid + ':topics', tid, next);
|
|
||||||
}
|
|
||||||
], function(err) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user