Since blocks now store their keys, we don't need to store it somewhere else.

This commit is contained in:
Sebastian Messmer 2015-01-24 22:27:14 +01:00
parent 196b543cbb
commit 76b3724da4
8 changed files with 35 additions and 57 deletions

View File

@ -3,7 +3,6 @@
#define FSPP_BLOCKSTORE_BLOCKSTORE_H_ #define FSPP_BLOCKSTORE_BLOCKSTORE_H_
#include <blockstore/interface/Block.h> #include <blockstore/interface/Block.h>
#include <blockstore/utils/BlockWithKey.h>
#include <string> #include <string>
#include <memory> #include <memory>
@ -16,7 +15,7 @@ class BlockStore {
public: public:
virtual ~BlockStore() {} virtual ~BlockStore() {}
virtual BlockWithKey create(size_t size) = 0; virtual std::unique_ptr<Block> create(size_t size) = 0;
//TODO Use boost::optional (if key doesn't exist) //TODO Use boost::optional (if key doesn't exist)
// Return nullptr if block with this key doesn't exists // Return nullptr if block with this key doesn't exists
virtual std::unique_ptr<Block> load(const Key &key) = 0; virtual std::unique_ptr<Block> load(const Key &key) = 0;

View File

@ -3,17 +3,18 @@
using namespace blockstore; using namespace blockstore;
using std::string; using std::string;
using std::unique_ptr;
BlockWithKey BlockStoreWithRandomKeys::create(size_t size) { unique_ptr<Block> BlockStoreWithRandomKeys::create(size_t size) {
BlockWithKey result = tryCreate(size); auto result = tryCreate(size);
while (!result.block) { while (!result) {
result = tryCreate(size); result = tryCreate(size);
} }
return result; return result;
} }
BlockWithKey BlockStoreWithRandomKeys::tryCreate(size_t size) { unique_ptr<Block> BlockStoreWithRandomKeys::tryCreate(size_t size) {
Key key = Key::CreateRandomKey(); Key key = Key::CreateRandomKey();
return BlockWithKey(key, create(key, size)); return create(key, size);
} }

View File

@ -16,10 +16,10 @@ public:
// Return nullptr if key already exists // Return nullptr if key already exists
virtual std::unique_ptr<Block> create(const Key &key, size_t size) = 0; virtual std::unique_ptr<Block> create(const Key &key, size_t size) = 0;
BlockWithKey create(size_t size) final; std::unique_ptr<Block> create(size_t size) final;
private: private:
BlockWithKey tryCreate(size_t size); std::unique_ptr<Block> tryCreate(size_t size);
}; };
} }

View File

