• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #405

16 Apr 2024 05:58AM UTC coverage: 0.0% (-88.9%) from 88.866%
#405

push

areyouok
add encoding to fix coverage report

0 of 5353 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/jetcache-core/src/main/java/com/alicp/jetcache/external/MockRemoteCache.java
1
/**
2
 * Created on  13-09-22 16:54
3
 */
4
package com.alicp.jetcache.external;
5

6
import com.alicp.jetcache.*;
7
import com.alicp.jetcache.embedded.LinkedHashMapCacheBuilder;
8
import com.alicp.jetcache.external.AbstractExternalCache;
9
import com.alicp.jetcache.external.ExternalCacheConfig;
10

11
import java.lang.reflect.Method;
12
import java.nio.ByteBuffer;
13
import java.util.*;
14
import java.util.concurrent.TimeUnit;
15
import java.util.stream.Collectors;
16

17
/**
18
 * @author huangli
19
 */
20
public class MockRemoteCache<K, V> extends AbstractExternalCache<K, V> {
21
    private Cache<ByteBuffer, byte[]> cache;
22
    private ExternalCacheConfig<K, V> config;
23

24
    public MockRemoteCache(MockRemoteCacheConfig<K, V> config) {
25
        super(config);
×
26
        this.config = config;
×
27
        cache = LinkedHashMapCacheBuilder.createLinkedHashMapCacheBuilder()
×
28
                .limit(config.getLimit())
×
29
                .expireAfterWrite(config.getExpireAfterWriteInMillis(), TimeUnit.MILLISECONDS)
×
30
                .buildCache();
×
31
    }
×
32

33
    @Override
34
    public CacheConfig<K, V> config() {
35
        return config;
×
36
    }
37

38
    private ByteBuffer genKey(K key) {
39
        return ByteBuffer.wrap(buildKey(key));
×
40
    }
41

42
    //-------------------------------
43

44

45
    @Override
46
    public <T> T unwrap(Class<T> clazz) {
47
        return cache.unwrap(clazz);
×
48
    }
49

50

51
    private static Method getHolder;
52

53
    static {
54
        try {
55
            getHolder = CacheGetResult.class.getDeclaredMethod("getHolder");
×
56
            getHolder.setAccessible(true);
×
57
        } catch (NoSuchMethodException e) {
×
58
            throw new CacheException(e);
×
59
        }
×
60
    }
×
61

62
    private CacheGetResult convertCacheGetResult(CacheGetResult originResult) {
63
        try {
64
            CacheValueHolder originHolder = (CacheValueHolder) getHolder.invoke(originResult);
×
65
            LinkedList<CacheValueHolder> list = new LinkedList<>();
×
66
            while (originHolder != null) {
×
67
                CacheValueHolder h = new CacheValueHolder();
×
68
                if (list.size() > 0) {
×
69
                    list.getLast().setValue(h);
×
70
                }
71
                list.add(h);
×
72
                h.setAccessTime(originHolder.getAccessTime());
×
73
                h.setExpireTime(originHolder.getExpireTime());
×
74

75
                Object v = originHolder.getValue();
×
76
                if (v != null && !(v instanceof CacheValueHolder)) {
×
77
                    h.setValue(config.getValueDecoder().apply((byte[]) v));
×
78
                    break;
×
79
                } else if (originHolder.getValue() == null) {
×
80
                    originHolder = (CacheValueHolder) originHolder.getValue();
×
81
                }
82
            }
×
83
            return new CacheGetResult(originResult.getResultCode(), originResult.getMessage(), list.peekFirst());
×
84
        } catch (Exception e) {
×
85
            throw new CacheException(e);
×
86
        }
87
    }
88

89
    public CacheValueHolder getHolder(K key) {
90
        try {
91
            CacheGetResult<V> r = GET(key);
×
92
            return (CacheValueHolder) getHolder.invoke(r);
×
93
        } catch (Exception e) {
×
94
            throw new CacheException(e);
×
95
        }
96
    }
97

98
    @Override
99
    protected CacheGetResult<V> do_GET(K key) {
100
        CacheGetResult r = cache.GET(genKey(key));
×
101
        if (r.isSuccess()) {
×
102
            r = convertCacheGetResult(r);
×
103
        }
104
        return r;
×
105
    }
106

107
    @Override
108
    protected MultiGetResult<K, V> do_GET_ALL(Set<? extends K> keys) {
109
        ArrayList<K> keyList = new ArrayList<>(keys.size());
×
110
        ArrayList<ByteBuffer> newKeyList = new ArrayList<>(keys.size());
×
111
        keys.stream().forEach((k) -> {
×
112
            ByteBuffer newKey = genKey(k);
×
113
            keyList.add(k);
×
114
            newKeyList.add(newKey);
×
115
        });
×
116
        MultiGetResult<ByteBuffer, byte[]> result = cache.GET_ALL(new HashSet(newKeyList));
×
117
        Map<ByteBuffer, CacheGetResult<byte[]>> resultMap = result.getValues();
×
118
        if (resultMap != null) {
×
119
            Map<K, CacheGetResult<V>> returnMap = new HashMap<>();
×
120
            for (int i = 0; i < keyList.size(); i++) {
×
121
                K key = keyList.get(i);
×
122
                ByteBuffer newKey = newKeyList.get(i);
×
123
                CacheGetResult r = resultMap.get(newKey);
×
124
                if (r.getValue() != null) {
×
125
                    r = convertCacheGetResult(r);
×
126
                }
127
                returnMap.put(key, r);
×
128
            }
129
            result = new MultiGetResult<ByteBuffer, byte[]>(result.getResultCode(), null, (Map) returnMap);
×
130
        }
131
        return (MultiGetResult) result;
×
132
    }
133

134
    @Override
135
    protected CacheResult do_PUT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
136
        return cache.PUT(genKey(key), config.getValueEncoder().apply(value), expireAfterWrite, timeUnit);
×
137
    }
138

139
    @Override
140
    protected CacheResult do_PUT_ALL(Map<? extends K, ? extends V> map, long expireAfterWrite, TimeUnit timeUnit) {
141
        Map<ByteBuffer, byte[]> newMap = new HashMap<>();
×
142
        map.entrySet().forEach((e) -> newMap.put(genKey(e.getKey()), config.getValueEncoder().apply(e.getValue())));
×
143
        return cache.PUT_ALL(newMap, expireAfterWrite, timeUnit);
×
144
    }
145

146
    @Override
147
    protected CacheResult do_REMOVE(K key) {
148
        return cache.REMOVE(genKey(key));
×
149
    }
150

151
    @Override
152
    protected CacheResult do_REMOVE_ALL(Set<? extends K> keys) {
153
        return cache.REMOVE_ALL(keys.stream().map((k) -> genKey(k)).collect(Collectors.toSet()));
×
154
    }
155

156
    @Override
157
    protected CacheResult do_PUT_IF_ABSENT(K key, V value, long expireAfterWrite, TimeUnit timeUnit) {
158
        return cache.PUT_IF_ABSENT(genKey(key), config.getValueEncoder().apply(value), expireAfterWrite, timeUnit);
×
159
    }
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc