MCPcopy Index your code
hub / github.com/apache/pouchdb / findPathToLeaf

Function findPathToLeaf

lib/index-browser.js:1078–1116  ·  view source on GitHub ↗
(revs, targetRev)

Source from the content-addressed store, hash-verified

1076// - The requested revision does not exist
1077// - The requested revision is not a leaf
1078function findPathToLeaf(revs, targetRev) {
1079 let path = [];
1080 const toVisit = revs.slice();
1081
1082 let node;
1083 while ((node = toVisit.pop())) {
1084 const { pos, ids: tree } = node;
1085 const rev = `${pos}-${tree[0]}`;
1086 const branches = tree[2];
1087
1088 // just assuming we're already working on the path up towards our desired leaf.
1089 path.push(rev);
1090
1091 // we've reached the leaf of our dreams, so return the computed path.
1092 if (rev === targetRev) {
1093 //…unleeeeess
1094 if (branches.length !== 0) {
1095 throw new Error('The requested revision is not a leaf');
1096 }
1097 return path.reverse();
1098 }
1099
1100 // this is based on the assumption that after we have a leaf (`branches.length == 0`), we handle the next
1101 // branch. this is true for all branches other than the path leading to the winning rev (which is 7-57e5 in
1102 // the example above. i've added a reset condition for branching nodes (`branches.length > 1`) as well.
1103 if (branches.length === 0 || branches.length > 1) {
1104 path = [];
1105 }
1106
1107 // as a next step, we push the branches of this node to `toVisit` for visiting it during the next iteration
1108 for (let i = 0, len = branches.length; i < len; i++) {
1109 toVisit.push({ pos: pos + 1, ids: branches[i] });
1110 }
1111 }
1112 if (path.length === 0) {
1113 throw new Error('The requested revision does not exist');
1114 }
1115 return path.reverse();
1116}
1117
1118// build up a list of all the paths to the leafs in this revision tree
1119function rootToLeaf(revs) {

Callers 1

index-browser.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…