Add Block::resize()
This commit is contained in:
parent
9d4165d2db
commit
337c338c61
@ -34,6 +34,10 @@ size_t CachedBlock::size() const {
|
||||
return _baseBlock->size();
|
||||
}
|
||||
|
||||
void CachedBlock::resize(size_t newSize) {
|
||||
return _baseBlock->resize(newSize);
|
||||
}
|
||||
|
||||
unique_ref<Block> CachedBlock::releaseBlock() {
|
||||
return std::move(_baseBlock);
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ public:
|
||||
|
||||
size_t size() const override;
|
||||
|
||||
void resize(size_t newSize) override;
|
||||
|
||||
cpputils::unique_ref<Block> releaseBlock();
|
||||
|
||||
private:
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "NewBlock.h"
|
||||
#include "CachingBlockStore.h"
|
||||
#include <messmer/cpp-utils/assert/assert.h>
|
||||
#include <messmer/cpp-utils/data/DataUtils.h>
|
||||
|
||||
using cpputils::Data;
|
||||
using boost::none;
|
||||
@ -61,6 +62,11 @@ size_t NewBlock::size() const {
|
||||
return _data.size();
|
||||
}
|
||||
|
||||
void NewBlock::resize(size_t newSize) {
|
||||
_data = cpputils::DataUtils::resize(std::move(_data), newSize);
|
||||
_dataChanged = true;
|
||||
}
|
||||
|
||||
bool NewBlock::alreadyExistsInBaseStore() const {
|
||||
return _baseBlock != none;
|
||||
}
|
||||
|
@ -29,6 +29,8 @@ public:
|
||||
|
||||
size_t size() const override;
|
||||
|
||||
void resize(size_t newSize) override;
|
||||
|
||||
void remove();
|
||||
|
||||
bool alreadyExistsInBaseStore() const;
|
||||
|
@ -12,6 +12,7 @@
|
||||
#include <boost/optional.hpp>
|
||||
#include <messmer/cpp-utils/crypto/symmetric/Cipher.h>
|
||||
#include <messmer/cpp-utils/assert/assert.h>
|
||||
#include <messmer/cpp-utils/data/DataUtils.h>
|
||||
#include <mutex>
|
||||
#include <messmer/cpp-utils/logging/logging.h>
|
||||
|
||||
@ -21,6 +22,8 @@ template<class Cipher> class EncryptedBlockStore;
|
||||
|
||||
//TODO Test EncryptedBlock
|
||||
|
||||
//TODO Fix mutexes & locks (basically true for all blockstores)
|
||||
|
||||
template<class Cipher>
|
||||
class EncryptedBlock final: public Block {
|
||||
public:
|
||||
@ -37,11 +40,12 @@ public:
|
||||
void flush() override;
|
||||
|
||||
size_t size() const override;
|
||||
void resize(size_t newSize) override;
|
||||
|
||||
cpputils::unique_ref<Block> releaseBlock();
|
||||
|
||||
private:
|
||||
cpputils::unique_ref<Block> _baseBlock;
|
||||
cpputils::unique_ref<Block> _baseBlock; // TODO Do I need the ciphertext block in memory or is the key enough?
|
||||
cpputils::Data _plaintextWithHeader;
|
||||
typename Cipher::EncryptionKey _encKey;
|
||||
bool _dataChanged;
|
||||
@ -145,6 +149,12 @@ size_t EncryptedBlock<Cipher>::size() const {
|
||||
return _plaintextWithHeader.size() - HEADER_LENGTH;
|
||||
}
|
||||
|
||||
template<class Cipher>
|
||||
void EncryptedBlock<Cipher>::resize(size_t newSize) {
|
||||
_plaintextWithHeader = cpputils::DataUtils::resize(std::move(_plaintextWithHeader), newSize + HEADER_LENGTH);
|
||||
_dataChanged = true;
|
||||
}
|
||||
|
||||
template<class Cipher>
|
||||
void EncryptedBlock<Cipher>::_encryptToBaseBlock() {
|
||||
if (_dataChanged) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "InMemoryBlock.h"
|
||||
#include "InMemoryBlockStore.h"
|
||||
#include <cstring>
|
||||
#include <messmer/cpp-utils/data/DataUtils.h>
|
||||
#include <messmer/cpp-utils/assert/assert.h>
|
||||
|
||||
using std::make_shared;
|
||||
@ -38,6 +39,10 @@ size_t InMemoryBlock::size() const {
|
||||
return _data->size();
|
||||
}
|
||||
|
||||
void InMemoryBlock::resize(size_t newSize) {
|
||||
*_data = cpputils::DataUtils::resize(std::move(*_data), newSize);
|
||||
}
|
||||
|
||||
void InMemoryBlock::flush() {
|
||||
}
|
||||
|
||||
|
@ -21,6 +21,7 @@ public:
|
||||
void flush() override;
|
||||
|
||||
size_t size() const override;
|
||||
void resize(size_t newSize) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<cpputils::Data> _data;
|
||||
|
@ -4,6 +4,7 @@
|
||||
#include "OnDiskBlock.h"
|
||||
#include "OnDiskBlockStore.h"
|
||||
#include "../../utils/FileDoesntExistException.h"
|
||||
#include <messmer/cpp-utils/data/DataUtils.h>
|
||||
#include <messmer/cpp-utils/assert/assert.h>
|
||||
|
||||
using std::istream;
|
||||
@ -44,6 +45,11 @@ size_t OnDiskBlock::size() const {
|
||||
return _data.size();
|
||||
}
|
||||
|
||||
void OnDiskBlock::resize(size_t newSize) {
|
||||
_data = cpputils::DataUtils::resize(std::move(_data), newSize);
|
||||
_dataChanged = true;
|
||||
}
|
||||
|
||||
optional<unique_ref<OnDiskBlock>> OnDiskBlock::LoadFromDisk(const bf::path &rootdir, const Key &key) {
|
||||
auto filepath = rootdir / key.ToString();
|
||||
try {
|
||||
|
@ -29,6 +29,7 @@ public:
|
||||
void flush() override;
|
||||
|
||||
size_t size() const override;
|
||||
void resize(size_t newSize) override;
|
||||
|
||||
private:
|
||||
const boost::filesystem::path _filepath;
|
||||
|
@ -32,6 +32,10 @@ public:
|
||||
return _baseBlock->size();
|
||||
}
|
||||
|
||||
void resize(size_t newSize) override {
|
||||
return _baseBlock->resize(newSize);
|
||||
}
|
||||
|
||||
private:
|
||||
Block *_baseBlock;
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "FakeBlockStore.h"
|
||||
#include <cstring>
|
||||
#include <messmer/cpp-utils/assert/assert.h>
|
||||
#include <messmer/cpp-utils/data/DataUtils.h>
|
||||
|
||||
using std::shared_ptr;
|
||||
using std::istream;
|
||||
@ -37,6 +38,11 @@ size_t FakeBlock::size() const {
|
||||
return _data->size();
|
||||
}
|
||||
|
||||
void FakeBlock::resize(size_t newSize) {
|
||||
*_data = cpputils::DataUtils::resize(std::move(*_data), newSize);
|
||||
_dataChanged = true;
|
||||
}
|
||||
|
||||
void FakeBlock::flush() {
|
||||
if(_dataChanged) {
|
||||
_store->updateData(key(), *_data);
|
||||
|
@ -23,6 +23,8 @@ public:
|
||||
|
||||
size_t size() const override;
|
||||
|
||||
void resize(size_t newSize) override;
|
||||
|
||||
private:
|
||||
FakeBlockStore *_store;
|
||||
std::shared_ptr<cpputils::Data> _data;
|
||||
|
@ -68,8 +68,7 @@ void FakeBlockStore::updateData(const Key &key, const Data &data) {
|
||||
found = insertResult.first;
|
||||
}
|
||||
Data &stored_data = found->second;
|
||||
ASSERT(data.size() == stored_data.size(), "Wrong data size in block");
|
||||
std::memcpy(stored_data.data(), data.data(), data.size());
|
||||
stored_data = data.copy();
|
||||
}
|
||||
|
||||
uint64_t FakeBlockStore::numBlocks() const {
|
||||
|
@ -21,6 +21,9 @@ public:
|
||||
|
||||
virtual size_t size() const = 0;
|
||||
|
||||
//TODO Test resize()
|
||||
virtual void resize(size_t newSize) = 0;
|
||||
|
||||
const Key &key() const {
|
||||
return _key;
|
||||
}
|
||||
|
@ -39,6 +39,7 @@ public:
|
||||
MOCK_METHOD3(write, void(const void*, uint64_t, uint64_t));
|
||||
MOCK_METHOD0(flush, void());
|
||||
MOCK_CONST_METHOD0(size, size_t());
|
||||
MOCK_METHOD1(resize, void(size_t));
|
||||
MOCK_CONST_METHOD0(key, const Key&());
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user