From f491d3d183627c2e651792c17702067072365a4b Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Mon, 23 Feb 2015 21:07:07 +0100 Subject: [PATCH] Added BlockStore::numBlocks() --- implementations/inmemory/InMemoryBlockStore.cpp | 4 ++++ implementations/inmemory/InMemoryBlockStore.h | 1 + implementations/ondisk/OnDiskBlockStore.cpp | 4 ++++ implementations/ondisk/OnDiskBlockStore.h | 1 + implementations/testfake/FakeBlockStore.cpp | 4 ++++ implementations/testfake/FakeBlockStore.h | 1 + interface/BlockStore.h | 2 ++ test/interface/helpers/BlockStoreWithRandomKeysTest.cpp | 1 + 8 files changed, 18 insertions(+) diff --git a/implementations/inmemory/InMemoryBlockStore.cpp b/implementations/inmemory/InMemoryBlockStore.cpp index ea52b095..439a4636 100644 --- a/implementations/inmemory/InMemoryBlockStore.cpp +++ b/implementations/inmemory/InMemoryBlockStore.cpp @@ -43,5 +43,9 @@ void InMemoryBlockStore::remove(unique_ptr block) { assert(1==numRemoved); } +uint64_t InMemoryBlockStore::numBlocks() const { + return _blocks.size(); +} + } } diff --git a/implementations/inmemory/InMemoryBlockStore.h b/implementations/inmemory/InMemoryBlockStore.h index b27b386c..1913c725 100644 --- a/implementations/inmemory/InMemoryBlockStore.h +++ b/implementations/inmemory/InMemoryBlockStore.h @@ -19,6 +19,7 @@ public: std::unique_ptr create(const Key &key, size_t size) override; std::unique_ptr load(const Key &key) override; void remove(std::unique_ptr block) override; + uint64_t numBlocks() const override; private: std::map _blocks; diff --git a/implementations/ondisk/OnDiskBlockStore.cpp b/implementations/ondisk/OnDiskBlockStore.cpp index 8b7e44d4..0f45b082 100644 --- a/implementations/ondisk/OnDiskBlockStore.cpp +++ b/implementations/ondisk/OnDiskBlockStore.cpp @@ -34,5 +34,9 @@ void OnDiskBlockStore::remove(unique_ptr block) { OnDiskBlock::RemoveFromDisk(_rootdir, key); } +uint64_t OnDiskBlockStore::numBlocks() const { + return std::distance(bf::directory_iterator(_rootdir), bf::directory_iterator()); +} + } } diff --git a/implementations/ondisk/OnDiskBlockStore.h b/implementations/ondisk/OnDiskBlockStore.h index cbb06839..3f0fc92d 100644 --- a/implementations/ondisk/OnDiskBlockStore.h +++ b/implementations/ondisk/OnDiskBlockStore.h @@ -19,6 +19,7 @@ public: std::unique_ptr create(const Key &key, size_t size) override; std::unique_ptr load(const Key &key) override; void remove(std::unique_ptr block) override; + uint64_t numBlocks() const override; private: const boost::filesystem::path _rootdir; diff --git a/implementations/testfake/FakeBlockStore.cpp b/implementations/testfake/FakeBlockStore.cpp index 900908d2..e15e9d7a 100644 --- a/implementations/testfake/FakeBlockStore.cpp +++ b/implementations/testfake/FakeBlockStore.cpp @@ -55,5 +55,9 @@ void FakeBlockStore::updateData(const Key &key, const Data &data) { std::memcpy(stored_data.data(), data.data(), data.size()); } +uint64_t FakeBlockStore::numBlocks() const { + return _blocks.size(); +} + } } diff --git a/implementations/testfake/FakeBlockStore.h b/implementations/testfake/FakeBlockStore.h index 2440cd45..e59b6cbf 100644 --- a/implementations/testfake/FakeBlockStore.h +++ b/implementations/testfake/FakeBlockStore.h @@ -34,6 +34,7 @@ public: std::unique_ptr create(const Key &key, 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 updateData(const Key &key, const Data &data); diff --git a/interface/BlockStore.h b/interface/BlockStore.h index ffb29e85..028c98c1 100644 --- a/interface/BlockStore.h +++ b/interface/BlockStore.h @@ -20,6 +20,8 @@ public: // Return nullptr if block with this key doesn't exists virtual std::unique_ptr load(const Key &key) = 0; virtual void remove(std::unique_ptr block) = 0; + virtual uint64_t numBlocks() const = 0; + //TODO Test numBlocks }; } diff --git a/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp b/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp index a524e8c7..93912252 100644 --- a/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp +++ b/test/interface/helpers/BlockStoreWithRandomKeysTest.cpp @@ -25,6 +25,7 @@ public: } MOCK_METHOD1(do_load, Block*(const Key &)); void remove(unique_ptr block) {} + MOCK_CONST_METHOD0(numBlocks, uint64_t()); }; class BlockMock: public Block {