CouchDB 和 PouchDB 之间的过滤同步 [英] Filtered Sync between CouchDB and PouchDB

查看:20
本文介绍了CouchDB 和 PouchDB 之间的过滤同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在考虑在我要编写的下一个应用程序中使用 CouchDB 2 和 PouchDB 7.基本上,我将在中央存储中拥有一个 CouchDB,Web 客户端和移动应用程序将启动一个能够思考的 PouchDB.基本上这就像一个魅力.

I am currently thinking about using CouchDB 2 and PouchDB 7 in my next app I want to write. Basically I will have a CouchDB in a central storage and web clients and mobile apps will start a PouchDB that thinks. Basically this works like a charm.

但是...如果应该根据文档所有权进行过滤,我该如何在 CouchDB 和 PouchDB 之间进行过滤同步?

But... How do I do filtered sync between CouchDB and PouchDB if the filter should be done based on document ownership?

我知道每个用户数据库的解决方案.但我的文档将由文档的创建者和他/她添加为读者或作者的人共享访问权限.

I know about the solutions with per user database. But my documents will have shared access by the creator of a documents and people he/she adds as reader or writer.

2018 年有针对此问题的解决方案吗?早在 2016 年,我就无法解决这个问题并放弃了这个应用程序的想法.

Any solutions in 2018 for this problem? Back in 2016 I was unable to solve this issue and dropped the app idea.

推荐答案

您应该在您的文档中包含限制对文档的访问权限、所有权、授权用户所需的信息.

You should include in your documents the information that you require to restrict the access to the document, ownership, authorized users.

基于此信息,CouchDB 和 PouchDB 之间的过滤复制定义有两个选项(检查过滤选项).

Based on this information, there are two options for filtered replication definition between CouchDB and PouchDB (check filtering options).

  1. 基于 CouchDB 设计文档中定义的 JavaScript 过滤器函数.过滤功能 允许您实现过滤接受请求期间提供的参数作为 URL 参数或通过 req 参数在 CouchDB 中进行身份验证的用户的逻辑.

  1. Based on JavaScript filter functions defined in CouchDB design documents. Filter functions allow you to implement your filtering logic that accept parameters provided during the request as URL parameters or the user that is authenticated in CouchDB via the req parameter.

这种方法的主要问题是,只要数据库增长,您就会注意到性能下降.过滤器应用于数据库中的每个文档,甚至是已删除的文档,以产生结果.因此,如果您预见到数据库中有大量文档,我不推荐这种过滤机制.在这里,您可以看到此类问题的示例.

The main problem with this approach is that you will notice a performance degradation as long as your database grows. The filter is applied to every doc, even deleted ones, in the database in order to produce a result. So I do not recommend this filtering mechanism if you foresee that you will have a significant number of docs in the database. Here you have a sample of this kind of problems.

对这个性能问题的一个小改进是用 Erlang 编写过滤逻辑,这比 JS 选项复杂一点,在我的测试中,我并没有从中获得很大的收获.

A lite improvement over this performance problem is to write your filtering logic in Erlang, which is a bit more complex than the JS option, and during my tests I didn't manage to have a big gain with this.

在 CouchDB 2.x 中,可以选择使用 选择器.选择器可以被索引,并且据报道比 JS 过滤器快 10 倍.选择器完全由客户端定义,不基于数据库中的身份验证上下文.此选项的扩展性比前一个要好得多.

In CouchDB 2.x there is the option of perform filtered replication using selectors. Selectors can be indexed and are reported to be 10x faster than JS filters. Selectors are completely defined by the client and are not based on the authentication context in the database. This option scales much better than the previous one.

无论如何,过滤允许您在复制过程中进行一些数据库分段,但它不是文档级读取权限的安全机制.

In any case, filtering allows you to do some database segmentation during the replication process but it is not a security mechanism for document-level read permissions.

文档写入权限可以使用 验证文档更新函数.<小时>更新我重新访问了这个答案,试图提供有关数据库过滤机制的更精确信息.我已经测试了不同过滤方法的性能,试图确认答案.

Document write permissions can be achieved using validate document update functions.


UPDATE I revisited this answer trying to offer more precise information about database filtering mechanisms. I've tested the performance of the different filtering approaches trying to confirm the answer statements.

我加载了一个包含 9000 个文档的数据库,并使用四种技术对 _changes 提要过滤进行了时间测量:JS 过滤、Erlang 过滤、Mango 选择器过滤和 Doc id 过滤,结果如下:

I loaded a database with 9000 docs and I performed time measurements of the _changes feed filtering using four techniques: JS filtering, Erlang filtering, Mango selectors filtering and Doc id filtering with the following results:

  • 9000 个文档的 JS 过滤 - 4.3 秒
  • Erlang 过滤 9000 个文档 - 2.3 秒
  • 芒果选择器过滤 9000 个文档 - 0.48 秒
  • 过滤 9000 个文档的文档 ID - 0.01 秒

测试证实 JS 过滤是更糟糕的选择,因为它需要在外部进程中评估过滤条件,这会引入额外的开销.Erlang 和 Mango 表达式在过滤过程中进行评估,这代表了真正的性能提升.

The test confirms that the JS filtering is the worse option as it needs to evaluate filter condition in an external process which introduces and additional overhead. Erlang and Mango expressions are evaluated inside the filtering process which represents a real performance gain.

为了验证文档数量对过滤的影响,我创建了一个包含 20.000 个文档的数据库,并执行了相同的测试,结果如下:

In order to verify the impact of the number of docs over filtering, I created a database with 20.000 docs and I performed the same tests with the following results:

  • 20000 个文档的 JS 过滤 - 10 秒
  • Erlang 过滤 20.000 个文档 - 5.45 秒
  • Mango 选择器过滤 20000 个文档 - 1.07 秒
  • 过滤 20.000 个文档的文档 ID - 0.01 秒

JS、Erlang 和 Mango 过滤时间增量与文档数成线性关系.这些过滤机制不使用索引.Doc ids 过滤是恒定的,因为它基于 _id 索引.

JS, Erlang and Mango filtering the time increment is linear to the # of docs. No index are used for these filtering mechanisms. Doc ids filtering is constant as it is based on the _id index.

这篇关于CouchDB 和 PouchDB 之间的过滤同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