From 14825f4eddd1b6828890cfe3d8eebdc25564ffed Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Thu, 19 Dec 2019 04:22:14 +0100 Subject: [PATCH] Improves #reindex_everything!. --- src/dodb.cr | 9 +++++++++ src/dodb/index.cr | 6 +++--- src/dodb/indexer.cr | 2 ++ src/dodb/partition.cr | 12 ++++++------ src/dodb/tags.cr | 8 ++++---- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/dodb.cr b/src/dodb.cr index db95daa..f299cb5 100644 --- a/src/dodb.cr +++ b/src/dodb.cr @@ -244,6 +244,12 @@ class DODB::DataBase(K, V) Dir.mkdir_p data_path end + private def remove_indexing! + @indexers.each do |indexer| + FileUtils.rm_rf indexer.indexing_directory + end + end + # A very slow operation that removes all indices and then rewrites # them all. # FIXME: Is this really useful in its current form? We should remove the @@ -252,6 +258,9 @@ class DODB::DataBase(K, V) def reindex_everything! old_data = to_h + remove_indexing! + remove_data! + old_data.each do |index, item| self[index] = item end diff --git a/src/dodb/index.cr b/src/dodb/index.cr index 8609f96..50aeb6b 100644 --- a/src/dodb/index.cr +++ b/src/dodb/index.cr @@ -10,7 +10,7 @@ class DODB::Index(V) < DODB::Indexer(V) getter storage_root : String def initialize(@storage_root, @name, @key_proc) - Dir.mkdir_p dir_path_indices + Dir.mkdir_p indexing_directory end def check!(key, value, old_value) @@ -66,12 +66,12 @@ class DODB::Index(V) < DODB::Indexer(V) nil end - private def dir_path_indices + def indexing_directory : String "#{@storage_root}/indices/by_#{@name}" end private def file_path_index(index_key : String) - "#{dir_path_indices}/#{index_key}.json" + "#{indexing_directory}/#{index_key}.json" end private def get_data_symlink_index(key : String) diff --git a/src/dodb/indexer.cr b/src/dodb/indexer.cr index 4a237c7..807cfd9 100644 --- a/src/dodb/indexer.cr +++ b/src/dodb/indexer.cr @@ -4,5 +4,7 @@ abstract class DODB::Indexer(V) abstract def deindex (key : String, value : V) abstract def check! (key : String, value : V, old_value : V?) abstract def name : String + + abstract def indexing_directory : String end diff --git a/src/dodb/partition.cr b/src/dodb/partition.cr index bb92439..c7f84a3 100644 --- a/src/dodb/partition.cr +++ b/src/dodb/partition.cr @@ -9,7 +9,7 @@ class DODB::Partition(V) < DODB::Indexer(V) getter storage_root : String def initialize(@storage_root, @name, @key_proc) - ::Dir.mkdir_p get_partition_directory + ::Dir.mkdir_p indexing_directory end def check!(key, value, old_value) @@ -40,7 +40,7 @@ class DODB::Partition(V) < DODB::Indexer(V) def get(partition) r_value = Array(V).new - partition_directory = get_partition_directory partition + partition_directory = indexing_directory partition Dir.each_child partition_directory do |child| r_value << V.from_json ::File.read "#{partition_directory}/#{child}" end @@ -48,16 +48,16 @@ class DODB::Partition(V) < DODB::Indexer(V) r_value end - private def get_partition_directory + def indexing_directory : String "#{@storage_root}/partitions/by_#{@name}" end - private def get_partition_directory(partition) - "#{get_partition_directory}/#{partition}" + private def indexing_directory(partition) + "#{indexing_directory}/#{partition}" end private def get_partition_symlink(partition : String, key : String) - "#{get_partition_directory partition}/#{key}.json" + "#{indexing_directory partition}/#{key}.json" end private def get_data_symlink(key : String) diff --git a/src/dodb/tags.cr b/src/dodb/tags.cr index ca6ce55..51003eb 100644 --- a/src/dodb/tags.cr +++ b/src/dodb/tags.cr @@ -7,7 +7,7 @@ class DODB::Tags(V) < DODB::Indexer(V) getter storage_root : String def initialize(@storage_root, @name, @key_proc) - ::Dir.mkdir_p get_tag_directory + ::Dir.mkdir_p indexing_directory end def index(key, value) @@ -41,7 +41,7 @@ class DODB::Tags(V) < DODB::Indexer(V) def get_with_indices(key) : Array(Tuple(V, Int32)) r_value = Array(Tuple(V, Int32)).new - partition_directory = "#{get_tag_directory}/#{key}" + partition_directory = "#{indexing_directory}/#{key}" return r_value unless Dir.exists? partition_directory @@ -59,12 +59,12 @@ class DODB::Tags(V) < DODB::Indexer(V) get_with_indices(key).map &.[0] end - private def get_tag_directory + def indexing_directory : String "#{@storage_root}/by_tags/by_#{@name}" end private def get_tagged_entry_path(key : String, index_key : String) - "#{get_tag_directory}/#{index_key}/#{key}.json" + "#{indexing_directory}/#{index_key}/#{key}.json" end private def get_data_symlink(key : String)