#3 minor compaction test ttl & enhance effeciency of reading current time

Merged
谢瑞阳 merged 1 commits from xxy into master 1 month ago
  1. +10
    -1
      db/builder.cc
  2. +5
    -2
      db/db_impl.cc
  3. +1
    -1
      test/ttl_test.cc

+ 10
- 1
db/builder.cc View File

@ -30,8 +30,17 @@ Status BuildTable(const std::string& dbname, Env* env, const Options& options,
TableBuilder* builder = new TableBuilder(options, file); TableBuilder* builder = new TableBuilder(options, file);
meta->smallest.DecodeFrom(iter->key()); meta->smallest.DecodeFrom(iter->key());
Slice key;
Slice key=iter->key();
// 小合并时检查ttl
time_t now = time(nullptr); // 获得当前时间
uint64_t now_time=static_cast<uint64_t>(now);
for (; iter->Valid(); iter->Next()) { for (; iter->Valid(); iter->Next()) {
uint64_t ttl=*(uint64_t*)(iter->value().data()+iter->value().size()-sizeof(uint64_t)); // 将 TTL 从 new_data 的末尾取出
// 如果 TTL 超过当前时间,说明数据已经过期
if(ttl < static_cast<uint64_t>(now_time)){
continue;
}
key = iter->key(); key = iter->key();
builder->Add(key, iter->value()); builder->Add(key, iter->value());
} }

+ 5
- 2
db/db_impl.cc View File

@ -57,6 +57,8 @@ struct DBImpl::CompactionState {
uint64_t number; uint64_t number;
uint64_t file_size; uint64_t file_size;
InternalKey smallest, largest; InternalKey smallest, largest;
// uint64_t now = static_cast<uint64_t>(time(nullptr)); // 合并发起的时间
}; };
Output* current_output() { return &outputs[outputs.size() - 1]; } Output* current_output() { return &outputs[outputs.size() - 1]; }
@ -923,12 +925,13 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) {
std::string current_user_key; std::string current_user_key;
bool has_current_user_key = false; bool has_current_user_key = false;
SequenceNumber last_sequence_for_key = kMaxSequenceNumber; SequenceNumber last_sequence_for_key = kMaxSequenceNumber;
time_t now = time(nullptr); // 获得当前时间
uint64_t now_time=static_cast<uint64_t>(now);
while (input->Valid() && !shutting_down_.load(std::memory_order_acquire)) { while (input->Valid() && !shutting_down_.load(std::memory_order_acquire)) {
auto x=input->value(); // 获取键值对 value auto x=input->value(); // 获取键值对 value
uint64_t ttl=*(uint64_t*)(x.data()+x.size()-sizeof(uint64_t));// 将 TTL 从 new_data 的末尾取出 uint64_t ttl=*(uint64_t*)(x.data()+x.size()-sizeof(uint64_t));// 将 TTL 从 new_data 的末尾取出
time_t now = time(nullptr); // 获得当前时间
// 如果 TTL 超过当前时间,说明数据已经过期 // 如果 TTL 超过当前时间,说明数据已经过期
if(ttl < static_cast<uint64_t>(now)){
if(ttl < now_time){
Log(options_.info_log, "delete record for ttl"); Log(options_.info_log, "delete record for ttl");
input->Next(); // 将 input 指向下一个键值对 input->Next(); // 将 input 指向下一个键值对
continue; continue;

+ 1
- 1
test/ttl_test.cc View File

@ -65,7 +65,7 @@ TEST(TestTTL, ReadTTL) {
ASSERT_TRUE(status.ok()); ASSERT_TRUE(status.ok());
} }
Env::Default()->SleepForMicroseconds((ttl+1) * 1000000);
Env::Default()->SleepForMicroseconds((ttl+2) * 1000000);
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
int key_ = rand() % key_num+1; int key_ = rand() % key_num+1;

Loading…
Cancel
Save