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

Speedup __add_triple_context. #1271

Merged
merged 3 commits into from
Mar 9, 2021
Merged
Changes from 1 commit
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
23 changes: 13 additions & 10 deletions rdflib/plugins/stores/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,20 +444,24 @@ def __add_triple_context(self, triple, context, quoted):
subj, pred, obj = triple
_ = self.__spo[subj][pred][obj]
# we know the triple exists somewhere in the store
if triple not in self.__tripleContexts:
try:
triple_context = self.__tripleContexts[triple]
except IndexError:
# triple exists with default ctx info
# start with a copy of the default ctx info
self.__tripleContexts[triple] = self.__defaultContexts.copy()
triple_context = self.__tripleContexts[triple] = self.__defaultContexts.copy()

triple_context[ctx] = quoted

self.__tripleContexts[triple][ctx] = quoted
if not quoted:
self.__tripleContexts[triple][None] = quoted
triple_context[None] = quoted

except KeyError:
# the triple didn't exist before in the store
if quoted: # this context only
self.__tripleContexts[triple] = {ctx: quoted}
triple_context = self.__tripleContexts[triple] = {ctx: quoted}
else: # default context as well
self.__tripleContexts[triple] = {ctx: quoted, None: quoted}
triple_context = self.__tripleContexts[triple] = {ctx: quoted, None: quoted}

# if the triple is not quoted add it to the default context
if not quoted:
Expand All @@ -470,11 +474,10 @@ def __add_triple_context(self, triple, context, quoted):

# if this is the first ever triple in the store, set default ctx info
if self.__defaultContexts is None:
self.__defaultContexts = self.__tripleContexts[triple]

self.__defaultContexts = triple_context
# if the context info is the same as default, no need to store it
if self.__tripleContexts[triple] == self.__defaultContexts:
del self.__tripleContexts[triple]
if triple_context == self.__defaultContexts:
del triple_context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this delete the context from self.__tripleContexts? It appears to me, that del triple_context just does nothing.

Copy link
Contributor Author

@rchateauneu rchateauneu Mar 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it strongly seems you are right.

>>> x={"a","b"}
>>> y=x
>>> del y``
>>> x``
set(['a', 'b'])``
>>> y``
Traceback (most recent call last):``
File "<stdin>", line 1, in <module>``
NameError: name 'y' is not defined``

It should probably be:

del self.__tripleContexts[triple]

The nasty thing is that it does not appear in the results.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did the fix in eb5e025

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right, thanks @white-gecko


def __get_context_for_triple(self, triple, skipQuoted=False):
"""return a list of contexts (str) for the triple, skipping
Expand Down