Skip to content

Commit

Permalink
feat: add public createEdge method
Browse files Browse the repository at this point in the history
  • Loading branch information
DIY0R committed Aug 29, 2024
1 parent 7a4d7df commit 6fb8851
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
30 changes: 24 additions & 6 deletions lib/graph/file.graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,31 @@ class FileGraphIml implements FileGraphAbstract {
return vertices;
}

public async createEdge(ids: uuidType[]): Promise<boolean> {
const updater = (vertex: IVertex<object>) => {
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<boolean> {
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}`,
Expand All @@ -83,8 +100,8 @@ class FileGraphIml implements FileGraphAbstract {
): Promise<boolean> {
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}`,
Expand All @@ -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(
Expand All @@ -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<T extends object>(data: T): IVertex<T> {
return { id: uuid(), data, arcs: [] };
return { id: uuid(), data, links: [] };
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/graph/storage.file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export class StorageFile {
await fileStream(async line => {
const vertex = this.deserializer(line) as IVertex<T>;
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');
});
Expand Down
13 changes: 13 additions & 0 deletions lib/interfaces/graph.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ export abstract class FileGraphAbstract {
predicate: IPredicate<T>,

Check warning on line 74 in lib/interfaces/graph.interface.ts

View workflow job for this annotation

GitHub Actions / lint-and-test (20.x)

'predicate' is defined but never used

Check warning on line 74 in lib/interfaces/graph.interface.ts

View workflow job for this annotation

GitHub Actions / lint-and-test (20.x)

'predicate' is defined but never used
): Promise<IVertex<T>[]>;

/**
* 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<boolean>} A promise that resolves to a boolean indicating whether the edge creation was successful.
*/
public abstract createEdge(ids: uuidType[]): Promise<boolean>;

Check warning on line 88 in lib/interfaces/graph.interface.ts

View workflow job for this annotation

GitHub Actions / lint-and-test (20.x)

'ids' is defined but never used

Check warning on line 88 in lib/interfaces/graph.interface.ts

View workflow job for this annotation

GitHub Actions / lint-and-test (20.x)

'ids' is defined but never used

/**
* Creates an arc (edge) from one vertex to another in the graph.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/interfaces/params.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export type uuidType = UUID;

export interface IVertex<T extends object> {
id: uuidType;
arcs: Array<uuidType>;
links: Array<uuidType>;
data: T;
}
export type IPredicate<T extends object> = (vertex: IVertex<T>) => boolean;
Expand Down

0 comments on commit 6fb8851

Please sign in to comment.