firebase - 在 Firestore 数据库中一次执行 500 多项操作

标签 firebase nosql batch-processing google-cloud-functions google-cloud-firestore

我正在尝试创建一个 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/

相关文章:

database - 使用复合索引通过查询参数的多个组合进行查询的最佳方法?

mysql - 大型 MySql 表给服务器带来过多负载

mysql - e 标志和特殊字符在 mysql 中起什么作用?

batch-file - 如何在 CMD 中获取 IPV4 变量并在新的 CMD 窗口中 Ping

java - Aerospike 中 batchRequests 的低性能

firebase - 如何从 flutter 的数据快照中获取特定值?

c# - MongoDB NoRM : query nested objects using Expando

javascript - Firebase Web 获取 Google 和 Facebook 中的提供商数据(性别和生日)

javascript - 'child_added' 和 'value' firebase 之间的区别?

android - 从 Intent 启动 Instrumentation 测试