mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-197 - improved buffer resizing
This commit is contained in:
@@ -19,43 +19,113 @@ package net.openhft.chronicle.map;
|
||||
import net.openhft.chronicle.hash.replication.AbstractReplication;
|
||||
import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig;
|
||||
import net.openhft.chronicle.hash.replication.UdpTransportConfig;
|
||||
import net.openhft.lang.io.ByteBufferBytes;
|
||||
import net.openhft.lang.io.Bytes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
final class Replicators {
|
||||
|
||||
static final String ONLY_UDP_WARN_MESSAGE =
|
||||
"MISSING TCP REPLICATION : The UdpReplicator only attempts to read data " +
|
||||
"(it does not enforce or guarantee delivery), you should use" +
|
||||
"the UdpReplicator if you have a large number of nodes, and you wish" +
|
||||
"to receive the data before it becomes available on TCP/IP. Since data" +
|
||||
"delivery is not guaranteed, it is recommended that you only use" +
|
||||
"the UDP Replicator in conjunction with a TCP Replicator";
|
||||
"(it does not enforce or guarantee delivery), you should use" +
|
||||
"the UdpReplicator if you have a large number of nodes, and you wish" +
|
||||
"to receive the data before it becomes available on TCP/IP. Since data" +
|
||||
"delivery is not guaranteed, it is recommended that you only use" +
|
||||
"the UDP Replicator in conjunction with a TCP Replicator";
|
||||
|
||||
private Replicators() {
|
||||
}
|
||||
|
||||
|
||||
static class OutBuffer implements BufferResizer {
|
||||
|
||||
@NotNull
|
||||
private ByteBufferBytes in;
|
||||
|
||||
@NotNull
|
||||
private ByteBuffer out;
|
||||
|
||||
@NotNull
|
||||
public ByteBufferBytes in() {
|
||||
return in;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public ByteBuffer out() {
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
OutBuffer(final int tcpBufferSize) {
|
||||
out = ByteBuffer.allocateDirect(tcpBufferSize);
|
||||
in = new ByteBufferBytes(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bytes resizeBuffer(int newCapacity) {
|
||||
|
||||
|
||||
if (newCapacity < out.capacity())
|
||||
throw new IllegalStateException("it not possible to resize the buffer smaller");
|
||||
|
||||
assert newCapacity < Integer.MAX_VALUE;
|
||||
|
||||
final ByteBuffer result = ByteBuffer.allocate(newCapacity).order(ByteOrder.nativeOrder());
|
||||
final long bytesPosition = in.position();
|
||||
|
||||
in = new ByteBufferBytes(result.slice());
|
||||
|
||||
out.position(0);
|
||||
out.limit((int) bytesPosition);
|
||||
|
||||
int numberOfLongs = (int) bytesPosition / 8;
|
||||
|
||||
// chunk in longs first
|
||||
for (int i = 0; i < numberOfLongs; i++) {
|
||||
in.writeLong(out.getLong());
|
||||
}
|
||||
|
||||
for (int i = numberOfLongs * 8; i < bytesPosition; i++) {
|
||||
in.writeByte(out.get());
|
||||
}
|
||||
|
||||
out = result;
|
||||
|
||||
assert out.capacity() == in.capacity();
|
||||
|
||||
assert out.capacity() == newCapacity;
|
||||
assert out.capacity() == in.capacity();
|
||||
assert in.limit() == in.capacity();
|
||||
return in;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static Replicator tcp(final AbstractReplication replication) {
|
||||
return new Replicator() {
|
||||
|
||||
|
||||
@Override
|
||||
protected Closeable applyTo(@NotNull final ChronicleMapBuilder builder,
|
||||
@NotNull final Replica replica,
|
||||
@NotNull final Replica.EntryExternalizable entryExternalizable,
|
||||
final ChronicleMap chronicleMap)
|
||||
throws IOException {
|
||||
|
||||
StatelessServerConnector statelessServer = new StatelessServerConnector(
|
||||
builder.keyBuilder, builder.valueBuilder,
|
||||
(VanillaChronicleMap) chronicleMap, builder.entrySize(true));
|
||||
final ChronicleMap chronicleMap) throws IOException {
|
||||
|
||||
TcpTransportAndNetworkConfig tcpConfig = replication.tcpTransportAndNetwork();
|
||||
assert tcpConfig != null;
|
||||
|
||||
TcpReplicator.StatelessClientParameters statelessClientParameters = new TcpReplicator.StatelessClientParameters(
|
||||
(VanillaChronicleMap) chronicleMap,
|
||||
builder.keyBuilder,
|
||||
builder.valueBuilder);
|
||||
|
||||
return new TcpReplicator(replica, entryExternalizable,
|
||||
tcpConfig,
|
||||
statelessServer,
|
||||
replication.remoteNodeValidator());
|
||||
replication.remoteNodeValidator(), statelessClientParameters);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user