Permalink
Browse files

(#635) - make comapction use status from rev_tree

Start with idb adapter
  • Loading branch information...
1 parent d71e3b5 commit 0d1dc218d7d37453a5ee1f1d15b146091f8316ef @neojski committed Apr 4, 2013
Showing with 30 additions and 35 deletions.
  1. +28 −14 src/adapters/pouch.idb.js
  2. +2 −21 src/pouch.adapter.js
View
@@ -841,7 +841,6 @@ var IdbPouch = function(opts, callback) {
call(callback, null);
};
- // compaction internal functions
api._getRevisionTree = function(docId, callback) {
var txn = idb.transaction([DOC_STORE], 'readonly');
var req = txn.objectStore(DOC_STORE).get(docId);
@@ -855,24 +854,39 @@ var IdbPouch = function(opts, callback) {
};
};
- api._removeDocRevisions = function(docId, revs, callback) {
- var txn = idb.transaction([BY_SEQ_STORE], IDBTransaction.READ_WRITE);
- var index = txn.objectStore(BY_SEQ_STORE).index('_doc_id_rev');
- revs.forEach(function(rev) {
- var key = docId + "::" + rev;
- index.getKey(key).onsuccess = function(e) {
- var seq = e.target.result;
- if (!seq) {
- return;
+ api._compactDocument = function(docId, 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;
+
+ 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';
+
+ count--;
+ if (!count) {
+ txn.objectStore(DOC_STORE).put(metadata);
+ }
+ };
}
- var req = txn.objectStore(BY_SEQ_STORE)['delete'](seq);
- };
- });
+ });
+ };
txn.oncomplete = function() {
callback();
};
};
- // end of compaction internal functions
return api;
};
View
@@ -56,7 +56,7 @@ var PouchAdapter = function(opts, callback) {
res.forEach(function(doc) {
if (doc.ok) {
// TODO: we need better error handling
- compactDocument(doc.id, 1, decCount);
+ customApi._compactDocument(doc.id, decCount);
} else {
decCount();
}
@@ -195,25 +195,6 @@ 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 nonLeaves = [];
- Object.keys(height).forEach(function(rev) {
- if (height[rev] > max_height) {
- nonLeaves.push(rev);
- }
- });
- customApi._removeDocRevisions(docId, nonLeaves, callback);
- });
- };
-
// compact the whole database using single document
// compaction
api.compact = function(callback) {
@@ -228,7 +209,7 @@ var PouchAdapter = function(opts, callback) {
return;
}
res.results.forEach(function(row) {
- compactDocument(row.id, 0, function() {
+ customApi._compactDocument(row.id, function() {
count--;
if (!count) {
call(callback);

0 comments on commit 0d1dc21

Please sign in to comment.