@ -8,9 +8,9 @@ using std::unique_ptr;
namespace blockstore { namespace blockstore {
namespace utils { namespace utils {
BlockWithKey copyToNewBlock(BlockStore *blockStore, const Block &block) { unique_ptr<Block> copyToNewBlock(BlockStore *blockStore, const Block &block) {
auto newBlock = blockStore->create(block.size()); 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; return newBlock;
} }

View File

@ -3,14 +3,13 @@
#define BLOCKSTORE_UTILS_BLOCKSTOREUTILS_H_ #define BLOCKSTORE_UTILS_BLOCKSTOREUTILS_H_
#include <memory> #include <memory>
#include "BlockWithKey.h"
namespace blockstore { namespace blockstore {
class BlockStore; class BlockStore;
class Block; class Block;
namespace utils { namespace utils {
BlockWithKey copyToNewBlock(BlockStore *blockStore, const Block &block); std::unique_ptr<Block> copyToNewBlock(BlockStore *blockStore, const Block &block);
} }
} }

View File

@ -1,21 +0,0 @@
#pragma once
#ifndef BLOCKSTORE_INTERFACE_BLOCKWITHKEY_H_
#define BLOCKSTORE_INTERFACE_BLOCKWITHKEY_H_
#include <blockstore/interface/Block.h>
#include <memory>
#include "fspp/utils/macros.h"
#include "Key.h"
namespace blockstore {
struct BlockWithKey {
BlockWithKey(const Key &key_, std::unique_ptr<Block> block_): key(key_), block(std::move(block_)) {}
Key key;
std::unique_ptr<Block> block;
};
}
#endif

View File

@ -31,23 +31,23 @@ public:
void TestCreatedBlockHasCorrectSize() { void TestCreatedBlockHasCorrectSize() {
auto block = blockStore->create(size); auto block = blockStore->create(size);
EXPECT_EQ(size, block.block->size()); EXPECT_EQ(size, block->size());
} }
void TestLoadingUnchangedBlockHasCorrectSize() { void TestLoadingUnchangedBlockHasCorrectSize() {
auto block = blockStore->create(size); 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()); EXPECT_EQ(size, loaded_block->size());
} }
void TestCreatedBlockIsZeroedOut() { void TestCreatedBlockIsZeroedOut() {
auto block = blockStore->create(size); 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() { void TestLoadingUnchangedBlockIsZeroedOut() {
auto block = blockStore->create(size); 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)); EXPECT_EQ(0, std::memcmp(ZEROES(size).data(), loaded_block->data(), size));
} }
@ -88,8 +88,8 @@ public:
blockstore::Key key = key; blockstore::Key key = key;
{ {
auto block = blockStore->create(size); auto block = blockStore->create(size);
key = block.key; key = block->key();
WriteDataToBlock(block.block.get(), randomData); WriteDataToBlock(block.get(), randomData);
} }
auto loaded_block = blockStore->load(key); auto loaded_block = blockStore->load(key);
EXPECT_BLOCK_DATA_CORRECT(*loaded_block, randomData); EXPECT_BLOCK_DATA_CORRECT(*loaded_block, randomData);
@ -99,7 +99,7 @@ public:
DataBlockFixture randomData(size); DataBlockFixture randomData(size);
blockstore::Key key = key; blockstore::Key key = key;
{ {
key = blockStore->create(size).key; key = blockStore->create(size)->key();
auto block = blockStore->load(key); auto block = blockStore->load(key);
WriteDataToBlock(block.get(), randomData); WriteDataToBlock(block.get(), randomData);
} }
@ -137,24 +137,24 @@ private:
blockstore::Key StoreDataToBlockAndGetKey(const DataBlockFixture &data) { blockstore::Key StoreDataToBlockAndGetKey(const DataBlockFixture &data) {
auto block = blockStore->create(data.size()); auto block = blockStore->create(data.size());
std::memcpy(block.block->data(), data.data(), data.size()); std::memcpy(block->data(), data.data(), data.size());
return block.key; return block->key();
} }
std::unique_ptr<blockstore::Block> StoreDataToBlockAndLoadItDirectlyAfterFlushing(const DataBlockFixture &data) { std::unique_ptr<blockstore::Block> StoreDataToBlockAndLoadItDirectlyAfterFlushing(const DataBlockFixture &data) {
auto block = blockStore->create(data.size()); auto block = blockStore->create(data.size());
std::memcpy(block.block->data(), data.data(), data.size()); std::memcpy(block->data(), data.data(), data.size());
block.block->flush(); block->flush();
return blockStore->load(block.key); return blockStore->load(block->key());
} }
std::unique_ptr<blockstore::Block> CreateBlockAndLoadIt() { std::unique_ptr<blockstore::Block> CreateBlockAndLoadIt() {
blockstore::Key key = blockStore->create(size).key; blockstore::Key key = blockStore->create(size)->key();
return blockStore->load(key); return blockStore->load(key);
} }
std::unique_ptr<blockstore::Block> CreateBlock() { std::unique_ptr<blockstore::Block> CreateBlock() {
return blockStore->create(size).block; return blockStore->create(size);
} }
void WriteDataToBlock(blockstore::Block *block, const DataBlockFixture &randomData) { void WriteDataToBlock(blockstore::Block *block, const DataBlockFixture &randomData) {
@ -195,7 +195,7 @@ TYPED_TEST_P(BlockStoreTest, TwoCreatedBlocksHaveDifferentKeys) {
auto blockStore = this->fixture.createBlockStore(); auto blockStore = this->fixture.createBlockStore();
auto block1 = blockStore->create(1024); auto block1 = blockStore->create(1024);
auto block2 = 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, REGISTER_TYPED_TEST_CASE_P(BlockStoreTest,

View File

@ -35,32 +35,32 @@ public:
TEST_F(BlockStoreUtilsTest, CopyEmptyBlock) { TEST_F(BlockStoreUtilsTest, CopyEmptyBlock) {
auto block = blockStore->create(0); 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) { TEST_F(BlockStoreUtilsTest, CopyZeroBlock) {
auto block = blockStore->create(SIZE).block; auto block = blockStore->create(SIZE);
auto block2 = copyToNewBlock(blockStore.get(), *block).block; auto block2 = copyToNewBlock(blockStore.get(), *block);
EXPECT_EQ(SIZE, block2->size()); EXPECT_EQ(SIZE, block2->size());
EXPECT_EQ(0, std::memcmp(ZEROES.data(), block2->data(), SIZE)); EXPECT_EQ(0, std::memcmp(ZEROES.data(), block2->data(), SIZE));
} }
TEST_F(BlockStoreUtilsTest, CopyDataBlock) { TEST_F(BlockStoreUtilsTest, CopyDataBlock) {
auto block = blockStore->create(SIZE).block; auto block = blockStore->create(SIZE);
std::memcpy(block->data(), dataFixture.data(), 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(SIZE, block2->size());
EXPECT_EQ(0, std::memcmp(dataFixture.data(), block2->data(), SIZE)); EXPECT_EQ(0, std::memcmp(dataFixture.data(), block2->data(), SIZE));
} }
TEST_F(BlockStoreUtilsTest, OriginalBlockUnchanged) { TEST_F(BlockStoreUtilsTest, OriginalBlockUnchanged) {
auto block = blockStore->create(SIZE).block; auto block = blockStore->create(SIZE);
std::memcpy(block->data(), dataFixture.data(), 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(SIZE, block->size());
EXPECT_EQ(0, std::memcmp(dataFixture.data(), block->data(), SIZE)); EXPECT_EQ(0, std::memcmp(dataFixture.data(), block->data(), SIZE));