diff --git a/db/dbformat.cc b/db/dbformat.cc index 2ef75e2..d2f7b18 100644 --- a/db/dbformat.cc +++ b/db/dbformat.cc @@ -12,14 +12,13 @@ namespace leveldb { -static uint64_t PackSequenceAndTypeAndTtlAndLookup( - uint64_t seq, ValueType t, bool havettl, bool islookup) { +static uint64_t PackSequenceAndTypeAndTtl( + uint64_t seq, ValueType t, bool havettl) { assert(seq <= kMaxSequenceNumber); assert(t <= kValueTypeForSeek); - return (seq << 8) | (islookup << 2) | (havettl << 1) | t; + return (seq << 8) | (havettl << 1) | t; } -//下面有两个调这个函数的没改,也许也要修改标志位? static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) { assert(seq <= kMaxSequenceNumber); assert(t <= kValueTypeForSeek); @@ -30,8 +29,8 @@ void AppendInternalKey(std::string* result, const ParsedInternalKey& key) { result->append(key.user_key.data(), key.user_key.size()); if(key.deadTime != 0) PutFixed64(result, key.deadTime); - PutFixed64(result, PackSequenceAndTypeAndTtlAndLookup( - key.sequence, key.type, (key.deadTime != 0), false)); + PutFixed64(result, PackSequenceAndTypeAndTtl( + key.sequence, key.type, (key.deadTime != 0))); } std::string ParsedInternalKey::DebugString() const { @@ -164,10 +163,10 @@ LookupKey::LookupKey(const Slice& user_key, SequenceNumber s, uint64_t nowTime) EncodeFixed64(dst, nowTime); dst += 8; // EncodeFixed64(dst, PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 0, true)); - EncodeFixed64(dst, PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 1, false)); + EncodeFixed64(dst, PackSequenceAndTypeAndTtl(s, kValueTypeForSeek, 1)); dst += 8; end_ = dst; - printf("lookupkey tag:%lx\n",PackSequenceAndTypeAndTtlAndLookup(s, kValueTypeForSeek, 1, false)); + printf("lookupkey tag:%lx\n",PackSequenceAndTypeAndTtl(s, kValueTypeForSeek, 1)); } } // namespace leveldb diff --git a/db/dbformat.h b/db/dbformat.h index c435c14..53e0751 100644 --- a/db/dbformat.h +++ b/db/dbformat.h @@ -102,9 +102,8 @@ inline Slice ExtractUserKey(const Slice& internal_key) { assert(internal_key.size() >= 8); uint64_t num = DecodeFixed64(internal_key.data() + internal_key.size() - 8); uint8_t havettl = (num & 0b10) >> 1; - uint8_t islookup = (num & 0b100) >> 2; size_t klen = internal_key.size() - 8; - if(havettl || islookup) klen -= 8; + if(havettl) klen -= 8; Slice user_key = Slice(internal_key.data(), klen); return user_key; } @@ -183,7 +182,6 @@ inline int InternalKeyComparator::Compare(const InternalKey& a, inline bool ParseInternalKey(const Slice& internal_key, ParsedInternalKey* result) { - //不确定需不需要标识islookup,先没改 const size_t n = internal_key.size(); if (n < 8) return false; uint64_t tag = DecodeFixed64(internal_key.data() + n - 8); @@ -228,10 +226,8 @@ class LookupKey { // klength varint32 <-- start_ // userkey char[klength] <-- kstart_ // nowTime uint64 - // tag uint64 最后一个字节为0000 0101 + // tag uint64 最后一个字节为0000 0001 // <-- end_ - // 同userkey下,原本(insert时)的比较器规则为seq优先,不考虑时间 - // 新增标识位(tag倒数第三位),使比较器考虑时间 // The array is a suitable MemTable key. // The suffix starting with "userkey" can be used as an InternalKey. const char* start_; diff --git a/db/memtable.cc b/db/memtable.cc index 2ddc12e..c80a05b 100644 --- a/db/memtable.cc +++ b/db/memtable.cc @@ -187,18 +187,6 @@ bool MemTable::Get(const LookupKey& key, std::string* value, Status* s) { const uint64_t tag = DecodeFixed64(key_ptr + key_length - 8); switch (static_cast(tag & 0x01)) { case kTypeValue: { - // uint8_t havettl = (tag & 0xff) >> 1; - // if(havettl){ - // time_t nowTime; - // time(&nowTime); - // assert(nowTime > 0); - // const uint64_t deadTime = DecodeFixed64(key_ptr + key_length - 16); - // if(static_cast(nowTime) >= deadTime){ //过期了 - // std::cout << nowTime << "dead:" << deadTime << std::endl; - // *s = Status::NotFound(Slice()); - // return true; //todo:之前有没过期的key - // } - // } Slice v = GetLengthPrefixedSlice(key_ptr + key_length); value->assign(v.data(), v.size()); return true; diff --git a/test/db_test2.cc b/test/db_test2.cc index 4b2b33c..37d9ba4 100644 --- a/test/db_test2.cc +++ b/test/db_test2.cc @@ -23,7 +23,7 @@ Status OpenDB(std::string dbName, DB **db) { void InsertData(DB *db) { WriteOptions writeOptions; int key_num = data_size / value_size; - srand(static_cast(time(0))); + srand(0); for (int i = 0; i < key_num; i++) { int key_ = rand() % key_num+1; @@ -39,7 +39,7 @@ void GetData(DB *db, int size = (1 << 30)) { int key_num = data_size / value_size; // 点查 - srand(static_cast(time(0))); + srand(0); for (int i = 0; i < 100; i++) { int key_ = rand() % key_num+1; std::string key = std::to_string(key_); diff --git a/test/ttl_mmtable_test.cc b/test/ttl_mmtable_test.cc deleted file mode 100644 index 5b44033..0000000 --- a/test/ttl_mmtable_test.cc +++ /dev/null @@ -1,113 +0,0 @@ -#include "leveldb/env.h" -#include "leveldb/db.h" -#include "ctime" -#include -#include -#include "gtest/gtest.h" - -using namespace leveldb; - -constexpr int value_size = 2048; -constexpr int data_size = 2048 << 15; - -Status OpenDB(std::string dbName, DB **db) { - Options options; - options.create_if_missing = true; - return DB::Open(options, dbName, db); -} - -void InsertData(DB *db, uint64_t ttl/* second */, int vsize = 1/*插不同长度的value*/) { - printf("-----inserting-----\n"); - Status status; - WriteOptions writeOptions; - int key_num = data_size / value_size; - srand(static_cast(time(0))); - - for (int i = 0; i < key_num; i++) { - //int key_ = rand() % key_num+1; - int key_ = i+1; - std::string key = std::to_string(key_); - std::string value(vsize, 'a'); - status = db->Put(writeOptions, key, value, ttl); - assert(status.ok()); - } -} - -void GetData(DB *db, bool isTimeout) { - printf("-----seeking-----\n"); - ReadOptions readOptions; - Status status; - int key_num = data_size / value_size; - srand(static_cast(time(0))); - for (int i = 0; i < key_num; i++) { - //int key_ = rand() % key_num+1; - int key_ = i+1; - std::string key = std::to_string(key_); - std::string value; - status = db->Get(readOptions, key, &value); - if(isTimeout) assert(status.IsNotFound()); - else{ - assert(status.ok()); - std::cout << value << std::endl; - } - } -} - -void TimeOut() { -DB *db; - printf("-----opening-----\n"); - if(OpenDB("testdb", &db).ok() == false) { - std::cerr << "open db failed" << std::endl; - abort(); - } - - uint64_t ttl = 3; - - InsertData(db, ttl); - GetData(db, false); - - Env::Default()->SleepForMicroseconds(ttl * 1000000); - GetData(db, true); - - delete(db); - printf("-----closing-----\n"); - - // printf("-----recovery-----\n"); - // if(OpenDB("testdb", &db).ok() == false) { - // std::cerr << "open db failed" << std::endl; - // abort(); - // } - // GetData(db, true); - printf("success!\n"); -} - -void GetEarlierData() { -DB *db; - printf("-----opening-----\n"); - if(OpenDB("testdb", &db).ok() == false) { - std::cerr << "open db failed" << std::endl; - abort(); - } - - uint64_t ttl1 = 3; - uint64_t ttl2 = 5; - - // InsertData(db, ttl2); - InsertData(db, ttl1, 2); - - //都没过期先找到后插的 - Env::Default()->SleepForMicroseconds(1 * 1000000); - GetData(db, false); - - //再找到前一次 - Env::Default()->SleepForMicroseconds(3 * 1000000); - GetData(db, true); - DestroyDB("testdb",Options()); - delete(db); - printf("-----closing-----\n"); - printf("success!\n"); -} - -int main(int argc, char** argv) { - GetEarlierData(); -}