From 197af64c83bafb42f104f64c2bb9d2404ade3b47 Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Sun, 5 May 2024 00:43:15 +0200 Subject: [PATCH] Way faster searches by indexes (index, partition, tags) with a cached DB. --- spec/benchmark-cars.cr | 4 ++-- src/cached.cr | 2 -- src/dodb/index.cr | 6 +----- src/dodb/partition.cr | 11 ++++------- src/dodb/tags.cr | 14 +++++--------- 5 files changed, 12 insertions(+), 25 deletions(-) diff --git a/spec/benchmark-cars.cr b/spec/benchmark-cars.cr index bb0eda3..f850c4c 100644 --- a/spec/benchmark-cars.cr +++ b/spec/benchmark-cars.cr @@ -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 diff --git a/src/cached.cr b/src/cached.cr index f2a7569..b3a88e7 100644 --- a/src/cached.cr +++ b/src/cached.cr @@ -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 diff --git a/src/dodb/index.cr b/src/dodb/index.cr index bb38ffa..9086744 100644 --- a/src/dodb/index.cr +++ b/src/dodb/index.cr @@ -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? diff --git a/src/dodb/partition.cr b/src/dodb/partition.cr index 84960f3..3220900 100644 --- a/src/dodb/partition.cr +++ b/src/dodb/partition.cr @@ -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 diff --git a/src/dodb/tags.cr b/src/dodb/tags.cr index e228089..d202b26 100644 --- a/src/dodb/tags.cr +++ b/src/dodb/tags.cr @@ -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