#pragma once #ifndef BLOCKSTORE_IMPLEMENTATIONS_SYNCHRONIZED_SYNCHRONIZEDBLOCKSTORE_H_ #define BLOCKSTORE_IMPLEMENTATIONS_SYNCHRONIZED_SYNCHRONIZEDBLOCKSTORE_H_ #include "messmer/cpp-utils/macros.h" #include #include #include #include #include "../../interface/BlockStore.h" namespace blockstore { namespace caching { class CachingBlockStore: public BlockStore { public: CachingBlockStore(std::unique_ptr baseBlockStore); std::unique_ptr create(size_t size) override; std::unique_ptr load(const Key &key) override; void remove(std::unique_ptr block) override; uint64_t numBlocks() const override; void release(const Block *block); private: struct OpenBlock { OpenBlock(std::unique_ptr block_): block(std::move(block_)), refCount(0) {} Block *getReference() { ++refCount; return block.get(); } void releaseReference() { --refCount; } std::unique_ptr block; uint32_t refCount; }; std::unique_ptr _baseBlockStore; std::map _openBlocks; std::mutex _mutex; std::map>> _blocksToRemove; std::unique_ptr _addOpenBlock(std::unique_ptr block); DISALLOW_COPY_AND_ASSIGN(CachingBlockStore); }; } } #endif