Skip to content

Commit fea52e4

Browse files
committed
feat(cluster)
1 parent fbaf062 commit fea52e4

File tree

7 files changed

+133
-48
lines changed

7 files changed

+133
-48
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
]
5656
},
5757
"dependencies": {
58-
"sporks": "0.0.1"
58+
"couch-slouch": "0.0.3",
59+
"sporks": "0.0.1",
60+
"squadron": "0.0.3"
5961
}
6062
}

scripts/cluster.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
var Slouch = require('couch-slouch'),
4+
squadron = require('squadron');
5+
6+
// params
7+
// source
8+
// target
9+
// concurrency
10+
// skip
11+
var Cluster = function (params) {
12+
this._params = params;
13+
14+
this._slouch = new Slouch(params.source);
15+
16+
if (this._params.concurrency === 1) {
17+
// Don't use a throttler
18+
this._throttler = null;
19+
} else {
20+
// Create a throttler with the specified or default concurrency
21+
var concurrency = this._params.concurrency ? this._params.concurrency : null;
22+
this._throttler = new squadron.Throttler(concurrency);
23+
}
24+
};
25+
26+
Cluster.prototype.replicate = function () {
27+
var self = this;
28+
return self._slouch.db.all().each(function (db) {
29+
return self._replicateDB(db);
30+
}, self._throttler);
31+
};
32+
33+
Cluster.prototype._replicateDB = function (db) {
34+
console.log(db);
35+
};
36+
37+
module.exports = Cluster;

scripts/foo.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

scripts/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('./cluster');

test/browser.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,4 @@ var chai = require('chai');
44
chai.use(require('chai-as-promised'));
55
chai.should();
66

7-
var Foo = require('../scripts/foo');
8-
97
require('./node-and-browser');
10-
11-
describe('browser', function () {
12-
13-
it('should test in only the browser', function () {
14-
// TODO: insert tests that can only be tested in the browser
15-
var foo = new Foo();
16-
return foo.bar().then(function (thing) {
17-
thing.should.eql('yar');
18-
});
19-
});
20-
21-
});

test/node-and-browser.js

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,99 @@
11
'use strict';
22

3-
var Foo = require('../scripts/foo');
3+
var Promise = require('sporks/scripts/promise'),
4+
Cluster = require('../scripts'),
5+
sporks = require('sporks'),
6+
Slouch = require('couch-slouch');
47

58
describe('node and browser', function () {
69

7-
it('should test in both node and the browser', function () {
8-
// TODO: insert tests that can be tested in both node and the browser
9-
var foo = new Foo();
10-
return foo.bar().then(function (thing) {
11-
thing.should.eql('yar');
10+
var slouch = null,
11+
id = 0,
12+
cluster = null;
13+
14+
var data = {
15+
test_db1: [{
16+
_id: '1',
17+
foo: 'bar'
18+
},
19+
{
20+
_id: '2',
21+
yar: 'nar'
22+
},
23+
],
24+
test_db2: [{
25+
_id: '3',
26+
foo: 'star'
27+
},
28+
{
29+
_id: '4',
30+
yar: 'raar'
31+
},
32+
]
33+
};
34+
35+
var createDocs = function (db, docs) {
36+
var promises = [];
37+
docs.forEach(function (doc) {
38+
promises.push(slouch.doc.create(db, doc));
39+
});
40+
return Promise.all(promises);
41+
};
42+
43+
var createDatabase = function (db, docs) {
44+
return slouch.db.create(db).then(function () {
45+
return createDocs(db, docs);
46+
});
47+
};
48+
49+
var createData = function () {
50+
var promises = [];
51+
sporks.each(data, function (docs, db) {
52+
promises.push(createDatabase(db + '_' + id, docs));
53+
});
54+
return Promise.all(promises);
55+
};
56+
57+
var destroyData = function () {
58+
var promises = [];
59+
sporks.each(data, function (docs, db) {
60+
promises.push(slouch.db.destroy(db + '_' + id));
61+
});
62+
return Promise.all(promises);
63+
};
64+
65+
beforeEach(function () {
66+
slouch = new Slouch('http://admin:admin@localhost:5984');
67+
68+
// Use a unique id as back to back creation/deletion of the same DBs can lead to problems
69+
id++;
70+
71+
return createData();
72+
});
73+
74+
afterEach(function () {
75+
return destroyData();
76+
});
77+
78+
it('should replicate', function () {
79+
var cluster = new Cluster({
80+
source: 'http://admin:admin@localhost:5984',
81+
target: 'http://admin:admin@localhost:5984'
1282
});
83+
return cluster.replicate();
1384
});
1485

86+
// it('should skip when replicating', function () {
87+
// var cluster = new Cluster({
88+
// source: 'http://admin:admin@localhost:5984',
89+
// target: 'http://admin:admin@localhost:5984',
90+
// skip: ['_global_changes', '_replicator', '_users']
91+
// });
92+
// return cluster.replicate();
93+
// });
94+
95+
// it('should support custom concurrency when replicating', function () {
96+
//
97+
// });
98+
1599
});

test/node.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,4 @@ var chai = require('chai');
44
chai.use(require('chai-as-promised'));
55
chai.should();
66

7-
var Foo = require('../scripts/foo');
8-
97
require('./node-and-browser');
10-
11-
describe('node', function () {
12-
13-
it('should test only in node', function () {
14-
// TODO: insert tests that can only be tested in node
15-
var foo = new Foo();
16-
return foo.bar().then(function (thing) {
17-
thing.should.eql('yar');
18-
});
19-
});
20-
21-
});

0 commit comments

Comments
 (0)