Added Block::write() and removed writeable data() pointer

This commit is contained in:
Sebastian Messmer 2015-03-04 20:47:02 +01:00
parent 09b648317f
commit d42bda0ca6
13 changed files with 39 additions and 26 deletions

View File

@ -25,12 +25,13 @@ InMemoryBlock::InMemoryBlock(const InMemoryBlock &rhs)
InMemoryBlock::~InMemoryBlock() {
}
void *InMemoryBlock::data() {
const void *InMemoryBlock::data() const {
return _data->data();
}
const void *InMemoryBlock::data() const {
return _data->data();
void InMemoryBlock::write(const void *source, uint64_t offset, uint64_t size) {
assert(offset <= _data->size() && offset + size <= _data->size()); //Also check offset < _data->size() because of possible overflow in the addition
std::memcpy((uint8_t*)_data->data()+offset, source, size);
}
size_t InMemoryBlock::size() const {

View File

@ -15,8 +15,8 @@ public:
InMemoryBlock(const InMemoryBlock &rhs);
virtual ~InMemoryBlock();
void *data() override;
const void *data() const override;
void write(const void *source, uint64_t offset, uint64_t size) override;
void flush() override;

View File

@ -31,12 +31,13 @@ OnDiskBlock::~OnDiskBlock() {
_storeToDisk();
}
void *OnDiskBlock::data() {
const void *OnDiskBlock::data() const {
return _data.data();
}
const void *OnDiskBlock::data() const {
return _data.data();
void OnDiskBlock::write(const void *source, uint64_t offset, uint64_t size) {
assert(offset <= _data.size() && offset + size <= _data.size()); //Also check offset < _data->size() because of possible overflow in the addition
std::memcpy((uint8_t*)_data.data()+offset, source, size);
}
size_t OnDiskBlock::size() const {

View File

@ -21,8 +21,8 @@ public:
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;
void write(const void *source, uint64_t offset, uint64_t size) override;
void flush() override;

View File

@ -22,12 +22,13 @@ FakeBlock::~FakeBlock() {
flush();
}
void *FakeBlock::data() {
const void *FakeBlock::data() const {
return _data->data();
}
const void *FakeBlock::data() const {
return _data->data();
void FakeBlock::write(const void *source, uint64_t offset, uint64_t size) {
assert(offset <= _data->size() && offset + size <= _data->size()); //Also check offset < _data->size() because of possible overflow in the addition
std::memcpy((uint8_t*)_data->data()+offset, source, size);
}
size_t FakeBlock::size() const {

View File

@ -16,8 +16,8 @@ public:
FakeBlock(FakeBlockStore *store, const Key &key, std::shared_ptr<Data> data);
virtual ~FakeBlock();
void *data() override;
const void *data() const override;
void write(const void *source, uint64_t offset, uint64_t size) override;
void flush() override;

View File

@ -11,8 +11,8 @@ class Block {
public:
virtual ~Block() {}
virtual void *data() = 0;
virtual const void *data() const = 0;
virtual void write(const void *source, uint64_t offset, uint64_t size) = 0;
virtual void flush() = 0;

View File

@ -47,7 +47,7 @@ public:
}
void WriteDataToBlock(const unique_ptr<OnDiskBlock> &block) {
std::memcpy(block->data(), randomData.data(), randomData.size());
block->write(randomData.data(), 0, randomData.size());
}
void EXPECT_BLOCK_DATA_CORRECT(const unique_ptr<OnDiskBlock> &block) {

View File

@ -31,8 +31,8 @@ public:
class BlockMock: public Block {
public:
BlockMock(): Block(Key::CreateRandomKey()) {}
MOCK_METHOD0(data, void*());
MOCK_CONST_METHOD0(data, const void*());
MOCK_METHOD3(write, void(const void*, uint64_t, uint64_t));
MOCK_METHOD0(flush, void());
MOCK_CONST_METHOD0(size, size_t());
MOCK_CONST_METHOD0(key, const Key&());

View File

@ -139,13 +139,13 @@ private:
blockstore::Key StoreDataToBlockAndGetKey(const DataBlockFixture &data) {
auto block = blockStore->create(data.size());
std::memcpy(block->data(), data.data(), data.size());
block->write(data.data(), 0, data.size());
return block->key();
}
std::unique_ptr<blockstore::Block> StoreDataToBlockAndLoadItDirectlyAfterFlushing(const DataBlockFixture &data) {
auto block = blockStore->create(data.size());
std::memcpy(block->data(), data.data(), data.size());
block->write(data.data(), 0, data.size());
block->flush();
return blockStore->load(block->key());
}
@ -160,7 +160,7 @@ private:
}
void WriteDataToBlock(blockstore::Block *block, const DataBlockFixture &randomData) {
std::memcpy(block->data(), randomData.data(), randomData.size());
block->write(randomData.data(), 0, randomData.size());
}
void EXPECT_BLOCK_DATA_CORRECT(const blockstore::Block &block, const DataBlockFixture &randomData) {

View File

@ -51,7 +51,7 @@ TEST_F(BlockStoreUtilsTest_CopyToNewBlock, CopyZeroBlock) {
TEST_F(BlockStoreUtilsTest_CopyToNewBlock, CopyDataBlock) {
auto block = blockStore->create(SIZE);
std::memcpy(block->data(), dataFixture.data(), SIZE);
block->write(dataFixture.data(), 0, SIZE);
auto block2 = copyToNewBlock(blockStore.get(), *block);
EXPECT_EQ(SIZE, block2->size());
@ -60,7 +60,7 @@ TEST_F(BlockStoreUtilsTest_CopyToNewBlock, CopyDataBlock) {
TEST_F(BlockStoreUtilsTest_CopyToNewBlock, OriginalBlockUnchanged) {
auto block = blockStore->create(SIZE);
std::memcpy(block->data(), dataFixture.data(), SIZE);
block->write(dataFixture.data(), 0, SIZE);
auto block2 = copyToNewBlock(blockStore.get(), *block);
EXPECT_EQ(SIZE, block->size());
@ -78,7 +78,7 @@ TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, CopyEmptyBlock) {
TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, CopyZeroBlock) {
auto block = blockStore->create(SIZE);
auto block2 = blockStore->create(SIZE);
std::memcpy(block2->data(), dataFixture.data(), SIZE);
block2->write(dataFixture.data(), 0, SIZE);
copyTo(block2.get(), *block);
EXPECT_EQ(0, std::memcmp(ZEROES.data(), block2->data(), SIZE));
@ -86,7 +86,7 @@ TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, CopyZeroBlock) {
TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, CopyDataBlock) {
auto block = blockStore->create(SIZE);
std::memcpy(block->data(), dataFixture.data(), SIZE);
block->write(dataFixture.data(), 0, SIZE);
auto block2 = blockStore->create(SIZE);
copyTo(block2.get(), *block);
@ -95,7 +95,7 @@ TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, CopyDataBlock) {
TEST_F(BlockStoreUtilsTest_CopyToExistingBlock, OriginalBlockUnchanged) {
auto block = blockStore->create(SIZE);
std::memcpy(block->data(), dataFixture.data(), SIZE);
block->write(dataFixture.data(), 0, SIZE);
auto block2 = blockStore->create(SIZE);
copyTo(block2.get(), *block);

View File

@ -1,5 +1,6 @@
#include <messmer/blockstore/interface/BlockStore.h>
#include <messmer/blockstore/utils/BlockStoreUtils.h>
#include "../interface/BlockStore.h"
#include "BlockStoreUtils.h"
#include "Data.h"
#include <memory>
#include <cassert>
@ -16,7 +17,13 @@ unique_ptr<Block> copyToNewBlock(BlockStore *blockStore, const Block &block) {
void copyTo(Block *target, const Block &source) {
assert(target->size() == source.size());
std::memcpy(target->data(), source.data(), source.size());
target->write(source.data(), 0, source.size());
}
void fillWithZeroes(Block *target) {
Data zeroes(target->size());
zeroes.FillWithZeroes();
target->write(zeroes.data(), 0, target->size());
}
}

View File

@ -11,6 +11,9 @@ namespace utils {
std::unique_ptr<Block> copyToNewBlock(BlockStore *blockStore, const Block &block);
void copyTo(Block *target, const Block &source);
void fillWithZeroes(Block *target);
//TODO Test fillWithZeroes
}
}