• 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/AbstractExternalCache.java
1
package com.alicp.jetcache.external;
2

3
import com.alicp.jetcache.AbstractCache;
4
import com.alicp.jetcache.CacheConfigException;
5
import com.alicp.jetcache.CacheException;
6
import com.alicp.jetcache.RefreshCache;
7
import com.alicp.jetcache.anno.KeyConvertor;
8

9
import java.io.IOException;
10

11
/**
12
 * Created on 2016/10/8.
13
 *
14
 * @author huangli
15
 */
16
public abstract class AbstractExternalCache<K, V> extends AbstractCache<K, V> {
17

18
    private ExternalCacheConfig<K, V> config;
19

20
    public AbstractExternalCache(ExternalCacheConfig<K, V> config) {
×
21
        this.config = config;
×
22
        checkConfig();
×
23
    }
×
24

25
    protected void checkConfig() {
26
        if (config.getValueEncoder() == null) {
×
27
            throw new CacheConfigException("no value encoder");
×
28
        }
29
        if (config.getValueDecoder() == null) {
×
30
            throw new CacheConfigException("no value decoder");
×
31
        }
32
        if (config.getKeyPrefix() == null) {
×
33
            throw new CacheConfigException("keyPrefix is required");
×
34
        }
35
    }
×
36

37
    public byte[] buildKey(K key) {
38
        try {
39
            Object newKey = key;
×
40
            if (config.getKeyConvertor() != null) {
×
41
                if (config.getKeyConvertor() instanceof KeyConvertor) {
×
42
                    if (!isPreservedKey(key)) {
×
43
                        // since 2.7.3 KeyConvertor extends Function<Object, Object>
44
                        newKey = config.getKeyConvertor().apply(key);
×
45
                    }
46
                } else {
47
                    // before 2.7.3, KeyConvertor is interface only place some constants.
48
                    // "key convertor" is Function<Object, Object> and can't process byte[] and String
49
                    if (key instanceof byte[]) {
×
50
                        newKey = key;
×
51
                    } else if (key instanceof String) {
×
52
                        newKey = key;
×
53
                    } else {
54
                        newKey = config.getKeyConvertor().apply(key);
×
55
                    }
56
                }
57
            }
58
            return ExternalKeyUtil.buildKeyAfterConvert(newKey, config.getKeyPrefix());
×
59
        } catch (IOException e) {
×
60
            throw new CacheException(e);
×
61
        }
62
    }
63

64
    private boolean isPreservedKey(Object key) {
65
        if (key instanceof byte[]) {
×
66
            byte[] keyBytes = (byte[]) key;
×
67
            return endWith(keyBytes, RefreshCache.LOCK_KEY_SUFFIX)
×
68
                    || endWith(keyBytes, RefreshCache.TIMESTAMP_KEY_SUFFIX);
×
69
        }
70
        return false;
×
71
    }
72

73
    private boolean endWith(byte[] key, byte[] suffix) {
74
        int len = suffix.length;
×
75
        if (key.length < len) {
×
76
            return false;
×
77
        }
78
        int startPos = key.length - len;
×
79
        for (int i = 0; i < len; i++) {
×
80
            if (key[startPos + i] != suffix[i]) {
×
81
                return false;
×
82
            }
83
        }
84
        return true;
×
85
    }
86

87
}
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