在 Firestore 数据库中一次执行 500 多个操作

Execute more than 500 operations at once in Firestore Database

我正在尝试创建一个 WriteBatch 来控制我的数据库中的一个动态引用。我的应用程序有一个简单的 User-Follow-Post-Feed 模型,我希望我的用户在他的提要中看到他关注的所有用户的 posts。在研究了 Firebase 示例(如 Firefeed )和 Stack Overflow 上的大量 post 之后,我在做什么。

最佳想法是保留一条路径(在本例中为 collection),我在其中存储我的用户应该在其提要中看到的 post 的 Ids,这意味着控制复制和删除他 follow/unfollow.

所有用户的每个 post

Cloud functions 以原子方式保存它,一切正常,但是当我尝试进行大规模测试时,为用户添加了超过 5000 posts试图跟随他(寻找 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();
        });

它是打字稿写的,但你肯定能看懂: https://github.com/FrangSierra/firestore-cloud-functions-typescript/blob/master/functions/src/atomic-operations/index.ts