2018-11-19 21:06:36 +01:00
|
|
|
|
|
|
|
require "json"
|
|
|
|
|
|
|
|
class FS::Hash(K, V)
|
2018-11-19 23:35:49 +01:00
|
|
|
class PartitionData(V)
|
|
|
|
property name : String
|
|
|
|
property key_proc : Proc(V, String)
|
|
|
|
|
|
|
|
def initialize(@name, @key_proc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@partitions = [] of PartitionData(V)
|
|
|
|
|
2018-11-19 21:06:36 +01:00
|
|
|
def initialize(@directory_name : String)
|
2018-11-19 23:35:49 +01:00
|
|
|
Dir.mkdir_p @directory_name
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# name is the name that will be used on the file system.
|
|
|
|
def new_partition(name : String, &block : Proc(V, String))
|
|
|
|
@partitions.push PartitionData(V).new name, block
|
|
|
|
|
|
|
|
Dir.mkdir_p "#{@directory_name}/.by_#{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_partition(name : String, key : K)
|
|
|
|
r_value = Array(V).new
|
|
|
|
|
|
|
|
partition_directory = "#{@directory_name}/.by_#{name}/#{key}"
|
|
|
|
Dir.each_child partition_directory do |child|
|
|
|
|
pp child
|
|
|
|
|
|
|
|
r_value << V.from_json File.read "#{partition_directory}/#{child}"
|
|
|
|
end
|
|
|
|
|
|
|
|
r_value
|
2018-11-19 21:06:36 +01:00
|
|
|
end
|
|
|
|
|
2018-11-22 14:28:41 +01:00
|
|
|
def []?(key : K) : V?
|
2018-11-19 21:06:36 +01:00
|
|
|
begin
|
|
|
|
read file_path key
|
|
|
|
rescue
|
|
|
|
# FIXME: Only rescue JSON and “no such file” errors.
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-22 14:28:41 +01:00
|
|
|
def [](key : K) : V
|
2018-11-19 21:06:36 +01:00
|
|
|
read file_path key
|
|
|
|
end
|
|
|
|
|
2018-11-22 14:28:41 +01:00
|
|
|
def []=(key : K, value : V)
|
2018-11-19 23:35:49 +01:00
|
|
|
# FIXME: Update partitions pointing to previous value (in any)
|
|
|
|
|
2018-11-19 21:06:36 +01:00
|
|
|
File.write file_path(key), value.to_json
|
2018-11-19 23:35:49 +01:00
|
|
|
|
|
|
|
@partitions.each do |index|
|
|
|
|
index_key = index.key_proc.call value
|
|
|
|
|
|
|
|
symlink = file_path(key, index.name, index_key)
|
|
|
|
|
|
|
|
Dir.mkdir_p File.dirname symlink
|
|
|
|
|
|
|
|
File.delete symlink if File.exists? symlink
|
|
|
|
|
|
|
|
File.symlink symlink_path(key), symlink
|
|
|
|
end
|
2018-11-19 21:06:36 +01:00
|
|
|
end
|
|
|
|
|
2018-11-22 14:28:41 +01:00
|
|
|
def delete(key : K)
|
2018-11-19 21:06:36 +01:00
|
|
|
value = self[key]?
|
|
|
|
|
|
|
|
begin
|
|
|
|
File.delete file_path key
|
|
|
|
rescue
|
|
|
|
# FIXME: Only intercept “no such file" errors
|
|
|
|
end
|
|
|
|
|
2018-11-19 23:35:49 +01:00
|
|
|
unless value.nil?
|
|
|
|
@partitions.each do |index|
|
|
|
|
index_key = index.key_proc.call value
|
|
|
|
|
|
|
|
symlink = file_path(key, index.name, index_key)
|
|
|
|
|
|
|
|
puts "old index #{key.to_s} => #{index_key}"
|
|
|
|
puts "symlink is #{symlink}"
|
|
|
|
|
|
|
|
File.delete symlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-19 21:06:36 +01:00
|
|
|
value
|
|
|
|
end
|
|
|
|
|
2018-11-19 23:35:49 +01:00
|
|
|
##
|
|
|
|
# CAUTION: Very slow. Try not to use.
|
|
|
|
# Can be useful for making dumps or to restore a database, however.
|
2018-11-19 21:06:36 +01:00
|
|
|
def each
|
|
|
|
Dir.each_child @directory_name do |child|
|
2018-11-19 23:35:49 +01:00
|
|
|
next if child.match /^\./
|
|
|
|
|
2018-11-19 21:06:36 +01:00
|
|
|
full_path = "#{@directory_name}/#{child}"
|
|
|
|
|
|
|
|
begin
|
|
|
|
# FIXME: Only intercept JSON parsing errors.
|
|
|
|
field = read full_path
|
|
|
|
rescue
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
# FIXME: Will only work for String. :(
|
|
|
|
key = child.gsub /\.json$/, ""
|
|
|
|
|
|
|
|
yield key, field
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private def file_path(key : K)
|
|
|
|
"#{@directory_name}/#{key.to_s}.json"
|
|
|
|
end
|
|
|
|
|
2018-11-19 23:35:49 +01:00
|
|
|
private def file_path(key : String, index_name : String, index_key : String)
|
|
|
|
"#{@directory_name}/.by_#{index_name}/#{index_key}/#{key}.json"
|
|
|
|
end
|
|
|
|
|
|
|
|
private def symlink_path(key : K)
|
|
|
|
"../../#{key.to_s}.json"
|
|
|
|
end
|
|
|
|
|
2018-11-19 21:06:36 +01:00
|
|
|
private def read(file_path : String)
|
|
|
|
V.from_json File.read file_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|