From fd0168d9b048d9d8630e85a72d30945040b4a40a Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Thu, 6 Jan 2011 17:23:02 -0800 Subject: [PATCH] * Fixed bug in MemoryStore where a frozen resource could be returned. --- lib/rdf_context/store/memory_store.rb | 56 ++++++++++++++++++--------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/lib/rdf_context/store/memory_store.rb b/lib/rdf_context/store/memory_store.rb index 1f8c8a9..32bd8c5 100644 --- a/lib/rdf_context/store/memory_store.rb +++ b/lib/rdf_context/store/memory_store.rb @@ -36,22 +36,7 @@ def formula_aware?; true; end # @return [MemoryStore] def initialize(identifier = nil, configuration = {}) super - # indexed by [context][subject][predicate][object] = 1 - @cspo = {} - # indexed by [context][predicate][object][subject] = 1 - @cpos = {} - # indexed by [context][object][subject][predicate] = 1 - @cosp = {} - # indexed by [subject][predicate][object] = [context] - @spo = {} - # indexed by [predicate][object][subject] = [context] - @pos = {} - # indexed by [object][subject][predicate] = [context] - @osp = {} - # indexes integer keys to identifiers - @forward = {} - # reverse index of forward - @reverse = {} + destroy({}) end def inspect @@ -70,6 +55,39 @@ def dump " reverse: #{@reverse.inspect}\n" end + # Destroy store or context + # If context is specified remove that context, otherwise, re-initialize the store + # + # @option configuration [Graph] :context Remove the specified context + def destroy(configuration = {}) + if ctx = configuration[:context] + remove(Triple.new(nil, nil, nil), ctx) + + ci = resource_to_int(ctx) + if ci + @reverse.delete(ctx.hash) + @forward.delete(ci) + end + else + # indexed by [context][subject][predicate][object] = 1 + @cspo = {} + # indexed by [context][predicate][object][subject] = 1 + @cpos = {} + # indexed by [context][object][subject][predicate] = 1 + @cosp = {} + # indexed by [subject][predicate][object] = [context] + @spo = {} + # indexed by [predicate][object][subject] = [context] + @pos = {} + # indexed by [object][subject][predicate] = [context] + @osp = {} + # indexes integer keys to identifiers + @forward = {} + # reverse index of forward + @reverse = {} + end + end + # Add a triple to the store # Add to default context, if context is nil # @@ -145,7 +163,7 @@ def triples(triple, context = nil, &block) # :yields: triple, context osp = @osp else ci = resource_to_int(context) - return [] unless ci + return [] if context.frozen? || !ci spo = @cspo[ci] pos = @cpos[ci] osp = @cosp[ci] @@ -354,6 +372,8 @@ def int_to_resource(i); @forward[i]; end def triple_to_int(triple) [@reverse[triple.subject.hash], @reverse[triple.predicate.hash], @reverse[triple.object.hash]] end - def resource_to_int(resource); @reverse[resource.hash]; end + def resource_to_int(resource); + @reverse[resource.hash] + end end end \ No newline at end of file