Index: change get_key
to either walk the FS or the cached db index.
This commit is contained in:
parent
bb6d9a26f7
commit
401578c77d
1 changed files with 15 additions and 9 deletions
|
@ -60,8 +60,14 @@ class DODB::Index(V) < DODB::Indexer(V)
|
||||||
end
|
end
|
||||||
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}/<index>`.
|
||||||
|
def get_key(index : String) : Int32
|
||||||
|
get_key_on_fs index
|
||||||
|
end
|
||||||
|
|
||||||
def get(index : String) : V
|
def get(index : String) : V
|
||||||
@storage[get_key_on_fs index]
|
@storage[get_key index]
|
||||||
end
|
end
|
||||||
|
|
||||||
def get?(index : String) : V?
|
def get?(index : String) : V?
|
||||||
|
@ -73,7 +79,7 @@ class DODB::Index(V) < DODB::Indexer(V)
|
||||||
# FIXME: Unlock on exception.
|
# FIXME: Unlock on exception.
|
||||||
def safe_get(index : String) : Nil
|
def safe_get(index : String) : Nil
|
||||||
@storage.request_lock @name, index
|
@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
|
@storage.request_lock internal_key
|
||||||
|
|
||||||
yield get index
|
yield get index
|
||||||
|
@ -97,7 +103,7 @@ class DODB::Index(V) < DODB::Indexer(V)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_with_key(index : String) : Tuple(V, Int32)
|
def get_with_key(index : String) : Tuple(V, Int32)
|
||||||
key = get_key_on_fs index
|
key = get_key index
|
||||||
|
|
||||||
value = @storage[key]
|
value = @storage[key]
|
||||||
|
|
||||||
|
@ -114,7 +120,7 @@ class DODB::Index(V) < DODB::Indexer(V)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(index : String, new_value : V)
|
def update(index : String, new_value : V)
|
||||||
key = get_key_on_fs index
|
key = get_key index
|
||||||
|
|
||||||
@storage[key] = new_value
|
@storage[key] = new_value
|
||||||
end
|
end
|
||||||
|
@ -126,7 +132,7 @@ class DODB::Index(V) < DODB::Indexer(V)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(index : String)
|
def delete(index : String)
|
||||||
key = get_key_on_fs index
|
key = get_key index
|
||||||
|
|
||||||
@storage.delete key
|
@storage.delete key
|
||||||
end
|
end
|
||||||
|
@ -181,9 +187,10 @@ class DODB::CachedIndex(V) < DODB::Index(V)
|
||||||
@data.delete index_key
|
@data.delete index_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(index : String) : V
|
# Get the key (ex: 343) for an entry in the DB.
|
||||||
# Get the key from the database representation on the file-system.
|
# With caching, the key is probably stored in a hash, or we'll search in the FS.
|
||||||
key = if k = @data[index]?
|
def get_key(index : String) : Int32
|
||||||
|
if k = @data[index]?
|
||||||
k
|
k
|
||||||
elsif k = get_key_on_fs(index)
|
elsif k = get_key_on_fs(index)
|
||||||
@data[index] = k
|
@data[index] = k
|
||||||
|
@ -191,6 +198,5 @@ class DODB::CachedIndex(V) < DODB::Index(V)
|
||||||
else
|
else
|
||||||
raise MissingEntry.new(@name, index)
|
raise MissingEntry.new(@name, index)
|
||||||
end
|
end
|
||||||
@storage[key]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue