@wazootech/sparql-engine 0.0.0-stage → 0.5.8-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +729 -2
- package/THIRD_PARTY_NOTICES.md +20 -0
- package/dist/ast-CIKmrXH9.d.ts +282 -0
- package/dist/data-factory-76Dxrf0g.js +286 -0
- package/dist/index.d.ts +365 -0
- package/dist/index.js +5023 -0
- package/dist/memory-store-DhAGI-j6.d.ts +102 -0
- package/dist/memory-store-Mq4x2Ya1.js +316 -0
- package/dist/mod-C4GFefO7.d.ts +78 -0
- package/dist/parser-CTZiNJfu.js +69170 -0
- package/dist/parser.d.ts +65 -0
- package/dist/parser.js +1 -0
- package/dist/public-BNHiaGho.d.ts +234 -0
- package/dist/public-D0uwoGhU.js +416 -0
- package/dist/rdf-terms-DGpxNnyn.d.ts +83 -0
- package/dist/serialize-BSkHhuBp.js +335 -0
- package/dist/serialize.d.ts +2 -0
- package/dist/serialize.js +1 -0
- package/dist/sparql-engine-interface-Bies9KjE.d.ts +102 -0
- package/dist/store.d.ts +2 -0
- package/dist/store.js +1 -0
- package/dist/term-s9I2y-ck.js +767 -0
- package/dist/term.d.ts +2 -0
- package/dist/term.js +4 -0
- package/dist/turtle-parser-CzWF7KXf.d.ts +83 -0
- package/package.json +117 -4
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { a as RdfQuadGraph, c as RdfStream, i as RdfQuad, l as RdfTerm, s as RdfStore } from "./rdf-terms-DGpxNnyn.js";
|
|
2
|
+
type Listener$1 = (...args: unknown[]) => void;
|
|
3
|
+
/**
|
|
4
|
+
* MiniEventEmitter implements the EventEmitter surface the `rdfjs.Stream`
|
|
5
|
+
* interface requires without depending on Node's `events` module, keeping the
|
|
6
|
+
* engine runtime dependency-free and browser-friendly.
|
|
7
|
+
*/
|
|
8
|
+
declare abstract class MiniEventEmitter {
|
|
9
|
+
protected _listeners: Map<string | symbol, Listener$1[]>;
|
|
10
|
+
protected _maxListeners: number;
|
|
11
|
+
on(eventName: string | symbol, listener: Listener$1): this;
|
|
12
|
+
protected onRaw(eventName: string | symbol, listener: Listener$1): this;
|
|
13
|
+
addListener(eventName: string | symbol, listener: Listener$1): this;
|
|
14
|
+
once(eventName: string | symbol, listener: Listener$1): this;
|
|
15
|
+
prependListener(eventName: string | symbol, listener: Listener$1): this;
|
|
16
|
+
prependOnceListener(eventName: string | symbol, listener: Listener$1): this;
|
|
17
|
+
removeListener(eventName: string | symbol, listener: Listener$1): this;
|
|
18
|
+
off(eventName: string | symbol, listener: Listener$1): this;
|
|
19
|
+
removeAllListeners(eventName?: string | symbol): this;
|
|
20
|
+
setMaxListeners(n: number): this;
|
|
21
|
+
getMaxListeners(): number;
|
|
22
|
+
listeners(eventName: string | symbol): Listener$1[];
|
|
23
|
+
rawListeners(eventName: string | symbol): Listener$1[];
|
|
24
|
+
emit(eventName: string | symbol, ...args: unknown[]): boolean;
|
|
25
|
+
listenerCount(eventName: string | symbol): number;
|
|
26
|
+
eventNames(): Array<string | symbol>;
|
|
27
|
+
}
|
|
28
|
+
type Listener = (...args: unknown[]) => void;
|
|
29
|
+
/**
|
|
30
|
+
* MemoryStream is a minimal, zero-dependency RDF/JS Stream that replays a
|
|
31
|
+
* fixed set of quads with Node Readable-compatible semantics:
|
|
32
|
+
*
|
|
33
|
+
* - attaching a `data` listener switches to flow mode and drains the quads,
|
|
34
|
+
* - attaching a `readable` listener (or calling `read()`) enables pull mode,
|
|
35
|
+
* - attaching only `end`/`error` listeners (a bare completion signal, as
|
|
36
|
+
* `promisifyEventEmitter` does for store operations) ends the stream once
|
|
37
|
+
* nothing else is consuming it,
|
|
38
|
+
* - `end` is only emitted after every quad has been consumed.
|
|
39
|
+
*
|
|
40
|
+
* It implements the full EventEmitter surface required by the `rdfjs.Stream`
|
|
41
|
+
* interface without depending on Node's `events` module, keeping the engine
|
|
42
|
+
* runtime dependency-free and browser-friendly.
|
|
43
|
+
*/
|
|
44
|
+
declare class MemoryStream extends MiniEventEmitter implements RdfStream<RdfQuad> {
|
|
45
|
+
private readonly quads;
|
|
46
|
+
private _ended;
|
|
47
|
+
private _flowing;
|
|
48
|
+
private _readStarted;
|
|
49
|
+
constructor(quads: RdfQuad[]);
|
|
50
|
+
read(): RdfQuad | null;
|
|
51
|
+
[Symbol.iterator](): Iterator<RdfQuad>;
|
|
52
|
+
destroy(error?: Error): void;
|
|
53
|
+
on(eventName: string | symbol, listener: Listener): this;
|
|
54
|
+
private _startFlowing;
|
|
55
|
+
private _drain;
|
|
56
|
+
private _end;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* MemoryStore is a minimal, zero-dependency in-memory RDF/JS Store used to
|
|
60
|
+
* materialize FROM and FROM NAMED datasets (and transient update
|
|
61
|
+
* materializations) inside the engine. Every quad is held once in a canonical
|
|
62
|
+
* map keyed by all four positions and mirrored into four positional indexes
|
|
63
|
+
* (subject / predicate / object / graph), so match scans only the smallest
|
|
64
|
+
* constrained bucket instead of the whole store.
|
|
65
|
+
*
|
|
66
|
+
* Prior art: the mirror-into-per-position-indexes design (any pattern
|
|
67
|
+
* resolves through one bucket rather than a full scan) is the RDF-store
|
|
68
|
+
* index pattern of RDF-3X and Hexastore, and probing the smallest
|
|
69
|
+
* constrained bucket is System R-style access-path selection.
|
|
70
|
+
* @see {@link https://doi.org/10.14778/1453856.1453927 Neumann & Weikum, "RDF-3X: A RISC-Style Engine for RDF," PVLDB 1(1), 2008, pp. 647–659}
|
|
71
|
+
* @see {@link https://doi.org/10.14778/1453856.1453965 Weiss, Karras & Bernstein, "Hexastore: Sextuple Indexing for Semantic Web Data Management," PVLDB 1(1), 2008, pp. 1008–1019}
|
|
72
|
+
* @see {@link https://doi.org/10.1145/582095.582099 Selinger et al., "Access Path Selection in a Relational Database Management System," SIGMOD '79, pp. 23–34}
|
|
73
|
+
*/
|
|
74
|
+
declare class MemoryStore implements RdfStore<RdfQuad> {
|
|
75
|
+
/** kind marks this store as the built-in in-memory store (see StoreKind). */
|
|
76
|
+
readonly kind: "memory";
|
|
77
|
+
private quads;
|
|
78
|
+
private bySubject;
|
|
79
|
+
private byPredicate;
|
|
80
|
+
private byObject;
|
|
81
|
+
private byGraph;
|
|
82
|
+
private _version;
|
|
83
|
+
/**
|
|
84
|
+
* version increments on every mutation, letting caches that snapshot the
|
|
85
|
+
* store (the EXISTS synchronous index) detect staleness and rebuild only
|
|
86
|
+
* when the data actually changed.
|
|
87
|
+
*/
|
|
88
|
+
get version(): number;
|
|
89
|
+
constructor(initialQuads?: RdfQuad[]);
|
|
90
|
+
addQuad(quad: RdfQuad): this;
|
|
91
|
+
addQuad(subject: RdfTerm, predicate: RdfTerm, object: RdfTerm, graph?: RdfTerm): this;
|
|
92
|
+
removeQuad(quad: RdfQuad): this;
|
|
93
|
+
remove(stream: RdfStream<RdfQuad>): RdfStream<RdfQuad>;
|
|
94
|
+
import(stream: RdfStream<RdfQuad>): RdfStream<RdfQuad>;
|
|
95
|
+
getQuads(subject?: RdfTerm | null, predicate?: RdfTerm | null, object?: RdfTerm | null, graph?: RdfTerm | null): RdfQuad[];
|
|
96
|
+
countQuads(subject?: RdfTerm | null, predicate?: RdfTerm | null, object?: RdfTerm | null, graph?: RdfTerm | null): number;
|
|
97
|
+
removeMatches(subject?: RdfTerm | null, predicate?: RdfTerm | null, object?: RdfTerm | null, graph?: RdfTerm | null): MemoryStream;
|
|
98
|
+
deleteGraph(graph: RdfQuadGraph | string): MemoryStream;
|
|
99
|
+
match(subject?: RdfTerm | null, predicate?: RdfTerm | null, object?: RdfTerm | null, graph?: RdfTerm | null): MemoryStream;
|
|
100
|
+
get size(): number;
|
|
101
|
+
}
|
|
102
|
+
export { MemoryStream as n, MemoryStore as t };
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import "./term-s9I2y-ck.js";
|
|
2
|
+
import { d as sameRdfTerm, f as termKey, t as DataFactory } from "./data-factory-76Dxrf0g.js";
|
|
3
|
+
// src/store/event-emitter-base.ts
|
|
4
|
+
/**
|
|
5
|
+
* MiniEventEmitter implements the EventEmitter surface the `rdfjs.Stream`
|
|
6
|
+
* interface requires without depending on Node's `events` module, keeping the
|
|
7
|
+
* engine runtime dependency-free and browser-friendly.
|
|
8
|
+
*/
|
|
9
|
+
var MiniEventEmitter = class {
|
|
10
|
+
_listeners = /* @__PURE__ */ new Map();
|
|
11
|
+
_maxListeners = 10;
|
|
12
|
+
on(eventName, listener) {
|
|
13
|
+
return this.onRaw(eventName, listener);
|
|
14
|
+
}
|
|
15
|
+
onRaw(eventName, listener) {
|
|
16
|
+
const list = this._listeners.get(eventName);
|
|
17
|
+
if (list) list.push(listener);
|
|
18
|
+
else this._listeners.set(eventName, [listener]);
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
addListener(eventName, listener) {
|
|
22
|
+
return this.on(eventName, listener);
|
|
23
|
+
}
|
|
24
|
+
once(eventName, listener) {
|
|
25
|
+
const wrapper = (...args) => {
|
|
26
|
+
this.removeListener(eventName, wrapper);
|
|
27
|
+
listener(...args);
|
|
28
|
+
};
|
|
29
|
+
return this.on(eventName, wrapper);
|
|
30
|
+
}
|
|
31
|
+
prependListener(eventName, listener) {
|
|
32
|
+
const list = this._listeners.get(eventName);
|
|
33
|
+
if (list) list.unshift(listener);
|
|
34
|
+
else this._listeners.set(eventName, [listener]);
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
prependOnceListener(eventName, listener) {
|
|
38
|
+
const wrapper = (...args) => {
|
|
39
|
+
this.removeListener(eventName, wrapper);
|
|
40
|
+
listener(...args);
|
|
41
|
+
};
|
|
42
|
+
return this.prependListener(eventName, wrapper);
|
|
43
|
+
}
|
|
44
|
+
removeListener(eventName, listener) {
|
|
45
|
+
const list = this._listeners.get(eventName);
|
|
46
|
+
if (list) {
|
|
47
|
+
const index = list.indexOf(listener);
|
|
48
|
+
if (index >= 0) list.splice(index, 1);
|
|
49
|
+
}
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
off(eventName, listener) {
|
|
53
|
+
return this.removeListener(eventName, listener);
|
|
54
|
+
}
|
|
55
|
+
removeAllListeners(eventName) {
|
|
56
|
+
if (eventName === void 0) this._listeners.clear();
|
|
57
|
+
else this._listeners.delete(eventName);
|
|
58
|
+
return this;
|
|
59
|
+
}
|
|
60
|
+
setMaxListeners(n) {
|
|
61
|
+
this._maxListeners = n;
|
|
62
|
+
return this;
|
|
63
|
+
}
|
|
64
|
+
getMaxListeners() {
|
|
65
|
+
return this._maxListeners;
|
|
66
|
+
}
|
|
67
|
+
listeners(eventName) {
|
|
68
|
+
return [...this._listeners.get(eventName) ?? []];
|
|
69
|
+
}
|
|
70
|
+
rawListeners(eventName) {
|
|
71
|
+
return [...this._listeners.get(eventName) ?? []];
|
|
72
|
+
}
|
|
73
|
+
emit(eventName, ...args) {
|
|
74
|
+
const list = this._listeners.get(eventName);
|
|
75
|
+
if (!list || list.length === 0) return false;
|
|
76
|
+
for (const fn of [...list]) fn.apply(this, args);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
listenerCount(eventName) {
|
|
80
|
+
return this._listeners.get(eventName)?.length ?? 0;
|
|
81
|
+
}
|
|
82
|
+
eventNames() {
|
|
83
|
+
return [...this._listeners.keys()];
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
// src/store/quad-stream.ts
|
|
87
|
+
/**
|
|
88
|
+
* MemoryStream is a minimal, zero-dependency RDF/JS Stream that replays a
|
|
89
|
+
* fixed set of quads with Node Readable-compatible semantics:
|
|
90
|
+
*
|
|
91
|
+
* - attaching a `data` listener switches to flow mode and drains the quads,
|
|
92
|
+
* - attaching a `readable` listener (or calling `read()`) enables pull mode,
|
|
93
|
+
* - attaching only `end`/`error` listeners (a bare completion signal, as
|
|
94
|
+
* `promisifyEventEmitter` does for store operations) ends the stream once
|
|
95
|
+
* nothing else is consuming it,
|
|
96
|
+
* - `end` is only emitted after every quad has been consumed.
|
|
97
|
+
*
|
|
98
|
+
* It implements the full EventEmitter surface required by the `rdfjs.Stream`
|
|
99
|
+
* interface without depending on Node's `events` module, keeping the engine
|
|
100
|
+
* runtime dependency-free and browser-friendly.
|
|
101
|
+
*/
|
|
102
|
+
var MemoryStream = class extends MiniEventEmitter {
|
|
103
|
+
quads;
|
|
104
|
+
_ended = false;
|
|
105
|
+
_flowing = false;
|
|
106
|
+
_readStarted = false;
|
|
107
|
+
constructor(quads) {
|
|
108
|
+
super();
|
|
109
|
+
this.quads = quads;
|
|
110
|
+
}
|
|
111
|
+
read() {
|
|
112
|
+
this._readStarted = true;
|
|
113
|
+
const quad = this.quads.shift();
|
|
114
|
+
if (quad === void 0) {
|
|
115
|
+
this._end();
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
return quad;
|
|
119
|
+
}
|
|
120
|
+
[Symbol.iterator]() {
|
|
121
|
+
return this.quads[Symbol.iterator]();
|
|
122
|
+
}
|
|
123
|
+
destroy(error) {
|
|
124
|
+
if (error) this.emit("error", error);
|
|
125
|
+
this._ended = true;
|
|
126
|
+
this.removeAllListeners();
|
|
127
|
+
}
|
|
128
|
+
on(eventName, listener) {
|
|
129
|
+
this.onRaw(eventName, listener);
|
|
130
|
+
if (eventName === "data" && !this._ended && !this._flowing) this._startFlowing();
|
|
131
|
+
if (eventName === "readable" && !this._ended && this.quads.length > 0) queueMicrotask(() => {
|
|
132
|
+
if (!this._ended && this.quads.length > 0 && this.listenerCount("readable") > 0) this.emit("readable");
|
|
133
|
+
});
|
|
134
|
+
if (eventName === "end" && !this._ended) queueMicrotask(() => {
|
|
135
|
+
if (!this._ended && !this._flowing && !this._readStarted && this.listenerCount("data") === 0 && this.listenerCount("readable") === 0) this._end();
|
|
136
|
+
});
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
_startFlowing() {
|
|
140
|
+
this._flowing = true;
|
|
141
|
+
queueMicrotask(() => this._drain());
|
|
142
|
+
}
|
|
143
|
+
_drain() {
|
|
144
|
+
if (this._ended || !this._flowing) return;
|
|
145
|
+
while (this.listenerCount("data") > 0) {
|
|
146
|
+
const quad = this.quads.shift();
|
|
147
|
+
if (quad === void 0) break;
|
|
148
|
+
this.emit("data", quad);
|
|
149
|
+
}
|
|
150
|
+
if (this.quads.length === 0) this._end();
|
|
151
|
+
}
|
|
152
|
+
_end() {
|
|
153
|
+
if (this._ended) return;
|
|
154
|
+
this._ended = true;
|
|
155
|
+
this.emit("end");
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* subscribeQuadData registers a data listener on an RDF/JS quad stream. The
|
|
160
|
+
* binding lives here so the shared store never casts a stream to the local
|
|
161
|
+
* event-source surface.
|
|
162
|
+
*/
|
|
163
|
+
function subscribeQuadData(stream, listener) {
|
|
164
|
+
stream.on("data", listener);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* matchQuadStream starts a store scan and returns its RDF/JS quad stream.
|
|
168
|
+
*/
|
|
169
|
+
function matchQuadStream(store, subject, predicate, object, graph) {
|
|
170
|
+
return store.match(subject, predicate, object, graph);
|
|
171
|
+
}
|
|
172
|
+
// src/store/memory-store.ts
|
|
173
|
+
/**
|
|
174
|
+
* joinQuadKey renders the deterministic dedup key for a stored quad from its
|
|
175
|
+
* four already-computed position keys. Unlike `termKey`, which treats a quad
|
|
176
|
+
* as an RDF 1.2 triple term (identity without a graph), quad identity in a
|
|
177
|
+
* dataset includes the graph, so two quads that differ only by graph must not
|
|
178
|
+
* collide. Callers compute each position key once and reuse it for the dedup
|
|
179
|
+
* key and the positional index.
|
|
180
|
+
*/
|
|
181
|
+
function joinQuadKey(subjectKey, predicateKey, objectKey, graphKey) {
|
|
182
|
+
return subjectKey + "|" + predicateKey + "|" + objectKey + "|" + graphKey;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* indexPosition appends a quad to the bucket for the given term key.
|
|
186
|
+
*/
|
|
187
|
+
function indexPosition(index, key, quad) {
|
|
188
|
+
const bucket = index.get(key);
|
|
189
|
+
if (bucket) bucket.push(quad);
|
|
190
|
+
else index.set(key, [quad]);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* unindexPosition removes a quad from the bucket for the given term key,
|
|
194
|
+
* dropping the key once its bucket empties. `stored` is the instance the store
|
|
195
|
+
* indexed, so buckets are searched by reference: rebuilding each candidate's key
|
|
196
|
+
* would make removing many quads quadratic in far more than reference checks.
|
|
197
|
+
*/
|
|
198
|
+
function unindexPosition(index, key, stored) {
|
|
199
|
+
const bucket = index.get(key);
|
|
200
|
+
if (bucket === void 0) return;
|
|
201
|
+
const position = bucket.lastIndexOf(stored);
|
|
202
|
+
if (position >= 0) {
|
|
203
|
+
bucket.splice(position, 1);
|
|
204
|
+
if (bucket.length === 0) index.delete(key);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* MemoryStore is a minimal, zero-dependency in-memory RDF/JS Store used to
|
|
209
|
+
* materialize FROM and FROM NAMED datasets (and transient update
|
|
210
|
+
* materializations) inside the engine. Every quad is held once in a canonical
|
|
211
|
+
* map keyed by all four positions and mirrored into four positional indexes
|
|
212
|
+
* (subject / predicate / object / graph), so match scans only the smallest
|
|
213
|
+
* constrained bucket instead of the whole store.
|
|
214
|
+
*
|
|
215
|
+
* Prior art: the mirror-into-per-position-indexes design (any pattern
|
|
216
|
+
* resolves through one bucket rather than a full scan) is the RDF-store
|
|
217
|
+
* index pattern of RDF-3X and Hexastore, and probing the smallest
|
|
218
|
+
* constrained bucket is System R-style access-path selection.
|
|
219
|
+
* @see {@link https://doi.org/10.14778/1453856.1453927 Neumann & Weikum, "RDF-3X: A RISC-Style Engine for RDF," PVLDB 1(1), 2008, pp. 647–659}
|
|
220
|
+
* @see {@link https://doi.org/10.14778/1453856.1453965 Weiss, Karras & Bernstein, "Hexastore: Sextuple Indexing for Semantic Web Data Management," PVLDB 1(1), 2008, pp. 1008–1019}
|
|
221
|
+
* @see {@link https://doi.org/10.1145/582095.582099 Selinger et al., "Access Path Selection in a Relational Database Management System," SIGMOD '79, pp. 23–34}
|
|
222
|
+
*/
|
|
223
|
+
var MemoryStore = class {
|
|
224
|
+
/** kind marks this store as the built-in in-memory store (see StoreKind). */
|
|
225
|
+
kind = "memory";
|
|
226
|
+
quads = /* @__PURE__ */ new Map();
|
|
227
|
+
bySubject = /* @__PURE__ */ new Map();
|
|
228
|
+
byPredicate = /* @__PURE__ */ new Map();
|
|
229
|
+
byObject = /* @__PURE__ */ new Map();
|
|
230
|
+
byGraph = /* @__PURE__ */ new Map();
|
|
231
|
+
_version = 0;
|
|
232
|
+
/**
|
|
233
|
+
* version increments on every mutation, letting caches that snapshot the
|
|
234
|
+
* store (the EXISTS synchronous index) detect staleness and rebuild only
|
|
235
|
+
* when the data actually changed.
|
|
236
|
+
*/
|
|
237
|
+
get version() {
|
|
238
|
+
return this._version;
|
|
239
|
+
}
|
|
240
|
+
constructor(initialQuads) {
|
|
241
|
+
if (initialQuads) for (const q of initialQuads) this.addQuad(q);
|
|
242
|
+
}
|
|
243
|
+
addQuad(quadOrSubject, predicate, object, graph) {
|
|
244
|
+
const quad = predicate !== void 0 && object !== void 0 ? DataFactory.quad(quadOrSubject, predicate, object, graph) : quadOrSubject;
|
|
245
|
+
const subjectKey = termKey(quad.subject);
|
|
246
|
+
const predicateKey = termKey(quad.predicate);
|
|
247
|
+
const objectKey = termKey(quad.object);
|
|
248
|
+
const graphKey = termKey(quad.graph);
|
|
249
|
+
const key = joinQuadKey(subjectKey, predicateKey, objectKey, graphKey);
|
|
250
|
+
if (!this.quads.has(key)) {
|
|
251
|
+
this.quads.set(key, quad);
|
|
252
|
+
indexPosition(this.bySubject, subjectKey, quad);
|
|
253
|
+
indexPosition(this.byPredicate, predicateKey, quad);
|
|
254
|
+
indexPosition(this.byObject, objectKey, quad);
|
|
255
|
+
indexPosition(this.byGraph, graphKey, quad);
|
|
256
|
+
}
|
|
257
|
+
this._version++;
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
removeQuad(quad) {
|
|
261
|
+
const subjectKey = termKey(quad.subject);
|
|
262
|
+
const predicateKey = termKey(quad.predicate);
|
|
263
|
+
const objectKey = termKey(quad.object);
|
|
264
|
+
const graphKey = termKey(quad.graph);
|
|
265
|
+
const key = joinQuadKey(subjectKey, predicateKey, objectKey, graphKey);
|
|
266
|
+
const stored = this.quads.get(key);
|
|
267
|
+
if (stored !== void 0) {
|
|
268
|
+
this.quads.delete(key);
|
|
269
|
+
unindexPosition(this.bySubject, subjectKey, stored);
|
|
270
|
+
unindexPosition(this.byPredicate, predicateKey, stored);
|
|
271
|
+
unindexPosition(this.byObject, objectKey, stored);
|
|
272
|
+
unindexPosition(this.byGraph, graphKey, stored);
|
|
273
|
+
}
|
|
274
|
+
this._version++;
|
|
275
|
+
return this;
|
|
276
|
+
}
|
|
277
|
+
remove(stream) {
|
|
278
|
+
subscribeQuadData(stream, (quad) => this.removeQuad(quad));
|
|
279
|
+
return stream;
|
|
280
|
+
}
|
|
281
|
+
import(stream) {
|
|
282
|
+
subscribeQuadData(stream, (quad) => this.addQuad(quad));
|
|
283
|
+
return stream;
|
|
284
|
+
}
|
|
285
|
+
getQuads(subject, predicate, object, graph) {
|
|
286
|
+
if (subject != null && !this.bySubject.has(termKey(subject)) || predicate != null && !this.byPredicate.has(termKey(predicate)) || object != null && !this.byObject.has(termKey(object)) || graph != null && !this.byGraph.has(termKey(graph))) return [];
|
|
287
|
+
const constrained = [];
|
|
288
|
+
if (subject != null) constrained.push(this.bySubject.get(termKey(subject)));
|
|
289
|
+
if (predicate != null) constrained.push(this.byPredicate.get(termKey(predicate)));
|
|
290
|
+
if (object != null) constrained.push(this.byObject.get(termKey(object)));
|
|
291
|
+
if (graph != null) constrained.push(this.byGraph.get(termKey(graph)));
|
|
292
|
+
if (constrained.length === 0) return [...this.quads.values()];
|
|
293
|
+
let bucket = constrained[0];
|
|
294
|
+
for (const candidate of constrained) if (candidate.length < bucket.length) bucket = candidate;
|
|
295
|
+
return bucket.filter((quad) => (subject == null || sameRdfTerm(quad.subject, subject)) && (predicate == null || sameRdfTerm(quad.predicate, predicate)) && (object == null || sameRdfTerm(quad.object, object)) && (graph == null || sameRdfTerm(quad.graph, graph)));
|
|
296
|
+
}
|
|
297
|
+
countQuads(subject, predicate, object, graph) {
|
|
298
|
+
return this.getQuads(subject, predicate, object, graph).length;
|
|
299
|
+
}
|
|
300
|
+
removeMatches(subject, predicate, object, graph) {
|
|
301
|
+
const matches = this.getQuads(subject, predicate, object, graph);
|
|
302
|
+
for (const q of matches) this.removeQuad(q);
|
|
303
|
+
return new MemoryStream(matches);
|
|
304
|
+
}
|
|
305
|
+
deleteGraph(graph) {
|
|
306
|
+
const graphTerm = typeof graph === "string" ? DataFactory.namedNode(graph) : graph;
|
|
307
|
+
return this.removeMatches(null, null, null, graphTerm);
|
|
308
|
+
}
|
|
309
|
+
match(subject, predicate, object, graph) {
|
|
310
|
+
return new MemoryStream(this.getQuads(subject, predicate, object, graph));
|
|
311
|
+
}
|
|
312
|
+
get size() {
|
|
313
|
+
return this.quads.size;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
export { MemoryStream as n, matchQuadStream as r, MemoryStore as t };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { o as SparqlResponse } from "./sparql-engine-interface-Bies9KjE.js";
|
|
2
|
+
import { i as RdfQuad } from "./rdf-terms-DGpxNnyn.js";
|
|
3
|
+
/**
|
|
4
|
+
* serializeJsonResults serializes a SELECT or ASK SparqlResponse into the
|
|
5
|
+
* SPARQL 1.1 Query Results JSON format ("application/sparql-results+json",
|
|
6
|
+
* the .srj serialization), extended with the SPARQL 1.2 direction and
|
|
7
|
+
* triple-term members the engine's wire format already carries.
|
|
8
|
+
*
|
|
9
|
+
* The response is already in the wire shape (src/sparql-engine-interface.ts):
|
|
10
|
+
* every SparqlValue is emitted as-is — uri/bnode as {type, value}; literals
|
|
11
|
+
* with their "xml:lang", "its:dir" (RDF 1.2 base direction), and datatype
|
|
12
|
+
* members; RDF 1.2 triple terms as the nested {type: "triple", value:
|
|
13
|
+
* {subject, predicate, object}} object — so this is a document builder, not a
|
|
14
|
+
* term re-encoder.
|
|
15
|
+
*
|
|
16
|
+
* Coverage: SELECT (head.vars + results.bindings) and ASK (boolean). The
|
|
17
|
+
* format does not cover CONSTRUCT/DESCRIBE (RDF graph results, serialized as
|
|
18
|
+
* RDF) or updates (void responses carry no result body) — those kinds throw
|
|
19
|
+
* a clear error.
|
|
20
|
+
*
|
|
21
|
+
* Output is deterministic: members are emitted in a fixed order (head, then
|
|
22
|
+
* results/boolean; for literals type, value, xml:lang, its:dir, datatype), so
|
|
23
|
+
* equal responses produce byte-identical documents.
|
|
24
|
+
*/
|
|
25
|
+
declare function serializeJsonResults(response: SparqlResponse): string;
|
|
26
|
+
/**
|
|
27
|
+
* serializeXmlResults serializes a SELECT or ASK SparqlResponse into the
|
|
28
|
+
* SPARQL 1.1 Query Results XML format ("application/sparql-results+xml", the
|
|
29
|
+
* .srx serialization), extended with the SPARQL 1.2 surface the engine's wire
|
|
30
|
+
* format carries: RDF 1.2 triple terms as nested <subject>/<predicate>/<object>
|
|
31
|
+
* elements, and the base direction of a directional language-tagged literal
|
|
32
|
+
* as the its:dir attribute (ITS 2.0 namespace, mirroring how the SPARQL 1.2
|
|
33
|
+
* results JSON format carries it) — matching the shape of the vendored
|
|
34
|
+
* sparql12 .srx fixtures.
|
|
35
|
+
*
|
|
36
|
+
* Coverage: SELECT (head variables + one <result> per binding) and ASK
|
|
37
|
+
* (<boolean>). The format does not cover CONSTRUCT/DESCRIBE (RDF graph
|
|
38
|
+
* results, serialized as RDF) or updates (void responses carry no result
|
|
39
|
+
* body) — those kinds throw a clear error.
|
|
40
|
+
*
|
|
41
|
+
* Output is deterministic: elements are emitted in a fixed order with
|
|
42
|
+
* two-space indentation, and every text/attribute value is XML-escaped
|
|
43
|
+
* (&, <, >, ", '), so equal responses produce byte-identical documents.
|
|
44
|
+
*/
|
|
45
|
+
declare function serializeXmlResults(response: SparqlResponse): string;
|
|
46
|
+
/**
|
|
47
|
+
* TurtleWriterFormat selects the serialization dialect:
|
|
48
|
+
*
|
|
49
|
+
* - `"turtle"` — Turtle, upgraded to TriG graph blocks when any quad carries
|
|
50
|
+
* a named graph (the parser's grammar is TriG-based, so this round-trips
|
|
51
|
+
* losslessly). A prefix map compacts IRIs that share a registered namespace.
|
|
52
|
+
* - `"n-quads"` — one quad per line, graph label included.
|
|
53
|
+
* - `"n-triples"` — one triple per line; named graphs are rejected because
|
|
54
|
+
* RDF 1.1 N-Triples cannot express them (lossless over lenient).
|
|
55
|
+
*/
|
|
56
|
+
type TurtleFormat = "turtle" | "n-quads" | "n-triples";
|
|
57
|
+
/** TurtleWriterOptions configures serializeTurtle. */
|
|
58
|
+
interface TurtleWriterOptions {
|
|
59
|
+
/** Serialization dialect; defaults to `"turtle"`. */
|
|
60
|
+
format?: TurtleFormat;
|
|
61
|
+
/** Namespace prefixes: `{ prefix: namespace }`, emitted as `@prefix` lines. */
|
|
62
|
+
prefixes?: Record<string, string>;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* serializeTurtle serializes RDF/JS quads into a Turtle-family document.
|
|
66
|
+
*
|
|
67
|
+
* It is the writer counterpart to `parseTurtleQuads`: the two round-trip
|
|
68
|
+
* losslessly over Turtle, TriG, N-Quads, and N-Triples, including RDF 1.2
|
|
69
|
+
* triple terms (`<<( s p o )>>`), directional language literals
|
|
70
|
+
* (`"v"@en--ltr`), and named graphs. Output preserves input order and groups
|
|
71
|
+
* statements by subject and predicate (`,` and `;` shorthand) in Turtle mode.
|
|
72
|
+
*
|
|
73
|
+
* @param quads quads to serialize
|
|
74
|
+
* @param options format and prefix configuration
|
|
75
|
+
* @returns the serialized document (trailing newline, empty string for no quads)
|
|
76
|
+
*/
|
|
77
|
+
declare function serializeTurtle(quads: RdfQuad[], options?: TurtleWriterOptions): string;
|
|
78
|
+
export { serializeJsonResults as a, serializeXmlResults as i, TurtleWriterOptions as n, serializeTurtle as r, TurtleFormat as t };
|