/
RemoteCacheImpl.java
697 lines (633 loc) · 25.4 KB
/
RemoteCacheImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package org.infinispan.client.hotrod.impl;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.infinispan.client.hotrod.Flag;
import org.infinispan.client.hotrod.MetadataValue;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.ServerStatistics;
import org.infinispan.client.hotrod.Version;
import org.infinispan.client.hotrod.VersionedValue;
import org.infinispan.client.hotrod.event.ClientListenerNotifier;
import org.infinispan.client.hotrod.exceptions.HotRodClientException;
import org.infinispan.client.hotrod.exceptions.RemoteCacheManagerNotStartedException;
import org.infinispan.client.hotrod.impl.operations.*;
import org.infinispan.client.hotrod.impl.iteration.RemoteCloseableIterator;
import org.infinispan.client.hotrod.logging.Log;
import org.infinispan.client.hotrod.logging.LogFactory;
import org.infinispan.client.hotrod.marshall.MarshallerUtil;
import org.infinispan.commons.marshall.Marshaller;
import org.infinispan.commons.util.CloseableIterator;
import org.infinispan.commons.util.concurrent.NotifyingFuture;
import org.infinispan.commons.util.concurrent.NotifyingFutureImpl;
/**
* @author Mircea.Markus@jboss.com
* @since 4.1
*/
public class RemoteCacheImpl<K, V> extends RemoteCacheSupport<K, V> {
private static final Log log = LogFactory.getLog(RemoteCacheImpl.class, Log.class);
private Marshaller marshaller;
private final String name;
private final RemoteCacheManager remoteCacheManager;
private volatile ExecutorService executorService;
private OperationsFactory operationsFactory;
private int estimateKeySize;
private int estimateValueSize;
public RemoteCacheImpl(RemoteCacheManager rcm, String name) {
if (log.isTraceEnabled()) {
log.tracef("Creating remote cache: %s", name);
}
this.name = name;
this.remoteCacheManager = rcm;
}
public void init(Marshaller marshaller, ExecutorService executorService, OperationsFactory operationsFactory, int estimateKeySize, int estimateValueSize) {
this.marshaller = marshaller;
this.executorService = executorService;
this.operationsFactory = operationsFactory;
this.estimateKeySize = estimateKeySize;
this.estimateValueSize = estimateValueSize;
}
public OperationsFactory getOperationsFactory() {
return operationsFactory;
}
@Override
public RemoteCacheManager getRemoteCacheManager() {
return remoteCacheManager;
}
@Override
public boolean removeWithVersion(K key, long version) {
assertRemoteCacheManagerIsStarted();
RemoveIfUnmodifiedOperation op = operationsFactory.newRemoveIfUnmodifiedOperation(obj2bytes(key, true), version);
VersionedOperationResponse response = op.execute();
return response.getCode().isUpdated();
}
@Override
public NotifyingFuture<Boolean> removeWithVersionAsync(final K key, final long version) {
assertRemoteCacheManagerIsStarted();
final NotifyingFutureImpl<Boolean> result = new NotifyingFutureImpl<Boolean>();
Future<Boolean> future = executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
try {
boolean removed = removeWithVersion(key, version);
try {
result.notifyDone(removed);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
return removed;
} catch (Exception e) {
try {
result.notifyException(e);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
throw e;
}
}
});
result.setFuture(future);
return result;
}
@Override
public boolean replaceWithVersion(K key, V newValue, long version, int lifespanSeconds, int maxIdleTimeSeconds) {
return replaceWithVersion(key, newValue, version, lifespanSeconds, TimeUnit.SECONDS, maxIdleTimeSeconds, TimeUnit.SECONDS);
}
@Override
public boolean replaceWithVersion(K key, V newValue, long version, long lifespan, TimeUnit lifespanTimeUnit, long maxIdle, TimeUnit maxIdleTimeUnit) {
assertRemoteCacheManagerIsStarted();
ReplaceIfUnmodifiedOperation op = operationsFactory.newReplaceIfUnmodifiedOperation(obj2bytes(key, true), obj2bytes(newValue, false), lifespan, lifespanTimeUnit, maxIdle, maxIdleTimeUnit, version);
VersionedOperationResponse response = op.execute();
return response.getCode().isUpdated();
}
@Override
public NotifyingFuture<Boolean> replaceWithVersionAsync(final K key, final V newValue, final long version, final int lifespanSeconds, final int maxIdleSeconds) {
assertRemoteCacheManagerIsStarted();
final NotifyingFutureImpl<Boolean> result = new NotifyingFutureImpl<Boolean>();
Future<Boolean> future = executorService.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
try {
boolean removed = replaceWithVersion(key, newValue, version, lifespanSeconds, maxIdleSeconds);
try {
result.notifyDone(removed);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
return removed;
} catch (Exception e) {
try {
result.notifyException(e);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
throw e;
}
}
});
result.setFuture(future);
return result;
}
@Override
public CloseableIterator<Entry<Object, Object>> retrieveEntries(String filterConverterFactory, Set<Integer> segments, int batchSize) {
assertRemoteCacheManagerIsStarted();
RemoteCloseableIterator remoteCloseableIterator = new RemoteCloseableIterator(operationsFactory, filterConverterFactory, segments, batchSize, marshaller);
remoteCloseableIterator.start();
return remoteCloseableIterator;
}
@Override
public CloseableIterator<Entry<Object, Object>> retrieveEntries(String filterConverterFactory, int batchSize) {
return retrieveEntries(filterConverterFactory, null, batchSize);
}
@Override
public VersionedValue<V> getVersioned(K key) {
assertRemoteCacheManagerIsStarted();
GetWithVersionOperation op = operationsFactory.newGetWithVersionOperation(obj2bytes(key, true));
VersionedValue<byte[]> value = op.execute();
return binary2VersionedValue(value);
}
@Override
public MetadataValue<V> getWithMetadata(K key) {
assertRemoteCacheManagerIsStarted();
GetWithMetadataOperation op = operationsFactory.newGetWithMetadataOperation(obj2bytes(key, true));
MetadataValue<byte[]> value = op.execute();
return binary2MetadataValue(value);
}
@Override
public void putAll(Map<? extends K, ? extends V> map, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) {
assertRemoteCacheManagerIsStarted();
applyDefaultExpirationFlags(lifespan, maxIdleTime);
if (log.isTraceEnabled()) {
log.tracef("About to putAll entries (%s) lifespan:%d (%s), maxIdle:%d (%s)", map, lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit);
}
Map<byte[], byte[]> byteMap = new HashMap<>();
for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
byteMap.put(obj2bytes(entry.getKey(), true), obj2bytes(entry.getValue(), false));
}
PutAllOperation op = operationsFactory.newPutAllOperation(byteMap, lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit);
op.execute();
}
@Override
public NotifyingFuture<Void> putAllAsync(final Map<? extends K, ? extends V> data, final long lifespan, final TimeUnit lifespanUnit, final long maxIdle, final TimeUnit maxIdleUnit) {
assertRemoteCacheManagerIsStarted();
final NotifyingFutureImpl<Void> result = new NotifyingFutureImpl<Void>();
Future<Void> future = executorService.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
putAll(data, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
try {
result.notifyDone(null);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
return null;
} catch (Exception e) {
try {
result.notifyException(e);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
throw e;
}
}
});
result.setFuture(future);
return result;
}
@Override
public int size() {
assertRemoteCacheManagerIsStarted();
SizeOperation op = operationsFactory.newSizeOperation();
return op.execute();
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public ServerStatistics stats() {
assertRemoteCacheManagerIsStarted();
StatsOperation op = operationsFactory.newStatsOperation();
Map<String, String> statsMap = op.execute();
ServerStatisticsImpl stats = new ServerStatisticsImpl();
for (Map.Entry<String, String> entry : statsMap.entrySet()) {
stats.addStats(entry.getKey(), entry.getValue());
}
return stats;
}
@Override
public V put(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) {
assertRemoteCacheManagerIsStarted();
applyDefaultExpirationFlags(lifespan, maxIdleTime);
if (log.isTraceEnabled()) {
log.tracef("About to add (K,V): (%s, %s) lifespan:%d, maxIdle:%d", key, value, lifespan, maxIdleTime);
}
PutOperation op = operationsFactory.newPutKeyValueOperation(obj2bytes(key, true), obj2bytes(value, false), lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit);
byte[] result = op.execute();
return MarshallerUtil.bytes2obj(marshaller, result);
}
@Override
public V putIfAbsent(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) {
assertRemoteCacheManagerIsStarted();
applyDefaultExpirationFlags(lifespan, maxIdleTime);
PutIfAbsentOperation op = operationsFactory.newPutIfAbsentOperation(obj2bytes(key, true), obj2bytes(value, false), lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit);
byte[] bytes = op.execute();
return MarshallerUtil.bytes2obj(marshaller, bytes);
}
@Override
public V replace(K key, V value, long lifespan, TimeUnit lifespanUnit, long maxIdleTime, TimeUnit maxIdleTimeUnit) {
assertRemoteCacheManagerIsStarted();
applyDefaultExpirationFlags(lifespan, maxIdleTime);
ReplaceOperation op = operationsFactory.newReplaceOperation(obj2bytes(key, true), obj2bytes(value, false), lifespan, lifespanUnit, maxIdleTime, maxIdleTimeUnit);
byte[] bytes = op.execute();
return MarshallerUtil.bytes2obj(marshaller, bytes);
}
@Override
public NotifyingFuture<V> putAsync(final K key, final V value, final long lifespan, final TimeUnit lifespanUnit, final long maxIdle, final TimeUnit maxIdleUnit) {
assertRemoteCacheManagerIsStarted();
final NotifyingFutureImpl<V> result = new NotifyingFutureImpl<V>();
Future<V> future = executorService.submit(new Callable<V>() {
@Override
public V call() throws Exception {
try {
V prevValue = put(key, value, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
try {
result.notifyDone(prevValue);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
return prevValue;
} catch (Exception e) {
try {
result.notifyException(e);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
throw e;
}
}
});
result.setFuture(future);
return result;
}
@Override
public NotifyingFuture<Void> clearAsync() {
assertRemoteCacheManagerIsStarted();
final NotifyingFutureImpl<Void> result = new NotifyingFutureImpl<Void>();
Future<Void> future = executorService.submit(new Callable<Void>() {
@Override
public Void call() throws Exception {
try {
clear();
try {
result.notifyDone(null);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
return null;
} catch (Exception e) {
try {
result.notifyException(e);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
throw e;
}
}
});
result.setFuture(future);
return result;
}
@Override
public NotifyingFuture<V> putIfAbsentAsync(final K key,final V value,final long lifespan,final TimeUnit lifespanUnit,final long maxIdle,final TimeUnit maxIdleUnit) {
assertRemoteCacheManagerIsStarted();
final NotifyingFutureImpl<V> result = new NotifyingFutureImpl<V>();
Future<V> future = executorService.submit(new Callable<V>() {
@Override
public V call() throws Exception {
try {
V prevValue = putIfAbsent(key, value, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
try {
result.notifyDone(prevValue);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
return prevValue;
} catch (Exception e) {
try {
result.notifyException(e);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
throw e;
}
}
});
result.setFuture(future);
return result;
}
@Override
public NotifyingFuture<V> removeAsync(final Object key) {
assertRemoteCacheManagerIsStarted();
final NotifyingFutureImpl<V> result = new NotifyingFutureImpl<V>();
Future<V> future = executorService.submit(new Callable<V>() {
@Override
public V call() throws Exception {
try {
V toReturn = remove(key);
try {
result.notifyDone(toReturn);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
return toReturn;
} catch (Exception e) {
try {
result.notifyException(e);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
throw e;
}
}
});
result.setFuture(future);
return result;
}
@Override
public NotifyingFuture<V> replaceAsync(final K key,final V value,final long lifespan,final TimeUnit lifespanUnit,final long maxIdle,final TimeUnit maxIdleUnit) {
assertRemoteCacheManagerIsStarted();
final NotifyingFutureImpl<V> result = new NotifyingFutureImpl<V>();
Future<V> future = executorService.submit(new Callable<V>() {
@Override
public V call() throws Exception {
try {
V old = replace(key, value, lifespan, lifespanUnit, maxIdle, maxIdleUnit);
try {
result.notifyDone(old);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
return old;
} catch (Exception e) {
try {
result.notifyException(e);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
throw e;
}
}
});
result.setFuture(future);
return result;
}
@Override
public boolean containsKey(Object key) {
assertRemoteCacheManagerIsStarted();
ContainsKeyOperation op = operationsFactory.newContainsKeyOperation(obj2bytes(key, true));
return op.execute();
}
@Override
public V get(Object key) {
assertRemoteCacheManagerIsStarted();
byte[] keyBytes = obj2bytes(key, true);
GetOperation gco = operationsFactory.newGetKeyOperation(keyBytes);
byte[] bytes = gco.execute();
V result = MarshallerUtil.bytes2obj(marshaller, bytes);
if (log.isTraceEnabled()) {
log.tracef("For key(%s) returning %s", key, result);
}
return result;
}
@Override
public Map<K, V> getAll(Set<? extends K> keys) {
assertRemoteCacheManagerIsStarted();
if (log.isTraceEnabled()) {
log.tracef("About to getAll entries (%s)", keys);
}
Set<byte[]> byteKeys = new HashSet<>(keys.size());
for (K key : keys) {
byteKeys.add(obj2bytes(key, true));
}
GetAllOperation op = operationsFactory.newGetAllOperation(byteKeys);
Map<byte[], byte[]> result = op.execute();
Map<K,V> toReturn = new HashMap<K,V>();
for (Map.Entry<byte[], byte[]> entry : result.entrySet()) {
V value = MarshallerUtil.bytes2obj(marshaller, entry.getValue());
K key = MarshallerUtil.bytes2obj(marshaller, entry.getKey());
toReturn.put(key, value);
}
return Collections.unmodifiableMap(toReturn);
}
@Override
public Map<K, V> getBulk() {
return getBulk(0);
}
@Override
public Map<K, V> getBulk(int size) {
assertRemoteCacheManagerIsStarted();
BulkGetOperation op = operationsFactory.newBulkGetOperation(size);
Map<byte[], byte[]> result = op.execute();
Map<K,V> toReturn = new HashMap<K,V>();
for (Map.Entry<byte[], byte[]> entry : result.entrySet()) {
V value = MarshallerUtil.bytes2obj(marshaller, entry.getValue());
K key = MarshallerUtil.bytes2obj(marshaller, entry.getKey());
toReturn.put(key, value);
}
return Collections.unmodifiableMap(toReturn);
}
@Override
public V remove(Object key) {
assertRemoteCacheManagerIsStarted();
RemoveOperation removeOperation = operationsFactory.newRemoveOperation(obj2bytes(key, true));
byte[] existingValue = removeOperation.execute();
// TODO: It sucks that you need the prev value to see if it works...
// We need to find a better API for RemoteCache...
return MarshallerUtil.bytes2obj(marshaller, existingValue);
}
@Override
public void clear() {
assertRemoteCacheManagerIsStarted();
ClearOperation op = operationsFactory.newClearOperation() ;
op.execute();
}
@Override
public void start() {
if (log.isDebugEnabled()) {
log.debugf("Start called, nothing to do here(%s)", getName());
}
}
@Override
public void stop() {
if (log.isDebugEnabled()) {
log.debugf("Stop called, nothing to do here(%s)", getName());
}
}
@Override
public String getName() {
return name;
}
@Override
public String getVersion() {
return RemoteCacheImpl.class.getPackage().getImplementationVersion();
}
@Override
public String getProtocolVersion() {
return Version.getProtocolVersion();
}
@Override
public void addClientListener(Object listener) {
assertRemoteCacheManagerIsStarted();
AddClientListenerOperation op = operationsFactory.newAddClientListenerOperation(listener);
op.execute();
}
@Override
public void addClientListener(Object listener, Object[] filterFactoryParams, Object[] converterFactoryParams) {
assertRemoteCacheManagerIsStarted();
byte[][] marshalledFilterParams = marshallParams(filterFactoryParams);
byte[][] marshalledConverterParams = marshallParams(converterFactoryParams);
AddClientListenerOperation op = operationsFactory.newAddClientListenerOperation(
listener, marshalledFilterParams, marshalledConverterParams);
op.execute();
}
private byte[][] marshallParams(Object[] params) {
if (params == null)
return new byte[0][];
byte[][] marshalledParams = new byte[params.length][];
for (int i = 0; i < marshalledParams.length; i++) {
byte[] bytes = obj2bytes(params[i], true);// should be small
marshalledParams[i] = bytes;
}
return marshalledParams;
}
@Override
public void removeClientListener(Object listener) {
assertRemoteCacheManagerIsStarted();
RemoveClientListenerOperation op = operationsFactory.newRemoveClientListenerOperation(listener);
op.execute();
}
@Override
public Set<Object> getListeners() {
ClientListenerNotifier listenerNotifier = operationsFactory.getListenerNotifier();
return listenerNotifier.getListeners(operationsFactory.getCacheName());
}
@Override
public RemoteCache<K, V> withFlags(Flag... flags) {
operationsFactory.setFlags(flags);
return this;
}
@Override
public NotifyingFuture<V> getAsync(final K key) {
assertRemoteCacheManagerIsStarted();
final NotifyingFutureImpl<V> result = new NotifyingFutureImpl<V>();
Future<V> future = executorService.submit(new Callable<V>() {
@Override
public V call() throws Exception {
try {
V toReturn = get(key);
try {
result.notifyDone(toReturn);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
return toReturn;
} catch (Exception e) {
try {
result.notifyException(e);
} catch (Throwable t) {
log.trace("Error when notifying", t);
}
throw e;
}
}
});
result.setFuture(future);
return result;
}
public PingOperation.PingResult ping() {
return operationsFactory.newFaultTolerantPingOperation().execute();
}
private byte[] obj2bytes(Object o, boolean isKey) {
try {
return marshaller.objectToByteBuffer(o, isKey ? estimateKeySize : estimateValueSize);
} catch (IOException ioe) {
throw new HotRodClientException(
"Unable to marshall object of type [" + o.getClass().getName() + "]", ioe);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
return null;
}
}
private VersionedValue<V> binary2VersionedValue(VersionedValue<byte[]> value) {
if (value == null)
return null;
V valueObj = MarshallerUtil.bytes2obj(marshaller, value.getValue());
return new VersionedValueImpl<V>(value.getVersion(), valueObj);
}
private MetadataValue<V> binary2MetadataValue(MetadataValue<byte[]> value) {
if (value == null)
return null;
V valueObj = MarshallerUtil.bytes2obj(marshaller, value.getValue());
return new MetadataValueImpl<V>(value.getCreated(), value.getLifespan(), value.getLastUsed(), value.getMaxIdle(), value.getVersion(), valueObj);
}
private void assertRemoteCacheManagerIsStarted() {
if (!remoteCacheManager.isStarted()) {
String message = "Cannot perform operations on a cache associated with an unstarted RemoteCacheManager. Use RemoteCacheManager.start before using the remote cache.";
if (log.isInfoEnabled()) {
log.unstartedRemoteCacheManager();
}
throw new RemoteCacheManagerNotStartedException(message);
}
}
@Override
protected void set(K key, V value) {
// no need to optimize the put operation: all invocations are already non-return by default,
// see org.infinispan.client.hotrod.Flag.FORCE_RETURN_VALUE
// Warning: never invoke put(K,V) in this scope or we'll get a stackoverflow.
put(key, value, defaultLifespan, MILLISECONDS, defaultMaxIdleTime, MILLISECONDS);
}
private void applyDefaultExpirationFlags(long lifespan, long maxIdle) {
if (lifespan == 0) {
operationsFactory.addFlags(Flag.DEFAULT_LIFESPAN);
}
if (maxIdle == 0) {
operationsFactory.addFlags(Flag.DEFAULT_MAXIDLE);
}
}
@Override
public Set<K> keySet() {
assertRemoteCacheManagerIsStarted();
// Use default scope
BulkGetKeysOperation op = operationsFactory.newBulkGetKeysOperation(0);
Set<byte[]> result = op.execute();
Set<K> toReturn = new HashSet<K>();
for (byte[] keyBytes : result) {
K key = MarshallerUtil.bytes2obj(marshaller, keyBytes);
toReturn.add(key);
}
return Collections.unmodifiableSet(toReturn);
}
@Override
public <T> T execute(String taskName, Map<String, ?> params) {
assertRemoteCacheManagerIsStarted();
Map<String, byte[]> marshalledParams = new HashMap<>();
if (params != null) {
for(java.util.Map.Entry<String, ?> entry : params.entrySet()) {
marshalledParams.put(entry.getKey(), obj2bytes(entry.getValue(), false));
}
}
ExecuteOperation op = operationsFactory.newExecuteOperation(taskName, marshalledParams);
return MarshallerUtil.bytes2obj(marshaller, op.execute());
}
}