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

removeMatches removes quads from Store asynchronously #373

Closed
mrkvon opened this issue Nov 26, 2023 · 4 comments
Closed

removeMatches removes quads from Store asynchronously #373

mrkvon opened this issue Nov 26, 2023 · 4 comments

Comments

@mrkvon
Copy link

mrkvon commented Nov 26, 2023

When we remove quads from Store with removeMatches, they seem to stay in the Store until next tick. I'd expect them to be removed immediately. Also seems to be the case with deleteGraph.

You can see it reproduced in this code sandbox.

Example code

(async () => {
  const store = new n3.Store(); // new empty store

  console.log("number of quads in store:");

  console.log(store.size, "before addition"); // we expect 0 here, get 0

  store.add(
    new n3.Quad(
      new n3.NamedNode("https://subject"),
      new n3.NamedNode("https://predicate"),
      new n3.NamedNode("https://object"),
    ),
  );

  console.log(store.size, "after addition"); // we expect 1 here, get 1

  store.removeMatches(); // remove everything

  console.log(store.size, "synchronously after removal"); // we expect 0 here, get 1 <=== This is the problem

  await new Promise(process.nextTick); // wait for the next tick

  console.log(store.size, "next tick after removal"); // we expect 0 here, get 0
})();
@mrkvon mrkvon changed the title removeMatches removes quads from Store asynchronously removeMatches removes quads from Store asynchronously Nov 26, 2023
@jeswr
Copy link
Collaborator

jeswr commented Nov 26, 2023

Note that this library implements removeMatches as an event emitter; in particular it is implemented as follows

N3.js/src/N3Store.js

Lines 357 to 367 in d3faa57

removeMatches(subject, predicate, object, graph) {
const stream = new Readable({ objectMode: true });
stream._read = () => {
for (const quad of this.readQuads(subject, predicate, object, graph))
stream.push(quad);
stream.push(null);
};
return this.remove(stream);
}
.

So I would not expect the matches to be removed until the end event of the returned event emitter has been called.

Are there docs / types somewhere that indicate this should be synchronous?

@joachimvh
Copy link

It's because N3.js implements removeMatches as described in the RDF/JS Stream spec here.

I think the only sync solution is to call getQuads and then removeQuads with the result.

@mrkvon
Copy link
Author

mrkvon commented Nov 27, 2023

I've seen this comment by @RubenVerborgh:

The N3.Store interface is synchronous [...]

Which i guess is slightly incorrect, then.

I hadn't noticed the links to the external specs, thanks @joachimvh for pointing them out, and for the workaround.

Also, thank you all for promptly resolving this. 🙂

@RubenVerborgh
Copy link
Member

RubenVerborgh commented Nov 27, 2023

Back in 2018 it was correct 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants