Pārlūkot izejas kodu

pass testRead,func of CompactRange waiting for rectify

lzj_version
林子骥 pirms 3 nedēļām
vecāks
revīzija
153ebe44b7
4 mainītis faili ar 134 papildinājumiem un 237 dzēšanām
  1. +7
    -5
      db/db_impl.cc
  2. +14
    -78
      test/db_test2.cc
  3. +70
    -29
      test/simple_test.cc
  4. +43
    -125
      test/ttl_test.cc

+ 7
- 5
db/db_impl.cc Parādīt failu

@ -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<<std::endl;
std::cout<< "read timestamp "<<b << std::endl;
// std::cout<< "read when " << a<<std::endl;
// std::cout<< "read timestamp "<<b << std::endl;
if(env_->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<const char*>(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::milliseconds>(
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<const char*>(&expiration_time), sizeof(expiration_time));
std::cout<<"PUT"<<std::endl;
std::cout << "timestamp: " << expiration_time << std::endl;
// std::cout<<"PUT"<<std::endl;
// std::cout << "timestamp: " << expiration_time << std::endl;
//"a\323='\277\222\001\000"
WriteBatch batch;
batch.Put(key, Slice(val_with_ts));

+ 14
- 78
test/db_test2.cc Parādīt failu

@ -1,21 +1,12 @@
#include "leveldb/db.h"
#include "leveldb/filter_policy.h"
#include "leveldb/env.h"
#include "leveldb/db.h"
using namespace leveldb;
constexpr int value_size = 2048;
constexpr int data_size = 128 << 20;
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace leveldb;
constexpr int value_size = 2048;
constexpr int data_size = 256 << 20;
// 3. 数据管理(Manifest/创建/恢复数据库)
Status OpenDB(std::string dbName, DB **db) {
@ -27,48 +18,31 @@ Status OpenDB(std::string dbName, DB **db) {
// 1. 存储(数据结构与写入)
// 4. 数据合并(Compaction)
//void InsertData(DB *db) {
// WriteOptions writeOptions;
// int key_num = data_size / value_size;
// srand(static_cast<unsigned int>(time(0)));
//
// 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->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<unsigned int>(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<unsigned int>(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<unsigned int>(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;
}

+ 70
- 29
test/simple_test.cc Parādīt failu

@ -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<unsigned int>(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<unsigned int>(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;
//}

+ 43
- 125
test/ttl_test.cc Parādīt failu

@ -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<unsigned int>(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<unsigned int>(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<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;
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<unsigned int>(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<unsigned int>(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<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;
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();
}
}

Notiek ielāde…
Atcelt
Saglabāt