Rename SynchronizedBlockStore -> CachingBlockStore

This commit is contained in:
Sebastian Meßmer 2015-04-02 02:59:43 -04:00
parent e3f7491d2d
commit da0efd80c5
6 changed files with 47 additions and 46 deletions

View File

@ -1,14 +1,14 @@
#include "CachedBlockRef.h"
#include "SynchronizedBlockStore.h"
#include <messmer/blockstore/implementations/caching/CachedBlockRef.h>
#include <messmer/blockstore/implementations/caching/CachingBlockStore.h>
using std::shared_ptr;
using std::make_unique;
using std::function;
namespace blockstore {
namespace synchronized {
namespace caching {
CachedBlockRef::CachedBlockRef(Block *baseBlock, SynchronizedBlockStore *blockStore)
CachedBlockRef::CachedBlockRef(Block *baseBlock, CachingBlockStore *blockStore)
//TODO We store key twice here - once in OpenBlock, once in the underlying baseBlock.
// Should we move that to make CachedBlockRef::key() call _baseBlock.key()?
:Block(baseBlock->key()),

View File

@ -8,12 +8,12 @@
#include <memory>
namespace blockstore {
namespace synchronized {
class SynchronizedBlockStore;
namespace caching {
class CachingBlockStore;
class CachedBlockRef: public Block {
public:
CachedBlockRef(Block *baseBlock, SynchronizedBlockStore *blockStore);
CachedBlockRef(Block *baseBlock, CachingBlockStore *blockStore);
virtual ~CachedBlockRef();
const void *data() const override;
@ -25,7 +25,7 @@ public:
private:
Block *_baseBlock;
SynchronizedBlockStore *_blockStore;
CachingBlockStore *_blockStore;
DISALLOW_COPY_AND_ASSIGN(CachedBlockRef);
};

View File

@ -1,5 +1,5 @@
#include "CachedBlockRef.h"
#include "SynchronizedBlockStore.h"
#include <messmer/blockstore/implementations/caching/CachedBlockRef.h>
#include <messmer/blockstore/implementations/caching/CachingBlockStore.h>
#include <cassert>
using std::unique_ptr;
@ -10,26 +10,26 @@ using std::lock_guard;
using std::promise;
namespace blockstore {
namespace synchronized {
namespace caching {
SynchronizedBlockStore::SynchronizedBlockStore(unique_ptr<BlockStore> baseBlockStore)
CachingBlockStore::CachingBlockStore(unique_ptr<BlockStore> baseBlockStore)
: _baseBlockStore(std::move(baseBlockStore)),
_openBlocks() {
}
unique_ptr<Block> SynchronizedBlockStore::create(size_t size) {
unique_ptr<Block> CachingBlockStore::create(size_t size) {
auto block = _baseBlockStore->create(size);
lock_guard<mutex> lock(_mutex);
return _addOpenBlock(std::move(block));
}
unique_ptr<Block> SynchronizedBlockStore::_addOpenBlock(unique_ptr<Block> block) {
unique_ptr<Block> CachingBlockStore::_addOpenBlock(unique_ptr<Block> block) {
auto insertResult = _openBlocks.emplace(block->key(), std::move(block));
assert(true == insertResult.second);
return make_unique<CachedBlockRef>(insertResult.first->second.getReference(), this);
}
unique_ptr<Block> SynchronizedBlockStore::load(const Key &key) {
unique_ptr<Block> CachingBlockStore::load(const Key &key) {
lock_guard<mutex> lock(_mutex);
auto found = _openBlocks.find(key);
if (found == _openBlocks.end()) {
@ -43,7 +43,7 @@ unique_ptr<Block> SynchronizedBlockStore::load(const Key &key) {
}
}
void SynchronizedBlockStore::release(const Block *block) {
void CachingBlockStore::release(const Block *block) {
lock_guard<mutex> lock(_mutex);
Key key = block->key();
auto found = _openBlocks.find(key);
@ -58,7 +58,7 @@ void SynchronizedBlockStore::release(const Block *block) {
}
}
void SynchronizedBlockStore::remove(unique_ptr<Block> block) {
void CachingBlockStore::remove(unique_ptr<Block> block) {
auto insertResult = _blocksToRemove.emplace(block->key(), promise<unique_ptr<Block>>());
assert(true == insertResult.second);
block.reset();
@ -69,7 +69,7 @@ void SynchronizedBlockStore::remove(unique_ptr<Block> block) {
_baseBlockStore->remove(std::move(blockToRemove));
}
uint64_t SynchronizedBlockStore::numBlocks() const {
uint64_t CachingBlockStore::numBlocks() const {
return _baseBlockStore->numBlocks();
}

View File

@ -11,12 +11,11 @@
#include "../../interface/BlockStore.h"
namespace blockstore {
namespace synchronized {
namespace caching {
//TODO Rename to CachingBlockStore or something else
class SynchronizedBlockStore: public BlockStore {
class CachingBlockStore: public BlockStore {
public:
SynchronizedBlockStore(std::unique_ptr<BlockStore> baseBlockStore);
CachingBlockStore(std::unique_ptr<BlockStore> baseBlockStore);
std::unique_ptr<Block> create(size_t size) override;
std::unique_ptr<Block> load(const Key &key) override;
@ -45,7 +44,7 @@ private:
std::unique_ptr<Block> _addOpenBlock(std::unique_ptr<Block> block);
DISALLOW_COPY_AND_ASSIGN(SynchronizedBlockStore);
DISALLOW_COPY_AND_ASSIGN(CachingBlockStore);
};
}

View File

@ -0,0 +1,25 @@
#include "../../../implementations/caching/CachingBlockStore.h"
#include "../../../implementations/testfake/FakeBlockStore.h"
#include "../../testutils/BlockStoreTest.h"
#include "google/gtest/gtest.h"
using blockstore::BlockStore;
using blockstore::caching::CachingBlockStore;
using blockstore::testfake::FakeBlockStore;
using std::unique_ptr;
using std::make_unique;
class CachingBlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<CachingBlockStore>(make_unique<FakeBlockStore>());
}
};
INSTANTIATE_TYPED_TEST_CASE_P(Caching, BlockStoreTest, CachingBlockStoreTestFixture);
//TODO Add specific tests ensuring that
// (a) loading the same block twice doesn't load it twice from the underlying blockstore
// (b) ... cache tests

View File

@ -1,23 +0,0 @@
#include "../../../implementations/synchronized/SynchronizedBlockStore.h"
#include "../../../implementations/testfake/FakeBlockStore.h"
#include "../../testutils/BlockStoreTest.h"
#include "google/gtest/gtest.h"
using blockstore::BlockStore;
using blockstore::synchronized::SynchronizedBlockStore;
using blockstore::testfake::FakeBlockStore;
using std::unique_ptr;
using std::make_unique;
class SynchronizedBlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<SynchronizedBlockStore>(make_unique<FakeBlockStore>());
}
};
INSTANTIATE_TYPED_TEST_CASE_P(Synchronized, BlockStoreTest, SynchronizedBlockStoreTestFixture);
//TODO Add specific tests ensuring that the access to the underlying blockstore is synchronized