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