128
|
1 import { EventEmitter } from "events";
|
|
2 import {
|
|
3 BlankNode,
|
|
4 OTerm,
|
|
5 Quad,
|
|
6 QuadPredicate,
|
|
7 Store,
|
|
8 Term,
|
|
9 extractListOptions,
|
|
10 } from "n3";
|
|
11 import * as RDF from "rdf-js";
|
|
12 import { SubEvent } from "sub-events";
|
144
|
13 import { Patch, PatchDirection } from "./Patch";
|
128
|
14 import { SourceGraph } from "./SourceGraph";
|
|
15
|
|
16 // queries over multiple Store objects
|
|
17 export class MultiStore implements Store {
|
|
18 // emitted when there's been a net change to the graph data
|
|
19 graphChanged: SubEvent<Patch> = new SubEvent();
|
|
20
|
|
21 // sources of statements
|
|
22 private stores: Store[] = [];
|
|
23 private tempCombinedGraph: Store;
|
|
24
|
|
25 constructor() {
|
|
26 this.tempCombinedGraph = new Store();
|
|
27 }
|
|
28
|
|
29 newStore(s: SourceGraph) {
|
|
30 this.stores.push(s.store);
|
144
|
31 const p = new Patch(PatchDirection.ADD); // todo
|
128
|
32 s.sourceGraphChanged.subscribe((p) => {
|
|
33 this.sourceGraphDataChanged(p); // todo
|
|
34 });
|
|
35 }
|
|
36
|
|
37 lostStore(s: Store) {
|
|
38 throw new Error("notimplemented");
|
|
39 }
|
|
40
|
|
41 sourceGraphDataChanged(p: Patch) {
|
|
42
|
|
43 this.tempCombinedGraph = new Store();
|
|
44 for (let st of this.stores) {
|
|
45 for (let q of st.getQuads(null, null, null, null)) {
|
|
46 this.tempCombinedGraph.addQuad(q);
|
|
47 }
|
|
48 }
|
|
49 this.graphChanged.emit(p);
|
|
50 }
|
|
51
|
|
52 //
|
|
53 // Store interface follows:
|
|
54 //
|
|
55 forEach(qfn: (qfn: Quad) => void, s: OTerm, p: OTerm, o: OTerm, g: OTerm) {
|
|
56 this.tempCombinedGraph.forEach(qfn, s, p, o, g);
|
|
57 }
|
|
58 countQuads(s: OTerm, p: OTerm, o: OTerm, g: OTerm): number {
|
|
59 return this.tempCombinedGraph.countQuads(s, p, o, g);
|
|
60 // const seen: Set<Quad> = new Set();
|
|
61 // let count = 0;
|
|
62 // for (let src of this.sources.currentSourceGraphs) {
|
|
63 // for (let q of src.store.getQuads(s, p, o, g)) {
|
|
64 // if (!seen.has(q)) {
|
|
65 // count++;
|
|
66 // seen.add(q);
|
|
67 // }
|
|
68 // }
|
|
69 // }
|
|
70 // return count;
|
|
71 }
|
|
72
|
|
73 get size(): number {
|
|
74 return this.countQuads(null, null, null, null);
|
|
75 }
|
|
76 has(quad: Quad): boolean {
|
|
77 throw new Error("notimplemented");
|
|
78 }
|
|
79 getQuads(
|
|
80 subject: OTerm,
|
|
81 predicate: OTerm,
|
|
82 object: OTerm | OTerm[],
|
|
83 graph: OTerm
|
|
84 ): Quad[] {
|
|
85 return this.tempCombinedGraph.getQuads(subject, predicate, object, graph);
|
|
86 }
|
|
87 match(
|
|
88 subject?: Term | null,
|
|
89 predicate?: Term | null,
|
|
90 object?: Term | null,
|
|
91 graph?: Term | null
|
|
92 ): RDF.Stream<Quad> & RDF.DatasetCore<Quad, Quad> {
|
|
93 throw new Error("notimplemented");
|
|
94 }
|
|
95
|
|
96 every(
|
|
97 callback: QuadPredicate<Quad>,
|
|
98 subject: OTerm,
|
|
99 predicate: OTerm,
|
|
100 object: OTerm,
|
|
101 graph: OTerm
|
|
102 ): boolean {
|
|
103 throw new Error("notimplemented");
|
|
104 }
|
|
105 some(
|
|
106 callback: QuadPredicate<Quad>,
|
|
107 subject: OTerm,
|
|
108 predicate: OTerm,
|
|
109 object: OTerm,
|
|
110 graph: OTerm
|
|
111 ): boolean {
|
|
112 throw new Error("notimplemented");
|
|
113 }
|
|
114 getSubjects(
|
|
115 predicate: OTerm,
|
|
116 object: OTerm,
|
|
117 graph: OTerm
|
|
118 ): Array<Quad["subject"]> {
|
|
119 throw new Error("notimplemented");
|
|
120 }
|
|
121 forSubjects(
|
|
122 callback: (result: Quad["subject"]) => void,
|
|
123 predicate: OTerm,
|
|
124 object: OTerm,
|
|
125 graph: OTerm
|
|
126 ): void {
|
|
127 throw new Error("notimplemented");
|
|
128 }
|
|
129 getPredicates(
|
|
130 subject: OTerm,
|
|
131 object: OTerm,
|
|
132 graph: OTerm
|
|
133 ): Array<Quad["predicate"]> {
|
|
134 throw new Error("notimplemented");
|
|
135 return [];
|
|
136 }
|
|
137 forPredicates(
|
|
138 callback: (result: Quad["predicate"]) => void,
|
|
139 subject: OTerm,
|
|
140 object: OTerm,
|
|
141 graph: OTerm
|
|
142 ): void {
|
|
143 throw new Error("notimplemented");
|
|
144 }
|
|
145 getObjects(
|
|
146 subject: OTerm,
|
|
147 predicate: OTerm,
|
|
148 graph: OTerm
|
|
149 ): Array<Quad["object"]> {
|
|
150 return this.tempCombinedGraph.getObjects(subject, predicate, graph);
|
|
151 }
|
|
152 forObjects(
|
|
153 callback: (result: Quad["object"]) => void,
|
|
154 subject: OTerm,
|
|
155 predicate: OTerm,
|
|
156 graph: OTerm
|
|
157 ): void {
|
|
158 throw new Error("notimplemented");
|
|
159 }
|
|
160 getGraphs(
|
|
161 subject: OTerm,
|
|
162 predicate: OTerm,
|
|
163 object: OTerm
|
|
164 ): Array<Quad["graph"]> {
|
|
165 throw new Error("notimplemented");
|
|
166 }
|
|
167 forGraphs(
|
|
168 callback: (result: Quad["graph"]) => void,
|
|
169 subject: OTerm,
|
|
170 predicate: OTerm,
|
|
171 object: OTerm
|
|
172 ): void {
|
|
173 throw new Error("notimplemented");
|
|
174 }
|
|
175 extractLists(options?: extractListOptions): Record<string, RDF.Term[]> {
|
|
176 throw new Error("notimplemented");
|
|
177 }
|
|
178 [Symbol.iterator](): Iterator<Quad> {
|
|
179 throw new Error("notimplemented");
|
|
180 }
|
|
181
|
|
182 add(): this {
|
|
183 throw new Error("MultiStore is readonly");
|
|
184 }
|
|
185 addQuad() {
|
|
186 throw new Error("notimplemented");
|
|
187 }
|
|
188 addQuads(): void {
|
|
189 throw new Error("MultiStore is readonly");
|
|
190 }
|
|
191 delete(): this {
|
|
192 throw new Error("MultiStore is readonly");
|
|
193 }
|
|
194 import(): EventEmitter {
|
|
195 throw new Error("MultiStore is readonly");
|
|
196 }
|
|
197 removeQuad(): void {
|
|
198 throw new Error("MultiStore is readonly");
|
|
199 }
|
|
200 removeQuads(): void {
|
|
201 throw new Error("MultiStore is readonly");
|
|
202 }
|
|
203 remove(): EventEmitter {
|
|
204 throw new Error("MultiStore is readonly");
|
|
205 }
|
|
206 removeMatches(): EventEmitter {
|
|
207 throw new Error("MultiStore is readonly");
|
|
208 }
|
|
209 deleteGraph(): EventEmitter {
|
|
210 throw new Error("MultiStore is readonly");
|
|
211 }
|
|
212 createBlankNode(): BlankNode {
|
|
213 throw new Error("MultiStore is readonly");
|
|
214 }
|
|
215 }
|