CCF
Loading...
Searching...
No Matches
map_handle.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6
7namespace ccf::kv
8{
11 template <typename K, typename V, typename KSerialiser, typename VSerialiser>
13 {
14 protected:
16
17 public:
18 using KeyType = K;
19 using ValueType = V;
20
22
27 std::string get_name_of_map() const
28 {
30 }
31
45 std::optional<V> get(const K& key)
46 {
47 const auto opt_v_rep = read_handle.get(KSerialiser::to_serialised(key));
48
49 if (opt_v_rep.has_value())
50 {
51 return VSerialiser::from_serialised(*opt_v_rep);
52 }
53
54 return std::nullopt;
55 }
56
71 std::optional<V> get_globally_committed(const K& key)
72 {
73 const auto opt_v_rep =
74 read_handle.get_globally_committed(KSerialiser::to_serialised(key));
75
76 if (opt_v_rep.has_value())
77 {
78 return VSerialiser::from_serialised(*opt_v_rep);
79 }
80
81 return std::nullopt;
82 }
83
93 bool has(const K& key)
94 {
95 return read_handle.has(KSerialiser::to_serialised(key));
96 }
97
112 std::optional<Version> get_version_of_previous_write(const K& key)
113 {
115 KSerialiser::to_serialised(key));
116 }
117
145 template <class F>
146 void foreach(F&& f)
147 {
148 auto g = [&](
151 return f(
152 KSerialiser::from_serialised(k_rep),
153 VSerialiser::from_serialised(v_rep));
154 };
156 }
157
168 template <class F>
169 void foreach_key(F&& f)
170 {
171 auto g = [&](
174 return f(KSerialiser::from_serialised(k_rep));
175 };
177 }
178
189 template <class F>
190 void foreach_value(F&& f)
191 {
192 auto g = [&](
195 return f(VSerialiser::from_serialised(v_rep));
196 };
198 }
199
208 size_t size()
209 {
210 return read_handle.size();
211 }
212 };
213
216 template <typename K, typename V, typename KSerialiser, typename VSerialiser>
218 {
219 protected:
221
222 public:
224
233 void put(const K& key, const V& value)
234 {
236 KSerialiser::to_serialised(key), VSerialiser::to_serialised(value));
237 }
238
245 void remove(const K& key)
246 {
247 write_handle.remove(KSerialiser::to_serialised(key));
248 }
249
252 void clear()
253 {
255 }
256 };
257
264 template <typename K, typename V, typename KSerialiser, typename VSerialiser>
265 class MapHandle : public AbstractHandle,
266 public ReadableMapHandle<K, V, KSerialiser, VSerialiser>,
267 public WriteableMapHandle<K, V, KSerialiser, VSerialiser>
268 {
269 protected:
271
274
275 public:
277 ccf::kv::untyped::ChangeSet& changes, const std::string& map_name) :
280 untyped_handle(changes, map_name)
281 {}
282 };
283}
Definition abstract_handle.h:8
Definition map_handle.h:268
ccf::kv::untyped::MapHandle untyped_handle
Definition map_handle.h:270
MapHandle(ccf::kv::untyped::ChangeSet &changes, const std::string &map_name)
Definition map_handle.h:276
Definition map_handle.h:13
bool has(const K &key)
Definition map_handle.h:93
V ValueType
Definition map_handle.h:19
K KeyType
Definition map_handle.h:18
ReadableMapHandle(ccf::kv::untyped::MapHandle &uh)
Definition map_handle.h:21
std::optional< V > get_globally_committed(const K &key)
Definition map_handle.h:71
void foreach_value(F &&f)
Definition map_handle.h:190
ccf::kv::untyped::MapHandle & read_handle
Definition map_handle.h:15
std::string get_name_of_map() const
Definition map_handle.h:27
void foreach_key(F &&f)
Definition map_handle.h:169
size_t size()
Definition map_handle.h:208
std::optional< V > get(const K &key)
Definition map_handle.h:45
std::optional< Version > get_version_of_previous_write(const K &key)
Definition map_handle.h:112
Definition map_handle.h:218
void remove(const K &key)
Definition map_handle.h:245
void put(const K &key, const V &value)
Definition map_handle.h:233
WriteableMapHandle(ccf::kv::untyped::MapHandle &uh)
Definition map_handle.h:223
void clear()
Definition map_handle.h:252
ccf::kv::untyped::MapHandle & write_handle
Definition map_handle.h:220
Definition untyped_map_handle.h:18
void clear()
Definition untyped_map_handle.cpp:175
bool has(const KeyType &key)
Definition untyped_map_handle.cpp:145
void put(const KeyType &key, const ValueType &value)
Definition untyped_map_handle.cpp:160
std::optional< Version > get_version_of_previous_write(const KeyType &key)
Definition untyped_map_handle.cpp:111
std::optional< ValueType > get(const KeyType &key)
Definition untyped_map_handle.cpp:96
std::optional< ValueType > get_globally_committed(const KeyType &key)
Definition untyped_map_handle.cpp:131
size_t size()
Definition untyped_map_handle.cpp:188
void remove(const KeyType &key)
Definition untyped_map_handle.cpp:168
std::string get_name_of_map() const
Definition untyped_map_handle.cpp:91
void foreach(const ElementVisitorWithEarlyOut &fn)
Definition untyped_map_handle.cpp:183
ccf::ByteVector SerialisedEntry
Definition serialised_entry.h:8
Definition app_interface.h:19
Definition untyped_change_set.h:43