|
@ -1161,6 +1161,24 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key, |
|
|
mem->Unref(); |
|
|
mem->Unref(); |
|
|
if (imm != nullptr) imm->Unref(); |
|
|
if (imm != nullptr) imm->Unref(); |
|
|
current->Unref(); |
|
|
current->Unref(); |
|
|
|
|
|
/** parse ttl from value **/ |
|
|
|
|
|
size_t pos = value->find_last_of('_'); |
|
|
|
|
|
if (pos != std::string::npos) { |
|
|
|
|
|
std::string substring = value->substr(pos + 1); |
|
|
|
|
|
auto ttl = static_cast<uint64_t>(std::stoll(substring)); |
|
|
|
|
|
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); |
|
|
|
|
|
if (ttl <= microsecondsTimestamp) { |
|
|
|
|
|
value->clear(); |
|
|
|
|
|
Slice msg1("value not found!"); |
|
|
|
|
|
Slice msg2("value has expired!"); |
|
|
|
|
|
s = leveldb::Status::NotFound(msg1, msg2); |
|
|
|
|
|
} else { |
|
|
|
|
|
value->resize(pos); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
/** parse ttl from value **/ |
|
|
return s; |
|
|
return s; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -1198,6 +1216,19 @@ Status DBImpl::Put(const WriteOptions& o, const Slice& key, const Slice& val) { |
|
|
return DB::Put(o, key, val); |
|
|
return DB::Put(o, key, 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); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
Status DBImpl::Delete(const WriteOptions& options, const Slice& key) { |
|
|
Status DBImpl::Delete(const WriteOptions& options, const Slice& key) { |
|
|
return DB::Delete(options, key); |
|
|
return DB::Delete(options, key); |
|
|
} |
|
|
} |
|
|