| Apache Ignite C++

cache_client.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_THIN_CACHE_CACHE_CLIENT
24 #define _IGNITE_THIN_CACHE_CACHE_CLIENT
25
26 #include <ignite/common/concurrent.h>
27
28 #include <ignite/impl/thin/writable.h>
29 #include <ignite/impl/thin/writable_key.h>
30 #include <ignite/impl/thin/readable.h>
31
32 #include <ignite/impl/thin/cache/cache_client_proxy.h>
33
34 namespace ignite
35 {
36 namespace thin
37 {
38 namespace cache
39 {
55 template<typename K, typename V>
56 class CacheClient
57 {
58 friend class impl::thin::cache::CacheClientProxy;
59
60 public:
62 typedef K KeyType;
63
65 typedef V ValueType;
66
72 CacheClient(common::concurrent::SharedPointer<void> impl) :
73 proxy(impl)
74 {
75 // No-op.
76 }
77
81 CacheClient()
82 {
83 // No-op.
84 }
85
89 ~CacheClient()
90 {
91 // No-op.
92 }
93
100 void Put(const KeyType& key, const ValueType& value)
101 {
102 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
103 impl::thin::WritableImpl<ValueType> wrValue(value);
104
105 proxy.Put(wrKey, wrValue);
106 }
107
115 template<typename InIter>
116 void PutAll(InIter begin, InIter end)
117 {
118 impl::thin::WritableMapImpl<K, V, InIter> wrSeq(begin, end);
119
120 proxy.PutAll(wrSeq);
121 }
122
129 template<typename Map>
130 void PutAll(const Map& vals)
131 {
132 PutAll(vals.begin(), vals.end());
133 }
134
141 void Get(const KeyType& key, ValueType& value)
142 {
143 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
144 impl::thin::ReadableImpl<ValueType> rdValue(value);
145
146 proxy.Get(wrKey, rdValue);
147 }
148
155 ValueType Get(const KeyType& key)
156 {
157 ValueType value;
158
159 Get(key, value);
160
161 return value;
162 }
163
174 template<typename InIter, typename OutIter>
175 void GetAll(InIter begin, InIter end, OutIter dst)
176 {
177 impl::thin::WritableSetImpl<K, InIter> wrSeq(begin, end);
178 impl::thin::ReadableMapImpl<K, V, OutIter> rdSeq(dst);
179
180 proxy.GetAll(wrSeq, rdSeq);
181 }
182
192 template<typename Set, typename Map>
193 void GetAll(const Set& keys, Map& res)
194 {
195 return GetAll(keys.begin(), keys.end(), std::inserter(res, res.end()));
196 }
197
210 bool Replace(const K& key, const V& value)
211 {
212 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
213 impl::thin::WritableImpl<ValueType> wrValue(value);
214
215 return proxy.Replace(wrKey, wrValue);
216 }
217
227 bool Replace(const KeyType& key, const ValueType& oldVal, const ValueType& newVal)
228 {
229 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
230 impl::thin::WritableImpl<ValueType> wrOldVal(oldVal);
231 impl::thin::WritableImpl<ValueType> wrNewVal(newVal);
232
233 return proxy.Replace(wrKey, wrOldVal, wrNewVal);
234 }
235
242 bool ContainsKey(const KeyType& key)
243 {
244 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
245
246 return proxy.ContainsKey(wrKey);
247 }
248
255 template<typename Set>
256 bool ContainsKeys(const Set& keys)
257 {
258 return ContainsKeys(keys.begin(), keys.end());
259 }
260
268 template<typename InIter>
269 bool ContainsKeys(InIter begin, InIter end)
270 {
271 impl::thin::WritableSetImpl<K, InIter> wrSeq(begin, end);
272
273 return proxy.ContainsKeys(wrSeq);
274 }
275
285 int64_t GetSize(int32_t peekModes)
286 {
287 return proxy.GetSize(peekModes);
288 }
289
302 bool Remove(const KeyType& key)
303 {
304 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
305
306 return proxy.Remove(wrKey);
307 }
308
317 bool Remove(const KeyType& key, const ValueType& val)
318 {
319 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
320 impl::thin::WritableImpl<ValueType> wrVal(val);
321
322 return proxy.Remove(wrKey, wrVal);
323 }
324
331 template<typename Set>
332 void RemoveAll(const Set& keys)
333 {
334 RemoveAll(keys.begin(), keys.end());
335 }
336
344 template<typename InIter>
345 void RemoveAll(InIter begin, InIter end)
346 {
347 impl::thin::WritableSetImpl<K, InIter> wrSeq(begin, end);
348
349 proxy.RemoveAll(wrSeq);
350 }
351
357 void RemoveAll()
358 {
359 proxy.RemoveAll();
360 }
361
368 void Clear(const KeyType& key)
369 {
370 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
371
372 proxy.Clear(wrKey);
373 }
374
378 void Clear()
379 {
380 proxy.Clear();
381 }
382
389 template<typename Set>
390 void ClearAll(const Set& keys)
391 {
392 ClearAll(keys.begin(), keys.end());
393 }
394
402 template<typename InIter>
403 void ClearAll(InIter begin, InIter end)
404 {
405 impl::thin::WritableSetImpl<K, InIter> wrSeq(begin, end);
406
407 proxy.ClearAll(wrSeq);
408 }
409
419 void GetAndPut(const KeyType& key, const ValueType& valIn, ValueType& valOut)
420 {
421 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
422 impl::thin::WritableImpl<ValueType> wrValIn(valIn);
423 impl::thin::ReadableImpl<ValueType> rdValOut(valOut);
424
425 proxy.GetAndPut(wrKey, wrValIn, rdValOut);
426 }
427
437 ValueType GetAndPut(const KeyType& key, const ValueType& valIn)
438 {
439 ValueType valOut;
440
441 GetAndPut(key, valIn, valOut);
442
443 return valOut;
444 }
445
453 void GetAndRemove(const KeyType& key, ValueType& valOut)
454 {
455 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
456 impl::thin::ReadableImpl<ValueType> rdValOut(valOut);
457
458 proxy.GetAndRemove(wrKey, rdValOut);
459 }
460
468 ValueType GetAndRemove(const KeyType& key)
469 {
470 ValueType valOut;
471
472 GetAndRemove(key, valOut);
473
474 return valOut;
475 }
476
486 void GetAndReplace(const KeyType& key, const ValueType& valIn, ValueType& valOut)
487 {
488 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
489 impl::thin::WritableImpl<ValueType> wrValIn(valIn);
490 impl::thin::ReadableImpl<ValueType> rdValOut(valOut);
491
492 proxy.GetAndReplace(wrKey, wrValIn, rdValOut);
493 }
494
504 ValueType GetAndReplace(const KeyType& key, const ValueType& valIn)
505 {
506 ValueType valOut;
507
508 GetAndReplace(key, valIn, valOut);
509
510 return valOut;
511 }
512
521 bool PutIfAbsent(const KeyType& key, const ValueType& val)
522 {
523 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
524 impl::thin::WritableImpl<ValueType> wrValIn(val);
525
526 return proxy.PutIfAbsent(wrKey, wrValIn);
527 }
528
548 void GetAndPutIfAbsent(const KeyType& key, const ValueType& valIn, ValueType& valOut)
549 {
550 impl::thin::WritableKeyImpl<KeyType> wrKey(key);
551 impl::thin::WritableImpl<ValueType> wrValIn(valIn);
552 impl::thin::ReadableImpl<ValueType> rdValOut(valOut);
553
554 proxy.GetAndPutIfAbsent(wrKey, wrValIn, rdValOut);
555 }
556
576 ValueType GetAndPutIfAbsent(const KeyType& key, const ValueType& valIn)
577 {
578 ValueType valOut;
579
580 GetAndPutIfAbsent(key, valIn, valOut);
581
582 return valOut;
583 }
584
596 void RefreshAffinityMapping()
597 {
598 // No-op.
599 }
600
601 private:
603 impl::thin::cache::CacheClientProxy proxy;
604 };
605 }
606 }
607 }
608
609 #endif // _IGNITE_THIN_CACHE_CACHE_CLIENT
ignite::thin::cache::CacheClient::ValueType
V ValueType
Value type.
Definition: cache_client.h:65
ignite::thin::cache::CacheClient::GetAndRemove
void GetAndRemove(const KeyType &key, ValueType &valOut)
Atomically removes the entry for a key only if currently mapped to some value.
Definition: cache_client.h:453
ignite::thin::cache::CacheClient::GetAll
void GetAll(InIter begin, InIter end, OutIter dst)
Retrieves values mapped to the specified keys from cache.
Definition: cache_client.h:175
ignite::thin::cache::CacheClient::RemoveAll
void RemoveAll()
Removes all mappings from cache.
Definition: cache_client.h:357
ignite::thin::cache::CacheClient::CacheClient
CacheClient(common::concurrent::SharedPointer< void > impl)
Constructor.
Definition: cache_client.h:72
ignite::thin::cache::CacheClient::GetAndRemove
ValueType GetAndRemove(const KeyType &key)
Atomically removes the entry for a key only if currently mapped to some value.
Definition: cache_client.h:468
ignite::thin::cache::CacheClient::GetSize
int64_t GetSize(int32_t peekModes)
Gets the number of all entries cached across all nodes.
Definition: cache_client.h:285
ignite::thin::cache::CacheClient::RemoveAll
void RemoveAll(InIter begin, InIter end)
Removes given key mappings from cache.
Definition: cache_client.h:345
ignite::thin::cache::CacheClient::KeyType
K KeyType
Key type.
Definition: cache_client.h:62
ignite::thin::cache::CacheClient::GetAndPutIfAbsent
ValueType GetAndPutIfAbsent(const KeyType &key, const ValueType &valIn)
Stores given key-value pair in cache only if cache had no previous mapping for it.
Definition: cache_client.h:576
ignite::thin::cache::CacheClient::ContainsKeys
bool ContainsKeys(InIter begin, InIter end)
Check if cache contains mapping for these keys.
Definition: cache_client.h:269
ignite::thin::cache::CacheClient::GetAndReplace
ValueType GetAndReplace(const KeyType &key, const ValueType &valIn)
Atomically replaces the value for a given key if and only if there is a value currently mapped by the...
Definition: cache_client.h:504
ignite::thin::cache::CacheClient::~CacheClient
~CacheClient()
Destructor.
Definition: cache_client.h:89
ignite::thin::cache::CacheClient::Replace
bool Replace(const K &key, const V &value)
Stores given key-value pair in cache only if there is a previous mapping for it.
Definition: cache_client.h:210
ignite::thin::cache::CacheClient::Get
ValueType Get(const KeyType &key)
Get value from cache.
Definition: cache_client.h:155
ignite::thin::cache::CacheClient::Remove
bool Remove(const KeyType &key, const ValueType &val)
Removes given key mapping from cache if one exists and value is equal to the passed in value...
Definition: cache_client.h:317
ignite::thin::cache::CacheClient::GetAndPutIfAbsent
void GetAndPutIfAbsent(const KeyType &key, const ValueType &valIn, ValueType &valOut)
Stores given key-value pair in cache only if cache had no previous mapping for it.
Definition: cache_client.h:548
ignite::thin::cache::CacheClient::PutAll
void PutAll(const Map &vals)
Stores given key-value pairs in cache.
Definition: cache_client.h:130
ignite::thin::cache::CacheClient::PutAll
void PutAll(InIter begin, InIter end)
Stores given key-value pairs in cache.
Definition: cache_client.h:116
ignite::thin::cache::CacheClient::CacheClient
CacheClient()
Default constructor.
Definition: cache_client.h:81
ignite::thin::cache::CacheClient::ContainsKeys
bool ContainsKeys(const Set &keys)
Check if cache contains mapping for these keys.
Definition: cache_client.h:256
ignite::thin::cache::CacheClient::Clear
void Clear(const KeyType &key)
Clear entry from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache_client.h:368
ignite::thin::cache::CacheClient::Put
void Put(const KeyType &key, const ValueType &value)
Associate the specified value with the specified key in the cache.
Definition: cache_client.h:100
ignite::thin::cache::CacheClient::GetAndPut
void GetAndPut(const KeyType &key, const ValueType &valIn, ValueType &valOut)
Associates the specified value with the specified key in this cache, returning an existing value if o...
Definition: cache_client.h:419
ignite::thin::cache::CacheClient::Replace
bool Replace(const KeyType &key, const ValueType &oldVal, const ValueType &newVal)
Stores given key-value pair in cache only if the previous value is equal to the old value passed as a...
Definition: cache_client.h:227
ignite::thin::cache::CacheClient::RefreshAffinityMapping
void RefreshAffinityMapping()
Refresh affinity mapping.
Definition: cache_client.h:596
ignite::thin::cache::CacheClient::GetAll
void GetAll(const Set &keys, Map &res)
Retrieves values mapped to the specified keys from cache.
Definition: cache_client.h:193
ignite::thin::cache::CacheClient
Cache client class template.
Definition: cache_client.h:56
ignite::thin::cache::CacheClient::Remove
bool Remove(const KeyType &key)
Removes given key mapping from cache.
Definition: cache_client.h:302
ignite::thin::cache::CacheClient::RemoveAll
void RemoveAll(const Set &keys)
Removes given key mappings from cache.
Definition: cache_client.h:332
ignite::thin::cache::CacheClient::ClearAll
void ClearAll(InIter begin, InIter end)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache_client.h:403
ignite
Apache Ignite API.
Definition: cache.h:48
ignite::thin::cache::CacheClient::GetAndReplace
void GetAndReplace(const KeyType &key, const ValueType &valIn, ValueType &valOut)
Atomically replaces the value for a given key if and only if there is a value currently mapped by the...
Definition: cache_client.h:486
ignite::thin::cache::CacheClient::ContainsKey
bool ContainsKey(const KeyType &key)
Check if the cache contains a value for the specified key.
Definition: cache_client.h:242
ignite::thin::cache::CacheClient::ClearAll
void ClearAll(const Set &keys)
Clear entries from the cache and swap storage, without notifying listeners or CacheWriters.
Definition: cache_client.h:390
ignite::thin::cache::CacheClient::PutIfAbsent
bool PutIfAbsent(const KeyType &key, const ValueType &val)
Atomically associates the specified key with the given value if it is not already associated with a v...
Definition: cache_client.h:521
ignite::thin::cache::CacheClient::Clear
void Clear()
Clear cache.
Definition: cache_client.h:378
ignite::thin::cache::CacheClient::Get
void Get(const KeyType &key, ValueType &value)
Get value from the cache.
Definition: cache_client.h:141
ignite::thin::cache::CacheClient::GetAndPut
ValueType GetAndPut(const KeyType &key, const ValueType &valIn)
Associates the specified value with the specified key in this cache, returning an existing value if o...
Definition: cache_client.h:437