|
|
@ -1,4 +1,4 @@ |
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
@ -20,12 +20,13 @@ 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); |
|
|
|
|
|
|
|
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->ttl = ttl; |
|
|
|
db->Put(writeOptions, key, value, ttl); |
|
|
|
} |
|
|
|
} |
|
|
@ -35,7 +36,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_); |
|
|
@ -46,7 +47,7 @@ void GetData(DB *db, int size = (1 << 30)) { |
|
|
|
|
|
|
|
TEST(TestTTL, ReadTTL) { |
|
|
|
DB *db; |
|
|
|
if(OpenDB("testdb", &db).ok() == false) { |
|
|
|
if(OpenDB("testdb_ReadTTL", &db).ok() == false) { |
|
|
|
std::cerr << "open db failed" << std::endl; |
|
|
|
abort(); |
|
|
|
} |
|
|
@ -58,12 +59,14 @@ 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); |
|
|
|
uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
|
|
|
std::cout << status.ToString() << " key: " << key << " value: ******" << value.substr(value.find("_ts_")) << " now: " << now << std::endl; |
|
|
|
ASSERT_TRUE(status.ok()); |
|
|
|
} |
|
|
|
|
|
|
@ -74,6 +77,8 @@ TEST(TestTTL, ReadTTL) { |
|
|
|
std::string key = std::to_string(key_); |
|
|
|
std::string value; |
|
|
|
status = db->Get(readOptions, key, &value); |
|
|
|
uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
|
|
|
std::cout << status.ToString() << " key: " << key << " value: ******" << value.substr(value.find("_ts_")) << " now: " << now << std::endl; |
|
|
|
ASSERT_FALSE(status.ok()); |
|
|
|
} |
|
|
|
} |
|
|
@ -81,8 +86,8 @@ TEST(TestTTL, ReadTTL) { |
|
|
|
TEST(TestTTL, CompactionTTL) { |
|
|
|
DB *db; |
|
|
|
|
|
|
|
if(OpenDB("testdb", &db).ok() == false) { |
|
|
|
std::cerr << "open db failed" << std::endl; |
|
|
|
if(OpenDB("testdb_CompactionTTL", &db).ok() == false) { |
|
|
|
std::cerr << "open db failed" << OpenDB("testdb_CompactionTTL", &db).ToString() << std::endl; |
|
|
|
abort(); |
|
|
|
} |
|
|
|
|
|
|
@ -93,6 +98,7 @@ TEST(TestTTL, CompactionTTL) { |
|
|
|
ranges[0] = leveldb::Range("-", "A"); |
|
|
|
uint64_t sizes[1]; |
|
|
|
db->GetApproximateSizes(ranges, 1, sizes); |
|
|
|
std::cout << "ApproximateSizes before TTL: " << sizes[0] << std::endl; |
|
|
|
ASSERT_GT(sizes[0], 0); |
|
|
|
|
|
|
|
Env::Default()->SleepForMicroseconds(ttl * 1000000); |
|
|
@ -103,6 +109,7 @@ TEST(TestTTL, CompactionTTL) { |
|
|
|
ranges[0] = leveldb::Range("-", "A"); |
|
|
|
// uint64_t sizes[1];
|
|
|
|
db->GetApproximateSizes(ranges, 1, sizes); |
|
|
|
std::cout << "ApproximateSizes after TTL: " << sizes[0] << std::endl; |
|
|
|
ASSERT_EQ(sizes[0], 0); |
|
|
|
} |
|
|
|
|
|
|
|