|
|
@ -10,7 +10,7 @@ using namespace leveldb; |
|
|
|
|
|
|
|
constexpr int value_size = 2048; |
|
|
|
constexpr int data_size = 128 << 20; |
|
|
|
|
|
|
|
std::vector<std::pair<std::string,std::string>> gen_v; |
|
|
|
Status OpenDB(std::string dbName, DB **db) { |
|
|
|
Options options; |
|
|
|
options.create_if_missing = true; |
|
|
@ -19,34 +19,37 @@ Status OpenDB(std::string dbName, DB **db) { |
|
|
|
|
|
|
|
void InsertData(DB *db, uint64_t ttl/* second */) { |
|
|
|
WriteOptions writeOptions; |
|
|
|
int key_num = data_size / value_size; |
|
|
|
int key_num = 50; |
|
|
|
srand(static_cast<unsigned int>(time(0))); |
|
|
|
|
|
|
|
for (int i = 0; i < key_num; i++) { |
|
|
|
int key_ = rand() % key_num+1; |
|
|
|
//int key_ = rand() % key_num+1;
|
|
|
|
int key_=i; |
|
|
|
std::string key = std::to_string(key_); |
|
|
|
std::string value(value_size, 'a'); |
|
|
|
db->Put(writeOptions, key, value, ttl); |
|
|
|
gen_v.push_back({key,value}); |
|
|
|
auto status=db->Put(writeOptions, key, value, ttl); |
|
|
|
assert(status.ok()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void GetData(DB *db, int size = (1 << 30)) { |
|
|
|
ReadOptions readOptions; |
|
|
|
int key_num = data_size / value_size; |
|
|
|
// void GetData(DB *db, int size = (1 << 30)) {
|
|
|
|
// ReadOptions readOptions;
|
|
|
|
// int key_num = data_size / value_size;
|
|
|
|
|
|
|
|
// 点查
|
|
|
|
srand(static_cast<unsigned int>(time(0))); |
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
int key_ = rand() % key_num+1; |
|
|
|
std::string key = std::to_string(key_); |
|
|
|
std::string value; |
|
|
|
db->Get(readOptions, key, &value); |
|
|
|
} |
|
|
|
} |
|
|
|
// // 点查
|
|
|
|
// srand(static_cast<unsigned int>(time(0)));
|
|
|
|
// for (int i = 0; i < 100; i++) {
|
|
|
|
// int key_ = rand() % key_num+1;
|
|
|
|
// std::string key = std::to_string(key_);
|
|
|
|
// std::string value;
|
|
|
|
// db->Get(readOptions, key, &value);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
TEST(TestTTL, ReadTTL) { |
|
|
|
DB *db; |
|
|
|
if(OpenDB("testdb", &db).ok() == false) { |
|
|
|
if(OpenDB("testdb_4", &db).ok() == false) { |
|
|
|
std::cerr << "open db failed" << std::endl; |
|
|
|
abort(); |
|
|
|
} |
|
|
@ -58,10 +61,9 @@ TEST(TestTTL, ReadTTL) { |
|
|
|
ReadOptions readOptions; |
|
|
|
Status status; |
|
|
|
int key_num = data_size / value_size; |
|
|
|
srand(static_cast<unsigned int>(time(0))); |
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
int key_ = rand() % key_num+1; |
|
|
|
std::string key = std::to_string(key_); |
|
|
|
//srand(static_cast<unsigned int>(time(0)));
|
|
|
|
for (auto pr:gen_v) { |
|
|
|
std::string key = pr.first; |
|
|
|
std::string value; |
|
|
|
status = db->Get(readOptions, key, &value); |
|
|
|
ASSERT_TRUE(status.ok()); |
|
|
@ -69,42 +71,46 @@ TEST(TestTTL, ReadTTL) { |
|
|
|
|
|
|
|
Env::Default()->SleepForMicroseconds(ttl * 1000000); |
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
int key_ = rand() % key_num+1; |
|
|
|
std::string key = std::to_string(key_); |
|
|
|
for (auto pr:gen_v) { |
|
|
|
std::string key = pr.first; |
|
|
|
std::string value; |
|
|
|
status = db->Get(readOptions, key, &value); |
|
|
|
Slice slice_key= Slice(key); |
|
|
|
status = db->Get(readOptions,slice_key, &value); |
|
|
|
ASSERT_FALSE(status.ok()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
TEST(TestTTL, CompactionTTL) { |
|
|
|
DB *db; |
|
|
|
|
|
|
|
if(OpenDB("testdb", &db).ok() == false) { |
|
|
|
std::cerr << "open db failed" << std::endl; |
|
|
|
abort(); |
|
|
|
} |
|
|
|
|
|
|
|
uint64_t ttl = 20; |
|
|
|
InsertData(db, ttl); |
|
|
|
|
|
|
|
leveldb::Range ranges[1]; |
|
|
|
ranges[0] = leveldb::Range("-", "A"); |
|
|
|
uint64_t sizes[1]; |
|
|
|
db->GetApproximateSizes(ranges, 1, sizes); |
|
|
|
ASSERT_GT(sizes[0], 0); |
|
|
|
|
|
|
|
Env::Default()->SleepForMicroseconds(ttl * 1000000); |
|
|
|
|
|
|
|
db->CompactRange(nullptr, nullptr); |
|
|
|
|
|
|
|
leveldb::Range ranges[1]; |
|
|
|
ranges[0] = leveldb::Range("-", "A"); |
|
|
|
uint64_t sizes[1]; |
|
|
|
db->GetApproximateSizes(ranges, 1, sizes); |
|
|
|
ASSERT_EQ(sizes[0], 0); |
|
|
|
} |
|
|
|
// TEST(TestTTL, CompactionTTL) {
|
|
|
|
// DB *db;
|
|
|
|
|
|
|
|
// if(OpenDB("testdb", &db).ok() == false) {
|
|
|
|
// std::cerr << "open db failed" << std::endl;
|
|
|
|
// abort();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// uint64_t ttl = 20;
|
|
|
|
// InsertData(db, ttl);
|
|
|
|
|
|
|
|
// {
|
|
|
|
// leveldb::Range ranges[1];
|
|
|
|
// ranges[0] = leveldb::Range("-", "A");
|
|
|
|
// uint64_t sizes[1];
|
|
|
|
// db->GetApproximateSizes(ranges, 1, sizes);
|
|
|
|
// ASSERT_GT(sizes[0], 0);
|
|
|
|
|
|
|
|
// }
|
|
|
|
// Env::Default()->SleepForMicroseconds(ttl * 1000000);
|
|
|
|
|
|
|
|
// db->CompactRange(nullptr, nullptr);
|
|
|
|
|
|
|
|
// {
|
|
|
|
// leveldb::Range ranges[1];
|
|
|
|
// ranges[0] = leveldb::Range("-", "A");
|
|
|
|
// uint64_t sizes[1];
|
|
|
|
// db->GetApproximateSizes(ranges, 1, sizes);
|
|
|
|
// ASSERT_EQ(sizes[0], 0);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|