2019-12-11 18:10:09 +01:00
|
|
|
|
require "file_utils"
|
|
|
|
|
require "json"
|
|
|
|
|
|
2019-12-11 23:18:20 +01:00
|
|
|
|
class DODB::Tags(V) < DODB::Indexer(V)
|
2019-12-11 18:10:09 +01:00
|
|
|
|
property name : String
|
|
|
|
|
property key_proc : Proc(V, Array(String))
|
|
|
|
|
getter storage_root : String
|
|
|
|
|
|
|
|
|
|
def initialize(@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
|
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
# FIXME: Too slow. That `uniq` call alone is a perf eater.
|
|
|
|
|
def tag_combinations(tags)
|
|
|
|
|
combinations = [] of Array(String)
|
|
|
|
|
|
|
|
|
|
previous_tags = [] of String
|
|
|
|
|
while tag = tags.shift?
|
|
|
|
|
previous_tags.push tag
|
|
|
|
|
|
|
|
|
|
combinations.push previous_tags.clone
|
|
|
|
|
|
|
|
|
|
if tags.size > 0
|
|
|
|
|
combinations.push tags.clone
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
combinations.uniq
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-11 18:10:09 +01:00
|
|
|
|
def index(key, value)
|
2020-01-29 16:59:39 +01:00
|
|
|
|
indices = key_proc.call(value).sort
|
2019-12-11 18:10:09 +01:00
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
tag_combinations(indices).each do |previous_indices|
|
|
|
|
|
# FIXME: Not on `index`, but on the list of all previous indices.
|
|
|
|
|
symdir = symlinks_directory previous_indices
|
|
|
|
|
otdir = other_tags_directory previous_indices
|
2019-12-11 18:10:09 +01:00
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
::Dir.mkdir_p symdir
|
|
|
|
|
::Dir.mkdir_p otdir
|
|
|
|
|
|
|
|
|
|
symlink = get_tagged_entry_path(key, previous_indices)
|
2019-12-11 18:10:09 +01:00
|
|
|
|
|
|
|
|
|
::File.delete symlink if ::File.exists? symlink
|
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
::File.symlink get_data_symlink(key, previous_indices), symlink
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def deindex(key, value)
|
2020-01-29 16:59:39 +01:00
|
|
|
|
indices = key_proc.call(value).sort
|
|
|
|
|
|
|
|
|
|
tag_combinations(indices).each do |previous_indices|
|
|
|
|
|
# FIXME: Not on `index`, but on the list of all previous indices.
|
|
|
|
|
symdir = symlinks_directory previous_indices
|
|
|
|
|
otdir = other_tags_directory previous_indices
|
2019-12-11 18:10:09 +01:00
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
::Dir.mkdir_p symdir
|
|
|
|
|
::Dir.mkdir_p otdir
|
2019-12-11 18:10:09 +01:00
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
symlink = get_tagged_entry_path(key, previous_indices)
|
|
|
|
|
|
|
|
|
|
::File.delete symlink if ::File.exists? symlink
|
|
|
|
|
|
|
|
|
|
# FIXME: Remove directories if empty?
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def check!(key, value, old_value)
|
|
|
|
|
return true # Tags don’t have collisions or overloads.
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
def get_with_indices(key : String) : Array(Tuple(V, Int32))
|
|
|
|
|
get_with_indices [key]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def get_with_indices(keys : Array(String)) : Array(Tuple(V, Int32))
|
2019-12-18 03:43:09 +01:00
|
|
|
|
r_value = Array(Tuple(V, Int32)).new
|
2019-12-11 18:10:09 +01:00
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
partition_directory = symlinks_directory keys
|
2019-12-11 18:10:09 +01:00
|
|
|
|
|
|
|
|
|
return r_value unless Dir.exists? partition_directory
|
|
|
|
|
|
|
|
|
|
Dir.each_child partition_directory do |child|
|
2019-12-18 03:43:09 +01:00
|
|
|
|
r_value << {
|
|
|
|
|
V.from_json(::File.read("#{partition_directory}/#{child}")),
|
|
|
|
|
File.basename(child).gsub(/\.json$/, "").to_i
|
|
|
|
|
}
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
r_value
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
def get(key : String) : Array(V)
|
2019-12-18 03:43:09 +01:00
|
|
|
|
get_with_indices(key).map &.[0]
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
def get(keys : Array(String)) : Array(V)
|
|
|
|
|
get_with_indices(keys.sort).map &.[0]
|
|
|
|
|
end
|
|
|
|
|
|
2019-12-19 04:22:14 +01:00
|
|
|
|
def indexing_directory : String
|
2020-01-29 16:59:39 +01:00
|
|
|
|
"#{@storage_root}/tags/by_#{@name}"
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
private def symlinks_directory(previous_indices : Array(String))
|
|
|
|
|
"#{indexing_directory}#{previous_indices.map { |i| "/other-tags/#{i}" }.join}/data"
|
|
|
|
|
end
|
|
|
|
|
private def other_tags_directory(previous_indices : Array(String))
|
|
|
|
|
"#{indexing_directory}#{previous_indices.map { |i| "/other-tags/#{i}" }.join}/other-tags"
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
|
2020-01-29 16:59:39 +01:00
|
|
|
|
private def get_tagged_entry_path(key : String, indices : Array(String))
|
|
|
|
|
"#{indexing_directory}#{indices.map { |i| "/other-tags/#{i}" }.join}/data/#{key}.json"
|
|
|
|
|
end
|
|
|
|
|
private def get_data_symlink(key : String, indices : Array(String))
|
|
|
|
|
"../../../#{indices.map { "../../" }.join}/data/#{key}.json"
|
2019-12-11 18:10:09 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|