blob: da378488b8af93ffd9cfe28e1f70b600c6103618 [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 */
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070016package org.onlab.onos.store.serializers;
17
tomf5d85d42014-10-02 05:27:56 -070018import java.nio.ByteBuffer;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070019
Jonathan Hart4f60f982014-10-27 08:11:17 -070020import org.onlab.util.KryoNamespace;
21
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070022/**
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070023 * StoreSerializer implementation using Kryo.
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070024 */
Yuta HIGUCHI971addc2014-10-07 23:23:17 -070025public class KryoSerializer implements StoreSerializer {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070026
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070027 protected KryoNamespace serializerPool;
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070028
Yuta HIGUCHI672488d2014-10-07 09:23:43 -070029 public KryoSerializer() {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070030 setupKryoPool();
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070031 }
32
33 /**
Jonathan Hart4f60f982014-10-27 08:11:17 -070034 * Sets up the common serializers pool.
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070035 */
36 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070037 serializerPool = KryoNamespace.newBuilder()
38 .register(KryoNamespaces.API)
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070039 .build()
40 .populate(1);
41 }
42
43 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070044 public byte[] encode(final Object obj) {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070045 return serializerPool.serialize(obj);
46 }
47
48 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070049 public <T> T decode(final byte[] bytes) {
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070050 if (bytes == null) {
51 return null;
52 }
53 return serializerPool.deserialize(bytes);
54 }
55
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070056 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070057 public void encode(Object obj, ByteBuffer buffer) {
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070058 serializerPool.serialize(obj, buffer);
59 }
60
61 @Override
Yuta HIGUCHI53a285d2014-10-06 23:58:01 -070062 public <T> T decode(ByteBuffer buffer) {
Yuta HIGUCHIf6d11702014-09-30 19:18:59 -070063 return serializerPool.deserialize(buffer);
64 }
65
Yuta HIGUCHIad4c2182014-09-29 11:16:23 -070066}