FIFO database -> Common database.
This commit is contained in:
parent
e608981fa9
commit
14ed5dda5c
@ -24,10 +24,10 @@ class SPECDB::Cached(V) < DODB::Storage::Cached(V)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class SPECDB::FIFO(V) < DODB::Storage::Stacked(V)
|
class SPECDB::Common(V) < DODB::Storage::Common(V)
|
||||||
property storage_dir : String
|
property storage_dir : String
|
||||||
def initialize(storage_ext = "", @max_entries : UInt32 = 5_000, remove_previous_data = true)
|
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
|
::FileUtils.rm_rf storage_dir if remove_previous_data
|
||||||
super storage_dir, max_entries
|
super storage_dir, max_entries
|
||||||
end
|
end
|
||||||
|
32
spec/test-common.cr
Normal file
32
spec/test-common.cr
Normal file
@ -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
|
@ -1,15 +1,15 @@
|
|||||||
require "spec"
|
require "spec"
|
||||||
require "./db-cars.cr"
|
require "../src/fifo.cr"
|
||||||
|
|
||||||
describe "FIFO" do
|
describe "FIFO" do
|
||||||
it "add and remove values" do
|
it "add and remove values" do
|
||||||
fifo = FIFO(Int32).new 3 # Only 3 allowed entries.
|
fifo = FIFO(Int32).new 3 # Only 3 allowed entries.
|
||||||
(fifo << 1).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 stack
|
(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 << 3).should be_nil # last entry without exceeding the allowed size
|
||||||
(fifo << 4).should eq 1 # -> 1 (least recently used data)
|
(fifo << 4).should eq 1 # -> 1 (least recently used data)
|
||||||
(fifo << 4).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 stack)
|
(fifo << 2).should be_nil # -> nil (already in the fifo)
|
||||||
(fifo << 5).should eq 3 # -> 3 (least recently used data)
|
(fifo << 5).should eq 3 # -> 3 (least recently used data)
|
||||||
fifo.data.should eq([5, 2, 4] of Int32)
|
fifo.data.should eq([5, 2, 4] of Int32)
|
||||||
|
|
||||||
@ -17,33 +17,3 @@ describe "FIFO" do
|
|||||||
fifo.data.should eq([5, 4] of Int32)
|
fifo.data.should eq([5, 4] of Int32)
|
||||||
end
|
end
|
||||||
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
|
|
||||||
|
@ -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.
|
# 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;
|
# This database is relevant for high demand applications;
|
||||||
# which means both a high number of entries (data cannot fit entirely in RAM),
|
# 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.
|
# Typically a retail website.
|
||||||
# In such applications, the "keep the most recently used data in cache" policy works since new users
|
# 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.
|
# # 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.
|
# # Creates a (cached) index.
|
||||||
# cars_by_name = car_database.new_index "name", &.name
|
# 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.
|
# NOTE: fast for frequently requested data and requires a stable (and configurable) amount of memory.
|
||||||
class DODB::Storage::Stacked(V) < DODB::Storage::Cached(V)
|
class DODB::Storage::Common(V) < DODB::Storage::Cached(V)
|
||||||
# The *stack* a simple FIFO instance where the key of the requested data is pushed.
|
# 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.
|
# 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.
|
# Initializes the `StackedDataBase` with a maximum number of entries in the cache.
|
||||||
def initialize(@directory_name : String, max_entries : UInt32)
|
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 data_path
|
||||||
Dir.mkdir_p locks_directory
|
Dir.mkdir_p locks_directory
|
||||||
|
|
||||||
@ -70,15 +71,15 @@ class DODB::Storage::Stacked(V) < DODB::Storage::Cached(V)
|
|||||||
end
|
end
|
||||||
|
|
||||||
def unsafe_delete(key : Int32)
|
def unsafe_delete(key : Int32)
|
||||||
@stack.delete key if super key
|
@fifo.delete key if super key
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(key : Int32)
|
def delete(key : Int32)
|
||||||
@stack.delete key if super key
|
@fifo.delete key if super key
|
||||||
end
|
end
|
||||||
|
|
||||||
private def push_fifo(key : Int32)
|
private def push_fifo(key : Int32)
|
||||||
if entry_to_remove = @stack << key
|
if entry_to_remove = @fifo << key
|
||||||
@data.delete entry_to_remove
|
@data.delete entry_to_remove
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue
Block a user