Added dummy implementation for new caching block store

This commit is contained in:
Sebastian Messmer 2015-04-15 15:46:35 +02:00
parent cc30dcde10
commit 72f952b57c
5 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,2 @@
#include "CacheEntry.h"
#include "../../interface/Block.h"

View File

@ -0,0 +1,37 @@
#pragma once
#ifndef MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING2_CACHEENTRY_H_
#define MESSMER_BLOCKSTORE_IMPLEMENTATIONS_CACHING2_CACHEENTRY_H_
#include <ctime>
#include <memory>
#include <messmer/cpp-utils/macros.h>
namespace blockstore {
class Block;
namespace caching2 {
class CacheEntry {
public:
CacheEntry(std::unique_ptr<Block> block): _lastAccess(time(nullptr)), _block(std::move(block)) {
}
double ageSeconds() {
return difftime(time(nullptr), _lastAccess);
}
std::unique_ptr<Block> releaseBlock() {
return std::move(_block);
}
private:
time_t _lastAccess;
std::unique_ptr<Block> _block;
DISALLOW_COPY_AND_ASSIGN(CacheEntry);
};
}
}
#endif

View File

@ -0,0 +1,34 @@
#include "Caching2BlockStore.h"
#include "../../interface/Block.h"
using std::unique_ptr;
namespace blockstore {
namespace caching2 {
Caching2BlockStore::Caching2BlockStore(std::unique_ptr<BlockStore> baseBlockStore)
:_baseBlockStore(std::move(baseBlockStore)) {
}
unique_ptr<Block> Caching2BlockStore::create(size_t size) {
//TODO Also cache this and only write back in the destructor?
// When writing back is done efficiently in the base store (e.g. only one safe-to-disk, not one in the create() and then one in the save(), this is not supported by the current BlockStore interface),
// then the base store could actually directly create a block in the create() call, OnDiskBlockStore wouldn't have to avoid file creation in the create() call for performance reasons and I could also adapt the OnDiskBlockStore test cases and remove a lot of flush() calls there because then blocks are loadable directly after the create call() without a flush.
// Currently, OnDiskBlockStore doesn't create new blocks directly but only after they're destructed (performance reasons), but this means a newly created block can't be loaded directly.
return _baseBlockStore->create(size);
}
unique_ptr<Block> Caching2BlockStore::load(const Key &key) {
return _baseBlockStore->load(key);
}
void Caching2BlockStore::remove(std::unique_ptr<Block> block) {
return _baseBlockStore->remove(std::move(block));
}
uint64_t Caching2BlockStore::numBlocks() const {
return _baseBlockStore->numBlocks();
}
}
}

View File

@ -0,0 +1,31 @@
#pragma once
#ifndef BLOCKSTORE_IMPLEMENTATIONS_CACHING2_CACHINGBLOCKSTORE_H_
#define BLOCKSTORE_IMPLEMENTATIONS_CACHING2_CACHINGBLOCKSTORE_H_
#include "../../interface/BlockStore.h"
#include "CacheEntry.h"
namespace blockstore {
namespace caching2 {
class Caching2BlockStore: public BlockStore {
public:
Caching2BlockStore(std::unique_ptr<BlockStore> baseBlockStore);
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;
private:
std::unique_ptr<BlockStore> _baseBlockStore;
std::map<Key, CacheEntry> _cachedBlocks;
DISALLOW_COPY_AND_ASSIGN(Caching2BlockStore);
};
}
}
#endif

View File

@ -0,0 +1,23 @@
#include "../../../implementations/caching2/Caching2BlockStore.h"
#include "../../../implementations/testfake/FakeBlockStore.h"
#include "../../testutils/BlockStoreTest.h"
#include "google/gtest/gtest.h"
using blockstore::BlockStore;
using blockstore::caching2::Caching2BlockStore;
using blockstore::testfake::FakeBlockStore;
using std::unique_ptr;
using std::make_unique;
class Caching2BlockStoreTestFixture: public BlockStoreTestFixture {
public:
unique_ptr<BlockStore> createBlockStore() override {
return make_unique<Caching2BlockStore>(make_unique<FakeBlockStore>());
}
};
INSTANTIATE_TYPED_TEST_CASE_P(Caching2, BlockStoreTest, Caching2BlockStoreTestFixture);
//TODO Add specific tests for the blockstore