-
Notifications
You must be signed in to change notification settings - Fork 4
/
RDFStore.ts
397 lines (341 loc) · 14 KB
/
RDFStore.ts
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import { DataFactory, Feature, QuadPosition } from "@ontologies/core";
import ld from "@ontologies/ld";
import rdf from "@ontologies/rdf";
import rdfs from "@ontologies/rdfs";
import { equals, id } from "./factoryHelpers";
import ll from "./ontology/ll";
import rdfFactory, {
NamedNode,
OptionalNamedNode,
OptionalNode,
OptionalTerm,
Quad,
Quadruple,
Term,
} from "./rdf";
import { deltaProcessor } from "./store/deltaProcessor";
import RDFIndex from "./store/RDFIndex";
import { ChangeBuffer, DeltaProcessor, SomeNode, StoreProcessor } from "./types";
import { doc, getPropBestLang } from "./utilities";
import { patchRDFLibStoreWithOverrides } from "./utilities/monkeys";
const EMPTY_ST_ARR: ReadonlyArray<Quad> = Object.freeze([]);
export interface RDFStoreOpts {
deltaProcessorOpts?: { [k: string]: NamedNode[] };
innerStore?: RDFIndex;
}
export interface Funlets {
allRDFPropertyStatements: (obj: Quad[] | undefined, predicate: SomeNode) => Quad[];
flushFilter: (changeStamp: number) => (s: Quad) => boolean;
/** Builds a cache of types per resource. Can be omitted when compiled against a well known service. */
processTypeQuad: (quad: Quad) => boolean;
replaceQuadsPicker: (replacement: Quad) => ({ subject, predicate }: Quad) => boolean;
}
const memberPrefix = rdf.ns("_").value;
/**
* Provides a clean consistent interface to stored (RDF) data.
*/
export class RDFStore implements ChangeBuffer, DeltaProcessor {
public changeBuffer: Quad[] = new Array(100);
public changeBufferCount: number = 0;
/**
* Record of the last time a resource was flushed.
*
* @note Not to be confused with the last change in the store, which might be later than the flush time.
*/
public changeTimestamps: number[] = [];
public typeCache: NamedNode[][] = [];
private deltas: Quadruple[][] = [];
private deltaProcessor: StoreProcessor;
private langPrefs: string[] = Array.from(typeof navigator !== "undefined"
? (navigator.languages || [navigator.language])
: ["en"]);
private store: RDFIndex = new RDFIndex();
private funlets: Funlets;
public get rdfFactory(): DataFactory {
return rdfFactory;
}
public set rdfFactory(_: DataFactory) {
throw new Error("Factory is global (see @ontologies/core)");
}
constructor({ deltaProcessorOpts, innerStore }: RDFStoreOpts = {}) {
this.processDelta = this.processDelta.bind(this);
const g = innerStore || new RDFIndex();
this.store = patchRDFLibStoreWithOverrides(g, this);
const defaults = {
addGraphIRIS: [ll.add, ld.add],
purgeGraphIRIS: [ll.purge, ld.purge],
removeGraphIRIS: [ll.remove, ld.remove],
replaceGraphIRIS: [
ll.replace,
ld.replace,
rdfFactory.defaultGraph(),
],
sliceGraphIRIS: [ll.slice, ld.slice],
};
this.deltaProcessor = deltaProcessor(
deltaProcessorOpts?.addGraphIRIS || defaults.addGraphIRIS,
deltaProcessorOpts?.replaceGraphIRIS || defaults.replaceGraphIRIS,
deltaProcessorOpts?.removeGraphIRIS || defaults.removeGraphIRIS,
deltaProcessorOpts?.purgeGraphIRIS || defaults.purgeGraphIRIS,
deltaProcessorOpts?.sliceGraphIRIS || defaults.sliceGraphIRIS,
)(this.store);
const defaultFunlets: Funlets = Object.freeze({
allRDFPropertyStatements: (obj: Quad[] | undefined, predicate: SomeNode): Quad[] => {
if (typeof obj === "undefined") {
return [];
}
if (equals(predicate, rdfs.member)) {
return obj.filter((s) =>
equals(s.predicate, rdfs.member) || s.predicate.value.startsWith(memberPrefix));
}
const t = [];
for (let i = 0; i < obj.length; i++) {
if (equals(predicate, obj[i].predicate)) {
t.push(obj[i]);
}
}
return t;
},
flushFilter: (changeStamp: number): any => (s: Quad): boolean => {
this.changeTimestamps[id(s.subject)] = changeStamp;
this.changeTimestamps[id(doc(s.subject))] = changeStamp;
return equals(s.predicate, rdf.type);
},
processTypeQuad: (quad: Quad): boolean => {
if (!equals(quad.predicate, rdf.type)) {
return false;
}
const subjId = id(this.canon(quad.subject));
if (!Array.isArray(this.typeCache[subjId])) {
this.typeCache[subjId] = [];
}
this.typeCache[subjId] = this.quadsFor((quad.subject as NamedNode))
.filter((s) => equals(s.predicate, rdf.type))
.map((s) => s.object as NamedNode);
return false;
},
replaceQuadsPicker: (replacement: Quad): any => ({ subject, predicate }: Quad): boolean =>
equals(subject, replacement.subject) && equals(predicate, replacement.predicate),
});
const idFunlets: Funlets = Object.freeze({
allRDFPropertyStatements: (obj: Quad[] | undefined, predicate: SomeNode): Quad[] => {
if (typeof obj === "undefined") {
return [];
}
if (predicate === rdfs.member) {
return obj.filter((s) => s.predicate === rdfs.member || s.predicate.value.startsWith(memberPrefix));
}
const t = [];
for (let i = 0; i < obj.length; i++) {
if (equals(predicate, obj[i].predicate)) {
t.push(obj[i]);
}
}
return t;
},
flushFilter: (changeStamp: number): any => (s: Quad): boolean => {
this.changeTimestamps[s.subject.id as number] = changeStamp;
this.changeTimestamps[doc(s.subject).id as number] = changeStamp;
return s.predicate === rdf.type;
},
processTypeQuad: (quad: Quad): boolean => {
if (quad.predicate !== rdf.type) {
return false;
}
const subjId = this.canon(quad.subject).id as number;
if (!Array.isArray(this.typeCache[subjId])) {
this.typeCache[subjId] = [];
}
this.typeCache[subjId] = this.quadsFor((quad.subject as NamedNode))
.filter((s) => s.predicate === rdf.type)
.map((s) => s.object as NamedNode);
return false;
},
replaceQuadsPicker: (replacement: Quad): any => ({ subject, predicate }: Quad): boolean =>
subject === replacement.subject && predicate === replacement.predicate,
});
this.funlets = rdfFactory.supports[Feature.identity] ? idFunlets : defaultFunlets;
g.addDataCallback(this.funlets.processTypeQuad.bind(this));
}
/**
* Add statements to the store.
* @param data Data to parse and add to the store.
*/
public addQuads(data: Quad[]): Quad[] {
if (!Array.isArray(data)) {
throw new TypeError("An array of quads must be passed to addQuads");
}
return data.map((q) => this.store.add(q.subject, q.predicate, q.object, q.graph));
}
public addQuadruples(data: Quadruple[]): Quad[] {
const statements = new Array(data.length);
for (let i = 0, len = data.length; i < len; i++) {
statements[i] = this.store.add(data[i][0], data[i][1], data[i][2], data[i][3]);
}
return statements;
}
public canon(term: SomeNode): SomeNode {
return this.store.canon(term);
}
public defaultGraph(): SomeNode {
return rdfFactory.defaultGraph();
}
/**
* Find the first quad matching the given arguments.
* Use null or undefined as a wild-card.
*/
public find(subj: OptionalNode,
pred: OptionalNamedNode,
obj: OptionalTerm,
graph: OptionalNode): Quad | undefined {
return this.match(subj, pred, obj, graph, true)[0];
}
/**
* Flushes the change buffer to the return value.
* @return Statements held in memory since the last flush.
*/
public flush(): Quad[] {
const deltas = this.deltas;
this.deltas = [];
for (let i = 0; i < deltas.length; i++) {
this.processDelta(deltas[i]);
}
if (this.changeBufferCount === 0) {
return EMPTY_ST_ARR as Quad[];
}
const processingBuffer = this.changeBuffer;
this.changeBuffer = new Array(100);
this.changeBufferCount = 0;
const changeStamp = Date.now();
processingBuffer
.filter(this.funlets.flushFilter(changeStamp))
.map(this.funlets.processTypeQuad);
return processingBuffer;
}
/** @private */
public getInternalStore(): RDFIndex {
return this.store;
}
public match(subj: OptionalNode,
pred: OptionalNamedNode,
obj: OptionalTerm,
graph: OptionalNode,
justOne: boolean = false): Quad[] {
return this.store.match(subj, pred, obj, graph, justOne) || [];
}
public processDelta(delta: Quadruple[]): Quad[] {
const [
addables,
replacables,
removables,
] = this.deltaProcessor(delta);
this.removeQuads(removables);
return this.replaceMatches(replacables).concat(this.addQuadruples(addables));
}
public removeResource(subject: SomeNode): void {
const canSubj = this.canon(subject);
this.touch(canSubj);
this.typeCache[id(canSubj)] = [];
this.removeQuads(this.quadsFor(subject));
}
public removeQuads(statements: Quad[]): void {
this.store.removeQuads(statements);
}
/**
* Removes an array of statements and inserts another.
* Note: Be sure that the replacement contains the same subjects as the original to let the
* broadcast work correctly.
* @access private This is in conflict with the typescript declaration due to the development of some experimental
* features, but this method shouldn't be used nevertheless.
* @param original The statements to remove from the store.
* @param replacements The statements to add to the store.
*/
public replaceQuads(original: Quad[], replacements: Quad[]): Quad[] {
const uniqueStatements = new Array(replacements.length).filter(Boolean);
for (const replacement of replacements) {
const cond = original.some(this.funlets.replaceQuadsPicker);
if (!cond) {
uniqueStatements.push(replacement);
}
}
this.removeQuads(original);
// Remove statements not in the old object. Useful for replacing data loosely related to the main resource.
for (let i = 0; i < uniqueStatements.length; i++) {
this.store.removeMatches(
uniqueStatements[i].subject,
uniqueStatements[i].predicate,
null,
null,
);
}
return this.addQuads(replacements);
}
public replaceMatches(statements: Quadruple[]): Quad[] {
for (let i = 0; i < statements.length; i++) {
this.removeQuads(this.match(
statements[i][0],
statements[i][1],
null,
null,
));
}
return this.addQuadruples(statements);
}
public getResourcePropertyRaw(subject: SomeNode, property: SomeNode | SomeNode[]): Quad[] {
const props = this.quadsFor(subject);
const allProps = this.funlets.allRDFPropertyStatements;
if (Array.isArray(property)) {
for (let i = 0; i < property.length; i++) {
const values = allProps(props, property[i]);
if (values.length > 0) {
return values;
}
}
return EMPTY_ST_ARR as Quad[];
}
return allProps(props, property);
}
public getResourceProperties<TT extends Term = Term>(subject: SomeNode, property: SomeNode | SomeNode[]): TT[] {
if (property === rdf.type) {
return (this.typeCache[id(this.canon(subject))] || []) as TT[];
}
return this
.getResourcePropertyRaw(subject, property)
.map((s) => s.object as TT);
}
public getResourceProperty<T extends Term = Term>(
subject: SomeNode,
property: SomeNode | SomeNode[],
): T | undefined {
if (!Array.isArray(property) && equals(property, rdf.type)) {
const entry = this.typeCache[id(this.canon(subject))];
return entry ? entry[0] as T : undefined;
}
const rawProp = this.getResourcePropertyRaw(subject, property);
if (rawProp.length === 0) {
return undefined;
}
return getPropBestLang<T>(rawProp, this.langPrefs);
}
public queueDelta(delta: Quadruple[]): void {
this.deltas.push(delta);
}
/**
* Searches the store for all the quads on {iri} (so not all statements relating to {iri}).
* @param subject The identifier of the resource.
*/
public quadsFor(subject: SomeNode): Quad[] {
const sId = id(this.store.canon(subject));
return typeof this.store.indices[QuadPosition.subject][sId] !== "undefined"
? this.store.indices[QuadPosition.subject][sId]
: EMPTY_ST_ARR as Quad[];
}
public touch(iri: SomeNode): void {
this.changeTimestamps[id(iri)] = Date.now();
this.changeBuffer.push(rdfFactory.quad(iri, ll.nop, ll.nop));
this.changeBufferCount++;
}
public workAvailable(): number {
return this.deltas.length + this.changeBufferCount;
}
}