Implemented SynchronizedBlockStore::remove()

This commit is contained in:
Sebastian Meßmer 2015-03-29 07:55:57 -04:00
parent 5571a42980
commit 9b5ad835db
3 changed files with 19 additions and 8 deletions

View File

@ -44,7 +44,7 @@ unique_ptr<Block> OpenBlockList::acquire(const Key &key, function<unique_ptr<Blo
} }
future<unique_ptr<Block>> OpenBlockList::_addPromiseForBlock(const Key &key) { future<unique_ptr<Block>> OpenBlockList::_addPromiseForBlock(const Key &key) {
auto insertResult = _wantedBlocks.emplace(std::make_pair(key, promise<unique_ptr<Block>>())); auto insertResult = _wantedBlocks.emplace(key, promise<unique_ptr<Block>>());
assert(insertResult.second == true); assert(insertResult.second == true);
return insertResult.first->second.get_future(); return insertResult.first->second.get_future();
} }
@ -55,8 +55,20 @@ void OpenBlockList::release(unique_ptr<Block> block) {
foundWantedBlock->second.set_value(std::move(block)); foundWantedBlock->second.set_value(std::move(block));
} else { } else {
_openBlocks.erase(block->key()); _openBlocks.erase(block->key());
auto foundBlockToClose = _blocksToClose.find(block->key());
if (foundBlockToClose != _blocksToClose.end()) {
foundBlockToClose->second.set_value(std::move(block));
}
} }
} }
void OpenBlockList::close(unique_ptr<Block> block, function<void (unique_ptr<Block>)> onClose) {
auto insertResult = _blocksToClose.emplace(block->key(), promise<unique_ptr<Block>>());
assert(insertResult.second == true);
block.reset();
auto closedBlock = insertResult.first->second.get_future().get();
onClose(std::move(closedBlock));
}
} }
} }

View File

@ -4,6 +4,7 @@
#include <memory> #include <memory>
#include <set> #include <set>
#include <map> #include <map>
#include <vector>
#include <functional> #include <functional>
#include "../../utils/Key.h" #include "../../utils/Key.h"
@ -22,10 +23,12 @@ public:
std::unique_ptr<Block> acquire(const Key &key, std::function<std::unique_ptr<Block> ()> loader); std::unique_ptr<Block> acquire(const Key &key, std::function<std::unique_ptr<Block> ()> loader);
void release(std::unique_ptr<Block> block); void release(std::unique_ptr<Block> block);
void close(std::unique_ptr<Block> block, std::function<void (std::unique_ptr<Block>)> onClose);
private: private:
std::set<Key> _openBlocks; std::set<Key> _openBlocks;
std::map<Key, std::promise<std::unique_ptr<Block>>> _wantedBlocks; std::map<Key, std::promise<std::unique_ptr<Block>>> _wantedBlocks;
std::map<Key, std::promise<std::unique_ptr<Block>>> _blocksToClose;
std::future<std::unique_ptr<Block>> _addPromiseForBlock(const Key &key); std::future<std::unique_ptr<Block>> _addPromiseForBlock(const Key &key);
}; };

View File

@ -23,13 +23,9 @@ unique_ptr<Block> SynchronizedBlockStore::load(const Key &key) {
} }
void SynchronizedBlockStore::remove(unique_ptr<Block> block) { void SynchronizedBlockStore::remove(unique_ptr<Block> block) {
//TODO _openBlockList.close(std::move(block), [this] (unique_ptr<Block> block) {
//Remove from openBlockList, therefore close it, and second parameter is meant to be an onClose event handler _baseBlockStore->remove(std::move(block));
//(called after all threads wanting to work with the block have been satisfied). });
//But is quite unreadable here this way...
//_openBlockList.remove(std::move(block), [] (unique_ptr<Block> block) {
// _baseBlockStore->remove(block);
//});
} }
uint64_t SynchronizedBlockStore::numBlocks() const { uint64_t SynchronizedBlockStore::numBlocks() const {