Added BlockStore::remove(key)

This commit is contained in:
Sebastian Messmer 2015-02-22 00:29:21 +01:00
parent a3a4bef88a
commit 37e8a511f5
11 changed files with 44 additions and 3 deletions

View File

@ -36,5 +36,10 @@ unique_ptr<Block> InMemoryBlockStore::load(const Key &key) {
}
}
void InMemoryBlockStore::remove(const Key &key) {
int numRemoved = _blocks.erase(key.ToString());
assert(1==numRemoved);
}
}
}

View File

@ -18,6 +18,7 @@ public:
std::unique_ptr<Block> create(const Key &key, size_t size) override;
std::unique_ptr<Block> load(const Key &key) override;
void remove(const Key &key) override;
private:
std::map<std::string, InMemoryBlock> _blocks;

View File

@ -71,6 +71,12 @@ unique_ptr<OnDiskBlock> OnDiskBlock::CreateOnDisk(const bf::path &rootdir, const
return block;
}
void OnDiskBlock::RemoveFromDisk(const bf::path &rootdir, const Key &key) {
auto filepath = rootdir / key.ToString();
assert(bf::is_regular_file(filepath));
bf::remove(filepath);
}
void OnDiskBlock::_fillDataWithZeroes() {
_data.FillWithZeroes();
}

View File

@ -19,6 +19,7 @@ public:
static std::unique_ptr<OnDiskBlock> LoadFromDisk(const boost::filesystem::path &rootdir, const Key &key);
static std::unique_ptr<OnDiskBlock> CreateOnDisk(const boost::filesystem::path &rootdir, const Key &key, size_t size);
static void RemoveFromDisk(const boost::filesystem::path &rootdir, const Key &key);
void *data() override;
const void *data() const override;

View File

@ -28,5 +28,9 @@ unique_ptr<Block> OnDiskBlockStore::load(const Key &key) {
return OnDiskBlock::LoadFromDisk(_rootdir, key);
}
void OnDiskBlockStore::remove(const Key &key) {
OnDiskBlock::RemoveFromDisk(_rootdir, key);
}
}
}

View File

@ -18,6 +18,7 @@ public:
std::unique_ptr<Block> create(const Key &key, size_t size) override;
std::unique_ptr<Block> load(const Key &key) override;
void remove(const Key &key) override;
private:
const boost::filesystem::path _rootdir;

View File

@ -36,6 +36,11 @@ unique_ptr<Block> FakeBlockStore::load(const Key &key) {
}
}
void FakeBlockStore::remove(const Key &key) {
int numRemoved = _blocks.erase(key.ToString());
assert(numRemoved == 1);
}
unique_ptr<Block> FakeBlockStore::makeFakeBlockFromData(const Key &key, const Data &data) {
auto newdata = make_shared<Data>(data.copy());
_used_dataregions_for_blocks.push_back(newdata);

View File

@ -33,6 +33,7 @@ public:
std::unique_ptr<Block> create(const Key &key, size_t size) override;
std::unique_ptr<Block> load(const Key &key) override;
void remove(const Key &key) override;
void updateData(const Key &key, const Data &data);

View File

@ -19,8 +19,7 @@ public:
//TODO Use boost::optional (if key doesn't exist)
// Return nullptr if block with this key doesn't exists
virtual std::unique_ptr<Block> load(const Key &key) = 0;
//TODO Needed for performance? Or is deleting loaded blocks enough?
//virtual void remove(const std::string &key) = 0;
virtual void remove(const Key &key) = 0;
};
}

View File

@ -24,6 +24,7 @@ public:
return unique_ptr<Block>(do_load(key));
}
MOCK_METHOD1(do_load, Block*(const Key &));
MOCK_METHOD1(remove, void(const Key &));
};
class BlockMock: public Block {

View File

@ -200,6 +200,21 @@ TYPED_TEST_P(BlockStoreTest, TwoCreatedBlocksHaveDifferentKeys) {
EXPECT_NE(block1->key(), block2->key());
}
TYPED_TEST_P(BlockStoreTest, BlockIsNotLoadableAfterDeleting) {
auto blockStore = this->fixture.createBlockStore();
auto blockkey = blockStore->create(1024)->key();
EXPECT_NE(nullptr, blockStore->load(blockkey));
blockStore->remove(blockkey);
EXPECT_EQ(nullptr, blockStore->load(blockkey));
}
TYPED_TEST_P(BlockStoreTest, CrashesWhenTryingToDeleteNonexistingBlock) {
auto blockStore = this->fixture.createBlockStore();
auto blockkey = blockStore->create(1024)->key();
blockStore->remove(blockkey);
EXPECT_DEATH(blockStore->remove(blockkey), "");
}
REGISTER_TYPED_TEST_CASE_P(BlockStoreTest,
CreatedBlockHasCorrectSize,
LoadingUnchangedBlockHasCorrectSize,
@ -213,7 +228,9 @@ REGISTER_TYPED_TEST_CASE_P(BlockStoreTest,
AfterLoad_FlushesWhenDestructed,
LoadNonExistingBlock,
LoadNonExistingBlockWithEmptyKey,
TwoCreatedBlocksHaveDifferentKeys
TwoCreatedBlocksHaveDifferentKeys,
BlockIsNotLoadableAfterDeleting,
CrashesWhenTryingToDeleteNonexistingBlock
);