New load_db! method for Storage::Cached and remove puts.

This commit is contained in:
Philippe Pittoli 2025-12-28 02:24:07 +01:00
parent cd1ff4de79
commit 96fd8ec3a1
7 changed files with 26 additions and 12 deletions

View file

@ -350,8 +350,7 @@ abstract class DODB::Storage(V)
end
end
private def clear_storage_cache!
puts "DODB::Storage(V) clear_storage_cache! (no cache)"
def clear_storage_cache!
# There's no cache by default.
# This function has to be changed in storage implementations.
end
@ -369,7 +368,6 @@ abstract class DODB::Storage(V)
# WARNING: slow operation.
# NOTE: clears all caches (`storage` and `triggers`).
def reindex_everything!
clear_cache!
nuke_triggers!
each_with_key() do |item, key|

View file

@ -45,15 +45,30 @@ class DODB::Storage::Cached(V) < DODB::Storage(V)
@cached_last_key = init_last_key
# Load the database (to fill up the cache) at start-up.
load_db!
end
# `Storage::Cached` doesn't perform look-ups from the filesystem by itself upon requests,
# the entire database has to be initialized by reading the entire on-disk data in order to
# fill up the cache.
#
# This function is called once at start-up and should be used whenever the entire cache
# is cleaned up for whatever reason, otherwise the database will be seen as empty.
#
# WARNING: beware of triggers.
# NOTE: this function has no use in `Storage::Common` because the database entries are read from the disk
# when the value isn't in cache.
# This function doesn't exist in `Storage::Uncached` since there is no cache.
def load_db!
# Load the database in RAM at start-up.
DODB::Storage::Uncached(V).new(@directory_name).each_with_key do |v, key|
puts "\rloading data from #{@directory_name} at key #{key}"
# puts "\rloading data from #{@directory_name} at key #{key}"
self[key] = v
end
end
private def clear_storage_cache!
puts "DODB::Storage::Cached(V) clear_storage_cache!"
def clear_storage_cache!
data.clear
end

View file

@ -46,12 +46,17 @@ class DODB::Storage::Common(V) < DODB::Storage::Cached(V)
@cached_last_key = init_last_key
end
private def clear_storage_cache!
puts "DODB::Storage::Common(V) clear_storage_cache!"
def clear_storage_cache!
data.clear
@lru = EfficientLRU(Int32).new lru.max_entries
end
# :nodoc:
# There is no need for this function in `Storage::Common`,
# therefore it is put in private and removed from the documentation.
private def load_db!
end
# Verifies that the value is in cache, or read it on disk.
# Pushes the key in the lru.
def [](key : Int32) : V

View file

@ -31,7 +31,6 @@ abstract class DODB::Trigger(V)
# Removes all cached values.
def clear_cache!
puts "DODB::Trigger(V) no cache"
# By default, there is no cache.
end

View file

@ -345,7 +345,6 @@ class DODB::Trigger::IndexCached(V) < DODB::Trigger::Index(V)
end
def clear_cache!
puts "DODB::Trigger::IndexCached(V) clear_cache!"
data.clear
end

View file

@ -261,7 +261,6 @@ class DODB::Trigger::PartitionCached(V) < DODB::Trigger::Partition(V)
end
def clear_cache!
puts "DODB::Trigger::PartitionCached(V) clear_cache!"
data.clear
end

View file

@ -301,7 +301,6 @@ class DODB::Trigger::TagsCached(V) < DODB::Trigger::Tags(V)
end
def clear_cache!
puts "DODB::Trigger::TagsCached(V) clear_cache!"
data.clear
end