From 153ebe44b7e585eaac3faf8ce9ca99fc3a3abb71 Mon Sep 17 00:00:00 2001 From: lzj <2541406139@qq.com> Date: Wed, 30 Oct 2024 05:59:41 -0700 Subject: [PATCH] pass testRead,func of CompactRange waiting for rectify --- db/db_impl.cc | 12 ++-- test/db_test2.cc | 92 +++++----------------------- test/simple_test.cc | 99 ++++++++++++++++++++++--------- test/ttl_test.cc | 168 ++++++++++++++-------------------------------------- 4 files changed, 134 insertions(+), 237 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 0d5e730..d66a01c 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1167,8 +1167,8 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key, if(s.ok()){ auto a = env_->GetCurrentTime(); auto b = GetTS(value); - std::cout<< "read when " << a<GetCurrentTime() > GetTS(value)){ return Status::Expire("Expire",Slice()); } @@ -1211,6 +1211,7 @@ Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) { } Status DBImpl::Put(const WriteOptions& options, const Slice& key, const Slice& value, uint64_t ttl) { + return DB::Put(options, key, value,ttl); } @@ -1514,6 +1515,7 @@ void DBImpl::AppendTS(const Slice& val, std::string* val_with_ts,uint64_t ttl) { uint64_t DBImpl::GetTS(const std::string* val) { uint64_t expiration_time; + //auto expiration_time = DecodeFixed64(reinterpret_cast(val)); memcpy(&expiration_time, val->data() + val->size() - sizeof(uint64_t), sizeof(uint64_t)); return expiration_time; } @@ -1536,15 +1538,15 @@ Status DB::Put(const WriteOptions& options, const Slice& key, uint64_t expiration_time = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) - .count() + ttl; + .count() + ttl * 1000; // 追加原始 value 到 val_with_ts val_with_ts.append(value.data(), value.size()); // 将 expiration_time 追加到 val_with_ts val_with_ts.append(reinterpret_cast(&expiration_time), sizeof(expiration_time)); - std::cout<<"PUT"<Put(writeOptions, key, value); -// } -//} - -void InsertData(DB *db, uint64_t ttl/* second */) { +void InsertData(DB *db) { WriteOptions writeOptions; int key_num = data_size / value_size; - srand(static_cast(time(0))); + srand(0); for (int i = 0; i < key_num; i++) { int key_ = rand() % key_num+1; - key_ = 1; std::string key = std::to_string(key_); std::string value(value_size, 'a'); - db->Put(writeOptions, key, value, ttl); - std::cout << "time to alive" << ttl << std::endl; - break; + db->Put(writeOptions, key, value); } } + // 2. 数据访问(如何读数据) void GetData(DB *db, int size = (1 << 30)) { ReadOptions readOptions; int key_num = data_size / value_size; - + // 点查 - srand(static_cast(time(0))); + 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); - //break; } // 范围查询 @@ -82,54 +56,16 @@ void GetData(DB *db, int size = (1 << 30)) { int main() { -// DB *db; -// if(OpenDB("testdb", &db).ok()) { -// uint64_t ttl = 20; -// -// InsertData(db, ttl); -// delete db; -// } -// -// if(OpenDB("testdb", &db).ok()) { -// GetData(db); -// delete db; -// } -// DB *db; - if(OpenDB("testdb", &db).ok() == false) { - std::cerr << "open db failed" << std::endl; - abort(); + if(OpenDB("testdb", &db).ok()) { + InsertData(db); + delete db; } - uint64_t ttl = 200; - - InsertData(db, ttl); - - ReadOptions readOptions; - Status status; - int key_num = data_size / value_size; - srand(static_cast(time(0))); - for (int i = 0; i < 100; i++) { - int key_ = rand() % key_num+1; - key_ = 1; - std::string key = std::to_string(key_); - std::string value; - status = db->Get(readOptions, key, &value); - assert(status.ok()); - break; + if(OpenDB("testdb", &db).ok()) { + GetData(db); + delete db; } - Env::Default()->SleepForMicroseconds(ttl * 10000); - - for (int i = 0; i < 100; i++) { - int key_ = rand() % key_num+1; - key_ = 1; - std::string key = std::to_string(key_); - std::string value; - status = db->Get(readOptions, key, &value); - assert(status.ok() != true); - break; - } return 0; } - diff --git a/test/simple_test.cc b/test/simple_test.cc index a4e5f3c..b0fb505 100644 --- a/test/simple_test.cc +++ b/test/simple_test.cc @@ -40,54 +40,95 @@ 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(time(0))); +// for (int i = 0; i < key_num; i++) { +// int key_ = rand() % key_num+1; +// key_ = 1; +// std::string key = std::to_string(key_); +// std::string value(value_size, 'a'); +// db->Put(writeOptions, key, value, ttl); +// std::cout << "time to alive" << ttl << std::endl; +// break; +// } +//} + void InsertData(DB *db, uint64_t ttl/* second */) { WriteOptions writeOptions; int key_num = data_size / value_size; - srand(static_cast(time(0))); + srand(0); + for (int i = 0; i < key_num; i++) { int key_ = rand() % key_num+1; - key_ = 1; std::string key = std::to_string(key_); std::string value(value_size, 'a'); db->Put(writeOptions, key, value, ttl); - std::cout << "time to alive" << ttl << std::endl; - break; } } - -int main() { - +int main(){ DB *db; + if(OpenDB("testdb", &db).ok() == false) { std::cerr << "open db failed" << std::endl; abort(); } - uint64_t ttl = 200; - -// InsertData(db, ttl); - WriteOptions writeOptions; - int key_ = 1; - std::string key = std::to_string(key_); - std::string value(1, 'a'); - db->Put(writeOptions, key, value, ttl); - std::cout << "time to alive" << ttl << std::endl; + uint64_t ttl = 20; + InsertData(db, ttl); - ReadOptions readOptions; - std::string value_read; - Status status = db->Get(readOptions, key, &value); + leveldb::Range ranges[1]; + ranges[0] = leveldb::Range("-", "A"); + uint64_t sizes[1]; + db->GetApproximateSizes(ranges, 1, sizes); + //ASSERT_GT(sizes[0], 0); + assert(sizes[0] > 0); + Env::Default()->SleepForMicroseconds(ttl * 1000000); + db->CompactRange(nullptr, nullptr); - //std::cout<<"start sleep for " << - Env::Default()->SleepForMicroseconds(ttl * 100000); - + leveldb::Range ranges2[1]; + ranges2[0] = leveldb::Range("-", "A"); + uint64_t sizes2[1]; + db->GetApproximateSizes(ranges2, 1, sizes2); + assert(sizes2[0] == 0); +} - key_ = 1; - key = std::to_string(key_); - std::string value_read_second; - status = db->Get(readOptions, key, &value_read_second); - assert(status.ok() != true); - return 0; -} +//int main() { +// +// DB *db; +// if(OpenDB("testdb", &db).ok() == false) { +// std::cerr << "open db failed" << std::endl; +// abort(); +// } +// +// uint64_t ttl = 200; +// +//// InsertData(db, ttl); +// WriteOptions writeOptions; +// int key_ = 1; +// std::string key = std::to_string(key_); +// std::string value(1, 'a'); +// db->Put(writeOptions, key, value, ttl); +// std::cout << "time to alive" << ttl << std::endl; +// +// ReadOptions readOptions; +// std::string value_read; +// Status status = db->Get(readOptions, key, &value); +// +// +// //std::cout<<"start sleep for " << +// Env::Default()->SleepForMicroseconds(ttl * 100000); +// +// +// key_ = 1; +// key = std::to_string(key_); +// std::string value_read_second; +// status = db->Get(readOptions, key, &value_read_second); +// assert(status.ok() != true); +// +// return 0; +//} diff --git a/test/ttl_test.cc b/test/ttl_test.cc index ce43f04..745cc15 100644 --- a/test/ttl_test.cc +++ b/test/ttl_test.cc @@ -1,25 +1,7 @@ - - -#include "gtest/gtest.h" - -#include "leveldb/env.h" -#include "leveldb/db.h" - - -using namespace leveldb; - -constexpr int value_size = 2048; -constexpr int data_size = 128 << 20; - -Status OpenDB(std::string dbName, DB **db) { - Options options; - options.create_if_missing = true; #include "gtest/gtest.h" - #include "leveldb/env.h" #include "leveldb/db.h" - using namespace leveldb; constexpr int value_size = 2048; @@ -34,7 +16,7 @@ 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(time(0))); + srand(0); for (int i = 0; i < key_num; i++) { int key_ = rand() % key_num+1; @@ -44,12 +26,12 @@ void InsertData(DB *db, uint64_t ttl/* second */) { } } -void GetData(DB *db, int size = (1 << 30)){ +void GetData(DB *db, int size = (1 << 30)) { ReadOptions readOptions; int key_num = data_size / value_size; - + // 点查 - srand(static_cast(time(0))); + srand(0); for (int i = 0; i < 100; i++) { int key_ = rand() % key_num+1; std::string key = std::to_string(key_); @@ -59,135 +41,71 @@ void GetData(DB *db, int size = (1 << 30)){ } TEST(TestTTL, ReadTTL) { - DB *db; - if(OpenDB("testdb", &db).ok() == false) { - std::cerr << "open db failed" << std::endl; - abort(); - } - - uint64_t ttl = 20; - - InsertData(db, ttl); - - ReadOptions readOptions; - Status status; - int key_num = data_size / value_size; - srand(static_cast(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; - status = db->Get(readOptions, key, &value); - ASSERT_TRUE(status.ok()); - } - - 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_); - std::string value; - status = db->Get(readOptions, key, &value); - ASSERT_FALSE(status.ok()); - } -} + DB *db; + if(OpenDB("testdb", &db).ok() == false) { + std::cerr << "open db failed" << std::endl; + abort(); + } - return DB::Open(options, dbName, db); -} + uint64_t ttl = 20; -void InsertData(DB *db, uint64_t ttl/* second */) { - WriteOptions writeOptions; - int key_num = data_size / value_size; - srand(static_cast(time(0))); + InsertData(db, ttl); - for (int i = 0; i < key_num; i++) { + ReadOptions readOptions; + Status status; + 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(value_size, 'a'); - db->Put(writeOptions, key, value, ttl); + std::string value; + status = db->Get(readOptions, key, &value); + ASSERT_TRUE(status.ok()); } -} -void GetData(DB *db, int size = (1 << 30)) { - ReadOptions readOptions; - int key_num = data_size / value_size; - - // 点查 - srand(static_cast(time(0))); + 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_); std::string value; - db->Get(readOptions, key, &value); + status = db->Get(readOptions, key, &value); + ASSERT_FALSE(status.ok()); } -} -TEST(TestTTL, ReadTTL) { - DB *db; - if(OpenDB("testdb", &db).ok() == false) { - std::cerr << "open db failed" << std::endl; - abort(); - } - - uint64_t ttl = 20; - - InsertData(db, ttl); - - ReadOptions readOptions; - Status status; - int key_num = data_size / value_size; - srand(static_cast(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; - status = db->Get(readOptions, key, &value); - ASSERT_TRUE(status.ok()); - } - - 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_); - std::string value; - status = db->Get(readOptions, key, &value); - ASSERT_FALSE(status.ok()); - } } TEST(TestTTL, CompactionTTL) { - DB *db; + DB *db; - if(OpenDB("testdb", &db).ok() == false) { - std::cerr << "open db failed" << std::endl; - abort(); - } + if(OpenDB("testdb", &db).ok() == false) { + std::cerr << "open db failed" << std::endl; + abort(); + } - uint64_t ttl = 20; - InsertData(db, ttl); + 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); + 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); + Env::Default()->SleepForMicroseconds(ttl * 1000000); - db->CompactRange(nullptr, nullptr); + 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); + leveldb::Range ranges2[1]; + ranges2[0] = leveldb::Range("-", "A"); + uint64_t sizes2[1]; + db->GetApproximateSizes(ranges2, 1, sizes2); + ASSERT_EQ(sizes[0], 0); } int main(int argc, char** argv) { // All tests currently run with the same read-only file limits. - testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); -} +} \ No newline at end of file