From 14ed5dda5c39d8e301581fa445a34d38d8fbb678 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sat, 25 May 2024 03:41:00 +0200 Subject: [PATCH] FIFO database -> Common database. --- spec/spec-database.cr | 4 +-- spec/test-common.cr | 32 +++++++++++++++++ spec/test-fifo.cr | 40 +++------------------- src/dodb/storage/{stacked.cr => common.cr} | 25 +++++++------- 4 files changed, 52 insertions(+), 49 deletions(-) create mode 100644 spec/test-common.cr rename src/dodb/storage/{stacked.cr => common.cr} (74%) diff --git a/spec/spec-database.cr b/spec/spec-database.cr index 82d964e..63ce7d4 100644 --- a/spec/spec-database.cr +++ b/spec/spec-database.cr @@ -24,10 +24,10 @@ class SPECDB::Cached(V) < DODB::Storage::Cached(V) end end -class SPECDB::FIFO(V) < DODB::Storage::Stacked(V) +class SPECDB::Common(V) < DODB::Storage::Common(V) property storage_dir : String def initialize(storage_ext = "", @max_entries : UInt32 = 5_000, remove_previous_data = true) - @storage_dir = "specdb-storage-fifo-#{@max_entries}#{storage_ext}" + @storage_dir = "specdb-storage-common-#{@max_entries}#{storage_ext}" ::FileUtils.rm_rf storage_dir if remove_previous_data super storage_dir, max_entries end diff --git a/spec/test-common.cr b/spec/test-common.cr new file mode 100644 index 0000000..ce2f53e --- /dev/null +++ b/spec/test-common.cr @@ -0,0 +1,32 @@ +require "spec" +require "./db-cars.cr" + +describe "SPECDB::Common" do + it "basics, 3 values" do + car0 = Car.new "Corvet-0", "red", [] of String + car1 = Car.new "Corvet-1", "red", [] of String + car2 = Car.new "Corvet-2", "red", [] of String + car3 = Car.new "Corvet-3", "red", [] of String + + db = SPECDB::Common(Car).new "", 3 + + db.data.keys.sort.should eq([] of Int32) + + db << car0 + db.data.keys.sort.should eq([0] of Int32) + + db << car1 + db.data.keys.sort.should eq([0, 1] of Int32) + + db << car2 + db.data.keys.sort.should eq([0, 1, 2] of Int32) + db[0] # Let's use the first value, it shouldn't be the one to be dropped. + + db << car3 + db.data.keys.sort.should eq([0, 2, 3] of Int32) + + db.delete 2 + db.data.keys.sort.should eq([0, 3] of Int32) + db.fifo.data.should eq([3, 0] of Int32) + end +end diff --git a/spec/test-fifo.cr b/spec/test-fifo.cr index 2639865..cf3231c 100644 --- a/spec/test-fifo.cr +++ b/spec/test-fifo.cr @@ -1,15 +1,15 @@ require "spec" -require "./db-cars.cr" +require "../src/fifo.cr" describe "FIFO" do it "add and remove values" do fifo = FIFO(Int32).new 3 # Only 3 allowed entries. - (fifo << 1).should be_nil # there is still room in the stack - (fifo << 2).should be_nil # there is still room in the stack + (fifo << 1).should be_nil # there is still room in the fifo + (fifo << 2).should be_nil # there is still room in the fifo (fifo << 3).should be_nil # last entry without exceeding the allowed size (fifo << 4).should eq 1 # -> 1 (least recently used data) - (fifo << 4).should be_nil # -> nil (already in the stack) - (fifo << 2).should be_nil # -> nil (already in the stack) + (fifo << 4).should be_nil # -> nil (already in the fifo) + (fifo << 2).should be_nil # -> nil (already in the fifo) (fifo << 5).should eq 3 # -> 3 (least recently used data) fifo.data.should eq([5, 2, 4] of Int32) @@ -17,33 +17,3 @@ describe "FIFO" do fifo.data.should eq([5, 4] of Int32) end end - -describe "SPECDB::FIFO" do - it "basics, 3 values" do - car0 = Car.new "Corvet-0", "red", [] of String - car1 = Car.new "Corvet-1", "red", [] of String - car2 = Car.new "Corvet-2", "red", [] of String - car3 = Car.new "Corvet-3", "red", [] of String - - db = SPECDB::FIFO(Car).new "", 3 - - db.data.keys.sort.should eq([] of Int32) - - db << car0 - db.data.keys.sort.should eq([0] of Int32) - - db << car1 - db.data.keys.sort.should eq([0, 1] of Int32) - - db << car2 - db.data.keys.sort.should eq([0, 1, 2] of Int32) - db[0] # Let's use the first value, it shouldn't be the one to be dropped. - - db << car3 - db.data.keys.sort.should eq([0, 2, 3] of Int32) - - db.delete 2 - db.data.keys.sort.should eq([0, 3] of Int32) - db.stack.data.should eq([3, 0] of Int32) - end -end diff --git a/src/dodb/storage/stacked.cr b/src/dodb/storage/common.cr similarity index 74% rename from src/dodb/storage/stacked.cr rename to src/dodb/storage/common.cr index 8a39dfc..cac6fe5 100644 --- a/src/dodb/storage/stacked.cr +++ b/src/dodb/storage/common.cr @@ -1,18 +1,19 @@ -# Stacked database: only recently requested entries are kept in memory. +# Common database: only recently requested entries are kept in memory. # -# Most recently used entries are in cache and put on the top of the stack. # Least recently used entries may be removed from the cache in order to keep the amount of memory used reasonable. # +# The number of entries to keep in memory is configurable. +# # This database is relevant for high demand applications; # which means both a high number of entries (data cannot fit entirely in RAM), -# and a high number of requests. +# and a high number of requests, often to reach the same entries. # Typically a retail website. # In such applications, the "keep the most recently used data in cache" policy works since new users -# constantly push commonly requested data on top of the stack. +# constantly ask for the same data over and over. # # ``` # # Creates a DODB stacked database. -# car_database = DODB::Storage::Stacked.new "/path/to/db" +# car_database = DODB::Storage::Common.new "/path/to/db" # # # Creates a (cached) index. # cars_by_name = car_database.new_index "name", &.name @@ -31,14 +32,14 @@ # ``` # # NOTE: fast for frequently requested data and requires a stable (and configurable) amount of memory. -class DODB::Storage::Stacked(V) < DODB::Storage::Cached(V) - # The *stack* a simple FIFO instance where the key of the requested data is pushed. +class DODB::Storage::Common(V) < DODB::Storage::Cached(V) + # The *fifo* a simple FIFO instance where the key of the requested data is pushed. # In case the number of stored entries exceeds what is allowed, the least recently used entry is removed. - property stack : FIFO(Int32) + property fifo : FIFO(Int32) # Initializes the `StackedDataBase` with a maximum number of entries in the cache. def initialize(@directory_name : String, max_entries : UInt32) - @stack = FIFO(Int32).new max_entries + @fifo = FIFO(Int32).new max_entries Dir.mkdir_p data_path Dir.mkdir_p locks_directory @@ -70,15 +71,15 @@ class DODB::Storage::Stacked(V) < DODB::Storage::Cached(V) end def unsafe_delete(key : Int32) - @stack.delete key if super key + @fifo.delete key if super key end def delete(key : Int32) - @stack.delete key if super key + @fifo.delete key if super key end private def push_fifo(key : Int32) - if entry_to_remove = @stack << key + if entry_to_remove = @fifo << key @data.delete entry_to_remove end end