Function firebase批量更新不适用于大型数据集
我想在一个拥有大量追随者的用户使用GCP云功能发布内容后,向近一百万用户填充一个提要 为了做到这一点,我正在设计将feed的firebase更新分为多个小批量。这是因为我认为如果我不拆分更新,我可能会面临以下问题: i) 在内存中保留一百万用户将超过分配的最大2GB内存 ii)一次性更新一百万个条目将不起作用(更新一百万个条目需要多长时间?) 但是,批量更新只在每次更新调用仅插入大约100个条目时对我有效。当我尝试每批1000个时,只插入了第一批。我想知道这是否是由于: i) 暂停?但是我在日志中没有看到这个错误 ii)当函数超出范围时,保存批处理的数组变量userFeeds{}会被销毁吗 下面是我的代码:Function firebase批量更新不适用于大型数据集,function,firebase,google-cloud-functions,Function,Firebase,Google Cloud Functions,我想在一个拥有大量追随者的用户使用GCP云功能发布内容后,向近一百万用户填充一个提要 为了做到这一点,我正在设计将feed的firebase更新分为多个小批量。这是因为我认为如果我不拆分更新,我可能会面临以下问题: i) 在内存中保留一百万用户将超过分配的最大2GB内存 ii)一次性更新一百万个条目将不起作用(更新一百万个条目需要多长时间?) 但是,批量更新只在每次更新调用仅插入大约100个条目时对我有效。当我尝试每批1000个时,只插入了第一批。我想知道这是否是由于: i) 暂停?但是我在日志
var admin = require('firebase-admin');
var spark = require('./spark');
var user = require('./user');
var Promise = require('promise');
var sparkRecord;
exports.newSpark = function (sparkID) {
var getSparkPromise = spark.getSpark(sparkID);
Promise.all([getSparkPromise]).then(function(result) {
var userSpark = result[0];
sparkRecord = userSpark;
sparkRecord.sparkID = sparkID;
// the batch update only works if the entries per batch is aroud 100 instead of 1000
populateFeedsToFollowers(sparkRecord.uidFrom, 100, null, myCallback);
});
};
var populateFeedsToFollowers = function(uid, fetchSize, startKey, callBack){
var fetchCount = 0;
//retrieving only follower list by batch
user.setFetchLimit(fetchSize);
user.setStartKey(startKey);
//I use this array variable to keep the entries by batch
var userFeeds = {};
user.getFollowersByBatch(uid).then(function(users){
if(users == null){
callBack(null, null, null);
return;
}
//looping thru the followers by batch size
Object.keys(users).forEach(function(userKey) {
fetchCount += 1;
if(fetchCount > fetchSize){
// updating users feed by batch
admin.database().ref().update(userFeeds);
callBack(null, userKey);
fetchCount = 0;
return;
}else{
userFeeds['/userFeed/' + userKey + '/' + sparkRecord.sparkID] = {
phase:sparkRecord.phase,
postTimeIntervalSince1970:sparkRecord.postTimeIntervalSince1970
}
}
});//Object.keys(users).forEach
if(fetchCount > 0){
admin.database().ref().update(userFeeds);
}
});//user.getFollowersByBatch
};
var myCallback = function(err, nextKey) {
if (err) throw err; // Check for the error and throw if it exists.
if(nextKey != null){ //if having remaining followers, keep populating
populateFeedsToFollowers(sparkRecord.uidFrom, 100, nextKey, myCallback);
}
};