@@ -4,7 +4,7 @@ import { getLoggerFor } from '../../logging/LogUtil';
4
4
import { InternalServerError } from '../../util/errors/InternalServerError' ;
5
5
import type { ContentType } from '../../util/HeaderUtil' ;
6
6
import { parseContentType } from '../../util/HeaderUtil' ;
7
- import { toNamedTerm , toObjectTerm , toCachedNamedNode , isTerm , toLiteral } from '../../util/TermUtil' ;
7
+ import { toNamedTerm , toObjectTerm , isTerm , toLiteral } from '../../util/TermUtil' ;
8
8
import { CONTENT_TYPE_TERM , CONTENT_LENGTH_TERM , XSD , SOLID_META , RDFS } from '../../util/Vocabularies' ;
9
9
import type { ResourceIdentifier } from './ResourceIdentifier' ;
10
10
import { isResourceIdentifier } from './ResourceIdentifier' ;
@@ -21,6 +21,22 @@ export function isRepresentationMetadata(object: any): object is RepresentationM
21
21
return typeof object ?. setMetadata === 'function' ;
22
22
}
23
23
24
+ // Caches named node conversions
25
+ const cachedNamedNodes : Record < string , NamedNode > = { } ;
26
+
27
+ /**
28
+ * Converts the incoming name (URI or shorthand) to a named node.
29
+ * The generated terms get cached to reduce the number of created nodes,
30
+ * so only use this for internal constants!
31
+ * @param name - Predicate to potentially transform.
32
+ */
33
+ function toCachedNamedNode ( name : string ) : NamedNode {
34
+ if ( ! ( name in cachedNamedNodes ) ) {
35
+ cachedNamedNodes [ name ] = DataFactory . namedNode ( name ) ;
36
+ }
37
+ return cachedNamedNodes [ name ] ;
38
+ }
39
+
24
40
/**
25
41
* Stores the metadata triples and provides methods for easy access.
26
42
* Most functions return the metadata object to allow for chaining.
@@ -116,7 +132,7 @@ export class RepresentationMetadata {
116
132
*/
117
133
public quads (
118
134
subject : NamedNode | BlankNode | string | null = null ,
119
- predicate : NamedNode | string | null = null ,
135
+ predicate : NamedNode | null = null ,
120
136
object : NamedNode | BlankNode | Literal | string | null = null ,
121
137
graph : MetadataGraph | null = null ,
122
138
) : Quad [ ] {
@@ -167,12 +183,12 @@ export class RepresentationMetadata {
167
183
*/
168
184
public addQuad (
169
185
subject : NamedNode | BlankNode | string ,
170
- predicate : NamedNode | string ,
186
+ predicate : NamedNode ,
171
187
object : NamedNode | BlankNode | Literal | string ,
172
188
graph ?: MetadataGraph ,
173
189
) : this {
174
190
this . store . addQuad ( toNamedTerm ( subject ) ,
175
- toCachedNamedNode ( predicate ) ,
191
+ predicate ,
176
192
toObjectTerm ( object , true ) ,
177
193
graph ? toNamedTerm ( graph ) : undefined ) ;
178
194
return this ;
@@ -194,12 +210,12 @@ export class RepresentationMetadata {
194
210
*/
195
211
public removeQuad (
196
212
subject : NamedNode | BlankNode | string ,
197
- predicate : NamedNode | string ,
213
+ predicate : NamedNode ,
198
214
object : NamedNode | BlankNode | Literal | string ,
199
215
graph ?: MetadataGraph ,
200
216
) : this {
201
217
const quads = this . quads ( toNamedTerm ( subject ) ,
202
- toCachedNamedNode ( predicate ) ,
218
+ predicate ,
203
219
toObjectTerm ( object , true ) ,
204
220
graph ? toNamedTerm ( graph ) : undefined ) ;
205
221
return this . removeQuads ( quads ) ;
@@ -219,7 +235,7 @@ export class RepresentationMetadata {
219
235
* @param object - Value(s) to add.
220
236
* @param graph - Optional graph of where to add the values to.
221
237
*/
222
- public add ( predicate : NamedNode | string , object : MetadataValue , graph ?: MetadataGraph ) : this {
238
+ public add ( predicate : NamedNode , object : MetadataValue , graph ?: MetadataGraph ) : this {
223
239
return this . forQuads ( predicate , object , ( pred , obj ) : any => this . addQuad ( this . id , pred , obj , graph ) ) ;
224
240
}
225
241
@@ -229,20 +245,19 @@ export class RepresentationMetadata {
229
245
* @param object - Value(s) to remove.
230
246
* @param graph - Optional graph of where to remove the values from.
231
247
*/
232
- public remove ( predicate : NamedNode | string , object : MetadataValue , graph ?: MetadataGraph ) : this {
248
+ public remove ( predicate : NamedNode , object : MetadataValue , graph ?: MetadataGraph ) : this {
233
249
return this . forQuads ( predicate , object , ( pred , obj ) : any => this . removeQuad ( this . id , pred , obj , graph ) ) ;
234
250
}
235
251
236
252
/**
237
253
* Helper function to simplify add/remove
238
254
* Runs the given function on all predicate/object pairs, but only converts the predicate to a named node once.
239
255
*/
240
- private forQuads ( predicate : NamedNode | string , object : MetadataValue ,
256
+ private forQuads ( predicate : NamedNode , object : MetadataValue ,
241
257
forFn : ( pred : NamedNode , obj : NamedNode | Literal ) => void ) : this {
242
- const predicateNode = toCachedNamedNode ( predicate ) ;
243
258
const objects = Array . isArray ( object ) ? object : [ object ] ;
244
259
for ( const obj of objects ) {
245
- forFn ( predicateNode , toObjectTerm ( obj , true ) ) ;
260
+ forFn ( predicate , toObjectTerm ( obj , true ) ) ;
246
261
}
247
262
return this ;
248
263
}
@@ -252,8 +267,8 @@ export class RepresentationMetadata {
252
267
* @param predicate - Predicate to remove.
253
268
* @param graph - Optional graph where to remove from.
254
269
*/
255
- public removeAll ( predicate : NamedNode | string , graph ?: MetadataGraph ) : this {
256
- this . removeQuads ( this . store . getQuads ( this . id , toCachedNamedNode ( predicate ) , null , graph ?? null ) ) ;
270
+ public removeAll ( predicate : NamedNode , graph ?: MetadataGraph ) : this {
271
+ this . removeQuads ( this . store . getQuads ( this . id , predicate , null , graph ?? null ) ) ;
257
272
return this ;
258
273
}
259
274
@@ -278,8 +293,8 @@ export class RepresentationMetadata {
278
293
*
279
294
* @returns An array with all matches.
280
295
*/
281
- public getAll ( predicate : NamedNode | string , graph ?: MetadataGraph ) : Term [ ] {
282
- return this . store . getQuads ( this . id , toCachedNamedNode ( predicate ) , null , graph ?? null )
296
+ public getAll ( predicate : NamedNode , graph ?: MetadataGraph ) : Term [ ] {
297
+ return this . store . getQuads ( this . id , predicate , null , graph ?? null )
283
298
. map ( ( quad ) : Term => quad . object ) ;
284
299
}
285
300
@@ -292,15 +307,15 @@ export class RepresentationMetadata {
292
307
*
293
308
* @returns The corresponding value. Undefined if there is no match
294
309
*/
295
- public get ( predicate : NamedNode | string , graph ?: MetadataGraph ) : Term | undefined {
310
+ public get ( predicate : NamedNode , graph ?: MetadataGraph ) : Term | undefined {
296
311
const terms = this . getAll ( predicate , graph ) ;
297
312
if ( terms . length === 0 ) {
298
313
return ;
299
314
}
300
315
if ( terms . length > 1 ) {
301
- this . logger . error ( `Multiple results for ${ typeof predicate === 'string' ? predicate : predicate . value } ` ) ;
316
+ this . logger . error ( `Multiple results for ${ predicate . value } ` ) ;
302
317
throw new InternalServerError (
303
- `Multiple results for ${ typeof predicate === 'string' ? predicate : predicate . value } ` ,
318
+ `Multiple results for ${ predicate . value } ` ,
304
319
) ;
305
320
}
306
321
return terms [ 0 ] ;
@@ -313,7 +328,7 @@ export class RepresentationMetadata {
313
328
* @param object - Value(s) to set.
314
329
* @param graph - Optional graph where the triple should be stored.
315
330
*/
316
- public set ( predicate : NamedNode | string , object ?: MetadataValue , graph ?: MetadataGraph ) : this {
331
+ public set ( predicate : NamedNode , object ?: MetadataValue , graph ?: MetadataGraph ) : this {
317
332
this . removeAll ( predicate , graph ) ;
318
333
if ( object ) {
319
334
this . add ( predicate , object , graph ) ;
0 commit comments