Skip to content

Commit

Permalink
(pouchdb#635) - make merging status less explicit
Browse files Browse the repository at this point in the history
If we merge statuses {} and {} we now get {} and not {status: missing}
  • Loading branch information
neojski committed Apr 5, 2013
1 parent 753a23d commit 769f782
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 70 deletions.
2 changes: 0 additions & 2 deletions src/adapters/pouch.websql.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,6 @@ var webSqlPouch = function(opts, callback) {

tx.executeSql(sql, [JSON.stringify(metadata), docId], function(tx, result) {
callback();
}, function(tx, res){
console.log(res);
});
});
});
Expand Down
7 changes: 3 additions & 4 deletions src/pouch.merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ function mergeTree(in_tree1, in_tree2) {
var tree1 = item.tree1;
var tree2 = item.tree2;

if (tree1[1].status === 'available' || tree2[1].status === 'available') {
tree1[1].status = 'available';
} else {
tree1[1].status = 'missing';
if (tree1[1].status || tree2[1].status) {
tree1[1].status = (tree1[1].status === 'available' ||
tree2[1].status === 'available') ? 'available' : 'missing';
}

for (var i = 0; i < tree2[2].length; i++) {
Expand Down
128 changes: 64 additions & 64 deletions tests/test.merge_rev_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@ if (typeof module !== undefined && module.exports) {

qunit('merge rev tree');

var simple = {pos: 1, ids: ['1', {status: 'missing'}, []]};
var two0 = {pos: 1, ids: ['1', {status: 'missing'}, [['2_0', {status: 'missing'}, []]]]};
var two1 = {pos: 1, ids: ['1', {status: 'missing'}, [['2_1', {status: 'missing'}, []]]]};
var newleaf = {pos: 2, ids: ['2_0', {status: 'missing'}, [['3', {status: 'missing'}, []]]]};
var withnewleaf = {pos: 1, ids: ['1', {status: 'missing'}, [['2_0', {status: 'missing'}, [['3', {status: 'missing'}, []]]]]]};
var newbranch = {pos: 1, ids: ['1', {status: 'missing'}, [['2_0', {status: 'missing'}, []], ['2_1', {status: 'missing'}, []]]]};
var newdeepbranch = {pos: 2, ids: ['2_0', {status: 'missing'}, [['3_1', {status: 'missing'}, []]]]};

var stemmededit = {pos: 3, ids: ['3', {status: 'missing'}, []]};
var simple = {pos: 1, ids: ['1', {}, []]};
var two0 = {pos: 1, ids: ['1', {}, [['2_0', {}, []]]]};
var two1 = {pos: 1, ids: ['1', {}, [['2_1', {}, []]]]};
var newleaf = {pos: 2, ids: ['2_0', {}, [['3', {}, []]]]};
var withnewleaf = {pos: 1, ids: ['1', {}, [['2_0', {}, [['3', {}, []]]]]]};
var newbranch = {pos: 1, ids: ['1', {}, [['2_0', {}, []], ['2_1', {}, []]]]};
var newdeepbranch = {pos: 2, ids: ['2_0', {}, [['3_1', {}, []]]]};

var stemmededit = {pos: 3, ids: ['3', {}, []]};
var stemmedconflicts = [simple, stemmededit];

var newbranchleaf = {pos: 1, ids: ['1', {status: 'missing'}, [['2_0', {status: 'missing'}, [['3', {status: 'missing'}, []]]], ['2_1', {status: 'missing'}, []]]]};
var newbranchleafbranch = {pos: 1, ids: ['1', {status: 'missing'}, [['2_0', {status: 'missing'}, [['3', {status: 'missing'}, []], ['3_1', {status: 'missing'}, []]]], ['2_1', {status: 'missing'}, []]]]};
var newbranchleaf = {pos: 1, ids: ['1', {}, [['2_0', {}, [['3', {}, []]]], ['2_1', {}, []]]]};
var newbranchleafbranch = {pos: 1, ids: ['1', {}, [['2_0', {}, [['3', {}, []], ['3_1', {}, []]]], ['2_1', {}, []]]]};

var stemmed2 = [{pos: 1, ids: ['1', {status: 'missing'}, [['2_1', {status: 'missing'}, []]]]},
{pos: 2, ids: ['2_0', {status: 'missing'}, [['3', {status: 'missing'}, []], ['3_1', {status: 'missing'}, []]]]}];
var stemmed2 = [{pos: 1, ids: ['1', {}, [['2_1', {}, []]]]},
{pos: 2, ids: ['2_0', {}, [['3', {}, []], ['3_1', {}, []]]]}];

var stemmed3 = [{pos: 2, ids: ['2_1', {status: 'missing'}, []]},
{pos: 3, ids: ['3', {status: 'missing'}, []]},
{pos: 3, ids: ['3_1', {status: 'missing'}, []]}];
var partialrecover = [{pos: 1, ids: ['1', {status: 'missing'}, [['2_0', {status: 'missing'}, [['3', {status: 'missing'}, []]]]]]},
{pos: 2, ids: ['2_1', {status: 'missing'}, []]},
{pos: 3, ids: ['3_1', {status: 'missing'}, []]}];
var stemmed3 = [{pos: 2, ids: ['2_1', {}, []]},
{pos: 3, ids: ['3', {}, []]},
{pos: 3, ids: ['3_1', {}, []]}];
var partialrecover = [{pos: 1, ids: ['1', {}, [['2_0', {}, [['3', {}, []]]]]]},
{pos: 2, ids: ['2_1', {}, []]},
{pos: 3, ids: ['3_1', {}, []]}];

test('Merging a path into an empty tree is the path', function() {
deepEqual(JSON.stringify(Pouch.merge([], simple, 10)), JSON.stringify({
Expand Down Expand Up @@ -148,10 +148,10 @@ test('Merging unstemmed recovers as much as possible without losing info', funct
test('winningRev returns the longest leaf', function() {
var tree = [
{"pos":1,"ids":[
"bfe70372c90ded1087239e5191984f76", {status: 'missing'}, [
["44d71a718b90e4696c06a90e08912c8f", {status: 'missing'}, []],
["56e657612d55ab1a402dcb281c874f2a", {status: 'missing'}, [
["93c3db16462f656f7172ccabd3cf6cd6", {status: 'missing'}, []]
"bfe70372c90ded1087239e5191984f76", {}, [
["44d71a718b90e4696c06a90e08912c8f", {}, []],
["56e657612d55ab1a402dcb281c874f2a", {}, [
["93c3db16462f656f7172ccabd3cf6cd6", {}, []]
]]
]]
}];
Expand All @@ -164,17 +164,17 @@ test('winningRev returns the longest leaf again', function() {
// this one is from issue #293
var tree = [
{"pos": 1,"ids": [
"203db1a1810a838895d561f67b224b5d", {status: 'missing'}, [
["bf5e08a4f9fa6d33a53f4a00ae3ea399", {status: 'missing'}, [
["28cd77a3ca30f79e1cfffcd6a41ca308", {status: 'missing'}, []]
"203db1a1810a838895d561f67b224b5d", {}, [
["bf5e08a4f9fa6d33a53f4a00ae3ea399", {}, [
["28cd77a3ca30f79e1cfffcd6a41ca308", {}, []]
]]
]
]},
{"pos": 1,"ids": [
"c6d5cce35bcfbef90b20f140d723cbdb", {status: 'missing'}, [
["1b8dfbb1267e213328920bae43f2f597", {status: 'missing'}, []],
["59ed830b84b276ab776c3c51aaf93a16", {status: 'missing'}, [
["64a9842c6aea50bf24660378e496e853", {status: 'missing'}, []]
"c6d5cce35bcfbef90b20f140d723cbdb", {}, [
["1b8dfbb1267e213328920bae43f2f597", {}, []],
["59ed830b84b276ab776c3c51aaf93a16", {}, [
["64a9842c6aea50bf24660378e496e853", {}, []]
]]
]]
}
Expand All @@ -186,7 +186,7 @@ test('winningRev returns the longest leaf again', function() {

///// These are tests from CouchDB's kt-merging.erl test suite

var one = {pos: 1, ids: ['1', {status: 'missing'}, []]};
var one = {pos: 1, ids: ['1', {}, []]};
test('The empty tree is the identity for merge.', function() {
deepEqual(Pouch.merge([], one, 10), {
tree: [one],
Expand All @@ -201,7 +201,7 @@ test('Merging is reflexive', function() {
});
});

var two = {pos: 1, ids: ['2', {status: 'missing'}, []]};
var two = {pos: 1, ids: ['2', {}, []]};
var twoSibs = [one, two];
test('Merging a prefix of a tree with the tree yields the tree.', function() {
deepEqual(Pouch.merge(twoSibs, one, 10), {
Expand All @@ -210,7 +210,7 @@ test('Merging a prefix of a tree with the tree yields the tree.', function() {
});
});

var three = {pos: 1, ids: ['3', {status: 'missing'}, []]};
var three = {pos: 1, ids: ['3', {}, []]};
var threeSibs = [one, two, three];
test('Merging a third unrelated branch leads to a conflict.', function() {
deepEqual(Pouch.merge(twoSibs, three, 10), {
Expand All @@ -219,9 +219,9 @@ test('Merging a third unrelated branch leads to a conflict.', function() {
});
});

var twoChild = {pos: 1, ids: ['1', {status: 'missing'}, [
['1a', {status: 'missing'}, [
['1aa', {status: 'missing'}, []]
var twoChild = {pos: 1, ids: ['1', {}, [
['1a', {}, [
['1aa', {}, []]
]]
]]};
test('Merging two children is still reflexive.', function() {
Expand All @@ -231,9 +231,9 @@ test('Merging two children is still reflexive.', function() {
});
});

var twoChildSibs = {pos: 1, ids: ['1', {status: 'missing'}, [
['1a', {status: 'missing'}, []],
['1b', {status: 'missing'}, []]
var twoChildSibs = {pos: 1, ids: ['1', {}, [
['1a', {}, []],
['1b', {}, []]
]]};
test('Merging a tree to itself is itself.', function() {
deepEqual(Pouch.merge([twoChildSibs], twoChildSibs, 10), {
Expand All @@ -242,11 +242,11 @@ test('Merging a tree to itself is itself.', function() {
});
});

var twoChildPlusSibs = {pos: 1, ids: ['1', {status: 'missing'}, [
['1a', {status: 'missing'}, [
['1aa', {status: 'missing'}, []]
var twoChildPlusSibs = {pos: 1, ids: ['1', {}, [
['1a', {}, [
['1aa', {}, []]
]],
['1b', {status: 'missing'}, []]
['1b', {}, []]
]]};
test('Merging tree of uneven length at node 2.', function() {
deepEqual(Pouch.merge([twoChild], twoChildSibs, 10), {
Expand All @@ -255,21 +255,21 @@ test('Merging tree of uneven length at node 2.', function() {
});
});

var stemmed1b = {pos: 2, ids: ['1a', {status: 'missing'}, []]};
var stemmed1b = {pos: 2, ids: ['1a', {}, []]};
test('Merging a tree with a stem.', function() {
deepEqual(Pouch.merge([twoChildSibs], stemmed1b, 10), {
tree: [twoChildSibs],
conflicts: 'internal_node'
});
});

var twoChildPlusSibs2 = {pos: 1, ids: ['1', {status: 'missing'}, [
['1a', {status: 'missing'}, []],
['1b', {status: 'missing'}, [
['1bb', {status: 'missing'}, []]
var twoChildPlusSibs2 = {pos: 1, ids: ['1', {}, [
['1a', {}, []],
['1b', {}, [
['1bb', {}, []]
]]
]]};
var stemmed1bb = {pos:3, ids: ['1bb', {status: 'missing'}, []]};
var stemmed1bb = {pos:3, ids: ['1bb', {}, []]};
test('Merging a stem at a deeper level.', function() {
deepEqual(Pouch.merge([twoChildPlusSibs2], stemmed1bb, 10), {
tree: [twoChildPlusSibs2],
Expand All @@ -278,8 +278,8 @@ test('Merging a stem at a deeper level.', function() {
});

var stemmedTwoChildSibs2 = [
{pos: 2, ids: ['1a', {status: 'missing'}, []]},
{pos: 2, ids: ['1b', {status: 'missing'}, [['1bb', {status: 'missing'}, []]]]}
{pos: 2, ids: ['1a', {}, []]},
{pos: 2, ids: ['1b', {}, [['1bb', {}, []]]]}
];
test('Merging a stem at a deeper level against paths at deeper levels.', function() {
deepEqual(Pouch.merge(stemmedTwoChildSibs2, stemmed1bb, 10), {
Expand All @@ -288,15 +288,15 @@ test('Merging a stem at a deeper level against paths at deeper levels.', functio
});
});

var stemmed1aa = {pos: 3, ids: ['1aa', {status: 'missing'}, []]};
var stemmed1aa = {pos: 3, ids: ['1aa', {}, []]};
test("Merging a single tree with a deeper stem.", function() {
deepEqual(Pouch.merge([twoChild], stemmed1aa, 10), {
tree: [twoChild],
conflicts: 'internal_node'
});
});

var stemmed1a = {pos: 2, ids: ['1a', {status: 'missing'}, [['1aa', {status: 'missing'}, []]]]};
var stemmed1a = {pos: 2, ids: ['1a', {}, [['1aa', {}, []]]]};
test('Merging a larger stem.', function() {
deepEqual(Pouch.merge([twoChild], stemmed1a, 10), {
tree: [twoChild],
Expand All @@ -311,7 +311,7 @@ test('More merging.', function() {
});
});

var oneChild = {pos: 1, ids: ['1', {status: 'missing'}, [['1a', {status: 'missing'}, []]]]};
var oneChild = {pos: 1, ids: ['1', {}, [['1a', {}, []]]]};
test('Merging should create conflicts.', function() {
deepEqual(Pouch.merge([oneChild], stemmed1aa, 10), {
tree: [oneChild, stemmed1aa],
Expand All @@ -326,19 +326,19 @@ test('Merging should have no conflicts.', function() {
});
});

var foo = {pos: 1, ids: ['foo', {status: 'missing'}, [
['foo2', {status: 'missing'}, []],
['foo3', {status: 'missing'}, []]
var foo = {pos: 1, ids: ['foo', {}, [
['foo2', {}, []],
['foo3', {}, []]
]]};
var bar = {pos: 1, ids: ['foo', {status: 'missing'}, [
['foo3', {status: 'missing'}, [
['foo4', {status: 'missing'}, []]
var bar = {pos: 1, ids: ['foo', {}, [
['foo3', {}, [
['foo4', {}, []]
]]
]]};
var fooBar = {pos: 1, ids: ['foo', {status: 'missing'}, [
['foo2', {status: 'missing'}, []],
['foo3', {status: 'missing'}, [
['foo4', {status: 'missing'}, []]
var fooBar = {pos: 1, ids: ['foo', {}, [
['foo2', {}, []],
['foo3', {}, [
['foo4', {}, []]
]]
]]};
test('Merging trees with conflicts ought to behave.', function() {
Expand Down

0 comments on commit 769f782

Please sign in to comment.