#pragma once #ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_COMPRESSING_COMPRESSINGBLOCKSTORE_H_ #define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_COMPRESSING_COMPRESSINGBLOCKSTORE_H_ #include "../../interface/BlockStore.h" #include "CompressedBlock.h" namespace blockstore { namespace compressing { template class CompressingBlockStore final: public BlockStore { public: CompressingBlockStore(cpputils::unique_ref baseBlockStore); ~CompressingBlockStore(); Key createKey() override; boost::optional> tryCreate(const Key &key, cpputils::Data data) override; boost::optional> load(const Key &key) override; void remove(cpputils::unique_ref block) override; uint64_t numBlocks() const override; private: cpputils::unique_ref _baseBlockStore; DISALLOW_COPY_AND_ASSIGN(CompressingBlockStore); }; template CompressingBlockStore::CompressingBlockStore(cpputils::unique_ref baseBlockStore) : _baseBlockStore(std::move(baseBlockStore)) { } template CompressingBlockStore::~CompressingBlockStore() { } template Key CompressingBlockStore::createKey() { return _baseBlockStore->createKey(); } template boost::optional> CompressingBlockStore::tryCreate(const Key &key, cpputils::Data data) { auto result = CompressedBlock::TryCreateNew(_baseBlockStore.get(), key, std::move(data)); if (result == boost::none) { return boost::none; } return cpputils::unique_ref(std::move(*result)); } template boost::optional> CompressingBlockStore::load(const Key &key) { auto loaded = _baseBlockStore->load(key); if (loaded == boost::none) { return boost::none; } return boost::optional>(CompressedBlock::Decompress(std::move(*loaded))); } template void CompressingBlockStore::remove(cpputils::unique_ref block) { auto _block = cpputils::dynamic_pointer_move>(block); ASSERT(_block != boost::none, "Wrong block type"); auto baseBlock = (*_block)->releaseBaseBlock(); return _baseBlockStore->remove(std::move(baseBlock)); } template uint64_t CompressingBlockStore::numBlocks() const { return _baseBlockStore->numBlocks(); } } } #endif