@ldo/subscribable-dataset 0.0.1-1.0.0-alpha.1.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.txt +21 -0
- package/Readme.md +298 -0
- package/dist/SubscribableDataset.d.ts +37 -0
- package/dist/SubscribableDataset.js +180 -0
- package/dist/SubscribableDataset.js.map +1 -0
- package/dist/SubscribableDatasetFactory.d.ts +9 -0
- package/dist/SubscribableDatasetFactory.js +15 -0
- package/dist/SubscribableDatasetFactory.js.map +1 -0
- package/dist/TransactionDataset.d.ts +25 -0
- package/dist/TransactionDataset.js +124 -0
- package/dist/TransactionDataset.js.map +1 -0
- package/dist/TransactionDatasetFactory.d.ts +9 -0
- package/dist/TransactionDatasetFactory.js +15 -0
- package/dist/TransactionDatasetFactory.js.map +1 -0
- package/dist/createSubscribableDataset.d.ts +5 -0
- package/dist/createSubscribableDataset.js +25 -0
- package/dist/createSubscribableDataset.js.map +1 -0
- package/dist/createSubscribableDatasetFromSerializedInput.d.ts +4 -0
- package/dist/createSubscribableDatasetFromSerializedInput.js +11 -0
- package/dist/createSubscribableDatasetFromSerializedInput.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +28 -0
- package/dist/index.js.map +1 -0
- package/dist/mergeDatasetChanges.d.ts +3 -0
- package/dist/mergeDatasetChanges.js +39 -0
- package/dist/mergeDatasetChanges.js.map +1 -0
- package/dist/types.d.ts +35 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/util.d.ts +3 -0
- package/dist/util.js +20 -0
- package/dist/util.js.map +1 -0
- package/package.json +47 -0
- package/src/SubscribableDataset.ts +424 -0
- package/src/SubscribableDatasetFactory.ts +31 -0
- package/src/TransactionDataset.ts +284 -0
- package/src/TransactionDatasetFactory.ts +27 -0
- package/src/createSubscribableDataset.ts +46 -0
- package/src/createSubscribableDatasetFromSerializedInput.ts +28 -0
- package/src/index.ts +9 -0
- package/src/mergeDatasetChanges.ts +53 -0
- package/src/types.ts +167 -0
- package/src/util.ts +27 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import type { Dataset, BaseQuad, Term, DatasetFactory } from "@rdfjs/types";
|
|
2
|
+
import type { DatasetChanges } from "@ldo/rdf-utils";
|
|
3
|
+
import type { ITransactionDataset, ITransactionDatasetFactory } from "./types";
|
|
4
|
+
import { mergeDatasetChanges } from "./mergeDatasetChanges";
|
|
5
|
+
import { SubscribableDataset } from "./SubscribableDataset";
|
|
6
|
+
import { updateDatasetInBulk } from "./util";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Proxy Transactional Dataset is a transactional dataset that does not duplicate
|
|
10
|
+
* the parent dataset, it will dynamically determine the correct return value for
|
|
11
|
+
* methods in real time when the method is called.
|
|
12
|
+
*/
|
|
13
|
+
export class TransactionDataset<InAndOutQuad extends BaseQuad = BaseQuad>
|
|
14
|
+
extends SubscribableDataset<InAndOutQuad>
|
|
15
|
+
implements ITransactionDataset<InAndOutQuad>
|
|
16
|
+
{
|
|
17
|
+
/**
|
|
18
|
+
* The parent dataset that will be updated upon commit
|
|
19
|
+
*/
|
|
20
|
+
public readonly parentDataset: Dataset<InAndOutQuad, InAndOutQuad>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The changes made that are ready to commit
|
|
24
|
+
*/
|
|
25
|
+
private datasetChanges: DatasetChanges<InAndOutQuad>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A list of changes made to the parent dataset upon commit used for rolling back changes.
|
|
29
|
+
* This is different from 'datasetChanges' because datasetChanges is allowed to overlap
|
|
30
|
+
* with the parent dataset.
|
|
31
|
+
* For example, the parent dataset may already have triple A, and datasetChanges can
|
|
32
|
+
* also have triple A.
|
|
33
|
+
*/
|
|
34
|
+
private committedDatasetChanges?: DatasetChanges<InAndOutQuad>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Constructor
|
|
38
|
+
* @param parentDataset The dataset that will be updated upon commit
|
|
39
|
+
*/
|
|
40
|
+
constructor(
|
|
41
|
+
parentDataset: Dataset<InAndOutQuad, InAndOutQuad>,
|
|
42
|
+
datasetFactory: DatasetFactory<InAndOutQuad, InAndOutQuad>,
|
|
43
|
+
transactionDatasetFactory: ITransactionDatasetFactory<InAndOutQuad>,
|
|
44
|
+
) {
|
|
45
|
+
super(datasetFactory, transactionDatasetFactory, datasetFactory.dataset());
|
|
46
|
+
this.parentDataset = parentDataset;
|
|
47
|
+
this.datasetChanges = {};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* ==================================================================
|
|
52
|
+
* DATASET METHODS
|
|
53
|
+
* ==================================================================
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Imports the quads into this dataset.
|
|
58
|
+
* This method differs from Dataset.union in that it adds all quads to the current instance, rather than combining quads and the current instance to create a new instance.
|
|
59
|
+
* @param quads
|
|
60
|
+
* @returns the dataset instance it was called on.
|
|
61
|
+
*/
|
|
62
|
+
public addAll(
|
|
63
|
+
quads: Dataset<InAndOutQuad, InAndOutQuad> | InAndOutQuad[],
|
|
64
|
+
): this {
|
|
65
|
+
this.updateDatasetChanges({ added: quads });
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Bulk add and remove triples
|
|
71
|
+
* @param changed
|
|
72
|
+
*/
|
|
73
|
+
public bulk(changes: DatasetChanges<InAndOutQuad>): this {
|
|
74
|
+
this.updateDatasetChanges(changes);
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* This method removes the quads in the current instance that match the given arguments. The logic described in Quad Matching is applied for each quad in this dataset to select the quads which will be deleted.
|
|
80
|
+
* @param subject
|
|
81
|
+
* @param predicate
|
|
82
|
+
* @param object
|
|
83
|
+
* @param graph
|
|
84
|
+
* @returns the dataset instance it was called on.
|
|
85
|
+
*/
|
|
86
|
+
deleteMatches(
|
|
87
|
+
subject?: Term,
|
|
88
|
+
predicate?: Term,
|
|
89
|
+
object?: Term,
|
|
90
|
+
graph?: Term,
|
|
91
|
+
): this {
|
|
92
|
+
this.checkIfTransactionCommitted();
|
|
93
|
+
const matching = this.match(subject, predicate, object, graph);
|
|
94
|
+
for (const quad of matching) {
|
|
95
|
+
this.delete(quad);
|
|
96
|
+
}
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* This method 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.
|
|
102
|
+
* @param subject
|
|
103
|
+
* @param predicate
|
|
104
|
+
* @param object
|
|
105
|
+
* @param graph
|
|
106
|
+
* @returns a Dataset with matching triples
|
|
107
|
+
*/
|
|
108
|
+
public match(
|
|
109
|
+
subject?: Term | null,
|
|
110
|
+
predicate?: Term | null,
|
|
111
|
+
object?: Term | null,
|
|
112
|
+
graph?: Term | null,
|
|
113
|
+
): Dataset<InAndOutQuad, InAndOutQuad> {
|
|
114
|
+
let finalMatch = this.parentDataset.match(
|
|
115
|
+
subject,
|
|
116
|
+
predicate,
|
|
117
|
+
object,
|
|
118
|
+
graph,
|
|
119
|
+
);
|
|
120
|
+
if (this.datasetChanges.removed) {
|
|
121
|
+
finalMatch = finalMatch.difference(this.datasetChanges.removed);
|
|
122
|
+
}
|
|
123
|
+
if (this.datasetChanges.added) {
|
|
124
|
+
finalMatch = finalMatch.union(
|
|
125
|
+
this.datasetChanges.added.match(subject, predicate, object, graph),
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
return finalMatch;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* A non-negative integer that specifies the number of quads in the set.
|
|
133
|
+
*/
|
|
134
|
+
public get size(): number {
|
|
135
|
+
return (
|
|
136
|
+
this.parentDataset.size +
|
|
137
|
+
(this.datasetChanges.added?.difference(this.parentDataset).size || 0) -
|
|
138
|
+
(this.datasetChanges.removed?.intersection(this.parentDataset).size || 0)
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Adds the specified quad to the dataset.
|
|
144
|
+
* Existing quads, as defined in Quad.equals, will be ignored.
|
|
145
|
+
* @param quad
|
|
146
|
+
* @returns the dataset instance it was called on.
|
|
147
|
+
*/
|
|
148
|
+
public add(quad: InAndOutQuad): this {
|
|
149
|
+
this.updateDatasetChanges({ added: [quad] });
|
|
150
|
+
return this;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Removes the specified quad from the dataset.
|
|
155
|
+
* This method returns the dataset instance it was called on.
|
|
156
|
+
* @param quad
|
|
157
|
+
*/
|
|
158
|
+
public delete(quad: InAndOutQuad): this {
|
|
159
|
+
this.updateDatasetChanges({ removed: [quad] });
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Determines whether a dataset includes a certain quad, returning true or false as appropriate.
|
|
165
|
+
* @param quad
|
|
166
|
+
*/
|
|
167
|
+
public has(quad: InAndOutQuad): boolean {
|
|
168
|
+
return (
|
|
169
|
+
!this.datasetChanges.removed?.has(quad) &&
|
|
170
|
+
(this.datasetChanges.added?.has(quad) || this.parentDataset.has(quad))
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Returns an iterator
|
|
176
|
+
*/
|
|
177
|
+
public [Symbol.iterator](): Iterator<InAndOutQuad> {
|
|
178
|
+
const addedIterator = (this.datasetChanges.added || [])[Symbol.iterator]();
|
|
179
|
+
let addedNext = addedIterator.next();
|
|
180
|
+
const parentIterator = this.parentDataset[Symbol.iterator]();
|
|
181
|
+
let parentNext = parentIterator.next();
|
|
182
|
+
return {
|
|
183
|
+
next: () => {
|
|
184
|
+
if (!addedNext || !addedNext.done) {
|
|
185
|
+
const toReturn = addedNext;
|
|
186
|
+
addedNext = addedIterator.next();
|
|
187
|
+
return toReturn;
|
|
188
|
+
}
|
|
189
|
+
while (!parentNext.done) {
|
|
190
|
+
const toReturn = parentNext;
|
|
191
|
+
parentNext = parentIterator.next();
|
|
192
|
+
if (
|
|
193
|
+
!(
|
|
194
|
+
this.datasetChanges.added &&
|
|
195
|
+
this.datasetChanges.added.has(toReturn.value)
|
|
196
|
+
) &&
|
|
197
|
+
!(
|
|
198
|
+
this.datasetChanges.removed &&
|
|
199
|
+
this.datasetChanges.removed.has(toReturn.value)
|
|
200
|
+
)
|
|
201
|
+
) {
|
|
202
|
+
return toReturn;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return { value: undefined, done: true };
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* ==================================================================
|
|
212
|
+
* TANSACTIONAL METHODS
|
|
213
|
+
* ==================================================================
|
|
214
|
+
*/
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Checks if the transaction has been committed and throws an error if it has
|
|
218
|
+
* @param changed
|
|
219
|
+
*/
|
|
220
|
+
private checkIfTransactionCommitted() {
|
|
221
|
+
if (this.committedDatasetChanges) {
|
|
222
|
+
throw new Error("Transaction has already committed");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Helper method to update the changes made
|
|
228
|
+
* @param changes
|
|
229
|
+
*/
|
|
230
|
+
private updateDatasetChanges(changes: {
|
|
231
|
+
added?: Dataset<InAndOutQuad> | InAndOutQuad[];
|
|
232
|
+
removed?: Dataset<InAndOutQuad> | InAndOutQuad[];
|
|
233
|
+
}): void {
|
|
234
|
+
this.checkIfTransactionCommitted();
|
|
235
|
+
|
|
236
|
+
mergeDatasetChanges(this.datasetChanges, {
|
|
237
|
+
added: changes.added
|
|
238
|
+
? this.datasetFactory.dataset(changes.added)
|
|
239
|
+
: undefined,
|
|
240
|
+
removed: changes.removed
|
|
241
|
+
? this.datasetFactory.dataset(changes.removed)
|
|
242
|
+
: undefined,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Helper method to update the parent dataset or any other provided dataset
|
|
248
|
+
*/
|
|
249
|
+
private updateParentDataset(datasetChanges: DatasetChanges<InAndOutQuad>) {
|
|
250
|
+
return updateDatasetInBulk(this.parentDataset, datasetChanges);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Commits changes made to the parent dataset
|
|
255
|
+
*/
|
|
256
|
+
public commit(): void {
|
|
257
|
+
this.checkIfTransactionCommitted();
|
|
258
|
+
this.committedDatasetChanges = {
|
|
259
|
+
added: this.datasetChanges.added?.difference(this.parentDataset),
|
|
260
|
+
removed: this.datasetChanges.removed?.intersection(this.parentDataset),
|
|
261
|
+
};
|
|
262
|
+
this.updateParentDataset(this.committedDatasetChanges);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Rolls back changes made to the parent dataset
|
|
267
|
+
*/
|
|
268
|
+
public rollback(): void {
|
|
269
|
+
if (!this.committedDatasetChanges) {
|
|
270
|
+
throw new Error(
|
|
271
|
+
"Cannot rollback. Transaction has not yet been committed",
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
this.updateParentDataset({
|
|
275
|
+
added: this.committedDatasetChanges.removed,
|
|
276
|
+
removed: this.committedDatasetChanges.added,
|
|
277
|
+
});
|
|
278
|
+
this.committedDatasetChanges = undefined;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
public getChanges(): DatasetChanges<InAndOutQuad> {
|
|
282
|
+
return this.datasetChanges;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { BaseQuad, DatasetFactory } from "@rdfjs/types";
|
|
2
|
+
import type { ISubscribableDataset, ITransactionDatasetFactory } from "./types";
|
|
3
|
+
import { TransactionDataset } from "./TransactionDataset";
|
|
4
|
+
|
|
5
|
+
export class TransactionDatasetFactory<InAndOutQuad extends BaseQuad>
|
|
6
|
+
implements ITransactionDatasetFactory<InAndOutQuad>
|
|
7
|
+
{
|
|
8
|
+
private datasetFactory: DatasetFactory<InAndOutQuad, InAndOutQuad>;
|
|
9
|
+
private transactionDatasetFactory: ITransactionDatasetFactory<InAndOutQuad>;
|
|
10
|
+
constructor(
|
|
11
|
+
datasetFactory: DatasetFactory<InAndOutQuad, InAndOutQuad>,
|
|
12
|
+
transactionDatasetFactory?: ITransactionDatasetFactory<InAndOutQuad>,
|
|
13
|
+
) {
|
|
14
|
+
this.datasetFactory = datasetFactory;
|
|
15
|
+
this.transactionDatasetFactory = transactionDatasetFactory || this;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
transactionDataset(
|
|
19
|
+
parentDataset: ISubscribableDataset<InAndOutQuad>,
|
|
20
|
+
): TransactionDataset<InAndOutQuad> {
|
|
21
|
+
return new TransactionDataset<InAndOutQuad>(
|
|
22
|
+
parentDataset,
|
|
23
|
+
this.datasetFactory,
|
|
24
|
+
this.transactionDatasetFactory,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Dataset, DatasetFactory, Quad } from "@rdfjs/types";
|
|
2
|
+
import { createDataset } from "@ldo/dataset";
|
|
3
|
+
import { SubscribableDatasetFactory } from "./SubscribableDatasetFactory";
|
|
4
|
+
import type {
|
|
5
|
+
ISubscribableDataset,
|
|
6
|
+
ISubscribableDatasetFactory,
|
|
7
|
+
ITransactionDatasetFactory,
|
|
8
|
+
} from "./types";
|
|
9
|
+
import { TransactionDatasetFactory } from "./TransactionDatasetFactory";
|
|
10
|
+
|
|
11
|
+
const datasetFactory: DatasetFactory<Quad> = {
|
|
12
|
+
dataset: (quads?: Dataset<Quad> | Quad[]): Dataset<Quad> => {
|
|
13
|
+
return createDataset(quads);
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates a factory that generates TransactionDatasets
|
|
19
|
+
* @returns TransactionDatasetFactory
|
|
20
|
+
*/
|
|
21
|
+
export function createTransactionDatasetFactory(): ITransactionDatasetFactory<Quad> {
|
|
22
|
+
return new TransactionDatasetFactory(datasetFactory);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Creates a dataset factory that generates a SubscribableDataset
|
|
27
|
+
* @returns DatasetFactory for SubscribableDataset
|
|
28
|
+
*/
|
|
29
|
+
export function createSubscribableDatasetFactory(): ISubscribableDatasetFactory<Quad> {
|
|
30
|
+
return new SubscribableDatasetFactory(
|
|
31
|
+
datasetFactory,
|
|
32
|
+
createTransactionDatasetFactory(),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Creates a SubscribableDataset
|
|
38
|
+
* @param quads: A dataset or array of Quads to initialize the dataset.
|
|
39
|
+
* @returns Dataset
|
|
40
|
+
*/
|
|
41
|
+
export function createSubscribableDataset(
|
|
42
|
+
quads?: Dataset<Quad> | Quad[],
|
|
43
|
+
): ISubscribableDataset<Quad> {
|
|
44
|
+
const subscribableDatasetFactory = createSubscribableDatasetFactory();
|
|
45
|
+
return subscribableDatasetFactory.dataset(quads);
|
|
46
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Quad } from "@rdfjs/types";
|
|
2
|
+
import type { ParserOptions } from "@ldo/rdf-utils";
|
|
3
|
+
import { createDatasetFromSerializedInput } from "@ldo/dataset";
|
|
4
|
+
import { createSubscribableDatasetFactory } from "./createSubscribableDataset";
|
|
5
|
+
import type { ISubscribableDataset } from "./types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Creates a SubscribableDataset with a string input that could be JSON-LD, Turtle, N-Triples, TriG, RDF*, or N3.
|
|
9
|
+
* @param data A string representation of RDF Data in JSON-LD, Turtle, N-Triples, TriG, RDF*, or N3.
|
|
10
|
+
* @param options Parser options: {
|
|
11
|
+
* format?: string;
|
|
12
|
+
* factory?: RDF.DataFactory;
|
|
13
|
+
* baseIRI?: string;
|
|
14
|
+
* blankNodePrefix?: string;
|
|
15
|
+
* }
|
|
16
|
+
* @returns A dataset
|
|
17
|
+
*/
|
|
18
|
+
export async function createWrapperSubscribableDatasetFromSerializedInput(
|
|
19
|
+
data: string,
|
|
20
|
+
options?: ParserOptions,
|
|
21
|
+
): Promise<ISubscribableDataset<Quad>> {
|
|
22
|
+
const datasetFactory = createSubscribableDatasetFactory();
|
|
23
|
+
return createDatasetFromSerializedInput<ISubscribableDataset<Quad>>(
|
|
24
|
+
datasetFactory,
|
|
25
|
+
data,
|
|
26
|
+
options,
|
|
27
|
+
);
|
|
28
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./createSubscribableDataset";
|
|
2
|
+
export { createWrapperSubscribableDatasetFromSerializedInput as serializedToSubscribableDataset } from "./createSubscribableDatasetFromSerializedInput";
|
|
3
|
+
export * from "./TransactionDataset";
|
|
4
|
+
export * from "./TransactionDatasetFactory";
|
|
5
|
+
export * from "./SubscribableDataset";
|
|
6
|
+
export * from "./SubscribableDatasetFactory";
|
|
7
|
+
export * from "./types";
|
|
8
|
+
export * from "./mergeDatasetChanges";
|
|
9
|
+
export * from "./util";
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { DatasetChanges } from "@ldo/rdf-utils";
|
|
2
|
+
import type { BaseQuad } from "@rdfjs/types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Merges a new change into an original change
|
|
6
|
+
* @param originalChange
|
|
7
|
+
* @param newChange
|
|
8
|
+
*/
|
|
9
|
+
export function mergeDatasetChanges<InAndOutQuad extends BaseQuad>(
|
|
10
|
+
originalChange: DatasetChanges<InAndOutQuad>,
|
|
11
|
+
newChange: DatasetChanges<InAndOutQuad>,
|
|
12
|
+
): void {
|
|
13
|
+
// Add added
|
|
14
|
+
if (newChange.added) {
|
|
15
|
+
if (originalChange.added) {
|
|
16
|
+
originalChange.added.addAll(newChange.added);
|
|
17
|
+
} else {
|
|
18
|
+
originalChange.added = newChange.added;
|
|
19
|
+
}
|
|
20
|
+
// Delete from removed if present
|
|
21
|
+
const changesIntersection = originalChange.removed?.intersection(
|
|
22
|
+
newChange.added,
|
|
23
|
+
);
|
|
24
|
+
if (changesIntersection && changesIntersection.size > 0) {
|
|
25
|
+
originalChange.removed =
|
|
26
|
+
originalChange.removed?.difference(changesIntersection);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
// Add removed
|
|
30
|
+
if (newChange.removed) {
|
|
31
|
+
if (originalChange.removed) {
|
|
32
|
+
originalChange.removed.addAll(newChange.removed);
|
|
33
|
+
} else {
|
|
34
|
+
originalChange.removed = newChange.removed;
|
|
35
|
+
}
|
|
36
|
+
// Delete from added if present
|
|
37
|
+
const changesIntersection = originalChange.added?.intersection(
|
|
38
|
+
newChange.removed,
|
|
39
|
+
);
|
|
40
|
+
if (changesIntersection && changesIntersection.size > 0) {
|
|
41
|
+
originalChange.added =
|
|
42
|
+
originalChange.added?.difference(changesIntersection);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Make undefined if size is zero
|
|
47
|
+
if (originalChange.added && originalChange.added.size === 0) {
|
|
48
|
+
originalChange.added = undefined;
|
|
49
|
+
}
|
|
50
|
+
if (originalChange.removed && originalChange.removed.size === 0) {
|
|
51
|
+
originalChange.removed = undefined;
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { DatasetChanges, QuadMatch } from "@ldo/rdf-utils";
|
|
2
|
+
import type { Dataset, BaseQuad, DatasetFactory } from "@rdfjs/types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* An event listeners for nodes
|
|
6
|
+
*/
|
|
7
|
+
export type nodeEventListener<InAndOutQuad extends BaseQuad = BaseQuad> = (
|
|
8
|
+
changes: DatasetChanges<InAndOutQuad>,
|
|
9
|
+
) => void;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Adds the bulk method for add and remove
|
|
13
|
+
*/
|
|
14
|
+
export interface IBulkEditableDataset<InAndOutQuad extends BaseQuad = BaseQuad>
|
|
15
|
+
extends Dataset<InAndOutQuad, InAndOutQuad> {
|
|
16
|
+
bulk(changes: DatasetChanges<InAndOutQuad>): this;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Factory for creating SubscribableDatasets
|
|
21
|
+
*/
|
|
22
|
+
export type ISubscribableDatasetFactory<
|
|
23
|
+
InAndOutQuad extends BaseQuad = BaseQuad,
|
|
24
|
+
> = DatasetFactory<
|
|
25
|
+
InAndOutQuad,
|
|
26
|
+
InAndOutQuad,
|
|
27
|
+
ISubscribableDataset<InAndOutQuad>
|
|
28
|
+
>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Dataset that allows developers to subscribe to a sepecific term and be alerted
|
|
32
|
+
* if a quad is added or removed containing that term. It's methods follow the
|
|
33
|
+
* EventEmitter interface except take in namedNodes as keys.
|
|
34
|
+
*/
|
|
35
|
+
export interface ISubscribableDataset<InAndOutQuad extends BaseQuad = BaseQuad>
|
|
36
|
+
extends IBulkEditableDataset<InAndOutQuad> {
|
|
37
|
+
/**
|
|
38
|
+
* Alias for emitter.on(eventName, listener).
|
|
39
|
+
* @param eventName
|
|
40
|
+
* @param listener
|
|
41
|
+
* @returns
|
|
42
|
+
*/
|
|
43
|
+
addListener(
|
|
44
|
+
eventName: QuadMatch,
|
|
45
|
+
listener: nodeEventListener<InAndOutQuad>,
|
|
46
|
+
): this;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.
|
|
50
|
+
* @param eventName
|
|
51
|
+
* @param changes
|
|
52
|
+
* @returns true if the event had listeners, false otherwise.
|
|
53
|
+
*/
|
|
54
|
+
emit(eventName: QuadMatch, changes: DatasetChanges<InAndOutQuad>): boolean;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Returns an array listing the events for which the emitter has registered listeners. The values in the array are strings or Symbols.
|
|
58
|
+
*/
|
|
59
|
+
eventNames(): QuadMatch[];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to events.defaultMaxListeners.
|
|
63
|
+
*/
|
|
64
|
+
getMaxListeners(): number;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Returns the number of listeners listening to the event named eventName.
|
|
68
|
+
*/
|
|
69
|
+
listenerCount(eventName: QuadMatch): number;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns a copy of the array of listeners for the event named eventName.
|
|
73
|
+
*/
|
|
74
|
+
listeners(eventName: QuadMatch): nodeEventListener<InAndOutQuad>[];
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Alias for emitter.removeListener()
|
|
78
|
+
*/
|
|
79
|
+
off(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): void;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
|
|
83
|
+
*/
|
|
84
|
+
on(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): this;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.
|
|
88
|
+
*/
|
|
89
|
+
once(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): this;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
|
|
93
|
+
*/
|
|
94
|
+
prependListener(
|
|
95
|
+
eventName: QuadMatch,
|
|
96
|
+
listener: nodeEventListener<InAndOutQuad>,
|
|
97
|
+
): this;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.
|
|
101
|
+
*/
|
|
102
|
+
prependOnceListener(
|
|
103
|
+
eventName: QuadMatch,
|
|
104
|
+
listener: nodeEventListener<InAndOutQuad>,
|
|
105
|
+
): this;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Removes all listeners, or those of the specified eventName.
|
|
109
|
+
*/
|
|
110
|
+
removeAllListeners(eventName: QuadMatch): this;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Removes the specified listener from the listener array for the event named eventName.
|
|
114
|
+
*/
|
|
115
|
+
removeListener(
|
|
116
|
+
eventName: QuadMatch,
|
|
117
|
+
listener: nodeEventListener<InAndOutQuad>,
|
|
118
|
+
): this;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Removes the specified listener from all events
|
|
122
|
+
*/
|
|
123
|
+
removeListenerFromAllEvents(listener: nodeEventListener<InAndOutQuad>): this;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.
|
|
127
|
+
*/
|
|
128
|
+
setMaxListeners(n: number): this;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).
|
|
132
|
+
*/
|
|
133
|
+
rawListeners(eventName: QuadMatch): nodeEventListener<InAndOutQuad>[];
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* ==================================================================
|
|
137
|
+
* TRANSACTION METHODS
|
|
138
|
+
* ==================================================================
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Returns a transactional dataset that will update this dataset when its transaction is committed.
|
|
143
|
+
*/
|
|
144
|
+
startTransaction(): ITransactionDataset<InAndOutQuad>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Creates a TransactionDataset
|
|
149
|
+
*/
|
|
150
|
+
export interface ITransactionDatasetFactory<
|
|
151
|
+
InAndOutQuad extends BaseQuad = BaseQuad,
|
|
152
|
+
> {
|
|
153
|
+
transactionDataset(
|
|
154
|
+
parent: Dataset<InAndOutQuad, InAndOutQuad>,
|
|
155
|
+
): ITransactionDataset<InAndOutQuad>;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A dataset that allows you to modify the dataset and
|
|
160
|
+
*/
|
|
161
|
+
export interface ITransactionDataset<InAndOutQuad extends BaseQuad = BaseQuad>
|
|
162
|
+
extends ISubscribableDataset<InAndOutQuad> {
|
|
163
|
+
readonly parentDataset: Dataset<InAndOutQuad, InAndOutQuad>;
|
|
164
|
+
rollback(): void;
|
|
165
|
+
commit(): void;
|
|
166
|
+
getChanges(): DatasetChanges<InAndOutQuad>;
|
|
167
|
+
}
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { DatasetChanges } from "@ldo/rdf-utils";
|
|
2
|
+
import type { BaseQuad, Dataset } from "@rdfjs/types";
|
|
3
|
+
import type { IBulkEditableDataset } from "./types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Performs a bulk update for a dataset even if it doesn't have a bulk method.
|
|
7
|
+
* @param dataset - the input dataset
|
|
8
|
+
* @param datasetChanges - changes to be applied
|
|
9
|
+
*/
|
|
10
|
+
export function updateDatasetInBulk<InAndOutQuad extends BaseQuad = BaseQuad>(
|
|
11
|
+
dataset: Dataset<InAndOutQuad>,
|
|
12
|
+
datasetChanges: DatasetChanges<InAndOutQuad>,
|
|
13
|
+
) {
|
|
14
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
15
|
+
if ((dataset as any).bulk) {
|
|
16
|
+
(dataset as IBulkEditableDataset<InAndOutQuad>).bulk(datasetChanges);
|
|
17
|
+
} else {
|
|
18
|
+
if (datasetChanges.added) {
|
|
19
|
+
dataset.addAll(datasetChanges.added);
|
|
20
|
+
}
|
|
21
|
+
if (datasetChanges.removed) {
|
|
22
|
+
datasetChanges.removed.forEach((curQuad) => {
|
|
23
|
+
dataset.delete(curQuad);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|