Skip to content

Commit 9d8afd8

Browse files
committed
perf: mint quad ids using term ids
1 parent 211bf07 commit 9d8afd8

1 file changed

Lines changed: 67 additions & 30 deletions

File tree

src/N3Store.js

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// **N3Store** objects store N3 quads by graph in memory.
22
import { default as N3DataFactory, termToId, termFromId } from './N3DataFactory';
3+
import { isDefaultGraph } from './N3Util';
34
import { Readable } from 'readable-stream';
45
import namespaces from './IRIs';
56

@@ -30,6 +31,42 @@ export default class N3Store {
3031
this.addQuads(quads);
3132
}
3233

34+
_termFromId(id, factory) {
35+
if (id[0] === '_') {
36+
const entities = this._entities;
37+
const terms = id.split('_');
38+
const q = this._factory.quad(
39+
this._termFromId(entities[terms[1]]),
40+
this._termFromId(entities[terms[2]]),
41+
this._termFromId(entities[terms[3]]),
42+
terms[4] && this._termFromId(entities[terms[4]]),
43+
)
44+
return q;
45+
}
46+
return termFromId(id, factory);
47+
}
48+
49+
_termToId(term) {
50+
if (term.termType === 'Quad') {
51+
const subject = this._termToId(term.subject),
52+
predicate = this._termToId(term.predicate),
53+
object = this._termToId(term.object),
54+
graph = isDefaultGraph(term.graph) && this._termToId(term.graph),
55+
ids = this._ids,
56+
entities = this._entities;
57+
58+
const res = `_${
59+
ids[subject] || (ids[entities[++this._id] = subject] = this._id)
60+
}_${
61+
ids[predicate] || (ids[entities[++this._id] = predicate] = this._id)
62+
}_${
63+
ids[object] || (ids[entities[++this._id] = object] = this._id)
64+
}${graph ? ('_' + (ids[graph] || (ids[entities[++this._id] = graph] = this._id))) : ''}`;
65+
return res;
66+
}
67+
return termToId(term);
68+
}
69+
3370
// ## Public properties
3471

