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