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.
This commit is contained in:
Luka Vandervelden 2020-07-15 17:19:27 +02:00
parent 800d139a3d
commit 9990a3ac1b
3 changed files with 22 additions and 1 deletions

View File

@ -76,6 +76,12 @@ class DODB::DataBase(V)
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)))
Tags(V).new(@directory_name, name, block).tap do |tags|
@indexers << tags

View File

@ -6,7 +6,7 @@ require "./indexer.cr"
class DODB::Index(V) < DODB::Indexer(V)
property name : String
property key_proc : Proc(V, String)
property key_proc : Proc(V, String | NoIndex) | Proc(V, String)
getter storage_root : String
@storage : DODB::DataBase(V)
@ -34,6 +34,8 @@ class DODB::Index(V) < DODB::Indexer(V)
def index(key, value)
index_key = key_proc.call value
return if index_key.is_a? NoIndex
symlink = file_path_index index_key
Dir.mkdir_p ::File.dirname symlink
@ -49,6 +51,8 @@ class DODB::Index(V) < DODB::Indexer(V)
def deindex(key, value)
index_key = key_proc.call value
return if index_key.is_a? NoIndex
symlink = file_path_index index_key
::File.delete symlink
@ -105,6 +109,9 @@ class DODB::Index(V) < DODB::Indexer(V)
# in case new_value hasn't changed its index
def update(new_value : V)
index = key_proc.call new_value
raise Exception.new "new value is not indexable" if index.is_a? NoIndex
update index, new_value
end

8
src/dodb/no_index.cr Normal file
View File

@ -0,0 +1,8 @@
class DODB::NoIndex
end
module DODB
class_getter no_index = NoIndex.new
end