From 96fd8ec3a1ff60dd689588c305d7253b52294201 Mon Sep 17 00:00:00 2001 From: Philippe Pittoli Date: Sun, 28 Dec 2025 02:24:07 +0100 Subject: [PATCH] New load_db! method for Storage::Cached and remove puts. --- src/dodb/storage.cr | 4 +--- src/dodb/storage/cached.cr | 21 ++++++++++++++++++--- src/dodb/storage/common.cr | 9 +++++++-- src/dodb/trigger.cr | 1 - src/dodb/trigger/index.cr | 1 - src/dodb/trigger/partition.cr | 1 - src/dodb/trigger/tags.cr | 1 - 7 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/dodb/storage.cr b/src/dodb/storage.cr index 5139d79..7bb12f2 100644 --- a/src/dodb/storage.cr +++ b/src/dodb/storage.cr @@ -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| diff --git a/src/dodb/storage/cached.cr b/src/dodb/storage/cached.cr index 596d71f..8155743 100644 --- a/src/dodb/storage/cached.cr +++ b/src/dodb/storage/cached.cr @@ -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 diff --git a/src/dodb/storage/common.cr b/src/dodb/storage/common.cr index de7c515..c5cc7a1 100644 --- a/src/dodb/storage/common.cr +++ b/src/dodb/storage/common.cr @@ -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 diff --git a/src/dodb/trigger.cr b/src/dodb/trigger.cr index eb5c19c..9835fea 100644 --- a/src/dodb/trigger.cr +++ b/src/dodb/trigger.cr @@ -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 diff --git a/src/dodb/trigger/index.cr b/src/dodb/trigger/index.cr index bf12e7d..4f452ee 100644 --- a/src/dodb/trigger/index.cr +++ b/src/dodb/trigger/index.cr @@ -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 diff --git a/src/dodb/trigger/partition.cr b/src/dodb/trigger/partition.cr index 24fba55..f482b04 100644 --- a/src/dodb/trigger/partition.cr +++ b/src/dodb/trigger/partition.cr @@ -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 diff --git a/src/dodb/trigger/tags.cr b/src/dodb/trigger/tags.cr index fbd6328..6397bce 100644 --- a/src/dodb/trigger/tags.cr +++ b/src/dodb/trigger/tags.cr @@ -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