From a141849f2a27d58d4641b299c7a798aab17b7df3 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Wed, 8 May 2024 21:32:21 +0200 Subject: [PATCH] Fix delete functions for tags & partition. --- src/dodb/partition.cr | 11 ++++++----- src/dodb/tags.cr | 35 ++++++++++++++++++++++++----------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/dodb/partition.cr b/src/dodb/partition.cr index 34cd920..cfcafb8 100644 --- a/src/dodb/partition.cr +++ b/src/dodb/partition.cr @@ -203,11 +203,12 @@ class DODB::RAMOnlyPartition(V) < DODB::CachedPartition(V) end def delete(partition, &matcher) - new_partition = @data[partition].select do |key| - item = @storage[key] - ! yield item + if keys = @data[partition]? + new_partition = keys.select do |key| + item = @storage[key] + ! yield item + end + @data[partition] = new_partition end - - @data[partition] = new_partition end end diff --git a/src/dodb/tags.cr b/src/dodb/tags.cr index 6258774..48113fc 100644 --- a/src/dodb/tags.cr +++ b/src/dodb/tags.cr @@ -171,6 +171,18 @@ class DODB::CachedTags(V) < DODB::Tags(V) r_value end + + def delete(tag, &matcher) + # Use `get_with_indexes` to retrieve data on-disk, if necessary. + new_tag = get_with_indexes(tag).map(&.[1]).select do |key| + item = @storage[key] + ! yield item + end + + @data[tag] = new_tag + + super(tag, matcher) + end end # TODO @@ -191,7 +203,6 @@ class DODB::RAMOnlyTags(V) < DODB::CachedTags(V) end def deindex(key, value) - super(key, value) indices = key_proc.call value indices.each do |tag| @@ -209,18 +220,20 @@ class DODB::RAMOnlyTags(V) < DODB::CachedTags(V) keys.each do |data_key| r_value << { @storage[data_key], data_key } end - else - # Get the key from the database representation on the file-system. - tag_directory = indexing_directory tag - raise MissingEntry.new(@name, tag) unless Dir.exists? tag_directory - - Dir.each_child tag_directory do |child| - r_value << { @storage[get_key child], get_key child } - end - - @data[tag] = r_value.map &.[1] end r_value end + + def delete(tag, &matcher) + # Use `get_with_indexes` to retrieve data on-disk, if necessary. + if keys = @data[tag]? + new_tag = keys.select do |key| + item = @storage[key] + ! yield item + end + + @data[tag] = new_tag + end + end end