GridGain C++ Client Library
cache.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) GridGain Systems. All Rights Reserved.
3  * _________ _____ __________________ _____
4  * __ ____/___________(_)______ /__ ____/______ ____(_)_______
5  * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \
6  * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / /
7  * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/
8  */
9 
15 #ifndef _GRIDGAIN_CACHE
16 #define _GRIDGAIN_CACHE
17 
18 #include <map>
19 #include <set>
20 
21 #include <ignite/common/common.h>
22 #include <ignite/common/concurrent.h>
23 
31 #include "gridgain/impl/cache/cache_impl.h"
32 #include "gridgain/impl/operations.h"
33 #include "gridgain/grid_error.h"
34 
35 namespace gridgain
36 {
37  namespace cache
38  {
42  template<typename K, typename V>
43  class IGNITE_IMPORT_EXPORT Cache
44  {
45  public:
49  Cache(impl::cache::CacheImpl* impl) : impl(ignite::common::concurrent::SharedPointer<impl::cache::CacheImpl>(impl))
50  {
51  // No-op.
52  }
53 
57  const char* GetName() const
58  {
59  return impl.Get()->GetName();
60  }
61 
68  bool IsEmpty()
69  {
70  GridError err;
71 
72  bool res = IsEmpty(err);
73 
75 
76  return res;
77  }
78 
86  bool IsEmpty(GridError& err)
87  {
88  return Size(err) == 0;
89  }
90 
97  bool ContainsKey(const K& key)
98  {
99  GridError err;
100 
101  bool res = ContainsKey(key, err);
102 
104 
105  return res;
106  }
107 
115  bool ContainsKey(const K& key, GridError& err)
116  {
117  impl::In1Operation<K> op(&key);
118 
119  return impl.Get()->ContainsKey(op, &err);
120  }
121 
128  bool ContainsKeys(const std::set<K>& keys)
129  {
130  GridError err;
131 
132  bool res = ContainsKeys(keys, err);
133 
135 
136  return res;
137  }
138 
146  bool ContainsKeys(const std::set<K>& keys, GridError& err)
147  {
148  impl::InSetOperation<K> op(&keys);
149 
150  return impl.Get()->ContainsKeys(op, &err);
151  }
152 
164  V LocalPeek(const K& key, int32_t peekModes)
165  {
166  GridError err;
167 
168  V res = LocalPeek(key, peekModes, err);
169 
171 
172  return res;
173  }
174 
187  V LocalPeek(const K& key, int32_t peekModes, GridError& err)
188  {
189  impl::InCacheLocalPeekOperation<K> inOp(&key, peekModes);
190  impl::Out1Operation<V> outOp;
191 
192  impl.Get()->LocalPeek(inOp, outOp, peekModes, &err);
193 
194  return outOp.GetResult();
195  }
196 
207  V Get(const K& key)
208  {
209  GridError err;
210 
211  V res = Get(key, err);
212 
214 
215  return res;
216  }
217 
229  V Get(const K& key, GridError& err)
230  {
231  impl::In1Operation<K> inOp(&key);
232  impl::Out1Operation<V> outOp;
233 
234  impl.Get()->Get(inOp, outOp, &err);
235 
236  return outOp.GetResult();
237  }
238 
249  std::map<K, V> GetAll(const std::set<K>& keys)
250  {
251  GridError err;
252 
253  std::map<K, V> res = GetAll(keys, err);
254 
256 
257  return res;
258  }
259 
271  std::map<K, V> GetAll(const std::set<K>& keys, GridError& err)
272  {
273  impl::InSetOperation<K> inOp(&keys);
274  impl::OutMapOperation<K, V> outOp;
275 
276  impl.Get()->GetAll(inOp, outOp, &err);
277 
278  return outOp.GetResult();
279  }
280 
289  void Put(const K& key, const V& val)
290  {
291  GridError err;
292 
293  Put(key, val, err);
294 
296  }
297 
307  void Put(const K& key, const V& val, GridError& err)
308  {
309  impl::In2Operation<K, V> op(&key, &val);
310 
311  impl.Get()->Put(op, &err);
312  }
313 
321  void PutAll(const std::map<K, V>& vals)
322  {
323  GridError err;
324 
325  PutAll(vals, err);
326 
328  }
329 
338  void PutAll(const std::map<K, V>& vals, GridError& err)
339  {
340  impl::InMapOperation<K, V> op(&vals);
341 
342  impl.Get()->PutAll(op, &err);
343  }
344 
354  V GetAndPut(const K& key, const V& val)
355  {
356  GridError err;
357 
358  V res = GetAndPut(key, val, err);
359 
361 
362  return res;
363  }
364 
375  V GetAndPut(const K& key, const V& val, GridError& err)
376  {
377  impl::In2Operation<K, V> inOp(&key, &val);
378  impl::Out1Operation<V> outOp;
379 
380  impl.Get()->GetAndPut(inOp, outOp, &err);
381 
382  return outOp.GetResult();
383  }
384 
394  V GetAndReplace(const K& key, const V& val)
395  {
396  GridError err;
397 
398  V res = GetAndReplace(key, val, err);
399 
401 
402  return res;
403  }
404 
415  V GetAndReplace(const K& key, const V& val, GridError& err)
416  {
417  impl::In2Operation<K, V> inOp(&key, &val);
418  impl::Out1Operation<V> outOp;
419 
420  impl.Get()->GetAndReplace(inOp, outOp, &err);
421 
422  return outOp.GetResult();
423  }
424 
431  V GetAndRemove(const K& key)
432  {
433  GridError err;
434 
435  V res = GetAndRemove(key, err);
436 
438 
439  return res;
440  }
441 
449  V GetAndRemove(const K& key, GridError& err)
450  {
451  impl::In1Operation<K> inOp(&key);
452  impl::Out1Operation<V> outOp;
453 
454  impl.Get()->GetAndRemove(inOp, outOp, &err);
455 
456  return outOp.GetResult();
457  }
458 
467  bool PutIfAbsent(const K& key, const V& val)
468  {
469  GridError err;
470 
471  bool res = PutIfAbsent(key, val, err);
472 
474 
475  return res;
476  }
477 
487  bool PutIfAbsent(const K& key, const V& val, GridError& err)
488  {
489  impl::In2Operation<K, V> op(&key, &val);
490 
491  return impl.Get()->PutIfAbsent(op, &err);
492  }
493 
510  V GetAndPutIfAbsent(const K& key, const V& val)
511  {
512  GridError err;
513 
514  V res = GetAndPutIfAbsent(key, val, err);
515 
517 
518  return res;
519  }
520 
538  V GetAndPutIfAbsent(const K& key, const V& val, GridError& err)
539  {
540  impl::In2Operation<K, V> inOp(&key, &val);
541  impl::Out1Operation<V> outOp;
542 
543  impl.Get()->GetAndPutIfAbsent(inOp, outOp, &err);
544 
545  return outOp.GetResult();
546  }
547 
561  bool Replace(const K& key, const V& val)
562  {
563  GridError err;
564 
565  bool res = Replace(key, val, err);
566 
568 
569  return res;
570  }
571 
586  bool Replace(const K& key, const V& val, GridError& err)
587  {
588  impl::In2Operation<K, V> op(&key, &val);
589 
590  return impl.Get()->Replace(op, &err);
591  }
592 
603  bool Replace(const K& key, const V& oldVal, const V& newVal)
604  {
605  GridError err;
606 
607  bool res = Replace(key, oldVal, newVal, err);
608 
610 
611  return res;
612  }
613 
625  bool Replace(const K& key, const V& oldVal, const V& newVal, GridError& err)
626  {
627  impl::In3Operation<K, V, V> op(&key, &oldVal, &newVal);
628 
629  return impl.Get()->ReplaceIfEqual(op, &err);
630  }
631 
638  void LocalEvict(const std::set<K>& keys)
639  {
640  GridError err;
641 
642  LocalEvict(keys, err);
643 
645  }
646 
654  void LocalEvict(const std::set<K>& keys, GridError& err)
655  {
656  impl::InSetOperation<K> op(&keys);
657 
658  impl.Get()->LocalEvict(op, &err);
659  }
660 
664  void Clear()
665  {
666  GridError err;
667 
668  Clear(err);
669 
671  }
672 
678  void Clear(GridError& err)
679  {
680  impl.Get()->Clear(&err);
681  }
682 
689  void Clear(const K& key)
690  {
691  GridError err;
692 
693  Clear(key, err);
694 
696  }
697 
705  void Clear(const K& key, GridError& err)
706  {
707  impl::In1Operation<K> op(&key);
708 
709  impl.Get()->Clear(op, &err);
710  }
711 
718  void ClearAll(const std::set<K>& keys)
719  {
720  GridError err;
721 
722  ClearAll(keys, err);
723 
725  }
726 
734  void ClearAll(const std::set<K>& keys, GridError& err)
735  {
736  impl::InSetOperation<K> op(&keys);
737 
738  impl.Get()->ClearAll(op, &err);
739  }
740 
749  void LocalClear(const K& key)
750  {
751  GridError err;
752 
753  LocalClear(key, err);
754 
756  }
757 
767  void LocalClear(const K& key, GridError& err)
768  {
769  impl::In1Operation<K> op(&key);
770 
771  impl.Get()->LocalClear(op, &err);
772  }
773 
782  void LocalClearAll(const std::set<K>& keys)
783  {
784  GridError err;
785 
786  LocalClearAll(keys, err);
787 
789  }
790 
800  void LocalClearAll(const std::set<K>& keys, GridError& err)
801  {
802  impl::InSetOperation<K> op(&keys);
803 
804  impl.Get()->LocalClearAll(op, &err);
805  }
806 
820  bool Remove(const K& key)
821  {
822  GridError err;
823 
824  bool res = Remove(key, err);
825 
827 
828  return res;
829  }
830 
845  bool Remove(const K& key, GridError& err)
846  {
847  impl::In1Operation<K> op(&key);
848 
849  return impl.Get()->Remove(op, &err);
850  }
851 
861  bool Remove(const K& key, const V& val)
862  {
863  GridError err;
864 
865  bool res = Remove(key, val, err);
866 
868 
869  return res;
870  }
871 
882  bool Remove(const K& key, const V& val, GridError& err)
883  {
884  impl::In2Operation<K, V> op(&key, &val);
885 
886  return impl.Get()->RemoveIfEqual(op, &err);
887  }
888 
896  void RemoveAll(const std::set<K>& keys)
897  {
898  GridError err;
899 
900  RemoveAll(keys, err);
901 
903  }
904 
913  void RemoveAll(const std::set<K>& keys, GridError& err)
914  {
915  impl::InSetOperation<K> op(&keys);
916 
917  impl.Get()->RemoveAll(op, &err);
918  }
919 
927  void RemoveAll()
928  {
929  GridError err;
930 
931  RemoveAll(err);
932 
934  }
935 
943  void RemoveAll(GridError& err)
944  {
945  return impl.Get()->RemoveAll(&err);
946  }
947 
953  int32_t LocalSize()
954  {
955  return LocalSize(GG_PEEK_MODE_ALL);
956  }
957 
964  int32_t LocalSize(GridError& err)
965  {
966  return LocalSize(GG_PEEK_MODE_ALL, err);
967  }
968 
975  int32_t LocalSize(int32_t peekModes)
976  {
977  GridError err;
978 
979  int32_t res = LocalSize(peekModes, err);
980 
982 
983  return res;
984  }
985 
993  int32_t LocalSize(int32_t peekModes, GridError& err)
994  {
995  return impl.Get()->Size(peekModes, true, &err);
996  }
997 
1004  int32_t Size()
1005  {
1006  return Size(gridgain::cache::GG_PEEK_MODE_ALL);
1007  }
1008 
1016  int32_t Size(GridError& err)
1017  {
1018  return Size(gridgain::cache::GG_PEEK_MODE_ALL, err);
1019  }
1020 
1028  int32_t Size(int32_t peekModes)
1029  {
1030  GridError err;
1031 
1032  int32_t res = Size(peekModes, err);
1033 
1035 
1036  return res;
1037  }
1038 
1047  int32_t Size(int32_t peekModes, GridError& err)
1048  {
1049  return impl.Get()->Size(peekModes, false, &err);
1050  }
1051 
1059  {
1060  GridError err;
1061 
1062  query::QueryCursor<K, V> res = Query(qry, err);
1063 
1065 
1066  return res;
1067  }
1068 
1077  {
1078  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QuerySql(qry, &err);
1079 
1080  return query::QueryCursor<K, V>(cursorImpl);
1081  }
1082 
1090  {
1091  GridError err;
1092 
1093  query::QueryCursor<K, V> res = Query(qry, err);
1094 
1096 
1097  return res;
1098  }
1099 
1108  {
1109  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QueryText(qry, &err);
1110 
1111  return query::QueryCursor<K, V>(cursorImpl);
1112  }
1113 
1121  {
1122  GridError err;
1123 
1124  query::QueryCursor<K, V> res = Query(qry, err);
1125 
1127 
1128  return res;
1129  }
1130 
1139  {
1140  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QueryScan(qry, &err);
1141 
1142  return query::QueryCursor<K, V>(cursorImpl);
1143  }
1144 
1152  {
1153  GridError err;
1154 
1155  query::QueryFieldsCursor res = Query(qry, err);
1156 
1158 
1159  return res;
1160  }
1161 
1170  {
1171  impl::cache::query::QueryCursorImpl* cursorImpl = impl.Get()->QuerySqlFields(qry, &err);
1172 
1173  return query::QueryFieldsCursor(cursorImpl);
1174  }
1175 
1181  bool IsValid()
1182  {
1183  return impl.IsValid();
1184  }
1185 
1186  private:
1188  ignite::common::concurrent::SharedPointer<impl::cache::CacheImpl> impl;
1189  };
1190  }
1191 }
1192 
1193 #endif
Main entry point for all Data Grid APIs.
Definition: cache.h:43
void ClearAll(const std::set< K > &keys)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:718
void LocalEvict(const std::set< K > &keys)
Attempts to evict all entries associated with keys.
Definition: cache.h:638
void Put(const K &key, const V &val)
Associates the specified value with the specified key in the cache.
Definition: cache.h:289
bool Replace(const K &key, const V &oldVal, const V &newVal, GridError &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:625
query::QueryCursor< K, V > Query(const query::ScanQuery &qry)
Perform scan query.
Definition: cache.h:1120
int32_t LocalSize(int32_t peekModes, GridError &err)
Gets the number of all entries cached on this node.
Definition: cache.h:993
query::QueryFieldsCursor Query(const query::SqlFieldsQuery &qry)
Perform sql fields query.
Definition: cache.h:1151
Cache(impl::cache::CacheImpl *impl)
Constructor.
Definition: cache.h:49
Declares gridgain::cache::query::QueryCursor class template.
void PutAll(const std::map< K, V > &vals)
Stores given key-value pairs in cache.
Definition: cache.h:321
bool ContainsKeys(const std::set< K > &keys, GridError &err)
Check if cache contains mapping for these keys.
Definition: cache.h:146
void RemoveAll(GridError &err)
Removes all mappings from cache.
Definition: cache.h:943
Peeks into all available cache storages.
Definition: cache_peek_mode.h:30
bool IsValid()
Check if the instance is valid.
Definition: cache.h:1181
const char * GetName() const
Name of this cache (null for default cache).
Definition: cache.h:57
Query fields cursor.
Definition: query_fields_cursor.h:37
Declares gridgain::cache::query::QueryFieldsCursor class.
Sql query.
Definition: query_sql.h:34
int32_t Size()
Gets the number of all entries cached across all nodes.
Definition: cache.h:1004
Declares gridgain::cache::query::SqlQuery class.
int32_t LocalSize()
Gets the number of all entries cached on this node.
Definition: cache.h:953
void Clear(const K &key, GridError &err)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:705
Text query.
Definition: query_text.h:32
Declares gridgain::cache::CachePeekMode enum.
int32_t Size(int32_t peekModes)
Gets the number of all entries cached across all nodes.
Definition: cache.h:1028
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:467
V GetAndRemove(const K &key)
Atomically removes the entry for a key only if currently mapped to some value.
Definition: cache.h:431
void PutAll(const std::map< K, V > &vals, GridError &err)
Stores given key-value pairs in cache.
Definition: cache.h:338
V GetAndPut(const K &key, const V &val, GridError &err)
Associates the specified value with the specified key in this cache, returning an existing value if o...
Definition: cache.h:375
void Clear()
Clear cache.
Definition: cache.h:664
bool ContainsKeys(const std::set< K > &keys)
Check if cache contains mapping for these keys.
Definition: cache.h:128
void RemoveAll(const std::set< K > &keys, GridError &err)
Removes given key mappings from cache.
Definition: cache.h:913
V GetAndRemove(const K &key, GridError &err)
Atomically removes the entry for a key only if currently mapped to some value.
Definition: cache.h:449
Scab query.
Definition: query_scan.h:32
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:354
void RemoveAll()
Removes all mappings from cache.
Definition: cache.h:927
void ClearAll(const std::set< K > &keys, GridError &err)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:734
bool ContainsKey(const K &key, GridError &err)
Check if cache contains mapping for this key.
Definition: cache.h:115
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:603
Declares gridgain::GridError class.
V LocalPeek(const K &key, int32_t peekModes, GridError &err)
Peeks at cached value using optional set of peek modes.
Definition: cache.h:187
void Clear(GridError &err)
Clear cache.
Definition: cache.h:678
std::map< K, V > GetAll(const std::set< K > &keys)
Retrieves values mapped to the specified keys from cache.
Definition: cache.h:249
bool ContainsKey(const K &key)
Check if cache contains mapping for this key.
Definition: cache.h:97
Sql fields query.
Definition: query_sql_fields.h:34
V GetAndPutIfAbsent(const K &key, const V &val, GridError &err)
Stores given key-value pair in cache only if cache had no previous mapping for it.
Definition: cache.h:538
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:561
query::QueryCursor< K, V > Query(const query::SqlQuery &qry, GridError &err)
Perform SQL query.
Definition: cache.h:1076
void LocalClear(const K &key)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:749
void LocalEvict(const std::set< K > &keys, GridError &err)
Attempts to evict all entries associated with keys.
Definition: cache.h:654
std::map< K, V > GetAll(const std::set< K > &keys, GridError &err)
Retrieves values mapped to the specified keys from cache.
Definition: cache.h:271
Query cursor.
Definition: query_cursor.h:37
bool Replace(const K &key, const V &val, GridError &err)
Stores given key-value pair in cache only if there is a previous mapping for it.
Definition: cache.h:586
query::QueryCursor< K, V > Query(const query::ScanQuery &qry, GridError &err)
Perform scan query.
Definition: cache.h:1138
void LocalClear(const K &key, GridError &err)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:767
bool IsEmpty(GridError &err)
Checks whether this cache contains no key-value mappings.
Definition: cache.h:86
int32_t LocalSize(int32_t peekModes)
Gets the number of all entries cached on this node.
Definition: cache.h:975
V LocalPeek(const K &key, int32_t peekModes)
Peeks at cached value using optional set of peek modes.
Definition: cache.h:164
V Get(const K &key, GridError &err)
Retrieves value mapped to the specified key from cache.
Definition: cache.h:229
void RemoveAll(const std::set< K > &keys)
Removes given key mappings from cache.
Definition: cache.h:896
void LocalClearAll(const std::set< K > &keys)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:782
Declares gridgain::cache::query::SqlFieldsQuery class.
int32_t LocalSize(GridError &err)
Gets the number of all entries cached on this node.
Definition: cache.h:964
query::QueryCursor< K, V > Query(const query::SqlQuery &qry)
Perform SQL query.
Definition: cache.h:1058
bool Remove(const K &key, GridError &err)
Removes given key mapping from cache.
Definition: cache.h:845
query::QueryCursor< K, V > Query(const query::TextQuery &qry)
Perform text query.
Definition: cache.h:1089
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:510
Declares gridgain::cache::query::ScanQuery class.
query::QueryFieldsCursor Query(const query::SqlFieldsQuery &qry, GridError &err)
Perform sql fields query.
Definition: cache.h:1169
bool Remove(const K &key)
Removes given key mapping from cache.
Definition: cache.h:820
static void ThrowIfNeeded(GridError &err)
Throw an error if code is not GG_SUCCESS.
Definition: grid_error.cpp:22
void Clear(const K &key)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:689
bool PutIfAbsent(const K &key, const V &val, GridError &err)
Atomically associates the specified key with the given value if it is not already associated with a v...
Definition: cache.h:487
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:861
int32_t Size(int32_t peekModes, GridError &err)
Gets the number of all entries cached across all nodes.
Definition: cache.h:1047
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:394
GridGain API.
Definition: cache.h:35
Grid error information.
Definition: grid_error.h:70
void LocalClearAll(const std::set< K > &keys, GridError &err)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache.h:800
bool Remove(const K &key, const V &val, GridError &err)
Removes given key mapping from cache if one exists and value is equal to the passed in value...
Definition: cache.h:882
int32_t Size(GridError &err)
Gets the number of all entries cached across all nodes.
Definition: cache.h:1016
V Get(const K &key)
Retrieves value mapped to the specified key from cache.
Definition: cache.h:207
V GetAndReplace(const K &key, const V &val, GridError &err)
Atomically replaces the value for a given key if and only if there is a value currently mapped by the...
Definition: cache.h:415
query::QueryCursor< K, V > Query(const query::TextQuery &qry, GridError &err)
Perform text query.
Definition: cache.h:1107
void Put(const K &key, const V &val, GridError &err)
Associates the specified value with the specified key in the cache.
Definition: cache.h:307
Declares gridgain::cache::query::TextQuery class.
bool IsEmpty()
Checks whether this cache contains no key-value mappings.
Definition: cache.h:68