diff --git a/src/dodb/index.cr b/src/dodb/index.cr index 54fbdb1..c559532 100644 --- a/src/dodb/index.cr +++ b/src/dodb/index.cr @@ -60,8 +60,14 @@ class DODB::Index(V) < DODB::Indexer(V) end end + # Get the key (ex: 343) for an entry in the DB. + # Without caching, it translates to walk the file-system in `db/indices/by_#{name}/`. + def get_key(index : String) : Int32 + get_key_on_fs index + end + def get(index : String) : V - @storage[get_key_on_fs index] + @storage[get_key index] end def get?(index : String) : V? @@ -73,7 +79,7 @@ class DODB::Index(V) < DODB::Indexer(V) # FIXME: Unlock on exception. def safe_get(index : String) : Nil @storage.request_lock @name, index - internal_key = get_key_on_fs(index).to_s + internal_key = get_key(index).to_s @storage.request_lock internal_key yield get index @@ -97,7 +103,7 @@ class DODB::Index(V) < DODB::Indexer(V) end def get_with_key(index : String) : Tuple(V, Int32) - key = get_key_on_fs index + key = get_key index value = @storage[key] @@ -114,7 +120,7 @@ class DODB::Index(V) < DODB::Indexer(V) end def update(index : String, new_value : V) - key = get_key_on_fs index + key = get_key index @storage[key] = new_value end @@ -126,7 +132,7 @@ class DODB::Index(V) < DODB::Indexer(V) end def delete(index : String) - key = get_key_on_fs index + key = get_key index @storage.delete key end @@ -181,9 +187,10 @@ class DODB::CachedIndex(V) < DODB::Index(V) @data.delete index_key end - def get(index : String) : V - # Get the key from the database representation on the file-system. - key = if k = @data[index]? + # Get the key (ex: 343) for an entry in the DB. + # With caching, the key is probably stored in a hash, or we'll search in the FS. + def get_key(index : String) : Int32 + if k = @data[index]? k elsif k = get_key_on_fs(index) @data[index] = k @@ -191,6 +198,5 @@ class DODB::CachedIndex(V) < DODB::Index(V) else raise MissingEntry.new(@name, index) end - @storage[key] end end