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