Skip to content

Commit ecd2732

Browse files
authored
Add option to prevent literals from being cached
1 parent c29233d commit ecd2732

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/RdfObjectLoader.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Resource } from './Resource';
1212
export class RdfObjectLoader {
1313
private readonly dataFactory: RDF.DataFactory;
1414
public readonly normalizeLists: boolean;
15+
public readonly uniqueLiterals: boolean;
1516
public readonly context: Promise<void>;
1617
public readonly resources: Record<string, Resource> = {};
1718
public contextResolved!: JsonLdContextNormalized;
@@ -20,6 +21,7 @@ export class RdfObjectLoader {
2021
public constructor(args?: IRdfClassLoaderArgs) {
2122
this.dataFactory = args?.dataFactory || new DataFactory();
2223
this.normalizeLists = !args || !('normalizeLists' in args) || Boolean(args.normalizeLists);
24+
this.uniqueLiterals = Boolean(args?.uniqueLiterals);
2325

2426
this.context = new ContextParser().parse(args && args.context || {})
2527
.then(contextResolved => {
@@ -37,6 +39,10 @@ export class RdfObjectLoader {
3739
* @return {Resource} A resource.
3840
*/
3941
public getOrMakeResource(term: RDF.Term): Resource {
42+
if (this.uniqueLiterals && term.termType === 'Literal') {
43+
return new Resource({ term, context: this.contextResolved });
44+
}
45+
4046
const termString: string = termToString(term);
4147
let resource: Resource = this.resources[termString];
4248
if (!resource) {
@@ -181,7 +187,7 @@ export class RdfObjectLoader {
181187
if (this.normalizeLists) {
182188
for (const listRoot of listMaterializer.getRoots()) {
183189
const listTerms = listMaterializer.getList(listRoot);
184-
this.resources[termToString(listRoot)].list = listTerms!.map(term => this.resources[termToString(term)]);
190+
this.resources[termToString(listRoot)].list = listTerms!.map(term => this.getOrMakeResource(term));
185191
}
186192
}
187193
resolve();
@@ -221,4 +227,9 @@ export interface IRdfClassLoaderArgs {
221227
* The factory to create RDF terms and quads with.
222228
*/
223229
dataFactory?: RDF.DataFactory;
230+
/**
231+
* If set to true literals will not be cached.
232+
* Defaults to false.
233+
*/
234+
uniqueLiterals?: boolean;
224235
}

test/RdfObjectLoader-test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,48 @@ describe('RdfObjectLoader', () => {
501501
});
502502
});
503503
});
504+
505+
describe('an instance with unique literals', (): void => {
506+
let loader: RdfObjectLoader;
507+
508+
beforeEach((): void => {
509+
loader = new RdfObjectLoader({ uniqueLiterals: true });
510+
});
511+
512+
it('does not store literals in the resource cache.', async(): Promise<void> => {
513+
await loader.import(streamifyArray([
514+
quad('http://example.org/s', 'http://example.org/p', '"test"'),
515+
]));
516+
const resourceP = loader.getOrMakeResource(DF.namedNode('http://example.org/p'));
517+
const resourceS = loader.getOrMakeResource(DF.namedNode('http://example.org/s'));
518+
expect(loader.resources).toEqual({
519+
'http://example.org/p': resourceP,
520+
'http://example.org/s': resourceS,
521+
});
522+
expect(loader.resources['"test"']).toBeUndefined();
523+
});
524+
525+
it('should normalize a list', async() => {
526+
await loader.import(streamifyArray([
527+
quad('http://example.org/listResource', 'http://example.org/listPredicate', 'http://example.org/l0'),
528+
quad('http://example.org/l0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"A"'),
529+
quad('http://example.org/l0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://example.org/l1'),
530+
quad('http://example.org/l1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"B"'),
531+
quad('http://example.org/l1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://example.org/l2'),
532+
quad('http://example.org/l2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"C"'),
533+
quad('http://example.org/l2',
534+
'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest',
535+
'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'),
536+
]));
537+
const valueA = loader.getOrMakeResource(DF.literal('A'));
538+
const valueB = loader.getOrMakeResource(DF.literal('B'));
539+
const valueC = loader.getOrMakeResource(DF.literal('C'));
540+
const list = loader.resources['http://example.org/listResource']
541+
.propertiesUri['http://example.org/listPredicate'][0].list;
542+
expect(list?.[0]).not.toBe(valueA);
543+
expect(list?.[0].value).toEqual(valueA.value);
544+
expect(list?.[1].value).toEqual(valueB.value);
545+
expect(list?.[2].value).toEqual(valueC.value);
546+
});
547+
});
504548
});

0 commit comments

Comments
 (0)