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

Function findPathToLeaf

lib/index.es.js:914–952  ·  view source on GitHub ↗
(revs, targetRev)

Source from the content-addressed store, hash-verified

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

Callers 1

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