diff --git a/db/builder.cc b/db/builder.cc index e6329e0..4c6d802 100644 --- a/db/builder.cc +++ b/db/builder.cc @@ -29,11 +29,17 @@ Status BuildTable(const std::string& dbname, Env* env, const Options& options, } TableBuilder* builder = new TableBuilder(options, file); - meta->smallest.DecodeFrom(iter->key()); + meta->smallest.DecodeFrom(iter->key());//这里是internal_key +// auto tmp_ts = DecodeFixed64(iter->value().data() + iter->value().size() - kTSLength); +// meta->oldest_ts = tmp_ts; +// meta->newer_ts = tmp_ts; Slice key; for (; iter->Valid(); iter->Next()) { key = iter->key(); builder->Add(key, iter->value()); +// tmp_ts = DecodeFixed64(iter->value().data() + iter->value().size() - kTSLength); +// meta->oldest_ts = meta->oldest_ts > tmp_ts ? tmp_ts : meta->oldest_ts; +// meta->newer_ts = meta->newer_ts > tmp_ts ? meta->newer_ts : tmp_ts; } if (!key.empty()) { meta->largest.DecodeFrom(key); diff --git a/db/db_impl.cc b/db/db_impl.cc index 4d47e9e..b117cb1 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -510,7 +510,7 @@ Status DBImpl::WriteLevel0Table(MemTable* mem, VersionEdit* edit, Status s; { mutex_.Unlock(); - s = BuildTable(dbname_, env_, options_, table_cache_, iter, &meta); + s = BuildTable(dbname_, env_, options_, table_cache_, iter, &meta);//meta包含largest_key,此时未刷盘meta,但meta.number对应生成的file会刷盘 mutex_.Lock(); } @@ -588,6 +588,7 @@ void DBImpl::CompactRange(const Slice* begin, const Slice* end) { for (int level = 0; level < max_level_with_files; level++) { TEST_CompactRange(level, begin, end); } + TEST_CompactRange(max_level_with_files, begin, end); } void DBImpl::TEST_CompactRange(int level, const Slice* begin, @@ -618,7 +619,7 @@ void DBImpl::TEST_CompactRange(int level, const Slice* begin, bg_error_.ok()) { if (manual_compaction_ == nullptr) { // Idle manual_compaction_ = &manual; - MaybeScheduleCompaction(); + MaybeScheduleCompaction();//有可能寻址过多,导致allow_seek为0,触发合并。 } else { // Running either my compaction or another compaction. background_work_finished_signal_.Wait(); } @@ -1166,7 +1167,7 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key, auto t2 = GetTS(value); if(t1 >= t2){ // 过期 - s = Status::Expire("Expire",Slice()); + s = Status::NotFound("NotFound",Slice()); } else { // 没过期 *value = value->substr(0, value->size() - sizeof(uint64_t)); @@ -1515,11 +1516,15 @@ void DBImpl::AppendTS(const Slice& val, std::string* val_with_ts,uint64_t ttl) { * @param val * @return timestamp in val,and remove timestamp from val */ -uint64_t DBImpl::GetTS(const std::string* val) { - // 不用auto再写一下 - uint64_t expiration_time; - memcpy(&expiration_time, val->data() + val->size() - sizeof(uint64_t), sizeof(uint64_t)); - return expiration_time; +uint64_t DBImpl::GetTS(std::string* val) { + // 不用auto再写一下 老逻辑: + // uint64_t expiration_time; + // memcpy(&expiration_time, val->data() + val->size() - sizeof(uint64_t), sizeof(uint64_t)); + // return expiration_time; + // 新逻辑: + auto expiration_time = DecodeFixed64(val->data() + val->size() - kTSLength); + val->resize(val->size() - kTSLength); + return expiration_time; } // Default implementations of convenience methods that subclasses of DB @@ -1535,14 +1540,18 @@ Status DB::Put(const WriteOptions& options, const Slice& key, const Slice& value, uint64_t ttl) { // 将 value 和 expiration_time 合并到一起,形成带 TTL 的 value std::string val_with_ts; - val_with_ts.reserve(value.size() + sizeof(uint64_t)); + val_with_ts.reserve(value.size() + kTSLength); uint64_t expiration_time = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count() + ttl * 1000; + + char ts_string[kTSLength]; + EncodeFixed64(ts_string, expiration_time); // 追加原始 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)); + // val_with_ts.append(reinterpret_cast(&expiration_time), sizeof(expiration_time)); + val_with_ts.append(ts_string, kTSLength); // std::cout<<"PUT"<