Introduce Data::copy() and test case for it, refactor FakeBlobStore using Data::copy()

This commit is contained in:
Sebastian Messmer 2014-12-11 01:41:08 +01:00
parent 15b10feeaf
commit 0d6dcd5b8b
5 changed files with 28 additions and 9 deletions

View File

@ -15,8 +15,7 @@ FakeBlockStore::FakeBlockStore()
: _blocks(), _used_dataregions_for_blocks() {}
unique_ptr<Block> 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<Block> FakeBlockStore::create(const Key &key, size_t size) {
}
//Return a copy of the stored data
_used_dataregions_for_blocks.push_back(make_shared<Data>(size));
std::memcpy(_used_dataregions_for_blocks.back()->data(), insert_result.first->second.data(), size);
return make_unique<FakeBlock>(this, key_string, _used_dataregions_for_blocks.back());
return load(key);
}
unique_ptr<Block> 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>(data.size()));
std::memcpy(_used_dataregions_for_blocks.back()->data(), data.data(), data.size());
return make_unique<FakeBlock>(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<Block> FakeBlockStore::makeFakeBlockFromData(const std::string &key, const Data &data) {
auto newdata = make_shared<Data>(data.copy());
_used_dataregions_for_blocks.push_back(newdata);
return make_unique<FakeBlock>(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());

View File

@ -46,6 +46,8 @@ private:
//We want to avoid this for the reasons mentioned above (overflow data).
std::vector<std::shared_ptr<Data>> _used_dataregions_for_blocks;
std::unique_ptr<Block> makeFakeBlockFromData(const std::string &key, const Data &data);
DISALLOW_COPY_AND_ASSIGN(FakeBlockStore);
};

View File

@ -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<void*>(const_cast<const Data*>(this)->data());
}

View File

@ -17,6 +17,8 @@ public:
Data(Data &&rhs); // move constructor
virtual ~Data();
Data copy() const;
void *data();
const void *data() const;

View File

@ -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();