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

Feat: Extract EntityIndex class #289

Closed
jeswr opened this issue Apr 16, 2022 · 4 comments
Closed

Feat: Extract EntityIndex class #289

jeswr opened this issue Apr 16, 2022 · 4 comments

Comments

@jeswr
Copy link
Collaborator

jeswr commented Apr 16, 2022

Would look something like

import { default as N3DataFactory, termToId, termFromId } from './N3DataFactory';

type Term = any;

interface EntityIndex<ID, T extends Term = Term> {
  termToId(term: Term): ID | undefined;
  termToIdSafe(term: Term): ID;
  idToTerm(id: ID): T | undefined;
}

class N3EntityIndex implements EntityIndex<number> {
  private ids: Record<number, Term> = {};
  private entities: Record<Term, number> = {};
  private id = 0;
  private factory;

  termToId(term: Term): number | undefined {
    term = termToId(term);
    return this.entities[termToId(term)];
  }

  termToIdSafe(term: Term): number | undefined {
    term = termToId(term);
    return this.entities[term] || (this.ids[this.entities[++this.id] = term]   = this.id);
  }

  idToTerm(id: number): Term {
    return termFromId(this.ids[id], this.factory);
  }
}

The point of this is to enable multiple sources to share the same EntityIndex for memory/performance reasons.

Pinging @jacoscaz who may have already done something similar with scoping in quadstore.

@rubensworks
Copy link
Member

A big 👍 from me on this.
Would be crucial for things like comunica/comunica#873.

An alternative approach could be to store this id value within the RDF/JS Term, but not sure which approach would be more convenient.

@jeswr
Copy link
Collaborator Author

jeswr commented May 7, 2022

allternative approach could be to store this id value within the RDF/JS Term

My main concern with this approach would be having to handle conflicting ID's if there are multiple sources.

@rubensworks
Copy link
Member

My main concern with this approach would be having to handle conflicting ID's if there are multiple sources.

Indeed, we'll need a way of detecting distinct sources if we'd go this route (not saying we should). (I did something similar to that here: https://rdfostrich.github.io/article-jws2018-ostrich/#dictionary)

@jeswr
Copy link
Collaborator Author

jeswr commented Jul 29, 2024

Implemented in #394

@jeswr jeswr closed this as completed Jul 29, 2024
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

2 participants