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