Compare commits
No commits in common. "456117bf209f9a27bb3a4acbe6626d82f5fc4c47" and "cd1ff4de7906032e4bf6301361899ea2bb334240" have entirely different histories.
456117bf20
...
cd1ff4de79
8 changed files with 12 additions and 105 deletions
|
|
@ -1,79 +0,0 @@
|
||||||
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
|
|
||||||
|
|
@ -350,7 +350,8 @@ abstract class DODB::Storage(V)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_storage_cache!
|
private def clear_storage_cache!
|
||||||
|
puts "DODB::Storage(V) clear_storage_cache! (no cache)"
|
||||||
# There's no cache by default.
|
# There's no cache by default.
|
||||||
# This function has to be changed in storage implementations.
|
# This function has to be changed in storage implementations.
|
||||||
end
|
end
|
||||||
|
|
@ -368,6 +369,7 @@ abstract class DODB::Storage(V)
|
||||||
# WARNING: slow operation.
|
# WARNING: slow operation.
|
||||||
# NOTE: clears all caches (`storage` and `triggers`).
|
# NOTE: clears all caches (`storage` and `triggers`).
|
||||||
def reindex_everything!
|
def reindex_everything!
|
||||||
|
clear_cache!
|
||||||
nuke_triggers!
|
nuke_triggers!
|
||||||
|
|
||||||
each_with_key() do |item, key|
|
each_with_key() do |item, key|
|
||||||
|
|
|
||||||
|
|
@ -45,30 +45,15 @@ class DODB::Storage::Cached(V) < DODB::Storage(V)
|
||||||
|
|
||||||
@cached_last_key = init_last_key
|
@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.
|
# Load the database in RAM at start-up.
|
||||||
DODB::Storage::Uncached(V).new(@directory_name).each_with_key do |v, key|
|
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
|
self[key] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_storage_cache!
|
private def clear_storage_cache!
|
||||||
|
puts "DODB::Storage::Cached(V) clear_storage_cache!"
|
||||||
data.clear
|
data.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,17 +46,12 @@ class DODB::Storage::Common(V) < DODB::Storage::Cached(V)
|
||||||
@cached_last_key = init_last_key
|
@cached_last_key = init_last_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_storage_cache!
|
private def clear_storage_cache!
|
||||||
|
puts "DODB::Storage::Common(V) clear_storage_cache!"
|
||||||
data.clear
|
data.clear
|
||||||
@lru = EfficientLRU(Int32).new lru.max_entries
|
@lru = EfficientLRU(Int32).new lru.max_entries
|
||||||
end
|
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.
|
# Verifies that the value is in cache, or read it on disk.
|
||||||
# Pushes the key in the lru.
|
# Pushes the key in the lru.
|
||||||
def [](key : Int32) : V
|
def [](key : Int32) : V
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ abstract class DODB::Trigger(V)
|
||||||
|
|
||||||
# Removes all cached values.
|
# Removes all cached values.
|
||||||
def clear_cache!
|
def clear_cache!
|
||||||
|
puts "DODB::Trigger(V) no cache"
|
||||||
# By default, there is no cache.
|
# By default, there is no cache.
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -345,6 +345,7 @@ class DODB::Trigger::IndexCached(V) < DODB::Trigger::Index(V)
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_cache!
|
def clear_cache!
|
||||||
|
puts "DODB::Trigger::IndexCached(V) clear_cache!"
|
||||||
data.clear
|
data.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -261,6 +261,7 @@ class DODB::Trigger::PartitionCached(V) < DODB::Trigger::Partition(V)
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_cache!
|
def clear_cache!
|
||||||
|
puts "DODB::Trigger::PartitionCached(V) clear_cache!"
|
||||||
data.clear
|
data.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -301,6 +301,7 @@ class DODB::Trigger::TagsCached(V) < DODB::Trigger::Tags(V)
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_cache!
|
def clear_cache!
|
||||||
|
puts "DODB::Trigger::TagsCached(V) clear_cache!"
|
||||||
data.clear
|
data.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue