Simplified support for Marshallable and BytesMarshallable, https://github.com/OpenHFT/Chronicle-Map/issues/196

This commit is contained in:
Peter Lawrey
2019-09-20 14:19:45 +01:00
parent edb7df2336
commit a7ebbf6a75
6 changed files with 130 additions and 120 deletions
@@ -50,6 +50,7 @@ public interface SizedWriter<T> extends Marshallable {
* @param toWrite the object which serialized form length should be returned
* @return the length (in bytes) of the serialized form of the given object
*/
// TODO lift the requirement to give a size first which can involve double serialization.
long size(@NotNull T toWrite);
/**
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2016-2019 Chronicle Software Ltd
*/
package net.openhft.chronicle.hash.serialization.impl;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesMarshallable;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.VanillaBytes;
import net.openhft.chronicle.hash.serialization.SizedReader;
import net.openhft.chronicle.hash.serialization.SizedWriter;
import net.openhft.chronicle.wire.BinaryWire;
import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.Wire;
import net.openhft.chronicle.wire.Wires;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class BytesMarshallableReaderWriter<V extends BytesMarshallable>
extends InstanceCreatingMarshaller<V>
implements SizedReader<V>, SizedWriter<V> {
public BytesMarshallableReaderWriter(Class<V> vClass) {
super(vClass);
}
private static final ThreadLocal<VanillaBytes> VANILLA_BYTES_TL = ThreadLocal.withInitial(() -> new VanillaBytes<>(BytesStore.empty()));
@NotNull
@Override
public V read(Bytes in, long size, @Nullable V using) {
if (using == null)
using = createInstance();
VanillaBytes vanillaBytes = VANILLA_BYTES_TL.get();
vanillaBytes.bytesStore(in, in.readPosition(), size);
using.readMarshallable(vanillaBytes);
return using;
}
@Override
public long size(@NotNull V toWrite) {
Bytes<?> bytes = Wires.acquireBytes();
toWrite.writeMarshallable(bytes);
return bytes.readRemaining();
}
@Override
public void write(Bytes out, long size, @NotNull V toWrite) {
VanillaBytes vanillaBytes = VANILLA_BYTES_TL.get();
vanillaBytes.bytesStore(out, out.readPosition(), out.readRemaining());
vanillaBytes.writeLimit(out.writePosition() + size);
toWrite.writeMarshallable(vanillaBytes);
}
}
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2016-2019 Chronicle Software Ltd
*/
package net.openhft.chronicle.hash.serialization.impl;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.hash.serialization.SizedReader;
import net.openhft.chronicle.hash.serialization.SizedWriter;
import net.openhft.chronicle.wire.BinaryWire;
import net.openhft.chronicle.wire.Marshallable;
import net.openhft.chronicle.wire.Wire;
import net.openhft.chronicle.wire.Wires;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class MarshallableReaderWriter<V extends Marshallable>
extends InstanceCreatingMarshaller<V>
implements SizedReader<V>, SizedWriter<V> {
public MarshallableReaderWriter(Class<V> vClass) {
super(vClass);
}
@NotNull
@Override
public V read(Bytes in, long size, @Nullable V using) {
if (using == null)
using = createInstance();
using.readMarshallable(Wires.binaryWireForRead(in, in.readPosition(), size));
return using;
}
@Override
public long size(@NotNull V toWrite) {
Wire wire = Wires.acquireBinaryWire();
toWrite.writeMarshallable(wire);
return wire.bytes().readRemaining();
}
@Override
public void write(Bytes out, long size, @NotNull V toWrite) {
BinaryWire wire = Wires.binaryWireForWrite(out, out.writePosition(), size);
toWrite.writeMarshallable(wire);
}
}