Partition#delete(partition_index, &matcher)

This commit is contained in:
Luka Vandervelden 2020-02-11 16:53:06 +01:00
parent 355f85d635
commit 10b0061133
2 changed files with 28 additions and 2 deletions

View File

@ -46,7 +46,7 @@ class DODB::DataBase(V)
## ##
# name is the name that will be used on the file system. # name is the name that will be used on the file system.
def new_partition(name : String, &block : Proc(V, String)) 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 @indexers << table
end end
end end

View File

@ -8,7 +8,9 @@ class DODB::Partition(V) < DODB::Indexer(V)
property key_proc : Proc(V, String) property key_proc : Proc(V, String)
getter storage_root : 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 ::Dir.mkdir_p indexing_directory
end end
@ -48,10 +50,34 @@ class DODB::Partition(V) < DODB::Indexer(V)
r_value r_value
end 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 def indexing_directory : String
"#{@storage_root}/partitions/by_#{@name}" "#{@storage_root}/partitions/by_#{@name}"
end end
private def get_key(path : String) : Int32
::File.readlink(path)
.sub(/\.json$/, "")
.sub(/^.*\//, "")
.to_i
end
private def indexing_directory(partition) private def indexing_directory(partition)
"#{indexing_directory}/#{partition}" "#{indexing_directory}/#{partition}"
end end