forked from Baguette/dodb.cr
DODB.no_index, Storage#new_nilable_index.
The APIs are still a bit experimental at this point, but the feature is very likely to stay.master
parent
800d139a3d
commit
9990a3ac1b
|
@ -76,6 +76,12 @@ class DODB::DataBase(V)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def new_nilable_index(name : String, &block : Proc(V, String | DODB::NoIndex))
|
||||||
|
Index(V).new(self, @directory_name, name, block).tap do |indexer|
|
||||||
|
@indexers << indexer
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def new_tags(name : String, &block : Proc(V, Array(String)))
|
def new_tags(name : String, &block : Proc(V, Array(String)))
|
||||||
Tags(V).new(@directory_name, name, block).tap do |tags|
|
Tags(V).new(@directory_name, name, block).tap do |tags|
|
||||||
@indexers << tags
|
@indexers << tags
|
||||||
|
|
|
@ -6,7 +6,7 @@ require "./indexer.cr"
|
||||||
|
|
||||||
class DODB::Index(V) < DODB::Indexer(V)
|
class DODB::Index(V) < DODB::Indexer(V)
|
||||||
property name : String
|
property name : String
|
||||||
property key_proc : Proc(V, String)
|
property key_proc : Proc(V, String | NoIndex) | Proc(V, String)
|
||||||
getter storage_root : String
|
getter storage_root : String
|
||||||
|
|
||||||
@storage : DODB::DataBase(V)
|
@storage : DODB::DataBase(V)
|
||||||
|
@ -34,6 +34,8 @@ class DODB::Index(V) < DODB::Indexer(V)
|
||||||
def index(key, value)
|
def index(key, value)
|
||||||
index_key = key_proc.call value
|
index_key = key_proc.call value
|
||||||
|
|
||||||
|
return if index_key.is_a? NoIndex
|
||||||
|
|
||||||
symlink = file_path_index index_key
|
symlink = file_path_index index_key
|
||||||
|
|
||||||
Dir.mkdir_p ::File.dirname symlink
|
Dir.mkdir_p ::File.dirname symlink
|
||||||
|
@ -49,6 +51,8 @@ class DODB::Index(V) < DODB::Indexer(V)
|
||||||
def deindex(key, value)
|
def deindex(key, value)
|
||||||
index_key = key_proc.call value
|
index_key = key_proc.call value
|
||||||
|
|
||||||
|
return if index_key.is_a? NoIndex
|
||||||
|
|
||||||
symlink = file_path_index index_key
|
symlink = file_path_index index_key
|
||||||
|
|
||||||
::File.delete symlink
|
::File.delete symlink
|
||||||
|
@ -105,6 +109,9 @@ class DODB::Index(V) < DODB::Indexer(V)
|
||||||
# in case new_value hasn't changed its index
|
# in case new_value hasn't changed its index
|
||||||
def update(new_value : V)
|
def update(new_value : V)
|
||||||
index = key_proc.call new_value
|
index = key_proc.call new_value
|
||||||
|
|
||||||
|
raise Exception.new "new value is not indexable" if index.is_a? NoIndex
|
||||||
|
|
||||||
update index, new_value
|
update index, new_value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
|
||||||
|
class DODB::NoIndex
|
||||||
|
end
|
||||||
|
|
||||||
|
module DODB
|
||||||
|
class_getter no_index = NoIndex.new
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in New Issue