From 76b3724da409100deec489dceefd4048b156629b Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Sat, 24 Jan 2015 22:27:14 +0100 Subject: [PATCH] Since blocks now store their keys, we don't need to store it somewhere else. --- src/blockstore/interface/BlockStore.h | 3 +- .../helpers/BlockStoreWithRandomKeys.cpp | 11 +++---- .../helpers/BlockStoreWithRandomKeys.h | 4 +-- src/blockstore/utils/BlockStoreUtils.cpp | 4 +-- src/blockstore/utils/BlockStoreUtils.h | 3 +- src/blockstore/utils/BlockWithKey.h | 21 ------------- .../blockstore/testutils/BlockStoreTest.h | 30 +++++++++---------- .../blockstore/utils/BlockStoreUtilsTest.cpp | 16 +++++----- 8 files changed, 35 insertions(+), 57 deletions(-) delete mode 100644 src/blockstore/utils/BlockWithKey.h diff --git a/src/blockstore/interface/BlockStore.h b/src/blockstore/interface/BlockStore.h index 41829c89..1d87d832 100644 --- a/src/blockstore/interface/BlockStore.h +++ b/src/blockstore/interface/BlockStore.h @@ -3,7 +3,6 @@ #define FSPP_BLOCKSTORE_BLOCKSTORE_H_ #include -#include #include #include @@ -16,7 +15,7 @@ class BlockStore { public: virtual ~BlockStore() {} - virtual BlockWithKey create(size_t size) = 0; + virtual std::unique_ptr create(size_t size) = 0; //TODO Use boost::optional (if key doesn't exist) // Return nullptr if block with this key doesn't exists virtual std::unique_ptr load(const Key &key) = 0; diff --git a/src/blockstore/interface/helpers/BlockStoreWithRandomKeys.cpp b/src/blockstore/interface/helpers/BlockStoreWithRandomKeys.cpp index 9bab4786..8962c63c 100644 --- a/src/blockstore/interface/helpers/BlockStoreWithRandomKeys.cpp +++ b/src/blockstore/interface/helpers/BlockStoreWithRandomKeys.cpp @@ -3,17 +3,18 @@ using namespace blockstore; using std::string; +using std::unique_ptr; -BlockWithKey BlockStoreWithRandomKeys::create(size_t size) { - BlockWithKey result = tryCreate(size); - while (!result.block) { +unique_ptr BlockStoreWithRandomKeys::create(size_t size) { + auto result = tryCreate(size); + while (!result) { result = tryCreate(size); } return result; } -BlockWithKey BlockStoreWithRandomKeys::tryCreate(size_t size) { +unique_ptr BlockStoreWithRandomKeys::tryCreate(size_t size) { Key key = Key::CreateRandomKey(); - return BlockWithKey(key, create(key, size)); + return create(key, size); } diff --git a/src/blockstore/interface/helpers/BlockStoreWithRandomKeys.h b/src/blockstore/interface/helpers/BlockStoreWithRandomKeys.h index 81ec82c1..05d639bb 100644 --- a/src/blockstore/interface/helpers/BlockStoreWithRandomKeys.h +++ b/src/blockstore/interface/helpers/BlockStoreWithRandomKeys.h @@ -16,10 +16,10 @@ public: // Return nullptr if key already exists virtual std::unique_ptr create(const Key &key, size_t size) = 0; - BlockWithKey create(size_t size) final; + std::unique_ptr create(size_t size) final; private: - BlockWithKey tryCreate(size_t size); + std::unique_ptr tryCreate(size_t size); }; } diff --git a/src/blockstore/utils/BlockStoreUtils.cpp b/src/blockstore/utils/BlockStoreUtils.cpp index cfd4e247..53671e70 100644 --- a/src/blockstore/utils/BlockStoreUtils.cpp +++ b/src/blockstore/utils/BlockStoreUtils.cpp @@ -8,9 +8,9 @@ using std::unique_ptr; namespace blockstore { namespace utils { -BlockWithKey copyToNewBlock(BlockStore *blockStore, const Block &block) { +unique_ptr copyToNewBlock(BlockStore *blockStore, const Block &block) { auto newBlock = blockStore->create(block.size()); - std::memcpy(newBlock.block->data(), block.data(), block.size()); + std::memcpy(newBlock->data(), block.data(), block.size()); return newBlock; } diff --git a/src/blockstore/utils/BlockStoreUtils.h b/src/blockstore/utils/BlockStoreUtils.h index 5341cecd..d521fae4 100644 --- a/src/blockstore/utils/BlockStoreUtils.h +++ b/src/blockstore/utils/BlockStoreUtils.h @@ -3,14 +3,13 @@ #define BLOCKSTORE_UTILS_BLOCKSTOREUTILS_H_ #include -#include "BlockWithKey.h" namespace blockstore { class BlockStore; class Block; namespace utils { -BlockWithKey copyToNewBlock(BlockStore *blockStore, const Block &block); +std::unique_ptr copyToNewBlock(BlockStore *blockStore, const Block &block); } } diff --git a/src/blockstore/utils/BlockWithKey.h b/src/blockstore/utils/BlockWithKey.h deleted file mode 100644 index f6edc276..00000000 --- a/src/blockstore/utils/BlockWithKey.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once -#ifndef BLOCKSTORE_INTERFACE_BLOCKWITHKEY_H_ -#define BLOCKSTORE_INTERFACE_BLOCKWITHKEY_H_ - -#include -#include -#include "fspp/utils/macros.h" -#include "Key.h" - -namespace blockstore { - -struct BlockWithKey { - BlockWithKey(const Key &key_, std::unique_ptr block_): key(key_), block(std::move(block_)) {} - - Key key; - std::unique_ptr block; -}; - -} - -#endif diff --git a/src/test/blockstore/testutils/BlockStoreTest.h b/src/test/blockstore/testutils/BlockStoreTest.h index 803f56a9..b92cad31 100644 --- a/src/test/blockstore/testutils/BlockStoreTest.h +++ b/src/test/blockstore/testutils/BlockStoreTest.h @@ -31,23 +31,23 @@ public: void TestCreatedBlockHasCorrectSize() { auto block = blockStore->create(size); - EXPECT_EQ(size, block.block->size()); + EXPECT_EQ(size, block->size()); } void TestLoadingUnchangedBlockHasCorrectSize() { auto block = blockStore->create(size); - auto loaded_block = blockStore->load(block.key); + auto loaded_block = blockStore->load(block->key()); EXPECT_EQ(size, loaded_block->size()); } void TestCreatedBlockIsZeroedOut() { auto block = blockStore->create(size); - EXPECT_EQ(0, std::memcmp(ZEROES(size).data(), block.block->data(), size)); + EXPECT_EQ(0, std::memcmp(ZEROES(size).data(), block->data(), size)); } void TestLoadingUnchangedBlockIsZeroedOut() { auto block = blockStore->create(size); - auto loaded_block = blockStore->load(block.key); + auto loaded_block = blockStore->load(block->key()); EXPECT_EQ(0, std::memcmp(ZEROES(size).data(), loaded_block->data(), size)); } @@ -88,8 +88,8 @@ public: blockstore::Key key = key; { auto block = blockStore->create(size); - key = block.key; - WriteDataToBlock(block.block.get(), randomData); + key = block->key(); + WriteDataToBlock(block.get(), randomData); } auto loaded_block = blockStore->load(key); EXPECT_BLOCK_DATA_CORRECT(*loaded_block, randomData); @@ -99,7 +99,7 @@ public: DataBlockFixture randomData(size); blockstore::Key key = key; { - key = blockStore->create(size).key; + key = blockStore->create(size)->key(); auto block = blockStore->load(key); WriteDataToBlock(block.get(), randomData); } @@ -137,24 +137,24 @@ private: blockstore::Key StoreDataToBlockAndGetKey(const DataBlockFixture &data) { auto block = blockStore->create(data.size()); - std::memcpy(block.block->data(), data.data(), data.size()); - return block.key; + std::memcpy(block->data(), data.data(), data.size()); + return block->key(); } std::unique_ptr StoreDataToBlockAndLoadItDirectlyAfterFlushing(const DataBlockFixture &data) { auto block = blockStore->create(data.size()); - std::memcpy(block.block->data(), data.data(), data.size()); - block.block->flush(); - return blockStore->load(block.key); + std::memcpy(block->data(), data.data(), data.size()); + block->flush(); + return blockStore->load(block->key()); } std::unique_ptr CreateBlockAndLoadIt() { - blockstore::Key key = blockStore->create(size).key; + blockstore::Key key = blockStore->create(size)->key(); return blockStore->load(key); } std::unique_ptr CreateBlock() { - return blockStore->create(size).block; + return blockStore->create(size); } void WriteDataToBlock(blockstore::Block *block, const DataBlockFixture &randomData) { @@ -195,7 +195,7 @@ TYPED_TEST_P(BlockStoreTest, TwoCreatedBlocksHaveDifferentKeys) { auto blockStore = this->fixture.createBlockStore(); auto block1 = blockStore->create(1024); auto block2 = blockStore->create(1024); - EXPECT_NE(block1.key, block2.key); + EXPECT_NE(block1->key(), block2->key()); } REGISTER_TYPED_TEST_CASE_P(BlockStoreTest, diff --git a/src/test/blockstore/utils/BlockStoreUtilsTest.cpp b/src/test/blockstore/utils/BlockStoreUtilsTest.cpp index b25837fc..9845b696 100644 --- a/src/test/blockstore/utils/BlockStoreUtilsTest.cpp +++ b/src/test/blockstore/utils/BlockStoreUtilsTest.cpp @@ -35,32 +35,32 @@ public: TEST_F(BlockStoreUtilsTest, CopyEmptyBlock) { auto block = blockStore->create(0); - auto block2 = copyToNewBlock(blockStore.get(), *block.block); + auto block2 = copyToNewBlock(blockStore.get(), *block); - EXPECT_EQ(0u, block2.block->size()); + EXPECT_EQ(0u, block2->size()); } TEST_F(BlockStoreUtilsTest, CopyZeroBlock) { - auto block = blockStore->create(SIZE).block; - auto block2 = copyToNewBlock(blockStore.get(), *block).block; + auto block = blockStore->create(SIZE); + auto block2 = copyToNewBlock(blockStore.get(), *block); EXPECT_EQ(SIZE, block2->size()); EXPECT_EQ(0, std::memcmp(ZEROES.data(), block2->data(), SIZE)); } TEST_F(BlockStoreUtilsTest, CopyDataBlock) { - auto block = blockStore->create(SIZE).block; + auto block = blockStore->create(SIZE); std::memcpy(block->data(), dataFixture.data(), SIZE); - auto block2 = copyToNewBlock(blockStore.get(), *block).block; + auto block2 = copyToNewBlock(blockStore.get(), *block); EXPECT_EQ(SIZE, block2->size()); EXPECT_EQ(0, std::memcmp(dataFixture.data(), block2->data(), SIZE)); } TEST_F(BlockStoreUtilsTest, OriginalBlockUnchanged) { - auto block = blockStore->create(SIZE).block; + auto block = blockStore->create(SIZE); std::memcpy(block->data(), dataFixture.data(), SIZE); - auto block2 = copyToNewBlock(blockStore.get(), *block).block; + auto block2 = copyToNewBlock(blockStore.get(), *block); EXPECT_EQ(SIZE, block->size()); EXPECT_EQ(0, std::memcmp(dataFixture.data(), block->data(), SIZE));