Improved reverse searches a bit.
This commit is contained in:
parent
863bf369b8
commit
1bb5268602
@ -170,7 +170,7 @@ describe "DODB::DataBase" do
|
|||||||
item.should eq Ship.all_ships[index]
|
item.should eq Ship.all_ships[index]
|
||||||
end
|
end
|
||||||
|
|
||||||
db.reverse_each_with_index do |item, index|
|
db.each_with_index(reversed: true) do |item, index|
|
||||||
item.should eq Ship.all_ships.reverse[index]
|
item.should eq Ship.all_ships.reverse[index]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
59
src/dodb.cr
59
src/dodb.cr
@ -178,62 +178,51 @@ class DODB::DataBase(V)
|
|||||||
@indexers.each &.deindex(stringify_key(key), value)
|
@indexers.each &.deindex(stringify_key(key), value)
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
private def each_key(reversed = false)
|
||||||
# CAUTION: Very slow. Try not to use.
|
range = if reversed
|
||||||
# Can be useful for making dumps or to restore a database, however.
|
(last_index..0)
|
||||||
def each_with_index
|
else
|
||||||
dirname = data_path
|
(0..last_index)
|
||||||
Dir.each_child dirname do |child|
|
end
|
||||||
next if child.match /^\./
|
|
||||||
|
|
||||||
full_path = "#{dirname}/#{child}"
|
range.each do |key|
|
||||||
|
|
||||||
begin
|
|
||||||
# FIXME: Only intercept JSON parsing errors.
|
|
||||||
field = read full_path
|
|
||||||
rescue
|
|
||||||
next
|
|
||||||
end
|
|
||||||
|
|
||||||
key = child.gsub(/\.json$/, "").to_i
|
|
||||||
|
|
||||||
yield field, key
|
|
||||||
end
|
|
||||||
end
|
|
||||||
def each
|
|
||||||
each_with_index do |item, index|
|
|
||||||
yield item
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def reverse_each_with_index
|
|
||||||
(last_index..0).each do |key|
|
|
||||||
full_path = file_path key
|
full_path = file_path key
|
||||||
|
|
||||||
next unless File.exists? full_path
|
next unless File.exists? full_path
|
||||||
|
|
||||||
|
yield key, full_path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
##
|
||||||
|
# CAUTION: Very slow. Try not to use.
|
||||||
|
# Can be useful for making dumps or to restore a database, however.
|
||||||
|
def each_with_index(reversed = false)
|
||||||
|
dirname = data_path
|
||||||
|
|
||||||
|
each_key(reversed) do |key, path|
|
||||||
begin
|
begin
|
||||||
# FIXME: Only intercept JSON parsing errors.
|
# FIXME: Only intercept JSON parsing errors.
|
||||||
item = read full_path
|
field = read path
|
||||||
rescue
|
rescue
|
||||||
next
|
next
|
||||||
end
|
end
|
||||||
|
|
||||||
yield item, key
|
yield field, key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def reverse_each
|
def each(reversed = false)
|
||||||
reverse_each_with_index do |item, index|
|
each_with_index(reversed: reversed) do |item, index|
|
||||||
yield item
|
yield item
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# CAUTION: Very slow. Try not to use.
|
# CAUTION: Very slow. Try not to use.
|
||||||
def to_a
|
def to_a(reversed = false)
|
||||||
array = ::Array(V).new
|
array = ::Array(V).new
|
||||||
|
|
||||||
each do |value|
|
each(reversed) do |value|
|
||||||
array << value
|
array << value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user