Skip to content

Commit

Permalink
(#2313) - Remove opts.server option for replication
Browse files Browse the repository at this point in the history
  • Loading branch information
daleharvey committed May 31, 2014
1 parent 5495d36 commit 7fe1fd0
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 186 deletions.
1 change: 0 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ All options default to `false` unless otherwise specified.
* `options.doc_ids`: Only replicate docs with these ids.
* `options.live`: If `true`, starts subscribing to future changes in the `source` database and continue replicating them.
* `options.since`: Replicate changes after the given sequence number.
* `options.server`: Initialize the replication on the server. The response is the CouchDB `POST _replicate` response and is different from the PouchDB replication response. Also, `options.onChange` is not supported on server replications.
* `options.create_target`: Create target database if it does not exist. Only for server replications.
* `options.batch_size`: Number of documents to process at a time. Defaults to 100. This affects the number of docs held in memory and the number sent at a time to the target server. You may need to adjust downward if targeting devices with low amounts of memory (e.g. phones) or if the documents are large in size (e.g. with attachments). If your documents are small in size, then increasing this number will probably speed replication up.
* `options.batches_limit`: Number of batches to process at a time. Defaults to 10. This (along wtih `batch_size`) controls how many docs are kept in memory at a time, so the maximum docs in memory at once would equal `batch_size` × `batches_limit`.
Expand Down
92 changes: 0 additions & 92 deletions lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,98 +872,6 @@ function HttpPouch(opts, callback) {
callback();
};

function replicateOnServer(target, opts, promise, targetHostUrl) {
opts = utils.clone(opts);
var targetHost = api.getHost(targetHostUrl);
var params = {
source: host.db,
target: targetHost.protocol === host.protocol &&
targetHost.authority ===
host.authority ? targetHost.db : targetHost.source
};

if (opts.continuous) {
params.continuous = true;
}

if (opts.create_target) {
params.create_target = true;
}

if (opts.doc_ids) {
params.doc_ids = opts.doc_ids;
}

if (opts.filter && typeof opts.filter === 'string') {
params.filter = opts.filter;
}

if (opts.query_params) {
params.query_params = opts.query_params;
}

var result = {};
var repOpts = {
headers: host.headers,
method: 'POST',
url: genUrl(host, '_replicate'),
body: params
};

var xhr;
promise.cancel = function () {
this.cancelled = true;
if (xhr && !result.ok) {
xhr.abort();
}
if (result._local_id) {
repOpts.body = {
replication_id: result._local_id
};
}
repOpts.body.cancel = true;
ajax(repOpts, function (err, resp, xhr) {
// If the replication cancel request fails, send an error to the
// callback
if (err) {
return callback(err);
}
// Send the replication cancel result to the complete callback
utils.call(opts.complete, null, result, xhr);
});
};

if (promise.cancelled) {
return;
}

xhr = ajax(repOpts, function (err, resp, xhr) {
// If the replication fails, send an error to the callback
if (err) {
return callback(err);
}

result.ok = true;

// Provided by CouchDB from 1.2.0 onward to cancel replication
if (resp._local_id) {
result._local_id = resp._local_id;
}

// Send the replication result to the complete callback
utils.call(opts.complete, null, resp, xhr);
});
}

api.replicateOnServer = function (target, opts, promise) {
if (!api.taskqueue.isReady) {
api.taskqueue.addTask('replicateOnServer', [target, opts, promise]);
return promise;
}
target.info(function (err, info) {
replicateOnServer(target, opts, promise, info.host);
});
};
api.destroy = utils.adapterFun('destroy', function (callback) {
ajax({
url: genDBUrl(host, ''),
Expand Down
22 changes: 3 additions & 19 deletions lib/replicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,25 +551,9 @@ function replicateWrapper(src, target, opts, callback) {
var replicateRet = new Replication(opts);
toPouch(src).then(function (src) {
return toPouch(target).then(function (target) {
if (opts.server) {
if (typeof src.replicateOnServer !== 'function') {
throw new TypeError(
'Server replication not supported for ' + src.type() + ' adapter'
);
}
if (src.type() !== target.type()) {
throw new TypeError('Server replication' +
' for different adapter types (' +
src.type() + ' and ' + target.type() + ') is not supported'
);
}
src.replicateOnServer(target, opts, replicateRet);
} else {
return genReplicationId(src, target, opts).then(function (repId) {
replicateRet.emit('id', repId);
replicate(repId, src, target, opts, replicateRet);
});
}
return genReplicationId(src, target, opts).then(function (repId) {
replicate(repId, src, target, opts, replicateRet);
});
});
}).catch(function (err) {
replicateRet.emit('error', err);
Expand Down
74 changes: 0 additions & 74 deletions tests/test.replication.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ if ('saucelabs' in testUtils.params()) {
}

var downAdapters = ['local'];
var interHTTPAdapters = [['http', 'http']];

adapters.forEach(function (adapters) {
describe('test.replication.js-' + adapters[0] + '-' + adapters[1],
Expand Down Expand Up @@ -1745,76 +1744,3 @@ downAdapters.map(function (adapter) {

});
});


// Server side replication via `server: true` between http

interHTTPAdapters.map(function (adapters) {

describe('test.replication.js-server', function () {

var dbs = {};

beforeEach(function (done) {
dbs.name = testUtils.adapterUrl(adapters[0], 'test_repl');
dbs.remote = testUtils.adapterUrl(adapters[1], 'test_repl_remote');
testUtils.cleanup([dbs.name, dbs.remote], done);
});

afterEach(function (done) {
testUtils.cleanup([dbs.name, dbs.remote], done);
});

var docs = [
{_id: '0', integer: 0, string: '0'},
{_id: '1', integer: 1, string: '1'},
{_id: '2', integer: 2, string: '2'}
];

it('Test basic replication', function (done) {
var db = new PouchDB(dbs.name);
db.bulkDocs({ docs: docs }, {}, function (err, results) {
PouchDB.replicate(dbs.name, dbs.remote, {server: true },
function (err, result) {
result.ok.should.equal(true);
result.history[0].docs_written.should.equal(docs.length);
done();
});
});
});

it('Test cancel live replication', function (done) {
var db = new PouchDB(dbs.name);
var remote = new PouchDB(dbs.remote);
var doc1 = {_id: 'adoc', foo: 'bar'};
var doc2 = {_id: 'anotherdoc', foo: 'baz'};
remote.bulkDocs({ docs: docs }, {}, function (err, results) {
var count = 0;
var replicate = db.replicate.from(dbs.remote, {
server: true,
live: true
});
var changes = db.changes({
live: true,
onChange: function (change) {
++count;
if (count === 3) {
remote.put(doc1);
}
if (count === 4) {
replicate.cancel();
remote.put(doc2);
// This setTimeout is needed to ensure no further changes come
// through
setTimeout(function () {
count.should.equal(4);
changes.cancel();
done();
}, 500);
}
}
});
});
});
});
});

0 comments on commit 7fe1fd0

Please sign in to comment.