UNPKG

envio

Version:

A latency and sync speed optimized, developer friendly blockchain data indexer.

242 lines (223 loc) • 7.51 kB
// Generated by ReScript, PLEASE EDIT WITH CARE import * as Utils from "./Utils.res.mjs"; import * as Internal from "./Internal.res.mjs"; import * as EntityFilter from "./db/EntityFilter.res.mjs"; import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.js"; import * as ErrorHandling from "./ErrorHandling.res.mjs"; import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js"; import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js"; import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js"; import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js"; let UnexpectedIdNotDefinedOnEntity = /* @__PURE__ */Primitive_exceptions.create("InMemoryTable.Entity.UnexpectedIdNotDefinedOnEntity"); function getEntityIdUnsafe(entity) { let id = entity.id; if (id !== undefined) { return id; } else { return ErrorHandling.mkLogAndRaise(undefined, "Property 'id' does not exist on expected entity object", { RE_EXN_ID: UnexpectedIdNotDefinedOnEntity }); } } function getOrCreateEntityFilters(self, entityId) { let s = self.filtersByEntityId[entityId]; if (s !== undefined) { return Primitive_option.valFromOption(s); } let s$1 = new Set(); self.filtersByEntityId[entityId] = s$1; return s$1; } function make() { return { latestEntityChangeById: {}, changesCount: 0, prevEntityChanges: [], filtersByEntityId: {}, filterIndices: {} }; } function snapshotChanges(self, committedCheckpointId, upToCheckpointId) { let changes = []; let keptPrev = []; self.prevEntityChanges.forEach(change => { let checkpointId = change.checkpointId; if (checkpointId > upToCheckpointId) { keptPrev.push(change); return; } else if (checkpointId > committedCheckpointId) { changes.push(change); return; } else { return; } }); let removedCount = self.prevEntityChanges.length - keptPrev.length | 0; self.prevEntityChanges = keptPrev; self.changesCount = self.changesCount - removedCount; Utils.Dict.forEach(self.latestEntityChangeById, change => { let checkpointId = change.checkpointId; if (checkpointId > committedCheckpointId && checkpointId <= upToCheckpointId) { changes.push(change); return; } }); return changes; } function dropCommittedChanges(self, committedCheckpointId, keepLoadedFromDb) { let keysToDelete = []; Utils.Dict.forEachWithKey(self.latestEntityChangeById, (change, key) => { let checkpointId = change.checkpointId; if (checkpointId <= committedCheckpointId && !(keepLoadedFromDb && checkpointId === Internal.loadedFromDbCheckpointId)) { keysToDelete.push(key); return; } }); keysToDelete.forEach(key => Utils.Dict.deleteInPlace(self.latestEntityChangeById, key)); self.changesCount = self.changesCount - keysToDelete.length; self.filtersByEntityId = {}; self.filterIndices = {}; } function updateIndices(self, entity) { let entityId = getEntityIdUnsafe(entity); let entityFilters = self.filtersByEntityId[entityId]; if (entityFilters !== undefined) { let entityFilters$1 = Primitive_option.valFromOption(entityFilters); entityFilters$1.forEach(filter => { if (!EntityFilter.matches(filter, entity)) { entityFilters$1.delete(filter); return; } }); } Utils.Dict.forEach(self.filterIndices, param => { let relatedEntityIds = param[1]; let filter = param[0]; if (EntityFilter.matches(filter, entity)) { relatedEntityIds.add(entityId); getOrCreateEntityFilters(self, entityId).add(filter); } else { relatedEntityIds.delete(entityId); } }); } function deleteEntityFromIndices(self, entityId) { let entityFilters = self.filtersByEntityId[entityId]; if (entityFilters === undefined) { return; } let entityFilters$1 = Primitive_option.valFromOption(entityFilters); entityFilters$1.forEach(filter => { let match = self.filterIndices[EntityFilter.toString(filter)]; if (match !== undefined) { match[1].delete(entityId); } entityFilters$1.delete(filter); }); } function set(inMemTable, committedCheckpointId, change) { let entityId = change.entityId; let prev = inMemTable.latestEntityChangeById[entityId]; if (prev !== undefined) { let prevCheckpointId = prev.checkpointId; if (prevCheckpointId > committedCheckpointId && prevCheckpointId < change.checkpointId) { inMemTable.prevEntityChanges.push(prev); inMemTable.changesCount = inMemTable.changesCount + 1; } } else { inMemTable.changesCount = inMemTable.changesCount + 1; } if (change.type === "SET") { updateIndices(inMemTable, change.entity); } else { deleteEntityFromIndices(inMemTable, change.entityId); } inMemTable.latestEntityChangeById[entityId] = change; } function initValue(inMemTable, committedCheckpointId, key, entity) { if (!Stdlib_Option.isNone(inMemTable.latestEntityChangeById[key])) { return; } let change = entity !== undefined ? ({ type: "SET", entityId: key, entity: entity, checkpointId: Internal.loadedFromDbCheckpointId }) : ({ type: "DELETE", entityId: key, checkpointId: Internal.loadedFromDbCheckpointId }); set(inMemTable, committedCheckpointId, change); } function mapChangeToEntity(change) { if (change.type === "SET") { return change.entity; } } function getUnsafe(inMemTable) { return key => mapChangeToEntity(inMemTable.latestEntityChangeById[key]); } function hasIndex(inMemTable) { return filterKey => inMemTable.filterIndices[filterKey] !== undefined; } function getUnsafeOnIndex(inMemTable) { let getEntity = getUnsafe(inMemTable); return filterKey => { let match = inMemTable.filterIndices[filterKey]; if (match !== undefined) { return Stdlib_Array.filterMap(Array.from(match[1]), entityId => { if (entityId in inMemTable.latestEntityChangeById) { return getEntity(entityId); } }); } else { return Stdlib_JsError.throwWithMessage(`Unexpected error. Must have an index for the filter ` + filterKey); } }; } function addEmptyIndex(inMemTable, filter) { let filterKey = EntityFilter.toString(filter); let match = inMemTable.filterIndices[filterKey]; if (match !== undefined) { return; } let relatedEntityIds = new Set(); Utils.Dict.forEach(inMemTable.latestEntityChangeById, change => { let entity = mapChangeToEntity(change); if (entity === undefined) { return; } if (!EntityFilter.matches(filter, entity)) { return; } let entityId = getEntityIdUnsafe(entity); getOrCreateEntityFilters(inMemTable, entityId).add(filter); relatedEntityIds.add(entityId); }); inMemTable.filterIndices[filterKey] = [ filter, relatedEntityIds ]; } let Entity = { UnexpectedIdNotDefinedOnEntity: UnexpectedIdNotDefinedOnEntity, getEntityIdUnsafe: getEntityIdUnsafe, getOrCreateEntityFilters: getOrCreateEntityFilters, make: make, snapshotChanges: snapshotChanges, dropCommittedChanges: dropCommittedChanges, updateIndices: updateIndices, deleteEntityFromIndices: deleteEntityFromIndices, set: set, initValue: initValue, mapChangeToEntity: mapChangeToEntity, getUnsafe: getUnsafe, hasIndex: hasIndex, getUnsafeOnIndex: getUnsafeOnIndex, addEmptyIndex: addEmptyIndex }; export { Entity, } /* Utils Not a pure module */