소스 검색

对filemetadata增加deadtime范围,并在相关地方进行输入的

pull/1/head
cyq 1 개월 전
부모
커밋
b2574f0b94
8개의 변경된 파일64개의 추가작업 그리고 22개의 파일을 삭제
  1. +9
    -0
      db/builder.cc
  2. +29
    -15
      db/db_impl.cc
  3. +4
    -1
      db/dbformat.cc
  4. +1
    -1
      db/memtable.cc
  5. +1
    -1
      db/repair.cc
  6. +9
    -1
      db/version_edit.cc
  7. +9
    -2
      db/version_edit.h
  8. +2
    -1
      db/version_set.cc

+ 9
- 0
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);

+ 29
- 15
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;

+ 4
- 1
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:"<<atime<<" btime:"<<btime<<" "<<aseq<<" "<<bseq<<" "<<btag<<" "<<atag<<std::endl;
if(atime <= btime){//过期了继续找
r = -1;
return r;
@ -164,6 +166,7 @@ LookupKey::LookupKey(const Slice& user_key, SequenceNumber s, uint64_t nowTime)
EncodeFixed64(dst, PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 0, true));
dst += 8;
end_ = dst;
printf("lookupkey tag:%lx\n",PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 0, true));
}
} // namespace leveldb

+ 1
- 1
db/memtable.cc 파일 보기

@ -115,7 +115,7 @@ void MemTable::Add(SequenceNumber s, ValueType type, const Slice& key,
std::memcpy(p, value.data(), val_size);
assert(p + val_size == buf + encoded_len);
table_.Insert(buf);
std::cout << "insert:" << key.ToString() << std::endl;
std::cout << "insert:" << key.ToString() <<" deadTime: " << deadTime << std::endl;
}
bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) {

+ 1
- 1
db/repair.cc 파일 보기

@ -90,7 +90,7 @@ class Repairer {
}
private:
struct TableInfo {
struct TableInfo { //TODO : add the recovery of smallest and largest deadtime
FileMetaData meta;
SequenceNumber max_sequence;
};

+ 9
- 1
db/version_edit.cc 파일 보기

@ -81,6 +81,8 @@ void VersionEdit::EncodeTo(std::string* dst) const {
PutVarint64(dst, f.file_size);
PutLengthPrefixedSlice(dst, f.smallest.Encode());
PutLengthPrefixedSlice(dst, f.largest.Encode());
PutVarint64(dst,f.smallest_deadtime);
PutVarint64(dst,f.largest_deadtime);
}
}
@ -179,7 +181,9 @@ Status VersionEdit::DecodeFrom(const Slice& src) {
if (GetLevel(&input, &level) && GetVarint64(&input, &f.number) &&
GetVarint64(&input, &f.file_size) &&
GetInternalKey(&input, &f.smallest) &&
GetInternalKey(&input, &f.largest)) {
GetInternalKey(&input, &f.largest) &&
GetVarint64(&input,&f.smallest_deadtime) &&
GetVarint64(&input,&f.largest_deadtime)) {
new_files_.push_back(std::make_pair(level, f));
} else {
msg = "new-file entry";
@ -250,6 +254,10 @@ std::string VersionEdit::DebugString() const {
r.append(f.smallest.DebugString());
r.append(" .. ");
r.append(f.largest.DebugString());
r.append(" ");
AppendNumberTo(&r,f.smallest_deadtime);
r.append(" .. ");
AppendNumberTo(&r,f.largest_deadtime);
}
r.append("\n}\n");
return r;

+ 9
- 2
db/version_edit.h 파일 보기

@ -16,7 +16,8 @@ namespace leveldb {
class VersionSet;
struct FileMetaData {
FileMetaData() : refs(0), allowed_seeks(1 << 30), file_size(0) {}
FileMetaData() : refs(0), allowed_seeks(1 << 30), file_size(0),
smallest_deadtime(UINT64_MAX),largest_deadtime(UINT64_MAX) {}
int refs;
int allowed_seeks; // Seeks allowed until compaction
@ -24,6 +25,9 @@ struct FileMetaData {
uint64_t file_size; // File size in bytes
InternalKey smallest; // Smallest internal key served by table
InternalKey largest; // Largest internal key served by table
//FileMetaData中使Uint64MAX作为没有生存期的标志
uint64_t smallest_deadtime; //smallest deadtime
uint64_t largest_deadtime; //largest deadtime
};
class VersionEdit {
@ -61,12 +65,15 @@ class VersionEdit {
// REQUIRES: This version has not been saved (see VersionSet::SaveTo)
// REQUIRES: "smallest" and "largest" are smallest and largest keys in file
void AddFile(int level, uint64_t file, uint64_t file_size,
const InternalKey& smallest, const InternalKey& largest) {
const InternalKey& smallest, const InternalKey& largest,
uint64_t smallest_deadtime = UINT64_MAX, uint64_t largest_deadtime = UINT64_MAX) {
FileMetaData f;
f.number = file;
f.file_size = file_size;
f.smallest = smallest;
f.largest = largest;
f.smallest_deadtime = smallest_deadtime;
f.largest_deadtime = largest_deadtime;
new_files_.push_back(std::make_pair(level, f));
}

+ 2
- 1
db/version_set.cc 파일 보기

@ -1087,7 +1087,8 @@ Status VersionSet::WriteSnapshot(log::Writer* log) {
const std::vector<FileMetaData*>& 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);
}
}

불러오는 중...
취소
저장