One of the greatest features of CouchDB is its replication which allows for great distributed computing. It reminds me when in 1999 I met Erlang language for the first time (Working for a Telco). Erlang is made for distributed computing and so CouchDB which of course is built in Erlang.
I have to say I have successfully tested this in upcoming version 1.1.1 ( built from a branch) Do not try this in 1.1.0.
The example below is based on the document that I have been discussing in the three part tutorial about building a Document Management System (DMS) with CouchDB.
Filtered or selective replication is a two step process:
Deleting a replication document is how you turn off that replication. This is not any different than deleting any other document:
The same applies for getting a list of the current defined "selective replicators". You can use a temporary view like I show here or create a permanent view to list all the replicators:
I have to say I have successfully tested this in upcoming version 1.1.1 ( built from a branch) Do not try this in 1.1.0.
The example below is based on the document that I have been discussing in the three part tutorial about building a Document Management System (DMS) with CouchDB.
Filtered or selective replication is a two step process:
- First create a filter named for example "clientFilter" in a new document called "replicateFilter". This sample filter will reject any client not matching the clientId parameter (step 2 explains what this parameter is about). Any deleted documents will be deleted from the target as well.
1234567891011121314151617181920212223curl -H
'Content-Type: application/json'
-X PUT http:
//127
.0.0.1:5984
/dms4/_design/replicateFilter
-d \
'{
"filters"
:{
"clientFilter"
:"
function
(doc, req) {
if
(doc._deleted) {
return
true
;
}
if
(!doc.clientId) {
return
false
;
}
if
(!req.query.clientId) {
throw(\"Please provide a query parameter clientId.\");
}
if
(doc.clientId == req.query.clientId) {
return
true
;
}
return
false
;
}"
}
}'
- Create a replication document called "by_clientId". This example passes clientId=1 as a parameter to the filter we created in step number 1 ("replicateFilter/clientFilter"). You figured we will end up replicating documents for that client.
12345678910curl -H
'Content-Type: application/json'
-X POST http:
//127
.0.0.1:5984
/_replicator
-d \
'{
"_id"
:
"by_clientId"
,
"source"
:
"dms4"
,
"create_target"
:
true
,
"continuous"
:
true
,
"filter"
:
"replicateFilter/clientFilter"
,
"query_params"
:{
"clientId"
:1}
}'
Deleting a replication document is how you turn off that replication. This is not any different than deleting any other document:
1
2
3
4
|
nestor:~ nestor$ curl -X GET http:
//127
.0.0.1:5984
/_replicator/by_clientId
{
"_id"
:
"by_clientId"
,
"_rev"
:
"5-e177ca7f79d9ba6f91b803a2cb2abc1e"
,
"source"
:
"dms4"
,
"target"
:
"http://couchdb.nestorurquiza.com:5984/dms4"
,
"create_target"
:
true
,
"continuous"
:
true
,
"filter"
:
"replicateFilter/clientFilter"
,
"query_params"
:{
"clientId"
:1},
"_replication_state"
:
"triggered"
,
"_replication_state_time"
:
"2011-10-20T13:09:56-04:00"
,
"_replication_id"
:
"d8dc09e97f4948de0294260dda19fc6f"
}
nestor:~ nestor$ curl -X DELETE http:
//127
.0.0.1:5984
/_replicator/by_clientId
?rev=5-e177ca7f79d9ba6f91b803a2cb2abc1e
{
"ok"
:
true
,
"id"
:
"by_clientId"
,
"rev"
:
"6-0d20d90cbed22837eb3233e2bd8dfb2c"
}
|
The same applies for getting a list of the current defined "selective replicators". You can use a temporary view like I show here or create a permanent view to list all the replicators:
1
2
3
4
5
|
$ curl -X POST http:
//127
.0.0.1:5984
/_replicator/_temp_view
-H
"Content-Type: application/json"
-d '{
"map"
: "
function
(doc) {
emit(null, doc);
}"
}'
|