Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://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. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 16 | package org.onlab.util; |
| 17 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 18 | import com.esotericsoftware.kryo.Kryo; |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 19 | import com.esotericsoftware.kryo.Registration; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 20 | import com.esotericsoftware.kryo.Serializer; |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 21 | import com.esotericsoftware.kryo.io.ByteBufferInput; |
| 22 | import com.esotericsoftware.kryo.io.ByteBufferOutput; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 23 | import com.esotericsoftware.kryo.io.Input; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 24 | import com.esotericsoftware.kryo.pool.KryoCallback; |
Yuta HIGUCHI | 633cf88 | 2014-10-20 09:10:28 -0700 | [diff] [blame] | 25 | import com.esotericsoftware.kryo.pool.KryoFactory; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 26 | import com.esotericsoftware.kryo.pool.KryoPool; |
| 27 | import com.google.common.base.MoreObjects; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 28 | import com.google.common.collect.ImmutableList; |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 29 | import org.apache.commons.lang3.tuple.Pair; |
| 30 | import org.objenesis.strategy.StdInstantiatorStrategy; |
| 31 | import org.slf4j.Logger; |
| 32 | |
| 33 | import java.io.InputStream; |
| 34 | import java.io.OutputStream; |
| 35 | import java.nio.ByteBuffer; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
| 38 | |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 40 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Pool of Kryo instances, with classes pre-registered. |
| 43 | */ |
| 44 | //@ThreadSafe |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 45 | public final class KryoNamespace implements KryoFactory, KryoPool { |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Default buffer size used for serialization. |
| 49 | * |
| 50 | * @see #serialize(Object) |
| 51 | */ |
Yuta HIGUCHI | 3878205 | 2014-11-09 23:51:58 -0800 | [diff] [blame] | 52 | public static final int DEFAULT_BUFFER_SIZE = 4096; |
Yuta HIGUCHI | d2a3882 | 2014-11-06 19:05:04 -0800 | [diff] [blame] | 53 | public static final int MAX_BUFFER_SIZE = 100 * 1000 * 1000; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 54 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 55 | /** |
| 56 | * ID to use if this KryoNamespace does not define registration id. |
| 57 | */ |
| 58 | public static final int FLOATING_ID = -1; |
| 59 | |
| 60 | /** |
| 61 | * Smallest ID free to use for user defined registrations. |
| 62 | */ |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 63 | public static final int INITIAL_ID = 16; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 64 | |
HIGUCHI Yuta | b49b007 | 2016-02-22 22:50:45 -0800 | [diff] [blame] | 65 | private static final Logger log = getLogger(KryoNamespace.class); |
| 66 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 67 | |
| 68 | private final KryoPool pool = new KryoPool.Builder(this) |
| 69 | .softReferences() |
| 70 | .build(); |
| 71 | |
| 72 | private final ImmutableList<RegistrationBlock> registeredBlocks; |
| 73 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 74 | private final boolean registrationRequired; |
| 75 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 76 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 77 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 78 | * KryoNamespace builder. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 79 | */ |
| 80 | //@NotThreadSafe |
| 81 | public static final class Builder { |
| 82 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 83 | private int blockHeadId = INITIAL_ID; |
| 84 | private List<Pair<Class<?>, Serializer<?>>> types = new ArrayList<>(); |
| 85 | private List<RegistrationBlock> blocks = new ArrayList<>(); |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 86 | private boolean registrationRequired = true; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 89 | * Builds a {@link KryoNamespace} instance. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 90 | * |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 91 | * @return KryoNamespace |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 92 | */ |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 93 | public KryoNamespace build() { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 94 | if (!types.isEmpty()) { |
| 95 | blocks.add(new RegistrationBlock(this.blockHeadId, types)); |
| 96 | } |
| 97 | return new KryoNamespace(blocks, registrationRequired).populate(1); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets the next Kryo registration Id for following register entries. |
| 102 | * |
| 103 | * @param id Kryo registration Id |
| 104 | * @return this |
| 105 | * |
| 106 | * @see Kryo#register(Class, Serializer, int) |
| 107 | */ |
| 108 | public Builder nextId(final int id) { |
| 109 | if (!types.isEmpty()) { |
HIGUCHI Yuta | b49b007 | 2016-02-22 22:50:45 -0800 | [diff] [blame] | 110 | if (id != FLOATING_ID && id < blockHeadId + types.size()) { |
| 111 | |
| 112 | log.warn("requested nextId {} could potentially overlap" + |
| 113 | "with existing registrations {}+{} ", |
| 114 | id, blockHeadId, types.size()); |
| 115 | } |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 116 | blocks.add(new RegistrationBlock(this.blockHeadId, types)); |
| 117 | types = new ArrayList<>(); |
| 118 | } |
| 119 | this.blockHeadId = id; |
| 120 | return this; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Registers classes to be serialized using Kryo default serializer. |
| 125 | * |
| 126 | * @param expectedTypes list of classes |
| 127 | * @return this |
| 128 | */ |
| 129 | public Builder register(final Class<?>... expectedTypes) { |
| 130 | for (Class<?> clazz : expectedTypes) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 131 | types.add(Pair.of(clazz, null)); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 132 | } |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Registers a class and it's serializer. |
| 138 | * |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 139 | * @param classes list of classes to register |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 140 | * @param serializer serializer to use for the class |
| 141 | * @return this |
| 142 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 143 | public Builder register(Serializer<?> serializer, final Class<?>... classes) { |
| 144 | for (Class<?> clazz : classes) { |
| 145 | types.add(Pair.of(clazz, serializer)); |
| 146 | } |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | private Builder register(RegistrationBlock block) { |
| 151 | if (block.begin() != FLOATING_ID) { |
| 152 | // flush pending types |
| 153 | nextId(block.begin()); |
| 154 | blocks.add(block); |
| 155 | nextId(block.begin() + block.types().size()); |
| 156 | } else { |
| 157 | // flush pending types |
| 158 | final int addedBlockBegin = blockHeadId + types.size(); |
| 159 | nextId(addedBlockBegin); |
| 160 | blocks.add(new RegistrationBlock(addedBlockBegin, block.types())); |
| 161 | nextId(addedBlockBegin + block.types().size()); |
| 162 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 163 | return this; |
| 164 | } |
| 165 | |
| 166 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 167 | * Registers all the class registered to given KryoNamespace. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 168 | * |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 169 | * @param ns KryoNamespace |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 170 | * @return this |
| 171 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 172 | public Builder register(final KryoNamespace ns) { |
| 173 | for (RegistrationBlock block : ns.registeredBlocks) { |
| 174 | this.register(block); |
| 175 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 176 | return this; |
| 177 | } |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 178 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 179 | /** |
| 180 | * Sets the registrationRequired flag. |
| 181 | * |
| 182 | * @param registrationRequired Kryo's registrationRequired flag |
| 183 | * @return this |
| 184 | * |
| 185 | * @see Kryo#setRegistrationRequired(boolean) |
| 186 | */ |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 187 | public Builder setRegistrationRequired(boolean registrationRequired) { |
| 188 | this.registrationRequired = registrationRequired; |
| 189 | return this; |
| 190 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 194 | * Creates a new {@link KryoNamespace} builder. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 195 | * |
| 196 | * @return builder |
| 197 | */ |
| 198 | public static Builder newBuilder() { |
| 199 | return new Builder(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Creates a Kryo instance pool. |
| 204 | * |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 205 | * @param registeredTypes types to register |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 206 | * @param registrationRequired |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 207 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 208 | private KryoNamespace(final List<RegistrationBlock> registeredTypes, boolean registrationRequired) { |
| 209 | this.registeredBlocks = ImmutableList.copyOf(registeredTypes); |
Yuta HIGUCHI | 2befc66 | 2014-10-30 15:57:49 -0700 | [diff] [blame] | 210 | this.registrationRequired = registrationRequired; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Populates the Kryo pool. |
| 215 | * |
| 216 | * @param instances to add to the pool |
| 217 | * @return this |
| 218 | */ |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 219 | public KryoNamespace populate(int instances) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 220 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 221 | for (int i = 0; i < instances; ++i) { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 222 | release(create()); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 223 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 224 | return this; |
| 225 | } |
| 226 | |
| 227 | /** |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 228 | * Serializes given object to byte array using Kryo instance in pool. |
| 229 | * <p> |
Yuta HIGUCHI | 3878205 | 2014-11-09 23:51:58 -0800 | [diff] [blame] | 230 | * Note: Serialized bytes must be smaller than {@link #MAX_BUFFER_SIZE}. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 231 | * |
| 232 | * @param obj Object to serialize |
| 233 | * @return serialized bytes |
| 234 | */ |
| 235 | public byte[] serialize(final Object obj) { |
| 236 | return serialize(obj, DEFAULT_BUFFER_SIZE); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Serializes given object to byte array using Kryo instance in pool. |
| 241 | * |
| 242 | * @param obj Object to serialize |
| 243 | * @param bufferSize maximum size of serialized bytes |
| 244 | * @return serialized bytes |
| 245 | */ |
| 246 | public byte[] serialize(final Object obj, final int bufferSize) { |
Yuta HIGUCHI | d2a3882 | 2014-11-06 19:05:04 -0800 | [diff] [blame] | 247 | ByteBufferOutput out = new ByteBufferOutput(bufferSize, MAX_BUFFER_SIZE); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 248 | try { |
Madan Jampani | 22fa5cb | 2015-04-13 15:53:44 -0700 | [diff] [blame] | 249 | Kryo kryo = borrow(); |
| 250 | try { |
| 251 | kryo.writeClassAndObject(out, obj); |
| 252 | out.flush(); |
| 253 | return out.toBytes(); |
| 254 | } finally { |
| 255 | release(kryo); |
| 256 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 257 | } finally { |
Madan Jampani | 22fa5cb | 2015-04-13 15:53:44 -0700 | [diff] [blame] | 258 | out.release(); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 263 | * Serializes given object to byte buffer using Kryo instance in pool. |
| 264 | * |
| 265 | * @param obj Object to serialize |
| 266 | * @param buffer to write to |
| 267 | */ |
| 268 | public void serialize(final Object obj, final ByteBuffer buffer) { |
| 269 | ByteBufferOutput out = new ByteBufferOutput(buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 270 | Kryo kryo = borrow(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 271 | try { |
| 272 | kryo.writeClassAndObject(out, obj); |
Yuta HIGUCHI | cac919c | 2014-10-20 22:17:20 -0700 | [diff] [blame] | 273 | out.flush(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 274 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 275 | release(kryo); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Serializes given object to OutputStream using Kryo instance in pool. |
| 281 | * |
| 282 | * @param obj Object to serialize |
| 283 | * @param stream to write to |
| 284 | */ |
| 285 | public void serialize(final Object obj, final OutputStream stream) { |
| 286 | serialize(obj, stream, DEFAULT_BUFFER_SIZE); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Serializes given object to OutputStream using Kryo instance in pool. |
| 291 | * |
| 292 | * @param obj Object to serialize |
| 293 | * @param stream to write to |
| 294 | * @param bufferSize size of the buffer in front of the stream |
| 295 | */ |
| 296 | public void serialize(final Object obj, final OutputStream stream, final int bufferSize) { |
| 297 | ByteBufferOutput out = new ByteBufferOutput(stream, bufferSize); |
| 298 | Kryo kryo = borrow(); |
| 299 | try { |
| 300 | kryo.writeClassAndObject(out, obj); |
| 301 | out.flush(); |
| 302 | } finally { |
| 303 | release(kryo); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 308 | * Deserializes given byte array to Object using Kryo instance in pool. |
| 309 | * |
| 310 | * @param bytes serialized bytes |
| 311 | * @param <T> deserialized Object type |
| 312 | * @return deserialized Object |
| 313 | */ |
| 314 | public <T> T deserialize(final byte[] bytes) { |
| 315 | Input in = new Input(bytes); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 316 | Kryo kryo = borrow(); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 317 | try { |
| 318 | @SuppressWarnings("unchecked") |
| 319 | T obj = (T) kryo.readClassAndObject(in); |
| 320 | return obj; |
| 321 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 322 | release(kryo); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 326 | /** |
| 327 | * Deserializes given byte buffer to Object using Kryo instance in pool. |
| 328 | * |
| 329 | * @param buffer input with serialized bytes |
| 330 | * @param <T> deserialized Object type |
| 331 | * @return deserialized Object |
| 332 | */ |
| 333 | public <T> T deserialize(final ByteBuffer buffer) { |
| 334 | ByteBufferInput in = new ByteBufferInput(buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 335 | Kryo kryo = borrow(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 336 | try { |
| 337 | @SuppressWarnings("unchecked") |
| 338 | T obj = (T) kryo.readClassAndObject(in); |
| 339 | return obj; |
| 340 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 341 | release(kryo); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 344 | |
| 345 | /** |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 346 | * Deserializes given InputStream to an Object using Kryo instance in pool. |
| 347 | * |
| 348 | * @param stream input stream |
| 349 | * @param <T> deserialized Object type |
| 350 | * @return deserialized Object |
| 351 | */ |
| 352 | public <T> T deserialize(final InputStream stream) { |
| 353 | return deserialize(stream, DEFAULT_BUFFER_SIZE); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Deserializes given InputStream to an Object using Kryo instance in pool. |
| 358 | * |
| 359 | * @param stream input stream |
| 360 | * @param <T> deserialized Object type |
| 361 | * @return deserialized Object |
| 362 | * @param bufferSize size of the buffer in front of the stream |
| 363 | */ |
| 364 | public <T> T deserialize(final InputStream stream, final int bufferSize) { |
| 365 | ByteBufferInput in = new ByteBufferInput(stream, bufferSize); |
| 366 | Kryo kryo = borrow(); |
| 367 | try { |
| 368 | @SuppressWarnings("unchecked") |
| 369 | T obj = (T) kryo.readClassAndObject(in); |
| 370 | return obj; |
| 371 | } finally { |
| 372 | release(kryo); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Creates a Kryo instance. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 378 | * |
| 379 | * @return Kryo instance |
| 380 | */ |
Yuta HIGUCHI | 633cf88 | 2014-10-20 09:10:28 -0700 | [diff] [blame] | 381 | @Override |
| 382 | public Kryo create() { |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 383 | log.trace("Creating Kryo instance for {}", this); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 384 | Kryo kryo = new Kryo(); |
| 385 | kryo.setRegistrationRequired(registrationRequired); |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 386 | |
| 387 | // TODO rethink whether we want to use StdInstantiatorStrategy |
| 388 | kryo.setInstantiatorStrategy( |
| 389 | new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); |
| 390 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 391 | for (RegistrationBlock block : registeredBlocks) { |
| 392 | int id = block.begin(); |
| 393 | if (id == FLOATING_ID) { |
| 394 | id = kryo.getNextRegistrationId(); |
| 395 | } |
| 396 | for (Pair<Class<?>, Serializer<?>> entry : block.types()) { |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 397 | register(kryo, entry.getLeft(), entry.getRight(), id++); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | return kryo; |
| 401 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 402 | |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 403 | /** |
| 404 | * Register {@code type} and {@code serializer} to {@code kryo} instance. |
| 405 | * |
| 406 | * @param kryo Kryo instance |
| 407 | * @param type type to register |
| 408 | * @param serializer Specific serializer to register or null to use default. |
| 409 | * @param id type registration id to use |
| 410 | */ |
| 411 | private static void register(Kryo kryo, Class<?> type, Serializer<?> serializer, int id) { |
| 412 | Registration existing = kryo.getRegistration(id); |
| 413 | if (existing != null) { |
| 414 | if (existing.getType() != type) { |
| 415 | log.error("Failed to register {} as {}, {} was already registered.", |
| 416 | type, id, existing.getType()); |
| 417 | |
| 418 | throw new IllegalStateException(String.format( |
| 419 | "Failed to register %s as %s, %s was already registered.", |
| 420 | type, id, existing.getType())); |
| 421 | } |
| 422 | // falling through to register call for now. |
| 423 | // Consider skipping, if there's reasonable |
| 424 | // way to compare serializer equivalence. |
| 425 | } |
| 426 | Registration r; |
| 427 | if (serializer == null) { |
| 428 | r = kryo.register(type, id); |
| 429 | } else { |
| 430 | r = kryo.register(type, serializer, id); |
| 431 | } |
| 432 | if (r.getId() != id) { |
Ray Milkey | 6f95568 | 2016-05-13 13:14:30 -0700 | [diff] [blame] | 433 | log.info("{} already registed as {}. Skipping {}.", |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 434 | r.getType(), r.getId(), id); |
| 435 | } |
| 436 | log.trace("{} registered as {}", r.getType(), r.getId()); |
| 437 | } |
| 438 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 439 | @Override |
| 440 | public Kryo borrow() { |
| 441 | return pool.borrow(); |
| 442 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 443 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 444 | @Override |
| 445 | public void release(Kryo kryo) { |
| 446 | pool.release(kryo); |
| 447 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 448 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 449 | @Override |
| 450 | public <T> T run(KryoCallback<T> callback) { |
| 451 | return pool.run(callback); |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public String toString() { |
| 456 | return MoreObjects.toStringHelper(getClass()) |
| 457 | .add("registeredBlocks", registeredBlocks) |
| 458 | .toString(); |
| 459 | } |
| 460 | |
| 461 | static final class RegistrationBlock { |
| 462 | private final int begin; |
| 463 | private final ImmutableList<Pair<Class<?>, Serializer<?>>> types; |
| 464 | |
| 465 | public RegistrationBlock(int begin, List<Pair<Class<?>, Serializer<?>>> types) { |
| 466 | this.begin = begin; |
| 467 | this.types = ImmutableList.copyOf(types); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 470 | public int begin() { |
| 471 | return begin; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 474 | public ImmutableList<Pair<Class<?>, Serializer<?>>> types() { |
| 475 | return types; |
| 476 | } |
| 477 | |
| 478 | @Override |
| 479 | public String toString() { |
| 480 | return MoreObjects.toStringHelper(getClass()) |
| 481 | .add("begin", begin) |
| 482 | .add("types", types) |
| 483 | .toString(); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 486 | } |