我正在尝试创建一个 WriteBatch
控制我的数据库中的动态引用之一。我的应用程序有一个简单的 User-Follow-Post-Feed
我希望我的用户在他的订阅源中看到他关注的所有用户的帖子的模型。在研究 Firebase 示例(如 Firefeed )和 Stack Overflow 上的大量帖子后,我正在做什么。
最佳想法是保留一条路径(在本例中为 collection
),我将 Ids
存储在其中我的用户应该在他的提要中看到的帖子,这意味着控制复制并删除他关注/取消关注的所有用户的每个帖子。
我做了我的 Cloud functions
以原子方式保持这一点,并且一切正常,但是当我尝试进行大规模测试时,为用户添加了 5000 多个帖子并试图关注他(查看 Cloud function
需要多少时间),我看到批处理有 500 次操作的限制。所以我所做的是将我的 5000 个 id 分成多个小列表,并为每个列表执行一批,永远不会超过 500 的限制。
但即使这样做,我仍然收到一个错误 I can't do more than 500 operations in a single commit
,我不知道可能是因为批处理同时执行,还是为什么。我想也许我可以一个接一个地连接起来,避免一次全部执行。但我仍然有一些麻烦。所以这就是我提问的原因。
这是我的方法:
fun updateFeedAfterUserfollow(postIds: QuerySnapshot, userId: String) {
//If there is no posts from the followed user, return
if (postIds.isEmpty) return
val listOfPostsId = postIds.map { it.id }
val mapOfInfo = postIds.map { it.id to it.toObject(PublicUserData::class.java) }.toMap()
//Get User ref
val ref = firestore.collection(PRIVATE_USER_DATA).document(userId).collection(FEED)
//Split the list in multiple list to avoid the max 500 operations per batch
val idsPartition = Lists.partition(listOfPostsId, 400)
//Create a batch with max 400 operations and execute it until the whole list have been updated
idsPartition.forEach { Ids ->
val batch = firestore.batch().also { batch ->
Ids.forEach { id -> batch.set(ref.document(id), mapOfInfo[id]!!) }
}
batch.commit().addOnCompleteListener {
if (it.isSuccessful)
Grove.d { "Commit updated successfully" }
else Grove.d { "Commit fail" }
}
}
}
最佳答案
最后这个问题是因为我试图在一个事务中实现这个批处理操作,它最终也像一个批处理。这就是为什么即使我为每 400 个引用生成批处理,这些实例是在事务中创建的,它就像一个超过 500 限制的单个大事务。
我做了一些更改并在我的 GitHub 上的存储库中实现。
//Generate the right amount of batches
const batches = _.chunk(updateReferences, MAX_BATCH_SIZE)
.map(dataRefs => {
const writeBatch = firestoreInstance.batch();
dataRefs.forEach(ref => {
writeBatch.update(ref, 'author', newAuthor);
});
return writeBatch.commit();
});
它写在 typescript 上,但你肯定会理解它:
https://github.com/FrangSierra/firestore-cloud-functions-typescript/blob/master/functions/src/atomic-operations/index.ts
关于firebase - 在 Firestore 数据库中一次执行 500 多项操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46834549/