|
@ -1,16 +1,13 @@ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
|
#include "leveldb/env.h"
|
|
|
#include "leveldb/env.h"
|
|
|
#include "leveldb/db.h"
|
|
|
#include "leveldb/db.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace leveldb; |
|
|
using namespace leveldb; |
|
|
|
|
|
|
|
|
constexpr int value_size = 2048; |
|
|
constexpr int value_size = 2048; |
|
|
constexpr int data_size = 128 << 20; |
|
|
constexpr int data_size = 128 << 20; |
|
|
constexpr int key_num = 10000; |
|
|
|
|
|
|
|
|
|
|
|
Status OpenDB(std::string dbName, DB **db) { |
|
|
Status OpenDB(std::string dbName, DB **db) { |
|
|
Options options; |
|
|
Options options; |
|
@ -20,15 +17,31 @@ Status OpenDB(std::string dbName, DB **db) { |
|
|
|
|
|
|
|
|
void InsertData(DB *db, uint64_t ttl/* second */) { |
|
|
void InsertData(DB *db, uint64_t ttl/* second */) { |
|
|
WriteOptions writeOptions; |
|
|
WriteOptions writeOptions; |
|
|
|
|
|
int key_num = data_size / value_size; |
|
|
|
|
|
srand(0); |
|
|
|
|
|
|
|
|
for (int i = 0; i < key_num; i++) { |
|
|
for (int i = 0; i < key_num; i++) { |
|
|
int key_ = i; |
|
|
|
|
|
|
|
|
int key_ = rand() % key_num+1; |
|
|
std::string key = std::to_string(key_); |
|
|
std::string key = std::to_string(key_); |
|
|
std::string value(value_size, 'a'); |
|
|
std::string value(value_size, 'a'); |
|
|
db->Put(writeOptions, key, value, ttl); |
|
|
db->Put(writeOptions, key, value, ttl); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void GetData(DB *db, int size = (1 << 30)) { |
|
|
|
|
|
ReadOptions readOptions; |
|
|
|
|
|
int key_num = data_size / value_size; |
|
|
|
|
|
|
|
|
|
|
|
// 点查
|
|
|
|
|
|
srand(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) { |
|
|
TEST(TestTTL, ReadTTL) { |
|
|
DB *db; |
|
|
DB *db; |
|
|
if(OpenDB("testdb", &db).ok() == false) { |
|
|
if(OpenDB("testdb", &db).ok() == false) { |
|
@ -36,62 +49,121 @@ TEST(TestTTL, ReadTTL) { |
|
|
abort(); |
|
|
abort(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
uint64_t ttl = 10; |
|
|
|
|
|
|
|
|
uint64_t ttl = 20; |
|
|
|
|
|
|
|
|
InsertData(db, ttl); |
|
|
InsertData(db, ttl); |
|
|
|
|
|
|
|
|
ReadOptions readOptions; |
|
|
ReadOptions readOptions; |
|
|
Status status; |
|
|
Status status; |
|
|
srand(static_cast<unsigned int>(time(0))); |
|
|
|
|
|
for (int i = 0; i < key_num; i++) { |
|
|
|
|
|
int key_ = i; |
|
|
|
|
|
|
|
|
int key_num = data_size / value_size; |
|
|
|
|
|
srand(0); |
|
|
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
|
|
int key_ = rand() % key_num+1; |
|
|
std::string key = std::to_string(key_); |
|
|
std::string key = std::to_string(key_); |
|
|
std::string value; |
|
|
std::string value; |
|
|
status = db->Get(readOptions, key, &value); |
|
|
status = db->Get(readOptions, key, &value); |
|
|
ASSERT_TRUE(status.ok()); |
|
|
ASSERT_TRUE(status.ok()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Env::Default()->SleepForMicroseconds((ttl+5) * 1000000); |
|
|
|
|
|
|
|
|
Env::Default()->SleepForMicroseconds((ttl+1) * 1000000); |
|
|
|
|
|
|
|
|
for (int i = 0; i < key_num; i++) { |
|
|
|
|
|
int key_ = i; |
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) { |
|
|
|
|
|
int key_ = rand() % key_num+1; |
|
|
std::string key = std::to_string(key_); |
|
|
std::string key = std::to_string(key_); |
|
|
std::string value; |
|
|
std::string value; |
|
|
status = db->Get(readOptions, key, &value); |
|
|
status = db->Get(readOptions, key, &value); |
|
|
if(status.ok()){ |
|
|
|
|
|
std::cout<<"!!"; |
|
|
|
|
|
} |
|
|
|
|
|
ASSERT_FALSE(status.ok()); |
|
|
ASSERT_FALSE(status.ok()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
delete db; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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+1) * 1000000); |
|
|
|
|
|
|
|
|
|
|
|
db->CompactRange(nullptr, nullptr); |
|
|
|
|
|
|
|
|
|
|
|
leveldb::Range ranges_1[1]; |
|
|
|
|
|
ranges[0] = leveldb::Range("-", "A"); |
|
|
|
|
|
uint64_t sizes_1[1]; |
|
|
|
|
|
db->GetApproximateSizes(ranges_1, 1, sizes_1); |
|
|
|
|
|
ASSERT_EQ(sizes_1[0], 0); |
|
|
|
|
|
|
|
|
|
|
|
delete db; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// TEST(TestTTL, CompactionTTL) {
|
|
|
|
|
|
// DB *db;
|
|
|
|
|
|
|
|
|
|
|
|
// if(OpenDB("testdb", &db).ok() == false) {
|
|
|
|
|
|
// std::cerr << "open db failed" << std::endl;
|
|
|
|
|
|
// abort();
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
TEST(TestTTL, OurTTL) { |
|
|
|
|
|
DB *db; |
|
|
|
|
|
WriteOptions writeOptions; |
|
|
|
|
|
ReadOptions readOptions; |
|
|
|
|
|
if(OpenDB("testdb_for_XOY", &db).ok() == false) { |
|
|
|
|
|
std::cerr << "open db failed" << std::endl; |
|
|
|
|
|
abort(); |
|
|
|
|
|
} |
|
|
|
|
|
for (int i = 0; i < 10000; i++) { |
|
|
|
|
|
std::string key = std::to_string(i); |
|
|
|
|
|
std::string value = std::to_string(i); |
|
|
|
|
|
db->Put(writeOptions, key, value); |
|
|
|
|
|
} |
|
|
|
|
|
for (int i = 0; i < 10000; i++) { |
|
|
|
|
|
std::string key = std::to_string(i); |
|
|
|
|
|
std::string value = std::to_string(i*2); |
|
|
|
|
|
db->Put(writeOptions, key, value, 30); |
|
|
|
|
|
} |
|
|
|
|
|
for (int i = 0; i < 10000; i++) { |
|
|
|
|
|
std::string key = std::to_string(i); |
|
|
|
|
|
std::string value = std::to_string(i*3); |
|
|
|
|
|
db->Put(writeOptions, key, value, 15); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// uint64_t ttl = 20;
|
|
|
|
|
|
// InsertData(db, ttl);
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10000; i++) { |
|
|
|
|
|
std::string key = std::to_string(i); |
|
|
|
|
|
std::string value; |
|
|
|
|
|
Status status = db->Get(readOptions, key, &value); |
|
|
|
|
|
ASSERT_TRUE(status.ok()); |
|
|
|
|
|
ASSERT_TRUE(value==std::to_string(i*3)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Env::Default()->SleepForMicroseconds((15+1) * 1000000); |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10000; i++) { |
|
|
|
|
|
std::string key = std::to_string(i); |
|
|
|
|
|
std::string value; |
|
|
|
|
|
Status status = db->Get(readOptions, key, &value); |
|
|
|
|
|
ASSERT_TRUE(status.ok()); |
|
|
|
|
|
ASSERT_TRUE(value==std::to_string(i*2)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 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((15+1) * 1000000); |
|
|
|
|
|
|
|
|
// Env::Default()->SleepForMicroseconds(ttl * 1000000);
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10000; i++) { |
|
|
|
|
|
std::string key = std::to_string(i); |
|
|
|
|
|
std::string value; |
|
|
|
|
|
Status status = db->Get(readOptions, key, &value); |
|
|
|
|
|
ASSERT_TRUE(status.ok()); |
|
|
|
|
|
ASSERT_TRUE(value==std::to_string(i)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// db->CompactRange(nullptr, nullptr);
|
|
|
|
|
|
|
|
|
delete db; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 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) { |
|
|
int main(int argc, char** argv) { |
|
|