MCPcopy Create free account
hub / github.com/codemix/graph / YGraphStorage

Class YGraphStorage

packages/y-graph-storage/src/YGraphStorage.ts:30–419  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28export const $OutEKey = makeInternalKey("outE");
29
30export class YGraphStorage implements GraphStorage {
31 #doc: Y.Doc;
32 #config: YGraphStorageConfig;
33 #vertexIdentities: WeakMap<Y.Map<unknown>, StoredVertex>;
34 #edgeIdentities: WeakMap<Y.Map<unknown>, StoredEdge>;
35
36 public constructor(doc: Y.Doc, config: YGraphStorageConfig) {
37 this.#doc = doc;
38 this.#config = config;
39 this.#vertexIdentities = new WeakMap();
40 this.#edgeIdentities = new WeakMap();
41 }
42
43 public get doc(): Y.Doc {
44 return this.#doc;
45 }
46
47 public getVertexCollectionMap(label: string): Y.Map<Y.Map<unknown>> {
48 if (!(label in this.#config.schema.vertices)) {
49 throw new LabelNotFoundError(label);
50 }
51 return this.#doc.getMap(`V:${label}`);
52 }
53
54 public getEdgeCollectionMap(label: string): Y.Map<Y.Map<unknown>> {
55 if (!(label in this.#config.schema.edges)) {
56 throw new LabelNotFoundError(label);
57 }
58 return this.#doc.getMap(`E:${label}`);
59 }
60
61 protected getVertexProperties(label: string, uuid: string, map: Y.Map<unknown>): object {
62 const schema = this.#config.schema.vertices[label];
63 if (schema == null) {
64 throw new LabelNotFoundError(label);
65 }
66 return createLazyPropertyDictionary(schema, map);
67 }
68
69 protected getEdgeProperties(label: string, uuid: string, map: Y.Map<unknown>): object {
70 const schema = this.#config.schema.edges[label];
71 if (schema == null) {
72 throw new LabelNotFoundError(label);
73 }
74 return createLazyPropertyDictionary(schema, map);
75 }
76
77 public getVertexById(id: ElementId): StoredVertex | undefined {
78 const [label, uuid] = parseElementId(id);
79 const collection = this.getVertexCollectionMap(label);
80 const data = collection.get(uuid);
81 if (data == null) {
82 return undefined;
83 }
84 const existing = this.#vertexIdentities.get(data);
85 if (existing != null) {
86 return existing;
87 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected