dodb.cr/src/dodb/partition.cr

154 lines
3.2 KiB
Crystal
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

require "file_utils"
require "./indexer.cr"
class DODB::Partition(V) < DODB::Indexer(V)
property name : String
property key_proc : Proc(V, String)
getter storage_root : String
# Required to remove an entry in the DB.
@storage : DODB::Storage(V)
def initialize(@storage, @storage_root, @name, @key_proc)
::Dir.mkdir_p indexing_directory
end
def check!(key, value, old_value)
return true # Partitions dont have collisions or overloads.
end
def index(key, value)
partition = key_proc.call value
symlink = get_partition_symlink(partition, key)
Dir.mkdir_p ::File.dirname symlink
# FIXME: Should not happen anymore. Should we remove this?
::File.delete symlink if ::File.exists? symlink
::File.symlink get_data_symlink(key), symlink
end
def deindex(key, value)
partition = key_proc.call value
symlink = get_partition_symlink(partition, key)
begin
::File.delete symlink
rescue File::NotFoundError
end
end
def get(partition) : Array(V)
r_value = Array(V).new
partition_directory = indexing_directory partition
return r_value unless Dir.exists? partition_directory
Dir.each_child partition_directory do |child|
r_value << @storage[get_key child]
end
r_value
end
def get?(partition) : Array(V)?
get partition
rescue MissingEntry
nil
end
def delete(partition)
delete partition, do true end
end
def delete(partition, &matcher)
partition_directory = indexing_directory partition
return unless Dir.exists? partition_directory
Dir.each_child partition_directory do |child|
key = get_key child
item = @storage[key]
if yield item
@storage.delete key
end
end
end
def indexing_directory : String
"#{@storage_root}/partitions/by_#{@name}"
end
private def get_key(path : String) : Int32
path.sub(/^.*\//, "").to_i
end
private def indexing_directory(partition)
"#{indexing_directory}/#{partition}"
end
private def get_partition_symlink(partition : String, key : String)
"#{indexing_directory partition}/#{key}"
end
private def get_data_symlink(key : String)
"../../../data/#{key}"
end
end
class DODB::CachedPartition(V) < DODB::Partition(V)
# This hash contains the relation between the index key and the data keys.
property data = Hash(String, Array(Int32)).new
def index(key, value)
super(key, value)
partition = key_proc.call value
array = if v = @data[partition]?
v
else
Array(Int32).new
end
array << key.to_i
@data[partition] = array
end
def deindex(key, value)
super(key, value)
partition = key_proc.call value
if v = @data[partition]?
v.delete key.to_i
@data[partition] = v
end
end
def get(partition)
r_value = Array(Tuple(V, Int32)).new
if keys = @data[partition]?
keys.each do |data_key|
r_value << { @storage[data_key], data_key }
end
else
# Get the key from the database representation on the file-system.
partition_directory = indexing_directory partition
raise MissingEntry.new(@name, partition) unless Dir.exists? partition_directory
Dir.each_child partition_directory do |child|
r_value << { @storage[get_key child], get_key child }
end
@data[partition] = r_value.map &.[1]
end
r_value.map &.[0]
end
end