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