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

查看:149
本文介绍了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中,可以选择使用

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:

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

JS,Erlang和Mango过滤的时间增量与文档数成线性关系.这些过滤机制未使用任何索引.文档ID过滤是恒定的,因为它基于_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天全站免登陆