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

Function findPathToLeaf

lib/index.js:919–957  ·  view source on GitHub ↗
(revs, targetRev)

Source from the content-addressed store, hash-verified

917// - The requested revision does not exist
918// - The requested revision is not a leaf
919function findPathToLeaf(revs, targetRev) {
920 let path$$1 = [];
921 const toVisit = revs.slice();
922
923 let node;
924 while ((node = toVisit.pop())) {
925 const { pos, ids: tree } = node;
926 const rev = `${pos}-${tree[0]}`;
927 const branches = tree[2];
928
929 // just assuming we're already working on the path up towards our desired leaf.
930 path$$1.push(rev);
931
932 // we've reached the leaf of our dreams, so return the computed path.
933 if (rev === targetRev) {
934 //…unleeeeess
935 if (branches.length !== 0) {
936 throw new Error('The requested revision is not a leaf');
937 }
938 return path$$1.reverse();
939 }
940
941 // this is based on the assumption that after we have a leaf (`branches.length == 0`), we handle the next
942 // branch. this is true for all branches other than the path leading to the winning rev (which is 7-57e5 in
943 // the example above. i've added a reset condition for branching nodes (`branches.length > 1`) as well.
944 if (branches.length === 0 || branches.length > 1) {
945 path$$1 = [];
946 }
947
948 // as a next step, we push the branches of this node to `toVisit` for visiting it during the next iteration
949 for (let i = 0, len = branches.length; i < len; i++) {
950 toVisit.push({ pos: pos + 1, ids: branches[i] });
951 }
952 }
953 if (path$$1.length === 0) {
954 throw new Error('The requested revision does not exist');
955 }
956 return path$$1.reverse();
957}
958
959// build up a list of all the paths to the leafs in this revision tree
960function rootToLeaf(revs) {

Callers 1

index.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…