Skip to content

Commit 681192b

Browse files
committed
feat(cluster): replicate
1 parent fea52e4 commit 681192b

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

scripts/cluster.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var Slouch = require('couch-slouch'),
1111
var Cluster = function (params) {
1212
this._params = params;
1313

14-
this._slouch = new Slouch(params.source);
14+
this._sourceSlouch = new Slouch(params.source);
15+
this._targetSlouch = new Slouch(params.target);
1516

1617
if (this._params.concurrency === 1) {
1718
// Don't use a throttler
@@ -25,13 +26,42 @@ var Cluster = function (params) {
2526

2627
Cluster.prototype.replicate = function () {
2728
var self = this;
28-
return self._slouch.db.all().each(function (db) {
29+
return self._sourceSlouch.db.all().each(function (db) {
2930
return self._replicateDB(db);
3031
}, self._throttler);
3132
};
3233

34+
Cluster.prototype._createDBIfMissing = function (db) {
35+
var self = this;
36+
return self._targetSlouch.db.exists(db).then(function (exists) {
37+
if (!exists) {
38+
return self._targetSlouch.db.create(db);
39+
}
40+
});
41+
};
42+
43+
Cluster.prototype._replicateSecurity = function (db) {
44+
var self = this;
45+
return self._sourceSlouch.security.get(db).then(function (security) {
46+
return self._targetSlouch.security.set(db, security);
47+
});
48+
};
49+
50+
Cluster.prototype._replicateRawDB = function (db) {
51+
return this._sourceSlouch.db.replicate({
52+
source: this._params.source + '/' + db,
53+
target: this._params.target + '/' + db
54+
});
55+
};
56+
3357
Cluster.prototype._replicateDB = function (db) {
34-
console.log(db);
58+
var self = this;
59+
return self._createDBIfMissing(db).then(function () {
60+
// Replicate security first so that security is put in place before data is copied over
61+
return self._replicateSecurity(db);
62+
}).then(function () {
63+
return self._replicateRawDB(db);
64+
});
3565
};
3666

3767
module.exports = Cluster;

test/node-and-browser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ var Promise = require('sporks/scripts/promise'),
88
describe('node and browser', function () {
99

1010
var slouch = null,
11-
id = 0,
12-
cluster = null;
11+
id = 0;
12+
13+
// TODO: also need to test security
1314

1415
var data = {
1516
test_db1: [{

test/node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ chai.use(require('chai-as-promised'));
55
chai.should();
66

77
require('./node-and-browser');
8+
9+
// TODO: use spawn to test CL interface

0 commit comments

Comments
 (0)