Add a little script to show comme use of the databases.

This commit is contained in:
Philippe Pittoli 2025-12-28 02:25:46 +01:00
parent 96fd8ec3a1
commit 456117bf20

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