KryoPool.java
5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package org.onlab.util;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.lang3.tuple.Pair;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.Serializer;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.google.common.collect.ImmutableList;
// TODO Add tests for this class.
/**
* Pool of Kryo instances, with classes pre-registered.
*/
//@ThreadSafe
public final class KryoPool {
/**
* Default buffer size used for serialization.
*
* @see #serialize(Object)
*/
public static final int DEFAULT_BUFFER_SIZE = 1 * 1000 * 1000;
private final ConcurrentLinkedQueue<Kryo> pool = new ConcurrentLinkedQueue<>();
private final ImmutableList<Pair<Class<?>, Serializer<?>>> registeredTypes;
private final boolean registrationRequired;
/**
* KryoPool builder.
*/
//@NotThreadSafe
public static final class Builder {
private final List<Pair<Class<?>, Serializer<?>>> types = new ArrayList<>();
/**
* Builds a {@link KryoPool} instance.
*
* @return KryoPool
*/
public KryoPool build() {
return new KryoPool(types);
}
/**
* Registers classes to be serialized using Kryo default serializer.
*
* @param expectedTypes list of classes
* @return this
*/
public Builder register(final Class<?>... expectedTypes) {
for (Class<?> clazz : expectedTypes) {
types.add(Pair.<Class<?>, Serializer<?>>of(clazz, null));
}
return this;
}
/**
* Registers a class and it's serializer.
*
* @param clazz the class to register
* @param serializer serializer to use for the class
* @return this
*/
public Builder register(final Class<?> clazz, Serializer<?> serializer) {
types.add(Pair.<Class<?>, Serializer<?>>of(clazz, serializer));
return this;
}
/**
* Registers all the class registered to given KryoPool.
*
* @param pool KryoPool
* @return this
*/
public Builder register(final KryoPool pool) {
types.addAll(pool.registeredTypes);
return this;
}
}
/**
* Creates a new {@link KryoPool} builder.
*
* @return builder
*/
public static Builder newBuilder() {
return new Builder();
}
/**
* Creates a Kryo instance pool.
*
* @param registerdTypes types to register
*/
private KryoPool(final List<Pair<Class<?>, Serializer<?>>> registerdTypes) {
this.registeredTypes = ImmutableList.copyOf(registerdTypes);
// always true for now
this.registrationRequired = true;
}
/**
* Populates the Kryo pool.
*
* @param instances to add to the pool
* @return this
*/
public KryoPool populate(int instances) {
List<Kryo> kryos = new ArrayList<>(instances);
for (int i = 0; i < instances; ++i) {
kryos.add(newKryoInstance());
}
pool.addAll(kryos);
return this;
}
/**
* Gets a Kryo instance from the pool.
*
* @return Kryo instance
*/
public Kryo getKryo() {
Kryo kryo = pool.poll();
if (kryo == null) {
return newKryoInstance();
}
return kryo;
}
/**
* Returns a Kryo instance to the pool.
*
* @param kryo instance obtained from this pool.
*/
public void putKryo(Kryo kryo) {
if (kryo != null) {
pool.add(kryo);
}
}
/**
* Serializes given object to byte array using Kryo instance in pool.
* <p>
* Note: Serialized bytes must be smaller than {@link #DEFAULT_BUFFER_SIZE}.
*
* @param obj Object to serialize
* @return serialized bytes
*/
public byte[] serialize(final Object obj) {
return serialize(obj, DEFAULT_BUFFER_SIZE);
}
/**
* Serializes given object to byte array using Kryo instance in pool.
*
* @param obj Object to serialize
* @param bufferSize maximum size of serialized bytes
* @return serialized bytes
*/
public byte[] serialize(final Object obj, final int bufferSize) {
Output out = new Output(bufferSize);
Kryo kryo = getKryo();
try {
kryo.writeClassAndObject(out, obj);
return out.toBytes();
} finally {
putKryo(kryo);
}
}
/**
* Deserializes given byte array to Object using Kryo instance in pool.
*
* @param bytes serialized bytes
* @param <T> deserialized Object type
* @return deserialized Object
*/
public <T> T deserialize(final byte[] bytes) {
Input in = new Input(bytes);
Kryo kryo = getKryo();
try {
@SuppressWarnings("unchecked")
T obj = (T) kryo.readClassAndObject(in);
return obj;
} finally {
putKryo(kryo);
}
}
/**
* Creates a Kryo instance with {@link #registeredTypes} pre-registered.
*
* @return Kryo instance
*/
private Kryo newKryoInstance() {
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(registrationRequired);
for (Pair<Class<?>, Serializer<?>> registry : registeredTypes) {
if (registry.getRight() == null) {
kryo.register(registry.getLeft());
} else {
kryo.register(registry.getLeft(), registry.getRight());
}
}
return kryo;
}
}