Skip to content

Commit 19ed891

Browse files
committed
chore: use _termToNumericId to convert Ids
1 parent 49e9fb6 commit 19ed891

2 files changed

Lines changed: 57 additions & 67 deletions

File tree

src/N3Store.js

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

@@ -15,7 +14,6 @@ export default class N3Store {
1514
// saving memory by using only numbers as keys in `_graphs`
1615
this._id = 0;
1716
this._ids = Object.create(null);
18-
this._ids['><'] = 0; // dummy entry, so the first actual key is non-zero
1917
this._entities = Object.create(null); // inverse of `_ids`
2018
// `_blankNodeIndex` is the index of the last automatically named blank node
2119
this._blankNodeIndex = 0;
@@ -38,35 +36,48 @@ export default class N3Store {
3836
const q = this._factory.quad(
3937
this._termFromId(entities[terms[1]]),
4038
this._termFromId(entities[terms[2]]),
41-
this._termFromId(entities[terms[3]]),
42-
terms[4] && this._termFromId(entities[terms[4]])
39+
this._termFromId(entities[terms[3]])
4340
);
4441
return q;
4542
}
4643
return termFromId(id, factory);
4744
}
4845

4946
_termToId(term) {
50-
if (term && (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-
47+
if (term && term.termType === 'Quad') {
5848
const res = `.${
59-
ids[subject] || (ids[entities[++this._id] = subject] = this._id)
49+
this._termToNewNumericId(term.subject)
6050
}.${
61-
ids[predicate] || (ids[entities[++this._id] = predicate] = this._id)
51+
this._termToNewNumericId(term.predicate)
6252
}.${
63-
ids[object] || (ids[entities[++this._id] = object] = this._id)
64-
}${graph ? (`.${ids[graph] || (ids[entities[++this._id] = graph] = this._id)}`) : ''}`;
53+
this._termToNewNumericId(term.object)
54+
}`;
6555
return res;
6656
}
6757
return termToId(term);
6858
}
6959

60+
_termToNumericId(term) {
61+
if (term.termType === 'Quad') {
62+
const s = this._termToNumericId(term.subject),
63+
p = this._termToNumericId(term.predicate),
64+
o = this._termToNumericId(term.object);
65+
66+
return s && p && o && this._ids[`.${s}.${p}.${o}`];
67+
}
68+
69+
return this._ids[this._termToId(term)];
70+
}
71+
72+
_termToNewNumericId(term) {
73+
// This assumes that no graph term is present - we may wish to error if there is one
74+
const str = term && term.termType === 'Quad' ?
75+
`.${this._termToNewNumericId(term.subject)}.${this._termToNewNumericId(term.predicate)}.${this._termToNewNumericId(term.object)}`
76+
: termToId(term);
77+
78+
return this._ids[str] || (this._ids[this._entities[++this._id] = str] = this._id);
79+
}
80+
7081
// ## Public properties
7182

7283
// ### `size` returns the number of quads in the store
@@ -251,9 +262,6 @@ export default class N3Store {
251262
predicate = subject.predicate, subject = subject.subject;
252263

253264
// Convert terms to internal string representation
254-
subject = this._termToId(subject);
255-
predicate = this._termToId(predicate);
256-
object = this._termToId(object);
257265
graph = this._termToId(graph);
258266

259267
// Find the graph that will contain the triple
@@ -269,11 +277,9 @@ export default class N3Store {
269277
// Since entities can often be long IRIs, we avoid storing them in every index.
270278
// Instead, we have a separate index that maps entities to numbers,
271279
// which are then used as keys in the other indexes.
272-
const ids = this._ids;
273-
const entities = this._entities;
274-
subject = ids[subject] || (ids[entities[++this._id] = subject] = this._id);
275-
predicate = ids[predicate] || (ids[entities[++this._id] = predicate] = this._id);
276-
object = ids[object] || (ids[entities[++this._id] = object] = this._id);
280+
subject = this._termToNewNumericId(subject);
281+
predicate = this._termToNewNumericId(predicate);
282+
object = this._termToNewNumericId(object);
277283

278284
const changed = this._addToIndex(graphItem.subjects, subject, predicate, object);
279285
this._addToIndex(graphItem.predicates, predicate, object, subject);
@@ -318,17 +324,14 @@ export default class N3Store {
318324
predicate = subject.predicate, subject = subject.subject;
319325

320326
// Convert terms to internal string representation
321-
subject = this._termToId(subject);
322-
predicate = this._termToId(predicate);
323-
object = this._termToId(object);
324327
graph = this._termToId(graph);
325328

326329
// Find internal identifiers for all components
327330
// and verify the quad exists.
328-
const ids = this._ids, graphs = this._graphs;
331+
const graphs = this._graphs;
329332
let graphItem, subjects, predicates;
330-
if (!(subject = ids[subject]) || !(predicate = ids[predicate]) ||
331-
!(object = ids[object]) || !(graphItem = graphs[graph]) ||
333+
if (!(subject = this._termToNewNumericId(subject)) || !(predicate = this._termToNewNumericId(predicate)) ||
334+
!(object = this._termToNewNumericId(object)) || !(graphItem = graphs[graph]) ||
332335
!(subjects = graphItem.subjects[subject]) ||
333336
!(predicates = subjects[predicate]) ||
334337
!(object in predicates))
@@ -387,18 +390,15 @@ export default class N3Store {
387390
// Setting any field to `undefined` or `null` indicates a wildcard.
388391
*readQuads(subject, predicate, object, graph) {
389392
// Convert terms to internal string representation
390-
subject = subject && this._termToId(subject);
391-
predicate = predicate && this._termToId(predicate);
392-
object = object && this._termToId(object);
393393
graph = graph && this._termToId(graph);
394394

395-
const graphs = this._getGraphs(graph), ids = this._ids;
395+
const graphs = this._getGraphs(graph);
396396
let content, subjectId, predicateId, objectId;
397397

398398
// Translate IRIs to internal index keys.
399-
if (isString(subject) && !(subjectId = ids[subject]) ||
400-
isString(predicate) && !(predicateId = ids[predicate]) ||
401-
isString(object) && !(objectId = ids[object]))
399+
if (subject && !(subjectId = this._termToNewNumericId(subject)) ||
400+
predicate && !(predicateId = this._termToNewNumericId(predicate)) ||
401+
object && !(objectId = this._termToNewNumericId(object)))
402402
return;
403403

404404
for (const graphId in graphs) {
@@ -445,18 +445,15 @@ export default class N3Store {
445445
// Setting any field to `undefined` or `null` indicates a wildcard.
446446
countQuads(subject, predicate, object, graph) {
447447
// Convert terms to internal string representation
448-
subject = subject && this._termToId(subject);
449-
predicate = predicate && this._termToId(predicate);
450-
object = object && this._termToId(object);
451448
graph = graph && this._termToId(graph);
452449

453-
const graphs = this._getGraphs(graph), ids = this._ids;
450+
const graphs = this._getGraphs(graph);
454451
let count = 0, content, subjectId, predicateId, objectId;
455452

456453
// Translate IRIs to internal index keys.
457-
if (isString(subject) && !(subjectId = ids[subject]) ||
458-
isString(predicate) && !(predicateId = ids[predicate]) ||
459-
isString(object) && !(objectId = ids[object]))
454+
if (subject && !(subjectId = this._termToNewNumericId(subject)) ||
455+
predicate && !(predicateId = this._termToNewNumericId(predicate)) ||
456+
object && !(objectId = this._termToNewNumericId(object)))
460457
return 0;
461458

462459
for (const graphId in graphs) {
@@ -527,17 +524,15 @@ export default class N3Store {
527524
// Setting any field to `undefined` or `null` indicates a wildcard.
528525
forSubjects(callback, predicate, object, graph) {
529526
// Convert terms to internal string representation
530-
predicate = predicate && this._termToId(predicate);
531-
object = object && this._termToId(object);
532527
graph = graph && this._termToId(graph);
533528

534-
const ids = this._ids, graphs = this._getGraphs(graph);
529+
const graphs = this._getGraphs(graph);
535530
let content, predicateId, objectId;
536531
callback = this._uniqueEntities(callback);
537532

538533
// Translate IRIs to internal index keys.
539-
if (isString(predicate) && !(predicateId = ids[predicate]) ||
540-
isString(object) && !(objectId = ids[object]))
534+
if (predicate && !(predicateId = this._termToNewNumericId(predicate)) ||
535+
object && !(objectId = this._termToNewNumericId(object)))
541536
return;
542537

543538
for (graph in graphs) {
@@ -574,17 +569,15 @@ export default class N3Store {
574569
// Setting any field to `undefined` or `null` indicates a wildcard.
575570
forPredicates(callback, subject, object, graph) {
576571
// Convert terms to internal string representation
577-
subject = subject && this._termToId(subject);
578-
object = object && this._termToId(object);
579572
graph = graph && this._termToId(graph);
580573

581-
const ids = this._ids, graphs = this._getGraphs(graph);
574+
const graphs = this._getGraphs(graph);
582575
let content, subjectId, objectId;
583576
callback = this._uniqueEntities(callback);
584577

585578
// Translate IRIs to internal index keys.
586-
if (isString(subject) && !(subjectId = ids[subject]) ||
587-
isString(object) && !(objectId = ids[object]))
579+
if (subject && !(subjectId = this._termToNewNumericId(subject)) ||
580+
object && !(objectId = this._termToNewNumericId(object)))
588581
return;
589582

590583
for (graph in graphs) {
@@ -621,17 +614,15 @@ export default class N3Store {
621614
// Setting any field to `undefined` or `null` indicates a wildcard.
622615
forObjects(callback, subject, predicate, graph) {
623616
// Convert terms to internal string representation
624-
subject = subject && this._termToId(subject);
625-
predicate = predicate && this._termToId(predicate);
626617
graph = graph && this._termToId(graph);
627618

628-
const ids = this._ids, graphs = this._getGraphs(graph);
619+
const graphs = this._getGraphs(graph);
629620
let content, subjectId, predicateId;
630621
callback = this._uniqueEntities(callback);
631622

632623
// Translate IRIs to internal index keys.
633-
if (isString(subject) && !(subjectId = ids[subject]) ||
634-
isString(predicate) && !(predicateId = ids[predicate]))
624+
if (subject && !(subjectId = this._termToNewNumericId(subject)) ||
625+
predicate && !(predicateId = this._termToNewNumericId(predicate)))
635626
return;
636627

637628
for (graph in graphs) {

test/N3Store-test.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ describe('Store', () => {
283283
new Quad(new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')), new NamedNode('p2'), new NamedNode('o2')),
284284
new Quad(new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')), new NamedNode('p1'), new NamedNode('o2')),
285285
new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o2')),
286-
]
286+
];
287287
before(() => {
288288
store = new Store(allQuads);
289289
});
@@ -292,17 +292,16 @@ describe('Store', () => {
292292
store.size.should.eql(5);
293293
});
294294

295-
it('should return the removed quads', async () => {
296-
const quads = await arrayifyStream(store.removeMatches(null, 'p2', 'o2'));
295+
it('should return the removed quads', () => arrayifyStream(store.removeMatches(null, 'p2', 'o2')).then(quads => {
297296
quads.length.should.equal(1);
298297
quads[0].equals(
299298
new Quad(
300-
new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')),
301-
new NamedNode('p2'),
299+
new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')),
300+
new NamedNode('p2'),
302301
new NamedNode('o2')
303302
)
304303
).should.equal(true);
305-
})
304+
}));
306305

307306
it('should decrease the size', () => {
308307
store.size.should.eql(4);
@@ -330,8 +329,8 @@ describe('Store', () => {
330329
new Quad(
331330
new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o2')),
332331
new NamedNode('p1'),
333-
new NamedNode('o2'))
334-
]);
332+
new NamedNode('o2')),
333+
]);
335334

336335
const stream = s2.removeMatches(new Quad(new NamedNode('s1'), new NamedNode('p1'), new NamedNode('o1')));
337336
stream.on('end', () => {

0 commit comments

Comments
 (0)