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

Don't check for ops in reverseOps before removing it, just try it. #469

Merged
merged 1 commit into from
Mar 6, 2015
Merged
Changes from all commits
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
34 changes: 15 additions & 19 deletions rdflib/plugins/stores/auditable.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ def add(self, triple, context, quoted=False):
context = context.__class__(self.store, context.identifier) if context is not None else None
ctxId = context.identifier if context is not None else None
self.reverseOps.append((s, p, o, ctxId, 'remove'))
if (s, p, o, ctxId, 'add') in self.reverseOps:
self.reverseOps.remove(
(s, p, o, context, 'add'))
try:
self.reverseOps.remove((s, p, o, ctxId, 'add'))
except ValueError:
pass
Copy link
Member

Choose a reason for hiding this comment

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

not so much a comment for this change, but for my understanding: is this to "compress" the reverseOps only?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In add() there was a bug: it checks if (s, p, o, ctxId, 'remove') is in self.reverseOps and if it is found it removes (s, p, o, context, 'add')

Then, it is generally a bad idea to check if an element is in a list before removing it. Just try to remove and deal with the ValueError exception.

if x in l:
    l.remove(x)
else:
    l.add(y)

becomes:

try:
    l.remove(x)
except ValueError:
    l.add(y)

It saves an uneeded call to __contains__

self.store.add((s, p, o), context, quoted)

def remove(self, (subject, predicate, object_), context=None):
Expand All @@ -72,26 +73,21 @@ def remove(self, (subject, predicate, object_), context=None):
if None in [subject, predicate, object_, context]:
if ctxId:
for s, p, o in context.triples((subject, predicate, object_)):
if (s, p, o, ctxId, 'remove') in self.reverseOps:
try:
self.reverseOps.remove((s, p, o, ctxId, 'remove'))
else:
except ValueError:
self.reverseOps.append((s, p, o, ctxId, 'add'))
Copy link
Member

Choose a reason for hiding this comment

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

this somehow seems "asymmetric" to the above add, please re-check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is typically the

if x in l:
    l.remove(x)
else:
    l.add(y)

becomes:

try:
    l.remove(x)
except ValueError:
    l.add(y)

transformation.

else:
for s, p, o, ctx in ConjunctiveGraph(
self.store).quads((subject, predicate, object_)):
if (s, p, o, ctx.identifier, 'remove') in self.reverseOps:
self.reverseOps.remove(
(s, p, o, ctx.identifier, 'remove'))
else:
self.reverseOps.append(
(s, p, o, ctx.identifier, 'add'))

elif (subject, predicate, object_, ctxId, 'add') in self.reverseOps:
self.reverseOps.remove(
(subject, predicate, object_, ctxId, 'add'))
for s, p, o, ctx in ConjunctiveGraph(self.store).quads((subject, predicate, object_)):
try:
self.reverseOps.remove((s, p, o, ctx.identifier, 'remove'))
except ValueError:
self.reverseOps.append((s, p, o, ctx.identifier, 'add'))
else:
self.reverseOps.append(
(subject, predicate, object_, ctxId, 'add'))
try:
self.reverseOps.remove((subject, predicate, object_, ctxId, 'add'))
except ValueError:
self.reverseOps.append((subject, predicate, object_, ctxId, 'add'))
self.store.remove((subject, predicate, object_), context)

def triples(self, triple, context=None):
Expand Down