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

234 lines 8.72 kB
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); } }; import { compute_chunks } from '@dstanesc/wasm-chunking-fastcdc-node'; import { chunkerFactory } from '../chunking'; import { graphStore } from '../graph-store'; import { Graph } from '../graph'; import { memoryBlockStoreFactory } from '../block-store'; import { linkCodecFactory, valueCodecFactory, } from '../codecs'; import * as assert from 'assert'; import { navigateVertices, PathElemType, RequestBuilder } from '../navigate'; import { versionStoreFactory } from '../version-store'; /** * Some proto-schema */ var ObjectTypes; (function (ObjectTypes) { ObjectTypes[ObjectTypes["FOLDER"] = 1] = "FOLDER"; ObjectTypes[ObjectTypes["FILE"] = 2] = "FILE"; })(ObjectTypes || (ObjectTypes = {})); var RlshpTypes; (function (RlshpTypes) { RlshpTypes[RlshpTypes["CONTAINS"] = 1] = "CONTAINS"; })(RlshpTypes || (RlshpTypes = {})); var PropTypes; (function (PropTypes) { PropTypes[PropTypes["META"] = 1] = "META"; PropTypes[PropTypes["DATA"] = 2] = "DATA"; })(PropTypes || (PropTypes = {})); var KeyTypes; (function (KeyTypes) { KeyTypes[KeyTypes["NAME"] = 1] = "NAME"; KeyTypes[KeyTypes["CONTENT"] = 2] = "CONTENT"; })(KeyTypes || (KeyTypes = {})); const { chunk } = chunkerFactory(512, compute_chunks); const linkCodec = linkCodecFactory(); const valueCodec = valueCodecFactory(); const blockStore = memoryBlockStoreFactory(); describe('Extract graph data fragments', function () { test('internal & proto-gremlin api, simple navigation incl. data template', async () => { /** * Build original data set */ const story = await versionStoreFactory({ chunk, linkCodec, valueCodec, blockStore, }); const store = graphStore({ chunk, linkCodec, valueCodec, blockStore }); const graph = new Graph(story, store); const tx = graph.tx(); await tx.start(); const v1 = tx.addVertex(ObjectTypes.FOLDER); const v2 = tx.addVertex(ObjectTypes.FOLDER); const v3 = tx.addVertex(ObjectTypes.FILE); const v4 = tx.addVertex(ObjectTypes.FILE); const v5 = tx.addVertex(ObjectTypes.FILE); const e1 = await tx.addEdge(v1, v2, RlshpTypes.CONTAINS); const e2 = await tx.addEdge(v1, v3, RlshpTypes.CONTAINS); const e3 = await tx.addEdge(v1, v4, RlshpTypes.CONTAINS); const e4 = await tx.addEdge(v2, v5, RlshpTypes.CONTAINS); await tx.addVertexProp(v1, KeyTypes.NAME, 'root-folder', PropTypes.META); await tx.addVertexProp(v2, KeyTypes.NAME, 'nested-folder-v2', PropTypes.META); await tx.addVertexProp(v3, KeyTypes.NAME, 'nested-file-v3', PropTypes.META); await tx.addVertexProp(v3, KeyTypes.CONTENT, 'hello world from v3', PropTypes.DATA); await tx.addVertexProp(v4, KeyTypes.NAME, 'nested-file-v4', PropTypes.META); await tx.addVertexProp(v4, KeyTypes.CONTENT, 'hello world from v4', PropTypes.DATA); await tx.addVertexProp(v5, KeyTypes.NAME, 'nested-file-v5', PropTypes.META); await tx.addVertexProp(v5, KeyTypes.CONTENT, 'hello world from v5', PropTypes.DATA); const { root: original } = await tx.commit({}); const roots = await zeroLevelList(graph); assert.strictEqual(roots.length, 1); assert.strictEqual(roots[0], 'root-folder'); const names = await firstLevelList(graph); assert.strictEqual(names.length, 3); assert.strictEqual(names[0], 'nested-folder-v2'); assert.strictEqual(names[1], 'nested-file-v3'); assert.strictEqual(names[2], 'nested-file-v4'); const results1 = await firstLevelTemplate(graph, DATA_TEMPLATE_1); assert.strictEqual(results1.length, 3); assert.strictEqual(results1[0].name, 'nested-folder-v2'); assert.strictEqual(results1[0].content, undefined); assert.deepStrictEqual(results1[0].includes, [ { name: 'nested-file-v5', content: 'hello world from v5', }, ]); assert.strictEqual(results1[1].name, 'nested-file-v3'); assert.strictEqual(results1[1].content, 'hello world from v3'); assert.strictEqual(results1[2].name, 'nested-file-v4'); assert.strictEqual(results1[2].content, 'hello world from v4'); const results2 = await firstLevelTemplate(graph, DATA_TEMPLATE_2); for (const content of results2) { console.log(content); } }); }); const zeroLevelList = async (graph) => { var _a, e_1, _b, _c; const request = new RequestBuilder() .add(PathElemType.VERTEX) .extract(KeyTypes.NAME) .maxResults(100) .get(); const vr = []; try { for (var _d = true, _e = __asyncValues(navigateVertices(graph, [0], request)), _f; _f = await _e.next(), _a = _f.done, !_a;) { _c = _f.value; _d = false; try { const result = _c; vr.push(result.value); } finally { _d = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) await _b.call(_e); } finally { if (e_1) throw e_1.error; } } return vr; }; const firstLevelList = async (graph) => { var _a, e_2, _b, _c; const request = new RequestBuilder() .add(PathElemType.VERTEX) .add(PathElemType.EDGE) .add(PathElemType.VERTEX) .extract(KeyTypes.NAME) .maxResults(100) .get(); const vr = []; try { for (var _d = true, _e = __asyncValues(navigateVertices(graph, [0], request)), _f; _f = await _e.next(), _a = _f.done, !_a;) { _c = _f.value; _d = false; try { const result = _c; vr.push(result.value); } finally { _d = true; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) await _b.call(_e); } finally { if (e_2) throw e_2.error; } } return vr; }; const DATA_TEMPLATE_1 = { name: { $elemType: PathElemType.EXTRACT, $type: KeyTypes.NAME, }, content: { $elemType: PathElemType.EXTRACT, $type: KeyTypes.CONTENT, }, includes: { $elemType: PathElemType.EDGE, $type: RlshpTypes.CONTAINS, name: { $elemType: PathElemType.EXTRACT, $type: KeyTypes.NAME, }, content: { $elemType: PathElemType.EXTRACT, $type: KeyTypes.CONTENT, }, }, }; const DATA_TEMPLATE_2 = { fileName: { $elemType: PathElemType.EXTRACT, $type: KeyTypes.NAME, }, includes: { $elemType: PathElemType.EDGE, $type: RlshpTypes.CONTAINS, $value: { $elemType: PathElemType.EXTRACT, $type: KeyTypes.NAME, }, }, }; const firstLevelTemplate = async (graph, template) => { var _a, e_3, _b, _c; const request = new RequestBuilder() .add(PathElemType.VERTEX) .add(PathElemType.EDGE) .add(PathElemType.VERTEX) .template(template) .maxResults(100) .get(); const vr = []; try { for (var _d = true, _e = __asyncValues(navigateVertices(graph, [0], request)), _f; _f = await _e.next(), _a = _f.done, !_a;) { _c = _f.value; _d = false; try { const result = _c; vr.push(result); } finally { _d = true; } } } catch (e_3_1) { e_3 = { error: e_3_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) await _b.call(_e); } finally { if (e_3) throw e_3.error; } } return vr; }; //# sourceMappingURL=template.test.js.map