diff --git a/lib/graph/file.graph.ts b/lib/graph/file.graph.ts index cd2f87c..33bb751 100644 --- a/lib/graph/file.graph.ts +++ b/lib/graph/file.graph.ts @@ -62,14 +62,31 @@ class FileGraphIml implements FileGraphAbstract { return vertices; } + public async createEdge(ids: uuidType[]): Promise { + const updater = (vertex: IVertex) => { + const index = ids.indexOf(vertex.id); + if (index === -1) return; + const neighbors = [ids[index - 1], ids[index + 1]].filter( + neighbor => neighbor !== undefined, + ); + const links = vertex.links; + neighbors.forEach(neighbor => { + if (!links.includes(neighbor)) links.push(neighbor); + }); + return { links }; + }; + const updateResult = await this.storageFile.updateLine(updater); + return updateResult; + } + public createArc( sourceVertexId: uuidType, targetVertexId: uuidType, ): Promise { return this.updateArc(targetVertexId, vertex => { if (vertex.id !== sourceVertexId) return; - if (!vertex.arcs.includes(targetVertexId)) - return { arcs: [...vertex.arcs, targetVertexId] }; + if (!vertex.links.includes(targetVertexId)) + return { links: [...vertex.links, targetVertexId] }; throw new Error( `targetVertexId: ${targetVertexId} already exists in vertex ${sourceVertexId}`, @@ -83,8 +100,8 @@ class FileGraphIml implements FileGraphAbstract { ): Promise { return this.updateArc(targetVertexId, vertex => { if (vertex.id !== sourceVertexId) return; - if (vertex.arcs.includes(targetVertexId)) - return { arcs: vertex.arcs.filter(v => v !== targetVertexId) }; + if (vertex.links.includes(targetVertexId)) + return { links: vertex.links.filter(v => v !== targetVertexId) }; throw new Error( `targetVertexId: ${targetVertexId} don't exists in vertex ${sourceVertexId}`, @@ -102,7 +119,7 @@ class FileGraphIml implements FileGraphAbstract { if (!targetVertexExists) throw new Error(`Target vertex with ID "${targetVertexId}" not found`); - return targetVertexExists.arcs.includes(targetVertexId); + return targetVertexExists.links.includes(targetVertexId); } private updateArc( @@ -115,13 +132,14 @@ class FileGraphIml implements FileGraphAbstract { ); if (!targetVertexExists) throw new Error(`Target vertex with ID "${targetVertexId}" not found`); + const updateResult = await this.storageFile.updateLine(updater); return updateResult; }); } private vertexTemplate(data: T): IVertex { - return { id: uuid(), data, arcs: [] }; + return { id: uuid(), data, links: [] }; } } diff --git a/lib/graph/storage.file.ts b/lib/graph/storage.file.ts index 16d53d4..648fccf 100644 --- a/lib/graph/storage.file.ts +++ b/lib/graph/storage.file.ts @@ -47,11 +47,11 @@ export class StorageFile { await fileStream(async line => { const vertex = this.deserializer(line) as IVertex; const updaterVertex = updater(vertex); - if (updaterVertex === true) return (updated = true); + if (updaterVertex === true) return void (updated = true); if (typeof updaterVertex === 'object' && updaterVertex !== null) { const newVertices = mergeVertices(vertex, updaterVertex); tempStream.write(this.serializer(newVertices) + '\n'); - return (updated = true); + return void (updated = true); } tempStream.write(line + '\n'); }); diff --git a/lib/interfaces/graph.interface.ts b/lib/interfaces/graph.interface.ts index 542dc99..e1f07e2 100644 --- a/lib/interfaces/graph.interface.ts +++ b/lib/interfaces/graph.interface.ts @@ -74,6 +74,19 @@ export abstract class FileGraphAbstract { predicate: IPredicate, ): Promise[]>; + /** + * Creates edges (links) between the specified vertices. + * + * This method establishes connections between vertices using the provided vertex IDs. + * Each vertex will be connected to the previous and next vertex in the given list of IDs. + * If the vertices are already connected, no duplicate connections will be created. + * + * @param {uuidType[]} ids - An array of vertex IDs between which to create edges. The vertices will be connected in the order they appear in the array. + * + * @returns {Promise} A promise that resolves to a boolean indicating whether the edge creation was successful. + */ + public abstract createEdge(ids: uuidType[]): Promise; + /** * Creates an arc (edge) from one vertex to another in the graph. * diff --git a/lib/interfaces/params.interface.ts b/lib/interfaces/params.interface.ts index 986c671..eb10cbc 100644 --- a/lib/interfaces/params.interface.ts +++ b/lib/interfaces/params.interface.ts @@ -3,7 +3,7 @@ export type uuidType = UUID; export interface IVertex { id: uuidType; - arcs: Array; + links: Array; data: T; } export type IPredicate = (vertex: IVertex) => boolean;