From 9990a3ac1b89638c01398900e613987110bc3216 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Wed, 15 Jul 2020 17:19:27 +0200 Subject: [PATCH] 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. --- src/dodb.cr | 6 ++++++ src/dodb/index.cr | 9 ++++++++- src/dodb/no_index.cr | 8 ++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 src/dodb/no_index.cr diff --git a/src/dodb.cr b/src/dodb.cr index 2a34db6..72dabe4 100644 --- a/src/dodb.cr +++ b/src/dodb.cr @@ -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 diff --git a/src/dodb/index.cr b/src/dodb/index.cr index 5c518f3..bb0ac0b 100644 --- a/src/dodb/index.cr +++ b/src/dodb/index.cr @@ -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 diff --git a/src/dodb/no_index.cr b/src/dodb/no_index.cr new file mode 100644 index 0000000..37e7a1c --- /dev/null +++ b/src/dodb/no_index.cr @@ -0,0 +1,8 @@ + +class DODB::NoIndex +end + +module DODB + class_getter no_index = NoIndex.new +end +