8000 Implement bulk insert and Graph.link().to() · HowProgrammingWorks/Graph@d2727a7 · GitHub
[go: up one dir, main page]

Skip to content

Commit d2727a7

Browse files
committed
Implement bulk insert and Graph.link().to()
1 parent 875028c commit d2727a7

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

JavaScript/2-insert-link-to.js

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
'use strict';
2+
3+
class Vertex {
4+
constructor(graph, data) {
5+
this.graph = graph;
6+
this.data = data;
7+
this.links = new Map();
8+
}
9+
link(...args) {
10+
const distinct = new Set(args);
11+
const links = this.links;
12+
const keyField = this.graph.keyField;
13+
let item, key;
14+
for (item of distinct) {
15+
key = item.data[keyField];
16+
links.set(key, null);
17+
}
18+
return this;
19+
}
20+
}
21+
22+
class Cursor {
23+
constructor(vertices) {
24+
this.vertices = vertices;
25+
}
26+
linked(...names) {
27+
const vertices = this.vertices;
28+
const result = new Set();
29+
let vertex, condition, name;
30+
for (vertex of vertices) {
31+
condition = true;
32+
for (name of names) {
33+
condition = condition && vertex.links.has(name);
34+
}
35+
if (condition) result.add(vertex);
36+
}
37+
return new Cursor(result);
38+
}
39+
}
40+
41+
class Graph {
42+
constructor(keyField) {
43+
this.keyField = keyField;
44+
this.vertices = new Map();
45+
}
46+
add(data) {
47+
const vertex = new Vertex(this, data);
48+
const key = data[this.keyField];
49+
if (this.vertices.get(key) === undefined) {
50+
this.vertices.set(key, vertex);
51+
}
52+
return vertex;
53+
}
54+
select(query) {
55+
const vertices = new Set();
56+
let vertex, condition, data, field;
57+
for (vertex of this.vertices.values()) {
58+
condition = true;
59+
data = vertex.data;
60+
if (data) {
61+
for (field in query) {
62+
condition = condition && data[field] === query[field];
63+
}
64+
if (condition) vertices.add(vertex);
65+
}
66+
}
67+
return new Cursor(vertices);
68+
}
69+
link(source) {
70+
const vertices = this.vertices;
71+
return {
72+
to(...destinations) {
73+
const from = vertices.get(source);
74+
if (from) {
75+
destinations.forEach((destination) => {
76+
const target = vertices.get(destination);
77+
if (target) from.link(target);
78+
});
79+
}
80+
}
81+
};
82+
}
83+
insert(records) {
84+
for (const record of records) {
85+
this.add(record);
86+
}
87+
}
88+
}
89+
90+
// Usage
91+
92+
const graph = new Graph('name');
93+
94+
graph.insert([
95+
{ name: 'Marcus Aurelius', city: 'Rome', born: 121, dynasty: 'Antonine' },
96+
{ name: 'Lucius Verus', city: 'Rome', born: 130, dynasty: 'Antonine' },
97+
{ name: 'Antoninus Pius', city: 'Lanuvium', born: 86, dynasty: 'Antonine' },
98+
{ name: 'Hadrian', city: 'Santiponce', born: 76, dynasty: 'Nerva–Trajan' },
99+
{ name: 'Trajan', city: 'Sevilla', born: 98, dynasty: 'Nerva–Trajan' }
100+
]);
101+
102+
graph.link('Marcus Aurelius').to('Lucius Verus');
103+
graph.link('Lucius Verus').to('Trajan', 'Marcus Aurelius', 'Marcus Aurelius');
104+
graph.link('Antoninus Pius').to('Marcus Aurelius', 'Lucius Verus');
105+
graph.link('Hadrian').to('Trajan');
106+
graph.link('Trajan').to('Lucius Verus', 'Marcus Aurelius');
107+
108+
console.dir({ graph }, { depth: null });
109+
110+
const res = graph
111+
.select({ city: 'Rome', dynasty: 'Antonine' })
112+
.linked('Trajan');
113+
114+
console.log('\nQuery result:\n');
115+
for (const item of res.vertices) {
116+
console.dir(item.data);
117+
}

0 commit comments

Comments
 (0)
0