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

mybatis / redis-cache / #330

pending completion
#330

Pull #187

github

web-flow
Merge b3aa7e284 into 359e3da7b
Pull Request #187: Update dependency org.mybatis:mybatis-parent to v37

163 of 199 relevant lines covered (81.91%)

0.82 hits per line

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

81.48
/src/main/java/org/mybatis/caches/redis/KryoSerializer.java
1
/*
2
 *    Copyright 2015-2022 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.caches.redis;
17

18
import com.esotericsoftware.kryo.Kryo;
19
import com.esotericsoftware.kryo.io.Input;
20
import com.esotericsoftware.kryo.io.Output;
21

22
import java.util.Arrays;
23
import java.util.Set;
24
import java.util.concurrent.ConcurrentHashMap;
25

26
/**
27
 * SerializeUtil with Kryo, which is faster and more space consuming.
28
 *
29
 * @author Lei Jiang(ladd.cn@gmail.com)
30
 */
31
public enum KryoSerializer implements Serializer {
1✔
32
  // Enum singleton, which is preferred approach since Java 1.5
33
  INSTANCE;
1✔
34

35
  /**
36
   * kryo is thread-unsafe, use ThreadLocal.
37
   */
38
  private ThreadLocal<Kryo> kryos = ThreadLocal.withInitial(() -> new Kryo());
1✔
39

40
  /**
41
   * Classes which can not resolved by default kryo serializer, which occurs very
42
   * rare(https://github.com/EsotericSoftware/kryo#using-standard-java-serialization) For these classes, we will use
43
   * fallbackSerializer(use JDKSerializer now) to resolve.
44
   */
45
  private Set<Class<?>> unnormalClassSet;
46

47
  /**
48
   * Hash codes of unnormal bytes which can not resolved by default kryo serializer, which will be resolved by
49
   * fallbackSerializer
50
   */
51
  private Set<Integer> unnormalBytesHashCodeSet;
52
  private Serializer fallbackSerializer;
53

54
  private KryoSerializer() {
1✔
55
    unnormalClassSet = ConcurrentHashMap.newKeySet();
1✔
56
    unnormalBytesHashCodeSet = ConcurrentHashMap.newKeySet();
1✔
57
    fallbackSerializer = JDKSerializer.INSTANCE;// use JDKSerializer as fallback
1✔
58
  }
1✔
59

60
  public byte[] serialize(Object object) {
61
    if (!unnormalClassSet.contains(object.getClass())) {
1✔
62
      /**
63
       * In the following cases: 1. This class occurs for the first time. 2. This class have occurred and can be
64
       * resolved by default kryo serializer
65
       */
66
      try {
67
        Output output = new Output(200, -1);
1✔
68
        kryos.get().writeClassAndObject(output, object);
1✔
69
        return output.toBytes();
1✔
70
      } catch (Exception e) {
×
71
        // For unnormal class occurred for the first time, exception will be thrown
72
        unnormalClassSet.add(object.getClass());
×
73
        return fallbackSerializer.serialize(object);// use fallback Serializer to resolve
×
74
      }
75
    } else {
76
      // For unnormal class
77
      return fallbackSerializer.serialize(object);
×
78
    }
79
  }
80

81
  public Object unserialize(byte[] bytes) {
82
    if (bytes == null) {
1✔
83
      return null;
1✔
84
    }
85
    int hashCode = Arrays.hashCode(bytes);
1✔
86
    if (!unnormalBytesHashCodeSet.contains(hashCode)) {
1✔
87
      /**
88
       * In the following cases: 1. This bytes occurs for the first time. 2. This bytes have occurred and can be
89
       * resolved by default kryo serializer
90
       */
91
      try {
92
        Input input = new Input();
1✔
93
        input.setBuffer(bytes);
1✔
94
        return kryos.get().readClassAndObject(input);
1✔
95
      } catch (Exception e) {
1✔
96
        // For unnormal bytes occurred for the first time, exception will be thrown
97
        unnormalBytesHashCodeSet.add(hashCode);
1✔
98
        return fallbackSerializer.unserialize(bytes);// use fallback Serializer to resolve
1✔
99
      }
100
    } else {
101
      // For unnormal bytes
102
      return fallbackSerializer.unserialize(bytes);
×
103
    }
104
  }
105

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