From 10b00611335dd392a6950991de12f6c785f7f58f Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Tue, 11 Feb 2020 16:53:06 +0100 Subject: [PATCH] Partition#delete(partition_index, &matcher) --- src/dodb.cr | 2 +- src/dodb/partition.cr | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/dodb.cr b/src/dodb.cr index 8ec346e..28fad7d 100644 --- a/src/dodb.cr +++ b/src/dodb.cr @@ -46,7 +46,7 @@ class DODB::DataBase(V) ## # name is the name that will be used on the file system. def new_partition(name : String, &block : Proc(V, String)) - Partition(V).new(@directory_name, name, block).tap do |table| + Partition(V).new(self, @directory_name, name, block).tap do |table| @indexers << table end end diff --git a/src/dodb/partition.cr b/src/dodb/partition.cr index c7f84a3..46bbbac 100644 --- a/src/dodb/partition.cr +++ b/src/dodb/partition.cr @@ -8,7 +8,9 @@ class DODB::Partition(V) < DODB::Indexer(V) property key_proc : Proc(V, String) getter storage_root : String - def initialize(@storage_root, @name, @key_proc) + @storage : DODB::DataBase(V) + + def initialize(@storage, @storage_root, @name, @key_proc) ::Dir.mkdir_p indexing_directory end @@ -48,10 +50,34 @@ class DODB::Partition(V) < DODB::Indexer(V) r_value end + def delete(partition, &matcher) + partition_directory = indexing_directory partition + + return unless Dir.exists? partition_directory + + Dir.each_child partition_directory do |child| + path = "#{partition_directory}/#{child}" + item = V.from_json ::File.read path + + if yield item + key = get_key path + + @storage.delete key + end + end + end + def indexing_directory : String "#{@storage_root}/partitions/by_#{@name}" end + private def get_key(path : String) : Int32 + ::File.readlink(path) + .sub(/\.json$/, "") + .sub(/^.*\//, "") + .to_i + end + private def indexing_directory(partition) "#{indexing_directory}/#{partition}" end