A collection of RDF libraries for JavaScript
@graphy/memory.dataset.fast
FastDataset
is used throughout examples in this document’s code sections to refer to the module’s export. This module was previously called DatasetTree..size
– number of quads in the dataset.* [Symbol.iterator](...)
– instances are iterable.add(...)
.addAll(...)
.addQuads(...)
.delete(...)
.deleteQuads(...)
.clear(...)
– remove all quads from the dataset.has(...)
– test if the dataset has a given quad.equals(...)
– A = B
.contains(...)
– (A ∩ B) = B
.disjoint(...)
– (A ∩ B) = Ø
.union(...)
– A ∪ B
.intersection(...)
– A ∩ B
.minus(...)
– A - (A ∩ B)
.difference(...)
– (A - (A ∩ B)) ∪ (B - (A ∩ B))
This data structure is best suited for storing quads in memory when the objective is to perform set operations quickly (e.g., union, difference, intersection, etc.) on relatively small datasets (still much better storage density than the alternatives – see memory usage comparison in the performance document).
Future releases of graphy plan to include other data structures, such as DenseDataset, FastDatacache, and DenseDatacache, for meeting the various trade-offs between storage density and insertion/deletion time. In this implementation, certain set operations may reuse pointers to existing object trees in order to save the time it takes to copy subtrees and in order to reduce the overall memory footprint. This has no effect on user functionality since object reuse is handled internally and all methods ensure that stale objects are released to GC.
The require’d return value is a wrapper function that constructs an instance of Dataset
. The new
keyword is optional.
dataset
([config:
DatasetConfig
])
new Duplex<Quad, Quad>
(accepts Quad objects on its writable side, pushes Quad objects on its readable side)The following example is shown for usage in Node.js, however the same async/await mechanisms can be used in the browser with polyfills for piping fetch
and awaiting stream events.
Read from a Turtle file in Node.js:
const fs = require('fs');
const { once } = require('events');
const ttl_read = require('@graphy/content.ttl.read');
const dataset = require('@graphy/memory.dataset.fast');
// load 'input-a.ttl' into a new Dataset
let y_input_a = dataset();
fs.createReadStream('input-a.ttl')
.pipe(ttl_read())
.pipe(y_input_a);
// load 'input-b.ttl' into a new Dataset
let y_input_b = dataset();
fs.createReadStream('input-b.ttl')
.pipe(ttl_read())
.pipe(y_input_b);
// wait for both datasets to finish loading using async/await
(async() => {
// Dataset extends Node.js' Duplex, so simply listen for the 'finish' event
// to be notified once the dataset has finished loading
await Promise.all([
once(y_input_a, 'finish'),
once(y_input_b, 'finish'),
]);
// compute the union of the two datasets
let y_union = y_input_a.union(y_input_b);
// do something with union...
})();
.size
#number/integer
let y_dataset = dataset();
let h_prefixes = {
dbr: 'http://dbpedia.org/resource/',
rdfs: 'http://www.w3.org/2000/01/rdf-schema#',
graph: 'http://ex.org/graph#',
};
y_dataset.size; // 0
y_dataset.addQuads(factory.c4({
'graph:example': {
'dbr:Banana': {
'rdfs:label': [
'@en"Banana',
'@fr"Banane',
],
},
},
}, h_prefixes));
y_dataset.size; // 2
* [Symbol.iterator]
()
per @RDFJS/DatasetCorethis
..canonicalize
()
.equals()
, .contains()
, .disjoint()
, and prior to using .union()
, .intersection()
, .minus()
, and .difference()
..add
(quad:
AnyQuad
)
implements @RDFJS/DatasetCore.addthis
..addAll
(quads:
@RDFJS/Dataset
| sequence<
AnyQuad
>)
implements @RDFJS/Dataset.addAllthis
.addQuads
(quads:
Iterable
<
Quad
>)
quads
must be an Iterable sequence of graphy Quads.#number/integer
indicating how many quads were successfully added to the dataset..delete
(quad:
AnyQuad
)
implements @RDFJS/DatasetCore.deletequad
from the dataset if it exists.this
.deleteQuads
(quads: list<
Quad
>)
quads
from the dataset if they exist.#number/integer
indicating how many quads were successfully deleted from the dataset..clear
()
undefined
..has
(quad:
AnyQuad
)
implements @RDFJS/DatasetCore.hasquad
.boolean
..equals
(other:
FastDataset
)
implements @RDFJS/Dataset.equalsA = B
this
and other
are strictly equal graphs.boolean
.contains
(other:
FastDataset
)
(A ∩ B) = B
this
contains all quads in other
.boolean
..disjoint
(other:
FastDataset
)
(A ∩ B) = Ø
this
is disjoint with other
.boolean
..union
(other:
FastDataset
)
implements @RDFJS/Dataset.unionthis
and other
..intersection
(other:
FastDataset
)
implements @RDFJS/Dataset.intersectionA ∩ B
this
and other
..minus
(other:
FastDataset
)
(A - (A ∩ B))
other
from this
..difference
(other:
FastDataset
)
implements @RDFJS/Dataset.difference(A - (A ∩ B)) ∪ (B - (A ∩ B))
this
and other
..match
([subject: null |
AnyTerm
[, predicate: null |
AnyTerm
[, object: null |
AnyTerm
[, graph: null |
AnyTerm
]]]])
implements @RDFJS/DatasetCore.matchsubject
, predicate
, object
, and/or graph
, or any quads if null
is given for any role.