Skip to content

Commit cbffbbf

Browse files
authored
fix: make sure filter works when encountering unseen named graphs (#498)
1 parent 3f0fb79 commit cbffbbf

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

src/N3Store.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,27 +1134,28 @@ class DatasetCoreAndReadableStream extends Readable {
11341134

11351135
const graphs = n3Store._getGraphs(graph);
11361136
for (const graphKey in graphs) {
1137-
let subjects, predicates, objects;
1138-
1139-
if (!subjectId && predicateId) {
1140-
if (predicates = indexMatch(graphs[graphKey].predicates, [predicateId, objectId, subjectId])) {
1141-
subjects = indexMatch(graphs[graphKey].subjects, [subjectId, predicateId, objectId]);
1142-
objects = indexMatch(graphs[graphKey].objects, [objectId, subjectId, predicateId]);
1137+
let subjects, predicates, objects, content;
1138+
if (content = graphs[graphKey]) {
1139+
if (!subjectId && predicateId) {
1140+
if (predicates = indexMatch(content.predicates, [predicateId, objectId, subjectId])) {
1141+
subjects = indexMatch(content.subjects, [subjectId, predicateId, objectId]);
1142+
objects = indexMatch(content.objects, [objectId, subjectId, predicateId]);
1143+
}
11431144
}
1144-
}
1145-
else if (objectId) {
1146-
if (objects = indexMatch(graphs[graphKey].objects, [objectId, subjectId, predicateId])) {
1147-
subjects = indexMatch(graphs[graphKey].subjects, [subjectId, predicateId, objectId]);
1148-
predicates = indexMatch(graphs[graphKey].predicates, [predicateId, objectId, subjectId]);
1145+
else if (objectId) {
1146+
if (objects = indexMatch(content.objects, [objectId, subjectId, predicateId])) {
1147+
subjects = indexMatch(content.subjects, [subjectId, predicateId, objectId]);
1148+
predicates = indexMatch(content.predicates, [predicateId, objectId, subjectId]);
1149+
}
1150+
}
1151+
else if (subjects = indexMatch(content.subjects, [subjectId, predicateId, objectId])) {
1152+
predicates = indexMatch(content.predicates, [predicateId, objectId, subjectId]);
1153+
objects = indexMatch(content.objects, [objectId, subjectId, predicateId]);
11491154
}
1150-
}
1151-
else if (subjects = indexMatch(graphs[graphKey].subjects, [subjectId, predicateId, objectId])) {
1152-
predicates = indexMatch(graphs[graphKey].predicates, [predicateId, objectId, subjectId]);
1153-
objects = indexMatch(graphs[graphKey].objects, [objectId, subjectId, predicateId]);
1154-
}
11551155

1156-
if (subjects)
1157-
newStore._graphs[graphKey] = { subjects, predicates, objects };
1156+
if (subjects)
1157+
newStore._graphs[graphKey] = { subjects, predicates, objects };
1158+
}
11581159
}
11591160
newStore._size = null;
11601161
}

test/N3Store-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ describe('Store', () => {
150150

151151
it('should have size 5', () => {
152152
expect(store.size).toEqual(5);
153+
154+
expect(store.match(namedNode('s2'), namedNode('p2'), namedNode('o2'), namedNode('g1')).size).toEqual(1);
155+
expect(store.match(null, namedNode('p2'), namedNode('o2'), namedNode('g1')).size).toEqual(1);
156+
expect(store.match(namedNode('s2'), null, namedNode('o2'), namedNode('g1')).size).toEqual(1);
157+
expect(store.match(namedNode('s2'), namedNode('p2'), null, namedNode('g1')).size).toEqual(1);
158+
expect(store.match(namedNode('s2'), namedNode('p2'), namedNode('o2'), null).size).toEqual(1);
159+
160+
expect(store.match(namedNode('s2'), namedNode('p2'), namedNode('o2'), namedNode('g2')).size).toEqual(0);
161+
expect(store.match(null, namedNode('p2'), namedNode('o2'), namedNode('g2')).size).toEqual(0);
162+
expect(store.match(namedNode('s2'), null, namedNode('o2'), namedNode('g2')).size).toEqual(0);
163+
expect(store.match(namedNode('s2'), namedNode('p2'), null, namedNode('g2')).size).toEqual(0);
153164
});
154165

155166
describe('adding a triple that already exists', () => {

0 commit comments

Comments
 (0)