Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to prevent literals from being cached #44

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/RdfObjectLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Resource } from './Resource';
export class RdfObjectLoader {
private readonly dataFactory: RDF.DataFactory;
public readonly normalizeLists: boolean;
public readonly uniqueLiterals: boolean;
public readonly context: Promise<void>;
public readonly resources: Record<string, Resource> = {};
public contextResolved!: JsonLdContextNormalized;
Expand All @@ -20,6 +21,7 @@ export class RdfObjectLoader {
public constructor(args?: IRdfClassLoaderArgs) {
this.dataFactory = args?.dataFactory || new DataFactory();
this.normalizeLists = !args || !('normalizeLists' in args) || Boolean(args.normalizeLists);
this.uniqueLiterals = Boolean(args?.uniqueLiterals);

this.context = new ContextParser().parse(args && args.context || {})
.then(contextResolved => {
Expand All @@ -37,6 +39,10 @@ export class RdfObjectLoader {
* @return {Resource} A resource.
*/
public getOrMakeResource(term: RDF.Term): Resource {
if (this.uniqueLiterals && term.termType === 'Literal') {
return new Resource({ term, context: this.contextResolved });
}

const termString: string = termToString(term);
let resource: Resource = this.resources[termString];
if (!resource) {
Expand Down Expand Up @@ -181,7 +187,7 @@ export class RdfObjectLoader {
if (this.normalizeLists) {
for (const listRoot of listMaterializer.getRoots()) {
const listTerms = listMaterializer.getList(listRoot);
this.resources[termToString(listRoot)].list = listTerms!.map(term => this.resources[termToString(term)]);
this.resources[termToString(listRoot)].list = listTerms!.map(term => this.getOrMakeResource(term));
}
}
resolve();
Expand Down Expand Up @@ -221,4 +227,9 @@ export interface IRdfClassLoaderArgs {
* The factory to create RDF terms and quads with.
*/
dataFactory?: RDF.DataFactory;
/**
* If set to true literals will not be cached.
* Defaults to false.
*/
uniqueLiterals?: boolean;
}
44 changes: 44 additions & 0 deletions test/RdfObjectLoader-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,48 @@ describe('RdfObjectLoader', () => {
});
});
});

describe('an instance with unique literals', (): void => {
let loader: RdfObjectLoader;

beforeEach((): void => {
loader = new RdfObjectLoader({ uniqueLiterals: true });
});

it('does not store literals in the resource cache.', async(): Promise<void> => {
await loader.import(streamifyArray([
quad('http://example.org/s', 'http://example.org/p', '"test"'),
]));
const resourceP = loader.getOrMakeResource(DF.namedNode('http://example.org/p'));
const resourceS = loader.getOrMakeResource(DF.namedNode('http://example.org/s'));
expect(loader.resources).toEqual({
'http://example.org/p': resourceP,
'http://example.org/s': resourceS,
});
expect(loader.resources['"test"']).toBeUndefined();
});

it('should normalize a list', async() => {
await loader.import(streamifyArray([
quad('http://example.org/listResource', 'http://example.org/listPredicate', 'http://example.org/l0'),
quad('http://example.org/l0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"A"'),
quad('http://example.org/l0', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://example.org/l1'),
quad('http://example.org/l1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"B"'),
quad('http://example.org/l1', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest', 'http://example.org/l2'),
quad('http://example.org/l2', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#first', '"C"'),
quad('http://example.org/l2',
'http://www.w3.org/1999/02/22-rdf-syntax-ns#rest',
'http://www.w3.org/1999/02/22-rdf-syntax-ns#nil'),
]));
const valueA = loader.getOrMakeResource(DF.literal('A'));
const valueB = loader.getOrMakeResource(DF.literal('B'));
const valueC = loader.getOrMakeResource(DF.literal('C'));
const list = loader.resources['http://example.org/listResource']
.propertiesUri['http://example.org/listPredicate'][0].list;
expect(list?.[0]).not.toBe(valueA);
expect(list?.[0].value).toEqual(valueA.value);
expect(list?.[1].value).toEqual(valueB.value);
expect(list?.[2].value).toEqual(valueC.value);
});
});
});
Loading