Ver a proveniência

删除了些没用的修改

main
augurier há 3 semanas
ascendente
cometimento
db274d24d1
5 ficheiros alterados com 11 adições e 141 eliminações
  1. +7
    -8
      db/dbformat.cc
  2. +2
    -6
      db/dbformat.h
  3. +0
    -12
      db/memtable.cc
  4. +2
    -2
      test/db_test2.cc
  5. +0
    -113
      test/ttl_mmtable_test.cc

+ 7
- 8
db/dbformat.cc Ver ficheiro

@ -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

+ 2
- 6
db/dbformat.h Ver ficheiro

@ -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_;

+ 0
- 12
db/memtable.cc Ver ficheiro

@ -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<ValueType>(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<uint64_t>(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;

+ 2
- 2
test/db_test2.cc Ver ficheiro

@ -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<unsigned int>(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<unsigned int>(time(0)));
srand(0);
for (int i = 0; i < 100; i++) {
int key_ = rand() % key_num+1;
std::string key = std::to_string(key_);

+ 0
- 113
test/ttl_mmtable_test.cc Ver ficheiro

@ -1,113 +0,0 @@
#include "leveldb/env.h"
#include "leveldb/db.h"
#include "ctime"
#include <iostream>
#include <cstdlib>
#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<unsigned int>(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<unsigned int>(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();
}

Carregando…
Cancelar
Guardar