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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
#pragma once
#include <yql/essentials/utils/yql_panic.h>
#include <util/generic/hash.h>
#include <util/generic/maybe.h>
#include <util/generic/vector.h>
#include <stdexcept>
#include <algorithm>
namespace NYql {
// TCheckpointHashMap - hash table with checkpoint/rollback support
//
// This class behaves like a regular THashMap but provides the ability
// to create checkpoints of the current state and rollback to them.
//
// Key methods:
// - Checkpoint() - creates a savepoint of the current state
// - Rollback() - reverts all changes back to the last checkpoint
// - Set(key, value) - sets value for the given key
// - Get(key) - gets value by key (throws exception if key doesn't exist)
// - erase(key) - removes element by key
// - find(key) - finds element, returns const_iterator
//
// Usage example:
// TCheckpointHashMap<int, string> map;
// map.Set(1, "hello");
// map.Checkpoint();
// map.Set(2, "world");
// map.Set(1, "modified");
// map.Rollback(); // map now contains only {1: "hello"}
//
// Alternative with TCheckpointGuard (RAII):
// {
// TCheckpointGuard guard(map);
// map.Set(2, "temp");
// } // automatic rollback when leaving scope
//
// WARNING: This class is NOT exception-safe!
// - Internal state corruption is possible during exceptions
template <typename TKey, typename TValue>
class TCheckpointHashMap {
public:
using key_type = typename THashMap<TKey, TValue>::key_type;
using mapped_type = typename THashMap<TKey, TValue>::mapped_type;
using value_type = typename THashMap<TKey, TValue>::value_type;
using size_type = typename THashMap<TKey, TValue>::size_type;
using const_iterator = typename THashMap<TKey, TValue>::const_iterator;
using TUnderlyingMap = THashMap<TKey, TValue>;
TCheckpointHashMap()
: CurrentCheckpointId_(0)
{
}
TCheckpointHashMap(const TCheckpointHashMap& other) = delete;
TCheckpointHashMap(TCheckpointHashMap&& other) = delete;
TCheckpointHashMap& operator=(const TCheckpointHashMap& other) = delete;
TCheckpointHashMap& operator=(TCheckpointHashMap&& other) = delete;
void Checkpoint() {
++CurrentCheckpointId_;
}
void Rollback() {
YQL_ENSURE(CurrentCheckpointId_ != 0, "Cannot rollback without checkpoint");
auto it = Commands_.rbegin();
while (it != Commands_.rend() && it->first == CurrentCheckpointId_) {
const auto& cmd = it->second;
if (cmd.OldValue.Defined()) {
Map_[cmd.Key] = *cmd.OldValue;
} else {
Map_.erase(cmd.Key);
}
++it;
}
auto eraseFrom = it.base();
Commands_.erase(eraseFrom, Commands_.end());
--CurrentCheckpointId_;
}
void Set(const TKey& key, const TValue& value) {
typename TUnderlyingMap::insert_ctx ctx = nullptr;
auto it = Map_.find(key, ctx);
if (CurrentCheckpointId_ != 0) {
if (it == Map_.end()) {
Commands_.emplace_back(CurrentCheckpointId_, TUpdateCommand(key, Nothing()));
} else {
Commands_.emplace_back(CurrentCheckpointId_, TUpdateCommand(key, it->second));
}
}
if (it == Map_.end()) {
Map_.emplace_direct(ctx, key, value);
} else {
it->second = value;
}
}
const TValue& Get(const TKey& key) const {
return Map_.at(key);
}
size_type erase(const TKey& key) {
auto it = Map_.find(key);
if (it != Map_.end()) {
if (CurrentCheckpointId_ != 0) {
Commands_.emplace_back(CurrentCheckpointId_, TUpdateCommand(key, it->second));
}
Map_.erase(it);
return 1;
}
return 0;
}
const_iterator find(const TKey& key) const {
return Map_.find(key);
}
size_type count(const TKey& key) const {
return Map_.count(key);
}
bool contains(const TKey& key) const {
return Map_.find(key) != Map_.end();
}
const_iterator begin() const {
return Map_.begin();
}
const_iterator cbegin() const {
return Map_.cbegin();
}
const_iterator end() const {
return Map_.end();
}
const_iterator cend() const {
return Map_.cend();
}
bool empty() const {
return Map_.empty();
}
size_type size() const {
return Map_.size();
}
size_type max_size() const {
return Map_.max_size();
}
bool operator==(const TCheckpointHashMap& other) const {
return Map_ == other.Map_;
}
bool operator!=(const TCheckpointHashMap& other) const {
return !(*this == other);
}
const THashMap<TKey, TValue>& GetUnderlyingMap() const {
return Map_;
}
size_t GetCommandCount() const {
return Commands_.size();
}
private:
struct TUpdateCommand {
TKey Key;
TMaybe<TValue> OldValue;
TUpdateCommand(TKey key, TMaybe<TValue> oldVal)
: Key(std::move(key))
, OldValue(std::move(oldVal))
{
}
};
using TCommand = TUpdateCommand;
TUnderlyingMap Map_;
TVector<std::pair<size_t, TCommand>> Commands_;
size_t CurrentCheckpointId_;
};
template <typename TKey, typename TValue>
class TCheckpointGuard {
public:
explicit TCheckpointGuard(TCheckpointHashMap<TKey, TValue>& map)
: Map_(map)
{
Map_.Checkpoint();
}
~TCheckpointGuard() {
Map_.Rollback();
}
private:
TCheckpointHashMap<TKey, TValue>& Map_;
};
} // namespace NYql
|