blob: dbad6d0dd3e5893989b60f04f3ed61fee34889d1 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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
7 *
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.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.serializers;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070017
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080018import java.io.InputStream;
19import java.io.OutputStream;
tomf5d85d42014-10-02 05:27:56 -070020import java.nio.ByteBuffer;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070021
Jonathan Hart4f60f982014-10-27 08:11:17 -070022import org.onlab.util.KryoNamespace;
23
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080024import com.google.common.base.MoreObjects;
25
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070026/**
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070027 * StoreSerializer implementation using Kryo.
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070028 */
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070029public class KryoSerializer implements StoreSerializer {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070030
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070031 protected KryoNamespace serializerPool;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070032
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070033 public KryoSerializer() {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070034 setupKryoPool();
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070035 }
36
37 /**
Jonathan Hart4f60f982014-10-27 08:11:17 -070038 * Sets up the common serializers pool.
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070039 */
40 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070041 serializerPool = KryoNamespace.newBuilder()
42 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080043 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
44 .build();
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070045 }
46
47 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070048 public byte[] encode(final Object obj) {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070049 return serializerPool.serialize(obj);
50 }
51
52 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070053 public <T> T decode(final byte[] bytes) {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070054 if (bytes == null) {
55 return null;
56 }
57 return serializerPool.deserialize(bytes);
58 }
59
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070060 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070061 public void encode(Object obj, ByteBuffer buffer) {
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070062 serializerPool.serialize(obj, buffer);
63 }
64
65 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070066 public <T> T decode(ByteBuffer buffer) {
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070067 return serializerPool.deserialize(buffer);
68 }
69
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080070 @Override
71 public void encode(Object obj, OutputStream stream) {
72 serializerPool.serialize(obj, stream);
73 }
74
75 @Override
76 public <T> T decode(InputStream stream) {
77 return serializerPool.deserialize(stream);
78 }
79
80 @Override
81 public String toString() {
82 return MoreObjects.toStringHelper(getClass())
83 .add("serializerPool", serializerPool)
84 .toString();
85 }
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070086}