import namespaces from './IRIs';
-import { isDefaultGraph } from './N3Util';
+
const { rdf, xsd } = namespaces;
diff --git a/docs/N3DataFactory.html b/docs/N3DataFactory.html index 545dd574..a0064b17 100644 --- a/docs/N3DataFactory.html +++ b/docs/N3DataFactory.html @@ -97,7 +97,7 @@
import namespaces from './IRIs';
-import { isDefaultGraph } from './N3Util';
+
const { rdf, xsd } = namespaces;
let DEFAULTGRAPH;
let _blankNodeCounter = 0;
-const escapedLiteral = /^"(.*".*)(?="[^"]*$)/;
-const quadId = /^<<("(?:""|[^"])*"[^ ]*|[^ ]+) ("(?:""|[^"])*"[^ ]*|[^ ]+) ("(?:""|[^"])*"[^ ]*|[^ ]+) ?("(?:""|[^"])*"[^ ]*|[^ ]+)?>>$/;
The third ‘nested’ parameter of this function is to aid +with recursion over nested terms. It should not be used +by consumers of this library. +See https://github.com/rdfjs/N3.js/pull/311#discussion_r1061042725
-export function termFromId(id, factory) {
+ export function termFromId(id, factory, nested) {
factory = factory || DataFactory;
@@ -815,17 +818,20 @@ Constructs a term f
return factory.literal(id.substr(1, endPos - 1),
id[endPos + 1] === '@' ? id.substr(endPos + 2)
: factory.namedNode(id.substr(endPos + 3)));
- case '<':
- const components = quadId.exec(id);
- return factory.quad(
- termFromId(unescapeQuotes(components[1]), factory),
- termFromId(unescapeQuotes(components[2]), factory),
- termFromId(unescapeQuotes(components[3]), factory),
- components[4] && termFromId(unescapeQuotes(components[4]), factory)
- );
+ case '[':
+ id = JSON.parse(id);
+ break;
default:
- return factory.namedNode(id);
+ if (!nested || !Array.isArray(id)) {
+ return factory.namedNode(id);
+ }
}
+ return factory.quad(
+ termFromId(id[0], factory, true),
+ termFromId(id[1], factory, true),
+ termFromId(id[2], factory, true),
+ id[3] && termFromId(id[3], factory, true)
+ );
}
The third ‘nested’ parameter of this function is to aid +with recursion over nested terms. It should not be used +by consumers of this library. +See https://github.com/rdfjs/N3.js/pull/311#discussion_r1061042725
-export function termToId(term) {
+ export function termToId(term, nested) {
if (typeof term === 'string')
return term;
if (term instanceof Term && term.termType !== 'Quad')
@@ -870,31 +880,16 @@ Const
case 'Literal': return `"${term.value}"${
term.language ? `@${term.language}` :
(term.datatype && term.datatype.value !== xsd.string ? `^^${term.datatype.value}` : '')}`;
- case 'Quad':
-
-
-
-
-
To identify RDF* quad components, we escape quotes by doubling them. -This avoids the overhead of backslash parsing of Turtle-like syntaxes.
- - return `<<${
- escapeQuotes(termToId(term.subject))
- } ${
- escapeQuotes(termToId(term.predicate))
- } ${
- escapeQuotes(termToId(term.object))
- }${
- (isDefaultGraph(term.graph)) ? '' : ` ${termToId(term.graph)}`
- }>>`;
+ case 'Quad':
+ const res = [
+ termToId(term.subject, true),
+ termToId(term.predicate, true),
+ termToId(term.object, true),
+ ];
+ if (term.graph && term.graph.termType !== 'DefaultGraph') {
+ res.push(termToId(term.graph, true));
+ }
+ return nested ? res : JSON.stringify(res);
default: throw new Error(`Unexpected termType: ${term.termType}`);
}
}
export function escapeQuotes(id) {
- return id.replace(escapedLiteral, (_, quoted) => `"${quoted.replace(/"/g, '""')}`);
-}
export function unescapeQuotes(id) {
- return id.replace(escapedLiteral, (_, quoted) => `"${quoted.replace(/""/g, '"')}`);
+ return id.replace(escapedLiteral, (_, quoted) => `"${quoted.replace(/""/g, '"')}`);
}
Create a language-tagged string
@@ -1101,11 +1098,11 @@Automatically determine datatype for booleans and numbers
@@ -1117,11 +1114,11 @@Convert a boolean
@@ -1133,11 +1130,11 @@Convert an integer or double
@@ -1157,11 +1154,11 @@Create a datatyped literal
@@ -1175,11 +1172,11 @@import namespaces from './IRIs';
-import queueMicrotask from 'queue-microtask';
+ import queueMicrotask from 'queue-microtask';
+import namespaces from './IRIs';
const { xsd } = namespaces;
diff --git a/docs/N3Store.html b/docs/N3Store.html
index cebfcf44..d1815d25 100644
--- a/docs/N3Store.html
+++ b/docs/N3Store.html
@@ -94,9 +94,10 @@ N3Store.js
import { default as N3DataFactory, termToId, termFromId } from './N3DataFactory';
-import { Readable } from 'readable-stream';
-import namespaces from './IRIs';
import { Readable } from 'readable-stream';
+import { default as N3DataFactory, termToId, termFromId } from './N3DataFactory';
+import namespaces from './IRIs';
+import { isDefaultGraph } from './N3Util';
this._id = 0;
this._ids = Object.create(null);
- this._ids['><'] = 0; // dummy entry, so the first actual key is non-zero
this._entities = Object.create(null); // inverse of `_ids`
if (quads)
this.addQuads(quads);
- }
This assumes that no graph term is present - we may wish to error if there is one
const str = term && term.termType === 'Quad' ?
+ `.${this._termToNewNumericId(term.subject)}.${this._termToNewNumericId(term.predicate)}.${this._termToNewNumericId(term.object)}${
+ isDefaultGraph(term.graph) ? '' : `.${this._termToNewNumericId(term.graph)}`
+ }`
+ : termToId(term);
+
+ return this._ids[str] || (this._ids[this._entities[++this._id] = str] = this._id);
+ }
size
returns the number of quads in the storesize
returns
Return the quad count if if was cached
@@ -261,11 +313,11 @@size
returns
- Calculate the number of quads by counting to the deepest level
@@ -284,11 +336,11 @@size
returns
- _addToIndex
adds a quad to a three-layered index.Returns if the index has changed, if the entry did not already exist.
@@ -313,11 +365,11 @@_addToIndex
-
+
Create layers as necessary
@@ -329,11 +381,11 @@ _addToIndex
-
+
Setting the key to any value signals the presence of the quad
@@ -348,11 +400,11 @@ _addToIndex
-
+
_removeFromIndex
removes a quad from a three-layered index
@@ -363,11 +415,11 @@ _remov
-
+
Remove the quad from the index
@@ -379,11 +431,11 @@ _remov
-
+
Remove intermediary index layers if they are empty
@@ -398,11 +450,11 @@ _remov
-
+
_findInIndex
finds a set of quads in a three-layered index.
The index base is index0
and the keys at each level are key0
, key1
, and key2
.
@@ -417,17 +469,17 @@
_findI
*_findInIndex(index0, key0, key1, key2, name0, name1, name2, graphId) {
let tmp, index1, index2;
const entityKeys = this._entities;
- const graph = termFromId(graphId, this._factory);
+ const graph = this._termFromId(graphId, this._factory);
const parts = { subject: null, predicate: null, object: null };
-
+
If a key is specified, use only that part of index 0.
@@ -436,16 +488,16 @@ _findI
if (key0) (tmp = index0, index0 = {})[key0] = tmp[key0];
for (const value0 in index0) {
if (index1 = index0[value0]) {
- parts[name0] = termFromId(entityKeys[value0], this._factory);
+ parts[name0] = this._termFromId(entityKeys[value0], this._factory);
-
+
If a key is specified, use only that part of index 1.
@@ -454,16 +506,16 @@ _findI
if (key1) (tmp = index1, index1 = {})[key1] = tmp[key1];
for (const value1 in index1) {
if (index2 = index1[value1]) {
- parts[name1] = termFromId(entityKeys[value1], this._factory);
+ parts[name1] = this._termFromId(entityKeys[value1], this._factory);
-
+
If a key is specified, use only that part of index 2, if it exists.
@@ -474,18 +526,18 @@ _findI
-
+
for (let l = 0; l < values.length; l++) {
- parts[name2] = termFromId(entityKeys[values[l]], this._factory);
+ parts[name2] = this._termFromId(entityKeys[values[l]], this._factory);
yield this._factory.quad(parts.subject, parts.predicate, parts.object, graph);
}
}
@@ -497,11 +549,11 @@ _findI
-
+
_loop
executes the callback on all keys of index 0
@@ -515,11 +567,11 @@ _loop
e
-
+
_loopByKey0
executes the callback on all keys of a certain entry in index 0
@@ -536,11 +588,11 @@
+
_loopByKey1
executes the callback on given keys of all entries in index 0
@@ -558,11 +610,11 @@
+
_loopBy2Keys
executes the callback on given keys of certain entries in index 2
@@ -579,11 +631,11 @@
+
_countInIndex
counts matching quads in a three-layered index.
The index base is index0
and the keys at each level are key0
, key1
, and key2
.
@@ -597,11 +649,11 @@
_cou
-
+
If a key is specified, count only that part of index 0
@@ -614,11 +666,11 @@ _cou
-
+
If a key is specified, count only that part of index 1
@@ -631,11 +683,11 @@ _cou
-
+
If a key is specified, count the quad if it exists
@@ -646,11 +698,11 @@ _cou
-
+
Otherwise, count all quads
@@ -667,11 +719,11 @@ _cou
-
+
_getGraphs
returns an array with the given graph,
or all graphs if the argument is null or undefined.
@@ -689,11 +741,11 @@ _getGraphs
-
+
_uniqueEntities
returns a function that accepts an entity ID
and passes the corresponding entity to callback if it hasn’t occurred before.
@@ -705,7 +757,7 @@ _uni
return id => {
if (!(id in uniqueIds)) {
uniqueIds[id] = true;
- callback(termFromId(this._entities[id], this._factory));
+ callback(this._termFromId(this._entities[id], this._factory));
}
};
}
@@ -713,11 +765,11 @@ _uni
-
+
Public methods
@@ -726,11 +778,11 @@ Public methods
-
+
add
adds the specified quad to the dataset.
Returns the dataset instance it was called on.
@@ -746,11 +798,11 @@
add
adds the sp
-
+
addQuad
adds a new quad to the store.
Returns if the quad index has changed, if the quad did not already exist.
@@ -762,11 +814,11 @@ addQuad
adds a new qu
-
+
Shift arguments if a quad object is given instead of components
@@ -779,29 +831,26 @@ addQuad
adds a new qu
-
+
- subject = termToId(subject);
- predicate = termToId(predicate);
- object = termToId(object);
- graph = termToId(graph);
+ graph = termToId(graph);
-
+
Find the graph that will contain the triple
@@ -812,11 +861,11 @@ addQuad
adds a new qu
-
+
Create the graph if it doesn’t exist yet
@@ -828,11 +877,11 @@ addQuad
adds a new qu
-
+
Freezing a graph helps subsequent add
performance,
and properties will never be modified anyway
@@ -845,11 +894,11 @@ addQuad
adds a new qu
-
+
Since entities can often be long IRIs, we avoid storing them in every index.
Instead, we have a separate index that maps entities to numbers,
@@ -857,11 +906,9 @@
addQuad
adds a new qu
- const ids = this._ids;
- const entities = this._entities;
- subject = ids[subject] || (ids[entities[++this._id] = subject] = this._id);
- predicate = ids[predicate] || (ids[entities[++this._id] = predicate] = this._id);
- object = ids[object] || (ids[entities[++this._id] = object] = this._id);
+ subject = this._termToNewNumericId(subject);
+ predicate = this._termToNewNumericId(predicate);
+ object = this._termToNewNumericId(object);
const changed = this._addToIndex(graphItem.subjects, subject, predicate, object);
this._addToIndex(graphItem.predicates, predicate, object, subject);
@@ -870,11 +917,11 @@ addQuad
adds a new qu
-
+
The cached quad count is now invalid
@@ -887,11 +934,11 @@ addQuad
adds a new qu
-
+
addQuads
adds multiple quads to the store
@@ -905,11 +952,11 @@ addQuads
adds mu
-
+
delete
removes the specified quad from the dataset.
Returns the dataset instance it was called on.
@@ -924,11 +971,11 @@ delete
-
+
has
determines whether a dataset includes a certain quad or quad pattern.
@@ -943,11 +990,11 @@
+
import
adds a stream of quads to the store
@@ -961,11 +1008,11 @@ import
adds a s
-
+
removeQuad
removes a quad from the store if it exists
@@ -976,11 +1023,11 @@ removeQuad<
-
+
Shift arguments if a quad object is given instead of components
@@ -993,39 +1040,36 @@ removeQuad<
-
+
- subject = termToId(subject);
- predicate = termToId(predicate);
- object = termToId(object);
- graph = termToId(graph);
+ graph = termToId(graph);
-
+
- const ids = this._ids, graphs = this._graphs;
+ const graphs = this._graphs;
let graphItem, subjects, predicates;
- if (!(subject = ids[subject]) || !(predicate = ids[predicate]) ||
- !(object = ids[object]) || !(graphItem = graphs[graph]) ||
+ if (!(subject = subject && this._termToNumericId(subject)) || !(predicate = predicate && this._termToNumericId(predicate)) ||
+ !(object = object && this._termToNumericId(object)) || !(graphItem = graphs[graph]) ||
!(subjects = graphItem.subjects[subject]) ||
!(predicates = subjects[predicate]) ||
!(object in predicates))
@@ -1034,11 +1078,11 @@ removeQuad<
-
+
Remove it from all indexes
@@ -1052,11 +1096,11 @@ removeQuad<
-
+
Remove the graph if it is empty
@@ -1070,11 +1114,11 @@ removeQuad<
-
+
removeQuads
removes multiple quads from the store
@@ -1088,11 +1132,11 @@ removeQuads
-
+
remove
removes a stream of quads from the store
@@ -1106,11 +1150,11 @@ remove
rem
-
+
removeMatches
removes all matching quads from the store
Setting any field to undefined
or null
indicates a wildcard.
@@ -1132,11 +1176,11 @@ removeMat
-
+
deleteGraph
removes all triples with the given graph from the store
@@ -1149,11 +1193,11 @@
-
+
getQuads
returns an array of quads matching a pattern.
Setting any field to undefined
or null
indicates a wildcard.
@@ -1167,11 +1211,11 @@ getQuads
-
+
readQuads
returns an generator of quads matching a pattern.
Setting any field to undefined
or null
indicates a wildcard.
@@ -1183,40 +1227,37 @@ readQu
-
+
- subject = subject && termToId(subject);
- predicate = predicate && termToId(predicate);
- object = object && termToId(object);
- graph = graph && termToId(graph);
+ graph = graph && termToId(graph);
- const graphs = this._getGraphs(graph), ids = this._ids;
+ const graphs = this._getGraphs(graph);
let content, subjectId, predicateId, objectId;
-
+
- if (isString(subject) && !(subjectId = ids[subject]) ||
- isString(predicate) && !(predicateId = ids[predicate]) ||
- isString(object) && !(objectId = ids[object]))
+ if (subject && !(subjectId = this._termToNumericId(subject)) ||
+ predicate && !(predicateId = this._termToNumericId(predicate)) ||
+ object && !(objectId = this._termToNumericId(object)))
return;
for (const graphId in graphs) {
@@ -1224,11 +1265,11 @@ readQu
-
+
Only if the specified graph contains triples, there can be results
@@ -1239,11 +1280,11 @@ readQu
-
+
Choose the optimal index, based on what fields are present
@@ -1255,11 +1296,11 @@ readQu
-
+
If subject and object are given, the object index will be the fastest
@@ -1272,11 +1313,11 @@ readQu
-
+
If only subject and possibly predicate are given, the subject index will be the fastest
@@ -1290,11 +1331,11 @@ readQu
-
+
If only predicate and possibly object are given, the predicate index will be the fastest
@@ -1307,11 +1348,11 @@ readQu
-
+
If only object is given, the object index will be the fastest
@@ -1324,11 +1365,11 @@ readQu
-
+
If nothing is given, iterate subjects and predicates first
@@ -1343,11 +1384,11 @@ readQu
-
+
match
returns a new dataset that is comprised of all quads in the current instance matching the given arguments.
The logic described in Quad Matching is applied for each quad in this dataset to check if it should be included in the output dataset.
@@ -1365,11 +1406,11 @@
+
countQuads
returns the number of quads matching a pattern.
Setting any field to undefined
or null
indicates a wildcard.
@@ -1381,40 +1422,37 @@ countQu
-
+
- subject = subject && termToId(subject);
- predicate = predicate && termToId(predicate);
- object = object && termToId(object);
- graph = graph && termToId(graph);
+ graph = graph && termToId(graph);
- const graphs = this._getGraphs(graph), ids = this._ids;
+ const graphs = this._getGraphs(graph);
let count = 0, content, subjectId, predicateId, objectId;
-
+
- if (isString(subject) && !(subjectId = ids[subject]) ||
- isString(predicate) && !(predicateId = ids[predicate]) ||
- isString(object) && !(objectId = ids[object]))
+ if (subject && !(subjectId = this._termToNumericId(subject)) ||
+ predicate && !(predicateId = this._termToNumericId(predicate)) ||
+ object && !(objectId = this._termToNumericId(object)))
return 0;
for (const graphId in graphs) {
@@ -1422,11 +1460,11 @@ countQu
-
+
Only if the specified graph contains triples, there can be results
@@ -1437,11 +1475,11 @@ countQu
-
+
Choose the optimal index, based on what fields are present
@@ -1453,11 +1491,11 @@ countQu
-
+
If subject and object are given, the object index will be the fastest
@@ -1469,11 +1507,11 @@ countQu
-
+
If only subject and possibly predicate are given, the subject index will be the fastest
@@ -1486,11 +1524,11 @@ countQu
-
+
If only predicate and possibly object are given, the predicate index will be the fastest
@@ -1503,11 +1541,11 @@ countQu
-
+
If only object is possibly given, the object index will be the fastest
@@ -1523,11 +1561,11 @@ countQu
-
+
forEach
executes the callback on all quads.
Setting any field to undefined
or null
indicates a wildcard.
@@ -1544,11 +1582,11 @@ forEach
execute
-
+
every
executes the callback on all quads,
and returns true
if it returns truthy for all them.
@@ -1568,11 +1606,11 @@
every
executes th
-
+
some
executes the callback on all quads,
and returns true
if it returns truthy for any of them.
@@ -1590,11 +1628,11 @@
some
executes the
-
+
getSubjects
returns all subjects that match the pattern.
Setting any field to undefined
or null
indicates a wildcard.
@@ -1610,11 +1648,11 @@ getSubjec
-
+
forSubjects
executes the callback on all subjects that match the pattern.
Setting any field to undefined
or null
indicates a wildcard.
@@ -1626,39 +1664,37 @@
+
- predicate = predicate && termToId(predicate);
- object = object && termToId(object);
- graph = graph && termToId(graph);
+