1 package com.atlassian.vcache.internal.legacy;
2
3 import com.atlassian.cache.Cache;
4 import com.atlassian.marshalling.api.MarshallingPair;
5 import com.atlassian.vcache.ExternalCacheException;
6 import com.atlassian.vcache.PutPolicy;
7 import com.atlassian.vcache.internal.core.cas.IdentifiedData;
8 import com.atlassian.vcache.internal.core.cas.IdentifiedUtils;
9
10 import java.util.Map;
11 import java.util.Objects;
12 import java.util.Optional;
13 import java.util.Set;
14 import java.util.stream.Collectors;
15 import java.util.stream.StreamSupport;
16
17
18
19
20
21
22 class LegacyUtils {
23
24 static boolean directPut(
25 String externalKey,
26 IdentifiedData identifiedData,
27 PutPolicy policy,
28 Cache<String, IdentifiedData> delegate,
29 boolean avoidCasOps) {
30
31 final PutPolicy convertedPolicy = avoidCasOps ? PutPolicy.PUT_ALWAYS : policy;
32
33 switch (convertedPolicy) {
34 case ADD_ONLY:
35 return delegate.putIfAbsent(externalKey, identifiedData) == null;
36
37 case PUT_ALWAYS:
38 delegate.put(externalKey, identifiedData);
39 return true;
40
41 case REPLACE_ONLY:
42 final IdentifiedData existingData = delegate.get(externalKey);
43 return (existingData != null) && delegate.replace(externalKey, existingData, identifiedData);
44
45 default:
46 throw new IllegalArgumentException("Unknown put policy: " + convertedPolicy);
47 }
48 }
49
50 static ExternalCacheException mapException(Exception ex) {
51 return new ExternalCacheException(ExternalCacheException.Reason.UNCLASSIFIED_FAILURE, ex);
52 }
53
54 static <V> Map<String, Optional<V>> directGetBulk(
55 Set<String> externalKeys, Cache<String, IdentifiedData> delegate, Optional<MarshallingPair<V>> valueMarshalling) {
56 return StreamSupport.stream(externalKeys.spliterator(), false)
57 .distinct()
58 .collect(Collectors.toMap(
59 Objects::requireNonNull,
60 k -> IdentifiedUtils.unmarshall(delegate.get(k), valueMarshalling)));
61 }
62 }