forked from Baguette/dodb.cr
DODB::Index(V): #update(String, V) and #delete(String) added.
parent
375ef6c465
commit
52753bce14
|
@ -50,7 +50,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_index(name : String, &block : Proc(V, String))
|
def new_index(name : String, &block : Proc(V, String))
|
||||||
Index(V).new(@directory_name, name, block).tap do |indexer|
|
Index(V).new(self, @directory_name, name, block).tap do |indexer|
|
||||||
@indexers << indexer
|
@indexers << indexer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,7 +9,9 @@ class DODB::Index(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
|
||||||
|
|
||||||
|
@ -66,6 +68,37 @@ class DODB::Index(V) < DODB::Indexer(V)
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def get_key(index : String) : Int32
|
||||||
|
file_path = file_path_index index
|
||||||
|
|
||||||
|
raise MissingEntry.new(@name, index) unless ::File.exists? file_path
|
||||||
|
|
||||||
|
::File.readlink(file_path)
|
||||||
|
.sub(/\.json$/, "")
|
||||||
|
.sub(/^.*\//, "")
|
||||||
|
.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
def get_with_key(index : String) : Tuple(V, Int32)
|
||||||
|
key = get_key index
|
||||||
|
|
||||||
|
value = @storage[key]
|
||||||
|
|
||||||
|
{value, key}
|
||||||
|
end
|
||||||
|
|
||||||
|
def update(index : String, new_value : V)
|
||||||
|
_, key = get_with_key index
|
||||||
|
|
||||||
|
@storage[key] = new_value
|
||||||
|
end
|
||||||
|
|
||||||
|
def delete(index : String)
|
||||||
|
key = get_key index
|
||||||
|
|
||||||
|
@storage.delete key
|
||||||
|
end
|
||||||
|
|
||||||
def indexing_directory : String
|
def indexing_directory : String
|
||||||
"#{@storage_root}/indices/by_#{@name}"
|
"#{@storage_root}/indices/by_#{@name}"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue