Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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 HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.serializers; |
| 17 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame^] | 18 | import java.io.InputStream; |
| 19 | import java.io.OutputStream; |
tom | f5d85d4 | 2014-10-02 05:27:56 -0700 | [diff] [blame] | 20 | import java.nio.ByteBuffer; |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 21 | |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
| 23 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame^] | 24 | import com.google.common.base.MoreObjects; |
| 25 | |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 26 | /** |
Yuta HIGUCHI | 971addc | 2014-10-07 23:23:17 -0700 | [diff] [blame] | 27 | * StoreSerializer implementation using Kryo. |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 28 | */ |
Yuta HIGUCHI | 971addc | 2014-10-07 23:23:17 -0700 | [diff] [blame] | 29 | public class KryoSerializer implements StoreSerializer { |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 30 | |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 31 | protected KryoNamespace serializerPool; |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 32 | |
Yuta HIGUCHI | 672488d | 2014-10-07 09:23:43 -0700 | [diff] [blame] | 33 | public KryoSerializer() { |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 34 | setupKryoPool(); |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /** |
Jonathan Hart | 4f60f98 | 2014-10-27 08:11:17 -0700 | [diff] [blame] | 38 | * Sets up the common serializers pool. |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 39 | */ |
| 40 | protected void setupKryoPool() { |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 41 | serializerPool = KryoNamespace.newBuilder() |
| 42 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame^] | 43 | .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID) |
| 44 | .build(); |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override |
Yuta HIGUCHI | 53a285d | 2014-10-06 23:58:01 -0700 | [diff] [blame] | 48 | public byte[] encode(final Object obj) { |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 49 | return serializerPool.serialize(obj); |
| 50 | } |
| 51 | |
| 52 | @Override |
Yuta HIGUCHI | 53a285d | 2014-10-06 23:58:01 -0700 | [diff] [blame] | 53 | public <T> T decode(final byte[] bytes) { |
Yuta HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 54 | if (bytes == null) { |
| 55 | return null; |
| 56 | } |
| 57 | return serializerPool.deserialize(bytes); |
| 58 | } |
| 59 | |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 60 | @Override |
Yuta HIGUCHI | 53a285d | 2014-10-06 23:58:01 -0700 | [diff] [blame] | 61 | public void encode(Object obj, ByteBuffer buffer) { |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 62 | serializerPool.serialize(obj, buffer); |
| 63 | } |
| 64 | |
| 65 | @Override |
Yuta HIGUCHI | 53a285d | 2014-10-06 23:58:01 -0700 | [diff] [blame] | 66 | public <T> T decode(ByteBuffer buffer) { |
Yuta HIGUCHI | f6d1170 | 2014-09-30 19:18:59 -0700 | [diff] [blame] | 67 | return serializerPool.deserialize(buffer); |
| 68 | } |
| 69 | |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame^] | 70 | @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 HIGUCHI | ad4c218 | 2014-09-29 11:16:23 -0700 | [diff] [blame] | 86 | } |