从只读CouchDB复制 [英] Replicating from a read-only couchdb

查看:138
本文介绍了从只读CouchDB复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台服务器,带有一个ouchdb数据库,其中包含我的应用程序需要的信息。它包含几兆字节的内容,一些每天都会更改的文档(添加最新新闻,删除旧文档,更新一些内容)。



该应用程序将在可能的情况下与服务器数据库同步(以使此信息可脱机使用)。



但是,此信息对我的应用程序的每个用户都是全局的-因此,需要限制为严格的只读访问。但是,我读到Sofadb要求对源进行 write 访问,以便有效复制(以便进行检查点)。



性能如何暗示?

解决方案

您可以通过编写 validate_doc_update()来限制写访问权限。 _design文档中的函数。您可以在官方CouchDB文档中找到详细信息。。 p>

例如,此函数将写操作限制为仅管理员:

 函数(newDoc,oldDoc,userCtx, secObj){
//仅允许管理员进行修改。
if(((userCtx.roles.indexOf(’_ admin’)!== -1)){
return;
}
投掷({禁止:‘您不能编辑公共文档。’});
}

将此内容放入 _design / readonly 文档(或您想要的设计文档名称),并将函数命名为 validate_doc_update


每个设计文档只能具有一个validate_doc_update函数,但是您当然可以定义多个设计文档。每当任何人尝试执行写操作时,都会将文档依次发送到所有那些验证功能。


因此,请注意不要将此设计文档复制到普通用户应该能够写入的数据库中。


据我所知,常规复制仍将起作用,因为不会对复制检查点文档执行验证功能(它们具有特殊的/ _local /前缀)。


性能含义:


每次写入文档时都会执行validate函数,即使对于管理员写,也用于复制。根据您的应用程序,您可能会考虑一些特殊的复制策略来提高性能。


您所描述的设置应该不会造成问题,但是:不允许用户写,所以如果他们遇到您的公共数据库的慢速复制,则可以。而且,如果您的公共数据库很少更新,那么也应该不会有任何性能问题。


希望我能帮助您!


I have a server, with a couchdb database that contains information my application needs. It contains a couple megabytes of stuff, a few documents which change on a day-to-day basis (adding latest news, deleting old documents, updating a few).

The application syncs with the servers database when it can (to have this information available offline).

However, this information is global to every user of my application -- so by necessity needs to be restricted to strictly read-only access. However, I read that couchdb requires write access to the source in order to replicate effectively (in order to checkpoint).

What are the performance implications? And are there any ways around this?

解决方案

You can restrict write access by writing a validate_doc_update() function in a _design document. You can find the details in the official CouchDB documentation.

For example, this function would restrict write operations to admins only:

function(newDoc, oldDoc, userCtx, secObj) {
    // allow modifications by admins only.
    if ((userCtx.roles.indexOf('_admin') !== -1)) {
            return;
    }
    throw({forbidden: 'You are not allowed to edit public documents.'});
}

Put this into a _design/readonly document (or whatever name you'd like for the design document), and name the function validate_doc_update.

Each design doc can only have one validate_doc_update function, but you can of course define several design documents. Whenever a write operation is tried by anyone, the document is sent to all those validation functions in sequence. If any function throws an error, the update is rejected.

So be careful not to replicate this design document to databases which normal users should be able to write to!

As far as I know, regular replications will still work, because validation functions are not executed against the replication checkpoint documents (they have the special /_local/ prefix). But you should definitively try this out to be sure.

Performance implications:

The validate functions are executed every time a document write occurs, even for admin writes, also for replications. Depending on your application, you might consider some special strategies for replications to improve performance.

The setup you described shouldn't pose a problem, though: Users are not allowed to write, so it's ok if they experience slow replications to your public database. And if your public database is updated only very seldomly, there shouldn't be any performance problems either.

Hope I could help!

这篇关于从只读CouchDB复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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