UNPKG

@dstanesc/o-o-o-o-o-o-o

Version:

O-O-O-O-O-O-O is a collection of content addressed persistent data structures

447 lines 20.3 kB
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; var __asyncDelegator = (this && this.__asyncDelegator) || function (o) { var i, p; return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i; function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; } }; var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; var PathElemType; (function (PathElemType) { PathElemType[PathElemType["VERTEX"] = 0] = "VERTEX"; PathElemType[PathElemType["EDGE"] = 1] = "EDGE"; PathElemType[PathElemType["EXTRACT"] = 2] = "EXTRACT"; PathElemType[PathElemType["TEMPLATE"] = 3] = "TEMPLATE"; })(PathElemType || (PathElemType = {})); class RequestBuilder { constructor() { this.path = []; } add(elemType) { if (elemType === PathElemType.EXTRACT) { throw new Error('For PathElemType.EXTRACT elemType please use the extract(...props) call'); } let elem = { elemType: elemType }; this.path.push(elem); return this; } type(...types) { const elemType = this.path[this.path.length - 1].elemType; switch (elemType) { case PathElemType.VERTEX: ; this.path[this.path.length - 1].types = types; break; case PathElemType.EDGE: ; this.path[this.path.length - 1].types = types; break; default: throw new Error(`Cannot set predicate to ${elemType}`); } return this; } propPred(keyType, operation) { const elemType = this.path[this.path.length - 1].elemType; switch (elemType) { case PathElemType.VERTEX: ; this.path[this.path.length - 1].propPredicate = { keyType, operation }; break; case PathElemType.EDGE: ; this.path[this.path.length - 1].propPredicate = { keyType, operation }; break; default: throw new Error(`Cannot set prop predicate to ${elemType}`); } return this; } extract(...props) { let elem = { elemType: PathElemType.EXTRACT, props }; this.path.push(elem); return this; } template(template) { let elem = { elemType: PathElemType.TEMPLATE, template, }; this.path.push(elem); return this; } reduce(reduceFunc) { const elemType = this.path[this.path.length - 1].elemType; if (elemType !== PathElemType.EXTRACT) { throw new Error('Reduce should be applied to PathElemType.EXTRACT'); } ; this.path[this.path.length - 1].reduce = reduceFunc; return this; } maxResults(value) { this.request = new NavigationThreshold(value); return this; } get() { if (this.request === undefined) this.request = new NavigationThreshold(100); return { path: this.path, request: this.request }; } } class SearchCompleted { } class NavigationThreshold { constructor(maxYield) { this.yieldCounter = 0; this.maxYield = maxYield; } increment() { this.yieldCounter++; } isDone() { return this.yieldCounter === this.maxYield; } completeWhenDone() { if (this.isDone()) { throw new SearchCompleted(); } } } function checkFinished(index, path) { return index === path.length - 1; } function isExtractPathElem(index, path) { const pathElem = path[index]; return pathElem.elemType === PathElemType.EXTRACT; } function isVertexPathElem(index, path) { const pathElem = path[index]; return pathElem.elemType === PathElemType.VERTEX; } function isEdgePathElem(index, path) { const pathElem = path[index]; return pathElem.elemType === PathElemType.EDGE; } function isTemplatePathElem(index, path) { const pathElem = path[index]; return pathElem.elemType === PathElemType.TEMPLATE; } async function lookaheadIndexedPredicate(graph, vertex, path, index) { const pathElem = path[index]; let found = undefined; if (pathElem !== undefined) { if (pathElem.elemType === PathElemType.VERTEX) { const vertexPathElement = pathElem; const propPredicate = vertexPathElement.propPredicate; if (propPredicate !== undefined) { const indexStruct = await graph.matchAnyIndex(vertex, propPredicate.keyType); if (indexStruct !== undefined) { found = { propPredicate, indexStruct }; } } } } return found; } function yieldVertexOrGoDeeper(index, path, vertex, graph, request) { return __asyncGenerator(this, arguments, function* yieldVertexOrGoDeeper_1() { let indexedPredicate = yield __await(lookaheadIndexedPredicate(graph, vertex, path, index + 2)); if (checkFinished(index, path)) { request.increment(); yield yield __await(vertex); request.completeWhenDone(); } else if (isExtractPathElem(index + 1, path)) yield __await(yield* __asyncDelegator(__asyncValues(navigateProp(graph, vertex, vertex.nextProp, path, index + 1, request)))); else if (isTemplatePathElem(index + 1, path)) { yield __await(yield* __asyncDelegator(__asyncValues(navigateTemplate(graph, vertex, path, index + 1, request)))); } else if (indexedPredicate !== undefined) yield __await(yield* __asyncDelegator(__asyncValues(navigateIndexedEdge(graph, indexedPredicate, path, index + 1, request)))); else yield __await(yield* __asyncDelegator(__asyncValues(navigateEdge(graph, vertex.nextEdge, path, index + 1, request)))); }); } function yieldEdgeOrGoDeeper(index, path, edge, graph, request) { return __asyncGenerator(this, arguments, function* yieldEdgeOrGoDeeper_1() { if (checkFinished(index, path)) { request.increment(); yield yield __await(edge); request.completeWhenDone(); } else if (isExtractPathElem(index + 1, path)) yield __await(yield* __asyncDelegator(__asyncValues(navigateProp(graph, edge, edge.nextProp, path, index + 1, request)))); else yield __await(yield* __asyncDelegator(__asyncValues(navigateVertex(graph, edge.target, path, index + 1, request)))); }); } function navigateVertices(graph, refs, { path, request }) { return __asyncGenerator(this, arguments, function* navigateVertices_1() { const index = 0; try { for (const ref of refs) { yield __await(yield* __asyncDelegator(__asyncValues(navigateVertex(graph, ref, path, index, request)))); } } catch (e) { if (e instanceof SearchCompleted) { return yield __await(void 0); } else throw e; } }); } function navigateEdges(graph, refs, { path, request }) { return __asyncGenerator(this, arguments, function* navigateEdges_1() { const index = 0; try { for (const ref of refs) { yield __await(yield* __asyncDelegator(__asyncValues(navigateEdge(graph, ref, path, index, request)))); } } catch (e) { if (e instanceof SearchCompleted) { return yield __await(void 0); } else throw e; } }); } function navigatePropCatchCompleted(graph, part, ref, path, index, request) { return __asyncGenerator(this, arguments, function* navigatePropCatchCompleted_1() { try { yield __await(yield* __asyncDelegator(__asyncValues(navigateProp(graph, part, ref, path, index, request)))); } catch (e) { if (e instanceof SearchCompleted) { return yield __await(void 0); } else throw e; } }); } function navigateEdgeCatchCompleted(graph, ref, path, index, request) { return __asyncGenerator(this, arguments, function* navigateEdgeCatchCompleted_1() { try { yield __await(yield* __asyncDelegator(__asyncValues(navigateEdge(graph, ref, path, index, request)))); } catch (e) { if (e instanceof SearchCompleted) { return yield __await(void 0); } else throw e; } }); } function navigateVertex(graph, ref, path, index, request) { return __asyncGenerator(this, arguments, function* navigateVertex_1() { const pathElem = path[index]; if (ref !== undefined) { const vertex = yield __await(graph.getVertex(ref)); if (pathElem.types === undefined) { if (pathElem.propPredicate === undefined) { yield __await(yield* __asyncDelegator(__asyncValues(yieldVertexOrGoDeeper(index, path, vertex, graph, request)))); } else if (yield __await(graph.matchAnyVertexProp(vertex, pathElem.propPredicate))) { yield __await(yield* __asyncDelegator(__asyncValues(yieldVertexOrGoDeeper(index, path, vertex, graph, request)))); } } else if (pathElem.types.includes(vertex.type)) { if (pathElem.propPredicate === undefined) { yield __await(yield* __asyncDelegator(__asyncValues(yieldVertexOrGoDeeper(index, path, vertex, graph, request)))); } else if (yield __await(graph.matchAnyVertexProp(vertex, pathElem.propPredicate))) { yield __await(yield* __asyncDelegator(__asyncValues(yieldVertexOrGoDeeper(index, path, vertex, graph, request)))); } } } }); } function navigateEdge(graph, ref, path, index, request) { return __asyncGenerator(this, arguments, function* navigateEdge_1() { const pathElem = path[index]; if (ref !== undefined) { let edge = yield __await(graph.getEdge(ref)); if (pathElem.types === undefined) { if (pathElem.propPredicate === undefined) { yield __await(yield* __asyncDelegator(__asyncValues(yieldEdgeOrGoDeeper(index, path, edge, graph, request)))); } else if (yield __await(graph.matchAnyEdgeProp(edge, pathElem.propPredicate))) yield __await(yield* __asyncDelegator(__asyncValues(yieldEdgeOrGoDeeper(index, path, edge, graph, request)))); } else if (pathElem.types.includes(edge.type)) { if (pathElem.propPredicate === undefined) { yield __await(yield* __asyncDelegator(__asyncValues(yieldEdgeOrGoDeeper(index, path, edge, graph, request)))); } else if (yield __await(graph.matchAnyEdgeProp(edge, pathElem.propPredicate))) { yield __await(yield* __asyncDelegator(__asyncValues(yieldEdgeOrGoDeeper(index, path, edge, graph, request)))); } } yield __await(yield* __asyncDelegator(__asyncValues(navigateEdge(graph, edge.sourceNext, path, index, request)))); } }); } function navigateIndexedEdge(graph, indexedPredicate, path, index, request) { return __asyncGenerator(this, arguments, function* navigateIndexedEdge_1() { const { value, ref: edgeRef } = yield __await(graph.searchIndex(indexedPredicate.indexStruct, indexedPredicate.propPredicate)); let edge = yield __await(graph.getEdge(edgeRef)); yield __await(yield* __asyncDelegator(__asyncValues(yieldEdgeOrGoDeeper(index, path, edge, graph, request)))); }); } function navigateProp(graph, partContext, ref, path, index, request) { return __asyncGenerator(this, arguments, function* navigateProp_1() { const pathElem = path[index]; if (ref !== undefined) { let prop = yield __await(graph.getProp(ref)); if (pathElem.reduce === undefined) { if (pathElem.props.includes(prop.key)) { request.increment(); yield yield __await(prop); request.completeWhenDone(); } yield __await(yield* __asyncDelegator(__asyncValues(navigateProp(graph, partContext, prop.nextProp, path, index, request)))); } else yield yield __await(yield __await(navigatePropsAndReduce(graph, partContext, pathElem, prop, undefined))); } }); } async function navigatePropsAndReduce(graph, context, pathElem, prop, previousReduced) { const reducedValue = pathElem.reduce(previousReduced, prop.value); if (prop.nextProp !== undefined) { let nextProp = await graph.getProp(prop.nextProp); return navigatePropsAndReduce(graph, context, pathElem, nextProp, reducedValue); } return { context, value: reducedValue }; } function navigateTemplate(graph, vertex, path, index, request) { return __asyncGenerator(this, arguments, function* navigateTemplate_1() { var _a, e_1, _b, _c, _d, e_2, _e, _f, _g, e_3, _h, _j; const pathElem = path[index]; const template = pathElem.template; const result = {}; for (const [key, value] of Object.entries(template)) { const elemType = value['$elemType']; const schemaType = value['$type']; if (elemType === PathElemType.EXTRACT) { let propElem = { elemType: PathElemType.EXTRACT, props: [schemaType], }; const propPath = [...path.slice(0, index), propElem]; try { for (var _k = true, _l = (e_1 = void 0, __asyncValues(navigatePropCatchCompleted(graph, vertex, vertex.nextProp, propPath, index, new NavigationThreshold(Number.MAX_SAFE_INTEGER)))), _m; _m = yield __await(_l.next()), _a = _m.done, !_a;) { _c = _m.value; _k = false; try { const prop = _c; result[key] = prop.value; } finally { _k = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_k && !_a && (_b = _l.return)) yield __await(_b.call(_l)); } finally { if (e_1) throw e_1.error; } } } else if (elemType === PathElemType.EDGE) { let edgeElem = { elemType: PathElemType.EDGE, types: [schemaType], }; const edgePath = [...path.slice(0, index), edgeElem]; const nestedResults = []; try { for (var _o = true, _p = (e_2 = void 0, __asyncValues(navigateEdgeCatchCompleted(graph, vertex.nextEdge, edgePath, index, new NavigationThreshold(Number.MAX_SAFE_INTEGER)))), _q; _q = yield __await(_p.next()), _d = _q.done, !_d;) { _f = _q.value; _o = false; try { const edge = _f; const realEdge = edge; const vertex = yield __await(graph.getVertex(realEdge.target)); const vertexElem = { elemType: PathElemType.VERTEX, }; const templateElem = { elemType: PathElemType.TEMPLATE, template: value, }; const valueOnly = value['$value'] !== undefined; const templateFragmentPath = [vertexElem, templateElem]; try { for (var _r = true, _s = (e_3 = void 0, __asyncValues(navigateTemplate(graph, vertex, templateFragmentPath, 1, new NavigationThreshold(Number.MAX_SAFE_INTEGER)))), _t; _t = yield __await(_s.next()), _g = _t.done, !_g;) { _j = _t.value; _r = false; try { const nestedResult = _j; const realNestedResult = valueOnly ? nestedResult.$value : nestedResult; nestedResults.push(realNestedResult); } finally { _r = true; } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_r && !_g && (_h = _s.return)) yield __await(_h.call(_s)); } finally { if (e_3) throw e_3.error; } } } finally { _o = true; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_o && !_d && (_e = _p.return)) yield __await(_e.call(_p)); } finally { if (e_2) throw e_2.error; } } if (nestedResults.length > 0) { result[key] = nestedResults; } } } request.increment(); yield yield __await(result); request.completeWhenDone(); }); } export { PathElemType, RequestBuilder, NavigationThreshold, navigateVertices, navigateEdges, }; //# sourceMappingURL=navigate.js.map