Enable all indexes (index, partition, tag) to be nilable.

This commit is contained in:
Philippe PITTOLI 2024-05-11 14:20:22 +02:00
parent 56fde98492
commit 5a2f17f7e0
5 changed files with 65 additions and 15 deletions

View File

@ -173,6 +173,24 @@ abstract class DODB::Storage(V)
end
end
def new_nilable_partition(name : String, &block : Proc(V, String | DODB::NoIndex))
CachedPartition(V).new(self, @directory_name, name, block).tap do |table|
@indexers << table
end
end
def new_nilable_uncached_partition(name : String, &block : Proc(V, String | DODB::NoIndex))
Partition(V).new(self, @directory_name, name, block).tap do |table|
@indexers << table
end
end
def new_nilable_RAM_partition(name : String, &block : Proc(V, String | DODB::NoIndex))
RAMOnlyPartition(V).new(self, @directory_name, name, block).tap do |table|
@indexers << table
end
end
def get_partition(table_name : String, partition_name : String)
partition = @indexers.find &.name.==(table_name)
@ -201,6 +219,24 @@ abstract class DODB::Storage(V)
end
end
def new_nilable_tags(name : String, &block : Proc(V, Array(String) | DODB::NoIndex))
CachedTags(V).new(self, @directory_name, name, block).tap do |tags|
@indexers << tags
end
end
def new_nilable_uncached_tags(name : String, &block : Proc(V, Array(String) | DODB::NoIndex))
Tags(V).new(self, @directory_name, name, block).tap do |tags|
@indexers << tags
end
end
def new_nilable_RAM_tags(name : String, &block : Proc(V, Array(String) | DODB::NoIndex))
RAMOnlyTags(V).new(self, @directory_name, name, block).tap do |tags|
@indexers << tags
end
end
def get_tags(name, key : String)
tag = @indexers.find &.name.==(name)

View File

@ -17,6 +17,8 @@ class DODB::Index(V) < DODB::Indexer(V)
def check!(key, value, old_value)
index_key = key_proc.call value
return if index_key.is_a? NoIndex
symlink = file_path_index index_key.to_s
if ::File.symlink? symlink
@ -33,7 +35,6 @@ class DODB::Index(V) < DODB::Indexer(V)
def index(key, value)
index_key = key_proc.call value
return if index_key.is_a? NoIndex
symlink = file_path_index index_key
@ -45,7 +46,6 @@ class DODB::Index(V) < DODB::Indexer(V)
def deindex(key, value)
index_key = key_proc.call value
return if index_key.is_a? NoIndex
symlink = file_path_index index_key
@ -151,6 +151,7 @@ class DODB::CachedIndex(V) < DODB::Index(V)
def check!(key, value, old_value)
index_key = key_proc.call value
return if index_key.is_a? NoIndex
if data[index_key]?
# In case both old and new values are pointing to the same key,
@ -170,19 +171,17 @@ class DODB::CachedIndex(V) < DODB::Index(V)
end
def index(key, value)
super(key, value)
index_key = key_proc.call value
return if index_key.is_a? NoIndex
super(key, value)
@data[index_key] = key.to_i
end
def deindex(key, value)
super(key, value)
index_key = key_proc.call value
return if index_key.is_a? NoIndex
super(key, value)
@data.delete index_key
end

View File

@ -1,5 +1,11 @@
class DODB::NoIndex
include JSON::Serializable
def_clone
def initialize()
end
end
module DODB

View File

@ -4,7 +4,7 @@ require "./indexer.cr"
class DODB::Partition(V) < DODB::Indexer(V)
property name : String
property key_proc : Proc(V, String)
property key_proc : Proc(V, String | NoIndex) | Proc(V, String)
getter storage_root : String
# Required to remove an entry in the DB.
@ -20,19 +20,18 @@ class DODB::Partition(V) < DODB::Indexer(V)
def index(key, value)
partition = key_proc.call value
return if partition.is_a? NoIndex
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
return if partition.is_a? NoIndex
symlink = get_partition_symlink(partition, key)
@ -111,8 +110,9 @@ class DODB::CachedPartition(V) < DODB::Partition(V)
end
def index(key, value)
super(key, value)
partition = key_proc.call value
return if partition.is_a? NoIndex
super(key, value)
array = if v = @data[partition]?
v
@ -125,8 +125,9 @@ class DODB::CachedPartition(V) < DODB::Partition(V)
end
def deindex(key, value)
super(key, value)
partition = key_proc.call value
return if partition.is_a? NoIndex
super(key, value)
if v = @data[partition]?
v.delete key.to_i
@ -178,6 +179,7 @@ end
class DODB::RAMOnlyPartition(V) < DODB::CachedPartition(V)
def index(key, value)
partition = key_proc.call value
return if partition.is_a? NoIndex
array = if v = @data[partition]?
v
@ -191,6 +193,7 @@ class DODB::RAMOnlyPartition(V) < DODB::CachedPartition(V)
def deindex(key, value)
partition = key_proc.call value
return if partition.is_a? NoIndex
if v = @data[partition]?
v.delete key.to_i

View File

@ -2,7 +2,7 @@ require "file_utils"
class DODB::Tags(V) < DODB::Indexer(V)
property name : String
property key_proc : Proc(V, Array(String))
property key_proc : Proc(V, Array(String) | NoIndex) | Proc(V, Array(String))
getter storage_root : String
# Required to remove an entry in the DB.
@ -18,6 +18,7 @@ class DODB::Tags(V) < DODB::Indexer(V)
def index(key, value)
indices = key_proc.call(value)
return if indices.is_a? NoIndex
indices.each do |i|
symlink = get_tagged_entry_path(i, key)
@ -30,6 +31,7 @@ class DODB::Tags(V) < DODB::Indexer(V)
def deindex(key, value)
indices = key_proc.call(value)
return if indices.is_a? NoIndex
indices.each do |i|
symlink = get_tagged_entry_path(i, key)
@ -131,8 +133,9 @@ class DODB::CachedTags(V) < DODB::Tags(V)
property data = Hash(String, Array(Int32)).new
def index(key, value)
super(key, value)
indices = key_proc.call value
return if indices.is_a? NoIndex
super(key, value)
indices.each do |tag|
array = if v = @data[tag]?
@ -147,8 +150,9 @@ class DODB::CachedTags(V) < DODB::Tags(V)
end
def deindex(key, value)
super(key, value)
indices = key_proc.call value
return if indices.is_a? NoIndex
super(key, value)
indices.each do |tag|
if v = @data[tag]?
@ -204,6 +208,7 @@ end
class DODB::RAMOnlyTags(V) < DODB::CachedTags(V)
def index(key, value)
indices = key_proc.call value
return if indices.is_a? NoIndex
indices.each do |tag|
array = if v = @data[tag]?
@ -219,6 +224,7 @@ class DODB::RAMOnlyTags(V) < DODB::CachedTags(V)
def deindex(key, value)
indices = key_proc.call value
return if indices.is_a? NoIndex
indices.each do |tag|
if v = @data[tag]?