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 | 86f142f | 2016-07-09 17:44:09 -0700 | [diff] [blame] | 24 | import com.esotericsoftware.kryo.io.Output; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 25 | import com.esotericsoftware.kryo.pool.KryoCallback; |
Yuta HIGUCHI | 633cf88 | 2014-10-20 09:10:28 -0700 | [diff] [blame] | 26 | import com.esotericsoftware.kryo.pool.KryoFactory; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 27 | import com.esotericsoftware.kryo.pool.KryoPool; |
| 28 | import com.google.common.base.MoreObjects; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableList; |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 30 | import org.apache.commons.lang3.tuple.Pair; |
| 31 | import org.objenesis.strategy.StdInstantiatorStrategy; |
| 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import java.io.InputStream; |
| 35 | import java.io.OutputStream; |
| 36 | import java.nio.ByteBuffer; |
| 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 39 | import java.util.Objects; |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 40 | |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 41 | import static com.google.common.base.Preconditions.checkNotNull; |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 42 | import static org.slf4j.LoggerFactory.getLogger; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 43 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 44 | /** |
| 45 | * Pool of Kryo instances, with classes pre-registered. |
| 46 | */ |
| 47 | //@ThreadSafe |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 48 | public final class KryoNamespace implements KryoFactory, KryoPool { |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Default buffer size used for serialization. |
| 52 | * |
| 53 | * @see #serialize(Object) |
| 54 | */ |
Yuta HIGUCHI | 3878205 | 2014-11-09 23:51:58 -0800 | [diff] [blame] | 55 | public static final int DEFAULT_BUFFER_SIZE = 4096; |
Yuta HIGUCHI | d2a3882 | 2014-11-06 19:05:04 -0800 | [diff] [blame] | 56 | public static final int MAX_BUFFER_SIZE = 100 * 1000 * 1000; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 57 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 58 | /** |
| 59 | * ID to use if this KryoNamespace does not define registration id. |
| 60 | */ |
| 61 | public static final int FLOATING_ID = -1; |
| 62 | |
| 63 | /** |
| 64 | * Smallest ID free to use for user defined registrations. |
| 65 | */ |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 66 | public static final int INITIAL_ID = 16; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 67 | |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 68 | private static final String NO_NAME = "(no name)"; |
| 69 | |
HIGUCHI Yuta | b49b007 | 2016-02-22 22:50:45 -0800 | [diff] [blame] | 70 | private static final Logger log = getLogger(KryoNamespace.class); |
| 71 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 72 | |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 73 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 74 | private final KryoPool pool = new KryoPool.Builder(this) |
| 75 | .softReferences() |
| 76 | .build(); |
| 77 | |
| 78 | private final ImmutableList<RegistrationBlock> registeredBlocks; |
| 79 | |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 80 | private final boolean registrationRequired; |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 81 | private final String friendlyName; |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 82 | |
| 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 | 86f142f | 2016-07-09 17:44:09 -0700 | [diff] [blame] | 275 | Output out = new Output(bufferSize, MAX_BUFFER_SIZE); |
| 276 | return pool.run(kryo -> { |
| 277 | kryo.writeClassAndObject(out, obj); |
| 278 | out.flush(); |
| 279 | return out.toBytes(); |
| 280 | }); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 284 | * Serializes given object to byte buffer using Kryo instance in pool. |
| 285 | * |
| 286 | * @param obj Object to serialize |
| 287 | * @param buffer to write to |
| 288 | */ |
| 289 | public void serialize(final Object obj, final ByteBuffer buffer) { |
| 290 | ByteBufferOutput out = new ByteBufferOutput(buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 291 | Kryo kryo = borrow(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 292 | try { |
| 293 | kryo.writeClassAndObject(out, obj); |
Yuta HIGUCHI | cac919c | 2014-10-20 22:17:20 -0700 | [diff] [blame] | 294 | out.flush(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 295 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 296 | release(kryo); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Serializes given object to OutputStream using Kryo instance in pool. |
| 302 | * |
| 303 | * @param obj Object to serialize |
| 304 | * @param stream to write to |
| 305 | */ |
| 306 | public void serialize(final Object obj, final OutputStream stream) { |
| 307 | serialize(obj, stream, DEFAULT_BUFFER_SIZE); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Serializes given object to OutputStream using Kryo instance in pool. |
| 312 | * |
| 313 | * @param obj Object to serialize |
| 314 | * @param stream to write to |
| 315 | * @param bufferSize size of the buffer in front of the stream |
| 316 | */ |
| 317 | public void serialize(final Object obj, final OutputStream stream, final int bufferSize) { |
| 318 | ByteBufferOutput out = new ByteBufferOutput(stream, bufferSize); |
| 319 | Kryo kryo = borrow(); |
| 320 | try { |
| 321 | kryo.writeClassAndObject(out, obj); |
| 322 | out.flush(); |
| 323 | } finally { |
| 324 | release(kryo); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | /** |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 329 | * Deserializes given byte array to Object using Kryo instance in pool. |
| 330 | * |
| 331 | * @param bytes serialized bytes |
| 332 | * @param <T> deserialized Object type |
| 333 | * @return deserialized Object |
| 334 | */ |
| 335 | public <T> T deserialize(final byte[] bytes) { |
| 336 | Input in = new Input(bytes); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 337 | Kryo kryo = borrow(); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 338 | try { |
| 339 | @SuppressWarnings("unchecked") |
| 340 | T obj = (T) kryo.readClassAndObject(in); |
| 341 | return obj; |
| 342 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 343 | release(kryo); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 347 | /** |
| 348 | * Deserializes given byte buffer to Object using Kryo instance in pool. |
| 349 | * |
| 350 | * @param buffer input with serialized bytes |
| 351 | * @param <T> deserialized Object type |
| 352 | * @return deserialized Object |
| 353 | */ |
| 354 | public <T> T deserialize(final ByteBuffer buffer) { |
| 355 | ByteBufferInput in = new ByteBufferInput(buffer); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 356 | Kryo kryo = borrow(); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 357 | try { |
| 358 | @SuppressWarnings("unchecked") |
| 359 | T obj = (T) kryo.readClassAndObject(in); |
| 360 | return obj; |
| 361 | } finally { |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 362 | release(kryo); |
Yuta HIGUCHI | f4b107e | 2014-09-29 17:27:26 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 365 | |
| 366 | /** |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 367 | * Deserializes given InputStream to an Object using Kryo instance in pool. |
| 368 | * |
| 369 | * @param stream input stream |
| 370 | * @param <T> deserialized Object type |
| 371 | * @return deserialized Object |
| 372 | */ |
| 373 | public <T> T deserialize(final InputStream stream) { |
| 374 | return deserialize(stream, DEFAULT_BUFFER_SIZE); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Deserializes given InputStream to an Object using Kryo instance in pool. |
| 379 | * |
| 380 | * @param stream input stream |
| 381 | * @param <T> deserialized Object type |
| 382 | * @return deserialized Object |
| 383 | * @param bufferSize size of the buffer in front of the stream |
| 384 | */ |
| 385 | public <T> T deserialize(final InputStream stream, final int bufferSize) { |
| 386 | ByteBufferInput in = new ByteBufferInput(stream, bufferSize); |
| 387 | Kryo kryo = borrow(); |
| 388 | try { |
| 389 | @SuppressWarnings("unchecked") |
| 390 | T obj = (T) kryo.readClassAndObject(in); |
| 391 | return obj; |
| 392 | } finally { |
| 393 | release(kryo); |
| 394 | } |
| 395 | } |
| 396 | |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 397 | private String friendlyName() { |
| 398 | return friendlyName; |
| 399 | } |
| 400 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 401 | /** |
Jonathan Hart | 679d24d | 2016-05-13 13:29:53 -0700 | [diff] [blame] | 402 | * Gets the number of classes registered in this Kryo namespace. |
| 403 | * |
| 404 | * @return size of namespace |
| 405 | */ |
| 406 | public int size() { |
| 407 | return (int) registeredBlocks.stream() |
| 408 | .flatMap(block -> block.types().stream()) |
| 409 | .count(); |
| 410 | } |
| 411 | |
| 412 | /** |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 413 | * Creates a Kryo instance. |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 414 | * |
| 415 | * @return Kryo instance |
| 416 | */ |
Yuta HIGUCHI | 633cf88 | 2014-10-20 09:10:28 -0700 | [diff] [blame] | 417 | @Override |
| 418 | public Kryo create() { |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 419 | log.trace("Creating Kryo instance for {}", this); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 420 | Kryo kryo = new Kryo(); |
| 421 | kryo.setRegistrationRequired(registrationRequired); |
Jonathan Hart | be093f7 | 2016-03-25 11:14:29 -0700 | [diff] [blame] | 422 | |
| 423 | // TODO rethink whether we want to use StdInstantiatorStrategy |
| 424 | kryo.setInstantiatorStrategy( |
| 425 | new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy())); |
| 426 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 427 | for (RegistrationBlock block : registeredBlocks) { |
| 428 | int id = block.begin(); |
| 429 | if (id == FLOATING_ID) { |
| 430 | id = kryo.getNextRegistrationId(); |
| 431 | } |
| 432 | for (Pair<Class<?>, Serializer<?>> entry : block.types()) { |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 433 | register(kryo, entry.getLeft(), entry.getRight(), id++); |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | return kryo; |
| 437 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 438 | |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 439 | /** |
| 440 | * Register {@code type} and {@code serializer} to {@code kryo} instance. |
| 441 | * |
| 442 | * @param kryo Kryo instance |
| 443 | * @param type type to register |
| 444 | * @param serializer Specific serializer to register or null to use default. |
| 445 | * @param id type registration id to use |
| 446 | */ |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 447 | private void register(Kryo kryo, Class<?> type, Serializer<?> serializer, int id) { |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 448 | Registration existing = kryo.getRegistration(id); |
| 449 | if (existing != null) { |
| 450 | if (existing.getType() != type) { |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 451 | log.error("{}: Failed to register {} as {}, {} was already registered.", |
| 452 | friendlyName(), type, id, existing.getType()); |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 453 | |
| 454 | throw new IllegalStateException(String.format( |
| 455 | "Failed to register %s as %s, %s was already registered.", |
| 456 | type, id, existing.getType())); |
| 457 | } |
| 458 | // falling through to register call for now. |
| 459 | // Consider skipping, if there's reasonable |
| 460 | // way to compare serializer equivalence. |
| 461 | } |
| 462 | Registration r; |
| 463 | if (serializer == null) { |
| 464 | r = kryo.register(type, id); |
| 465 | } else { |
| 466 | r = kryo.register(type, serializer, id); |
| 467 | } |
| 468 | if (r.getId() != id) { |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 469 | log.warn("{}: {} already registed as {}. Skipping {}.", |
| 470 | friendlyName(), r.getType(), r.getId(), id); |
HIGUCHI Yuta | 0a1f29e | 2016-05-05 15:34:41 -0700 | [diff] [blame] | 471 | } |
| 472 | log.trace("{} registered as {}", r.getType(), r.getId()); |
| 473 | } |
| 474 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 475 | @Override |
| 476 | public Kryo borrow() { |
| 477 | return pool.borrow(); |
| 478 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 479 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 480 | @Override |
| 481 | public void release(Kryo kryo) { |
| 482 | pool.release(kryo); |
| 483 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 484 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 485 | @Override |
| 486 | public <T> T run(KryoCallback<T> callback) { |
| 487 | return pool.run(callback); |
| 488 | } |
| 489 | |
| 490 | @Override |
| 491 | public String toString() { |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 492 | if (friendlyName != NO_NAME) { |
| 493 | return MoreObjects.toStringHelper(getClass()) |
| 494 | .omitNullValues() |
| 495 | .add("friendlyName", friendlyName) |
| 496 | // omit lengthy detail, when there's a name |
| 497 | .toString(); |
| 498 | } |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 499 | return MoreObjects.toStringHelper(getClass()) |
| 500 | .add("registeredBlocks", registeredBlocks) |
| 501 | .toString(); |
| 502 | } |
| 503 | |
| 504 | static final class RegistrationBlock { |
| 505 | private final int begin; |
| 506 | private final ImmutableList<Pair<Class<?>, Serializer<?>>> types; |
| 507 | |
| 508 | public RegistrationBlock(int begin, List<Pair<Class<?>, Serializer<?>>> types) { |
| 509 | this.begin = begin; |
| 510 | this.types = ImmutableList.copyOf(types); |
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 int begin() { |
| 514 | return begin; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 517 | public ImmutableList<Pair<Class<?>, Serializer<?>>> types() { |
| 518 | return types; |
| 519 | } |
| 520 | |
| 521 | @Override |
| 522 | public String toString() { |
| 523 | return MoreObjects.toStringHelper(getClass()) |
| 524 | .add("begin", begin) |
| 525 | .add("types", types) |
| 526 | .toString(); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 527 | } |
HIGUCHI Yuta | b2d0fd8 | 2016-05-17 20:34:58 -0700 | [diff] [blame] | 528 | |
| 529 | @Override |
| 530 | public int hashCode() { |
| 531 | return types.hashCode(); |
| 532 | } |
| 533 | |
| 534 | // Only the registered types are used for equality. |
| 535 | @Override |
| 536 | public boolean equals(Object obj) { |
| 537 | if (this == obj) { |
| 538 | return true; |
| 539 | } |
| 540 | |
| 541 | if (obj instanceof RegistrationBlock) { |
| 542 | RegistrationBlock that = (RegistrationBlock) obj; |
| 543 | return Objects.equals(this.types, that.types); |
| 544 | } |
| 545 | return false; |
| 546 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 547 | } |
Yuta HIGUCHI | 24a086b | 2014-09-21 23:28:41 -0700 | [diff] [blame] | 548 | } |