Fix test cases

This commit is contained in:
Sebastian Meßmer 2015-10-15 04:49:31 +02:00
parent 0e1ec6829a
commit 92d0d073a5
4 changed files with 18 additions and 2 deletions

View File

@ -26,7 +26,9 @@ public:
void push(const Key &key, Value value) {
auto newEntry = _entries.emplace(std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(_sentinel.prev, &_sentinel));
ASSERT(newEntry.second == true, "There is already an element with this key");
if (!newEntry.second) {
throw std::logic_error("There is already an element with this key");
}
newEntry.first->second.init(&newEntry.first->first, std::move(value));
//The following is ok, because std::unordered_map never invalidates pointers to its entries
_sentinel.prev->next = &newEntry.first->second;

View File

@ -15,6 +15,8 @@ namespace ondisk {
OnDiskBlockStore::OnDiskBlockStore(const boost::filesystem::path &rootdir)
: _rootdir(rootdir) {}
//TODO Do I have to lock tryCreate/remove and/or load? Or does ParallelAccessBlockStore take care of that?
optional<unique_ref<Block>> OnDiskBlockStore::tryCreate(const Key &key, Data data) {
//TODO Easier implementation? This is only so complicated because of the cast OnDiskBlock -> Block
auto result = std::move(OnDiskBlock::CreateOnDisk(_rootdir, key, std::move(data)));

View File

@ -19,6 +19,7 @@ FakeBlockStore::FakeBlockStore()
: _blocks(), _used_dataregions_for_blocks() {}
optional<unique_ref<Block>> FakeBlockStore::tryCreate(const Key &key, Data data) {
std::unique_lock<std::mutex> lock(_mutex);
auto insert_result = _blocks.emplace(key.ToString(), std::move(data));
if (!insert_result.second) {
@ -26,10 +27,15 @@ optional<unique_ref<Block>> FakeBlockStore::tryCreate(const Key &key, Data data)
}
//Return a copy of the stored data
return load(key);
return _load(key);
}
optional<unique_ref<Block>> FakeBlockStore::load(const Key &key) {
std::unique_lock<std::mutex> lock(_mutex);
return _load(key);
}
optional<unique_ref<Block>> FakeBlockStore::_load(const Key &key) {
//Return a copy of the stored data
string key_string = key.ToString();
try {
@ -42,6 +48,7 @@ optional<unique_ref<Block>> FakeBlockStore::load(const Key &key) {
void FakeBlockStore::remove(unique_ref<Block> block) {
Key key = block->key();
cpputils::destruct(std::move(block));
std::unique_lock<std::mutex> lock(_mutex);
int numRemoved = _blocks.erase(key.ToString());
ASSERT(numRemoved == 1, "Block not found");
}
@ -53,6 +60,7 @@ unique_ref<Block> FakeBlockStore::makeFakeBlockFromData(const Key &key, const Da
}
void FakeBlockStore::updateData(const Key &key, const Data &data) {
std::unique_lock<std::mutex> lock(_mutex);
auto found = _blocks.find(key.ToString());
if (found == _blocks.end()) {
auto insertResult = _blocks.emplace(key.ToString(), data.copy());
@ -65,6 +73,7 @@ void FakeBlockStore::updateData(const Key &key, const Data &data) {
}
uint64_t FakeBlockStore::numBlocks() const {
std::unique_lock<std::mutex> lock(_mutex);
return _blocks.size();
}

View File

@ -48,7 +48,10 @@ private:
//We want to avoid this for the reasons mentioned above (overflow data).
std::vector<std::shared_ptr<cpputils::Data>> _used_dataregions_for_blocks;
mutable std::mutex _mutex;
cpputils::unique_ref<Block> makeFakeBlockFromData(const Key &key, const cpputils::Data &data, bool dirty);
boost::optional<cpputils::unique_ref<Block>> _load(const Key &key);
DISALLOW_COPY_AND_ASSIGN(FakeBlockStore);
};