From 0d6dcd5b8b2a60e2b3b6378dfd6e4c9a4d57be79 Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Thu, 11 Dec 2014 01:41:08 +0100 Subject: [PATCH] Introduce Data::copy() and test case for it, refactor FakeBlobStore using Data::copy() --- .../testfake/FakeBlockStore.cpp | 18 +++++++++--------- .../implementations/testfake/FakeBlockStore.h | 2 ++ src/blockstore/utils/Data.cpp | 6 ++++++ src/blockstore/utils/Data.h | 2 ++ src/test/blockstore/utils/DataTest.cpp | 9 +++++++++ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/blockstore/implementations/testfake/FakeBlockStore.cpp b/src/blockstore/implementations/testfake/FakeBlockStore.cpp index 5f969af8..2d7cc306 100644 --- a/src/blockstore/implementations/testfake/FakeBlockStore.cpp +++ b/src/blockstore/implementations/testfake/FakeBlockStore.cpp @@ -15,8 +15,7 @@ FakeBlockStore::FakeBlockStore() : _blocks(), _used_dataregions_for_blocks() {} unique_ptr FakeBlockStore::create(const Key &key, size_t size) { - string key_string = key.AsString(); - auto insert_result = _blocks.emplace(key_string, size); + auto insert_result = _blocks.emplace(key.AsString(), size); insert_result.first->second.FillWithZeroes(); if (!insert_result.second) { @@ -24,24 +23,25 @@ unique_ptr FakeBlockStore::create(const Key &key, size_t size) { } //Return a copy of the stored data - _used_dataregions_for_blocks.push_back(make_shared(size)); - std::memcpy(_used_dataregions_for_blocks.back()->data(), insert_result.first->second.data(), size); - return make_unique(this, key_string, _used_dataregions_for_blocks.back()); + return load(key); } unique_ptr FakeBlockStore::load(const Key &key) { //Return a copy of the stored data string key_string = key.AsString(); try { - const Data &data = _blocks.at(key_string); - _used_dataregions_for_blocks.push_back(make_shared(data.size())); - std::memcpy(_used_dataregions_for_blocks.back()->data(), data.data(), data.size()); - return make_unique(this, key_string, _used_dataregions_for_blocks.back()); + return makeFakeBlockFromData(key_string, _blocks.at(key_string)); } catch (const std::out_of_range &e) { return nullptr; } } +unique_ptr FakeBlockStore::makeFakeBlockFromData(const std::string &key, const Data &data) { + auto newdata = make_shared(data.copy()); + _used_dataregions_for_blocks.push_back(newdata); + return make_unique(this, key, newdata); +} + void FakeBlockStore::updateData(const std::string &key, const Data &data) { Data &stored_data = _blocks.at(key); assert(data.size() == stored_data.size()); diff --git a/src/blockstore/implementations/testfake/FakeBlockStore.h b/src/blockstore/implementations/testfake/FakeBlockStore.h index 51510ab6..aec0c223 100644 --- a/src/blockstore/implementations/testfake/FakeBlockStore.h +++ b/src/blockstore/implementations/testfake/FakeBlockStore.h @@ -46,6 +46,8 @@ private: //We want to avoid this for the reasons mentioned above (overflow data). std::vector> _used_dataregions_for_blocks; + std::unique_ptr makeFakeBlockFromData(const std::string &key, const Data &data); + DISALLOW_COPY_AND_ASSIGN(FakeBlockStore); }; diff --git a/src/blockstore/utils/Data.cpp b/src/blockstore/utils/Data.cpp index db4ec5df..37bd3ff9 100644 --- a/src/blockstore/utils/Data.cpp +++ b/src/blockstore/utils/Data.cpp @@ -34,6 +34,12 @@ Data::~Data() { _data = nullptr; } +Data Data::copy() const { + Data copy(_size); + std::memcpy(copy._data, _data, _size); + return copy; +} + void *Data::data() { return const_cast(const_cast(this)->data()); } diff --git a/src/blockstore/utils/Data.h b/src/blockstore/utils/Data.h index 606aeada..29345574 100644 --- a/src/blockstore/utils/Data.h +++ b/src/blockstore/utils/Data.h @@ -17,6 +17,8 @@ public: Data(Data &&rhs); // move constructor virtual ~Data(); + Data copy() const; + void *data(); const void *data() const; diff --git a/src/test/blockstore/utils/DataTest.cpp b/src/test/blockstore/utils/DataTest.cpp index 3060d472..4bd7f199 100644 --- a/src/test/blockstore/utils/DataTest.cpp +++ b/src/test/blockstore/utils/DataTest.cpp @@ -125,6 +125,15 @@ TEST_P(DataTestWithSizeParam, StoreAndLoad) { EXPECT_DATA_CORRECT(loaded_data); } +TEST_P(DataTestWithSizeParam, Copy) { + Data data(GetParam()); + FillData(&data); + + Data copy = data.copy(); + + EXPECT_DATA_CORRECT(copy); +} + TEST_F(DataTest, InitializeWithZeroes) { Data data(10*1024); data.FillWithZeroes();