Преглед изворни кода

fix some bugs about ts

main
GUJIEJASON пре 2 недеља
родитељ
комит
59a258c3fd
3 измењених фајлова са 77 додато и 33 уклоњено
  1. +2
    -1
      CMakeLists.txt
  2. +63
    -21
      db/db_impl.cc
  3. +12
    -11
      test/ttl_test.cc

+ 2
- 1
CMakeLists.txt Прегледај датотеку

@ -112,6 +112,7 @@ include_directories(
if(BUILD_SHARED_LIBS)
# Only export LEVELDB_EXPORT symbols from the shared library.
add_compile_options(-fvisibility=hidden)
endif(BUILD_SHARED_LIBS)
# Must be included before CMAKE_INSTALL_INCLUDEDIR is used.
@ -544,4 +545,4 @@ target_link_libraries(test_a leveldb)
add_executable(time
"${PROJECT_SOURCE_DIR}/test/time.cpp"
)
target_link_libraries(time leveldb)
target_link_libraries(time leveldb)

+ 63
- 21
db/db_impl.cc Прегледај датотеку

@ -11,6 +11,7 @@
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <cctype>
#include <set>
#include <string>
#include <vector>
@ -594,7 +595,8 @@ void DBImpl::CompactRange(const Slice* begin, const Slice* end) {
}
}
TEST_CompactMemTable(); // TODO(sanjay): Skip if memtable does not overlap
for (int level = 0; level < max_level_with_files; level++) {
for (int level = 0; level <= max_level_with_files; level++) {
// for (int level = 0; level < max_level_with_files; level++) {
TEST_CompactRange(level, begin, end);
}
}
@ -897,6 +899,15 @@ Status DBImpl::InstallCompactionResults(CompactionState* compact) {
return versions_->LogAndApply(compact->compaction->edit(), &mutex_);
}
bool isAllDigits(const std::string& str) {
for (char c : str) {
if (!isdigit(c)) {
return false;
}
}
return true;
}
Status DBImpl::DoCompactionWork(CompactionState* compact) {
const uint64_t start_micros = env_->NowMicros();
int64_t imm_micros = 0; // Micros spent doing imm_ compactions
@ -985,13 +996,23 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) {
else {
std::string user_value = input->value().ToString();
uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
if (user_value.find("_ts_") == std::string::npos) {
input->value() = user_value + "_ts_" + std::to_string(now + ttl);
}
else {
uint64_t deadtime = std::stoi(user_value.substr(user_value.find("_ts_") + 4));
if (now >= deadtime) {
drop = true;
// if (user_value.find("_ts_") == std::string::npos) {
// input->value() = user_value + "_ts_" + std::to_string(now + ttl);
// }
// else {
// uint64_t deadtime = std::stoi(user_value.substr(user_value.find("_ts_") + 4));
// if (now >= deadtime) {
// drop = true;
// }
// }
size_t pos = user_value.rfind("_ts_");
if (pos != std::string::npos){
std::string timestampStr = user_value.substr(pos + 4);
if (isAllDigits(timestampStr)) {
uint64_t deadtime = std::stoull(timestampStr);
if (now >= deadtime) {
drop = true;
}
}
}
}
@ -1135,24 +1156,42 @@ int64_t DBImpl::TEST_MaxNextLevelOverlappingBytes() {
}
/* TODO: Add TTL Version isLive() */
Status isLive(const Slice& key, std::string* value, Status& s, uint64_t ttl) {
// Status isLive(const Slice& key, std::string* value, Status& s, uint64_t ttl) {
// if (value->empty()) {
// s = Status::NotFound(key);
// return s;
// }
// uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
// if (value->find("_ts_") == std::string::npos) {
// *value = *value + "_ts_" + std::to_string(now + ttl);
// }
// else {
// uint64_t deadtime = std::stoi(value->substr(value->find("_ts_") + 4));
// if (now >= deadtime) {
// s = Status::NotFound(key);
// }
// }
// return s;
// }
/* --------------------------- */
Status isLive(const Slice& key, std::string* value, Status& s) {
if (value->empty()) {
s = Status::NotFound(key);
return s;
}
uint64_t now = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
if (value->find("_ts_") == std::string::npos) {
*value = *value + "_ts_" + std::to_string(now + ttl);
}
else {
uint64_t deadtime = std::stoi(value->substr(value->find("_ts_") + 4));
if (now >= deadtime) {
s = Status::NotFound(key);
}
size_t pos = value->rfind("_ts_");
if (pos != std::string::npos){
std::string timestampStr = value->substr(pos + 4);
if (isAllDigits(timestampStr)) {
uint64_t deadtime = std::stoull(timestampStr);
if (now >= deadtime) {
s = Status::NotFound(key);
}
}
}
return s;
}
/* --------------------------- */
Status DBImpl::Get(const ReadOptions& options, const Slice& key,
std::string* value) {
@ -1184,14 +1223,17 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key,
/* TODO: Add TTL Version Get() */
if (mem->Get(lkey, value, &s)) {
isLive(key, value, s, ttl);
// isLive(key, value, s, ttl);
isLive(key, value, s);
// Done
} else if (imm != nullptr && imm->Get(lkey, value, &s)) {
isLive(key, value, s, ttl);
// isLive(key, value, s, ttl);
isLive(key, value, s);
// Done
} else {
s = current->Get(options, lkey, value, &stats);
if (s.ok()) isLive(key, value, s, ttl);
// if (s.ok()) isLive(key, value, s, ttl);
if (s.ok()) isLive(key, value, s);
have_stat_update = true;
}
/* --------------------------- */

+ 12
- 11
test/ttl_test.cc Прегледај датотеку

@ -1,8 +1,5 @@
<<<<<<< HEAD
=======
#include <chrono>
>>>>>>> virgil
#include "gtest/gtest.h"
#include "leveldb/env.h"
#include "leveldb/db.h"
@ -13,6 +10,9 @@ constexpr int value_size = 2048;
constexpr int data_size = 128 << 20;
Status OpenDB(std::string dbName, DB **db) {
std::string rm_command = "rm -rf " + dbName;
system(rm_command.c_str());
Options options;
options.create_if_missing = true;
return DB::Open(options, dbName, db);
@ -21,11 +21,7 @@ Status OpenDB(std::string dbName, DB **db) {
void InsertData(DB *db, uint64_t ttl/* second */) {
WriteOptions writeOptions;
int key_num = data_size / value_size;
<<<<<<< HEAD
srand(0);
=======
srand(42);
>>>>>>> virgil
for (int i = 0; i < key_num; i++) {
int key_ = rand() % key_num+1;
@ -34,6 +30,15 @@ void InsertData(DB *db, uint64_t ttl/* second */) {
db->ttl = ttl;
db->Put(writeOptions, key, value, ttl);
}
// for (int i = 0; i < key_num; i++) {
// int key_ = rand() % key_num+1;
// std::string key = std::to_string(key_ );
// std::string value = "aaaaaa_ts_5678aaaaaa";
// db->Put(writeOptions, key, value);
// }
}
void GetData(DB *db, int size = (1 << 30)) {
@ -41,11 +46,7 @@ void GetData(DB *db, int size = (1 << 30)) {
int key_num = data_size / value_size;
// 点查
<<<<<<< HEAD
srand(0);
=======
srand(42);
>>>>>>> virgil
for (int i = 0; i < 100; i++) {
int key_ = rand() % key_num+1;
std::string key = std::to_string(key_);

Loading…
Откажи
Сачувај