-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path3-index.js
More file actions
160 lines (141 loc) · 3.83 KB
/
3-index.js
File metadata and controls
160 lines (141 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
const intersection = (s1, s2) => new Set([...s1].filter((v) => s2.has(v)));
class Vertex {
constructor(graph, data) {
this.graph = graph;
this.data = data;
this.links = new Map();
}
link(...args) {
const distinct = new Set(args);
const { links } = this;
const { keyField } = this.graph;
for (const item of distinct) {
const key = item.data[keyField];
links.set(key, item);
}
return this;
}
}
class Cursor {
constructor(vertices) {
this.vertices = vertices;
}
linked(...names) {
const { vertices } = this;
const result = new Set();
for (const vertex of vertices.values()) {
let condition = true;
for (const name of names) {
condition = condition && vertex.links.has(name);
}
if (condition) result.add(vertex);
}
return new Cursor(result);
}
}
class Graph {
constructor(keyField) {
this.keyField = keyField;
this.vertices = new Map();
this.indices = new Map();
}
add(data) {
const vertex = new Vertex(this, data);
const key = data[this.keyField];
if (this.vertices.get(key) === undefined) {
this.vertices.set(key, vertex);
}
return vertex;
}
select(query) {
let vertices;
const keys = Object.keys(query);
for (const field of keys) {
const idx = this.indices.get(field);
if (idx) {
const value = query[field];
const records = idx.get(value);
vertices = vertices ? intersection(vertices, records) : records;
} else {
vertices = vertices || new Set(this.vertices.values());
for (const vertex of vertices) {
const { data } = vertex;
if (data[field] !== query[field]) {
vertices.delete(vertex);
}
}
}
}
return new Cursor(vertices);
}
static link(from) {
return {
to(...destinations) {
destinations.forEach((target) => {
if (target) from.link(target);
});
},
};
}
insert(rows) {
const vertices = [];
for (const record of rows) {
const vertex = this.add(record);
vertices.push(vertex);
const keys = Object.keys(record);
for (const [key, idx] of this.indices) {
if (keys.includes(key)) {
const value = record[key];
let records = idx.get(value);
if (!records) {
records = new Set();
idx.set(value, records);
}
records.add(vertex);
}
}
}
return vertices;
}
index(key) {
let idx = this.indices.get(key);
if (!idx) {
idx = new Map();
this.indices.set(key, idx);
}
for (const vertex of this.vertices.values()) {
const value = vertex.data[key];
let records = idx.get(value);
if (!records) {
records = new Set();
idx.set(value, records);
}
records.add(vertex);
}
return this;
}
}
// Usage
const graph = new Graph('name').index('city');
const [marcus, lucius, pius, hadrian, trajan] = graph.insert([
{ name: 'Marcus Aurelius', city: 'Rome', born: 121, dynasty: 'Antonine' },
{ name: 'Lucius Verus', city: 'Rome', born: 130, dynasty: 'Antonine' },
{ name: 'Antoninus Pius', city: 'Lanuvium', born: 86, dynasty: 'Antonine' },
{ name: 'Hadrian', city: 'Santiponce', born: 76, dynasty: 'Nerva–Trajan' },
{ name: 'Trajan', city: 'Sevilla', born: 98, dynasty: 'Nerva–Trajan' },
]);
graph.index('dynasty');
Graph.link(marcus).to(lucius);
Graph.link(lucius).to(trajan, marcus, marcus);
Graph.link(pius).to(marcus, lucius);
Graph.link(hadrian).to(trajan);
Graph.link(trajan).to(lucius, marcus);
console.dir({ graph }, { depth: null });
const res = graph
.select({ city: 'Rome', dynasty: 'Antonine' })
.linked('Trajan');
console.log('\nQuery result:\n');
for (const item of res.vertices) {
console.dir(item.data);
}