2016-03-18 23:50:34 +01:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2016-03-18 23:50:34 +01:00
|
|
|
//
|
2016-06-02 22:42:41 +02:00
|
|
|
#include <stdlib.h>
|
2016-03-18 23:50:34 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "db/db_test_util.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "memory/arena.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/testharness.h"
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "util/random.h"
|
2016-03-18 23:50:34 +01:00
|
|
|
#include "utilities/persistent_cache/hash_table.h"
|
|
|
|
#include "utilities/persistent_cache/hash_table_evictable.h"
|
|
|
|
|
2016-05-21 01:16:51 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2016-03-18 23:50:34 +01:00
|
|
|
|
|
|
|
struct HashTableTest : public testing::Test {
|
2019-02-14 22:52:47 +01:00
|
|
|
~HashTableTest() override { map_.Clear(&HashTableTest::ClearNode); }
|
2016-03-18 23:50:34 +01:00
|
|
|
|
|
|
|
struct Node {
|
|
|
|
Node() {}
|
|
|
|
explicit Node(const uint64_t key, const std::string& val = std::string())
|
|
|
|
: key_(key), val_(val) {}
|
|
|
|
|
|
|
|
uint64_t key_ = 0;
|
|
|
|
std::string val_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Equal {
|
|
|
|
bool operator()(const Node& lhs, const Node& rhs) {
|
|
|
|
return lhs.key_ == rhs.key_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Hash {
|
|
|
|
uint64_t operator()(const Node& node) {
|
|
|
|
return std::hash<uint64_t>()(node.key_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-05 22:08:17 +01:00
|
|
|
static void ClearNode(Node /*node*/) {}
|
2016-03-18 23:50:34 +01:00
|
|
|
|
|
|
|
HashTable<Node, Hash, Equal> map_;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct EvictableHashTableTest : public testing::Test {
|
2019-02-14 22:52:47 +01:00
|
|
|
~EvictableHashTableTest() override {
|
|
|
|
map_.Clear(&EvictableHashTableTest::ClearNode);
|
|
|
|
}
|
2016-03-18 23:50:34 +01:00
|
|
|
|
|
|
|
struct Node : LRUElement<Node> {
|
|
|
|
Node() {}
|
|
|
|
explicit Node(const uint64_t key, const std::string& val = std::string())
|
|
|
|
: key_(key), val_(val) {}
|
|
|
|
|
|
|
|
uint64_t key_ = 0;
|
|
|
|
std::string val_;
|
|
|
|
std::atomic<uint32_t> refs_{0};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Equal {
|
|
|
|
bool operator()(const Node* lhs, const Node* rhs) {
|
|
|
|
return lhs->key_ == rhs->key_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Hash {
|
|
|
|
uint64_t operator()(const Node* node) {
|
|
|
|
return std::hash<uint64_t>()(node->key_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-05 22:08:17 +01:00
|
|
|
static void ClearNode(Node* /*node*/) {}
|
2016-03-18 23:50:34 +01:00
|
|
|
|
|
|
|
EvictableHashTable<Node, Hash, Equal> map_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(HashTableTest, TestInsert) {
|
|
|
|
const uint64_t max_keys = 1024 * 1024;
|
|
|
|
|
|
|
|
// insert
|
|
|
|
for (uint64_t k = 0; k < max_keys; ++k) {
|
|
|
|
map_.Insert(Node(k, std::string(1000, k % 255)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify
|
|
|
|
for (uint64_t k = 0; k < max_keys; ++k) {
|
|
|
|
Node val;
|
2017-04-22 05:41:37 +02:00
|
|
|
port::RWMutex* rlock = nullptr;
|
2016-03-18 23:50:34 +01:00
|
|
|
assert(map_.Find(Node(k), &val, &rlock));
|
|
|
|
rlock->ReadUnlock();
|
|
|
|
assert(val.val_ == std::string(1000, k % 255));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(HashTableTest, TestErase) {
|
|
|
|
const uint64_t max_keys = 1024 * 1024;
|
|
|
|
// insert
|
|
|
|
for (uint64_t k = 0; k < max_keys; ++k) {
|
|
|
|
map_.Insert(Node(k, std::string(1000, k % 255)));
|
|
|
|
}
|
|
|
|
|
2016-06-02 22:42:41 +02:00
|
|
|
auto rand = Random64(time(nullptr));
|
2016-03-18 23:50:34 +01:00
|
|
|
// erase a few keys randomly
|
|
|
|
std::set<uint64_t> erased;
|
|
|
|
for (int i = 0; i < 1024; ++i) {
|
2016-06-02 22:42:41 +02:00
|
|
|
uint64_t k = rand.Next() % max_keys;
|
2016-03-18 23:50:34 +01:00
|
|
|
if (erased.find(k) != erased.end()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
assert(map_.Erase(Node(k), /*ret=*/nullptr));
|
|
|
|
erased.insert(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify
|
|
|
|
for (uint64_t k = 0; k < max_keys; ++k) {
|
|
|
|
Node val;
|
|
|
|
port::RWMutex* rlock = nullptr;
|
|
|
|
bool status = map_.Find(Node(k), &val, &rlock);
|
|
|
|
if (erased.find(k) == erased.end()) {
|
|
|
|
assert(status);
|
|
|
|
rlock->ReadUnlock();
|
|
|
|
assert(val.val_ == std::string(1000, k % 255));
|
|
|
|
} else {
|
|
|
|
assert(!status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EvictableHashTableTest, TestEvict) {
|
|
|
|
const uint64_t max_keys = 1024 * 1024;
|
|
|
|
|
|
|
|
// insert
|
|
|
|
for (uint64_t k = 0; k < max_keys; ++k) {
|
|
|
|
map_.Insert(new Node(k, std::string(1000, k % 255)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify
|
|
|
|
for (uint64_t k = 0; k < max_keys; ++k) {
|
|
|
|
Node* val = map_.Evict();
|
|
|
|
// unfortunately we can't predict eviction value since it is from any one of
|
|
|
|
// the lock stripe
|
|
|
|
assert(val);
|
|
|
|
assert(val->val_ == std::string(1000, val->key_ % 255));
|
|
|
|
delete val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2016-05-21 01:16:51 +02:00
|
|
|
#endif
|
2016-03-18 23:50:34 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|