Skip to content

Commit

Permalink
perf: make sure performance improvements apply to datasetcore (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr authored Sep 27, 2024
1 parent 373e90e commit 35d7142
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/N3Store.js
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,9 @@ export default class N3Store {
* Blank Nodes will be normalized.
*/
addAll(quads) {
if (quads instanceof DatasetCoreAndReadableStream)
quads = quads.filtered;

if (Array.isArray(quads))
this.addQuads(quads);
else if (quads instanceof N3Store && quads._entityIndex === this._entityIndex) {
Expand All @@ -883,6 +886,9 @@ export default class N3Store {
* Blank Nodes will be normalized.
*/
contains(other) {
if (other instanceof DatasetCoreAndReadableStream)
other = other.filtered;

if (other === this)
return true;

Expand Down Expand Up @@ -927,6 +933,9 @@ export default class N3Store {
* Returns a new dataset that contains all quads from the current dataset that are not included in the given dataset.
*/
difference(other) {
if (other && other instanceof DatasetCoreAndReadableStream)
other = other.filtered;

if (other === this)
return new N3Store({ entityIndex: this._entityIndex });

Expand All @@ -949,6 +958,9 @@ export default class N3Store {
* Blank Nodes will be normalized.
*/
equals(other) {
if (other instanceof DatasetCoreAndReadableStream)
other = other.filtered;

return other === this || (this.size === other.size && this.contains(other));
}

Expand All @@ -969,6 +981,9 @@ export default class N3Store {
* Returns a new dataset containing all quads from the current dataset that are also included in the given dataset.
*/
intersection(other) {
if (other instanceof DatasetCoreAndReadableStream)
other = other.filtered;

if (other === this) {
const store = new N3Store({ entityIndex: this._entityIndex });
store._graphs = merge(Object.create(null), this._graphs);
Expand Down
17 changes: 17 additions & 0 deletions test/N3Store-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,23 @@ describe('Store', () => {
expect(empty.some(quad => true)).toBe(false);
});
});

describe('#every', () => {
it('should return true if every quad passes the test', () => {
expect(store1.every(quad => quad.subject.value === 's1')).toBe(true);
expect(store1.every(quad => quad.subject.value === 's2')).toBe(false);
expect(store1.every(quad => quad.object.value === 'o1')).toBe(false);
expect(store1.every(quad => quad.subject.termType === 'NamedNode')).toBe(true);
});

it('should return false if no quad passes the test', () => {
expect(store1.every(quad => quad.subject.value === 's2')).toBe(false);
});

it('should return true on the empty set', () => {
expect(empty.every(quad => true)).toBe(true);
});
});
});
});

Expand Down

0 comments on commit 35d7142

Please sign in to comment.