Iteration and update conflict with set based IOMemory store · Issue #286 · RDFLib/rdflib (original) (raw)
Hi,
When you try to iterate over a Graph and modify another Graph, and both Graphs are backed by the same IOMemory instance, then you might easily run into a problem where the index dictionaries that are used to iterrate, are being modified. (Resulting in a RuntimeError)
here is a little code snippet that demonstrates the issue.
from rdflib.store import Store from rdflib import plugin
from rdflib import Graph, Literal, Namespace
dns = Namespace(u"http://www.example.com/")
store = plugin.get("IOMemory", Store)() g1 = Graph(store=store) g2 = Graph(store=store)
g1.add((dns.Name, dns.prop, Literal(u"test"))) g1.add((dns.Name, dns.prop, Literal(u"test2"))) g1.add((dns.Name, dns.prop, Literal(u"test3")))
for t in g1.triples((None, None, None)): g2.add(t) # next line causes problems because it adds a new Subject that needs # to be indexed in __subjectIndex dictionary in IOMemory Store. # which invalidates the iterator used to iterate over g1 g2.add((dns.Name1, dns.prop, Literal(u"test")))
This can be fixed by using .values() instead of .itervalues() in all graph iterating methods.
The question however is, what the desired behaviour would be. Current behaviour with the RuntimeError is IMO equally valid as the other where you would expect that this code should run cleanly. (I could see this causing some confusion with ConjunctiveGraph as well)
Any Suggestions?