1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.kryo;
15
16 import ch.qos.logback.core.kryo.SerializerBase;
17 import com.esotericsoftware.kryo.kryo5.Kryo;
18 import com.esotericsoftware.kryo.kryo5.io.Input;
19 import com.esotericsoftware.kryo.kryo5.io.Output;
20 import com.esotericsoftware.kryo.kryo5.minlog.Log;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25
26
27
28
29
30
31
32
33
34
35
36
37 public class ClassNameSerializer extends SerializerBase<String> {
38
39
40
41
42
43
44
45
46
47
48
49
50
51 private final Map<String, Integer> prefixToId = new HashMap<>();
52 private final Map<Integer, String> idToPrefix = new HashMap<>();
53 private int nextPrefixId = 0;
54
55 public ClassNameSerializer(int capacity) {
56 super(BiMapType.DIRECT_MAP, capacity);
57 setAcceptsNull(false);
58 }
59
60 @Override
61 protected void writeNewEntry(Kryo kryo, Output output, String loggerName) {
62
63
64 if (loggerName == null) {
65 throw new IllegalArgumentException("Null logger names are not permitted");
66 }
67
68
69 String prefix = findLongestPrefix(loggerName);
70
71 if (prefix != null && prefix.length() > 4) {
72 Integer prefixId = prefixToId.get(prefix);
73
74 if (prefixId == null) {
75
76 prefixId = nextPrefixId++;
77 prefixToId.put(prefix, prefixId);
78 idToPrefix.put(prefixId, prefix);
79
80
81 output.writeByte((byte) 1);
82 output.writeVarInt(prefixId, true);
83 output.writeString(prefix);
84 String suffix = loggerName.substring(prefix.length());
85 output.writeString(suffix);
86 Log.debug("New prefix [" + prefix + "] with id " + prefixId + ", suffix: " + suffix);
87 } else {
88
89
90 output.writeByte((byte) 2);
91 output.writeVarInt(prefixId, true);
92 String suffix = loggerName.substring(prefix.length());
93 output.writeString(suffix);
94 Log.debug("Using prefix id " + prefixId + " [" + prefix + "], suffix: " + suffix);
95 }
96 } else {
97
98
99 output.writeByte((byte) 0);
100 output.writeString(loggerName);
101 Log.debug("Writing full logger name: " + loggerName);
102 }
103 }
104
105 @Override
106 protected String readNewEntry(Kryo kryo, Input input) {
107 byte mode = input.readByte();
108
109 switch (mode) {
110 case 0:
111 String fullName = input.readString();
112 Log.trace("Read full logger name: " + fullName);
113 return fullName;
114
115 case 1:
116 int newPrefixId = input.readVarInt(true);
117 String prefix = input.readString();
118 String suffix = input.readString();
119
120
121 prefixToId.put(prefix, newPrefixId);
122 idToPrefix.put(newPrefixId, prefix);
123
124 String reconstructed = prefix + suffix;
125 Log.debug("Read new prefix [" + prefix + "] id=" + newPrefixId + ", result: " + reconstructed);
126 return reconstructed;
127
128 case 2:
129 int prefixId = input.readVarInt(true);
130 String cachedPrefix = idToPrefix.get(prefixId);
131 String suffixPart = input.readString();
132
133 if (cachedPrefix == null) {
134 throw new IllegalStateException("Unknown prefix id: " + prefixId);
135 }
136
137 String result = cachedPrefix + suffixPart;
138 Log.debug("Read with cached prefix id=" + prefixId + " [" + cachedPrefix + "], result: " + result);
139 return result;
140
141 default:
142 throw new IllegalStateException("Unknown mode: " + mode);
143 }
144 }
145
146
147
148
149
150 private String findLongestPrefix(String loggerName) {
151
152
153
154
155
156
157
158
159 int lastDot = loggerName.lastIndexOf('.');
160 if (lastDot > 0) {
161 return loggerName.substring(0, lastDot + 1);
162 }
163
164 return null;
165 }
166 }