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_CACHE
24 #define _IGNITE_CACHE_CACHE
25 
26 #include <map>
27 #include <set>
28 
29 #include <ignite/common/common.h>
30 #include <ignite/common/concurrent.h>
31 #include <ignite/ignite_error.h>
32 
40 #include "ignite/impl/cache/cache_impl.h"
41 #include "ignite/impl/operations.h"
42 
43 namespace ignite
44 {
45  namespace cache
46  {
59  template<typename K, typename V>
60  class IGNITE_IMPORT_EXPORT Cache
61  {
62  public:
70  Cache(impl::cache::CacheImpl* impl) :
71  impl(impl)
72  {
73  // No-op.
74  }
75 
83  const char* GetName() const
84  {
85  return impl.Get()->GetName();
86  }
87 
96  bool IsEmpty()
97  {
98  IgniteError err;
99 
100  bool res = IsEmpty(err);
101 
103 
104  return res;
105  }
106 
116  bool IsEmpty(IgniteError& err)
117  {
118  return Size(err) == 0;
119  }
120 
129  bool ContainsKey(const K& key)
130  {
131  IgniteError err;
132 
133  bool res = ContainsKey(key, err);
134 
136 
137  return res;
138  }
139 
149  bool ContainsKey(const K& key, IgniteError& err)
150  {
151  impl::In1Operation<K> op(&key);
152 
153  return impl.Get()->ContainsKey(op, &err);
154  }
155 
164  bool ContainsKeys(const std::set<K>& keys)
165  {
166  IgniteError err;
167 
168  bool res = ContainsKeys(keys, err);
169 
171 
172  return res;
173  }
174 
184  bool ContainsKeys(const std::set<K>& keys, IgniteError& err)
185  {
186  impl::InSetOperation<K> op(&keys);
187 
188  return impl.Get()->ContainsKeys(op, &err);
189  }
190 
204  V LocalPeek(const K& key, int32_t peekModes)
205  {
206  IgniteError err;
207 
208  V res = LocalPeek(key, peekModes, err);
209 
211 
212  return res;
213  }
214 
229  V LocalPeek(const K& key, int32_t peekModes, IgniteError& err)
230  {
231  impl::InCacheLocalPeekOperation<K> inOp(&key, peekModes);
232  impl::Out1Operation<V> outOp;
233 
234  impl.Get()->LocalPeek(inOp, outOp, peekModes, &err);
235 
236  return outOp.GetResult();
237  }
238 
251  V Get(const K& key)
252  {
253  IgniteError err;
254 
255  V res = Get(key, err);
256 
258 
259  return res;
260  }
261 
275  V Get(const K& key, IgniteError& err)
276  {
277  impl::In1Operation<K> inOp(&key);
278  impl::Out1Operation<V> outOp;
279 
280  impl.Get()->Get(inOp, outOp, &err);
281 
282  return outOp.GetResult();
283  }
284 
297  std::map<K, V> GetAll(const std::set<K>& keys)
298  {
299  IgniteError err;
300 
301  std::map<K, V> res = GetAll(keys, err);
302 
304 
305  return res;
306  }
307 
321  std::map<K, V> GetAll(const std::set<K>& keys, IgniteError& err)
322  {
323  impl::InSetOperation<K> inOp(&keys);
324  impl::OutMapOperation<K, V> outOp;
325 
326  impl.Get()->GetAll(inOp, outOp, &err);
327 
328  return outOp.GetResult();
329  }
330 
341  void Put(const K& key, const V& val)
342  {
343  IgniteError err;
344 
345  Put(key, val, err);
346 
348  }
349 
361  void Put(const K& key, const V& val, IgniteError& err)
362  {
363  impl::In2Operation<K, V> op(&key, &val);
364 
365  impl.Get()->Put(op, &err);
366  }
367 
377  void PutAll(const std::map<K, V>& vals)
378  {
379  IgniteError err;
380 
381  PutAll(vals, err);
382 
384  }
385 
396  void PutAll(const std::map<K, V>& vals, IgniteError& err)
397  {
398  impl::InMapOperation<K, V> op(&vals);
399 
400  impl.Get()->PutAll(op, &err);
401  }
402 
414  V GetAndPut(const K& key, const V& val)
415  {
416  IgniteError err;
417 
418  V res = GetAndPut(key, val, err);
419 
421 
422  return res;
423  }
424 
437  V GetAndPut(const K& key, const V& val, IgniteError& err)
438  {
439  impl::In2Operation<K, V> inOp(&key, &val);
440  impl::Out1Operation<V> outOp;
441 
442  impl.Get()->GetAndPut(inOp, outOp, &err);
443 
444  return outOp.GetResult();
445  }
446 
458  V GetAndReplace(const K& key, const V& val)
459  {
460  IgniteError err;
461 
462  V res = GetAndReplace(key, val, err);
463 
465 
466  return res;
467  }
468 
481  V GetAndReplace(const K& key, const V& val, IgniteError& err)
482  {
483  impl::In2Operation<K, V> inOp(&key, &val);
484  impl::Out1Operation<V> outOp;
485 
486  impl.Get()->GetAndReplace(inOp, outOp, &err);
487 
488  return outOp.GetResult();
489  }
490 
499  V GetAndRemove(const K& key)
500  {
501  IgniteError err;
502 
503  V res = GetAndRemove(key, err);
504 
506 
507  return res;
508  }
509 
519  V GetAndRemove(const K& key, IgniteError& err)
520  {
521  impl::In1Operation<K> inOp(&key);
522  impl::Out1Operation<V> outOp;
523 
524  impl.Get()->GetAndRemove(inOp, outOp, &err);
525 
526  return outOp.GetResult();
527  }
528 
539  bool PutIfAbsent(const K& key, const V& val)
540  {
541  IgniteError err;
542 
543  bool res = PutIfAbsent(key, val, err);
544 
546 
547  return res;
548  }
549 
561  bool PutIfAbsent(const K& key, const V& val, IgniteError& err)
562  {
563  impl::In2Operation<K, V> op(&key, &val);
564 
565  return impl.Get()->PutIfAbsent(op, &err);
566  }
567 
586  V GetAndPutIfAbsent(const K& key, const V& val)
587  {
588  IgniteError err;
589 
590  V res = GetAndPutIfAbsent(key, val, err);
591 
593 
594  return res;
595  }
596 
616  V GetAndPutIfAbsent(const K& key, const V& val, IgniteError& err)
617  {
618  impl::In2Operation<K, V> inOp(&key, &val);
619  impl::Out1Operation<V> outOp;
620 
621  impl.Get()->GetAndPutIfAbsent(inOp, outOp, &err);
622 
623  return outOp.GetResult();
624  }
625 
641  bool Replace(const K& key, const V& val)
642  {
643  IgniteError err;
644 
645  bool res = Replace(key, val, err);
646 
648 
649  return res;
650  }
651 
668  bool Replace(const K& key, const V& val, IgniteError& err)
669  {
670  impl::In2Operation<K, V> op(&key, &val);
671 
672  return impl.Get()->Replace(op, &err);
673  }
674 
687  bool Replace(const K& key, const V& oldVal, const V& newVal)
688  {
689  IgniteError err;
690 
691  bool res = Replace(key, oldVal, newVal, err);
692 
694 
695  return res;
696  }
697 
711  bool Replace(const K& key, const V& oldVal, const V& newVal, IgniteError& err)
712  {
713  impl::In3Operation<K, V, V> op(&key, &oldVal, &newVal);
714 
715  return impl.Get()->ReplaceIfEqual(op, &err);
716  }
717 
728  void LocalEvict(const std::set<K>& keys)
729  {
730  IgniteError err;
731 
732  LocalEvict(keys, err);
733 
735  }
736 
748  void LocalEvict(const std::set<K>& keys, IgniteError& err)
749  {
750  impl::InSetOperation<K> op(&keys);
751 
752  impl.Get()->LocalEvict(op, &err);
753  }
754 
760  void Clear()
761  {
762  IgniteError err;
763 
764  Clear(err);
765 
767  }
768 
776  void Clear(IgniteError& err)
777  {
778  impl.Get()->Clear(&err);
779  }
780 
789  void Clear(const K& key)
790  {
791  IgniteError err;
792 
793  Clear(key, err);
794 
796  }
797 
807  void Clear(const K& key, IgniteError& err)
808  {
809  impl::In1Operation<K> op(&key);
810 
811  impl.Get()->Clear(op, &err);
812  }
813 
822  void ClearAll(const std::set<K>& keys)
823  {
824  IgniteError err;
825 
826  ClearAll(keys, err);
827 
829  }
830 
840  void ClearAll(const std::set<K>& keys, IgniteError& err)
841  {
842  impl::InSetOperation<K> op(&keys);
843 
844  impl.Get()->ClearAll(op, &err);
845  }
846 
858  void LocalClear(const K& key)
859  {
860  IgniteError err;
861 
862  LocalClear(key, err);
863 
865  }
866 
879  void LocalClear(const K& key, IgniteError& err)
880  {
881  impl::In1Operation<K> op(&key);
882 
883  impl.Get()->LocalClear(op, &err);
884  }
885 
897  void LocalClearAll(const std::set<K>& keys)
898  {
899  IgniteError err;
900 
901  LocalClearAll(keys, err);
902 
904  }
905 
918  void LocalClearAll(const std::set<K>& keys, IgniteError& err)
919  {
920  impl::InSetOperation<K> op(&keys);
921 
922  impl.Get()->LocalClearAll(op, &err);
923  }
924 
940  bool Remove(const K& key)
941  {
942  IgniteError err;
943 
944  bool res = Remove(key, err);
945 
947 
948  return res;
949  }
950 
967  bool Remove(const K& key, IgniteError& err)
968  {
969  impl::In1Operation<K> op(&key);
970 
971  return impl.Get()->Remove(op, &err);
972  }
973 
985  bool Remove(const K& key, const V& val)
986  {
987  IgniteError err;
988 
989  bool res = Remove(key, val, err);
990 
992 
993  return res;
994  }
995 
1008  bool Remove(const K& key, const V& val, IgniteError& err)
1009  {
1010  impl::In2Operation<K, V> op(&key, &val);
1011 
1012  return impl.Get()->RemoveIfEqual(op, &err);
1013  }
1014 
1024  void RemoveAll(const std::set<K>& keys)
1025  {
1026  IgniteError err;
1027 
1028  RemoveAll(keys, err);
1029 
1031  }
1032 
1043  void RemoveAll(const std::set<K>& keys, IgniteError& err)
1044  {
1045  impl::InSetOperation<K> op(&keys);
1046 
1047  impl.Get()->RemoveAll(op, &err);
1048  }
1049 
1058  void RemoveAll()
1059  {
1060  IgniteError err;
1061 
1062  RemoveAll(err);
1063 
1065  }
1066 
1077  {
1078  return impl.Get()->RemoveAll(&err);
1079  }
1080 
1088  int32_t LocalSize()
1089  {
1090  return LocalSize(IGNITE_PEEK_MODE_ALL);
1091  }
1092 
1101  int32_t LocalSize(IgniteError& err)
1102  {
1103  return LocalSize(IGNITE_PEEK_MODE_ALL, err);
1104  }
1105 
1114  int32_t LocalSize(int32_t peekModes)
1115  {
1116  IgniteError err;
1117 
1118  int32_t res = LocalSize(peekModes, err);
1119 
1121 
1122  return res;
1123  }
1124 
1134  int32_t LocalSize(int32_t peekModes, IgniteError& err)
1135  {
1136  return impl.Get()->Size(peekModes, true, &err);
1137  }
1138 
1147  int32_t Size()
1148  {
1150  }
1151 
1161  int32_t Size(IgniteError& err)
1162  {
1163  return Size(ignite::cache::IGNITE_PEEK_MODE_ALL, err);
1164  }
1165 
1175  int32_t Size(int32_t peekModes)
1176  {
1177  IgniteError err;
1178 
1179  int32_t res = Size(peekModes, err);
1180 
1182 
1183  return res;
1184  }
1185 
1196  int32_t Size(int32_t peekModes, IgniteError& err)
1197  {
1198  return impl.Get()->Size(peekModes, false, &err);
1199  }
1200 
1210  {
1211  IgniteError err;
1212 
1213  query::QueryCursor<K, V> res = Query(qry, err);
1214 
1216 
1217  return res;
1218  }
1219 
1230  {
1231  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QuerySql(qry, &err);
1232 
1233  return query::QueryCursor<K, V>(cursorImpl);
1234  }
1235 
1245  {
1246  IgniteError err;
1247 
1248  query::QueryCursor<K, V> res = Query(qry, err);
1249 
1251 
1252  return res;
1253  }
1254 
1265  {
1266  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QueryText(qry, &err);
1267 
1268  return query::QueryCursor<K, V>(cursorImpl);
1269  }
1270 
1280  {
1281  IgniteError err;
1282 
1283  query::QueryCursor<K, V> res = Query(qry, err);
1284 
1286 
1287  return res;
1288  }
1289 
1300  {
1301  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QueryScan(qry, &err);
1302 
1303  return query::QueryCursor<K, V>(cursorImpl);
1304  }
1305 
1315  {
1316  IgniteError err;
1317 
1318  query::QueryFieldsCursor res = Query(qry, err);
1319 
1321 
1322  return res;
1323  }
1324 
1335  {
1336  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QuerySqlFields(qry, &err);
1337 
1338  return query::QueryFieldsCursor(cursorImpl);
1339  }
1340 
1352  bool IsValid() const
1353  {
1354  return impl.IsValid();
1355  }
1356 
1357  private:
1359  ignite::common::concurrent::SharedPointer<impl::cache::CacheImpl> impl;
1360  };
1361  }
1362 }
1363 
1364 #endif //_IGNITE_CACHE_CACHE
const char * GetName() const
Get name of this cache (null for default cache).
Definition: cache.h:83
query::QueryCursor< K, V > Query(const query::TextQuery &qry)
Perform text query.
Definition: cache.h:1244
void ClearAll(const std::set< K > &keys)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:822
Text query.
Definition: query_text.h:40
void Clear()
Clear cache.
Definition: cache.h:760
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:437
int32_t LocalSize(int32_t peekModes)
Gets the number of all entries cached on this node.
Definition: cache.h:1114
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:918
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:985
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:789
Main entry point for all Data Grid APIs.
Definition: cache.h:60
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:1008
int32_t Size()
Gets the number of all entries cached across all nodes.
Definition: cache.h:1147
bool ContainsKeys(const std::set< K > &keys)
Check if cache contains mapping for these keys.
Definition: cache.h:164
query::QueryCursor< K, V > Query(const query::SqlQuery &qry)
Perform SQL query.
Definition: cache.h:1209
void Clear(const K &key, IgniteError &err)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:807
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:616
Cache(impl::cache::CacheImpl *impl)
Constructor.
Definition: cache.h:70
bool Remove(const K &key)
Removes given key mapping from cache.
Definition: cache.h:940
bool IsEmpty(IgniteError &err)
Checks whether this cache contains no key-value mappings.
Definition: cache.h:116
void Clear(IgniteError &err)
Clear cache.
Definition: cache.h:776
bool ContainsKeys(const std::set< K > &keys, IgniteError &err)
Check if cache contains mapping for these keys.
Definition: cache.h:184
V Get(const K &key, IgniteError &err)
Retrieves value mapped to the specified key from cache.
Definition: cache.h:275
void RemoveAll()
Removes all mappings from cache.
Definition: cache.h:1058
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:1279
query::QueryCursor< K, V > Query(const query::SqlQuery &qry, IgniteError &err)
Perform SQL query.
Definition: cache.h:1229
V GetAndRemove(const K &key, IgniteError &err)
Atomically removes the entry for a key only if currently mapped to some value.
Definition: cache.h:519
V LocalPeek(const K &key, int32_t peekModes)
Peeks at cached value using optional set of peek modes.
Definition: cache.h:204
void RemoveAll(const std::set< K > &keys)
Removes given key mappings from cache.
Definition: cache.h:1024
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:711
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:840
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:414
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:129
void LocalClear(const K &key)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:858
int32_t Size(IgniteError &err)
Gets the number of all entries cached across all nodes.
Definition: cache.h:1161
bool IsEmpty()
Checks whether this cache contains no key-value mappings.
Definition: cache.h:96
bool IsValid() const
Check if the instance is valid.
Definition: cache.h:1352
void RemoveAll(const std::set< K > &keys, IgniteError &err)
Removes given key mappings from cache.
Definition: cache.h:1043
void PutAll(const std::map< K, V > &vals)
Stores given key-value pairs in cache.
Definition: cache.h:377
void LocalEvict(const std::set< K > &keys, IgniteError &err)
Attempts to evict all entries associated with keys.
Definition: cache.h:748
V GetAndRemove(const K &key)
Atomically removes the entry for a key only if currently mapped to some value.
Definition: cache.h:499
Peeks into all available cache storages.
Definition: cache_peek_mode.h:38
Query fields cursor.
Definition: query_fields_cursor.h:50
V Get(const K &key)
Retrieves value mapped to the specified key from cache.
Definition: cache.h:251
void Put(const K &key, const V &val)
Associates the specified value with the specified key in the cache.
Definition: cache.h:341
void LocalEvict(const std::set< K > &keys)
Attempts to evict all entries associated with keys.
Definition: cache.h:728
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:687
int32_t Size(int32_t peekModes, IgniteError &err)
Gets the number of all entries cached across all nodes.
Definition: cache.h:1196
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:539
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:668
int32_t Size(int32_t peekModes)
Gets the number of all entries cached across all nodes.
Definition: cache.h:1175
std::map< K, V > GetAll(const std::set< K > &keys)
Retrieves values mapped to the specified keys from cache.
Definition: cache.h:297
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:481
bool ContainsKey(const K &key, IgniteError &err)
Check if cache contains mapping for this key.
Definition: cache.h:149
void PutAll(const std::map< K, V > &vals, IgniteError &err)
Stores given key-value pairs in cache.
Definition: cache.h:396
V LocalPeek(const K &key, int32_t peekModes, IgniteError &err)
Peeks at cached value using optional set of peek modes.
Definition: cache.h:229
int32_t LocalSize(IgniteError &err)
Gets the number of all entries cached on this node.
Definition: cache.h:1101
Declares ignite::cache::query::SqlFieldsQuery class.
Ignite error information.
Definition: ignite_error.h:94
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:586
void LocalClearAll(const std::set< K > &keys)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:897
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:561
void Put(const K &key, const V &val, IgniteError &err)
Associates the specified value with the specified key in the cache.
Definition: cache.h:361
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:458
query::QueryCursor< K, V > Query(const query::ScanQuery &qry, IgniteError &err)
Perform scan query.
Definition: cache.h:1299
Apache Ignite API.
Definition: cache.h:43
query::QueryCursor< K, V > Query(const query::TextQuery &qry, IgniteError &err)
Perform text query.
Definition: cache.h:1264
Declares ignite::IgniteError class.
query::QueryFieldsCursor Query(const query::SqlFieldsQuery &qry, IgniteError &err)
Perform sql fields query.
Definition: cache.h:1334
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:641
query::QueryFieldsCursor Query(const query::SqlFieldsQuery &qry)
Perform sql fields query.
Definition: cache.h:1314
Query cursor class template.
Definition: query_cursor.h:54
static void ThrowIfNeeded(const IgniteError &err)
Throw an error if code is not IGNITE_SUCCESS.
Definition: ignite_error.cpp:27
void RemoveAll(IgniteError &err)
Removes all mappings from cache.
Definition: cache.h:1076
std::map< K, V > GetAll(const std::set< K > &keys, IgniteError &err)
Retrieves values mapped to the specified keys from cache.
Definition: cache.h:321
int32_t LocalSize()
Gets the number of all entries cached on this node.
Definition: cache.h:1088
bool Remove(const K &key, IgniteError &err)
Removes given key mapping from cache.
Definition: cache.h:967
void LocalClear(const K &key, IgniteError &err)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:879
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:1134