Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EphemeralDB index update after document deletion #306

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/orion/core/io/database/ephemeraldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,15 @@ def delete_many(self, query=None):
retained_documents = []
for document in self._documents:
if not document.match(query):
retained_documents.append(document)
retained_documents.append(document.to_dict())
else:
deleted += 1

self._documents = retained_documents
for name, (keys, _) in self._indexes.items():
self._indexes[name] = (keys, set())

self._documents = []
self.insert_many(retained_documents)

return deleted

Expand Down
20 changes: 20 additions & 0 deletions tests/unittests/core/test_ephemeraldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,26 @@ def test_remove_with_id(self, exp_config, database, orion_db):
backward.populate_space(loaded_config)
assert loaded_configs == exp_config[0][1:]

def test_remove_update_indexes(self, exp_config, database, orion_db):
"""Verify that indexes are properly update after deletion."""
with pytest.raises(DuplicateKeyError):
orion_db.write('experiments', {'_id': exp_config[0][0]['_id']})
with pytest.raises(DuplicateKeyError):
orion_db.write('experiments', {'_id': exp_config[0][1]['_id']})

filt = {'_id': exp_config[0][0]['_id']}

count_before = database['experiments'].count()
# call interface
assert orion_db.remove('experiments', filt) == 1
assert database['experiments'].count() == count_before - 1
# Should not fail now, otherwise it means the indexes were not updated properly during
# remove()
orion_db.write('experiments', filt)
# And this should still fail
with pytest.raises(DuplicateKeyError):
orion_db.write('experiments', {'_id': exp_config[0][1]['_id']})


@pytest.mark.usefixtures("clean_db")
class TestCount(object):
Expand Down
22 changes: 22 additions & 0 deletions tests/unittests/core/test_pickleddb.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,28 @@ def test_remove_with_id(self, exp_config, orion_db):
backward.populate_space(loaded_config)
assert loaded_configs == exp_config[0][1:]

def test_remove_update_indexes(self, exp_config, orion_db):
"""Verify that indexes are properly update after deletion."""
with pytest.raises(DuplicateKeyError):
orion_db.write('experiments', {'_id': exp_config[0][0]['_id']})
with pytest.raises(DuplicateKeyError):
orion_db.write('experiments', {'_id': exp_config[0][1]['_id']})

filt = {'_id': exp_config[0][0]['_id']}

database = orion_db._get_database()._db
count_before = database['experiments'].count()
# call interface
assert orion_db.remove('experiments', filt) == 1
database = orion_db._get_database()._db
assert database['experiments'].count() == count_before - 1
# Should not fail now, otherwise it means the indexes were not updated properly during
# remove()
orion_db.write('experiments', filt)
# And this should still fail
with pytest.raises(DuplicateKeyError):
orion_db.write('experiments', {'_id': exp_config[0][1]['_id']})


@pytest.mark.usefixtures("clean_db")
class TestCount(object):
Expand Down