Compare commits

...

2 commits

8 changed files with 105 additions and 12 deletions

View file

@ -0,0 +1,79 @@
require "./db-cars.cr"
rootdb = "/tmp/test-db"
db = DODB::Storage::Cached(Car).new rootdb
#db = DODB::Storage::Common(Car).new rootdb, 2
index_name = db.new_index "name", &.name
index_color = db.new_partition "color", &.color
index_keywords = db.new_tags "keywords", &.keywords
car0 = Car.new "Corvet-0", "red", ["shiny"]
car1 = Car.new "Corvet-1", "red", ["slow", "expensive"]
car2 = Car.new "Corvet-2", "blue", ["fast", "elegant"]
car3 = Car.new "Corvet-3", "violet", ["expensive"]
db << car0
db << car1
db << car2
db << car3
# Get a car from stored data based on its name.
if (c = index_name.get?("Corvet-2")) == car2
puts "We found the Corvet-2: #{c}"
else
puts "We didn't find the Corvet-2!!!"
end
# Get a car from stored data based on a keyword.
if c = index_keywords.get?("elegant")
puts "We found the elegant car: #{c}"
else
puts "We didn't find the elegant car!!!"
end
# All cached entries are dropped.
# WARNING: the Storage::Cached database don't read anything on the filesystem and relies entirely
# on cached values. Therefore, clearing the caches means the database won't answer any request
# with the proper values. The content has to be read again through a reindex_everything! for example.
#db.clear_cache!
db = DODB::Storage::Cached(Car).new rootdb
index_name = db.new_index "name", &.name
index_color = db.new_partition "color", &.color
index_keywords = db.new_tags "keywords", &.keywords
db.reindex_everything!
# Get a car from stored data based on its color.
if index_color.get?("violet").size == 1
puts "We found the violet car"
else
puts "We didn't find the violet car!!!"
end
# Get a car from stored data based on a keyword.
list_of_cars = index_keywords.get?("expensive")
if ! list_of_cars.nil? && list_of_cars.size > 0
puts "We found the expensive cars:"
list_of_cars.each { |car| puts "- #{car}" }
else
puts "We didn't find the expensive car!!!"
end
# Loop over entries, filling up the cache for Storage.
total_values = 0
db.each_with_key do |v, k|
total_values += 1
end
if total_values == 4
puts "We found all the values"
else
puts "We didn't find all the values!!!"
end
FileUtils.rm_r rootdb
# Caches should still have a single entry since we only searched through them only once.
#index_name.data.size.should eq 1
#index_color.data.size.should eq 1

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