WIP n-n partitions (is that the right name?)

remotes/1708105384931250775/master
Luka Vandervelden 2019-11-20 00:16:15 +01:00
parent f0a766322d
commit 2a92cacbb3
1 changed files with 72 additions and 18 deletions

View File

@ -3,7 +3,7 @@ require "file_utils"
require "json" require "json"
class FS::Hash(K, V) class FS::Hash(K, V)
# also works for indexes # Used for 1-n associations.
class PartitionData(V) class PartitionData(V)
property name : String property name : String
property key_proc : Proc(V, String) property key_proc : Proc(V, String)
@ -12,10 +12,21 @@ class FS::Hash(K, V)
end end
end end
# Used for 1-1 associations.
class IndexData(V) < PartitionData(V) class IndexData(V) < PartitionData(V)
end end
# Used for n-n associations.
class NNPartitionData(V)
property name : String
property key_proc : Proc(V, Array(String))
def initialize(@name, @key_proc)
end
end
@partitions = [] of PartitionData(V) @partitions = [] of PartitionData(V)
@nn_partitions = [] of NNPartitionData(V)
def initialize(@directory_name : String) def initialize(@directory_name : String)
Dir.mkdir_p data_path Dir.mkdir_p data_path
@ -37,17 +48,10 @@ class FS::Hash(K, V)
Dir.mkdir_p dir_path_indexes(name) Dir.mkdir_p dir_path_indexes(name)
end end
def get_partition(name : String, key : K) def new_nn_partition(name : String, &block : Proc(V, Array(String)))
r_value = Array(V).new @nn_partitions.push NNPartitionData(V).new name, block
partition_directory = "#{dir_path_partition name}/#{key}" Dir.mkdir_p "#{@directory_name}/.by_nn_#{name}"
Dir.each_child partition_directory do |child|
pp child
r_value << V.from_json ::File.read "#{partition_directory}/#{child}"
end
r_value
end end
def get_index(name : String) def get_index(name : String)
@ -55,14 +59,34 @@ class FS::Hash(K, V)
indexes_directory = dir_path_indexes name indexes_directory = dir_path_indexes name
Dir.each_child indexes_directory do |child| Dir.each_child indexes_directory do |child|
pp child
r_value << V.from_json ::File.read "#{indexes_directory}/#{child}" r_value << V.from_json ::File.read "#{indexes_directory}/#{child}"
end end
r_value r_value
end end
def get_partition(name : String, key : K)
r_value = Array(V).new
partition_directory = "#{dir_path_partition name}/#{key}"
Dir.each_child partition_directory do |child|
r_value << V.from_json ::File.read "#{partition_directory}/#{child}"
end
r_value
end
def get_nn_partition(name, key : K)
r_value = Array(V).new
partition_directory = "#{dir_path_nn name}/#{key}"
Dir.each_child partition_directory do |child|
r_value << V.from_json ::File.read "#{partition_directory}/#{child}"
end
r_value
end
def []?(key : K) : V? def []?(key : K) : V?
begin begin
read file_path key read file_path key
@ -110,6 +134,20 @@ class FS::Hash(K, V)
end end
end end
@nn_partitions.each do |nn|
indices = nn.key_proc.call value
indices.each do |index|
symlink = file_path_nn(key.to_s, nn.name, index)
Dir.mkdir_p ::File.dirname symlink
::File.delete symlink if ::File.exists? symlink
::File.symlink symlink_path_nn(key), symlink
end
end
end end
def delete(key : K) def delete(key : K)
@ -129,15 +167,19 @@ class FS::Hash(K, V)
when IndexData when IndexData
symlink = file_path_indexes(key.to_s, index.name) symlink = file_path_indexes(key.to_s, index.name)
puts "old index #{key.to_s} => #{index_key}"
puts "symlink is #{symlink}"
::File.delete symlink ::File.delete symlink
when PartitionData when PartitionData
symlink = file_path_partition(key, index.name, index_key) symlink = file_path_partition(key, index.name, index_key)
puts "old index #{key.to_s} => #{index_key}" ::File.delete symlink
puts "symlink is #{symlink}" end
end
@nn_partitions.each do |nn|
indices = nn.key_proc.call value
indices.each do |index_key|
symlink = file_path_nn(key.to_s, nn.name, index_key)
::File.delete symlink ::File.delete symlink
end end
@ -199,6 +241,10 @@ class FS::Hash(K, V)
"#{@directory_name}/indexes/by_#{index_name}" "#{@directory_name}/indexes/by_#{index_name}"
end end
private def dir_path_nn(name : String)
"#{@directory_name}/nn_partitions/by_#{name}"
end
private def file_path_indexes(key : String, index_name : String) private def file_path_indexes(key : String, index_name : String)
"#{dir_path_indexes index_name}/#{key}.json" "#{dir_path_indexes index_name}/#{key}.json"
end end
@ -207,6 +253,10 @@ class FS::Hash(K, V)
"#{dir_path_partition index_name}/#{index_key}/#{key}.json" "#{dir_path_partition index_name}/#{index_key}/#{key}.json"
end end
private def file_path_nn(key : String, index_name : String, index_key : String)
"#{dir_path_nn index_name}/#{index_key}/#{key}.json"
end
private def symlink_path_index(key : K) private def symlink_path_index(key : K)
"../../data/#{key.to_s}.json" "../../data/#{key.to_s}.json"
end end
@ -215,6 +265,10 @@ class FS::Hash(K, V)
"../../../data/#{key.to_s}.json" "../../../data/#{key.to_s}.json"
end end
private def symlink_path_nn(key : K)
symlink_path_partition key
end
private def read(file_path : String) private def read(file_path : String)
V.from_json ::File.read file_path V.from_json ::File.read file_path
end end