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

108 lines 3.85 kB
import { Graph } from './graph'; import { graphStoreFactory } from './graph-store'; import { MergePolicyEnum, merge } from './merge'; import { OFFSET_INCREMENTS } from './serde'; import { versionStoreFactory } from './version-store'; const itemListFactory = (versionStore, graphStore) => { const graph = new Graph(versionStore, graphStore); const tx = () => { return new ItemListTransaction(graph.tx()); }; const get = async (index) => { const offset = index * OFFSET_INCREMENTS.VERTEX_INCREMENT; const ref = { index, offset }; const vertex = await graph.getVertex(offset); const props = await graph.getVertexProps(vertex); const value = new Map(); for (const prop of props) { value.set(prop.key, prop.value); } return { ref, value }; }; const range = async (startIndex, itemCount) => { const items = []; const vertices = await graph.getVertexRange(startIndex * OFFSET_INCREMENTS.VERTEX_INCREMENT, itemCount); let index = startIndex; for (const vertex of vertices) { const props = await graph.getVertexProps(vertex); const value = new Map(); for (const prop of props) { value.set(prop.key, prop.value); } const ref = { index, offset: vertex.offset }; items.push({ ref, value }); index++; } return items; }; const length = async () => { const { root, index } = await versionStore.rootGet(); const { vertexOffset } = await graphStore.offsetsGet({ root, index }); return vertexOffset / OFFSET_INCREMENTS.VERTEX_INCREMENT; }; return { tx, get, range, length }; }; var ItemTypes; (function (ItemTypes) { ItemTypes[ItemTypes["GENERIC"] = 1] = "GENERIC"; })(ItemTypes || (ItemTypes = {})); var RlshpTypes; (function (RlshpTypes) { RlshpTypes[RlshpTypes["PARENT"] = 1] = "PARENT"; })(RlshpTypes || (RlshpTypes = {})); class ItemListTransaction { constructor(tx) { this.tx = tx; } async push(item) { const vertex = this.tx.addVertex(ItemTypes.GENERIC); for (const [key, value] of item) { this.tx.addVertexProp(vertex, key, value); } if (this.current) { await this.tx.addEdge(vertex, this.current, RlshpTypes.PARENT); } this.current = vertex; const offset = vertex.offset; const index = offset / OFFSET_INCREMENTS.VERTEX_INCREMENT; return { index, offset }; } async start() { await this.tx.start(); return this; } async commit({ comment, tags, signer, }) { return await this.tx.commit({ comment, tags, signer }); } } const readonlyItemList = async ({ versionRoot, chunk, linkCodec, valueCodec, blockStore, }) => { const readonlyVersionStore = await versionStoreFactory({ readOnly: true, versionRoot, chunk, linkCodec, valueCodec, blockStore, }); const readonlyGraphStore = graphStoreFactory({ chunk, linkCodec, valueCodec, blockStore, }); const readonlyItemList = itemListFactory(readonlyVersionStore, readonlyGraphStore); return readonlyItemList; }; const mergeItemLists = async ({ baseRoot, baseStore, currentRoot, currentStore, otherRoot, otherStore, }, chunk, linkCodec, valueCodec) => { const { root, index, blocks } = await merge({ baseRoot, baseStore, currentRoot, currentStore, otherRoot, otherStore, }, MergePolicyEnum.MultiValueRegistry, chunk, linkCodec, valueCodec); return { root, index, blocks }; }; export { ItemListTransaction, itemListFactory, readonlyItemList, mergeItemLists, }; //# sourceMappingURL=item-list.js.map