|
|
@ -4,7 +4,7 @@ |
|
|
|
|
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/db.h"
|
|
|
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
using namespace leveldb; |
|
|
|
|
|
|
@ -20,14 +20,28 @@ Status OpenDB(std::string dbName, DB **db) { |
|
|
|
void InsertData(DB *db, uint64_t ttl/* second */) { |
|
|
|
WriteOptions writeOptions; |
|
|
|
int key_num = data_size / value_size; |
|
|
|
srand(static_cast<unsigned int>(time(0))); |
|
|
|
srand(42); |
|
|
|
|
|
|
|
// 用于存储成功写入的唯一键
|
|
|
|
std::unordered_set<std::string> unique_keys; |
|
|
|
|
|
|
|
for (int i = 0; i < key_num; i++) { |
|
|
|
int key_ = rand() % key_num+1; |
|
|
|
std::string key = std::to_string(key_); |
|
|
|
std::string value(value_size, 'a'); |
|
|
|
db->Put(writeOptions, key, value, ttl); |
|
|
|
Status status = db->Put(writeOptions, key, value, ttl); |
|
|
|
if (!status.ok()) { |
|
|
|
// 输出失败的状态信息并退出循环
|
|
|
|
std::cerr << "Failed to write key: " << key |
|
|
|
<< ", Status: " << status.ToString() << std::endl; |
|
|
|
}else{ |
|
|
|
std::cerr << "Success to write key: " << key << std::endl; |
|
|
|
unique_keys.insert(key); // 插入集合中,如果已经存在则不会重复插入
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 打印成功写入的唯一键的数量
|
|
|
|
std::cout << "Total unique keys successfully written: " << unique_keys.size() << std::endl; |
|
|
|
} |
|
|
|
|
|
|
|
void GetData(DB *db, int size = (1 << 30)) { |
|
|
@ -35,7 +49,7 @@ void GetData(DB *db, int size = (1 << 30)) { |
|
|
|
int key_num = data_size / value_size; |
|
|
|
|
|
|
|
// 点查
|
|
|
|
srand(static_cast<unsigned int>(time(0))); |
|
|
|
srand(42); |
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
int key_ = rand() % key_num+1; |
|
|
|
std::string key = std::to_string(key_); |
|
|
@ -58,12 +72,18 @@ TEST(TestTTL, ReadTTL) { |
|
|
|
ReadOptions readOptions; |
|
|
|
Status status; |
|
|
|
int key_num = data_size / value_size; |
|
|
|
srand(static_cast<unsigned int>(time(0))); |
|
|
|
srand(42); |
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
int key_ = rand() % key_num+1; |
|
|
|
std::string key = std::to_string(key_); |
|
|
|
std::string value; |
|
|
|
status = db->Get(readOptions, key, &value); |
|
|
|
|
|
|
|
// 检查 status 并打印出失败的状态信息
|
|
|
|
if (!status.ok()) { |
|
|
|
std::cerr << "Key: " << key << ", Status: " << status.ToString() << std::endl; |
|
|
|
} |
|
|
|
|
|
|
|
ASSERT_TRUE(status.ok()); |
|
|
|
} |
|
|
|
|
|
|
@ -74,6 +94,12 @@ TEST(TestTTL, ReadTTL) { |
|
|
|
std::string key = std::to_string(key_); |
|
|
|
std::string value; |
|
|
|
status = db->Get(readOptions, key, &value); |
|
|
|
|
|
|
|
// 检查 status 并打印出失败的状态信息
|
|
|
|
if (status.ok()) { |
|
|
|
std::cerr << "Key: " << key << ", Status: " << status.ToString() << std::endl; |
|
|
|
} |
|
|
|
|
|
|
|
ASSERT_FALSE(status.ok()); |
|
|
|
} |
|
|
|
} |
|
|
@ -106,6 +132,7 @@ TEST(TestTTL, CompactionTTL) { |
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
srand(42); |
|
|
|
// All tests currently run with the same read-only file limits.
|
|
|
|
testing::InitGoogleTest(&argc, argv); |
|
|
|
return RUN_ALL_TESTS(); |
|
|
|