Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

(#635) - move more logic to generic adapter (auto_compaction)

In order auto_compaction to work I moved modification of the whole
rev_tree into compactDocument in generic adapter. It then sends
revisions which we want to delete and modified rev_tree to adapter
specific method which does the compaction process.
  • Loading branch information...
commit 801633708772a32c82ac42d14d8644481452dca8 1 parent 99b7bd6
@neojski authored
View
42 src/adapters/pouch.idb.js
@@ -854,37 +854,37 @@ var IdbPouch = function(opts, callback) {
};
};
- api._compactDocument = function(docId, callback) {
+ // This function removes revisions of document docId
+ // which are listed in revs and sets this document
+ // revision to to rev_tree
+ api._doCompaction = function(docId, rev_tree, revs, callback) {
var txn = idb.transaction([DOC_STORE, BY_SEQ_STORE], IDBTransaction.READ_WRITE);
var index = txn.objectStore(DOC_STORE);
index.get(docId).onsuccess = function(event) {
var metadata = event.target.result;
+ metadata.rev_tree = rev_tree;
- var count = 0;
- Pouch.merge.traverseRevTree(metadata.rev_tree, function(isLeaf, pos, revHash, ctx, opts) {
- if (!isLeaf && opts.status == 'available') {
- count++;
- var index = txn.objectStore(BY_SEQ_STORE).index('_doc_id_rev');
- var key = docId + "::" + pos + '-' + revHash;
- index.getKey(key).onsuccess = function(e) {
- var seq = e.target.result;
- if (!seq) {
- return;
- }
- var req = txn.objectStore(BY_SEQ_STORE)['delete'](seq);
- opts.status = 'missing';
+ var count = revs.length;
+ revs.forEach(function(rev) {
+ var index = txn.objectStore(BY_SEQ_STORE).index('_doc_id_rev');
+ var key = docId + "::" + rev;
+ index.getKey(key).onsuccess = function(e) {
+ var seq = e.target.result;
+ if (!seq) {
+ return;
+ }
+ var req = txn.objectStore(BY_SEQ_STORE)['delete'](seq);
- count--;
- if (!count) {
- txn.objectStore(DOC_STORE).put(metadata);
- }
- };
- }
+ count--;
+ if (!count) {
+ txn.objectStore(DOC_STORE).put(metadata);
+ }
+ };
});
};
txn.oncomplete = function() {
- callback();
+ call(callback);
};
};
View
13 src/adapters/pouch.leveldb.js
@@ -734,19 +734,10 @@ var LevelPouch = function(opts, callback) {
});
};
- api._compactDocument = function(docId, callback) {
-
+ api._doCompaction = function(docId, rev_tree, revs, callback) {
stores[DOC_STORE].get(docId, function(err, metadata) {
-
var seqs = metadata.rev_map; // map from rev to seq
-
- var revs = [];
- Pouch.merge.traverseRevTree(metadata.rev_tree, function(isLeaf, pos, revHash, ctx, opts) {
- if (!isLeaf && opts.status == 'available') {
- revs.push(pos + '-' + revHash);
- opts.status = 'missing';
- }
- });
+ metadata.rev_tree = rev_tree;
var count = revs.length;
function done() {
View
11 src/adapters/pouch.websql.js
@@ -680,7 +680,7 @@ var webSqlPouch = function(opts, callback) {
});
};
- api._compactDocument = function(docId, callback) {
+ api._doCompaction = function(docId, rev_tree, revs, callback) {
db.transaction(function (tx) {
var sql = 'SELECT json AS metadata FROM ' + DOC_STORE + ' WHERE id = ?';
tx.executeSql(sql, [docId], function(tx, result) {
@@ -688,14 +688,7 @@ var webSqlPouch = function(opts, callback) {
return call(callback);
}
var metadata = JSON.parse(result.rows.item(0).metadata);
- var revs = [];
-
- Pouch.merge.traverseRevTree(metadata.rev_tree, function(isLeaf, pos, revHash, ctx, opts) {
- if (!isLeaf && opts.status == 'available') {
- revs.push(pos + '-' + revHash);
- opts.status = 'missing';
- }
- });
+ metadata.rev_tree = rev_tree;
var sql = 'DELETE FROM ' + BY_SEQ_STORE + ' WHERE doc_id_rev IN (' +
revs.map(function(rev){return quote(docId + '::' + rev);}).join(',') + ')';
View
32 src/pouch.adapter.js
@@ -56,7 +56,7 @@ var PouchAdapter = function(opts, callback) {
res.forEach(function(doc) {
if (doc.ok) {
// TODO: we need better error handling
- customApi._compactDocument(doc.id, decCount);
+ compactDocument(doc.id, 1, decCount);
} else {
decCount();
}
@@ -195,6 +195,34 @@ var PouchAdapter = function(opts, callback) {
});
};
+ // compact one document and fire callback
+ // by compacting we mean removing all revisions which
+ // are further from the leaf in revision tree than max_height
+ var compactDocument = function(docId, max_height, callback) {
+ customApi._getRevisionTree(docId, function(err, rev_tree){
+ if (err) {
+ return call(callback);
+ }
+ var height = computeHeight(rev_tree);
+ var candidates = [];
+ var revs = [];
+ Object.keys(height).forEach(function(rev) {
+ if (height[rev] > max_height) {
+ candidates.push(rev);
+ }
+ });
+
+ Pouch.merge.traverseRevTree(rev_tree, function(isLeaf, pos, revHash, ctx, opts) {
+ var rev = pos + '-' + revHash;
+ if (opts.status === 'available' && candidates.indexOf(rev) !== -1) {
+ opts.status = 'missing';
+ revs.push(rev);
+ }
+ });
+ customApi._doCompaction(docId, rev_tree, revs, callback);
+ });
+ };
+
// compact the whole database using single document
// compaction
api.compact = function(callback) {
@@ -209,7 +237,7 @@ var PouchAdapter = function(opts, callback) {
return;
}
res.results.forEach(function(row) {
- customApi._compactDocument(row.id, function() {
+ compactDocument(row.id, 0, function() {
count--;
if (!count) {
call(callback);
Please sign in to comment.
Something went wrong with that request. Please try again.