2019-12-11 18:10:09 +01:00
|
|
|
|
require "file_utils"
|
|
|
|
|
require "json"
|
|
|
|
|
|
2019-12-11 22:08:26 +01:00
|
|
|
|
require "./exceptions.cr"
|
2019-12-11 18:10:09 +01:00
|
|
|
|
require "./indexer.cr"
|
|
|
|
|
|
2019-12-11 23:18:20 +01:00
|
|
|
|
class DODB::Index(V) < DODB::Indexer(V)
|
2019-12-11 18:10:09 +01:00
|
|
|
|
property name : String
|
2020-07-15 17:19:27 +02:00
|
|
|
|
property key_proc : Proc(V, String | NoIndex) | Proc(V, String)
|
2019-12-11 18:10:09 +01:00
|
|
|
|
getter storage_root : String
|
|
|
|
|
|
2020-07-21 12:49:32 +02:00
|
|
|
|
@storage : DODB::Storage(V)
|
2020-01-03 09:36:41 +01:00
|
|
|
|
|
|
|
|
|
def initialize(@storage, @storage_root, @name, @key_proc)
|
2019-12-19 04:22:14 +01:00
|
|
|
|
Dir.mkdir_p indexing_directory
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check!(key, value, old_value)
|
|
|
|
|
index_key = key_proc.call value
|
|
|
|
|
|
|
|
|
|
symlink = file_path_index index_key.to_s
|
|
|
|
|
|
|
|
|
|
# FIXME: Check it’s not pointing to “old_value”, if any, before raising.
|
|
|
|
|
if ::File.exists? symlink
|
|
|
|
|
if old_value
|
|
|
|
|
old_key = key_proc.call old_value
|
|
|
|
|
return if symlink == file_path_index old_key.to_s
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-10 11:15:09 +01:00
|
|
|
|
raise IndexOverload.new "index '#{@name}' is overloaded for key '#{key}'"
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def index(key, value)
|
|
|
|
|
index_key = key_proc.call value
|
|
|
|
|
|
2020-07-15 17:19:27 +02:00
|
|
|
|
return if index_key.is_a? NoIndex
|
|
|
|
|
|
2019-12-11 18:10:09 +01:00
|
|
|
|
symlink = file_path_index index_key
|
|
|
|
|
|
|
|
|
|
Dir.mkdir_p ::File.dirname symlink
|
|
|
|
|
|
|
|
|
|
# FIXME: Now that this is done in check!, can we remove it?
|
|
|
|
|
if ::File.exists? symlink
|
|
|
|
|
raise Exception.new "symlink already exists: #{symlink}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
::File.symlink get_data_symlink_index(key), symlink
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def deindex(key, value)
|
|
|
|
|
index_key = key_proc.call value
|
|
|
|
|
|
2020-07-15 17:19:27 +02:00
|
|
|
|
return if index_key.is_a? NoIndex
|
|
|
|
|
|
2019-12-11 18:10:09 +01:00
|
|
|
|
symlink = file_path_index index_key
|
|
|
|
|
|
|
|
|
|
::File.delete symlink
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-11 22:08:26 +01:00
|
|
|
|
def get(index : String) : V
|
|
|
|
|
file_path = file_path_index index
|
|
|
|
|
|
|
|
|
|
raise MissingEntry.new(@name, index) unless ::File.exists? file_path
|
|
|
|
|
|
|
|
|
|
V.from_json ::File.read file_path
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get?(index : String) : V?
|
|
|
|
|
get index
|
|
|
|
|
rescue MissingEntry
|
|
|
|
|
nil
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
|
2020-07-20 15:04:17 +02:00
|
|
|
|
# FIXME: Unlock on exception.
|
2020-06-24 21:45:45 +02:00
|
|
|
|
def safe_get(index : String) : Nil
|
2020-07-20 15:04:17 +02:00
|
|
|
|
@storage.request_lock @name, index
|
2020-06-24 21:45:45 +02:00
|
|
|
|
internal_key = get_key(index).to_s
|
|
|
|
|
@storage.request_lock internal_key
|
|
|
|
|
|
|
|
|
|
yield get index
|
|
|
|
|
|
|
|
|
|
@storage.release_lock internal_key
|
2020-07-20 15:04:17 +02:00
|
|
|
|
@storage.release_lock @name, index
|
2020-06-24 21:45:45 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def safe_get?(index : String, &block : Proc(V | Nil, Nil)) : Nil
|
|
|
|
|
safe_get index, &block
|
|
|
|
|
rescue MissingEntry
|
|
|
|
|
yield nil
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-03 09:36:41 +01:00
|
|
|
|
def get_key(index : String) : Int32
|
|
|
|
|
file_path = file_path_index index
|
|
|
|
|
|
|
|
|
|
raise MissingEntry.new(@name, index) unless ::File.exists? file_path
|
|
|
|
|
|
|
|
|
|
::File.readlink(file_path)
|
|
|
|
|
.sub(/\.json$/, "")
|
|
|
|
|
.sub(/^.*\//, "")
|
|
|
|
|
.to_i
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_with_key(index : String) : Tuple(V, Int32)
|
|
|
|
|
key = get_key index
|
|
|
|
|
|
|
|
|
|
value = @storage[key]
|
|
|
|
|
|
|
|
|
|
{value, key}
|
|
|
|
|
end
|
|
|
|
|
|
2020-06-24 21:03:01 +02:00
|
|
|
|
# in case new_value hasn't changed its index
|
2020-04-19 18:18:53 +02:00
|
|
|
|
def update(new_value : V)
|
|
|
|
|
index = key_proc.call new_value
|
2020-07-15 17:19:27 +02:00
|
|
|
|
|
|
|
|
|
raise Exception.new "new value is not indexable" if index.is_a? NoIndex
|
|
|
|
|
|
2020-04-19 18:18:53 +02:00
|
|
|
|
update index, new_value
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-03 09:36:41 +01:00
|
|
|
|
def update(index : String, new_value : V)
|
|
|
|
|
_, key = get_with_key index
|
|
|
|
|
|
|
|
|
|
@storage[key] = new_value
|
|
|
|
|
end
|
|
|
|
|
|
2020-02-02 14:18:03 +01:00
|
|
|
|
def update_or_create(index : String, new_value : V)
|
|
|
|
|
update index, new_value
|
|
|
|
|
rescue MissingEntry
|
|
|
|
|
@storage << new_value
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-03 09:36:41 +01:00
|
|
|
|
def delete(index : String)
|
|
|
|
|
key = get_key index
|
|
|
|
|
|
|
|
|
|
@storage.delete key
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-19 04:22:14 +01:00
|
|
|
|
def indexing_directory : String
|
2019-12-11 18:10:09 +01:00
|
|
|
|
"#{@storage_root}/indices/by_#{@name}"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private def file_path_index(index_key : String)
|
2019-12-19 04:22:14 +01:00
|
|
|
|
"#{indexing_directory}/#{index_key}.json"
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private def get_data_symlink_index(key : String)
|
|
|
|
|
"../../data/#{key}.json"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|