reindex_everything! method added.

remotes/1708105384931250775/master
Luka Vandervelden 2019-12-12 00:01:02 +01:00
parent aec1dc2011
commit 858d92970d
2 changed files with 39 additions and 0 deletions

View File

@ -249,5 +249,30 @@ describe "DODB::DataBase" do
db_ships_by_tags.get("flagship").should eq([] of Ship)
end
end
describe "tools" do
it "rebuilds indexes" do
db = DODB::SpecDataBase.new
db_ships_by_name = db.new_index "name", &.name
db_ships_by_class = db.new_partition "class", &.class
db_ships_by_tags = db.new_tags "tags", &.tags
Ship.all_ships.each do |ship|
db[ship.id] = ship
end
db.reindex_everything!
Ship.all_ships.each do |ship|
db_ships_by_name.get?(ship.name).should eq(ship)
db_ships_by_class.get(ship.class).should contain(ship)
end
end
# Migration testing code will go here as soon as migration testing
# becomes relevant (due to format changes or so). For small projects,
# reindexing will work very well in the meantime.
end
end

View File

@ -159,5 +159,19 @@ class DODB::DataBase(K, V)
private def read(file_path : String)
V.from_json ::File.read file_path
end
# A very slow operation that removes all indices and then rewrites
# them all.
def reindex_everything!
old_data = to_h
old_data.each do |key, value|
self.delete key
end
old_data.each do |key, value|
self[key] = value
end
end
end