Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
KEP-1087 Deletion of a database will flush ttl table
Browse files Browse the repository at this point in the history
  • Loading branch information
ebruck committed Feb 27, 2019
1 parent 4fbe33f commit c15322b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
21 changes: 19 additions & 2 deletions crud/crud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,9 @@ crud::handle_delete_db(const bzn::caller_id_t& caller_id, const database_msg& re
{
result = this->storage->remove(PERMISSION_UUID, request.header().db_uuid());

// TODO: flush ttl table, but stale entries WILL eventually be removed by the timer callback...

this->storage->remove(request.header().db_uuid());

this->flush_expiration_entries(request.header().db_uuid());
}
}

Expand Down Expand Up @@ -919,3 +919,20 @@ crud::check_key_expiration(const boost::system::error_code& ec)
this->expire_timer->async_wait(std::bind(&crud::check_key_expiration, shared_from_this(), std::placeholders::_1));
}
}


void
crud::flush_expiration_entries(const bzn::uuid_t& uuid)
{
for (const auto& generated_key : this->storage->get_keys(TTL_UUID))
{
const auto [db_uuid, key] = extract_uuid_key(generated_key);

if (db_uuid == uuid)
{
this->storage->remove(TTL_UUID, generated_key);

LOG(debug) << "removing ttl entry for: " << db_uuid << ":" << key;
}
}
}
1 change: 1 addition & 0 deletions crud/crud.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ namespace bzn
bool expired(const bzn::uuid_t& uuid, const bzn::key_t& key);
void update_expiration_entry(const bzn::uuid_t& uuid, const bzn::key_t& key, uint64_t expire);
void remove_expiration_entry(const bzn::key_t& generated_key);
void flush_expiration_entries(const bzn::uuid_t& uuid);
std::optional<uint64_t> get_ttl(const bzn::uuid_t& uuid, const bzn::key_t& key) const;

std::shared_ptr<bzn::storage_base> storage;
Expand Down
21 changes: 20 additions & 1 deletion crud/test/crud_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ using namespace ::testing;

namespace
{
const std::string TTL_UUID{"TTL"};

bool parse_env_to_db_resp(database_response& target, const std::string& source)
{
bzn_envelope intermediate;
Expand Down Expand Up @@ -1336,7 +1338,9 @@ TEST(crud, test_that_point_of_contact_has_db_request_sends_proper_response)

TEST(crud, test_that_delete_db_sends_proper_response)
{
bzn::crud crud(std::make_shared<NiceMock<bzn::asio::Mockio_context_base>>(), std::make_shared<bzn::mem_storage>(),
auto storage = std::make_shared<bzn::mem_storage>();

bzn::crud crud(std::make_shared<NiceMock<bzn::asio::Mockio_context_base>>(), storage,
std::make_shared<NiceMock<bzn::Mocksubscription_manager_base>>(), nullptr);

// delete database...
Expand All @@ -1358,6 +1362,13 @@ TEST(crud, test_that_delete_db_sends_proper_response)

crud.handle_request("caller_id", msg, mock_session);

// add a key with a ttl
msg.mutable_create()->set_key("key1");
msg.mutable_create()->set_value("value");
msg.mutable_create()->set_expire(123);

crud.handle_request("caller_id", msg, nullptr);

// delete database...
msg.mutable_delete_db();

Expand All @@ -1369,6 +1380,14 @@ TEST(crud, test_that_delete_db_sends_proper_response)
expect_signed_response(mock_session, "uuid", std::nullopt, database_response::RESPONSE_NOT_SET);

crud.handle_request("caller_id", msg, mock_session);

// test storage for ttl entry
Json::Value ttl_key;
ttl_key["uuid"] = "uuid";
ttl_key["key"] = "key1";

ASSERT_FALSE(storage->has(TTL_UUID, ttl_key.toStyledString()));

}

TEST(crud, test_that_point_of_contact_delete_db_sends_proper_response)
Expand Down

0 comments on commit c15322b

Please sign in to comment.