|
|
@ -1,3 +1,8 @@ |
|
|
|
<<<<<<< HEAD |
|
|
|
======= |
|
|
|
#include <chrono>
|
|
|
|
|
|
|
|
>>>>>>> virgil |
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/db.h"
|
|
|
@ -16,12 +21,17 @@ Status OpenDB(std::string dbName, DB **db) { |
|
|
|
void InsertData(DB *db, uint64_t ttl/* second */) { |
|
|
|
WriteOptions writeOptions; |
|
|
|
int key_num = data_size / value_size; |
|
|
|
<<<<<<< HEAD |
|
|
|
srand(0); |
|
|
|
======= |
|
|
|
srand(42); |
|
|
|
>>>>>>> virgil |
|
|
|
|
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
@ -31,7 +41,11 @@ void GetData(DB *db, int size = (1 << 30)) { |
|
|
|
int key_num = data_size / value_size; |
|
|
|
|
|
|
|
// 点查
|
|
|
|
<<<<<<< HEAD |
|
|
|
srand(0); |
|
|
|
======= |
|
|
|
srand(42); |
|
|
|
>>>>>>> virgil |
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
int key_ = rand() % key_num+1; |
|
|
|
std::string key = std::to_string(key_); |
|
|
@ -42,7 +56,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(); |
|
|
|
} |
|
|
@ -54,12 +68,14 @@ TEST(TestTTL, ReadTTL) { |
|
|
|
ReadOptions readOptions; |
|
|
|
Status status; |
|
|
|
int key_num = data_size / value_size; |
|
|
|
srand(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()); |
|
|
|
} |
|
|
|
|
|
|
@ -70,6 +86,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()); |
|
|
|
} |
|
|
|
} |
|
|
@ -77,8 +95,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(); |
|
|
|
} |
|
|
|
|
|
|
@ -89,6 +107,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); |
|
|
@ -99,6 +118,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); |
|
|
|
} |
|
|
|
|
|
|
|