@ldo/subscribable-dataset 0.0.1-alpha.14

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.
Files changed (33) hide show
  1. package/dist/ProxyTransactionalDataset.d.ts +27 -0
  2. package/dist/ProxyTransactionalDataset.js +138 -0
  3. package/dist/ProxyTransactionalDataset.js.map +1 -0
  4. package/dist/WrapperSubscribableDataset.d.ts +53 -0
  5. package/dist/WrapperSubscribableDataset.js +236 -0
  6. package/dist/WrapperSubscribableDataset.js.map +1 -0
  7. package/dist/WrapperSubscribableDatasetFactory.d.ts +7 -0
  8. package/dist/WrapperSubscribableDatasetFactory.js +16 -0
  9. package/dist/WrapperSubscribableDatasetFactory.js.map +1 -0
  10. package/dist/createWrapperSubscribableDataset.d.ts +5 -0
  11. package/dist/createWrapperSubscribableDataset.js +23 -0
  12. package/dist/createWrapperSubscribableDataset.js.map +1 -0
  13. package/dist/createWrapperSubscribableDatasetFromSerializedInput.d.ts +4 -0
  14. package/dist/createWrapperSubscribableDatasetFromSerializedInput.js +10 -0
  15. package/dist/createWrapperSubscribableDatasetFromSerializedInput.js.map +1 -0
  16. package/dist/index.d.ts +7 -0
  17. package/dist/index.js +34 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/mergeDatasetChanges.d.ts +3 -0
  20. package/dist/mergeDatasetChanges.js +39 -0
  21. package/dist/mergeDatasetChanges.js.map +1 -0
  22. package/dist/types.d.ts +30 -0
  23. package/dist/types.js +3 -0
  24. package/dist/types.js.map +1 -0
  25. package/package.json +47 -0
  26. package/src/ProxyTransactionalDataset.ts +317 -0
  27. package/src/WrapperSubscribableDataset.ts +616 -0
  28. package/src/WrapperSubscribableDatasetFactory.ts +27 -0
  29. package/src/createWrapperSubscribableDataset.ts +30 -0
  30. package/src/createWrapperSubscribableDatasetFromSerializedInput.ts +28 -0
  31. package/src/index.ts +10 -0
  32. package/src/mergeDatasetChanges.ts +53 -0
  33. package/src/types.ts +144 -0
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mergeDatasetChanges = void 0;
4
+ function mergeDatasetChanges(originalChange, newChange) {
5
+ if (newChange.added) {
6
+ if (originalChange.added) {
7
+ originalChange.added.addAll(newChange.added);
8
+ }
9
+ else {
10
+ originalChange.added = newChange.added;
11
+ }
12
+ const changesIntersection = originalChange.removed?.intersection(newChange.added);
13
+ if (changesIntersection && changesIntersection.size > 0) {
14
+ originalChange.removed =
15
+ originalChange.removed?.difference(changesIntersection);
16
+ }
17
+ }
18
+ if (newChange.removed) {
19
+ if (originalChange.removed) {
20
+ originalChange.removed.addAll(newChange.removed);
21
+ }
22
+ else {
23
+ originalChange.removed = newChange.removed;
24
+ }
25
+ const changesIntersection = originalChange.added?.intersection(newChange.removed);
26
+ if (changesIntersection && changesIntersection.size > 0) {
27
+ originalChange.added =
28
+ originalChange.added?.difference(changesIntersection);
29
+ }
30
+ }
31
+ if (originalChange.added && originalChange.added.size === 0) {
32
+ originalChange.added = undefined;
33
+ }
34
+ if (originalChange.removed && originalChange.removed.size === 0) {
35
+ originalChange.removed = undefined;
36
+ }
37
+ }
38
+ exports.mergeDatasetChanges = mergeDatasetChanges;
39
+ //# sourceMappingURL=mergeDatasetChanges.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mergeDatasetChanges.js","sourceRoot":"","sources":["../src/mergeDatasetChanges.ts"],"names":[],"mappings":";;;AAQA,SAAgB,mBAAmB,CACjC,cAA4C,EAC5C,SAAuC;IAGvC,IAAI,SAAS,CAAC,KAAK,EAAE;QACnB,IAAI,cAAc,CAAC,KAAK,EAAE;YACxB,cAAc,CAAC,KAAK,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;SAC9C;aAAM;YACL,cAAc,CAAC,KAAK,GAAG,SAAS,CAAC,KAAK,CAAC;SACxC;QAED,MAAM,mBAAmB,GAAG,cAAc,CAAC,OAAO,EAAE,YAAY,CAC9D,SAAS,CAAC,KAAK,CAChB,CAAC;QACF,IAAI,mBAAmB,IAAI,mBAAmB,CAAC,IAAI,GAAG,CAAC,EAAE;YACvD,cAAc,CAAC,OAAO;gBACpB,cAAc,CAAC,OAAO,EAAE,UAAU,CAAC,mBAAmB,CAAC,CAAC;SAC3D;KACF;IAED,IAAI,SAAS,CAAC,OAAO,EAAE;QACrB,IAAI,cAAc,CAAC,OAAO,EAAE;YAC1B,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;SAClD;aAAM;YACL,cAAc,CAAC,OAAO,GAAG,SAAS,CAAC,OAAO,CAAC;SAC5C;QAED,MAAM,mBAAmB,GAAG,cAAc,CAAC,KAAK,EAAE,YAAY,CAC5D,SAAS,CAAC,OAAO,CAClB,CAAC;QACF,IAAI,mBAAmB,IAAI,mBAAmB,CAAC,IAAI,GAAG,CAAC,EAAE;YACvD,cAAc,CAAC,KAAK;gBAClB,cAAc,CAAC,KAAK,EAAE,UAAU,CAAC,mBAAmB,CAAC,CAAC;SACzD;KACF;IAGD,IAAI,cAAc,CAAC,KAAK,IAAI,cAAc,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,EAAE;QAC3D,cAAc,CAAC,KAAK,GAAG,SAAS,CAAC;KAClC;IACD,IAAI,cAAc,CAAC,OAAO,IAAI,cAAc,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC,EAAE;QAC/D,cAAc,CAAC,OAAO,GAAG,SAAS,CAAC;KACpC;AACH,CAAC;AA5CD,kDA4CC"}
@@ -0,0 +1,30 @@
1
+ import type { DatasetChanges, QuadMatch } from "@ldo/rdf-utils";
2
+ import type { Dataset, BaseQuad } from "@rdfjs/types";
3
+ export type nodeEventListener<InAndOutQuad extends BaseQuad = BaseQuad> = (changes: DatasetChanges<InAndOutQuad>) => void;
4
+ export interface BulkEditableDataset<InAndOutQuad extends BaseQuad = BaseQuad> extends Dataset<InAndOutQuad, InAndOutQuad> {
5
+ bulk(changes: DatasetChanges<InAndOutQuad>): this;
6
+ }
7
+ export interface TransactionalDataset<InAndOutQuad extends BaseQuad = BaseQuad> extends Dataset<InAndOutQuad, InAndOutQuad> {
8
+ rollback(): void;
9
+ commit(): void;
10
+ getChanges(): DatasetChanges<InAndOutQuad>;
11
+ }
12
+ export interface SubscribableDataset<InAndOutQuad extends BaseQuad = BaseQuad> extends BulkEditableDataset<InAndOutQuad> {
13
+ addListener(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): this;
14
+ emit(eventName: QuadMatch, changes: DatasetChanges<InAndOutQuad>): boolean;
15
+ eventNames(): QuadMatch[];
16
+ getMaxListeners(): number;
17
+ listenerCount(eventName: QuadMatch): number;
18
+ listeners(eventName: QuadMatch): nodeEventListener<InAndOutQuad>[];
19
+ off(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): void;
20
+ on(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): this;
21
+ once(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): this;
22
+ prependListener(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): this;
23
+ prependOnceListener(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): this;
24
+ removeAllListeners(eventName: QuadMatch): this;
25
+ removeListener(eventName: QuadMatch, listener: nodeEventListener<InAndOutQuad>): this;
26
+ removeListenerFromAllEvents(listener: nodeEventListener<InAndOutQuad>): this;
27
+ setMaxListeners(n: number): this;
28
+ rawListeners(eventName: QuadMatch): nodeEventListener<InAndOutQuad>[];
29
+ startTransaction(): TransactionalDataset<InAndOutQuad>;
30
+ }
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@ldo/subscribable-dataset",
3
+ "version": "0.0.1-alpha.14",
4
+ "description": "An RDFJS dataset implementation that can be subscribed to for updates",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc --project tsconfig.build.json",
8
+ "watch": "tsc --watch",
9
+ "test": "jest --coverage",
10
+ "example:subscribableDataset": "ts-node ./example/subscribableDatasetExample.ts",
11
+ "prepublishOnly": "npm run test && npm run build",
12
+ "lint": "eslint src/** --fix --no-error-on-unmatched-pattern"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/o-development/ldobjects.git"
17
+ },
18
+ "author": "Jackson Morgan",
19
+ "license": "MIT",
20
+ "bugs": {
21
+ "url": "https://github.com/o-development/ldobjects/issues"
22
+ },
23
+ "homepage": "https://github.com/o-development/ldobjects/tree/main/packages/subscribable-dataset#readme",
24
+ "devDependencies": {
25
+ "@rdfjs/data-model": "^1.2.0",
26
+ "@rdfjs/dataset": "^1.1.0",
27
+ "@rdfjs/types": "^1.0.1",
28
+ "@types/jest": "^27.0.3",
29
+ "@types/jsonld": "^1.5.6",
30
+ "@types/rdfjs__dataset": "^1.0.4",
31
+ "jest": "^27.4.5",
32
+ "ts-jest": "^27.1.2",
33
+ "ts-node": "^9.1.1"
34
+ },
35
+ "dependencies": {
36
+ "@ldo/dataset": "^0.0.1-alpha.14",
37
+ "@ldo/rdf-utils": "^0.0.1-alpha.14"
38
+ },
39
+ "files": [
40
+ "dist",
41
+ "src"
42
+ ],
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "gitHead": "5acfea4c43c07c02df9ed4e9166e61a540e26db8"
47
+ }
@@ -0,0 +1,317 @@
1
+ import type { Dataset, BaseQuad, Term, DatasetFactory } from "@rdfjs/types";
2
+ import type { DatasetChanges } from "@ldo/rdf-utils";
3
+ import type { BulkEditableDataset, TransactionalDataset } from "./types";
4
+ import { ExtendedDataset } from "@ldo/dataset";
5
+ import { mergeDatasetChanges } from "./mergeDatasetChanges";
6
+
7
+ /**
8
+ * Proxy Transactional Dataset is a transactional dataset that does not duplicate
9
+ * the parent dataset, it will dynamically determine the correct return value for
10
+ * methods in real time when the method is called.
11
+ */
12
+ export default class ProxyTransactionalDataset<
13
+ InAndOutQuad extends BaseQuad = BaseQuad,
14
+ >
15
+ extends ExtendedDataset<InAndOutQuad>
16
+ implements
17
+ BulkEditableDataset<InAndOutQuad>,
18
+ TransactionalDataset<InAndOutQuad>
19
+ {
20
+ /**
21
+ * The parent dataset that will be updated upon commit
22
+ */
23
+ private parentDataset: Dataset<InAndOutQuad, InAndOutQuad>;
24
+
25
+ /**
26
+ * A factory for creating new datasets to be added to the update method
27
+ */
28
+ private datasetFactory: DatasetFactory<InAndOutQuad, InAndOutQuad>;
29
+
30
+ /**
31
+ * The changes made that are ready to commit
32
+ */
33
+ private datasetChanges: DatasetChanges<InAndOutQuad>;
34
+
35
+ /**
36
+ * A list of changes made to the parent dataset upon commit used for rolling back changes.
37
+ * This is different from 'datasetChanges' because datasetChanges is allowed to overlap
38
+ * with the parent dataset.
39
+ * For example, the parent dataset may already have triple A, and datasetChanges can
40
+ * also have triple A.
41
+ */
42
+ private committedDatasetChanges?: DatasetChanges<InAndOutQuad>;
43
+
44
+ /**
45
+ * Constructor
46
+ * @param parentDataset The dataset that will be updated upon commit
47
+ */
48
+ constructor(
49
+ parentDataset: Dataset<InAndOutQuad, InAndOutQuad>,
50
+ datasetFactory: DatasetFactory<InAndOutQuad, InAndOutQuad>,
51
+ ) {
52
+ super(datasetFactory.dataset(), datasetFactory);
53
+ this.parentDataset = parentDataset;
54
+ this.datasetFactory = datasetFactory;
55
+ this.datasetChanges = {};
56
+ }
57
+
58
+ /**
59
+ * ==================================================================
60
+ * DATASET METHODS
61
+ * ==================================================================
62
+ */
63
+
64
+ /**
65
+ * Imports the quads into this dataset.
66
+ * 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.
67
+ * @param quads
68
+ * @returns the dataset instance it was called on.
69
+ */
70
+ public addAll(
71
+ quads: Dataset<InAndOutQuad, InAndOutQuad> | InAndOutQuad[],
72
+ ): this {
73
+ this.updateDatasetChanges({ added: quads });
74
+ return this;
75
+ }
76
+
77
+ /**
78
+ * Bulk add and remove triples
79
+ * @param changed
80
+ */
81
+ public bulk(changes: DatasetChanges<InAndOutQuad>): this {
82
+ this.updateDatasetChanges(changes);
83
+ return this;
84
+ }
85
+
86
+ /**
87
+ * 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.
88
+ * @param subject
89
+ * @param predicate
90
+ * @param object
91
+ * @param graph
92
+ * @returns the dataset instance it was called on.
93
+ */
94
+ deleteMatches(
95
+ subject?: Term,
96
+ predicate?: Term,
97
+ object?: Term,
98
+ graph?: Term,
99
+ ): this {
100
+ this.checkIfTransactionCommitted();
101
+ const matching = this.match(subject, predicate, object, graph);
102
+ for (const quad of matching) {
103
+ this.delete(quad);
104
+ }
105
+ return this;
106
+ }
107
+
108
+ /**
109
+ * 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.
110
+ * @param subject
111
+ * @param predicate
112
+ * @param object
113
+ * @param graph
114
+ * @returns a Dataset with matching triples
115
+ */
116
+ public match(
117
+ subject?: Term | null,
118
+ predicate?: Term | null,
119
+ object?: Term | null,
120
+ graph?: Term | null,
121
+ ): Dataset<InAndOutQuad, InAndOutQuad> {
122
+ let finalMatch = this.parentDataset.match(
123
+ subject,
124
+ predicate,
125
+ object,
126
+ graph,
127
+ );
128
+ if (this.datasetChanges.removed) {
129
+ finalMatch = finalMatch.difference(this.datasetChanges.removed);
130
+ }
131
+ if (this.datasetChanges.added) {
132
+ finalMatch = finalMatch.union(
133
+ this.datasetChanges.added.match(subject, predicate, object, graph),
134
+ );
135
+ }
136
+ return finalMatch;
137
+ }
138
+
139
+ /**
140
+ * A non-negative integer that specifies the number of quads in the set.
141
+ */
142
+ public get size(): number {
143
+ return (
144
+ this.parentDataset.size +
145
+ (this.datasetChanges.added?.difference(this.parentDataset).size || 0) -
146
+ (this.datasetChanges.removed?.intersection(this.parentDataset).size || 0)
147
+ );
148
+ }
149
+
150
+ /**
151
+ * Adds the specified quad to the dataset.
152
+ * Existing quads, as defined in Quad.equals, will be ignored.
153
+ * @param quad
154
+ * @returns the dataset instance it was called on.
155
+ */
156
+ public add(quad: InAndOutQuad): this {
157
+ this.updateDatasetChanges({ added: [quad] });
158
+ return this;
159
+ }
160
+
161
+ /**
162
+ * Removes the specified quad from the dataset.
163
+ * This method returns the dataset instance it was called on.
164
+ * @param quad
165
+ */
166
+ public delete(quad: InAndOutQuad): this {
167
+ this.updateDatasetChanges({ removed: [quad] });
168
+ return this;
169
+ }
170
+
171
+ /**
172
+ * Determines whether a dataset includes a certain quad, returning true or false as appropriate.
173
+ * @param quad
174
+ */
175
+ public has(quad: InAndOutQuad): boolean {
176
+ return (
177
+ !this.datasetChanges.removed?.has(quad) &&
178
+ (this.datasetChanges.added?.has(quad) || this.parentDataset.has(quad))
179
+ );
180
+ }
181
+
182
+ /**
183
+ * Returns an iterator
184
+ */
185
+ public [Symbol.iterator](): Iterator<InAndOutQuad> {
186
+ const addedIterator = (this.datasetChanges.added || [])[Symbol.iterator]();
187
+ let addedNext = addedIterator.next();
188
+ const parentIterator = this.parentDataset[Symbol.iterator]();
189
+ let parentNext = parentIterator.next();
190
+ return {
191
+ next: () => {
192
+ if (!addedNext || !addedNext.done) {
193
+ const toReturn = addedNext;
194
+ addedNext = addedIterator.next();
195
+ return toReturn;
196
+ }
197
+ while (!parentNext.done) {
198
+ const toReturn = parentNext;
199
+ parentNext = parentIterator.next();
200
+ if (
201
+ !(
202
+ this.datasetChanges.added &&
203
+ this.datasetChanges.added.has(toReturn.value)
204
+ ) &&
205
+ !(
206
+ this.datasetChanges.removed &&
207
+ this.datasetChanges.removed.has(toReturn.value)
208
+ )
209
+ ) {
210
+ return toReturn;
211
+ }
212
+ }
213
+ return { value: undefined, done: true };
214
+ },
215
+ };
216
+ }
217
+
218
+ /**
219
+ * ==================================================================
220
+ * TANSACTIONAL METHODS
221
+ * ==================================================================
222
+ */
223
+
224
+ /**
225
+ * Checks if the transaction has been committed and throws an error if it has
226
+ * @param changed
227
+ */
228
+ private checkIfTransactionCommitted() {
229
+ if (this.committedDatasetChanges) {
230
+ throw new Error("Transaction has already committed");
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Helper method to update the changes made
236
+ * @param changes
237
+ */
238
+ private updateDatasetChanges(changes: {
239
+ added?: Dataset<InAndOutQuad> | InAndOutQuad[];
240
+ removed?: Dataset<InAndOutQuad> | InAndOutQuad[];
241
+ }): void {
242
+ this.checkIfTransactionCommitted();
243
+
244
+ mergeDatasetChanges(this.datasetChanges, {
245
+ added: changes.added
246
+ ? this.datasetFactory.dataset(changes.added)
247
+ : undefined,
248
+ removed: changes.removed
249
+ ? this.datasetFactory.dataset(changes.removed)
250
+ : undefined,
251
+ });
252
+ }
253
+
254
+ /**
255
+ * Helper method to update the parent dataset or any other provided dataset
256
+ */
257
+ private updateParentDataset(datasetChanges: DatasetChanges<InAndOutQuad>) {
258
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
259
+ if ((this.parentDataset as any).bulk) {
260
+ (this.parentDataset as BulkEditableDataset<InAndOutQuad>).bulk(
261
+ datasetChanges,
262
+ );
263
+ } else {
264
+ if (datasetChanges.added) {
265
+ this.parentDataset.addAll(datasetChanges.added);
266
+ }
267
+ if (datasetChanges.removed) {
268
+ datasetChanges.removed.forEach((curQuad) => {
269
+ this.parentDataset.delete(curQuad);
270
+ });
271
+ }
272
+ }
273
+ }
274
+
275
+ /**
276
+ * Commits changes made to the parent dataset
277
+ */
278
+ public commit(): void {
279
+ this.checkIfTransactionCommitted();
280
+ this.committedDatasetChanges = {
281
+ added: this.datasetChanges.added?.difference(this.parentDataset),
282
+ removed: this.datasetChanges.removed?.intersection(this.parentDataset),
283
+ };
284
+ this.updateParentDataset(this.committedDatasetChanges);
285
+ }
286
+
287
+ /**
288
+ * Rolls back changes made to the parent dataset
289
+ */
290
+ public rollback(): void {
291
+ if (!this.committedDatasetChanges) {
292
+ throw new Error(
293
+ "Cannot rollback. Transaction has not yet been committed",
294
+ );
295
+ }
296
+ this.updateParentDataset({
297
+ added: this.committedDatasetChanges.removed,
298
+ removed: this.committedDatasetChanges.added,
299
+ });
300
+ this.committedDatasetChanges = undefined;
301
+ }
302
+
303
+ /**
304
+ * Starts a new transaction with this transactional dataset as the parent
305
+ * @returns
306
+ */
307
+ public startTransaction(): TransactionalDataset<InAndOutQuad> {
308
+ // This is caused by the typings being incorrect for the intersect method
309
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
310
+ // @ts-ignore
311
+ return new ProxyTransactionalDataset(this, this.datasetFactory);
312
+ }
313
+
314
+ public getChanges(): DatasetChanges<InAndOutQuad> {
315
+ return this.datasetChanges;
316
+ }
317
+ }