diff --git a/db/builder.cc b/db/builder.cc index e6329e0..5a0e07f 100644 --- a/db/builder.cc +++ b/db/builder.cc @@ -31,9 +31,18 @@ Status BuildTable(const std::string& dbname, Env* env, const Options& options, TableBuilder* builder = new TableBuilder(options, file); meta->smallest.DecodeFrom(iter->key()); Slice key; + ParsedInternalKey parsed; + meta->smallest_deadtime = UINT64_MAX; + meta->largest_deadtime = 0; for (; iter->Valid(); iter->Next()) { key = iter->key(); builder->Add(key, iter->value()); + //在构建sstable文件的时候,记录当前文件的生存期最大和最小值, + //这里要注意internalkey和metadata中对于没有生存期的表示的转换 + ParseInternalKey(key,&parsed); + if(parsed.deadTime == 0) parsed.deadTime = UINT64_MAX; + meta->smallest_deadtime = std::min(meta->smallest_deadtime,parsed.deadTime); + meta->largest_deadtime = std::max(meta->largest_deadtime,parsed.deadTime); } if (!key.empty()) { meta->largest.DecodeFrom(key); diff --git a/db/db_impl.cc b/db/db_impl.cc index 4617e18..6513f89 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -52,11 +52,12 @@ struct DBImpl::Writer { }; struct DBImpl::CompactionState { - // Files produced by compaction + // Files produced by compaction 这里的改动和filemetadata对应 struct Output { uint64_t number; uint64_t file_size; InternalKey smallest, largest; + uint64_t smallest_deadtime,largest_deadtime; }; Output* current_output() { return &outputs[outputs.size() - 1]; } @@ -536,7 +537,7 @@ Status DBImpl::WriteLevel0Table(MemTable* mem, VersionEdit* edit, level = base->PickLevelForMemTableOutput(min_user_key, max_user_key); } edit->AddFile(level, meta.number, meta.file_size, meta.smallest, - meta.largest); + meta.largest,meta.smallest_deadtime,meta.largest_deadtime); } CompactionStats stats; @@ -740,7 +741,7 @@ void DBImpl::BackgroundCompaction() { FileMetaData* f = c->input(0, 0); c->edit()->RemoveFile(c->level(), f->number); c->edit()->AddFile(c->level() + 1, f->number, f->file_size, f->smallest, - f->largest); + f->largest,f->smallest_deadtime,f->largest_deadtime); status = versions_->LogAndApply(c->edit(), &mutex_); if (!status.ok()) { RecordBackgroundError(status); @@ -814,6 +815,8 @@ Status DBImpl::OpenCompactionOutputFile(CompactionState* compact) { out.number = file_number; out.smallest.Clear(); out.largest.Clear(); + out.smallest_deadtime = UINT64_MAX; + out.largest_deadtime = 0; compact->outputs.push_back(out); mutex_.Unlock(); } @@ -889,7 +892,7 @@ Status DBImpl::InstallCompactionResults(CompactionState* compact) { for (size_t i = 0; i < compact->outputs.size(); i++) { const CompactionState::Output& out = compact->outputs[i]; compact->compaction->edit()->AddFile(level + 1, out.number, out.file_size, - out.smallest, out.largest); + out.smallest, out.largest,out.smallest_deadtime,out.largest_deadtime); } return versions_->LogAndApply(compact->compaction->edit(), &mutex_); } @@ -1003,6 +1006,17 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) { compact->current_output()->smallest.DecodeFrom(key); } compact->current_output()->largest.DecodeFrom(key); + + ParsedInternalKey parsed; + ParseInternalKey(key,&parsed); + uint64_t &smallest_deadtime = compact->current_output()->smallest_deadtime; + uint64_t &largest_deadtime = compact->current_output()->largest_deadtime; + if(parsed.deadTime == 0) { + smallest_deadtime = UINT64_MAX; + } + smallest_deadtime = std::min(smallest_deadtime,parsed.deadTime); + largest_deadtime = std::max(largest_deadtime,parsed.deadTime); + compact->builder->Add(key, input->value()); // Close output file if it is big enough @@ -1206,10 +1220,10 @@ Status DBImpl::Put(const WriteOptions& o, const Slice& key, return DB::Put(o, key, val, ttl); } -Status DBImpl::Put(const WriteOptions& options, const Slice& key, - const Slice& value, uint64_t ttl) { - return DB::Put(options,key,value,ttl); -} +// Status DBImpl::Put(const WriteOptions& options, const Slice& key, +// const Slice& value, uint64_t ttl) { +// return DB::Put(options,key,value,ttl); +// } Status DBImpl::Delete(const WriteOptions& options, const Slice& key) { return DB::Delete(options, key); @@ -1505,13 +1519,13 @@ Status DB::Put(const WriteOptions& opt, const Slice& key, return Write(opt, &batch); } -//为了通过编译,忽略ttl -Status DB::Put(const WriteOptions& options, const Slice& key, - const Slice& value, uint64_t ttl) { - WriteBatch batch; - batch.Put(key, value); - return Write(options, &batch); -} +// //为了通过编译,忽略ttl +// Status DB::Put(const WriteOptions& options, const Slice& key, +// const Slice& value, uint64_t ttl) { +// WriteBatch batch; +// batch.Put(key, value); +// return Write(options, &batch); +// } Status DB::Delete(const WriteOptions& opt, const Slice& key) { WriteBatch batch; diff --git a/db/dbformat.cc b/db/dbformat.cc index 686d04f..6f11a1d 100644 --- a/db/dbformat.cc +++ b/db/dbformat.cc @@ -75,11 +75,13 @@ int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const { r = -1; return r; } - + const uint64_t atime = DecodeFixed64(akey.data() + akey.size() - 16); + const uint64_t btime = DecodeFixed64(bkey.data() + bkey.size() - 16); //原本应该找到了,新加判断 if((btag & 0b100) && (atag & 0b10)){ //一个是查询键,另一个有ttl const uint64_t atime = DecodeFixed64(akey.data() + akey.size() - 16); const uint64_t btime = DecodeFixed64(bkey.data() + bkey.size() - 16); + std::cout<<"atime:"<& files = current_->files_[level]; for (size_t i = 0; i < files.size(); i++) { const FileMetaData* f = files[i]; - edit.AddFile(level, f->number, f->file_size, f->smallest, f->largest); + edit.AddFile(level, f->number, f->file_size, f->smallest, f->largest, + f->smallest_deadtime,f->largest_deadtime); } }