|
2 | 2 | import { default as N3DataFactory, termToId, termFromId } from './N3DataFactory'; |
3 | 3 | import { Readable } from 'readable-stream'; |
4 | 4 | import namespaces from './IRIs'; |
| 5 | +import { isDefaultGraph } from './N3Util'; |
5 | 6 |
|
6 | 7 | // ## Constructor |
7 | 8 | export default class N3Store { |
@@ -36,45 +37,33 @@ export default class N3Store { |
36 | 37 | const q = this._factory.quad( |
37 | 38 | this._termFromId(entities[terms[1]]), |
38 | 39 | this._termFromId(entities[terms[2]]), |
39 | | - this._termFromId(entities[terms[3]]) |
40 | | - // terms[4] && this._termFromId(entities[terms[4]]) |
| 40 | + this._termFromId(entities[terms[3]]), |
| 41 | + terms[4] && this._termFromId(entities[terms[4]]) |
41 | 42 | ); |
42 | 43 | return q; |
43 | 44 | } |
44 | 45 | return termFromId(id, factory); |
45 | 46 | } |
46 | 47 |
|
47 | | - _termToId(term) { |
48 | | - if (term && term.termType === 'Quad') { |
49 | | - // const g = this._termToNewNumericId(term.graph) |
50 | | - const res = `.${ |
51 | | - this._termToNewNumericId(term.subject) |
52 | | - }.${ |
53 | | - this._termToNewNumericId(term.predicate) |
54 | | - }.${ |
55 | | - this._termToNewNumericId(term.object) |
56 | | - }`; |
57 | | - return res; |
58 | | - } |
59 | | - return termToId(term); |
60 | | - } |
61 | | - |
62 | 48 | _termToNumericId(term) { |
63 | 49 | if (term.termType === 'Quad') { |
64 | 50 | const s = this._termToNumericId(term.subject), |
65 | 51 | p = this._termToNumericId(term.predicate), |
66 | 52 | o = this._termToNumericId(term.object); |
| 53 | + let g; |
67 | 54 |
|
68 | | - return s && p && o && this._ids[`.${s}.${p}.${o}`]; |
| 55 | + return s && p && o && (isDefaultGraph(term.graph) || (g = this._termToNumericId(term.graph))) && |
| 56 | + this._ids[g ? `.${s}.${p}.${o}.${g}` : `.${s}.${p}.${o}`]; |
69 | 57 | } |
70 | | - |
71 | | - return this._ids[this._termToId(term)]; |
| 58 | + return this._ids[termToId(term)]; |
72 | 59 | } |
73 | 60 |
|
74 | 61 | _termToNewNumericId(term) { |
75 | 62 | // This assumes that no graph term is present - we may wish to error if there is one |
76 | 63 | const str = term && term.termType === 'Quad' ? |
77 | | - `.${this._termToNewNumericId(term.subject)}.${this._termToNewNumericId(term.predicate)}.${this._termToNewNumericId(term.object)}` |
| 64 | + `.${this._termToNewNumericId(term.subject)}.${this._termToNewNumericId(term.predicate)}.${this._termToNewNumericId(term.object)}${ |
| 65 | + isDefaultGraph(term.graph) ? '' : `.${this._termToNewNumericId(term.graph)}` |
| 66 | + }` |
78 | 67 | : termToId(term); |
79 | 68 |
|
80 | 69 | return this._ids[str] || (this._ids[this._entities[++this._id] = str] = this._id); |
@@ -264,7 +253,7 @@ export default class N3Store { |
264 | 253 | predicate = subject.predicate, subject = subject.subject; |
265 | 254 |
|
266 | 255 | // Convert terms to internal string representation |
267 | | - graph = this._termToId(graph); |
| 256 | + graph = termToId(graph); |
268 | 257 |
|
269 | 258 | // Find the graph that will contain the triple |
270 | 259 | let graphItem = this._graphs[graph]; |
@@ -326,7 +315,7 @@ export default class N3Store { |
326 | 315 | predicate = subject.predicate, subject = subject.subject; |
327 | 316 |
|
328 | 317 | // Convert terms to internal string representation |
329 | | - graph = this._termToId(graph); |
| 318 | + graph = termToId(graph); |
330 | 319 |
|
331 | 320 | // Find internal identifiers for all components |
332 | 321 | // and verify the quad exists. |
@@ -392,7 +381,7 @@ export default class N3Store { |
392 | 381 | // Setting any field to `undefined` or `null` indicates a wildcard. |
393 | 382 | *readQuads(subject, predicate, object, graph) { |
394 | 383 | // Convert terms to internal string representation |
395 | | - graph = graph && this._termToId(graph); |
| 384 | + graph = graph && termToId(graph); |
396 | 385 |
|
397 | 386 | const graphs = this._getGraphs(graph); |
398 | 387 | let content, subjectId, predicateId, objectId; |
@@ -447,7 +436,7 @@ export default class N3Store { |
447 | 436 | // Setting any field to `undefined` or `null` indicates a wildcard. |
448 | 437 | countQuads(subject, predicate, object, graph) { |
449 | 438 | // Convert terms to internal string representation |
450 | | - graph = graph && this._termToId(graph); |
| 439 | + graph = graph && termToId(graph); |
451 | 440 |
|
452 | 441 | const graphs = this._getGraphs(graph); |
453 | 442 | let count = 0, content, subjectId, predicateId, objectId; |
@@ -526,7 +515,7 @@ export default class N3Store { |
526 | 515 | // Setting any field to `undefined` or `null` indicates a wildcard. |
527 | 516 | forSubjects(callback, predicate, object, graph) { |
528 | 517 | // Convert terms to internal string representation |
529 | | - graph = graph && this._termToId(graph); |
| 518 | + graph = graph && termToId(graph); |
530 | 519 |
|
531 | 520 | const graphs = this._getGraphs(graph); |
532 | 521 | let content, predicateId, objectId; |
@@ -571,7 +560,7 @@ export default class N3Store { |
571 | 560 | // Setting any field to `undefined` or `null` indicates a wildcard. |
572 | 561 | forPredicates(callback, subject, object, graph) { |
573 | 562 | // Convert terms to internal string representation |
574 | | - graph = graph && this._termToId(graph); |
| 563 | + graph = graph && termToId(graph); |
575 | 564 |
|
576 | 565 | const graphs = this._getGraphs(graph); |
577 | 566 | let content, subjectId, objectId; |
@@ -616,7 +605,7 @@ export default class N3Store { |
616 | 605 | // Setting any field to `undefined` or `null` indicates a wildcard. |
617 | 606 | forObjects(callback, subject, predicate, graph) { |
618 | 607 | // Convert terms to internal string representation |
619 | | - graph = graph && this._termToId(graph); |
| 608 | + graph = graph && termToId(graph); |
620 | 609 |
|
621 | 610 | const graphs = this._getGraphs(graph); |
622 | 611 | let content, subjectId, predicateId; |
|
0 commit comments