2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/db/SeqKeyValue.h"
|
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
#include "td/utils/HashTableUtils.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/port/RwMutex.h"
|
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
|
2022-02-09 22:59:52 +01:00
|
|
|
#include <unordered_map>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace td {
|
2018-07-17 22:08:38 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class TsSeqKeyValue {
|
|
|
|
public:
|
|
|
|
using SeqNo = SeqKeyValue::SeqNo;
|
|
|
|
TsSeqKeyValue() = default;
|
|
|
|
explicit TsSeqKeyValue(SeqKeyValue kv) : kv_(std::move(kv)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
TsSeqKeyValue(TsSeqKeyValue &&) = default;
|
|
|
|
TsSeqKeyValue &operator=(TsSeqKeyValue &&) = default;
|
|
|
|
TsSeqKeyValue(const TsSeqKeyValue &) = delete;
|
|
|
|
TsSeqKeyValue &operator=(const TsSeqKeyValue &) = delete;
|
|
|
|
~TsSeqKeyValue() = default;
|
|
|
|
|
|
|
|
SeqNo set(Slice key, Slice value) {
|
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
|
|
|
return kv_.set(key, value);
|
|
|
|
}
|
2022-08-17 15:52:27 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
std::pair<SeqNo, RwMutex::WriteLock> set_and_lock(Slice key, Slice value) {
|
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
|
|
|
return std::make_pair(kv_.set(key, value), std::move(lock));
|
|
|
|
}
|
2022-08-17 15:52:27 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
SeqNo erase(const string &key) {
|
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
|
|
|
return kv_.erase(key);
|
|
|
|
}
|
2022-08-17 15:52:27 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
std::pair<SeqNo, RwMutex::WriteLock> erase_and_lock(const string &key) {
|
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
|
|
|
return std::make_pair(kv_.erase(key), std::move(lock));
|
|
|
|
}
|
2022-08-17 15:52:27 +02:00
|
|
|
|
2022-08-18 12:59:34 +02:00
|
|
|
string get(const string &key) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_read().move_as_ok();
|
|
|
|
return kv_.get(key);
|
|
|
|
}
|
2022-08-17 15:52:27 +02:00
|
|
|
|
2022-08-18 12:59:34 +02:00
|
|
|
bool isset(const string &key) const {
|
2022-08-17 15:52:27 +02:00
|
|
|
auto lock = rw_mutex_.lock_read().move_as_ok();
|
|
|
|
return kv_.isset(key);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
size_t size() const {
|
|
|
|
return kv_.size();
|
|
|
|
}
|
2022-08-17 15:52:27 +02:00
|
|
|
|
2022-11-23 17:37:32 +01:00
|
|
|
std::unordered_map<string, string, Hash<string>> get_all() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto lock = rw_mutex_.lock_write().move_as_ok();
|
|
|
|
return kv_.get_all();
|
|
|
|
}
|
2022-08-17 15:52:27 +02:00
|
|
|
|
2022-10-12 14:59:58 +02:00
|
|
|
// non-thread-safe method
|
2018-12-31 20:04:05 +01:00
|
|
|
SeqKeyValue &inner() {
|
|
|
|
return kv_;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lock() {
|
|
|
|
return rw_mutex_.lock_write().move_as_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2022-08-18 12:59:34 +02:00
|
|
|
mutable RwMutex rw_mutex_;
|
2018-12-31 20:04:05 +01:00
|
|
|
SeqKeyValue kv_;
|
|
|
|
};
|
2018-07-17 22:08:38 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|