Apache Ignite C++ Client
Loading...
Searching...
No Matches
key_value_view.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#pragma once
19
20#include "ignite/client/detail/type_mapping_utils.h"
21#include "ignite/client/table/ignite_tuple.h"
22#include "ignite/client/transaction/transaction.h"
23#include "ignite/client/type_mapping.h"
24
25#include "ignite/common/detail/config.h"
26#include "ignite/common/ignite_result.h"
27
28#include <memory>
29#include <type_traits>
30#include <utility>
31#include <vector>
32
33namespace ignite {
34
35class table;
36
37namespace detail {
38class table_impl;
39}
40
41template<typename K, typename V>
42class key_value_view;
43
47template<>
48class key_value_view<ignite_tuple, ignite_tuple> {
49 friend class table;
50
51public:
52 typedef ignite_tuple key_type;
53 typedef ignite_tuple value_type;
54
55 // Deleted
56 key_value_view(const key_value_view &) = delete;
57 key_value_view &operator=(const key_value_view &) = delete;
58
59 // Default
60 key_value_view() = default;
61 key_value_view(key_value_view &&) noexcept = default;
62 key_value_view &operator=(key_value_view &&) noexcept = default;
63
73 IGNITE_API void get_async(
74 transaction *tx, const key_type &key, ignite_callback<std::optional<value_type>> callback);
75
84 [[nodiscard]] IGNITE_API std::optional<value_type> get(transaction *tx, const key_type &key) {
85 return sync<std::optional<value_type>>(
86 [this, tx, &key](auto callback) { get_async(tx, key, std::move(callback)); });
87 }
88
101 IGNITE_API void get_all_async(
102 transaction *tx, std::vector<key_type> keys, ignite_callback<std::vector<std::optional<value_type>>> callback);
103
115 [[nodiscard]] IGNITE_API std::vector<std::optional<value_type>> get_all(
116 transaction *tx, std::vector<key_type> keys) {
117 return sync<std::vector<std::optional<value_type>>>([this, tx, keys = std::move(keys)](auto callback) mutable {
118 get_all_async(tx, std::move(keys), std::move(callback));
119 });
120 }
121
131 IGNITE_API void contains_async(transaction *tx, const key_type &key, ignite_callback<bool> callback);
132
141 [[nodiscard]] IGNITE_API bool contains(transaction *tx, const key_type &key) {
142 return sync<bool>([this, tx, &key](auto callback) { contains_async(tx, key, std::move(callback)); });
143 }
144
154 IGNITE_API void put_async(
155 transaction *tx, const key_type &key, const value_type &value, ignite_callback<void> callback);
156
165 IGNITE_API void put(transaction *tx, const key_type &key, const value_type &value) {
166 sync<void>([this, tx, &key, &value](auto callback) { put_async(tx, key, value, std::move(callback)); });
167 }
168
177 IGNITE_API void put_all_async(
178 transaction *tx, const std::vector<std::pair<key_type, value_type>> &pairs, ignite_callback<void> callback);
179
187 IGNITE_API void put_all(transaction *tx, const std::vector<std::pair<key_type, value_type>> &pairs) {
188 sync<void>([this, tx, pairs](auto callback) mutable { put_all_async(tx, pairs, std::move(callback)); });
189 }
190
201 IGNITE_API void get_and_put_async(transaction *tx, const key_type &key, const value_type &value,
202 ignite_callback<std::optional<value_type>> callback);
203
213 [[nodiscard]] IGNITE_API std::optional<value_type> get_and_put(
214 transaction *tx, const key_type &key, const value_type &value) {
215 return sync<std::optional<value_type>>(
216 [this, tx, &key, &value](auto callback) { get_and_put_async(tx, key, value, std::move(callback)); });
217 }
218
230 IGNITE_API void put_if_absent_async(
231 transaction *tx, const key_type &key, const value_type &value, ignite_callback<bool> callback);
232
241 IGNITE_API bool put_if_absent(transaction *tx, const key_type &key, const value_type &value) {
242 return sync<bool>(
243 [this, tx, &key, &value](auto callback) { put_if_absent_async(tx, key, value, std::move(callback)); });
244 }
245
255 IGNITE_API void remove_async(transaction *tx, const key_type &key, ignite_callback<bool> callback);
256
265 IGNITE_API bool remove(transaction *tx, const key_type &key) {
266 return sync<bool>([this, tx, &key](auto callback) { remove_async(tx, key, std::move(callback)); });
267 }
268
279 IGNITE_API void remove_async(
280 transaction *tx, const key_type &key, const value_type &value, ignite_callback<bool> callback);
281
292 IGNITE_API bool remove(transaction *tx, const key_type &key, const value_type &value) {
293 return sync<bool>(
294 [this, tx, &key, &value](auto callback) { remove_async(tx, key, value, std::move(callback)); });
295 }
296
307 IGNITE_API void remove_all_async(
308 transaction *tx, std::vector<key_type> keys, ignite_callback<std::vector<key_type>> callback);
309
319 IGNITE_API std::vector<key_type> remove_all(transaction *tx, std::vector<key_type> keys) {
320 return sync<std::vector<key_type>>([this, tx, keys = std::move(keys)](auto callback) mutable {
321 remove_all_async(tx, std::move(keys), std::move(callback));
322 });
323 }
324
335 IGNITE_API void remove_all_async(transaction *tx, const std::vector<std::pair<key_type, value_type>> &pairs,
336 ignite_callback<std::vector<key_type>> callback);
337
347 IGNITE_API std::vector<key_type> remove_all(transaction *tx, std::vector<std::pair<key_type, value_type>> pairs) {
348 return sync<std::vector<key_type>>([this, tx, pairs = std::move(pairs)](auto callback) mutable {
349 remove_all_async(tx, std::move(pairs), std::move(callback));
350 });
351 }
352
362 IGNITE_API void get_and_remove_async(
363 transaction *tx, const key_type &key, ignite_callback<std::optional<value_type>> callback);
364
373 IGNITE_API std::optional<value_type> get_and_remove(transaction *tx, const key_type &key) {
374 return sync<std::optional<value_type>>(
375 [this, tx, &key](auto callback) { get_and_remove_async(tx, key, std::move(callback)); });
376 }
377
389 IGNITE_API void replace_async(
390 transaction *tx, const key_type &key, const value_type &value, ignite_callback<bool> callback);
391
403 IGNITE_API bool replace(transaction *tx, const key_type &key, const value_type &value) {
404 return sync<bool>(
405 [this, tx, &key, &value](auto callback) { replace_async(tx, key, value, std::move(callback)); });
406 }
407
420 IGNITE_API void replace_async(transaction *tx, const key_type &key, const value_type &old_value,
421 const value_type &new_value, ignite_callback<bool> callback);
422
434 IGNITE_API bool replace(
435 transaction *tx, const key_type &key, const value_type &old_value, const value_type &new_value) {
436 return sync<bool>([this, tx, &key, &old_value, &new_value](
437 auto callback) { replace_async(tx, key, old_value, new_value, std::move(callback)); });
438 }
439
451 IGNITE_API void get_and_replace_async(transaction *tx, const key_type &key, const value_type &value,
452 ignite_callback<std::optional<value_type>> callback);
453
465 [[nodiscard]] IGNITE_API std::optional<value_type> get_and_replace(
466 transaction *tx, const key_type &key, const value_type &value) {
467 return sync<std::optional<value_type>>(
468 [this, tx, &key, &value](auto callback) { get_and_replace_async(tx, key, value, std::move(callback)); });
469 }
470
471private:
477 explicit key_value_view(std::shared_ptr<detail::table_impl> impl)
478 : m_impl(std::move(impl)) {}
479
481 std::shared_ptr<detail::table_impl> m_impl;
482};
483
487template<typename K, typename V>
488class key_value_view {
489 friend class table;
490
491public:
492 typedef typename std::decay<K>::type key_type;
493 typedef typename std::decay<V>::type value_type;
494
495 // Deleted
496 key_value_view(const key_value_view &) = delete;
497 key_value_view &operator=(const key_value_view &) = delete;
498
499 // Default
500 key_value_view() = default;
501 key_value_view(key_value_view &&) noexcept = default;
502 key_value_view &operator=(key_value_view &&) noexcept = default;
503
513 void get_async(transaction *tx, const key_type &key, ignite_callback<std::optional<value_type>> callback) {
514 m_delegate.get_async(tx, convert_to_tuple(key),
515 [callback = std::move(callback)](auto res) { callback(convert_result<value_type>(std::move(res))); });
516 }
517
526 [[nodiscard]] std::optional<value_type> get(transaction *tx, const key_type &key) {
527 return sync<std::optional<value_type>>(
528 [this, tx, &key](auto callback) { get_async(tx, key, std::move(callback)); });
529 }
530
544 transaction *tx, std::vector<key_type> keys, ignite_callback<std::vector<std::optional<value_type>>> callback) {
545 m_delegate.get_all_async(tx, values_to_tuples<key_type>(std::move(keys)),
546 [callback = std::move(callback)](auto res) { callback(convert_result<value_type>(std::move(res))); });
547 }
548
560 [[nodiscard]] std::vector<std::optional<value_type>> get_all(transaction *tx, std::vector<key_type> keys) {
561 return sync<std::vector<std::optional<value_type>>>([this, tx, keys = std::move(keys)](auto callback) mutable {
562 get_all_async(tx, std::move(keys), std::move(callback));
563 });
564 }
565
575 void contains_async(transaction *tx, const key_type &key, ignite_callback<bool> callback) {
576 m_delegate.contains_async(tx, convert_to_tuple(key), std::move(callback));
577 }
578
587 [[nodiscard]] bool contains(transaction *tx, const key_type &key) {
588 return sync<bool>([this, tx, &key](auto callback) { contains_async(tx, key, std::move(callback)); });
589 }
590
600 void put_async(transaction *tx, const key_type &key, const value_type &value, ignite_callback<void> callback) {
601 m_delegate.put_async(tx, convert_to_tuple(key), convert_to_tuple(value), std::move(callback));
602 }
603
612 void put(transaction *tx, const key_type &key, const value_type &value) {
613 sync<void>([this, tx, &key, &value](auto callback) { put_async(tx, key, value, std::move(callback)); });
614 }
615
625 transaction *tx, const std::vector<std::pair<key_type, value_type>> &pairs, ignite_callback<void> callback) {
626 m_delegate.put_all_async(tx, values_to_tuples<key_type, value_type>(std::move(pairs)), std::move(callback));
627 }
628
636 void put_all(transaction *tx, const std::vector<std::pair<key_type, value_type>> &pairs) {
637 sync<void>([this, tx, pairs](auto callback) mutable { put_all_async(tx, pairs, std::move(callback)); });
638 }
639
650 void get_and_put_async(transaction *tx, const key_type &key, const value_type &value,
651 ignite_callback<std::optional<value_type>> callback) {
652 m_delegate.get_and_put_async(tx, convert_to_tuple(key), convert_to_tuple(value),
653 [callback = std::move(callback)](auto res) { callback(convert_result<value_type>(std::move(res))); });
654 }
655
665 [[nodiscard]] std::optional<value_type> get_and_put(transaction *tx, const key_type &key, const value_type &value) {
666 return sync<std::optional<value_type>>(
667 [this, tx, &key, &value](auto callback) { get_and_put_async(tx, key, value, std::move(callback)); });
668 }
669
682 transaction *tx, const key_type &key, const value_type &value, ignite_callback<bool> callback) {
683 m_delegate.put_if_absent_async(tx, convert_to_tuple(key), convert_to_tuple(value), std::move(callback));
684 }
685
694 bool put_if_absent(transaction *tx, const key_type &key, const value_type &value) {
695 return sync<bool>(
696 [this, tx, &key, &value](auto callback) { put_if_absent_async(tx, key, value, std::move(callback)); });
697 }
698
708 void remove_async(transaction *tx, const key_type &key, ignite_callback<bool> callback) {
709 m_delegate.remove_async(tx, convert_to_tuple(key), std::move(callback));
710 }
711
720 bool remove(transaction *tx, const key_type &key) {
721 return sync<bool>([this, tx, &key](auto callback) { remove_async(tx, key, std::move(callback)); });
722 }
723
734 void remove_async(transaction *tx, const key_type &key, const value_type &value, ignite_callback<bool> callback) {
735 m_delegate.remove_async(tx, convert_to_tuple(key), convert_to_tuple(value), std::move(callback));
736 }
737
748 bool remove(transaction *tx, const key_type &key, const value_type &value) {
749 return sync<bool>(
750 [this, tx, &key, &value](auto callback) { remove_async(tx, key, value, std::move(callback)); });
751 }
752
764 transaction *tx, std::vector<key_type> keys, ignite_callback<std::vector<key_type>> callback) {
765 m_delegate.remove_all_async(tx, values_to_tuples<key_type>(std::move(keys)),
766 [callback = std::move(callback)](auto res) { callback(convert_result<key_type>(std::move(res))); });
767 }
768
778 std::vector<key_type> remove_all(transaction *tx, std::vector<key_type> keys) {
779 return sync<std::vector<key_type>>([this, tx, keys = std::move(keys)](auto callback) mutable {
780 remove_all_async(tx, std::move(keys), std::move(callback));
781 });
782 }
783
794 void remove_all_async(transaction *tx, const std::vector<std::pair<key_type, value_type>> &pairs,
795 ignite_callback<std::vector<key_type>> callback) {
796 m_delegate.remove_all_async(tx, values_to_tuples<key_type, value_type>(std::move(pairs)),
797 [callback = std::move(callback)](auto res) { callback(convert_result<key_type>(std::move(res))); });
798 }
799
809 std::vector<key_type> remove_all(transaction *tx, std::vector<std::pair<key_type, value_type>> pairs) {
810 return sync<std::vector<key_type>>([this, tx, pairs = std::move(pairs)](auto callback) mutable {
811 remove_all_async(tx, std::move(pairs), std::move(callback));
812 });
813 }
814
825 transaction *tx, const key_type &key, ignite_callback<std::optional<value_type>> callback) {
826 m_delegate.get_and_remove_async(tx, convert_to_tuple(key),
827 [callback = std::move(callback)](auto res) { callback(convert_result<value_type>(std::move(res))); });
828 }
829
838 std::optional<value_type> get_and_remove(transaction *tx, const key_type &key) {
839 return sync<std::optional<value_type>>(
840 [this, tx, &key](auto callback) { get_and_remove_async(tx, key, std::move(callback)); });
841 }
842
853 void replace_async(transaction *tx, const key_type &key, const value_type &value, ignite_callback<bool> callback) {
854 m_delegate.replace_async(tx, convert_to_tuple(key), convert_to_tuple(value), std::move(callback));
855 }
856
868 bool replace(transaction *tx, const key_type &key, const value_type &value) {
869 return sync<bool>(
870 [this, tx, &key, &value](auto callback) { replace_async(tx, key, value, std::move(callback)); });
871 }
872
885 void replace_async(transaction *tx, const key_type &key, const value_type &old_value, const value_type &new_value,
886 ignite_callback<bool> callback) {
887 m_delegate.replace_async(
888 tx, convert_to_tuple(key), convert_to_tuple(old_value), convert_to_tuple(new_value), std::move(callback));
889 }
890
902 bool replace(transaction *tx, const key_type &key, const value_type &old_value, const value_type &new_value) {
903 return sync<bool>([this, tx, &key, &old_value, &new_value](
904 auto callback) { replace_async(tx, key, old_value, new_value, std::move(callback)); });
905 }
906
918 void get_and_replace_async(transaction *tx, const key_type &key, const value_type &value,
919 ignite_callback<std::optional<value_type>> callback) {
920 m_delegate.get_and_replace_async(tx, convert_to_tuple(key), convert_to_tuple(value),
921 [callback = std::move(callback)](auto res) { callback(convert_result<value_type>(std::move(res))); });
922 }
923
935 [[nodiscard]] std::optional<value_type> get_and_replace(
936 transaction *tx, const key_type &key, const value_type &value) {
937 return sync<std::optional<value_type>>(
938 [this, tx, &key, &value](auto callback) { get_and_replace_async(tx, key, value, std::move(callback)); });
939 }
940
941private:
948 : m_delegate(std::move(delegate)) {}
949
951 key_value_view<ignite_tuple, ignite_tuple> m_delegate;
952};
953
954} // namespace ignite
Definition ignite_tuple.h:40
IGNITE_API std::vector< std::optional< value_type > > get_all(transaction *tx, std::vector< key_type > keys)
Definition key_value_view.h:115
IGNITE_API bool remove(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:292
IGNITE_API void put(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:165
IGNITE_API std::optional< value_type > get_and_remove(transaction *tx, const key_type &key)
Definition key_value_view.h:373
IGNITE_API bool put_if_absent(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:241
IGNITE_API std::optional< value_type > get_and_put(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:213
IGNITE_API bool contains(transaction *tx, const key_type &key)
Definition key_value_view.h:141
IGNITE_API bool replace(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:403
IGNITE_API std::optional< value_type > get(transaction *tx, const key_type &key)
Definition key_value_view.h:84
IGNITE_API std::vector< key_type > remove_all(transaction *tx, std::vector< key_type > keys)
Definition key_value_view.h:319
IGNITE_API std::optional< value_type > get_and_replace(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:465
IGNITE_API std::vector< key_type > remove_all(transaction *tx, std::vector< std::pair< key_type, value_type > > pairs)
Definition key_value_view.h:347
IGNITE_API bool remove(transaction *tx, const key_type &key)
Definition key_value_view.h:265
IGNITE_API bool replace(transaction *tx, const key_type &key, const value_type &old_value, const value_type &new_value)
Definition key_value_view.h:434
IGNITE_API void put_all(transaction *tx, const std::vector< std::pair< key_type, value_type > > &pairs)
Definition key_value_view.h:187
Definition key_value_view.h:488
bool replace(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:868
void get_all_async(transaction *tx, std::vector< key_type > keys, ignite_callback< std::vector< std::optional< value_type > > > callback)
Definition key_value_view.h:543
std::optional< value_type > get_and_remove(transaction *tx, const key_type &key)
Definition key_value_view.h:838
void put_if_absent_async(transaction *tx, const key_type &key, const value_type &value, ignite_callback< bool > callback)
Definition key_value_view.h:681
bool put_if_absent(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:694
void remove_async(transaction *tx, const key_type &key, const value_type &value, ignite_callback< bool > callback)
Definition key_value_view.h:734
void put_async(transaction *tx, const key_type &key, const value_type &value, ignite_callback< void > callback)
Definition key_value_view.h:600
std::optional< value_type > get_and_replace(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:935
void put_all_async(transaction *tx, const std::vector< std::pair< key_type, value_type > > &pairs, ignite_callback< void > callback)
Definition key_value_view.h:624
void get_and_remove_async(transaction *tx, const key_type &key, ignite_callback< std::optional< value_type > > callback)
Definition key_value_view.h:824
bool contains(transaction *tx, const key_type &key)
Definition key_value_view.h:587
bool remove(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:748
void replace_async(transaction *tx, const key_type &key, const value_type &value, ignite_callback< bool > callback)
Definition key_value_view.h:853
std::vector< key_type > remove_all(transaction *tx, std::vector< key_type > keys)
Definition key_value_view.h:778
void get_and_replace_async(transaction *tx, const key_type &key, const value_type &value, ignite_callback< std::optional< value_type > > callback)
Definition key_value_view.h:918
void put(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:612
void get_and_put_async(transaction *tx, const key_type &key, const value_type &value, ignite_callback< std::optional< value_type > > callback)
Definition key_value_view.h:650
bool remove(transaction *tx, const key_type &key)
Definition key_value_view.h:720
void put_all(transaction *tx, const std::vector< std::pair< key_type, value_type > > &pairs)
Definition key_value_view.h:636
std::optional< value_type > get_and_put(transaction *tx, const key_type &key, const value_type &value)
Definition key_value_view.h:665
void remove_async(transaction *tx, const key_type &key, ignite_callback< bool > callback)
Definition key_value_view.h:708
bool replace(transaction *tx, const key_type &key, const value_type &old_value, const value_type &new_value)
Definition key_value_view.h:902
void remove_all_async(transaction *tx, const std::vector< std::pair< key_type, value_type > > &pairs, ignite_callback< std::vector< key_type > > callback)
Definition key_value_view.h:794
void contains_async(transaction *tx, const key_type &key, ignite_callback< bool > callback)
Definition key_value_view.h:575
void get_async(transaction *tx, const key_type &key, ignite_callback< std::optional< value_type > > callback)
Definition key_value_view.h:513
std::vector< std::optional< value_type > > get_all(transaction *tx, std::vector< key_type > keys)
Definition key_value_view.h:560
void replace_async(transaction *tx, const key_type &key, const value_type &old_value, const value_type &new_value, ignite_callback< bool > callback)
Definition key_value_view.h:885
std::optional< value_type > get(transaction *tx, const key_type &key)
Definition key_value_view.h:526
std::vector< key_type > remove_all(transaction *tx, std::vector< std::pair< key_type, value_type > > pairs)
Definition key_value_view.h:809
void remove_all_async(transaction *tx, std::vector< key_type > keys, ignite_callback< std::vector< key_type > > callback)
Definition key_value_view.h:763
Definition table.h:41
Definition transaction.h:35