@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
100 lines • 4.76 kB
JavaScript
import { memoryBlockStoreFactory } from '../block-store';
import { compute_chunks } from '@dstanesc/wasm-chunking-fastcdc-node';
import { chunkerFactory } from '../chunking';
import { linkCodecFactory, valueCodecFactory, } from '../codecs';
import { Graph } from '../graph';
import { graphStore } from '../graph-store';
import * as assert from 'assert';
import { versionStoreFactory } from '../version-store';
const { chunk } = chunkerFactory(1024, compute_chunks);
const linkCodec = linkCodecFactory();
const valueCodec = valueCodecFactory();
describe('Edge, fields computed on commit', function () {
test('targetNext and targetPrev are properly set', async () => {
let ObjectTypes;
(function (ObjectTypes) {
ObjectTypes[ObjectTypes["TWEET"] = 1] = "TWEET";
})(ObjectTypes || (ObjectTypes = {}));
let RlshpTypes;
(function (RlshpTypes) {
RlshpTypes[RlshpTypes["COMMENT_TO"] = 1] = "COMMENT_TO";
})(RlshpTypes || (RlshpTypes = {}));
let PropTypes;
(function (PropTypes) {
PropTypes[PropTypes["COMMENT"] = 1] = "COMMENT";
PropTypes[PropTypes["WEIGHT"] = 2] = "WEIGHT";
})(PropTypes || (PropTypes = {}));
let KeyTypes;
(function (KeyTypes) {
KeyTypes[KeyTypes["JSON"] = 1] = "JSON";
KeyTypes[KeyTypes["TEXT"] = 3] = "TEXT";
KeyTypes[KeyTypes["VALUE"] = 33] = "VALUE";
})(KeyTypes || (KeyTypes = {}));
const blockStore = memoryBlockStoreFactory();
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.TWEET);
// second level
const v2 = tx.addVertex(ObjectTypes.TWEET);
const v3 = tx.addVertex(ObjectTypes.TWEET);
//third level
const v4 = tx.addVertex(ObjectTypes.TWEET);
const v5 = tx.addVertex(ObjectTypes.TWEET);
const v6 = tx.addVertex(ObjectTypes.TWEET);
const v7 = tx.addVertex(ObjectTypes.TWEET);
// fourth level
const v8 = tx.addVertex(ObjectTypes.TWEET);
const v9 = tx.addVertex(ObjectTypes.TWEET);
const v10 = tx.addVertex(ObjectTypes.TWEET);
const v11 = tx.addVertex(ObjectTypes.TWEET);
const v12 = tx.addVertex(ObjectTypes.TWEET);
const v13 = tx.addVertex(ObjectTypes.TWEET);
const v14 = tx.addVertex(ObjectTypes.TWEET);
const v15 = tx.addVertex(ObjectTypes.TWEET);
const e1 = await tx.addEdge(v1, v2, RlshpTypes.COMMENT_TO);
const e2 = await tx.addEdge(v1, v3, RlshpTypes.COMMENT_TO);
const e3 = await tx.addEdge(v2, v4, RlshpTypes.COMMENT_TO);
const e4 = await tx.addEdge(v2, v5, RlshpTypes.COMMENT_TO);
const e5 = await tx.addEdge(v3, v6, RlshpTypes.COMMENT_TO);
const e6 = await tx.addEdge(v3, v7, RlshpTypes.COMMENT_TO);
const e7 = await tx.addEdge(v4, v8, RlshpTypes.COMMENT_TO);
const e8 = await tx.addEdge(v4, v9, RlshpTypes.COMMENT_TO);
await tx.addEdge(v5, v10, RlshpTypes.COMMENT_TO);
await tx.addEdge(v5, v11, RlshpTypes.COMMENT_TO);
const { root, index } = await tx.commit({});
assert.strictEqual(e7.targetPrev, e3.offset);
assert.strictEqual(e3.targetNext, e7.offset);
assert.strictEqual(e3.targetPrev, e1.offset);
assert.strictEqual(e1.targetNext, e3.offset);
assert.strictEqual(e1.targetPrev, undefined);
// all roads lead to Rome, navigating targetPrev from any edge should lead to root
const topEdge1 = await toRoot(graph, e7);
const rootVertex1 = await graph.getVertex(topEdge1.source);
const topEdge2 = await toRoot(graph, e8);
const rootVertex2 = await graph.getVertex(topEdge2.source);
const topEdge3 = await toRoot(graph, e4);
const rootVertex3 = await graph.getVertex(topEdge3.source);
const topEdge4 = await toRoot(graph, e5);
const rootVertex4 = await graph.getVertex(topEdge4.source);
assert.strictEqual(rootVertex1.offset, rootVertex2.offset);
assert.strictEqual(rootVertex2.offset, rootVertex3.offset);
assert.strictEqual(rootVertex3.offset, rootVertex4.offset);
});
});
async function toRoot(graph, edge) {
if (edge.targetPrev !== undefined) {
const targetPrev = await graph.getEdge(edge.targetPrev);
return toRoot(graph, targetPrev);
}
else
return edge;
}
//# sourceMappingURL=edge-target.test.js.map