diff --git a/src/dodb/storage.cr b/src/dodb/storage.cr index 2e3ec43..20a35a9 100644 --- a/src/dodb/storage.cr +++ b/src/dodb/storage.cr @@ -344,7 +344,7 @@ abstract class DODB::Storage(V) Dir.mkdir_p data_path end - private def remove_triggers! + private def nuke_triggers! @triggers.each do |trigger| trigger.nuke_trigger end @@ -354,7 +354,7 @@ abstract class DODB::Storage(V) # # WARNING: slow operation. def reindex_everything! - remove_triggers! + nuke_triggers! each_with_key() do |item, key| run_triggers key, item @@ -436,7 +436,7 @@ abstract class DODB::Storage(V) end # Lists all the keys in the database. - private def each_key(reversed = false) + def each_key_from_fs(reversed = false) # Removes the first two "." and ".." directories. keys = Dir.children(data_path).map(&.to_i).sort (reversed ? keys.reverse : keys).each do |key| @@ -455,7 +455,7 @@ abstract class DODB::Storage(V) -1 end - each_key(reversed) do |key| + each_key_from_fs(reversed) do |key| offset -= 1 if offset >= 0 next if offset >= 0 diff --git a/src/dodb/storage/common.cr b/src/dodb/storage/common.cr index abd9446..8094615 100644 --- a/src/dodb/storage/common.cr +++ b/src/dodb/storage/common.cr @@ -83,7 +83,35 @@ class DODB::Storage::Common(V) < DODB::Storage::Cached(V) private def push_lru(key : Int32) if entry_to_remove = @lru << key + # Remove the data from the cache but doesn't remove the indexes. @data.delete entry_to_remove end end + + # Function `each_with_key` is a rewrite of the parent. + # The `Common` database doesn't keep all entries in memory, data has to be read from disk. + def each_with_key(reversed : Bool = false, offset = 0, limit : Int32? = -1) + limit = if l = limit + l + else + -1 + end + + each_key_from_fs(reversed) do |key| + offset -= 1 if offset >= 0 + next if offset >= 0 + + return if limit == 0 + limit -= 1 if limit > 0 + + begin + # FIXME: Only intercept JSON parsing errors. + value = self[key] + rescue + next + end + + yield value, key + end + end end