From 7fe1fd0d64624fd05ef358c880864fd4cb934f5b Mon Sep 17 00:00:00 2001 From: Dale Harvey Date: Thu, 29 May 2014 12:12:30 +0100 Subject: [PATCH] (#2313) - Remove opts.server option for replication --- docs/api.md | 1 - lib/adapters/http.js | 92 --------------------------------------- lib/replicate.js | 22 ++-------- tests/test.replication.js | 74 ------------------------------- 4 files changed, 3 insertions(+), 186 deletions(-) diff --git a/docs/api.md b/docs/api.md index 8dbd43b18a..76213f2e3c 100644 --- a/docs/api.md +++ b/docs/api.md @@ -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`. diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 35a767e644..29205b1ae2 100644 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -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, ''), diff --git a/lib/replicate.js b/lib/replicate.js index 77d8edfca4..7a3dd97e7a 100644 --- a/lib/replicate.js +++ b/lib/replicate.js @@ -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); diff --git a/tests/test.replication.js b/tests/test.replication.js index d67f8ddca0..fa6a30aec4 100644 --- a/tests/test.replication.js +++ b/tests/test.replication.js @@ -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], @@ -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); - } - } - }); - }); - }); - }); -});