Apache Ignite C++
cache.h
Go to the documentation of this file.
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 
23 #ifndef _IGNITE_CACHE
24 #define _IGNITE_CACHE
25 
26 #include <map>
27 #include <set>
28 
29 #include <ignite/common/common.h>
30 #include <ignite/common/concurrent.h>
31 
39 #include "ignite/impl/cache/cache_impl.h"
40 #include "ignite/impl/operations.h"
41 #include "ignite/ignite_error.h"
42 
43 namespace ignite
44 {
45  namespace cache
46  {
50  template<typename K, typename V>
51  class IGNITE_IMPORT_EXPORT Cache
52  {
53  public:
57  Cache(impl::cache::CacheImpl* impl) : impl(ignite::common::concurrent::SharedPointer<impl::cache::CacheImpl>(impl))
58  {
59  // No-op.
60  }
61 
65  const char* GetName() const
66  {
67  return impl.Get()->GetName();
68  }
69 
76  bool IsEmpty()
77  {
78  IgniteError err;
79 
80  bool res = IsEmpty(err);
81 
83 
84  return res;
85  }
86 
94  bool IsEmpty(IgniteError& err)
95  {
96  return impl.Get()->IsEmpty(&err);
97  }
98 
105  bool ContainsKey(const K& key)
106  {
107  IgniteError err;
108 
109  bool res = ContainsKey(key, err);
110 
112 
113  return res;
114  }
115 
123  bool ContainsKey(const K& key, IgniteError& err)
124  {
125  impl::In1Operation<K> op(&key);
126 
127  return impl.Get()->ContainsKey(op, &err);
128  }
129 
136  bool ContainsKeys(const std::set<K>& keys)
137  {
138  IgniteError err;
139 
140  bool res = ContainsKeys(keys, err);
141 
143 
144  return res;
145  }
146 
154  bool ContainsKeys(const std::set<K>& keys, IgniteError& err)
155  {
156  impl::InSetOperation<K> op(&keys);
157 
158  return impl.Get()->ContainsKeys(op, &err);
159  }
160 
172  V LocalPeek(const K& key, int32_t peekModes)
173  {
174  IgniteError err;
175 
176  V res = LocalPeek(key, peekModes, err);
177 
179 
180  return res;
181  }
182 
195  V LocalPeek(const K& key, int32_t peekModes, IgniteError& err)
196  {
197  impl::InCacheLocalPeekOperation<K> inOp(&key, peekModes);
198  impl::Out1Operation<V> outOp;
199 
200  impl.Get()->LocalPeek(inOp, outOp, peekModes, &err);
201 
202  return outOp.GetResult();
203  }
204 
215  V Get(const K& key)
216  {
217  IgniteError err;
218 
219  V res = Get(key, err);
220 
222 
223  return res;
224  }
225 
237  V Get(const K& key, IgniteError& err)
238  {
239  impl::In1Operation<K> inOp(&key);
240  impl::Out1Operation<V> outOp;
241 
242  impl.Get()->Get(inOp, outOp, &err);
243 
244  return outOp.GetResult();
245  }
246 
257  std::map<K, V> GetAll(const std::set<K>& keys)
258  {
259  IgniteError err;
260 
261  std::map<K, V> res = GetAll(keys, err);
262 
264 
265  return res;
266  }
267 
279  std::map<K, V> GetAll(const std::set<K>& keys, IgniteError& err)
280  {
281  impl::InSetOperation<K> inOp(&keys);
282  impl::OutMapOperation<K, V> outOp;
283 
284  impl.Get()->GetAll(inOp, outOp, &err);
285 
286  return outOp.GetResult();
287  }
288 
297  void Put(const K& key, const V& val)
298  {
299  IgniteError err;
300 
301  Put(key, val, err);
302 
304  }
305 
315  void Put(const K& key, const V& val, IgniteError& err)
316  {
317  impl::In2Operation<K, V> op(&key, &val);
318 
319  impl.Get()->Put(op, &err);
320  }
321 
329  void PutAll(const std::map<K, V>& vals)
330  {
331  IgniteError err;
332 
333  PutAll(vals, err);
334 
336  }
337 
346  void PutAll(const std::map<K, V>& vals, IgniteError& err)
347  {
348  impl::InMapOperation<K, V> op(&vals);
349 
350  impl.Get()->PutAll(op, &err);
351  }
352 
362  V GetAndPut(const K& key, const V& val)
363  {
364  IgniteError err;
365 
366  V res = GetAndPut(key, val, err);
367 
369 
370  return res;
371  }
372 
383  V GetAndPut(const K& key, const V& val, IgniteError& err)
384  {
385  impl::In2Operation<K, V> inOp(&key, &val);
386  impl::Out1Operation<V> outOp;
387 
388  impl.Get()->GetAndPut(inOp, outOp, &err);
389 
390  return outOp.GetResult();
391  }
392 
402  V GetAndReplace(const K& key, const V& val)
403  {
404  IgniteError err;
405 
406  V res = GetAndReplace(key, val, err);
407 
409 
410  return res;
411  }
412 
423  V GetAndReplace(const K& key, const V& val, IgniteError& err)
424  {
425  impl::In2Operation<K, V> inOp(&key, &val);
426  impl::Out1Operation<V> outOp;
427 
428  impl.Get()->GetAndReplace(inOp, outOp, &err);
429 
430  return outOp.GetResult();
431  }
432 
439  V GetAndRemove(const K& key)
440  {
441  IgniteError err;
442 
443  V res = GetAndRemove(key, err);
444 
446 
447  return res;
448  }
449 
457  V GetAndRemove(const K& key, IgniteError& err)
458  {
459  impl::In1Operation<K> inOp(&key);
460  impl::Out1Operation<V> outOp;
461 
462  impl.Get()->GetAndRemove(inOp, outOp, &err);
463 
464  return outOp.GetResult();
465  }
466 
475  bool PutIfAbsent(const K& key, const V& val)
476  {
477  IgniteError err;
478 
479  bool res = PutIfAbsent(key, val, err);
480 
482 
483  return res;
484  }
485 
495  bool PutIfAbsent(const K& key, const V& val, IgniteError& err)
496  {
497  impl::In2Operation<K, V> op(&key, &val);
498 
499  return impl.Get()->PutIfAbsent(op, &err);
500  }
501 
518  V GetAndPutIfAbsent(const K& key, const V& val)
519  {
520  IgniteError err;
521 
522  V res = GetAndPutIfAbsent(key, val, err);
523 
525 
526  return res;
527  }
528 
546  V GetAndPutIfAbsent(const K& key, const V& val, IgniteError& err)
547  {
548  impl::In2Operation<K, V> inOp(&key, &val);
549  impl::Out1Operation<V> outOp;
550 
551  impl.Get()->GetAndPutIfAbsent(inOp, outOp, &err);
552 
553  return outOp.GetResult();
554  }
555 
569  bool Replace(const K& key, const V& val)
570  {
571  IgniteError err;
572 
573  bool res = Replace(key, val, err);
574 
576 
577  return res;
578  }
579 
594  bool Replace(const K& key, const V& val, IgniteError& err)
595  {
596  impl::In2Operation<K, V> op(&key, &val);
597 
598  return impl.Get()->Replace(op, &err);
599  }
600 
611  bool Replace(const K& key, const V& oldVal, const V& newVal)
612  {
613  IgniteError err;
614 
615  bool res = Replace(key, oldVal, newVal, err);
616 
618 
619  return res;
620  }
621 
633  bool Replace(const K& key, const V& oldVal, const V& newVal, IgniteError& err)
634  {
635  impl::In3Operation<K, V, V> op(&key, &oldVal, &newVal);
636 
637  return impl.Get()->ReplaceIfEqual(op, &err);
638  }
639 
646  void LocalEvict(const std::set<K>& keys)
647  {
648  IgniteError err;
649 
650  LocalEvict(keys, err);
651 
653  }
654 
662  void LocalEvict(const std::set<K>& keys, IgniteError& err)
663  {
664  impl::InSetOperation<K> op(&keys);
665 
666  impl.Get()->LocalEvict(op, &err);
667  }
668 
672  void Clear()
673  {
674  IgniteError err;
675 
676  Clear(err);
677 
679  }
680 
686  void Clear(IgniteError& err)
687  {
688  impl.Get()->Clear(&err);
689  }
690 
697  void Clear(const K& key)
698  {
699  IgniteError err;
700 
701  Clear(key, err);
702 
704  }
705 
713  void Clear(const K& key, IgniteError& err)
714  {
715  impl::In1Operation<K> op(&key);
716 
717  impl.Get()->Clear(op, &err);
718  }
719 
726  void ClearAll(const std::set<K>& keys)
727  {
728  IgniteError err;
729 
730  ClearAll(keys, err);
731 
733  }
734 
742  void ClearAll(const std::set<K>& keys, IgniteError& err)
743  {
744  impl::InSetOperation<K> op(&keys);
745 
746  impl.Get()->ClearAll(op, &err);
747  }
748 
757  void LocalClear(const K& key)
758  {
759  IgniteError err;
760 
761  LocalClear(key, err);
762 
764  }
765 
775  void LocalClear(const K& key, IgniteError& err)
776  {
777  impl::In1Operation<K> op(&key);
778 
779  impl.Get()->LocalClear(op, &err);
780  }
781 
790  void LocalClearAll(const std::set<K>& keys)
791  {
792  IgniteError err;
793 
794  LocalClearAll(keys, err);
795 
797  }
798 
808  void LocalClearAll(const std::set<K>& keys, IgniteError& err)
809  {
810  impl::InSetOperation<K> op(&keys);
811 
812  impl.Get()->LocalClearAll(op, &err);
813  }
814 
828  bool Remove(const K& key)
829  {
830  IgniteError err;
831 
832  bool res = Remove(key, err);
833 
835 
836  return res;
837  }
838 
853  bool Remove(const K& key, IgniteError& err)
854  {
855  impl::In1Operation<K> op(&key);
856 
857  return impl.Get()->Remove(op, &err);
858  }
859 
869  bool Remove(const K& key, const V& val)
870  {
871  IgniteError err;
872 
873  bool res = Remove(key, val, err);
874 
876 
877  return res;
878  }
879 
890  bool Remove(const K& key, const V& val, IgniteError& err)
891  {
892  impl::In2Operation<K, V> op(&key, &val);
893 
894  return impl.Get()->RemoveIfEqual(op, &err);
895  }
896 
904  void RemoveAll(const std::set<K>& keys)
905  {
906  IgniteError err;
907 
908  RemoveAll(keys, err);
909 
911  }
912 
921  void RemoveAll(const std::set<K>& keys, IgniteError& err)
922  {
923  impl::InSetOperation<K> op(&keys);
924 
925  impl.Get()->RemoveAll(op, &err);
926  }
927 
935  void RemoveAll()
936  {
937  IgniteError err;
938 
939  RemoveAll(err);
940 
942  }
943 
952  {
953  return impl.Get()->RemoveAll(&err);
954  }
955 
961  int32_t LocalSize()
962  {
963  return LocalSize(IGNITE_PEEK_MODE_ALL);
964  }
965 
972  int32_t LocalSize(IgniteError& err)
973  {
974  return LocalSize(IGNITE_PEEK_MODE_ALL, err);
975  }
976 
983  int32_t LocalSize(int32_t peekModes)
984  {
985  IgniteError err;
986 
987  int32_t res = LocalSize(peekModes, err);
988 
990 
991  return res;
992  }
993 
1001  int32_t LocalSize(int32_t peekModes, IgniteError& err)
1002  {
1003  return impl.Get()->LocalSize(peekModes, &err);
1004  }
1005 
1012  int32_t Size()
1013  {
1015  }
1016 
1024  int32_t Size(IgniteError& err)
1025  {
1026  return Size(ignite::cache::IGNITE_PEEK_MODE_ALL, err);
1027  }
1028 
1036  int32_t Size(int32_t peekModes)
1037  {
1038  IgniteError err;
1039 
1040  int32_t res = Size(peekModes, err);
1041 
1043 
1044  return res;
1045  }
1046 
1055  int32_t Size(int32_t peekModes, IgniteError& err)
1056  {
1057  return impl.Get()->Size(peekModes, &err);
1058  }
1059 
1067  {
1068  IgniteError err;
1069 
1070  query::QueryCursor<K, V> res = Query(qry, err);
1071 
1073 
1074  return res;
1075  }
1076 
1085  {
1086  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QuerySql(qry, &err);
1087 
1088  return query::QueryCursor<K, V>(cursorImpl);
1089  }
1090 
1098  {
1099  IgniteError err;
1100 
1101  query::QueryCursor<K, V> res = Query(qry, err);
1102 
1104 
1105  return res;
1106  }
1107 
1116  {
1117  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QueryText(qry, &err);
1118 
1119  return query::QueryCursor<K, V>(cursorImpl);
1120  }
1121 
1129  {
1130  IgniteError err;
1131 
1132  query::QueryCursor<K, V> res = Query(qry, err);
1133 
1135 
1136  return res;
1137  }
1138 
1147  {
1148  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QueryScan(qry, &err);
1149 
1150  return query::QueryCursor<K, V>(cursorImpl);
1151  }
1152 
1160  {
1161  IgniteError err;
1162 
1163  query::QueryFieldsCursor res = Query(qry, err);
1164 
1166 
1167  return res;
1168  }
1169 
1178  {
1179  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QuerySqlFields(qry, &err);
1180 
1181  return query::QueryFieldsCursor(cursorImpl);
1182  }
1183 
1189  bool IsValid()
1190  {
1191  return impl.IsValid();
1192  }
1193 
1194  private:
1196  ignite::common::concurrent::SharedPointer<impl::cache::CacheImpl> impl;
1197  };
1198  }
1199 }
1200 
1201 #endif
const char * GetName() const
Name of this cache (null for default cache).
Definition: cache.h:65
query::QueryCursor< K, V > Query(const query::TextQuery &qry)
Perform text query.
Definition: cache.h:1097
void ClearAll(const std::set< K > &keys)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:726
Text query.
Definition: query_text.h:40
void Clear()
Clear cache.
Definition: cache.h:672
V GetAndPut(const K &key, const V &val, IgniteError &err)
Associates the specified value with the specified key in this cache, returning an existing value if o...
Definition: cache.h:383
int32_t LocalSize(int32_t peekModes)
Gets the number of all entries cached on this node.
Definition: cache.h:983
void LocalClearAll(const std::set< K > &keys, IgniteError &err)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:808
Declares ignite::cache::query::QueryCursor class template.
bool Remove(const K &key, const V &val)
Removes given key mapping from cache if one exists and value is equal to the passed in value...
Definition: cache.h:869
Declares ignite::cache::query::QueryFieldsCursor class.
Declares ignite::cache::query::SqlQuery class.
void Clear(const K &key)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:697
Main entry point for all Data Grid APIs.
Definition: cache.h:51
Declares ignite::cache::CachePeekMode enum.
bool Remove(const K &key, const V &val, IgniteError &err)
Removes given key mapping from cache if one exists and value is equal to the passed in value...
Definition: cache.h:890
int32_t Size()
Gets the number of all entries cached across all nodes.
Definition: cache.h:1012
bool ContainsKeys(const std::set< K > &keys)
Check if cache contains mapping for these keys.
Definition: cache.h:136
query::QueryCursor< K, V > Query(const query::SqlQuery &qry)
Perform SQL query.
Definition: cache.h:1066
void Clear(const K &key, IgniteError &err)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:713
V GetAndPutIfAbsent(const K &key, const V &val, IgniteError &err)
Stores given key-value pair in cache only if cache had no previous mapping for it.
Definition: cache.h:546
Cache(impl::cache::CacheImpl *impl)
Constructor.
Definition: cache.h:57
bool Remove(const K &key)
Removes given key mapping from cache.
Definition: cache.h:828
bool IsEmpty(IgniteError &err)
Checks whether this cache contains no key-value mappings.
Definition: cache.h:94
void Clear(IgniteError &err)
Clear cache.
Definition: cache.h:686
bool ContainsKeys(const std::set< K > &keys, IgniteError &err)
Check if cache contains mapping for these keys.
Definition: cache.h:154
V Get(const K &key, IgniteError &err)
Retrieves value mapped to the specified key from cache.
Definition: cache.h:237
void RemoveAll()
Removes all mappings from cache.
Definition: cache.h:935
Scan query.
Definition: query_scan.h:40
Sql query.
Definition: query_sql.h:42
query::QueryCursor< K, V > Query(const query::ScanQuery &qry)
Perform scan query.
Definition: cache.h:1128
query::QueryCursor< K, V > Query(const query::SqlQuery &qry, IgniteError &err)
Perform SQL query.
Definition: cache.h:1084
V GetAndRemove(const K &key, IgniteError &err)
Atomically removes the entry for a key only if currently mapped to some value.
Definition: cache.h:457
V LocalPeek(const K &key, int32_t peekModes)
Peeks at cached value using optional set of peek modes.
Definition: cache.h:172
void RemoveAll(const std::set< K > &keys)
Removes given key mappings from cache.
Definition: cache.h:904
bool Replace(const K &key, const V &oldVal, const V &newVal, IgniteError &err)
Stores given key-value pair in cache only if only if the previous value is equal to the old value pas...
Definition: cache.h:633
void ClearAll(const std::set< K > &keys, IgniteError &err)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:742
V GetAndPut(const K &key, const V &val)
Associates the specified value with the specified key in this cache, returning an existing value if o...
Definition: cache.h:362
Sql fields query.
Definition: query_sql_fields.h:42
bool ContainsKey(const K &key)
Check if cache contains mapping for this key.
Definition: cache.h:105
bool IsValid()
Check if the instance is valid.
Definition: cache.h:1189
void LocalClear(const K &key)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:757
int32_t Size(IgniteError &err)
Gets the number of all entries cached across all nodes.
Definition: cache.h:1024
bool IsEmpty()
Checks whether this cache contains no key-value mappings.
Definition: cache.h:76
void RemoveAll(const std::set< K > &keys, IgniteError &err)
Removes given key mappings from cache.
Definition: cache.h:921
void PutAll(const std::map< K, V > &vals)
Stores given key-value pairs in cache.
Definition: cache.h:329
void LocalEvict(const std::set< K > &keys, IgniteError &err)
Attempts to evict all entries associated with keys.
Definition: cache.h:662
V GetAndRemove(const K &key)
Atomically removes the entry for a key only if currently mapped to some value.
Definition: cache.h:439
Peeks into all available cache storages.
Definition: cache_peek_mode.h:38
Query fields cursor.
Definition: query_fields_cursor.h:45
V Get(const K &key)
Retrieves value mapped to the specified key from cache.
Definition: cache.h:215
void Put(const K &key, const V &val)
Associates the specified value with the specified key in the cache.
Definition: cache.h:297
void LocalEvict(const std::set< K > &keys)
Attempts to evict all entries associated with keys.
Definition: cache.h:646
bool Replace(const K &key, const V &oldVal, const V &newVal)
Stores given key-value pair in cache only if only if the previous value is equal to the old value pas...
Definition: cache.h:611
int32_t Size(int32_t peekModes, IgniteError &err)
Gets the number of all entries cached across all nodes.
Definition: cache.h:1055
bool PutIfAbsent(const K &key, const V &val)
Atomically associates the specified key with the given value if it is not already associated with a v...
Definition: cache.h:475
bool Replace(const K &key, const V &val, IgniteError &err)
Stores given key-value pair in cache only if there is a previous mapping for it.
Definition: cache.h:594
int32_t Size(int32_t peekModes)
Gets the number of all entries cached across all nodes.
Definition: cache.h:1036
std::map< K, V > GetAll(const std::set< K > &keys)
Retrieves values mapped to the specified keys from cache.
Definition: cache.h:257
V GetAndReplace(const K &key, const V &val, IgniteError &err)
Atomically replaces the value for a given key if and only if there is a value currently mapped by the...
Definition: cache.h:423
static void ThrowIfNeeded(IgniteError &err)
Throw an error if code is not IGNITE_SUCCESS.
Definition: ignite_error.cpp:27
bool ContainsKey(const K &key, IgniteError &err)
Check if cache contains mapping for this key.
Definition: cache.h:123
void PutAll(const std::map< K, V > &vals, IgniteError &err)
Stores given key-value pairs in cache.
Definition: cache.h:346
V LocalPeek(const K &key, int32_t peekModes, IgniteError &err)
Peeks at cached value using optional set of peek modes.
Definition: cache.h:195
int32_t LocalSize(IgniteError &err)
Gets the number of all entries cached on this node.
Definition: cache.h:972
Declares ignite::cache::query::SqlFieldsQuery class.
Ignite error information.
Definition: ignite_error.h:78
V GetAndPutIfAbsent(const K &key, const V &val)
Stores given key-value pair in cache only if cache had no previous mapping for it.
Definition: cache.h:518
void LocalClearAll(const std::set< K > &keys)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:790
Declares ignite::cache::query::ScanQuery class.
bool PutIfAbsent(const K &key, const V &val, IgniteError &err)
Atomically associates the specified key with the given value if it is not already associated with a v...
Definition: cache.h:495
void Put(const K &key, const V &val, IgniteError &err)
Associates the specified value with the specified key in the cache.
Definition: cache.h:315
V GetAndReplace(const K &key, const V &val)
Atomically replaces the value for a given key if and only if there is a value currently mapped by the...
Definition: cache.h:402
query::QueryCursor< K, V > Query(const query::ScanQuery &qry, IgniteError &err)
Perform scan query.
Definition: cache.h:1146
Apache Ignite API.
Definition: binary_consts.h:28
query::QueryCursor< K, V > Query(const query::TextQuery &qry, IgniteError &err)
Perform text query.
Definition: cache.h:1115
Declares ignite::IgniteError class.
query::QueryFieldsCursor Query(const query::SqlFieldsQuery &qry, IgniteError &err)
Perform sql fields query.
Definition: cache.h:1177
bool Replace(const K &key, const V &val)
Stores given key-value pair in cache only if there is a previous mapping for it.
Definition: cache.h:569
query::QueryFieldsCursor Query(const query::SqlFieldsQuery &qry)
Perform sql fields query.
Definition: cache.h:1159
Query cursor.
Definition: query_cursor.h:45
void RemoveAll(IgniteError &err)
Removes all mappings from cache.
Definition: cache.h:951
std::map< K, V > GetAll(const std::set< K > &keys, IgniteError &err)
Retrieves values mapped to the specified keys from cache.
Definition: cache.h:279
int32_t LocalSize()
Gets the number of all entries cached on this node.
Definition: cache.h:961
bool Remove(const K &key, IgniteError &err)
Removes given key mapping from cache.
Definition: cache.h:853
void LocalClear(const K &key, IgniteError &err)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:775
Declares ignite::cache::query::TextQuery class.
int32_t LocalSize(int32_t peekModes, IgniteError &err)
Gets the number of all entries cached on this node.
Definition: cache.h:1001