2015-03-19 11:16:20 +01:00
|
|
|
#pragma once
|
|
|
|
#ifndef BLOCKSTORE_IMPLEMENTATIONS_SYNCHRONIZED_SYNCHRONIZEDBLOCKSTORE_H_
|
|
|
|
#define BLOCKSTORE_IMPLEMENTATIONS_SYNCHRONIZED_SYNCHRONIZEDBLOCKSTORE_H_
|
|
|
|
|
|
|
|
#include "messmer/cpp-utils/macros.h"
|
|
|
|
#include <memory>
|
2015-03-31 14:02:24 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <map>
|
|
|
|
#include <future>
|
2015-03-19 11:16:20 +01:00
|
|
|
|
2015-03-28 18:44:22 +01:00
|
|
|
#include "../../interface/BlockStore.h"
|
|
|
|
|
2015-03-19 11:16:20 +01:00
|
|
|
namespace blockstore {
|
2015-04-02 08:59:43 +02:00
|
|
|
namespace caching {
|
2015-03-19 11:16:20 +01:00
|
|
|
|
2015-04-02 08:59:43 +02:00
|
|
|
class CachingBlockStore: public BlockStore {
|
2015-03-19 11:16:20 +01:00
|
|
|
public:
|
2015-04-02 08:59:43 +02:00
|
|
|
CachingBlockStore(std::unique_ptr<BlockStore> baseBlockStore);
|
2015-03-19 11:16:20 +01:00
|
|
|
|
|
|
|
std::unique_ptr<Block> create(size_t size) override;
|
|
|
|
std::unique_ptr<Block> load(const Key &key) override;
|
|
|
|
void remove(std::unique_ptr<Block> block) override;
|
|
|
|
uint64_t numBlocks() const override;
|
|
|
|
|
2015-03-31 14:02:24 +02:00
|
|
|
void release(const Block *block);
|
|
|
|
|
2015-03-19 11:16:20 +01:00
|
|
|
private:
|
2015-03-31 14:02:24 +02:00
|
|
|
struct OpenBlock {
|
|
|
|
OpenBlock(std::unique_ptr<Block> block_): block(std::move(block_)), refCount(0) {}
|
|
|
|
Block *getReference() {
|
|
|
|
++refCount;
|
|
|
|
return block.get();
|
|
|
|
}
|
|
|
|
void releaseReference() {
|
|
|
|
--refCount;
|
|
|
|
}
|
|
|
|
std::unique_ptr<Block> block;
|
|
|
|
uint32_t refCount;
|
|
|
|
};
|
2015-03-19 11:16:20 +01:00
|
|
|
std::unique_ptr<BlockStore> _baseBlockStore;
|
2015-03-31 14:02:24 +02:00
|
|
|
std::map<Key, OpenBlock> _openBlocks;
|
|
|
|
std::mutex _mutex;
|
|
|
|
std::map<Key, std::promise<std::unique_ptr<Block>>> _blocksToRemove;
|
|
|
|
|
|
|
|
std::unique_ptr<Block> _addOpenBlock(std::unique_ptr<Block> block);
|
2015-03-19 11:16:20 +01:00
|
|
|
|
2015-04-02 08:59:43 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CachingBlockStore);
|
2015-03-19 11:16:20 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|