1
- import { DataFactory } from "@ontologies/core" ;
1
+ import { DataFactory , QuadPosition } from "@ontologies/core" ;
2
2
import ld from "@ontologies/ld" ;
3
3
import rdf from "@ontologies/rdf" ;
4
- import {
5
- graph ,
6
- Store ,
7
- } from "./rdflib" ;
8
4
9
5
import ll from "./ontology/ll" ;
10
6
import rdfFactory , {
11
7
NamedNode ,
12
- Node ,
8
+ OptionalNamedNode ,
13
9
OptionalNode ,
10
+ OptionalTerm ,
14
11
Quad ,
15
12
Quadruple ,
16
13
Term ,
17
14
} from "./rdf" ;
18
15
import { deltaProcessor } from "./store/deltaProcessor" ;
16
+ import RDFIndex from "./store/RDFIndex" ;
19
17
import { ChangeBuffer , DeltaProcessor , SomeNode , StoreProcessor } from "./types" ;
20
18
import { allRDFPropertyStatements , getPropBestLang } from "./utilities" ;
21
19
import { patchRDFLibStoreWithOverrides } from "./utilities/monkeys" ;
@@ -24,7 +22,7 @@ const EMPTY_ST_ARR: ReadonlyArray<Quad> = Object.freeze([]);
24
22
25
23
export interface RDFStoreOpts {
26
24
deltaProcessorOpts ?: { [ k : string ] : Array < NamedNode | undefined > } ;
27
- innerStore ?: Store ;
25
+ innerStore ?: RDFIndex ;
28
26
}
29
27
30
28
/**
@@ -46,21 +44,21 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
46
44
private langPrefs : string [ ] = Array . from ( typeof navigator !== "undefined"
47
45
? ( navigator . languages || [ navigator . language ] )
48
46
: [ "en" ] ) ;
49
- private store : Store = graph ( ) ;
47
+ private store : RDFIndex = new RDFIndex ( ) ;
50
48
51
49
public get rdfFactory ( ) : DataFactory {
52
- return this . store . rdfFactory ;
50
+ return rdfFactory ;
53
51
}
54
- public set rdfFactory ( value : DataFactory ) {
55
- throw this . store . rdfFactory = value ;
52
+ public set rdfFactory ( _ : DataFactory ) {
53
+ throw new Error ( "Factory is global (see @ontologies/core)" ) ;
56
54
}
57
55
58
56
constructor ( { deltaProcessorOpts, innerStore } : RDFStoreOpts = { } ) {
59
57
this . processDelta = this . processDelta . bind ( this ) ;
60
58
61
- const g = innerStore || graph ( ) ;
59
+ const g = innerStore || new RDFIndex ( ) ;
60
+ g . addDataCallback ( this . processTypeStatement . bind ( this ) ) ;
62
61
this . store = patchRDFLibStoreWithOverrides ( g , this ) ;
63
- this . store . newPropertyAction ( rdf . type , this . processTypeStatement . bind ( this ) ) ;
64
62
65
63
const defaults = {
66
64
addGraphIRIS : [ ll . add , ld . add ] ,
@@ -88,7 +86,7 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
88
86
* Add statements to the store.
89
87
* @param data Data to parse and add to the store.
90
88
*/
91
- public addStatements ( data : Quad [ ] ) : void {
89
+ public addQuads ( data : Quad [ ] ) : void {
92
90
if ( ! Array . isArray ( data ) ) {
93
91
throw new TypeError ( "An array of statements must be passed to addStatements" ) ;
94
92
}
@@ -98,7 +96,7 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
98
96
}
99
97
}
100
98
101
- public addQuads ( data : Quadruple [ ] ) : Quad [ ] {
99
+ public addQuadruples ( data : Quadruple [ ] ) : Quad [ ] {
102
100
const statements = new Array ( data . length ) ;
103
101
for ( let i = 0 , len = data . length ; i < len ; i ++ ) {
104
102
statements [ i ] = this . store . add ( data [ i ] [ 0 ] , data [ i ] [ 1 ] , data [ i ] [ 2 ] , data [ i ] [ 3 ] ) ;
@@ -107,35 +105,13 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
107
105
return statements ;
108
106
}
109
107
110
- public any (
111
- subj : OptionalNode ,
112
- pred ?: OptionalNode ,
113
- obj ?: OptionalNode ,
114
- why ?: OptionalNode ,
115
- ) : Term | undefined {
116
- return this . store . any ( subj , pred , obj , why ) ;
117
- }
118
-
119
- public anyStatementMatching ( subj : OptionalNode ,
120
- pred ?: OptionalNode ,
121
- obj ?: OptionalNode ,
122
- why ?: OptionalNode ) : Quad | undefined {
123
- return this . store . anyStatementMatching ( subj , pred , obj , why ) ;
124
- }
125
-
126
- public anyValue ( subj : OptionalNode ,
127
- pred ?: OptionalNode ,
128
- obj ?: OptionalNode ,
129
- why ?: OptionalNode ) : string | undefined {
130
- return this . store . anyValue ( subj , pred , obj , why ) ;
131
- }
132
-
133
108
public canon ( term : SomeNode ) : SomeNode {
134
- return this . store . canon ( term ) ;
109
+ // TODO
110
+ return term ;
135
111
}
136
112
137
113
public defaultGraph ( ) : SomeNode {
138
- return this . store . rdfFactory . defaultGraph ( ) ;
114
+ return rdfFactory . defaultGraph ( ) ;
139
115
}
140
116
141
117
/**
@@ -162,21 +138,26 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
162
138
this . changeTimestamps [ rdfFactory . id ( s . subject ) ] = changeStamp ;
163
139
return rdfFactory . equals ( s . predicate , rdf . type ) ;
164
140
} )
165
- . map ( ( s ) => this . processTypeStatement ( undefined , s . subject , s . predicate , undefined , undefined ) ) ;
141
+ . map ( ( s ) => this . processTypeStatement ( s ) ) ;
166
142
167
143
return processingBuffer ;
168
144
}
169
145
170
146
/** @private */
171
- public getInternalStore ( ) : Store {
147
+ public getInternalStore ( ) : RDFIndex {
172
148
return this . store ;
173
149
}
174
150
175
151
public match ( subj : OptionalNode ,
176
- pred ?: OptionalNode ,
177
- obj ?: OptionalNode ,
178
- why ?: OptionalNode ) : Quad [ ] {
179
- return this . store . match ( subj , pred , obj , why ) || [ ] ;
152
+ pred ?: OptionalNamedNode ,
153
+ obj ?: OptionalTerm ,
154
+ graph ?: OptionalNode ) : Quad [ ] {
155
+ return this . store . match (
156
+ subj || null ,
157
+ pred || null ,
158
+ obj || null ,
159
+ graph || null ,
160
+ ) || [ ] ;
180
161
}
181
162
182
163
public processDelta ( delta : Quadruple [ ] ) : Quad [ ] {
@@ -186,19 +167,19 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
186
167
removables ,
187
168
] = this . deltaProcessor ( delta ) ;
188
169
189
- this . removeStatements ( removables ) ;
170
+ this . removeQuads ( removables ) ;
190
171
191
- return this . replaceMatches ( replacables ) . concat ( this . addQuads ( addables ) ) ;
172
+ return this . replaceMatches ( replacables ) . concat ( this . addQuadruples ( addables ) ) ;
192
173
}
193
174
194
175
public removeResource ( subject : SomeNode ) : void {
195
176
this . touch ( subject ) ;
196
177
this . typeCache [ rdfFactory . id ( subject ) ] = [ ] ;
197
- this . removeStatements ( this . statementsFor ( subject ) ) ;
178
+ this . removeQuads ( this . statementsFor ( subject ) ) ;
198
179
}
199
180
200
- public removeStatements ( statements : Quad [ ] ) : void {
201
- this . store . remove ( statements . slice ( ) ) ;
181
+ public removeQuads ( statements : Quad [ ] ) : void {
182
+ this . store . removeQuads ( statements ) ;
202
183
}
203
184
204
185
/**
@@ -210,7 +191,7 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
210
191
* @param original The statements to remove from the store.
211
192
* @param replacement The statements to add to the store.
212
193
*/
213
- public replaceStatements ( original : Quad [ ] , replacement : Quad [ ] ) : void {
194
+ public replaceQuads ( original : Quad [ ] , replacement : Quad [ ] ) : void {
214
195
const uniqueStatements = new Array ( replacement . length ) . filter ( Boolean ) ;
215
196
for ( let i = 0 ; i < replacement . length ; i ++ ) {
216
197
const cond = original . some (
@@ -222,26 +203,31 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
222
203
}
223
204
}
224
205
225
- this . removeStatements ( original ) ;
206
+ this . removeQuads ( original ) ;
226
207
// Remove statements not in the old object. Useful for replacing data loosely related to the main resource.
227
208
for ( let i = 0 ; i < uniqueStatements . length ; i ++ ) {
228
- this . store . removeMatches ( uniqueStatements [ i ] . subject , uniqueStatements [ i ] . predicate ) ;
209
+ this . store . removeMatches (
210
+ uniqueStatements [ i ] . subject ,
211
+ uniqueStatements [ i ] . predicate ,
212
+ null ,
213
+ null ,
214
+ ) ;
229
215
}
230
216
231
- return this . addStatements ( replacement ) ;
217
+ return this . addQuads ( replacement ) ;
232
218
}
233
219
234
220
public replaceMatches ( statements : Quadruple [ ] ) : Quad [ ] {
235
221
for ( let i = 0 ; i < statements . length ; i ++ ) {
236
- this . removeStatements ( this . match (
222
+ this . removeQuads ( this . match (
237
223
statements [ i ] [ 0 ] ,
238
224
statements [ i ] [ 1 ] ,
239
225
undefined ,
240
226
undefined ,
241
227
) ) ;
242
228
}
243
229
244
- return this . addQuads ( statements ) ;
230
+ return this . addQuadruples ( statements ) ;
245
231
}
246
232
247
233
public getResourcePropertyRaw ( subject : SomeNode , property : SomeNode | SomeNode [ ] ) : Quad [ ] {
@@ -297,10 +283,11 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
297
283
* @param subject The identifier of the resource.
298
284
*/
299
285
public statementsFor ( subject : SomeNode ) : Quad [ ] {
300
- const canon = rdfFactory . id ( this . store . canon ( subject ) ) ;
286
+ // TODO: Use the schema to replace canon
287
+ const id = rdfFactory . id ( subject ) ;
301
288
302
- return typeof this . store . subjectIndex [ canon ] !== "undefined"
303
- ? this . store . subjectIndex [ canon ]
289
+ return typeof this . store . indices [ QuadPosition . subject ] [ id ] !== "undefined"
290
+ ? this . store . indices [ QuadPosition . subject ] [ id ]
304
291
: EMPTY_ST_ARR as Quad [ ] ;
305
292
}
306
293
@@ -317,22 +304,17 @@ export class RDFStore implements ChangeBuffer, DeltaProcessor {
317
304
/**
318
305
* Builds a cache of types per resource. Can be omitted when compiled against a well known service.
319
306
*/
320
- private processTypeStatement ( _formula : Store | undefined ,
321
- subj : SomeNode ,
322
- _pred : NamedNode ,
323
- obj ?: Term ,
324
- _why ?: Node ) : boolean {
325
- const subjId = rdfFactory . id ( subj ) ;
326
- if ( ! Array . isArray ( this . typeCache [ subjId ] ) ) {
327
- this . typeCache [ subjId ] = [ obj as NamedNode ] ;
307
+ private processTypeStatement ( quad : Quad ) : boolean {
308
+ if ( ! rdfFactory . equals ( quad . predicate , rdf . type ) ) {
328
309
return false ;
329
310
}
330
- this . typeCache [ subjId ] = this . statementsFor ( ( subj as NamedNode ) )
311
+ const subjId = rdfFactory . id ( quad . subject ) ;
312
+ if ( ! Array . isArray ( this . typeCache [ subjId ] ) ) {
313
+ this . typeCache [ subjId ] = [ ] ;
314
+ }
315
+ this . typeCache [ subjId ] = this . statementsFor ( ( quad . subject as NamedNode ) )
331
316
. filter ( ( s ) => rdfFactory . equals ( s . predicate , rdf . type ) )
332
317
. map ( ( s ) => s . object as NamedNode ) ;
333
- if ( obj ) {
334
- this . typeCache [ subjId ] . push ( ( obj as NamedNode ) ) ;
335
- }
336
318
return false ;
337
319
}
338
320
}
0 commit comments