-
Notifications
You must be signed in to change notification settings - Fork 34
/
extrinsic_event_key_repository.hpp
57 lines (45 loc) · 1.44 KB
/
extrinsic_event_key_repository.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <mutex>
#include "primitives/event_types.hpp"
#include "primitives/transaction.hpp"
#include "log/logger.hpp"
namespace kagome::subscription {
class ExtrinsicEventKeyRepository {
public:
using ExtrinsicKey = primitives::events::SubscribedExtrinsicId;
ExtrinsicEventKeyRepository()
: logger_{log::createLogger("ExtrinsicEventKeyRepo", "transactions")} {}
ExtrinsicKey add(const primitives::Transaction::Hash &hash) {
std::unique_lock lock{mutex_};
if (auto it = keys_.find(hash); it != keys_.end()) {
return it->second;
}
auto key = last_key_++;
keys_[hash] = key;
SL_DEBUG(logger_, "Registered tx {}, key is {}", hash, key);
return key;
}
bool remove(const primitives::Transaction::Hash &hash) {
std::unique_lock lock{mutex_};
return keys_.erase(hash) > 0;
}
std::optional<ExtrinsicKey> get(
const primitives::Transaction::Hash &hash) const {
std::unique_lock lock{mutex_};
if (auto it = keys_.find(hash); it != keys_.end()) {
return it->second;
}
return std::nullopt;
}
private:
mutable std::mutex mutex_;
std::atomic<ExtrinsicKey> last_key_{};
std::unordered_map<primitives::Transaction::Hash, ExtrinsicKey> keys_;
log::Logger logger_;
};
} // namespace kagome::subscription