-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip(merge): findPathToLeaf, removeLeafFromTree
- Loading branch information
1 parent
5fd1522
commit 972ae33
Showing
4 changed files
with
213 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
function findPathToLeaf(revs, targetRev) { | ||
// `revs` has the same structure as what `revs_tree` has on e.g. the IndexedDB representation | ||
// of the rev tree datastructure. | ||
let path = []; | ||
const toVisit = revs.slice(); | ||
|
||
let node; | ||
while ((node = toVisit.pop())) { | ||
const { pos, ids: tree } = node; | ||
const rev = `${pos}-${tree[0]}`; | ||
const branches = tree[2]; | ||
|
||
// just assuming we're already working on the path up towards our desired leaf. | ||
path.push(rev); | ||
|
||
// we've reached the leaf of our dreams, so return the computed path. | ||
if (rev === targetRev) { | ||
//…unleeeeess | ||
if (branches.length !== 0) { | ||
throw new Error('The requested revision is not a leaf'); | ||
} | ||
return path.reverse(); | ||
} | ||
|
||
// this is based on the assumption that after we have a leaf (`branches.length == 0`), we handle the next | ||
// branch. this is true for all branches other than the path leading to the winning rev (which is 6-a in | ||
// the example below. i've added a reset condition for branching nodes (`branches.length > 1`) as well. | ||
if (branches.length === 0 || branches.length > 1) { | ||
path = []; | ||
} | ||
|
||
// as a next step, we push the branches of this node to `toVisit` for visiting it during the next iteration | ||
for (let i = 0, len = branches.length; i < len; i++) { | ||
toVisit.push({ pos: pos + 1, ids: branches[i] }); | ||
} | ||
} | ||
if (path.length === 0) { | ||
throw new Error('The requested revision does not exist'); | ||
} | ||
return path.reverse(); | ||
} | ||
|
||
export default findPathToLeaf; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/node_modules/pouchdb-merge/src/removeLeafFromTree.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { clone } from 'pouchdb-utils'; | ||
|
||
function removeLeafFromRevTree(tree, leafRev) { | ||
const _tree = clone(tree); | ||
const toVisit = _tree.slice(); | ||
|
||
let previousNode; | ||
let node; | ||
while ((node = toVisit.pop())) { | ||
const pos = node.pos; | ||
const [id, opts, branches] = node.ids; | ||
const isLeaf = branches.length === 0; | ||
const hash = `${pos}-${id}`; | ||
|
||
if (isLeaf && hash === leafRev && opts.status === "available") { | ||
previousNode.ids[2] = previousNode.ids[2].filter(function (branchNode) { | ||
return branchNode[0] !== id; | ||
}); | ||
return _tree; | ||
} | ||
previousNode = node; | ||
|
||
if (branches.length) { | ||
for (let i = 0, len = branches.length; i < len; i++) { | ||
toVisit.push({pos: pos + 1, ids: branches[i]}); | ||
} | ||
} | ||
} | ||
return _tree; | ||
} | ||
|
||
export default removeLeafFromRevTree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters