Introduce Data::copy() and test case for it, refactor FakeBlobStore using Data::copy()
This commit is contained in:
parent
15b10feeaf
commit
0d6dcd5b8b
@ -15,8 +15,7 @@ FakeBlockStore::FakeBlockStore()
|
|||||||
: _blocks(), _used_dataregions_for_blocks() {}
|
: _blocks(), _used_dataregions_for_blocks() {}
|
||||||
|
|
||||||
unique_ptr<Block> FakeBlockStore::create(const Key &key, size_t size) {
|
unique_ptr<Block> FakeBlockStore::create(const Key &key, size_t size) {
|
||||||
string key_string = key.AsString();
|
auto insert_result = _blocks.emplace(key.AsString(), size);
|
||||||
auto insert_result = _blocks.emplace(key_string, size);
|
|
||||||
insert_result.first->second.FillWithZeroes();
|
insert_result.first->second.FillWithZeroes();
|
||||||
|
|
||||||
if (!insert_result.second) {
|
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
|
//Return a copy of the stored data
|
||||||
_used_dataregions_for_blocks.push_back(make_shared<Data>(size));
|
return load(key);
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unique_ptr<Block> FakeBlockStore::load(const Key &key) {
|
unique_ptr<Block> FakeBlockStore::load(const Key &key) {
|
||||||
//Return a copy of the stored data
|
//Return a copy of the stored data
|
||||||
string key_string = key.AsString();
|
string key_string = key.AsString();
|
||||||
try {
|
try {
|
||||||
const Data &data = _blocks.at(key_string);
|
return makeFakeBlockFromData(key_string, _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());
|
|
||||||
} catch (const std::out_of_range &e) {
|
} catch (const std::out_of_range &e) {
|
||||||
return nullptr;
|
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) {
|
void FakeBlockStore::updateData(const std::string &key, const Data &data) {
|
||||||
Data &stored_data = _blocks.at(key);
|
Data &stored_data = _blocks.at(key);
|
||||||
assert(data.size() == stored_data.size());
|
assert(data.size() == stored_data.size());
|
||||||
|
@ -46,6 +46,8 @@ private:
|
|||||||
//We want to avoid this for the reasons mentioned above (overflow data).
|
//We want to avoid this for the reasons mentioned above (overflow data).
|
||||||
std::vector<std::shared_ptr<Data>> _used_dataregions_for_blocks;
|
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);
|
DISALLOW_COPY_AND_ASSIGN(FakeBlockStore);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -34,6 +34,12 @@ Data::~Data() {
|
|||||||
_data = nullptr;
|
_data = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Data Data::copy() const {
|
||||||
|
Data copy(_size);
|
||||||
|
std::memcpy(copy._data, _data, _size);
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
void *Data::data() {
|
void *Data::data() {
|
||||||
return const_cast<void*>(const_cast<const Data*>(this)->data());
|
return const_cast<void*>(const_cast<const Data*>(this)->data());
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,8 @@ public:
|
|||||||
Data(Data &&rhs); // move constructor
|
Data(Data &&rhs); // move constructor
|
||||||
virtual ~Data();
|
virtual ~Data();
|
||||||
|
|
||||||
|
Data copy() const;
|
||||||
|
|
||||||
void *data();
|
void *data();
|
||||||
const void *data() const;
|
const void *data() const;
|
||||||
|
|
||||||
|
@ -125,6 +125,15 @@ TEST_P(DataTestWithSizeParam, StoreAndLoad) {
|
|||||||
EXPECT_DATA_CORRECT(loaded_data);
|
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) {
|
TEST_F(DataTest, InitializeWithZeroes) {
|
||||||
Data data(10*1024);
|
Data data(10*1024);
|
||||||
data.FillWithZeroes();
|
data.FillWithZeroes();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user