UNPKG

@x5e/gink

Version:

an eventually consistent database

221 lines 8.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getStorageKey = getStorageKey; exports.storageKeyToString = storageKeyToString; exports.extractMovement = extractMovement; exports.movementHelper = movementHelper; exports.extractContainerMuid = extractContainerMuid; exports.buildPointeeList = buildPointeeList; exports.buildPairLists = buildPairLists; exports.medallionChainStartToString = medallionChainStartToString; exports.buildChainTracker = buildChainTracker; exports.toStorageKey = toStorageKey; exports.bundleKeyToInfo = bundleKeyToInfo; exports.bundleInfoToKey = bundleInfoToKey; exports.bundlePropertyEntry = bundlePropertyEntry; const HasMap_1 = require("./HasMap"); const builders_1 = require("./builders"); const utils_1 = require("./utils"); /** * * @param entryBuilder * @param entryMuid * @returns A well defined string that's different for each valid key, given the behavior */ function getStorageKey(entryBuilder, entryMuid) { const behavior = entryBuilder.getBehavior(); if (behavior === builders_1.Behavior.DIRECTORY || behavior === builders_1.Behavior.KEY_SET) { (0, utils_1.ensure)(entryBuilder.hasKey()); const key = (0, utils_1.unwrapKey)(entryBuilder.getKey()); // if (key instanceof Uint8Array) return [key.toString()]; return key; } else if (behavior === builders_1.Behavior.SEQUENCE || behavior === builders_1.Behavior.EDGE_TYPE) { return entryBuilder.getEffective() || entryMuid.timestamp; } else if (behavior === builders_1.Behavior.BOX || behavior === builders_1.Behavior.VERTEX || behavior == builders_1.Behavior.ACCUMULATOR) { return []; } else if (behavior === builders_1.Behavior.PROPERTY || behavior === builders_1.Behavior.GROUP) { (0, utils_1.ensure)(entryBuilder.hasDescribing()); return (0, utils_1.muidToTuple)((0, utils_1.builderToMuid)(entryBuilder.getDescribing(), entryMuid)); } else if (behavior === builders_1.Behavior.PAIR_SET || behavior === builders_1.Behavior.PAIR_MAP) { (0, utils_1.ensure)(entryBuilder.hasPair()); const pair = entryBuilder.getPair(); const left = (0, utils_1.builderToMuid)(pair.getLeft(), entryMuid); const rite = (0, utils_1.builderToMuid)(pair.getRite(), entryMuid); return [(0, utils_1.muidToTuple)(left), (0, utils_1.muidToTuple)(rite)]; } else { throw new Error(`unexpected behavior: ${behavior}`); } } function storageKeyToString(storageKey) { if (storageKey instanceof Uint8Array) return `(${storageKey})`; if (Array.isArray(storageKey)) { if (storageKey.length === 3) { return (0, utils_1.muidTupleToString)(storageKey); } return storageKey.toString(); } if (typeof storageKey === "number" || typeof storageKey === "string") return JSON.stringify(storageKey); } function extractMovement(changeBuilder, bundleInfo, offset) { const movementBuilder = changeBuilder.getMovement(); const entryMuid = movementBuilder.getEntry(); const entryId = [ entryMuid.getTimestamp() || bundleInfo.timestamp, entryMuid.getMedallion() || bundleInfo.medallion, entryMuid.getOffset(), ]; const movementId = [ bundleInfo.timestamp, bundleInfo.medallion, offset, ]; const containerId = [0, 0, 0]; if (movementBuilder.hasContainer()) { const srcMuid = movementBuilder.getContainer(); containerId[0] = srcMuid.getTimestamp() || bundleInfo.timestamp; containerId[1] = srcMuid.getMedallion() || bundleInfo.medallion; containerId[2] = srcMuid.getOffset(); } return { entryId, movementId, containerId, dest: movementBuilder.getDest(), purge: movementBuilder.getPurge(), }; } /** * Bundles a movement change into the provided bundler * @param bundler The bundler to add the change to * @param entryMuid The muid of the entry to move * @param containerMuid The muid of the container that holds this entry * @param dest Destination to move the entry to. If omitted, the entry is removed. * @param purge Purge the entry from the internal store? */ async function movementHelper(bundler, entryMuid, containerMuid, dest, purge) { const movementBuilder = new builders_1.MovementBuilder(); movementBuilder.setEntry((0, utils_1.muidToBuilder)(entryMuid)); if (dest) movementBuilder.setDest(dest); movementBuilder.setContainer((0, utils_1.muidToBuilder)(containerMuid)); if (purge) movementBuilder.setPurge(true); const changeBuilder = new builders_1.ChangeBuilder(); changeBuilder.setMovement(movementBuilder); bundler.addChange(changeBuilder); } function extractContainerMuid(entryBuilder, bundleInfo) { const containerId = [0, 0, 0]; const srcMuid = entryBuilder.getContainer(); containerId[0] = srcMuid.getTimestamp() || bundleInfo.timestamp; containerId[1] = srcMuid.getMedallion() || bundleInfo.medallion; containerId[2] = srcMuid.getOffset(); return containerId; } function buildPointeeList(entryBuilder, bundleInfo) { const pointeeList = []; const pointeeMuidBuilder = entryBuilder.getPointee(); const pointee = (0, utils_1.dehydrate)({ timestamp: pointeeMuidBuilder.getTimestamp() || bundleInfo.timestamp, medallion: pointeeMuidBuilder.getMedallion() || bundleInfo.medallion, offset: pointeeMuidBuilder.getOffset(), }); pointeeList.push(pointee); return pointeeList; } function buildPairLists(entryBuilder, bundleInfo) { const sourceList = []; const targetList = []; const pairBuilder = entryBuilder.getPair(); const source = (0, utils_1.dehydrate)({ timestamp: pairBuilder.getLeft().getTimestamp() || bundleInfo.timestamp, medallion: pairBuilder.getLeft().getMedallion() || bundleInfo.medallion, offset: pairBuilder.getLeft().getOffset(), }); sourceList.push(source); const target = (0, utils_1.dehydrate)({ timestamp: pairBuilder.getRite().getTimestamp() || bundleInfo.timestamp, medallion: pairBuilder.getRite().getMedallion() || bundleInfo.medallion, offset: pairBuilder.getRite().getOffset(), }); targetList.push(target); return [sourceList, targetList]; } function medallionChainStartToString(tuple) { // this is for [Medallion, ChainStart] keys return `${(0, utils_1.intToHex)(tuple[0])}-${(0, utils_1.intToHex)(tuple[1])}`; } function buildChainTracker(chainInfos) { const hasMap = new HasMap_1.HasMap({}); for (const value of chainInfos) { hasMap.markAsHaving(value); } return hasMap; } function toStorageKey(key) { if (key instanceof Uint8Array) return key; if (typeof key === "number" || typeof key === "string") { return key; } else if (Array.isArray(key)) { return [(0, utils_1.muidToTuple)(key[0]), (0, utils_1.muidToTuple)(key[1])]; } else if (key) { const muidKey = key; return [muidKey.timestamp, muidKey.medallion, muidKey.offset]; } if (key === undefined || key === null) { return []; } } function bundleKeyToInfo(bundleKey) { return { timestamp: bundleKey[0], medallion: bundleKey[1], chainStart: bundleKey[2], priorTime: bundleKey[3], comment: bundleKey[4], }; } function bundleInfoToKey(bundleInfo) { return [ bundleInfo.timestamp, bundleInfo.medallion, bundleInfo.chainStart, bundleInfo.priorTime || 0, bundleInfo.comment || "", ]; } /** * Utility function to add a property entry change to a bundler without reconstructing the property. * @param bundler required bundler to add this change to * @param propertyMuid the Muid of the property to add this entry to * @param containerMuid the Muid of the container this value is describing * @param value the property value associated with the container. If omitted, the property is deleted. */ function bundlePropertyEntry(bundler, propertyMuid, containerMuid, value) { const entryBuilder = new builders_1.EntryBuilder(); entryBuilder.setDescribing((0, utils_1.muidToBuilder)(containerMuid)); entryBuilder.setBehavior(builders_1.Behavior.PROPERTY); entryBuilder.setContainer((0, utils_1.muidToBuilder)(propertyMuid)); if (value) entryBuilder.setValue((0, utils_1.wrapValue)(value)); else entryBuilder.setDeletion(true); const changeBuilder = new builders_1.ChangeBuilder(); changeBuilder.setEntry(entryBuilder); return bundler.addChange(changeBuilder); } //# sourceMappingURL=store_utils.js.map