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

Commit

Permalink
KEP-1272 storage needs to also track key size
Browse files Browse the repository at this point in the history
  • Loading branch information
ebruck committed Mar 26, 2019
1 parent ce44182 commit 17ca0e3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
15 changes: 4 additions & 11 deletions crud/test/crud_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1284,20 +1284,15 @@ TEST(crud, test_that_size_sends_proper_response)
expect_signed_response(session, "uuid", uint64_t(123), database_response::kSize, std::nullopt,
[](auto resp)
{
ASSERT_EQ(resp.size().bytes(), uint64_t(5));
ASSERT_EQ(resp.size().bytes(), uint64_t(8));
ASSERT_EQ(resp.size().keys(), uint32_t(1));
});

crud->handle_request("caller_id", msg, session);

// invalid uuid returns zero...
msg.mutable_header()->set_db_uuid("invalid-uuid");
expect_signed_response(session, "invalid-uuid", uint64_t(123), database_response::kSize, std::nullopt,
[](auto resp)
{
ASSERT_EQ(resp.size().bytes(), uint64_t(0));
ASSERT_EQ(resp.size().keys(), uint32_t(0));
});
expect_signed_response(session, "invalid-uuid", uint64_t(123), database_response::kError);

crud->handle_request("caller_id", msg, session);

Expand Down Expand Up @@ -1359,7 +1354,7 @@ TEST(crud, test_that_point_of_contact_size_sends_proper_response)
ASSERT_EQ(resp.header().db_uuid(), "uuid");
ASSERT_EQ(resp.header().nonce(), uint64_t(123));
ASSERT_EQ(resp.response_case(), database_response::kSize);
ASSERT_EQ(resp.size().bytes(), uint64_t(5));
ASSERT_EQ(resp.size().bytes(), uint64_t(8));
ASSERT_EQ(resp.size().keys(), uint32_t(1));
}));

Expand All @@ -1375,9 +1370,7 @@ TEST(crud, test_that_point_of_contact_size_sends_proper_response)
ASSERT_TRUE(parse_env_to_db_resp(resp, msg->SerializeAsString()));
ASSERT_EQ(resp.header().db_uuid(), "invalid-uuid");
ASSERT_EQ(resp.header().nonce(), uint64_t(123));
ASSERT_EQ(resp.response_case(), database_response::kSize);
ASSERT_EQ(resp.size().bytes(), uint64_t(0));
ASSERT_EQ(resp.size().keys(), uint32_t(0));
ASSERT_EQ(resp.response_case(), database_response::kError);
}));

crud->handle_request("caller_id", msg, nullptr);
Expand Down
8 changes: 4 additions & 4 deletions storage/mem_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mem_storage::create(const bzn::uuid_t& uuid, const std::string& key, const std::

if (inner_db.second.find(key) == inner_db.second.end())
{
inner_db.first += value.size();
inner_db.first += value.size() + key.size();
inner_db.second.insert(std::make_pair(key,value));
}
else
Expand Down Expand Up @@ -112,7 +112,7 @@ mem_storage::update(const bzn::uuid_t& uuid, const std::string& key, const std::

inner_db.first -= inner_search->second.size();
inner_search->second = value;
inner_db.first += inner_search->second.size();
inner_db.first += inner_search->second.size() + key.size();

return bzn::storage_result::ok;
}
Expand All @@ -137,7 +137,7 @@ mem_storage::remove(const bzn::uuid_t& uuid, const std::string& key)
return bzn::storage_result::not_found;
}

search->second.first -= record->second.size();
search->second.first -= (record->second.size() + key.size());
search->second.second.erase(record);
return bzn::storage_result::ok;
}
Expand Down Expand Up @@ -272,7 +272,7 @@ mem_storage::remove_range(const bzn::uuid_t& uuid, const std::string& first, con
auto end = inner_db->second.second.lower_bound(last);
while (match != end)
{
inner_db->second.first -= match->second.size();
inner_db->second.first -= (match->second.size() + match->first.size());
match = inner_db->second.second.erase(match);
}
}
Expand Down
8 changes: 4 additions & 4 deletions storage/rocksdb_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ rocksdb_storage::create(const bzn::uuid_t& uuid, const std::string& key, const s
}

// update metadata...
this->update_metadata_size(uuid, NAMESPACE_KEY, SIZE_KEY, ns_prev_size + value.size());
this->update_metadata_size(uuid, SIZE_KEY, key, value.size());
this->update_metadata_size(uuid, NAMESPACE_KEY, SIZE_KEY, ns_prev_size + value.size() + key.size());
this->update_metadata_size(uuid, SIZE_KEY, key, value.size() + key.size());

return bzn::storage_result::ok;
}
Expand Down Expand Up @@ -155,8 +155,8 @@ rocksdb_storage::update(const bzn::uuid_t& uuid, const std::string& key, const s
}

// update metadata...
this->update_metadata_size(uuid, NAMESPACE_KEY, SIZE_KEY, ns_prev_size - prev_size + value.size());
this->update_metadata_size(uuid, SIZE_KEY, key, value.size());
this->update_metadata_size(uuid, NAMESPACE_KEY, SIZE_KEY, ns_prev_size - prev_size + value.size() + key.size());
this->update_metadata_size(uuid, SIZE_KEY, key, value.size() + key.size());

return bzn::storage_result::ok;
}
Expand Down
9 changes: 7 additions & 2 deletions storage/test/storage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,20 @@ TYPED_TEST(storageTest, test_that_storage_can_create_a_record_and_read_the_same_

const auto returned_record = this->storage->read(USER_UUID, KEY);

EXPECT_EQ((*returned_record).size(), this->storage->get_size(USER_UUID).second);
EXPECT_EQ((*returned_record).size() + KEY.size(), this->storage->get_size(USER_UUID).second);
EXPECT_EQ(size_t(1), this->storage->get_size(USER_UUID).first);

// add another one...
EXPECT_EQ(bzn::storage_result::ok, this->storage->create(USER_UUID, "another_key", value));
EXPECT_EQ(value.size()*2, this->storage->get_size(USER_UUID).second);
EXPECT_EQ(value.size()*2 + KEY.size()+std::string("another_key").size(), this->storage->get_size(USER_UUID).second);
EXPECT_EQ(size_t(2), this->storage->get_size(USER_UUID).first);

EXPECT_EQ(*returned_record, value);

// remove first key
size_t size = this->storage->get_size(USER_UUID).second;
this->storage->remove(USER_UUID, KEY);
EXPECT_EQ(size-(KEY.size()+value.size()), this->storage->get_size(USER_UUID).second);
}


Expand Down

0 comments on commit 17ca0e3

Please sign in to comment.