Browse Source

optimize the structure of Put

master
王雪飞 3 weeks ago
parent
commit
47f23b7d6b
1 changed files with 14 additions and 9 deletions
  1. +14
    -9
      db/db_impl.cc

+ 14
- 9
db/db_impl.cc View File

@ -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, Status DBImpl::Put(const WriteOptions& opt, const Slice& key,
const Slice& value, uint64_t ttl) { 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) { 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); batch.Put(key, value);
return Write(opt, &batch); 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) { Status DB::Delete(const WriteOptions& opt, const Slice& key) {
WriteBatch batch; WriteBatch batch;

Loading…
Cancel
Save