Way faster searches by indexes (index, partition, tags) with a cached DB.

This commit is contained in:
Philippe PITTOLI 2024-05-05 00:43:15 +02:00
parent d91452a5ae
commit 197af64c83
5 changed files with 12 additions and 25 deletions

View File

@ -98,7 +98,7 @@ Benchmark.ips do |x|
end
x.report("(cars db) searching a data with a partition (without a cache)") do
red_cars = cached_searchby_color.get "red"
red_cars = uncached_searchby_color.get "red"
end
end
@ -109,7 +109,7 @@ Benchmark.ips do |x|
end
x.report("(cars db) searching a data with a tag (without a cache)") do
red_cars = cached_searchby_keywords.get "spacious"
red_cars = uncached_searchby_keywords.get "spacious"
end
end

View File

@ -45,8 +45,6 @@ class DODB::CachedDataBase(V) < DODB::Storage(V)
return nil
end
def [](key : Int32) : V
# raise MissingEntry.new(key) unless ::File.exists? file_path key
# read file_path key
@data[key] rescue raise MissingEntry.new(key)
end

View File

@ -59,11 +59,7 @@ class DODB::Index(V) < DODB::Indexer(V)
end
def get(index : String) : V
file_path = file_path_index index
raise MissingEntry.new(@name, index) unless ::File.exists? file_path
V.from_json ::File.read file_path
@storage[get_key index]
end
def get?(index : String) : V?

View File

@ -48,7 +48,7 @@ class DODB::Partition(V) < DODB::Indexer(V)
return r_value unless Dir.exists? partition_directory
Dir.each_child partition_directory do |child|
r_value << V.from_json ::File.read "#{partition_directory}/#{child}"
r_value << @storage[get_key child]
end
r_value
@ -64,12 +64,10 @@ class DODB::Partition(V) < DODB::Indexer(V)
return unless Dir.exists? partition_directory
Dir.each_child partition_directory do |child|
path = "#{partition_directory}/#{child}"
item = V.from_json ::File.read path
key = get_key child
item = @storage[key]
if yield item
key = get_key path
@storage.delete key
end
end
@ -80,8 +78,7 @@ class DODB::Partition(V) < DODB::Indexer(V)
end
private def get_key(path : String) : Int32
::File.readlink(path)
.sub(/\.json$/, "")
path.sub(/\.json$/, "")
.sub(/^.*\//, "")
.to_i
end

View File

@ -46,10 +46,8 @@ class DODB::Tags(V) < DODB::Indexer(V)
return r_value unless Dir.exists? tag_directory
Dir.each_child tag_directory do |child|
r_value << {
V.from_json(::File.read("#{tag_directory}/#{child}")),
File.basename(child).gsub(/\.json$/, "").to_i
}
key = get_key child
r_value << { @storage[key], key }
end
r_value
@ -81,19 +79,17 @@ class DODB::Tags(V) < DODB::Indexer(V)
return unless Dir.exists? tag_directory
Dir.each_child tag_directory do |child|
path = "#{tag_directory}/#{child}"
item = V.from_json ::File.read path
key = get_key child
item = @storage[key]
if yield item
key = get_key path
@storage.delete key
end
end
end
private def get_key(path : String) : Int32
::File.readlink(path)
.sub(/\.json$/, "")
path.sub(/\.json$/, "")
.sub(/^.*\//, "")
.to_i
end