From 78e3a402aa60e16df24a38e93b802bf1fe1401ab Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sat, 11 May 2024 14:20:22 +0200 Subject: [PATCH] Enable all indexes (index, partition, tag) to be nilable. --- src/dodb.cr | 36 ++++++++++++++++++++++++++++++++++++ src/dodb/index.cr | 11 +++++------ src/dodb/no_index.cr | 6 ++++++ src/dodb/partition.cr | 15 +++++++++------ src/dodb/tags.cr | 12 +++++++++--- 5 files changed, 65 insertions(+), 15 deletions(-) diff --git a/src/dodb.cr b/src/dodb.cr index aa32c34..27ad9db 100644 --- a/src/dodb.cr +++ b/src/dodb.cr @@ -173,6 +173,24 @@ abstract class DODB::Storage(V) end end + def new_nilable_partition(name : String, &block : Proc(V, String | DODB::NoIndex)) + CachedPartition(V).new(self, @directory_name, name, block).tap do |table| + @indexers << table + end + end + + def new_nilable_uncached_partition(name : String, &block : Proc(V, String | DODB::NoIndex)) + Partition(V).new(self, @directory_name, name, block).tap do |table| + @indexers << table + end + end + + def new_nilable_RAM_partition(name : String, &block : Proc(V, String | DODB::NoIndex)) + RAMOnlyPartition(V).new(self, @directory_name, name, block).tap do |table| + @indexers << table + end + end + def get_partition(table_name : String, partition_name : String) partition = @indexers.find &.name.==(table_name) @@ -201,6 +219,24 @@ abstract class DODB::Storage(V) end end + def new_nilable_tags(name : String, &block : Proc(V, Array(String) | DODB::NoIndex)) + CachedTags(V).new(self, @directory_name, name, block).tap do |tags| + @indexers << tags + end + end + + def new_nilable_uncached_tags(name : String, &block : Proc(V, Array(String) | DODB::NoIndex)) + Tags(V).new(self, @directory_name, name, block).tap do |tags| + @indexers << tags + end + end + + def new_nilable_RAM_tags(name : String, &block : Proc(V, Array(String) | DODB::NoIndex)) + RAMOnlyTags(V).new(self, @directory_name, name, block).tap do |tags| + @indexers << tags + end + end + def get_tags(name, key : String) tag = @indexers.find &.name.==(name) diff --git a/src/dodb/index.cr b/src/dodb/index.cr index cb303dd..ac937b9 100644 --- a/src/dodb/index.cr +++ b/src/dodb/index.cr @@ -17,6 +17,8 @@ class DODB::Index(V) < DODB::Indexer(V) def check!(key, value, old_value) index_key = key_proc.call value + return if index_key.is_a? NoIndex + symlink = file_path_index index_key.to_s if ::File.symlink? symlink @@ -33,7 +35,6 @@ class DODB::Index(V) < DODB::Indexer(V) def index(key, value) index_key = key_proc.call value - return if index_key.is_a? NoIndex symlink = file_path_index index_key @@ -45,7 +46,6 @@ class DODB::Index(V) < DODB::Indexer(V) def deindex(key, value) index_key = key_proc.call value - return if index_key.is_a? NoIndex symlink = file_path_index index_key @@ -151,6 +151,7 @@ class DODB::CachedIndex(V) < DODB::Index(V) def check!(key, value, old_value) index_key = key_proc.call value + return if index_key.is_a? NoIndex if data[index_key]? # In case both old and new values are pointing to the same key, @@ -170,19 +171,17 @@ class DODB::CachedIndex(V) < DODB::Index(V) end def index(key, value) - super(key, value) - index_key = key_proc.call value return if index_key.is_a? NoIndex + super(key, value) @data[index_key] = key.to_i end def deindex(key, value) - super(key, value) - index_key = key_proc.call value return if index_key.is_a? NoIndex + super(key, value) @data.delete index_key end diff --git a/src/dodb/no_index.cr b/src/dodb/no_index.cr index 37e7a1c..7b4a0a7 100644 --- a/src/dodb/no_index.cr +++ b/src/dodb/no_index.cr @@ -1,5 +1,11 @@ class DODB::NoIndex + include JSON::Serializable + + def_clone + + def initialize() + end end module DODB diff --git a/src/dodb/partition.cr b/src/dodb/partition.cr index 6389d8a..3e8c6ae 100644 --- a/src/dodb/partition.cr +++ b/src/dodb/partition.cr @@ -4,7 +4,7 @@ require "./indexer.cr" class DODB::Partition(V) < DODB::Indexer(V) property name : String - property key_proc : Proc(V, String) + property key_proc : Proc(V, String | NoIndex) | Proc(V, String) getter storage_root : String # Required to remove an entry in the DB. @@ -20,19 +20,18 @@ class DODB::Partition(V) < DODB::Indexer(V) def index(key, value) partition = key_proc.call value + return if partition.is_a? NoIndex symlink = get_partition_symlink(partition, key) Dir.mkdir_p ::File.dirname symlink - # FIXME: Should not happen anymore. Should we remove this? - ::File.delete symlink if ::File.exists? symlink - ::File.symlink get_data_symlink(key), symlink end def deindex(key, value) partition = key_proc.call value + return if partition.is_a? NoIndex symlink = get_partition_symlink(partition, key) @@ -111,8 +110,9 @@ class DODB::CachedPartition(V) < DODB::Partition(V) end def index(key, value) - super(key, value) partition = key_proc.call value + return if partition.is_a? NoIndex + super(key, value) array = if v = @data[partition]? v @@ -125,8 +125,9 @@ class DODB::CachedPartition(V) < DODB::Partition(V) end def deindex(key, value) - super(key, value) partition = key_proc.call value + return if partition.is_a? NoIndex + super(key, value) if v = @data[partition]? v.delete key.to_i @@ -178,6 +179,7 @@ end class DODB::RAMOnlyPartition(V) < DODB::CachedPartition(V) def index(key, value) partition = key_proc.call value + return if partition.is_a? NoIndex array = if v = @data[partition]? v @@ -191,6 +193,7 @@ class DODB::RAMOnlyPartition(V) < DODB::CachedPartition(V) def deindex(key, value) partition = key_proc.call value + return if partition.is_a? NoIndex if v = @data[partition]? v.delete key.to_i diff --git a/src/dodb/tags.cr b/src/dodb/tags.cr index 6530983..fb939f7 100644 --- a/src/dodb/tags.cr +++ b/src/dodb/tags.cr @@ -2,7 +2,7 @@ require "file_utils" class DODB::Tags(V) < DODB::Indexer(V) property name : String - property key_proc : Proc(V, Array(String)) + property key_proc : Proc(V, Array(String) | NoIndex) | Proc(V, Array(String)) getter storage_root : String # Required to remove an entry in the DB. @@ -18,6 +18,7 @@ class DODB::Tags(V) < DODB::Indexer(V) def index(key, value) indices = key_proc.call(value) + return if indices.is_a? NoIndex indices.each do |i| symlink = get_tagged_entry_path(i, key) @@ -30,6 +31,7 @@ class DODB::Tags(V) < DODB::Indexer(V) def deindex(key, value) indices = key_proc.call(value) + return if indices.is_a? NoIndex indices.each do |i| symlink = get_tagged_entry_path(i, key) @@ -131,8 +133,9 @@ class DODB::CachedTags(V) < DODB::Tags(V) property data = Hash(String, Array(Int32)).new def index(key, value) - super(key, value) indices = key_proc.call value + return if indices.is_a? NoIndex + super(key, value) indices.each do |tag| array = if v = @data[tag]? @@ -147,8 +150,9 @@ class DODB::CachedTags(V) < DODB::Tags(V) end def deindex(key, value) - super(key, value) indices = key_proc.call value + return if indices.is_a? NoIndex + super(key, value) indices.each do |tag| if v = @data[tag]? @@ -204,6 +208,7 @@ end class DODB::RAMOnlyTags(V) < DODB::CachedTags(V) def index(key, value) indices = key_proc.call value + return if indices.is_a? NoIndex indices.each do |tag| array = if v = @data[tag]? @@ -219,6 +224,7 @@ class DODB::RAMOnlyTags(V) < DODB::CachedTags(V) def deindex(key, value) indices = key_proc.call value + return if indices.is_a? NoIndex indices.each do |tag| if v = @data[tag]?