1 | import { isQuad, DefaultDataFactory, isQuadLike } from "@opennetwork/rdf-data-model";
|
2 | import { Dataset } from "./dataset.js";
|
3 | import { ReadonlyDataset } from "./readonly-dataset.js";
|
4 | export class ImmutableDataset extends Dataset {
|
5 | constructor(set = new Set()) {
|
6 | super(set);
|
7 | this.#set = set;
|
8 | }
|
9 | #set;
|
10 | add(value) {
|
11 | return this.addAll([value]);
|
12 | }
|
13 | addAll(dataset) {
|
14 | return new ImmutableDataset(this.constructSet(this.union(dataset)));
|
15 | }
|
16 | async import(dataset, eager) {
|
17 | const next = this.constructSet(this.#set);
|
18 | for await (const value of dataset) {
|
19 | next.add(isQuad(value) ? value : DefaultDataFactory.fromQuad(value));
|
20 | }
|
21 | return new ImmutableDataset(next);
|
22 | }
|
23 | delete(quad) {
|
24 | return new ImmutableDataset(this.constructSet(this.without(quad)));
|
25 | }
|
26 | replace(replacing, replacers) {
|
27 | const replacingDataset = new Set(new ReadonlyDataset().union((isQuad(replacing) || isQuadLike(replacing)) ? [replacing] : replacing).filter(quad => this.has(quad)));
|
28 | if (!replacingDataset.size) {
|
29 |
|
30 | return this;
|
31 | }
|
32 | return new ImmutableDataset(this.constructSet(this
|
33 | .except(quad => replacingDataset.has(quad))
|
34 | .union((isQuad(replacers) || isQuadLike(replacers)) ? [replacers] : replacers)));
|
35 | }
|
36 | constructSet(initial) {
|
37 | if (this.#set.construct) {
|
38 | return this.#set.construct(initial);
|
39 | }
|
40 | else {
|
41 | return new Set(initial);
|
42 | }
|
43 | }
|
44 | }
|
45 |
|
\ | No newline at end of file |