1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.kryo;
15
16 import com.esotericsoftware.kryo.kryo5.Kryo;
17 import com.esotericsoftware.kryo.kryo5.KryoException;
18 import com.esotericsoftware.kryo.kryo5.Serializer;
19 import com.esotericsoftware.kryo.kryo5.io.Input;
20 import com.esotericsoftware.kryo.kryo5.io.Output;
21 import com.esotericsoftware.kryo.kryo5.minlog.Log;
22
23 import static ch.qos.logback.core.kryo.KryoCoreConstants.ENTRY_HEADER_CRC3_POLY;
24 import static ch.qos.logback.core.kryo.KryoCoreConstants.ENTRY_HEADER_CRC_BITS;
25 import static ch.qos.logback.core.kryo.KryoCoreConstants.ENTRY_HEADER_CRC_MASK;
26 import static ch.qos.logback.core.kryo.KryoCoreConstants.ENTRY_HEADER_DATA_BITS;
27 import static ch.qos.logback.core.kryo.KryoCoreConstants.ENTRY_HEADER_DATA_MASK;
28 import static ch.qos.logback.core.kryo.KryoCoreConstants.ENTRY_HEADER_ID_MASK;
29 import static ch.qos.logback.core.kryo.KryoCoreConstants.ENTRY_HEADER_NEW_FLAG;
30 import static ch.qos.logback.core.kryo.KryoCoreConstants.MAX_SERIALIZER_ID;
31 import static ch.qos.logback.core.kryo.KryoCoreConstants.UNREGISTERED_ID;
32
33
34
35
36
37
38
39
40
41
42
43
44 public abstract class SerializerBase<V> extends Serializer<V> {
45
46 public enum BiMapType {
47 NONE,
48 DIRECT_MAP,
49 FREQUENCY_GATED,
50
51 IDENTITY
52 }
53
54
55 final int capacity;
56 final BidirectionalObjectToIdMap<V> bimap;
57
58 public SerializerBase(int capacity) {
59
60 this(BiMapType.DIRECT_MAP, capacity);
61 if(capacity < 1024) {
62 System.out.println("WARNING: capacity less than 1024");
63 }
64 }
65
66 public SerializerBase(BiMapType biMapType, int capacity) {
67 this.capacity = capacity;
68
69 switch (biMapType) {
70 case NONE:
71 this.bimap = null;
72 break;
73 case DIRECT_MAP:
74 this.bimap = new BidirectionalObjectToIdMapDefault<>(capacity);
75 break;
76 case FREQUENCY_GATED:
77 this.bimap = new BidirectionalObjectToIdMapDefault(capacity);
78 break;
79 case IDENTITY:
80 this.bimap = new BidirectionalObjectToIdMapIdentity<>(capacity);
81 break;
82 default:
83 throw new IllegalArgumentException("Unsupported BiMapType: " + biMapType);
84 }
85 Log.debug(this.getClass().getSimpleName() + " bimap:"+biMapType+" capacity: " + capacity);
86 }
87
88
89 @Override
90 public void write(Kryo kryo, Output output, V value) {
91
92
93 if (bimap == null) {
94 writeNewEntry(kryo, output, value);
95 return;
96 }
97
98 Integer existingId = this.bimap.getIdByValue(value);
99
100 if (existingId == null) {
101 final int newId = this.bimap.insertNewEntryOnWrite(value);
102 writeEntryHeader(output, true, newId);
103
104 writeNewEntry(kryo, output, value);
105 } else {
106
107 writeEntryHeader(output, false, existingId);
108 }
109
110 }
111
112
113
114
115
116
117
118
119 abstract protected void writeNewEntry(Kryo kryo, Output output, V value);
120
121 @Override
122 public V read(Kryo kryo, Input input, Class<? extends V> type) {
123
124 if (bimap == null) {
125 return readNewEntry(kryo, input);
126 }
127
128 int data13 = readEntryHeaderData(input);
129 boolean isNewEntry = (data13 & ENTRY_HEADER_NEW_FLAG) != 0;
130 int id = decodeHeaderId(data13);
131
132 if (isNewEntry) {
133 V deserialized = readNewEntry(kryo, input);
134 if (id != UNREGISTERED_ID) {
135 V existingVal = bimap.insertNewEntryOnRead(id, deserialized);
136 if (existingVal != null) {
137 Log.warn("SerializerBase.read returned non-null existingVal for value id: " + id);
138 }
139 }
140 Log.debug("NEW entry for id: " + id + ", value: " + deserialized);
141 return deserialized;
142 } else {
143 V value = bimap.getValueById(id);
144 Log.debug("Cached entry for id: " + id + ", value: " + value);
145 return value;
146 }
147 }
148
149 abstract protected V readNewEntry(Kryo kryo, Input input);
150
151
152
153
154
155 static void writeEntryHeader(Output output, boolean newEntry, int id) {
156 int idBits;
157 if (id == UNREGISTERED_ID) {
158 if (!newEntry) {
159 throw new IllegalArgumentException("UNREGISTERED_ID is only valid for NEW_ENTRY");
160 }
161 idBits = 0;
162 } else {
163 if (id < 1 || id > MAX_SERIALIZER_ID) {
164 throw new IllegalStateException(
165 "Serializer id out of range: " + id + " (allowed 1.." + MAX_SERIALIZER_ID + ")");
166 }
167 idBits = id;
168 }
169 int data13 = (newEntry ? ENTRY_HEADER_NEW_FLAG : 0) | (idBits & ENTRY_HEADER_ID_MASK);
170 int crc = crc3(data13);
171 int packed = (data13 << ENTRY_HEADER_CRC_BITS) | crc;
172 output.writeShort(packed);
173 }
174
175
176
177
178
179
180 static int readEntryHeaderData(Input input) {
181 int packed = input.readShort() & 0xFFFF;
182 int data13 = (packed >>> ENTRY_HEADER_CRC_BITS) & ENTRY_HEADER_DATA_MASK;
183 int crc = packed & ENTRY_HEADER_CRC_MASK;
184 int expected = crc3(data13);
185 if (crc != expected) {
186 throw new KryoException(
187 "SerializerBase entry header CRC mismatch: got " + crc + ", expected " + expected
188 + " (data=0x" + Integer.toHexString(data13) + ")");
189 }
190 return data13;
191 }
192
193
194
195
196
197 static int decodeHeaderId(int data13) {
198 int idBits = data13 & ENTRY_HEADER_ID_MASK;
199 boolean isNew = (data13 & ENTRY_HEADER_NEW_FLAG) != 0;
200 if (isNew && idBits == 0) {
201 return UNREGISTERED_ID;
202 }
203 return idBits;
204 }
205
206
207
208
209
210
211
212 static int crc3(int data) {
213 int v = (data & ENTRY_HEADER_DATA_MASK) << ENTRY_HEADER_CRC_BITS;
214 for (int i = ENTRY_HEADER_DATA_BITS + ENTRY_HEADER_CRC_BITS - 1; i >= ENTRY_HEADER_CRC_BITS; i--) {
215 if (((v >> i) & 1) != 0) {
216 v ^= ENTRY_HEADER_CRC3_POLY << (i - ENTRY_HEADER_CRC_BITS);
217 }
218 }
219 return v & ENTRY_HEADER_CRC_MASK;
220 }
221 }