From 0f827769423c8c9bce0e7a1cdb11aeb765910a32 Mon Sep 17 00:00:00 2001 From: leventov Date: Wed, 1 Jul 2015 22:13:26 +0300 Subject: [PATCH] Added ComplextSerializationTest #29 --- .../map/example/ComplexSerializationTest.java | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 src/test/java/net/openhft/chronicle/map/example/ComplexSerializationTest.java diff --git a/src/test/java/net/openhft/chronicle/map/example/ComplexSerializationTest.java b/src/test/java/net/openhft/chronicle/map/example/ComplexSerializationTest.java new file mode 100644 index 000000000..8b0a5abdf --- /dev/null +++ b/src/test/java/net/openhft/chronicle/map/example/ComplexSerializationTest.java @@ -0,0 +1,117 @@ +/* + * Copyright 2015 Higher Frequency Trading + * + * http://www.higherfrequencytrading.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.openhft.chronicle.map.example; + +import net.openhft.chronicle.map.ChronicleMap; +import net.openhft.chronicle.map.ChronicleMapBuilder; +import net.openhft.lang.io.Bytes; +import net.openhft.lang.io.serialization.BytesMarshallable; +import net.openhft.lang.io.serialization.BytesMarshaller; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class ComplexSerializationTest { + + static class A { + String str_; + List list_; + } + + enum AMarshaller implements BytesMarshaller { + INSTANCE; + + @Override + public void write(Bytes out, A a) { + out.writeUTF(a.str_); + if (a.list_ != null) { + int size = a.list_.size(); + out.writeStopBit(size); + for (int i = 0; i < size; i++) { + a.list_.get(i).writeMarshallable(out); + } + } else { + out.writeStopBit(-1); + } + } + + @Override + public A read(Bytes in) { + return read(in, null); + } + + @Override + public A read(Bytes in, A a) { + if (a == null) + a = new A(); + a.str_ = in.readUTF(); + int size = (int) in.readStopBit(); + if (size >= 0) { + if (a.list_ == null) { + a.list_ = new ArrayList<>(size); + } else { + a.list_.clear(); + if (a.list_ instanceof ArrayList) + ((ArrayList) a.list_).ensureCapacity(size); + } + for (int i = 0; i < size; i++) { + B b = new B(); + b.readMarshallable(in); + a.list_.add(b); + } + } else { + assert size == -1; + a.list_ = null; + } + return a; + } + } + + static class B implements BytesMarshallable { + String str_; + + @Override public void readMarshallable(Bytes in) throws IllegalStateException { + str_ = in.readUTF(); + } + + @Override public void writeMarshallable(Bytes out) { + out.writeUTF(str_); + } + } + + @Test + public void testComplexSerialization() throws Exception { + try (ChronicleMap map = ChronicleMapBuilder + .of(String.class, A.class) + .valueMarshaller(AMarshaller.INSTANCE) + .entries(5) + .averageValueSize(1000) + .create()) { + A obj_A = new A(); + obj_A.str_ = "a"; + obj_A.list_ = new ArrayList<>(); + B b = new B(); + b.str_ = "b"; + obj_A.list_.add(b); + map.put("KEY1", obj_A); + map.get("KEY1"); + } + } +}