Permalink
Browse files

(#635) - refactor websql and leveldb adapters

  • Loading branch information...
1 parent 0d1dc21 commit 99b7bd674bc7c0012544aba4e1c70ab6fc23f0b5 @neojski committed Apr 4, 2013
Showing with 57 additions and 22 deletions.
  1. +27 −15 src/adapters/pouch.leveldb.js
  2. +30 −7 src/adapters/pouch.websql.js
@@ -724,7 +724,6 @@ var LevelPouch = function(opts, callback) {
}
};
- // compaction internal functions
api._getRevisionTree = function(docId, callback){
stores[DOC_STORE].get(docId, function(err, metadata) {
if (err) {
@@ -735,34 +734,47 @@ var LevelPouch = function(opts, callback) {
});
};
- api._removeDocRevisions = function(docId, revs, callback) {
- if (!revs.length) {
- callback();
- }
+ api._compactDocument = function(docId, callback) {
+
stores[DOC_STORE].get(docId, function(err, metadata) {
+
var seqs = metadata.rev_map; // map from rev to seq
- var count = revs.count;
+ 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';
+ }
+ });
+
+ var count = revs.length;
function done() {
count--;
if (!count) {
callback();
}
}
- revs.forEach(function(rev) {
- var seq = seqs[rev];
- if (!seq) {
- done();
- return;
- }
- stores[BY_SEQ_STORE].del(seq, function(err) {
- done();
+ if (!count) {
+ callback();
+ }
+
+ stores[DOC_STORE].put(metadata.id, metadata, function() {
+ revs.forEach(function(rev) {
+ var seq = seqs[rev];
+ if (!seq) {
+ done();
+ return;
+ }
+
+ stores[BY_SEQ_STORE].del(seq, function(err) {
+ done();
+ });
});
});
});
};
- // end of compaction internal functions
return api;
};
@@ -665,7 +665,7 @@ var webSqlPouch = function(opts, callback) {
});
}
};
- // comapction internal functions
+
api._getRevisionTree = function(docId, callback) {
db.transaction(function (tx) {
var sql = 'SELECT json AS metadata FROM ' + DOC_STORE + ' WHERE id = ?';
@@ -679,16 +679,39 @@ var webSqlPouch = function(opts, callback) {
});
});
};
- api._removeDocRevisions = function(docId, revs, callback) {
+
+ api._compactDocument = function(docId, callback) {
db.transaction(function (tx) {
- var sql = 'DELETE FROM ' + BY_SEQ_STORE + ' WHERE doc_id_rev IN (' +
- revs.map(function(rev){return quote(docId + '::' + rev);}).join(',') + ')';
- tx.executeSql(sql, [], function(tx, result) {
- callback();
+ var sql = 'SELECT json AS metadata FROM ' + DOC_STORE + ' WHERE id = ?';
+ tx.executeSql(sql, [docId], function(tx, result) {
+ if (!result.rows.length) {
+ 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';
+ }
+ });
+
+ var sql = 'DELETE FROM ' + BY_SEQ_STORE + ' WHERE doc_id_rev IN (' +
+ revs.map(function(rev){return quote(docId + '::' + rev);}).join(',') + ')';
+
+ tx.executeSql(sql, [], function(tx, result) {
+ var sql = 'UPDATE ' + DOC_STORE + ' SET json = ? WHERE id = ?';
+
+ tx.executeSql(sql, [JSON.stringify(metadata), docId], function(tx, result) {
+ callback();
+ }, function(tx, res){
+ console.log(res);
+ });
+ });
});
});
};
- // end of compaction internal functions
return api;
};

0 comments on commit 99b7bd6

Please sign in to comment.