瀏覽代碼

optimize the structure of Put

master
王雪飞 3 週之前
父節點
當前提交
47f23b7d6b
共有 1 個檔案被更改,包括 14 行新增9 行删除
  1. +14
    -9
      db/db_impl.cc

+ 14
- 9
db/db_impl.cc 查看文件

@ -1218,15 +1218,7 @@ Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) {
Status DBImpl::Put(const WriteOptions& opt, const Slice& key,
const Slice& value, uint64_t ttl) {
WriteBatch batch;
auto now = std::chrono::system_clock::now();
auto timestamp = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
auto microsecondsTimestamp = static_cast<uint64_t>(timestamp) + ttl*1000000;
std::string value_ttl = value.ToString();
value_ttl += "_" + std::to_string(microsecondsTimestamp);
Slice new_value(value_ttl.c_str(), value_ttl.size());
batch.Put(key, new_value);
return Write(opt, &batch);
return DB::Put(opt, key, value, ttl);
}
Status DBImpl::Delete(const WriteOptions& options, const Slice& key) {
@ -1521,6 +1513,19 @@ Status DB::Put(const WriteOptions& opt, const Slice& key, const Slice& value) {
batch.Put(key, value);
return Write(opt, &batch);
}
// for TTL
Status DB::Put(const WriteOptions& opt, const Slice& key,
const Slice& value, uint64_t ttl) {
WriteBatch batch;
auto now = std::chrono::system_clock::now();
auto timestamp = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
auto microsecondsTimestamp = static_cast<uint64_t>(timestamp) + ttl*1000000;
std::string value_ttl = value.ToString();
value_ttl += "_" + std::to_string(microsecondsTimestamp);
Slice new_value(value_ttl.c_str(), value_ttl.size());
batch.Put(key, new_value);
return Write(opt, &batch);
}
Status DB::Delete(const WriteOptions& opt, const Slice& key) {
WriteBatch batch;

Loading…
取消
儲存