3572
// ### `size` returns the number of quads in the store
@@ -88,24 +125,24 @@ export default class N3Store {
88125
*_findInIndex(index0, key0, key1, key2, name0, name1, name2, graphId) {
89126
let tmp, index1, index2;
90127
const entityKeys = this._entities;
91-
const graph = termFromId(graphId, this._factory);
128+
const graph = this._termFromId(graphId, this._factory);
92129
const parts = { subject: null, predicate: null, object: null };
93130

94131
// If a key is specified, use only that part of index 0.
95132
if (key0) (tmp = index0, index0 = {})[key0] = tmp[key0];
96133
for (const value0 in index0) {
97134
if (index1 = index0[value0]) {
98-
parts[name0] = termFromId(entityKeys[value0], this._factory);
135+
parts[name0] = this._termFromId(entityKeys[value0], this._factory);
99136
// If a key is specified, use only that part of index 1.
100137
if (key1) (tmp = index1, index1 = {})[key1] = tmp[key1];
101138
for (const value1 in index1) {
102139
if (index2 = index1[value1]) {
103-
parts[name1] = termFromId(entityKeys[value1], this._factory);
140+
parts[name1] = this._termFromId(entityKeys[value1], this._factory);
104141
// If a key is specified, use only that part of index 2, if it exists.
105142
const values = key2 ? (key2 in index2 ? [key2] : []) : Object.keys(index2);
106143
// Create quads for all items found in index 2.
107144
for (let l = 0; l < values.length; l++) {
108-
parts[name2] = termFromId(entityKeys[values[l]], this._factory);
145+
parts[name2] = this._termFromId(entityKeys[values[l]], this._factory);
109146
yield this._factory.quad(parts.subject, parts.predicate, parts.object, graph);
110147
}
111148
}
@@ -190,7 +227,7 @@ export default class N3Store {
190227
return id => {
191228
if (!(id in uniqueIds)) {
192229
uniqueIds[id] = true;
193-
callback(termFromId(this._entities[id], this._factory));
230+
callback(this._termFromId(this._entities[id], this._factory));
194231
}
195232
};
196233
}
@@ -214,10 +251,10 @@ export default class N3Store {
214251
predicate = subject.predicate, subject = subject.subject;
215252

216253
// Convert terms to internal string representation
217-
subject = termToId(subject);
218-
predicate = termToId(predicate);
219-
object = termToId(object);
220-
graph = termToId(graph);
254+
subject = this._termToId(subject);
255+
predicate = this._termToId(predicate);
256+
object = this._termToId(object);
257+
graph = this._termToId(graph);
221258

222259
// Find the graph that will contain the triple
223260
let graphItem = this._graphs[graph];
@@ -281,10 +318,10 @@ export default class N3Store {
281318
predicate = subject.predicate, subject = subject.subject;
282319

283320
// Convert terms to internal string representation
284-
subject = termToId(subject);
285-
predicate = termToId(predicate);
286-
object = termToId(object);
287-
graph = termToId(graph);
321+
subject = this._termToId(subject);
322+
predicate = this._termToId(predicate);
323+
object = this._termToId(object);
324+
graph = this._termToId(graph);
288325

289326
// Find internal identifiers for all components
290327
// and verify the quad exists.
@@ -350,10 +387,10 @@ export default class N3Store {
350387
// Setting any field to `undefined` or `null` indicates a wildcard.
351388
*readQuads(subject, predicate, object, graph) {
352389
// Convert terms to internal string representation
353-
subject = subject && termToId(subject);
354-
predicate = predicate && termToId(predicate);
355-
object = object && termToId(object);
356-
graph = graph && termToId(graph);
390+
subject = subject && this._termToId(subject);
391+
predicate = predicate && this._termToId(predicate);
392+
object = object && this._termToId(object);
393+
graph = graph && this._termToId(graph);
357394

358395
const graphs = this._getGraphs(graph), ids = this._ids;
359396
let content, subjectId, predicateId, objectId;
@@ -408,10 +445,10 @@ export default class N3Store {
408445
// Setting any field to `undefined` or `null` indicates a wildcard.
409446
countQuads(subject, predicate, object, graph) {
410447
// Convert terms to internal string representation
411-
subject = subject && termToId(subject);
412-
predicate = predicate && termToId(predicate);
413-
object = object && termToId(object);
414-
graph = graph && termToId(graph);
448+
subject = subject && this._termToId(subject);
449+
predicate = predicate && this._termToId(predicate);
450+
object = object && this._termToId(object);
451+
graph = graph && this._termToId(graph);
415452

416453
const graphs = this._getGraphs(graph), ids = this._ids;
417454
let count = 0, content, subjectId, predicateId, objectId;
@@ -490,9 +527,9 @@ export default class N3Store {
490527
// Setting any field to `undefined` or `null` indicates a wildcard.
491528
forSubjects(callback, predicate, object, graph) {
492529
// Convert terms to internal string representation
493-
predicate = predicate && termToId(predicate);
494-
object = object && termToId(object);
495-
graph = graph && termToId(graph);
530+
predicate = predicate && this._termToId(predicate);
531+
object = object && this._termToId(object);
532+
graph = graph && this._termToId(graph);
496533

497534
const ids = this._ids, graphs = this._getGraphs(graph);
498535
let content, predicateId, objectId;
@@ -537,9 +574,9 @@ export default class N3Store {
537574
// Setting any field to `undefined` or `null` indicates a wildcard.
538575
forPredicates(callback, subject, object, graph) {
539576
// Convert terms to internal string representation
540-
subject = subject && termToId(subject);
541-
object = object && termToId(object);
542-
graph = graph && termToId(graph);
577+
subject = subject && this._termToId(subject);
578+
object = object && this._termToId(object);
579+
graph = graph && this._termToId(graph);
543580

544581
const ids = this._ids, graphs = this._getGraphs(graph);
545582
let content, subjectId, objectId;
@@ -584,9 +621,9 @@ export default class N3Store {
584621
// Setting any field to `undefined` or `null` indicates a wildcard.
585622
forObjects(callback, subject, predicate, graph) {
586623
// Convert terms to internal string representation
587-
subject = subject && termToId(subject);
588-
predicate = predicate && termToId(predicate);
589-
graph = graph && termToId(graph);
624+
subject = subject && this._termToId(subject);
625+
predicate = predicate && this._termToId(predicate);
626+
graph = graph && this._termToId(graph);
590627

591628
const ids = this._ids, graphs = this._getGraphs(graph);
592629
let content, subjectId, predicateId;

0 commit comments

Comments
 (0)