From f1da352a5da35861d198090cf5c319d95f0435ee Mon Sep 17 00:00:00 2001 From: leventov Date: Tue, 2 Jun 2015 19:01:12 +0100 Subject: [PATCH] Made listeners almost work for replication --- pom.xml | 1 + .../openhft/chronicle/hash/ChronicleHash.java | 2 + .../net/openhft/chronicle/hash/Value.java | 9 +- .../hash/impl/JavaLangBytesAccessors.java | 2 +- .../chronicle/map/ChannelProvider.java | 2 +- .../chronicle/map/ChronicleMapBuilder.java | 3 +- .../chronicle/map/FilePerKeyChronicleMap.java | 7 + .../chronicle/map/MapEntryOperations.java | 5 +- .../net/openhft/chronicle/map/Replica.java | 27 - .../chronicle/map/ReplicatedChronicleMap.java | 96 +- .../net/openhft/chronicle/map/Replicator.java | 5 +- .../openhft/chronicle/map/Replicators.java | 6 +- .../chronicle/map/StatelessChronicleMap.java | 7 + .../openhft/chronicle/map/TcpReplicator.java | 70 +- .../chronicle/map/VanillaChronicleMap.java | 9 + .../map/impl/CompiledMapQueryContext.java | 1935 ++++++------ ...CompiledReplicatedMapIterationContext.java | 1776 +++++------ .../CompiledReplicatedMapQueryContext.java | 2729 +++++++++-------- .../net/openhft/chronicle/set/SetFromMap.java | 10 +- .../net/openhft/chronicle/map/BiMapTest.java | 4 +- .../chronicle/map/CHMUseCasesTest.java | 6 +- .../chronicle/map/ChannelReplicationTest.java | 1 - .../map/PostChannelCreationTest.java | 1 - .../map/ReplicatedChronicleMapTest.java | 1 - .../chronicle/map/ReplicationCheckingMap.java | 7 + .../chronicle/map/SingleMapChannelTest.java | 1 - .../TCPSocketReplication3VoidValueTest.java | 1 - ...cketReplication3wayPutReturnsNullTest.java | 1 - .../map/TCPSocketReplication3wayTest.java | 1 - .../map/TCPSocketReplication4WayMapTest.java | 2 - .../map/TCPSocketReplicationIntValueTest.java | 1 - .../map/TCPSocketReplicationNativeTest.java | 1 - ...CPSocketReplicationPostConnectionTest.java | 1 - .../map/TCPSocketReplicationTest.java | 2 - .../map/TcpReplicationLargeEntryTest.java | 1 - .../chronicle/map/TcpReplicationSoakTest.java | 1 - .../TcpReplicationWithAcquireContextTest.java | 1 - .../map/TcpTimeBasedReplicationSoakTest.java | 2 - ...esSpanningSeveralChunksReplicationHub.java | 1 - .../chronicle/map/TestReplication.java | 1 - .../map/TimeBasedReplicationTest.java | 1 - .../map/TwoMapOnDifferentServersTest.java | 1 - .../map/UDPSocketReplicationTest.java | 2 - .../jsr166/map/StatelessChronicleMapTest.java | 7 + .../openhft/lang/values/DoubleArrayTest.java | 1 - 45 files changed, 3472 insertions(+), 3279 deletions(-) diff --git a/pom.xml b/pom.xml index f132c36e3..9a431f163 100755 --- a/pom.xml +++ b/pom.xml @@ -227,6 +227,7 @@ net/openhft/chronicle/hash/KeyContext.java net/openhft/chronicle/map/MapKeyContext.java net/openhft/chronicle/hash/locks/LockingStrategy.java + net/openhft/chronicle/map/TcpReplicator.java -Xdoclint:none diff --git a/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java b/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java index ee419dd67..501800f5a 100644 --- a/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java +++ b/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java @@ -62,6 +62,8 @@ public interface ChronicleHash, EQC extends ExternalH @NotNull EQC queryContext(K key); + + @NotNull EQC queryContext(Value key); /** * Checks the given predicate on each entry in this {@code ChronicleHash} until all entries diff --git a/src/main/java/net/openhft/chronicle/hash/Value.java b/src/main/java/net/openhft/chronicle/hash/Value.java index 5ebd1a4b7..172011bb6 100644 --- a/src/main/java/net/openhft/chronicle/hash/Value.java +++ b/src/main/java/net/openhft/chronicle/hash/Value.java @@ -72,8 +72,8 @@ public interface Value { accessor.offset(source, index)); } - default void writeTo(WriteAccess access, T2 handle, long offset) { - Access.copy(access(), handle(), offset(), access, handle, offset, size()); + default void writeTo(WriteAccess targetAccess, T2 target, long targetOffset) { + Access.copy(access(), handle(), offset(), targetAccess, target, targetOffset, size()); } default void writeTo(StreamingDataOutput output) { @@ -83,8 +83,9 @@ public interface Value { } default > void writeTo( - Accessor accessor, S source, long index) { - writeTo(accessor.access(source), accessor.handle(source), accessor.offset(source, index)); + Accessor targetAccessor, S target, long index) { + writeTo(targetAccessor.access(target), targetAccessor.handle(target), + targetAccessor.offset(target, index)); } /** diff --git a/src/main/java/net/openhft/chronicle/hash/impl/JavaLangBytesAccessors.java b/src/main/java/net/openhft/chronicle/hash/impl/JavaLangBytesAccessors.java index a7fb65671..02a0a6aba 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/JavaLangBytesAccessors.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/JavaLangBytesAccessors.java @@ -28,7 +28,7 @@ import static net.openhft.chronicle.bytes.Access.nativeAccess; public final class JavaLangBytesAccessors { public static Accessor.Full uncheckedBytesAccessor(Bytes bytes) { - return bytes instanceof NativeBytes ? NativeBytesAccessor.INSTANCE : + return bytes instanceof NativeBytes ? GenericBytesAccessor.INSTANCE : GenericBytesAccessor.INSTANCE; } diff --git a/src/main/java/net/openhft/chronicle/map/ChannelProvider.java b/src/main/java/net/openhft/chronicle/map/ChannelProvider.java index bf0d07148..dd7e69f7c 100644 --- a/src/main/java/net/openhft/chronicle/map/ChannelProvider.java +++ b/src/main/java/net/openhft/chronicle/map/ChannelProvider.java @@ -548,7 +548,7 @@ public final class ChannelProvider implements Closeable { @Override protected Closeable applyTo(ChronicleMapBuilder builder, Replica map, EntryExternalizable entryExternalizable, - final ChronicleMap chronicleMap) { + final ReplicatedChronicleMap replicatedMap) { add(chronicleChannel, map, entryExternalizable); return this; } diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java index 1696839de..c29e21864 100755 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapBuilder.java @@ -1473,7 +1473,8 @@ public final class ChronicleMapBuilder implements replicators.add(ch); } for (Replicator replicator : replicators) { - Closeable token = replicator.applyTo(this, result, result, map); + Closeable token = replicator.applyTo(this, result, result, + (ReplicatedChronicleMap)map); if (replicators.size() == 1 && token.getClass() == UdpReplicator.class) { LOG.warn(Replicators.ONLY_UDP_WARN_MESSAGE); } diff --git a/src/main/java/net/openhft/chronicle/map/FilePerKeyChronicleMap.java b/src/main/java/net/openhft/chronicle/map/FilePerKeyChronicleMap.java index e00e91b24..a7b5bfed0 100644 --- a/src/main/java/net/openhft/chronicle/map/FilePerKeyChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/FilePerKeyChronicleMap.java @@ -1,5 +1,6 @@ package net.openhft.chronicle.map; +import net.openhft.chronicle.hash.Value; import net.openhft.chronicle.hash.function.SerializableFunction; import net.openhft.lang.Jvm; import org.jetbrains.annotations.NotNull; @@ -185,6 +186,12 @@ public class FilePerKeyChronicleMap implements ChronicleMap { throw new UnsupportedOperationException(); } + @NotNull + @Override + public ExternalMapQueryContext queryContext(Value key) { + throw new UnsupportedOperationException(); + } + @Override public boolean forEachEntryWhile(Predicate> predicate) { throw new UnsupportedOperationException(); diff --git a/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java b/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java index 956590c34..d59a05e26 100644 --- a/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java +++ b/src/main/java/net/openhft/chronicle/map/MapEntryOperations.java @@ -23,7 +23,10 @@ import org.jetbrains.annotations.NotNull; /** * SPI interface for customizing "low-level" modification operations on {@link ChronicleMap} - * entries. + * entries. All {@code ChronicleMap} modifications operate via and instance of this interface: + *
    + *
  • Ordinary query operations: {@code map.put()}, {@code map.compute()}. In fact, the
  • + *
* * @param the map key type * @param the map value type diff --git a/src/main/java/net/openhft/chronicle/map/Replica.java b/src/main/java/net/openhft/chronicle/map/Replica.java index 26e64f988..a4edf51dc 100644 --- a/src/main/java/net/openhft/chronicle/map/Replica.java +++ b/src/main/java/net/openhft/chronicle/map/Replica.java @@ -172,33 +172,6 @@ public interface Replica extends Closeable { } - /** - * provides a key and value from NativeBytes, this can be used in conjunction with the - * modification iterator to get the key and value out of the NativeBytes - */ - interface EntryResolver { - - /** - * gets the key from the entry - * - * @param entry the bytes which the bytes which point to the entry - * @param usingKey the key object to reuse, if possible - * @return the key which is in the entry - */ - K key(@NotNull Bytes entry, K usingKey); - - /** - * gets the value from the entry - * - * @param entry the bytes which reference to the entry - * @param usingValue the value object to reuse, if possible - * @return the value which is in the entry or null if the value has been remove from the map - */ - V value(@NotNull Bytes entry, V usingValue); - - boolean wasRemoved(@NotNull Bytes entry); - } - /** * Implemented typically by a replicator, This interface provides the event, which will get * called whenever a put() or remove() has occurred to the map diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index 262fe3495..62e3992fa 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -87,7 +87,7 @@ import static net.openhft.lang.collection.DirectBitSet.NOT_FOUND; public class ReplicatedChronicleMap, V, VI, MVI extends MetaBytesInterop, R> extends VanillaChronicleMap - implements Replica, Replica.EntryExternalizable, Replica.EntryResolver { + implements Replica, Replica.EntryExternalizable { // for file, jdbc and UDP replication public static final int RESERVED_MOD_ITER = 8; public static final int ADDITIONAL_ENTRY_BYTES = 10; @@ -108,6 +108,7 @@ public class ReplicatedChronicleMap remoteOperations = DefaultSpi.mapRemoteOperations(); transient CompiledReplicatedMapQueryContext remoteOpContext; + transient CompiledReplicatedMapIterationContext remoteItContext; public ReplicatedChronicleMap(@NotNull ChronicleMapBuilder builder, AbstractReplication replication) @@ -291,10 +292,12 @@ public class ReplicatedChronicleMap entry, int chronicleId) { return entry.originIdentifier() == localIdentifier; } + @Override public int sizeOfEntry(@NotNull Bytes entry, int chronicleId) { long start = entry.position(); @@ -413,12 +416,28 @@ public class ReplicatedChronicleMap remoteOpContext() { if (remoteOpContext == null) { - remoteOpContext = q().getContext(); + remoteOpContext = q(); } + assert !remoteOpContext.usedInit(); + remoteOpContext.initUsed(true); return remoteOpContext; } + + private CompiledReplicatedMapIterationContext remoteItContext() { + if (remoteItContext == null) { + remoteItContext = i(); + } + assert !remoteItContext.usedInit(); + remoteItContext.initUsed(true); + return remoteItContext; + } + /** * This method does not set a segment lock, A segment lock should be obtained before calling * this method, especially when being used in a multi threaded context. @@ -426,64 +445,9 @@ public class ReplicatedChronicleMap remoteOpContext = - remoteOpContext()) { - if (remoteOpContext.usedInit()) - throw new IllegalStateException(); - remoteOpContext.initUsed(true); + mapContext()) { remoteOpContext.initReplicationInput(source); - } - } - - @Override - public K key(@NotNull Bytes entry, K usingKey) { - final long start = entry.position(); - try { - long keySize = keySizeMarshaller.readSize(entry); - ThreadLocalCopies copies = keyReaderProvider.getCopies(null); - return keyReaderProvider.get(copies, originalKeyReader).read(entry, keySize); - } finally { - entry.position(start); - } - } - - @Override - public V value(@NotNull Bytes entry, V usingValue) { - final long start = entry.position(); - try { - entry.skip(keySizeMarshaller.readSize(entry)); - - //timeStamp - entry.readLong(); - - final byte identifier = entry.readByte(); - if (identifier != localIdentifier) { - return null; - } - - final boolean isDeleted = entry.readBoolean(); - long valueSize; - if (!isDeleted) { - valueSize = valueSizeMarshaller.readSize(entry); - assert valueSize > 0; - } else { - return null; - } - alignment.alignPositionAddr(entry); - ThreadLocalCopies copies = valueReaderProvider.getCopies(null); - BytesReader valueReader = valueReaderProvider.get(copies, originalValueReader); - return valueReader.read(entry, valueSize, usingValue); - } finally { - entry.position(start); - } - } - - @Override - public boolean wasRemoved(@NotNull Bytes entry) { - final long start = entry.position(); - try { - return entry.readBoolean(keySizeMarshaller.readSize(entry) + 9L); - } finally { - entry.position(start); + remoteOpContext.processReplicatedEvent(); } } @@ -520,8 +484,6 @@ public class ReplicatedChronicleMap context = - mapContext(); // records the current position of the cursor in the bitset private volatile long position = -1L; @@ -597,10 +559,10 @@ public class ReplicatedChronicleMap>> segmentIndexShift); - if (context.theSegmentIndexInit() && context.segmentIndex() != segmentIndex) + try (CompiledReplicatedMapIterationContext context = + iterationContext()) { context.initTheSegmentIndex(segmentIndex); - context.updateLock().lock(); - try { + context.updateLock().lock(); if (changesForUpdates.get(position)) { entryCallback.onBeforeEntry(); @@ -609,7 +571,7 @@ public class ReplicatedChronicleMapThis method isn't intended to be called from the client * code. * + * @param chronicleMap to wrap. * @param builder the builder from which the map was constructed. The replicator may * obtain some map configurations, not accessible via the map * instance. * @param map a replicated map instance. Provides basic tools for replication * implementation. * @param entryExternalizable the callback for ser/deser implementation in the replicator - * @param chronicleMap to wrap. + * @param replicatedMap * @return a {@code Closeable} token to control replication resources. It should be closed on * closing the replicated map. * @throws java.io.IOException if an io error occurred during the replicator setup @@ -49,5 +50,5 @@ abstract class Replicator { */ protected abstract Closeable applyTo(ChronicleMapBuilder builder, Replica map, Replica.EntryExternalizable entryExternalizable, - final ChronicleMap chronicleMap) throws IOException; + final ReplicatedChronicleMap replicatedMap) throws IOException; } diff --git a/src/main/java/net/openhft/chronicle/map/Replicators.java b/src/main/java/net/openhft/chronicle/map/Replicators.java index d72209de9..fcccf1e6d 100644 --- a/src/main/java/net/openhft/chronicle/map/Replicators.java +++ b/src/main/java/net/openhft/chronicle/map/Replicators.java @@ -114,13 +114,13 @@ final class Replicators { protected Closeable applyTo(@NotNull final ChronicleMapBuilder builder, @NotNull final Replica replica, @NotNull final Replica.EntryExternalizable entryExternalizable, - final ChronicleMap chronicleMap) throws IOException { + final ReplicatedChronicleMap replicatedMap) throws IOException { TcpTransportAndNetworkConfig tcpConfig = replication.tcpTransportAndNetwork(); TcpReplicator.StatelessClientParameters statelessClientParameters = new TcpReplicator.StatelessClientParameters( - (VanillaChronicleMap) chronicleMap, + replicatedMap, builder.keyBuilder, builder.valueBuilder); @@ -139,7 +139,7 @@ final class Replicators { protected Closeable applyTo(@NotNull final ChronicleMapBuilder builder, @NotNull final Replica map, @NotNull final Replica.EntryExternalizable entryExternalizable, - final ChronicleMap chronicleMap) + final ReplicatedChronicleMap replicatedMap) throws IOException { return new UdpReplicator(map, entryExternalizable, replicationConfig ); diff --git a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java index 885daeeec..43b946c90 100644 --- a/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/StatelessChronicleMap.java @@ -20,6 +20,7 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.bytes.IORuntimeException; import net.openhft.chronicle.core.util.CloseablesManager; +import net.openhft.chronicle.hash.Value; import net.openhft.chronicle.hash.function.SerializableFunction; import net.openhft.chronicle.hash.impl.util.BuildVersion; import net.openhft.chronicle.hash.serialization.BytesReader; @@ -552,6 +553,12 @@ class StatelessChronicleMap implements ChronicleMap, Closeable, Clon throw new UnsupportedOperationException("Contexts are not supported by stateless clients"); } + @NotNull + @Override + public ExternalMapQueryContext queryContext(Value key) { + throw new UnsupportedOperationException("Contexts are not supported by stateless clients"); + } + public V get(Object key) { return fetchObject(vClass, GET, (K) key); } diff --git a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java index 175023f97..9f124b7bd 100644 --- a/src/main/java/net/openhft/chronicle/map/TcpReplicator.java +++ b/src/main/java/net/openhft/chronicle/map/TcpReplicator.java @@ -24,8 +24,11 @@ import net.openhft.chronicle.hash.replication.RemoteNodeValidator; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.hash.replication.ThrottlingConfig; import net.openhft.chronicle.hash.serialization.BytesReader; +import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop; import net.openhft.chronicle.hash.serialization.internal.ReaderWithSize; import net.openhft.chronicle.hash.serialization.internal.SerializationBuilder; +import net.openhft.chronicle.map.impl.CompiledReplicatedMapQueryContext; +import net.openhft.chronicle.map.impl.QueryContextInterface; import net.openhft.lang.io.AbstractBytes; import net.openhft.lang.io.ByteBufferBytes; import net.openhft.lang.io.Bytes; @@ -68,7 +71,7 @@ interface Work { * * @author Rob Austin. */ -final class TcpReplicator extends AbstractChannelReplicator implements Closeable { +public final class TcpReplicator extends AbstractChannelReplicator implements Closeable { public static final long TIMESTAMP_FACTOR = 10000; private static final int STATELESS_CLIENT = -127; @@ -106,7 +109,7 @@ final class TcpReplicator extends AbstractChannelReplicator implements Clo StatelessClientParameters statelessClientParameters; static class StatelessClientParameters { - public StatelessClientParameters(VanillaChronicleMap map, + public StatelessClientParameters(ReplicatedChronicleMap map, SerializationBuilder keySerializationBuilder, SerializationBuilder valueSerializationBuilder) { this.map = map; @@ -114,7 +117,7 @@ final class TcpReplicator extends AbstractChannelReplicator implements Clo this.valueSerializationBuilder = valueSerializationBuilder; } - VanillaChronicleMap map; + ReplicatedChronicleMap map; SerializationBuilder keySerializationBuilder; SerializationBuilder valueSerializationBuilder; } @@ -1052,7 +1055,7 @@ final class TcpReplicator extends AbstractChannelReplicator implements Clo /** * @author Rob Austin. */ - class TcpSocketChannelEntryWriter { + public class TcpSocketChannelEntryWriter { @NotNull @@ -1092,7 +1095,7 @@ final class TcpReplicator extends AbstractChannelReplicator implements Clo in().writeByte(localIdentifier); } - void ensureBufferSize(long size) { + public void ensureBufferSize(long size) { if (in().remaining() < size) { size += entryCallback.in().position(); if (size > Integer.MAX_VALUE) @@ -1118,7 +1121,7 @@ final class TcpReplicator extends AbstractChannelReplicator implements Clo throw e; } - Bytes in() { + public Bytes in() { return entryCallback.in(); } @@ -1229,7 +1232,7 @@ final class TcpReplicator extends AbstractChannelReplicator implements Clo * localHeartbeatInterval */ private void writeHeartbeatToBuffer() { - // denotes the state - 0 for a beat + // denotes the state - 0 for a heartbeat in().writeByte(HEARTBEAT.ordinal()); // denotes the size in bytes @@ -1471,7 +1474,7 @@ final class TcpReplicator extends AbstractChannelReplicator implements Clo /** * @author Rob Austin. */ -class StatelessServerConnector { +class StatelessServerConnector { private static final Logger LOG = LoggerFactory.getLogger(StatelessServerConnector.class .getName()); @@ -1494,15 +1497,14 @@ class StatelessServerConnector { private final WriterWithSize valueWriterWithSize; @NotNull - private final VanillaChronicleMap map; - //private final BytesChronicleMap bytesMap; + private final ReplicatedChronicleMap map; private final SerializationBuilder keySerializationBuilder; private final SerializationBuilder valueSerializationBuilder; private final int tcpBufferSize; StatelessServerConnector( - @NotNull VanillaChronicleMap map, + @NotNull ReplicatedChronicleMap map, @NotNull final BufferResizer bufferResizer, int tcpBufferSize, final SerializationBuilder keySerializationBuilder, final SerializationBuilder valueSerializationBuilder) { @@ -1515,7 +1517,6 @@ class StatelessServerConnector { valueReaderWithSize = new ReaderWithSize<>(valueSerializationBuilder); valueWriterWithSize = new WriterWithSize<>(valueSerializationBuilder, bufferResizer); this.map = map; - //bytesMap = new BytesChronicleMap(map); } @Nullable @@ -1699,13 +1700,22 @@ class StatelessServerConnector { return null; } + private CompiledReplicatedMapQueryContext query(Bytes reader) { + CompiledReplicatedMapQueryContext q = map.mapContext(); + q.initInputBytes(reader); + q.initInputKey(q.inputKeyBytesValue()); + return q; + } + @Nullable private Work removeWithValue(Bytes reader, @NotNull TcpReplicator.TcpSocketChannelEntryWriter writer, final long sizeLocation, long timestamp, byte id) { try { writer.ensureBufferSize(1L); - //writer.in().writeBoolean(bytesMap.remove(reader, reader)); + try (CompiledReplicatedMapQueryContext q = query(reader)) { + writer.in().writeBoolean(map.methods.remove(q, q.inputFirstValueBytesValue())); + } } catch (Throwable e) { return sendException(writer, sizeLocation, e); } @@ -1719,7 +1729,10 @@ class StatelessServerConnector { final long sizeLocation, long timestamp, byte id) { try { writer.ensureBufferSize(1L); - //writer.in().writeBoolean(bytesMap.replace(reader, reader, reader)); + try (CompiledReplicatedMapQueryContext q = query(reader)) { + writer.in().writeBoolean(map.methods.replace(q, q.inputFirstValueBytesValue(), + q.inputSecondValueBytesValue())); + } } catch (Throwable e) { return sendException(writer, sizeLocation, e); } @@ -1849,7 +1862,10 @@ class StatelessServerConnector { @NotNull TcpReplicator.TcpSocketChannelEntryWriter writer, final long sizeLocation) { try { - //writer.in().writeBoolean(bytesMap.containsKey(reader)); + writer.ensureBufferSize(1L); + try (CompiledReplicatedMapQueryContext q = query(reader)) { + writer.in().writeBoolean(map.methods.containsKey(q)); + } } catch (Throwable e) { return sendException(writer, sizeLocation, e); } @@ -1901,7 +1917,7 @@ class StatelessServerConnector { final long sizeLocation, long timestamp, byte id) { //bytesMap.output = writer; try { - // bytesMap.put(reader, reader); + //bytesMap.put(reader, reader); } catch (Throwable e) { return sendException(writer, sizeLocation, e); } finally { @@ -1914,20 +1930,20 @@ class StatelessServerConnector { @SuppressWarnings("SameReturnValue") @Nullable private Work remove(Bytes reader, long timestamp, byte id) { - //bytesMap.remove(reader); +// bytesMap.remove(reader); return null; } @Nullable private Work remove(Bytes reader, TcpReplicator.TcpSocketChannelEntryWriter writer, final long sizeLocation, long timestamp, byte id) { - //bytesMap.output = writer; +// bytesMap.output = writer; try { - // bytesMap.remove(reader); +// bytesMap.remove(reader); } catch (Throwable e) { return sendException(writer, sizeLocation, e); } finally { - // bytesMap.output = null; +// bytesMap.output = null; } writeSizeAndFlags(sizeLocation, false, writer.in()); return null; @@ -1939,7 +1955,7 @@ class StatelessServerConnector { final long sizeLocation, long timestamp, byte id) { try { - // bytesMap.putAll(reader); +// bytesMap.putAll(reader); } catch (Throwable e) { return sendException(writer, sizeLocation, e); } @@ -1950,7 +1966,7 @@ class StatelessServerConnector { @SuppressWarnings("SameReturnValue") @Nullable private Work putAll(@NotNull Bytes reader, long timestamp, byte id) { - //bytesMap.putAll(reader); +// bytesMap.putAll(reader); return null; } @@ -2110,11 +2126,11 @@ class StatelessServerConnector { final long sizeLocation, long timestamp, byte id) { // bytesMap.output = writer; try { - // bytesMap.putIfAbsent(reader, reader); +// bytesMap.putIfAbsent(reader, reader); } catch (Throwable e) { return sendException(writer, sizeLocation, e); } finally { - // bytesMap.output = null; +// bytesMap.output = null; } writeSizeAndFlags(sizeLocation, false, writer.in()); return null; @@ -2123,13 +2139,13 @@ class StatelessServerConnector { @Nullable private Work replace(Bytes reader, TcpReplicator.TcpSocketChannelEntryWriter writer, final long sizeLocation, long timestamp, byte id) { - // bytesMap.output = writer; +// bytesMap.output = writer; try { - // bytesMap.replace(reader, reader); +// bytesMap.replace(reader, reader); } catch (Throwable e) { return sendException(writer, sizeLocation, e); } finally { - // bytesMap.output = null; +// bytesMap.output = null; } writeSizeAndFlags(sizeLocation, false, writer.in()); return null; diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 17293ab4a..cea0dcfc1 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -19,6 +19,7 @@ package net.openhft.chronicle.map; import net.openhft.chronicle.hash.KeyContext; +import net.openhft.chronicle.hash.Value; import net.openhft.chronicle.hash.impl.VanillaChronicleHash; import net.openhft.chronicle.hash.serialization.BytesReader; import net.openhft.chronicle.hash.serialization.SizeMarshaller; @@ -364,6 +365,14 @@ public class VanillaChronicleMap queryContext(Value key) { + QueryContextInterface q = mapContext(); + q.initInputKey(key); + return q; + } + @Override public V get(Object key) { try (QueryContextInterface q = queryContext(key)) { diff --git a/src/main/java/net/openhft/chronicle/map/impl/CompiledMapQueryContext.java b/src/main/java/net/openhft/chronicle/map/impl/CompiledMapQueryContext.java index 4383df885..7f290c4dc 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/CompiledMapQueryContext.java +++ b/src/main/java/net/openhft/chronicle/map/impl/CompiledMapQueryContext.java @@ -56,21 +56,21 @@ import java.util.concurrent.TimeUnit; public class CompiledMapQueryContext, V, VI, MVI extends MetaBytesInterop, R, T> implements AutoCloseable , HashEntry , InterProcessReadWriteUpdateLock , ExternalMapQueryContext , MapAbsentEntry , MapContext , MapEntry , QueryContextInterface , VanillaChronicleMapHolder { public void close() { - CompiledMapQueryContext.this.defaultReturnValue.closeDefaultReturnedValue(); - CompiledMapQueryContext.this.closeAllocatedChunks(); CompiledMapQueryContext.this.closeInputKey(); CompiledMapQueryContext.this.wrappedValueInstanceValue.closeValue(); - CompiledMapQueryContext.this.usingReturnValue.closeUsingReturnValue(); - CompiledMapQueryContext.this.wrappedValueInstanceValue.closeNext(); - CompiledMapQueryContext.this.closeInputBytes(); CompiledMapQueryContext.this.closeUsed(); - CompiledMapQueryContext.this.inputKeyInstanceValue.closeKey(); + CompiledMapQueryContext.this.closeInputBytes(); CompiledMapQueryContext.this.inputValueInstanceValue.closeValue(); - CompiledMapQueryContext.this.closeValueBytesInteropValueMetaInteropDependants(); - CompiledMapQueryContext.this.closeVanillaChronicleMapHolderImplContextAtIndexInChainDependants(); - CompiledMapQueryContext.this.closeOwnerThreadHolderCheckAccessingFromOwnerThreadDependants(); + CompiledMapQueryContext.this.usingReturnValue.closeUsingReturnValue(); + CompiledMapQueryContext.this.defaultReturnValue.closeDefaultReturnedValue(); + CompiledMapQueryContext.this.closeAllocatedChunks(); + CompiledMapQueryContext.this.wrappedValueInstanceValue.closeNext(); + CompiledMapQueryContext.this.inputKeyInstanceValue.closeKey(); CompiledMapQueryContext.this.closeKeyBytesInteropKeyMetaInteropDependants(); + CompiledMapQueryContext.this.closeOwnerThreadHolderCheckAccessingFromOwnerThreadDependants(); CompiledMapQueryContext.this.closeMapEntryStagesEntryBytesAccessOffsetDependants(); + CompiledMapQueryContext.this.closeVanillaChronicleMapHolderImplContextAtIndexInChainDependants(); + CompiledMapQueryContext.this.closeValueBytesInteropValueMetaInteropDependants(); } public void incrementModCountGuarded() { @@ -122,31 +122,31 @@ public class CompiledMapQueryContext)(entryBytesAccessor.access(entryBytes))); this.deprecatedMapKeyContext = new DeprecatedMapKeyContextOnQuery(); - this.inputKeyBytesValue = new InputKeyBytesValue(); - this.innerUpdateLock = new UpdateLock(); - this.inputKeyInstanceValue = new InputKeyInstanceValue(); + this.entryValue = new EntryValueBytesValue(); + this.owner = Thread.currentThread(); + this.defaultReturnValue = new DefaultReturnValue(); + this.deprecatedMapAcquireContext = new DeprecatedMapAcquireContextOnQuery(); this.copies = ThreadLocalCopies.get(); this.valueInterop = CompiledMapQueryContext.this.m().valueInteropProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.m().originalValueInterop); this.valueReader = CompiledMapQueryContext.this.m().valueReaderProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.m().originalValueReader); this.keyInterop = CompiledMapQueryContext.this.h().keyInteropProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.h().originalKeyInterop); this.keyReader = CompiledMapQueryContext.this.h().keyReaderProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.h().originalKeyReader); - this.wrappedValueInstanceValue = new WrappedValueInstanceValue(); - this.innerWriteLock = new WriteLock(); - this.owner = Thread.currentThread(); - this.usingReturnValue = new UsingReturnValue(); - this.deprecatedMapAcquireContext = new DeprecatedMapAcquireContextOnQuery(); - this.entryValue = new EntryValueBytesValue(); - this.innerReadLock = new ReadLock(); - this.inputSecondValueBytesValue = new InputSecondValueBytesValue(); - this.defaultReturnValue = new DefaultReturnValue(); + this.inputKeyInstanceValue = new InputKeyInstanceValue(); + this.inputKeyBytesValue = new InputKeyBytesValue(); this.inputValueInstanceValue = new InputValueInstanceValue(); - this.entryBytes = CompiledMapQueryContext.this.h().ms.bytes(); - this.entryBytesAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(entryBytes); - this.entryBytesAccessHandle = ((T)(entryBytesAccessor.handle(entryBytes))); - this.entryBytesAccess = ((Access)(entryBytesAccessor.access(entryBytes))); + this.innerWriteLock = new WriteLock(); this.entryKey = new EntryKeyBytesValue(); - this.inputFirstValueBytesValue = new InputFirstValueBytesValue(); + this.usingReturnValue = new UsingReturnValue(); + this.innerUpdateLock = new UpdateLock(); } public CompiledMapQueryContext(CompiledMapQueryContext c) { @@ -154,37 +154,41 @@ public class CompiledMapQueryContext)(c.m)); + this.inputFirstValueBytesValue = new InputFirstValueBytesValue(); + this.innerReadLock = new ReadLock(); + this.wrappedValueInstanceValue = new WrappedValueInstanceValue(); + this.inputSecondValueBytesValue = new InputSecondValueBytesValue(); + this.entryBytes = CompiledMapQueryContext.this.h().ms.bytes(); + this.entryBytesAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(entryBytes); + this.entryBytesAccessHandle = ((T)(entryBytesAccessor.handle(entryBytes))); + this.entryBytesAccess = ((Access)(entryBytesAccessor.access(entryBytes))); this.deprecatedMapKeyContext = new DeprecatedMapKeyContextOnQuery(); - this.inputKeyBytesValue = new InputKeyBytesValue(); - this.innerUpdateLock = new UpdateLock(); - this.inputKeyInstanceValue = new InputKeyInstanceValue(); + this.entryValue = new EntryValueBytesValue(); + this.owner = Thread.currentThread(); + this.defaultReturnValue = new DefaultReturnValue(); + this.deprecatedMapAcquireContext = new DeprecatedMapAcquireContextOnQuery(); this.copies = ThreadLocalCopies.get(); this.valueInterop = CompiledMapQueryContext.this.m().valueInteropProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.m().originalValueInterop); this.valueReader = CompiledMapQueryContext.this.m().valueReaderProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.m().originalValueReader); this.keyInterop = CompiledMapQueryContext.this.h().keyInteropProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.h().originalKeyInterop); this.keyReader = CompiledMapQueryContext.this.h().keyReaderProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.h().originalKeyReader); - this.wrappedValueInstanceValue = new WrappedValueInstanceValue(); - this.innerWriteLock = new WriteLock(); - this.owner = Thread.currentThread(); - this.usingReturnValue = new UsingReturnValue(); - this.deprecatedMapAcquireContext = new DeprecatedMapAcquireContextOnQuery(); - this.entryValue = new EntryValueBytesValue(); - this.innerReadLock = new ReadLock(); - this.inputSecondValueBytesValue = new InputSecondValueBytesValue(); - this.defaultReturnValue = new DefaultReturnValue(); + this.inputKeyInstanceValue = new InputKeyInstanceValue(); + this.inputKeyBytesValue = new InputKeyBytesValue(); this.inputValueInstanceValue = new InputValueInstanceValue(); - this.entryBytes = CompiledMapQueryContext.this.h().ms.bytes(); - this.entryBytesAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(entryBytes); - this.entryBytesAccessHandle = ((T)(entryBytesAccessor.handle(entryBytes))); - this.entryBytesAccess = ((Access)(entryBytesAccessor.access(entryBytes))); + this.innerWriteLock = new WriteLock(); this.entryKey = new EntryKeyBytesValue(); - this.inputFirstValueBytesValue = new InputFirstValueBytesValue(); + this.usingReturnValue = new UsingReturnValue(); + this.innerUpdateLock = new UpdateLock(); } public class InputKeyBytesValue extends AbstractValue { @Override - public T handle() { - return ((T)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledMapQueryContext.this.inputBytes()).handle(CompiledMapQueryContext.this.inputBytes()))); + public long size() { + return CompiledMapQueryContext.this.inputKeySize(); + } + + public void closeInputKeyBytesValueSizeDependants() { + InputKeyBytesValue.this.closeInputKeyBytesValueGetUsingDependants(); } @Override @@ -193,17 +197,8 @@ public class CompiledMapQueryContext { + @Override + public T handle() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledMapQueryContext.this.entryBytesAccessHandle; + } + + @Override + public ReadAccess access() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledMapQueryContext.this.entryBytesAccess; + } + @Override public long size() { CompiledMapQueryContext.this.checkOnEachPublicOperation(); @@ -314,18 +326,6 @@ public class CompiledMapQueryContext access() { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.entryBytesAccess; - } - - @Override - public T handle() { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.entryBytesAccessHandle; - } } public class WriteLock implements InterProcessLock { @@ -334,22 +334,6 @@ public class CompiledMapQueryContext { @@ -625,6 +625,12 @@ public class CompiledMapQueryContext access() { CompiledMapQueryContext.this.checkOnEachPublicOperation(); return CompiledMapQueryContext.this.entryBytesAccess; } + @Override + public V get() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return cachedEntryValue(); + } + @Override public T handle() { CompiledMapQueryContext.this.checkOnEachPublicOperation(); @@ -671,19 +677,18 @@ public class CompiledMapQueryContext { + @Override + public T handle() { + return ((T)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledMapQueryContext.this.inputBytes()).handle(CompiledMapQueryContext.this.inputBytes()))); + } + @Override public ReadAccess access() { return ((ReadAccess)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledMapQueryContext.this.inputBytes()).access(CompiledMapQueryContext.this.inputBytes()))); @@ -744,22 +749,22 @@ public class CompiledMapQueryContext { @Override public T handle() { return ((T)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledMapQueryContext.this.inputBytes()).handle(CompiledMapQueryContext.this.inputBytes()))); } - } - public class InputSecondValueBytesValue extends AbstractValue { @Override public ReadAccess access() { return ((ReadAccess)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledMapQueryContext.this.inputBytes()).access(CompiledMapQueryContext.this.inputBytes()))); } @Override - public T handle() { - return ((T)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledMapQueryContext.this.inputBytes()).handle(CompiledMapQueryContext.this.inputBytes()))); + public long offset() { + return JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledMapQueryContext.this.inputBytes()).offset(CompiledMapQueryContext.this.inputBytes(), CompiledMapQueryContext.this.secondInputValueOffset()); } @Override @@ -771,11 +776,6 @@ public class CompiledMapQueryContext { @@ -946,6 +946,24 @@ public class CompiledMapQueryContext { @NotNull @Override - public InterProcessLock readLock() { - return CompiledMapQueryContext.this.readLock(); + public InterProcessLock writeLock() { + return CompiledMapQueryContext.this.writeLock(); } @NotNull @@ -1054,26 +1054,8 @@ public class CompiledMapQueryContext entry = CompiledMapQueryContext.this.entry(); + if (entry != null) { + CompiledMapQueryContext.this.remove(entry); + return true; + } else { + return false; + } + } + + @Override + public boolean containsKey() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return (CompiledMapQueryContext.this.entry()) != null; } @Override @@ -1107,9 +1128,8 @@ public class CompiledMapQueryContext entry = CompiledMapQueryContext.this.entry(); - if (entry != null) { - CompiledMapQueryContext.this.remove(entry); - return true; - } else { - return false; - } - } - - @NotNull - @Override - public K key() { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.queriedKey().get(); - } } public enum SearchState { @@ -1386,6 +1386,11 @@ public class CompiledMapQueryContext { @@ -1746,67 +1746,6 @@ public class CompiledMapQueryContext context() { - return this; - } - - public T contextAtIndexInChain(int index) { - return ((T)(contextChain.get(index))); - } - - public void closeVanillaChronicleMapHolderImplContextAtIndexInChainDependants() { - CompiledMapQueryContext.this.closeQuerySegmentStagesTryFindInitLocksOfThisSegmentDependants(); - } - - long sizeOfEverythingBeforeValue(long keySize, long valueSize) { - return (((CompiledMapQueryContext.this.m().metaDataBytes) + (CompiledMapQueryContext.this.m().keySizeMarshaller.sizeEncodingSize(keySize))) + keySize) + (CompiledMapQueryContext.this.m().valueSizeMarshaller.sizeEncodingSize(valueSize)); - } - - public final long entrySize(long keySize, long valueSize) { - long sizeOfEverythingBeforeValue = sizeOfEverythingBeforeValue(keySize, valueSize); - return innerEntrySize(sizeOfEverythingBeforeValue, valueSize); - } - public MVI valueMetaInterop(V value) { return CompiledMapQueryContext.this.m().metaValueInteropProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.m().originalMetaValueInterop, valueInterop, value); } @@ -1826,6 +1765,46 @@ public class CompiledMapQueryContextT contextAtIndexInChain(int index) { + return ((T)(contextChain.get(index))); + } + + public void closeVanillaChronicleMapHolderImplContextAtIndexInChainDependants() { + CompiledMapQueryContext.this.closeQuerySegmentStagesTryFindInitLocksOfThisSegmentDependants(); + } + + public long entryBytesAccessOffset(long offset) { + return entryBytesAccessor.offset(entryBytes, offset); + } + + public void closeMapEntryStagesEntryBytesAccessOffsetDependants() { + CompiledMapQueryContext.this.closeEntry(); + CompiledMapQueryContext.this.closeValue(); + CompiledMapQueryContext.this.closeMapQueryKeyEqualsDependants(); + } + + public void checkAccessingFromOwnerThread() { + if ((owner) != (Thread.currentThread())) { + throw new ConcurrentModificationException("Context shouldn\'t be accessed from multiple threads"); + } + } + + public void closeOwnerThreadHolderCheckAccessingFromOwnerThreadDependants() { + CompiledMapQueryContext.this.closeQueryCheckOnEachPublicOperationCheckOnEachPublicOperationDependants(); + } + + long sizeOfEverythingBeforeValue(long keySize, long valueSize) { + return (((CompiledMapQueryContext.this.m().metaDataBytes) + (CompiledMapQueryContext.this.m().keySizeMarshaller.sizeEncodingSize(keySize))) + keySize) + (CompiledMapQueryContext.this.m().valueSizeMarshaller.sizeEncodingSize(valueSize)); + } + + public MKI keyMetaInterop(K key) { + return CompiledMapQueryContext.this.h().metaKeyInteropProvider.get(CompiledMapQueryContext.this.copies, CompiledMapQueryContext.this.h().originalMetaKeyInterop, keyInterop, key); + } + + public void closeKeyBytesInteropKeyMetaInteropDependants() { + CompiledMapQueryContext.this.inputKeyInstanceValue.closeBuffer(); + } + private CompiledMapQueryContext _Chaining_createChaining() { return new CompiledMapQueryContext(this); } @@ -1847,21 +1826,41 @@ public class CompiledMapQueryContext inputKey = null; public boolean inputKeyInit() { @@ -2116,269 +2132,11 @@ public class CompiledMapQueryContext= 0; - } - - public void initSegmentHashLookup() { - long hashLookupOffset = CompiledMapQueryContext.this.h().segmentOffset(CompiledMapQueryContext.this.segmentIndex()); - innerInitSegmentHashLookup(((CompiledMapQueryContext.this.h().ms.address()) + hashLookupOffset), CompiledMapQueryContext.this.h().segmentHashLookupCapacity, CompiledMapQueryContext.this.h().segmentHashLookupEntrySize, CompiledMapQueryContext.this.h().segmentHashLookupKeyBits, CompiledMapQueryContext.this.h().segmentHashLookupValueBits); - this.closeSegmentHashLookupDependants(); - } - - public void initSegmentHashLookup(long address, long capacity, int entrySize, int keyBits, int valueBits) { - innerInitSegmentHashLookup(address, capacity, entrySize, keyBits, valueBits); - this.closeSegmentHashLookupDependants(); - } - - public int hashLookupEntrySize() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.hashLookupEntrySize; - } - - public int keyBits() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.keyBits; - } - - public long address() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.address; - } - - public long capacityMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.capacityMask; - } - - public long capacityMask2() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.capacityMask2; - } - - public long entryMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.entryMask; - } - - public long keyMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.keyMask; - } - - public long valueMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.valueMask; - } - - public void closeSegmentHashLookup() { - if (!(this.segmentHashLookupInit())) - return ; - - this.closeSegmentHashLookupDependants(); - this.address = -1; - } - - public void closeSegmentHashLookupDependants() { - CompiledMapQueryContext.this.closeHashLookupKeyDependants(); - CompiledMapQueryContext.this.closeHashLookupStepBackDependants(); - CompiledMapQueryContext.this.closeHashLookupEmptyDependants(); - CompiledMapQueryContext.this.closeHashLookupIndexToPosDependants(); - CompiledMapQueryContext.this.closeHashLookupHlPosDependants(); - CompiledMapQueryContext.this.closeHashLookupReadEntryDependants(); - CompiledMapQueryContext.this.closeHashLookupMaskUnsetKeyDependants(); - CompiledMapQueryContext.this.closeHashLookupValueDependants(); - CompiledMapQueryContext.this.closeHashLookupStepDependants(); - } - - void clearEntry(long pos, long prevEntry) { - long entry = prevEntry & (~(entryMask())); - NativeBytes.UNSAFE.putLong(((address()) + pos), entry); - } - - public long key(long entry) { - return entry & (keyMask()); - } - - public void closeHashLookupKeyDependants() { - CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); - } - - public long stepBack(long pos) { - return (pos -= hashLookupEntrySize()) >= 0 ? pos : capacityMask2(); - } - - public void closeHashLookupStepBackDependants() { - CompiledMapQueryContext.this.closeHashLookupSearchFoundDependants(); - } - - void writeEntry(long pos, long prevEntry, long anotherEntry) { - long entry = (prevEntry & (~(entryMask()))) | (anotherEntry & (entryMask())); - NativeBytes.UNSAFE.putLong(((address()) + pos), entry); - } - - public boolean empty(long entry) { - return (entry & (entryMask())) == (UNSET_ENTRY); - } - - public void closeHashLookupEmptyDependants() { - CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - } - - long indexToPos(long index) { - return index * (hashLookupEntrySize()); - } - - public void closeHashLookupIndexToPosDependants() { - CompiledMapQueryContext.this.closeHashLookupHlPosDependants(); - } - - public long hlPos(long key) { - return indexToPos((key & (capacityMask()))); - } - - public void closeHashLookupHlPosDependants() { - CompiledMapQueryContext.this.closeSearchKey(); - } - - public long readEntry(long pos) { - return NativeBytes.UNSAFE.getLong(((address()) + pos)); - } - - public void closeHashLookupReadEntryDependants() { - CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); - } - - public long maskUnsetKey(long key) { - return (key &= keyMask()) != (UNSET_KEY) ? key : keyMask(); - } - - public void closeHashLookupMaskUnsetKeyDependants() { - CompiledMapQueryContext.this.closeSearchKey(); - } - - public long value(long entry) { - return (entry >>> (keyBits())) & (valueMask()); - } - - public void closeHashLookupValueDependants() { - CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); - } - - void forEach(EntryConsumer action) { - for (long pos = 0L ; pos <= (capacityMask2()) ; pos += hashLookupEntrySize()) { - long entry = readEntry(pos); - if (!(empty(entry))) - action.accept(key(entry), value(entry)); - - } - } - - String hashLookupToString() { - final StringBuilder sb = new StringBuilder("{"); - forEach((long key,long value) -> sb.append(key).append('=').append(value).append(',')); - sb.append('}'); - return sb.toString(); - } - - public void checkValueForPut(long value) { - assert (value & (~(valueMask()))) == 0L : "Value out of range, was " + value; - } - - public long step(long pos) { - return (pos += hashLookupEntrySize()) <= (capacityMask2()) ? pos : 0L; - } - - public void closeHashLookupStepDependants() { - CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - } - - public long remove(long posToRemove) { - long entryToRemove = readEntry(posToRemove); - long posToShift = posToRemove; - while (true) { - posToShift = step(posToShift); - long entryToShift = readEntry(posToShift); - if (empty(entryToShift)) - break; - - long insertPos = hlPos(key(entryToShift)); - boolean cond1 = insertPos <= posToRemove; - boolean cond2 = posToRemove <= posToShift; - if ((cond1 && cond2) || ((posToShift < insertPos) && (cond1 || cond2))) { - writeEntry(posToRemove, entryToRemove, entryToShift); - posToRemove = posToShift; - entryToRemove = entryToShift; - } - } - clearEntry(posToRemove, entryToRemove); - return posToRemove; - } - - public void clearHashLookup() { - NativeBytes.UNSAFE.setMemory(address(), ((capacityMask2()) + (hashLookupEntrySize())), ((byte)(0))); - } - - long entry(long key, long value) { - return key | (value << (keyBits())); - } - - public void writeEntryVolatile(long pos, long prevEntry, long key, long value) { - long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); - NativeBytes.UNSAFE.putLongVolatile(null, ((address()) + pos), entry); - } - - public void putValueVolatile(long pos, long value) { - checkValueForPut(value); - long currentEntry = readEntry(pos); - writeEntryVolatile(pos, currentEntry, key(currentEntry), value); - } - - public void writeEntry(long pos, long prevEntry, long key, long value) { - long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); - NativeBytes.UNSAFE.putLong(((address()) + pos), entry); - } - long segmentHeaderAddress; SegmentHeader segmentHeader = null; @@ -2421,16 +2179,8 @@ public class CompiledMapQueryContext= (CompiledMapQueryContext.this.h().actualChunksPerSegment)) + nextPosToSearchFrom = 0L; + + nextPosToSearchFrom(nextPosToSearchFrom); + } + + public long deleted() { + return segmentHeader().deleted(segmentHeaderAddress()); + } + + public long size() { + return (entries()) - (deleted()); + } + + long nextPosToSearchFrom() { + return segmentHeader().nextPosToSearchFrom(segmentHeaderAddress()); + } + + public void deleted(long deleted) { + segmentHeader().deleted(segmentHeaderAddress(), deleted); + } + int totalReadLockCount; int totalUpdateLockCount; @@ -2546,28 +2324,413 @@ public class CompiledMapQueryContext= 0; } - public long size() { - return (entries()) - (deleted()); + public void initSegmentHashLookup() { + long hashLookupOffset = CompiledMapQueryContext.this.h().segmentOffset(CompiledMapQueryContext.this.segmentIndex()); + innerInitSegmentHashLookup(((CompiledMapQueryContext.this.h().ms.address()) + hashLookupOffset), CompiledMapQueryContext.this.h().segmentHashLookupCapacity, CompiledMapQueryContext.this.h().segmentHashLookupEntrySize, CompiledMapQueryContext.this.h().segmentHashLookupKeyBits, CompiledMapQueryContext.this.h().segmentHashLookupValueBits); + this.closeSegmentHashLookupDependants(); } - public void nextPosToSearchFrom(long nextPosToSearchFrom) { - segmentHeader().nextPosToSearchFrom(segmentHeaderAddress(), nextPosToSearchFrom); + public void initSegmentHashLookup(long address, long capacity, int entrySize, int keyBits, int valueBits) { + innerInitSegmentHashLookup(address, capacity, entrySize, keyBits, valueBits); + this.closeSegmentHashLookupDependants(); } - public void updateNextPosToSearchFrom(long allocated, int chunks) { - long nextPosToSearchFrom = allocated + chunks; - if (nextPosToSearchFrom >= (CompiledMapQueryContext.this.h().actualChunksPerSegment)) - nextPosToSearchFrom = 0L; + public int hashLookupEntrySize() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); - nextPosToSearchFrom(nextPosToSearchFrom); + return this.hashLookupEntrySize; + } + + public int keyBits() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.keyBits; + } + + public long address() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.address; + } + + public long capacityMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.capacityMask; + } + + public long capacityMask2() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.capacityMask2; + } + + public long entryMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.entryMask; + } + + public long keyMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.keyMask; + } + + public long valueMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.valueMask; + } + + public void closeSegmentHashLookup() { + if (!(this.segmentHashLookupInit())) + return ; + + this.closeSegmentHashLookupDependants(); + this.address = -1; + } + + public void closeSegmentHashLookupDependants() { + CompiledMapQueryContext.this.closeHashLookupStepBackDependants(); + CompiledMapQueryContext.this.closeHashLookupStepDependants(); + CompiledMapQueryContext.this.closeHashLookupMaskUnsetKeyDependants(); + CompiledMapQueryContext.this.closeHashLookupReadEntryDependants(); + CompiledMapQueryContext.this.closeHashLookupIndexToPosDependants(); + CompiledMapQueryContext.this.closeHashLookupHlPosDependants(); + CompiledMapQueryContext.this.closeHashLookupValueDependants(); + CompiledMapQueryContext.this.closeHashLookupEmptyDependants(); + CompiledMapQueryContext.this.closeHashLookupKeyDependants(); + } + + public long stepBack(long pos) { + return (pos -= hashLookupEntrySize()) >= 0 ? pos : capacityMask2(); + } + + public void closeHashLookupStepBackDependants() { + CompiledMapQueryContext.this.closeHashLookupSearchFoundDependants(); + } + + void clearEntry(long pos, long prevEntry) { + long entry = prevEntry & (~(entryMask())); + NativeBytes.UNSAFE.putLong(((address()) + pos), entry); + } + + public void clearHashLookup() { + NativeBytes.UNSAFE.setMemory(address(), ((capacityMask2()) + (hashLookupEntrySize())), ((byte)(0))); + } + + public void checkValueForPut(long value) { + assert (value & (~(valueMask()))) == 0L : "Value out of range, was " + value; + } + + public long step(long pos) { + return (pos += hashLookupEntrySize()) <= (capacityMask2()) ? pos : 0L; + } + + public void closeHashLookupStepDependants() { + CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + } + + public long maskUnsetKey(long key) { + return (key &= keyMask()) != (UNSET_KEY) ? key : keyMask(); + } + + public void closeHashLookupMaskUnsetKeyDependants() { + CompiledMapQueryContext.this.closeSearchKey(); + } + + public long readEntry(long pos) { + return NativeBytes.UNSAFE.getLong(((address()) + pos)); + } + + public void closeHashLookupReadEntryDependants() { + CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); + } + + long entry(long key, long value) { + return key | (value << (keyBits())); + } + + public void writeEntryVolatile(long pos, long prevEntry, long key, long value) { + long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); + NativeBytes.UNSAFE.putLongVolatile(null, ((address()) + pos), entry); + } + + public void writeEntry(long pos, long prevEntry, long key, long value) { + long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); + NativeBytes.UNSAFE.putLong(((address()) + pos), entry); + } + + long indexToPos(long index) { + return index * (hashLookupEntrySize()); + } + + public void closeHashLookupIndexToPosDependants() { + CompiledMapQueryContext.this.closeHashLookupHlPosDependants(); + } + + public long hlPos(long key) { + return indexToPos((key & (capacityMask()))); + } + + public void closeHashLookupHlPosDependants() { + CompiledMapQueryContext.this.closeSearchKey(); + } + + long searchKey = UNSET_KEY; + + long searchStartPos; + + public boolean searchKeyInit() { + return (this.searchKey) != (UNSET_KEY); + } + + void initSearchKey() { + searchKey = CompiledMapQueryContext.this.maskUnsetKey(CompiledMapQueryContext.this.h().hashSplitting.segmentHash(CompiledMapQueryContext.this.hashOfKey())); + searchStartPos = CompiledMapQueryContext.this.hlPos(searchKey); + this.closeSearchKeyDependants(); + } + + public long searchKey() { + if (!(this.searchKeyInit())) + this.initSearchKey(); + + return this.searchKey; + } + + public long searchStartPos() { + if (!(this.searchKeyInit())) + this.initSearchKey(); + + return this.searchStartPos; + } + + public void closeSearchKey() { + if (!(this.searchKeyInit())) + return ; + + this.closeSearchKeyDependants(); + this.searchKey = UNSET_KEY; + } + + public void closeSearchKeyDependants() { + CompiledMapQueryContext.this.closeHashLookupPos(); + CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); + } + + public long hashLookupPos = -1; + + public boolean hashLookupPosInit() { + return (this.hashLookupPos) >= 0; + } + + public void initHashLookupPos() { + CompiledMapQueryContext.this.innerReadLock.lock(); + this.hashLookupPos = CompiledMapQueryContext.this.searchStartPos(); + this.closeHashLookupPosDependants(); + } + + public void initHashLookupPos(long hashLookupPos) { + this.hashLookupPos = hashLookupPos; + this.closeHashLookupPosDependants(); + } + + public long hashLookupPos() { + if (!(this.hashLookupPosInit())) + this.initHashLookupPos(); + + return this.hashLookupPos; + } + + public void closeHashLookupPos() { + if (!(this.hashLookupPosInit())) + return ; + + this.closeHashLookupPosDependants(); + this.hashLookupPos = -1; + } + + public void closeHashLookupPosDependants() { + CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); + CompiledMapQueryContext.this.closeHashLookupSearchFoundDependants(); + } + + void put(long value) { + CompiledMapQueryContext.this.checkValueForPut(value); + CompiledMapQueryContext.this.writeEntry(CompiledMapQueryContext.this.hashLookupPos(), CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos()), searchKey(), value); + } + + public void putNewVolatile(long value) { + CompiledMapQueryContext.this.checkValueForPut(value); + long currentEntry = CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos()); + CompiledMapQueryContext.this.writeEntryVolatile(CompiledMapQueryContext.this.hashLookupPos(), currentEntry, searchKey(), value); + } + + public void found() { + CompiledMapQueryContext.this.setHashLookupPosGuarded(CompiledMapQueryContext.this.stepBack(CompiledMapQueryContext.this.hashLookupPos())); + } + + public void closeHashLookupSearchFoundDependants() { + CompiledMapQueryContext.this.closeKeySearch(); + } + + public long value(long entry) { + return (entry >>> (keyBits())) & (valueMask()); + } + + public void closeHashLookupValueDependants() { + CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); + } + + public boolean empty(long entry) { + return (entry & (entryMask())) == (UNSET_ENTRY); + } + + public void closeHashLookupEmptyDependants() { + CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + } + + public boolean checkSlotIsEmpty() { + return CompiledMapQueryContext.this.empty(CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos())); + } + + public long key(long entry) { + return entry & (keyMask()); + } + + public void closeHashLookupKeyDependants() { + CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); + } + + public void putVolatile(long value) { + CompiledMapQueryContext.this.checkValueForPut(value); + long currentEntry = CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos()); + assert (CompiledMapQueryContext.this.key(currentEntry)) == (searchKey()); + CompiledMapQueryContext.this.writeEntryVolatile(CompiledMapQueryContext.this.hashLookupPos(), currentEntry, searchKey(), value); + } + + void forEach(EntryConsumer action) { + for (long pos = 0L ; pos <= (capacityMask2()) ; pos += hashLookupEntrySize()) { + long entry = readEntry(pos); + if (!(empty(entry))) + action.accept(key(entry), value(entry)); + + } + } + + String hashLookupToString() { + final StringBuilder sb = new StringBuilder("{"); + forEach((long key,long value) -> sb.append(key).append('=').append(value).append(',')); + sb.append('}'); + return sb.toString(); + } + + public long nextPos() { + long pos = CompiledMapQueryContext.this.hashLookupPos(); + while (true) { + long entry = CompiledMapQueryContext.this.readEntry(pos); + if (CompiledMapQueryContext.this.empty(entry)) { + CompiledMapQueryContext.this.setHashLookupPosGuarded(pos); + return -1L; + } + pos = CompiledMapQueryContext.this.step(pos); + if (pos == (searchStartPos())) + break; + + if ((CompiledMapQueryContext.this.key(entry)) == (searchKey())) { + CompiledMapQueryContext.this.setHashLookupPosGuarded(pos); + return CompiledMapQueryContext.this.value(entry); + } + } + throw new IllegalStateException(("MultiMap is full, that most likely means you " + ("misconfigured entrySize/chunkSize, and entries tend to take less chunks than " + "expected"))); + } + + public void closeHashLookupSearchNextPosDependants() { + CompiledMapQueryContext.this.closeKeySearch(); + } + + public boolean checkSlotContainsExpectedKeyAndValue(long value) { + long entry = CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos()); + return ((CompiledMapQueryContext.this.key(entry)) == (searchKey())) && ((CompiledMapQueryContext.this.value(entry)) == value); + } + + public void closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants() { + CompiledMapQueryContext.this.closeMapQueryDropSearchIfNestedContextsAndPresentHashLookupSlotCheckFailedDependants(); + } + + public void putValueVolatile(long pos, long value) { + checkValueForPut(value); + long currentEntry = readEntry(pos); + writeEntryVolatile(pos, currentEntry, key(currentEntry), value); + } + + void writeEntry(long pos, long prevEntry, long anotherEntry) { + long entry = (prevEntry & (~(entryMask()))) | (anotherEntry & (entryMask())); + NativeBytes.UNSAFE.putLong(((address()) + pos), entry); + } + + public long remove(long posToRemove) { + long entryToRemove = readEntry(posToRemove); + long posToShift = posToRemove; + while (true) { + posToShift = step(posToShift); + long entryToShift = readEntry(posToShift); + if (empty(entryToShift)) + break; + + long insertPos = hlPos(key(entryToShift)); + boolean cond1 = insertPos <= posToRemove; + boolean cond2 = posToRemove <= posToShift; + if ((cond1 && cond2) || ((posToShift < insertPos) && (cond1 || cond2))) { + writeEntry(posToRemove, entryToRemove, entryToShift); + posToRemove = posToShift; + entryToRemove = entryToShift; + } + } + clearEntry(posToRemove, entryToRemove); + return posToRemove; + } + + public void remove() { + CompiledMapQueryContext.this.setHashLookupPosGuarded(CompiledMapQueryContext.this.remove(CompiledMapQueryContext.this.hashLookupPos())); } long entrySpaceOffset = 0; @@ -2635,36 +2798,6 @@ public class CompiledMapQueryContext h = CompiledMapQueryContext.this.h(); - if (chunks > (h.maxChunksPerEntry)) - throw new IllegalArgumentException((((("Entry is too large: requires " + chunks) + " entry size chucks, ") + (h.maxChunksPerEntry)) + " is maximum.")); - - long ret = freeList().setNextNContinuousClearBits(nextPosToSearchFrom(), chunks); - if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { - if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) - freeList().clear(ret, h.actualChunksPerSegment); - - ret = freeList().setNextNContinuousClearBits(0L, chunks); - if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { - if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) - freeList().clear(ret, h.actualChunksPerSegment); - - if (chunks == 1) { - throw new IllegalStateException("Segment is full, no free entries found"); - } else { - throw new IllegalStateException((("Segment is full or has no ranges of " + chunks) + " continuous free chunks")); - } - } - updateNextPosToSearchFrom(ret, chunks); - } else { - if ((chunks == 1) || (freeList().isSet(nextPosToSearchFrom()))) { - updateNextPosToSearchFrom(ret, chunks); - } - } - return ret; - } - public long pos = -1; public long keySizeOffset; @@ -2831,9 +2964,9 @@ public class CompiledMapQueryContext value) { + initValue(value); + freeExtraAllocatedChunks(); + CompiledMapQueryContext.this.putValueVolatile(CompiledMapQueryContext.this.hashLookupPos(), pos()); + } + public long newSizeOfEverythingBeforeValue(Value newValue) { return ((valueSizeOffset()) + (CompiledMapQueryContext.this.m().valueSizeMarshaller.sizeEncodingSize(newValue.size()))) - (keySizeOffset()); } @@ -2894,151 +3041,34 @@ public class CompiledMapQueryContext h = CompiledMapQueryContext.this.h(); + if (chunks > (h.maxChunksPerEntry)) + throw new IllegalArgumentException((((("Entry is too large: requires " + chunks) + " entry size chucks, ") + (h.maxChunksPerEntry)) + " is maximum.")); - long searchStartPos; + long ret = freeList().setNextNContinuousClearBits(nextPosToSearchFrom(), chunks); + if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { + if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) + freeList().clear(ret, h.actualChunksPerSegment); - public boolean searchKeyInit() { - return (this.searchKey) != (UNSET_KEY); - } + ret = freeList().setNextNContinuousClearBits(0L, chunks); + if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { + if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) + freeList().clear(ret, h.actualChunksPerSegment); - void initSearchKey() { - searchKey = CompiledMapQueryContext.this.maskUnsetKey(CompiledMapQueryContext.this.h().hashSplitting.segmentHash(CompiledMapQueryContext.this.hashOfKey())); - searchStartPos = CompiledMapQueryContext.this.hlPos(searchKey); - this.closeSearchKeyDependants(); - } - - public long searchKey() { - if (!(this.searchKeyInit())) - this.initSearchKey(); - - return this.searchKey; - } - - public long searchStartPos() { - if (!(this.searchKeyInit())) - this.initSearchKey(); - - return this.searchStartPos; - } - - public void closeSearchKey() { - if (!(this.searchKeyInit())) - return ; - - this.closeSearchKeyDependants(); - this.searchKey = UNSET_KEY; - } - - public void closeSearchKeyDependants() { - CompiledMapQueryContext.this.closeHashLookupPos(); - CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); - } - - public long hashLookupPos = -1; - - public boolean hashLookupPosInit() { - return (this.hashLookupPos) >= 0; - } - - public void initHashLookupPos() { - CompiledMapQueryContext.this.innerReadLock.lock(); - this.hashLookupPos = CompiledMapQueryContext.this.searchStartPos(); - this.closeHashLookupPosDependants(); - } - - public void initHashLookupPos(long hashLookupPos) { - this.hashLookupPos = hashLookupPos; - this.closeHashLookupPosDependants(); - } - - public long hashLookupPos() { - if (!(this.hashLookupPosInit())) - this.initHashLookupPos(); - - return this.hashLookupPos; - } - - public void closeHashLookupPos() { - if (!(this.hashLookupPosInit())) - return ; - - this.closeHashLookupPosDependants(); - this.hashLookupPos = -1; - } - - public void closeHashLookupPosDependants() { - CompiledMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - CompiledMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); - CompiledMapQueryContext.this.closeHashLookupSearchFoundDependants(); - } - - public void remove() { - CompiledMapQueryContext.this.setHashLookupPosGuarded(CompiledMapQueryContext.this.remove(CompiledMapQueryContext.this.hashLookupPos())); - } - - public void putNewVolatile(long value) { - CompiledMapQueryContext.this.checkValueForPut(value); - long currentEntry = CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos()); - CompiledMapQueryContext.this.writeEntryVolatile(CompiledMapQueryContext.this.hashLookupPos(), currentEntry, searchKey(), value); - } - - public long nextPos() { - long pos = CompiledMapQueryContext.this.hashLookupPos(); - while (true) { - long entry = CompiledMapQueryContext.this.readEntry(pos); - if (CompiledMapQueryContext.this.empty(entry)) { - CompiledMapQueryContext.this.setHashLookupPosGuarded(pos); - return -1L; + if (chunks == 1) { + throw new IllegalStateException("Segment is full, no free entries found"); + } else { + throw new IllegalStateException((("Segment is full or has no ranges of " + chunks) + " continuous free chunks")); + } } - pos = CompiledMapQueryContext.this.step(pos); - if (pos == (searchStartPos())) - break; - - if ((CompiledMapQueryContext.this.key(entry)) == (searchKey())) { - CompiledMapQueryContext.this.setHashLookupPosGuarded(pos); - return CompiledMapQueryContext.this.value(entry); + updateNextPosToSearchFrom(ret, chunks); + } else { + if ((chunks == 1) || (freeList().isSet(nextPosToSearchFrom()))) { + updateNextPosToSearchFrom(ret, chunks); } } - throw new IllegalStateException(("MultiMap is full, that most likely means you " + ("misconfigured entrySize/chunkSize, and entries tend to take less chunks than " + "expected"))); - } - - public void closeHashLookupSearchNextPosDependants() { - CompiledMapQueryContext.this.closeKeySearch(); - } - - public void putVolatile(long value) { - CompiledMapQueryContext.this.checkValueForPut(value); - long currentEntry = CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos()); - assert (CompiledMapQueryContext.this.key(currentEntry)) == (searchKey()); - CompiledMapQueryContext.this.writeEntryVolatile(CompiledMapQueryContext.this.hashLookupPos(), currentEntry, searchKey(), value); - } - - public boolean checkSlotContainsExpectedKeyAndValue(long value) { - long entry = CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos()); - return ((CompiledMapQueryContext.this.key(entry)) == (searchKey())) && ((CompiledMapQueryContext.this.value(entry)) == value); - } - - public void closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants() { - CompiledMapQueryContext.this.closeMapQueryDropSearchIfNestedContextsAndPresentHashLookupSlotCheckFailedDependants(); - } - - public boolean checkSlotIsEmpty() { - return CompiledMapQueryContext.this.empty(CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos())); - } - - public void found() { - CompiledMapQueryContext.this.setHashLookupPosGuarded(CompiledMapQueryContext.this.stepBack(CompiledMapQueryContext.this.hashLookupPos())); - } - - public void closeHashLookupSearchFoundDependants() { - CompiledMapQueryContext.this.closeKeySearch(); - } - - void put(long value) { - CompiledMapQueryContext.this.checkValueForPut(value); - CompiledMapQueryContext.this.writeEntry(CompiledMapQueryContext.this.hashLookupPos(), CompiledMapQueryContext.this.readEntry(CompiledMapQueryContext.this.hashLookupPos()), searchKey(), value); + return ret; } boolean keyEquals() { @@ -3088,31 +3118,10 @@ public class CompiledMapQueryContext defaultValue() { + public InterProcessLock updateLock() { CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return wrapValueAsValue(CompiledMapQueryContext.this.m().defaultValue(CompiledMapQueryContext.this.deprecatedMapKeyContext)); + return CompiledMapQueryContext.this.innerUpdateLock; } @Override - public R insert(@NotNull - MapAbsentEntry absentEntry, Value value) { + public Value defaultValue(@NotNull + MapAbsentEntry absentEntry) { CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.m().entryOperations.insert(absentEntry, value); + return CompiledMapQueryContext.this.m().entryOperations.defaultValue(absentEntry); } @Override @@ -3173,6 +3182,53 @@ public class CompiledMapQueryContext key() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledMapQueryContext.this.entryKey; + } + + @NotNull + @Override + public Value defaultValue() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledMapQueryContext.this.wrapValueAsValue(CompiledMapQueryContext.this.m().defaultValue(CompiledMapQueryContext.this.deprecatedMapKeyContext)); + } + + @Override + public R remove(@NotNull + MapEntry entry) { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledMapQueryContext.this.m().entryOperations.remove(entry); + } + + @NotNull + @Override + public InterProcessLock readLock() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledMapQueryContext.this.innerReadLock; + } + + @NotNull + @Override + public MapContext context() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return this; + } + + @NotNull + @Override + public Value value() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledMapQueryContext.this.entryValue; + } + + public Value queriedKey() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + return inputKey(); + } + @Override public Value wrapValueAsValue(V value) { CompiledMapQueryContext.this.checkOnEachPublicOperation(); @@ -3184,42 +3240,31 @@ public class CompiledMapQueryContext absentKey() { CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.innerUpdateLock; - } - - @NotNull - @Override - public Value value() { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.entryValue; + return CompiledMapQueryContext.this.inputKey(); } @Override - public Value defaultValue(@NotNull - MapAbsentEntry absentEntry) { + public R insert(@NotNull + MapAbsentEntry absentEntry, Value value) { CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.m().entryOperations.defaultValue(absentEntry); + return CompiledMapQueryContext.this.m().entryOperations.insert(absentEntry, value); } - @NotNull + public boolean searchStatePresent() { + return (searchState()) == (SearchState.PRESENT); + } + + public boolean entryPresent() { + return searchStatePresent(); + } + + @Nullable @Override - public InterProcessLock readLock() { + public MapAbsentEntry absentEntry() { CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.innerReadLock; - } - - @NotNull - @Override - public Value key() { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.entryKey; - } - - public Value queriedKey() { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return inputKey(); + return entryPresent() ? null : CompiledMapQueryContext.this; } @Override @@ -3228,14 +3273,11 @@ public class CompiledMapQueryContext entry) { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledMapQueryContext.this.m().entryOperations.remove(entry); + public boolean searchStateAbsent() { + return (!(searchStatePresent())) && (!(searchStateDeleted())); } - private void putPrefix() { + protected void putPrefix() { CompiledMapQueryContext.this.checkOnEachPublicOperation(); boolean underUpdatedLockIsHeld = !(CompiledMapQueryContext.this.innerUpdateLock.isHeldByCurrentThread()); if (underUpdatedLockIsHeld) @@ -3247,144 +3289,21 @@ public class CompiledMapQueryContext absentEntry() { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return entryPresent() ? null : this; + private void _AllocatedChunks_incrementSegmentEntriesIfNeeded() { } - @Override - public void doRemove() { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - CompiledMapQueryContext.this.innerUpdateLock.lock(); - if (searchStatePresent()) { - CompiledMapQueryContext.this.innerWriteLock.lock(); - CompiledMapQueryContext.this.remove(); - CompiledMapQueryContext.this.innerRemoveEntryExceptHashLookupUpdate(); - setSearchStateGuarded(SearchState.DELETED); - } else { - throw new IllegalStateException("Entry is absent when doRemove() is called"); + public void incrementSegmentEntriesIfNeeded() { + if ((CompiledMapQueryContext.this.searchState()) != (SearchState.PRESENT)) { + CompiledMapQueryContext.this.entries(((CompiledMapQueryContext.this.entries()) + 1L)); } } - @NotNull - @Override - public Value absentKey() { - CompiledMapQueryContext.this.checkOnEachPublicOperation(); - return inputKey(); - } - - public int allocatedChunks = 0; - - public boolean allocatedChunksInit() { - return (this.allocatedChunks) != 0; - } - - public void initAllocatedChunks(int allocatedChunks) { - this.allocatedChunks = allocatedChunks; - } - - public int allocatedChunks() { - assert this.allocatedChunksInit() : "AllocatedChunks should be init"; - return this.allocatedChunks; - } - - public void closeAllocatedChunks() { - if (!(this.allocatedChunksInit())) - return ; - - this.allocatedChunks = 0; - } - - public final void freeExtraAllocatedChunks() { - if (((!(CompiledMapQueryContext.this.m().constantlySizedEntry)) && (CompiledMapQueryContext.this.m().couldNotDetermineAlignmentBeforeAllocation)) && ((entrySizeInChunks()) < (CompiledMapQueryContext.this.allocatedChunks()))) { - CompiledMapQueryContext.this.free(((pos()) + (entrySizeInChunks())), ((CompiledMapQueryContext.this.allocatedChunks()) - (entrySizeInChunks()))); - } else { - initTheEntrySizeInChunks(CompiledMapQueryContext.this.allocatedChunks()); - } - } - - public void writeValueAndPutPos(Value value) { - initValue(value); - freeExtraAllocatedChunks(); - CompiledMapQueryContext.this.putValueVolatile(CompiledMapQueryContext.this.hashLookupPos(), pos()); - } - - public void initEntryAndKey(long entrySize) { - initAllocatedChunks(CompiledMapQueryContext.this.h().inChunks(entrySize)); - CompiledMapQueryContext.this.initEntry(CompiledMapQueryContext.this.alloc(allocatedChunks()), CompiledMapQueryContext.this.inputKey()); - incrementSegmentEntriesIfNeeded(); - } - - void putEntry(Value value) { - assert searchStateAbsent(); - long entrySize = CompiledMapQueryContext.this.entrySize(inputKey().size(), value.size()); - CompiledMapQueryContext.this.initEntryAndKey(entrySize); - CompiledMapQueryContext.this.initValue(value); - CompiledMapQueryContext.this.freeExtraAllocatedChunks(); - CompiledMapQueryContext.this.putNewVolatile(CompiledMapQueryContext.this.pos()); - } - public void initEntryAndKeyCopying(long entrySize, long bytesToCopy) { initAllocatedChunks(CompiledMapQueryContext.this.h().inChunks(entrySize)); CompiledMapQueryContext.this.initEntryCopying(CompiledMapQueryContext.this.alloc(allocatedChunks()), bytesToCopy); incrementSegmentEntriesIfNeeded(); } - protected void relocation(Value newValue, long newSizeOfEverythingBeforeValue) { - CompiledMapQueryContext.this.free(pos(), entrySizeInChunks()); - long entrySize = innerEntrySize(newSizeOfEverythingBeforeValue, newValue.size()); - CompiledMapQueryContext.this.initEntryAndKeyCopying(entrySize, ((valueSizeOffset()) - (keySizeOffset()))); - writeValueAndPutPos(newValue); - } - - public void innerDefaultReplaceValue(Value newValue) { - assert CompiledMapQueryContext.this.innerUpdateLock.isHeldByCurrentThread(); - boolean newValueSizeIsDifferent = (newValue.size()) != (this.valueSize()); - if (newValueSizeIsDifferent) { - long newSizeOfEverythingBeforeValue = newSizeOfEverythingBeforeValue(newValue); - long entryStartOffset = keySizeOffset(); - VanillaChronicleMap m = CompiledMapQueryContext.this.m(); - long newValueOffset = m.alignment.alignAddr((entryStartOffset + newSizeOfEverythingBeforeValue)); - long newEntrySize = (newValueOffset + (newValue.size())) - entryStartOffset; - int newSizeInChunks = m.inChunks(newEntrySize); - newValueDoesNotFit : if (newSizeInChunks > (entrySizeInChunks())) { - if (newSizeInChunks > (m.maxChunksPerEntry)) { - throw new IllegalArgumentException(((((("Value too large: " + "entry takes ") + newSizeInChunks) + " chunks, ") + (m.maxChunksPerEntry)) + " is maximum.")); - } - if (CompiledMapQueryContext.this.freeList().allClear(((pos()) + (entrySizeInChunks())), ((pos()) + newSizeInChunks))) { - CompiledMapQueryContext.this.freeList().set(((pos()) + (entrySizeInChunks())), ((pos()) + newSizeInChunks)); - break newValueDoesNotFit; - } - relocation(newValue, newSizeOfEverythingBeforeValue); - return ; - } else if (newSizeInChunks < (entrySizeInChunks())) { - CompiledMapQueryContext.this.freeList().clear(((pos()) + newSizeInChunks), ((pos()) + (entrySizeInChunks()))); - } - } else { - } - CompiledMapQueryContext.this.innerWriteLock.lock(); - if (newValueSizeIsDifferent) { - initValueAgain(newValue); - } else { - writeValueGuarded(newValue); - } - CompiledMapQueryContext.this.putValueVolatile(CompiledMapQueryContext.this.hashLookupPos(), pos()); - } - - @Override - public void doReplaceValue(Value newValue) { - putPrefix(); - if (searchStatePresent()) { - CompiledMapQueryContext.this.innerDefaultReplaceValue(newValue); - CompiledMapQueryContext.this.incrementModCountGuarded(); - setSearchStateGuarded(SearchState.PRESENT); - } else { - throw new IllegalStateException("Entry is absent in the map when doReplaceValue() is called"); - } - } - public void putValueDeletedEntry(Value newValue) { assert CompiledMapQueryContext.this.innerUpdateLock.isHeldByCurrentThread(); int newSizeInChunks; @@ -3430,19 +3349,101 @@ public class CompiledMapQueryContext newValue, long newSizeOfEverythingBeforeValue) { + CompiledMapQueryContext.this.free(pos(), entrySizeInChunks()); + long entrySize = innerEntrySize(newSizeOfEverythingBeforeValue, newValue.size()); + CompiledMapQueryContext.this.initEntryAndKeyCopying(entrySize, ((valueSizeOffset()) - (keySizeOffset()))); + writeValueAndPutPos(newValue); + } + + public void innerDefaultReplaceValue(Value newValue) { + assert CompiledMapQueryContext.this.innerUpdateLock.isHeldByCurrentThread(); + boolean newValueSizeIsDifferent = (newValue.size()) != (this.valueSize()); + if (newValueSizeIsDifferent) { + long newSizeOfEverythingBeforeValue = newSizeOfEverythingBeforeValue(newValue); + long entryStartOffset = keySizeOffset(); + VanillaChronicleMap m = CompiledMapQueryContext.this.m(); + long newValueOffset = m.alignment.alignAddr((entryStartOffset + newSizeOfEverythingBeforeValue)); + long newEntrySize = (newValueOffset + (newValue.size())) - entryStartOffset; + int newSizeInChunks = m.inChunks(newEntrySize); + newValueDoesNotFit : if (newSizeInChunks > (entrySizeInChunks())) { + if (newSizeInChunks > (m.maxChunksPerEntry)) { + throw new IllegalArgumentException(((((("Value too large: " + "entry takes ") + newSizeInChunks) + " chunks, ") + (m.maxChunksPerEntry)) + " is maximum.")); + } + if (CompiledMapQueryContext.this.freeList().allClear(((pos()) + (entrySizeInChunks())), ((pos()) + newSizeInChunks))) { + CompiledMapQueryContext.this.freeList().set(((pos()) + (entrySizeInChunks())), ((pos()) + newSizeInChunks)); + break newValueDoesNotFit; + } + relocation(newValue, newSizeOfEverythingBeforeValue); + return ; + } else if (newSizeInChunks < (entrySizeInChunks())) { + CompiledMapQueryContext.this.freeList().clear(((pos()) + newSizeInChunks), ((pos()) + (entrySizeInChunks()))); + } + } else { + } + CompiledMapQueryContext.this.innerWriteLock.lock(); + if (newValueSizeIsDifferent) { + initValueAgain(newValue); + } else { + writeValueGuarded(newValue); + } + CompiledMapQueryContext.this.putValueVolatile(CompiledMapQueryContext.this.hashLookupPos(), pos()); + } + + public void initEntryAndKey(long entrySize) { + initAllocatedChunks(CompiledMapQueryContext.this.h().inChunks(entrySize)); + CompiledMapQueryContext.this.initEntry(CompiledMapQueryContext.this.alloc(allocatedChunks()), CompiledMapQueryContext.this.inputKey()); + incrementSegmentEntriesIfNeeded(); + } + + void putEntry(Value value) { + assert CompiledMapQueryContext.this.searchStateAbsent(); + long entrySize = CompiledMapQueryContext.this.entrySize(CompiledMapQueryContext.this.inputKey().size(), value.size()); + CompiledMapQueryContext.this.initEntryAndKey(entrySize); + CompiledMapQueryContext.this.initValue(value); + CompiledMapQueryContext.this.freeExtraAllocatedChunks(); + CompiledMapQueryContext.this.putNewVolatile(CompiledMapQueryContext.this.pos()); + } + @Override public void doInsert(Value value) { - putPrefix(); - if (!(searchStatePresent())) { - if (searchStateDeleted()) { + CompiledMapQueryContext.this.putPrefix(); + if (!(CompiledMapQueryContext.this.searchStatePresent())) { + if (CompiledMapQueryContext.this.searchStateDeleted()) { CompiledMapQueryContext.this.putValueDeletedEntry(value); } else { putEntry(value); } CompiledMapQueryContext.this.incrementModCountGuarded(); - setSearchStateGuarded(SearchState.PRESENT); + CompiledMapQueryContext.this.setSearchStateGuarded(SearchState.PRESENT); } else { throw new IllegalStateException("Entry is present in the map when doInsert() is called"); } } + + @Override + public void doReplaceValue(Value newValue) { + putPrefix(); + if (searchStatePresent()) { + CompiledMapQueryContext.this.innerDefaultReplaceValue(newValue); + CompiledMapQueryContext.this.incrementModCountGuarded(); + setSearchStateGuarded(SearchState.PRESENT); + } else { + throw new IllegalStateException("Entry is absent in the map when doReplaceValue() is called"); + } + } + + @Override + public void doRemove() { + CompiledMapQueryContext.this.checkOnEachPublicOperation(); + CompiledMapQueryContext.this.innerUpdateLock.lock(); + if (searchStatePresent()) { + CompiledMapQueryContext.this.innerWriteLock.lock(); + CompiledMapQueryContext.this.remove(); + CompiledMapQueryContext.this.innerRemoveEntryExceptHashLookupUpdate(); + setSearchStateGuarded(SearchState.DELETED); + } else { + throw new IllegalStateException("Entry is absent when doRemove() is called"); + } + } } diff --git a/src/main/java/net/openhft/chronicle/map/impl/CompiledReplicatedMapIterationContext.java b/src/main/java/net/openhft/chronicle/map/impl/CompiledReplicatedMapIterationContext.java index b76f02fa2..00e308a3f 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/CompiledReplicatedMapIterationContext.java +++ b/src/main/java/net/openhft/chronicle/map/impl/CompiledReplicatedMapIterationContext.java @@ -47,19 +47,19 @@ import java.util.function.Predicate; public class CompiledReplicatedMapIterationContext, V, VI, MVI extends MetaBytesInterop, R, T> implements AutoCloseable , HashEntry , InterProcessReadWriteUpdateLock , RemoteOperationContext , MapContext , MapEntry , IterationContextInterface , ReplicatedChronicleMapHolder , MapReplicableEntry { public void close() { - CompiledReplicatedMapIterationContext.this.closeReplicationUpdate(); - CompiledReplicatedMapIterationContext.this.closeHashLookupPos(); - CompiledReplicatedMapIterationContext.this.wrappedValueInstanceValue.closeValue(); - CompiledReplicatedMapIterationContext.this.closeAllocatedChunks(); - CompiledReplicatedMapIterationContext.this.wrappedValueInstanceValue.closeNext(); - CompiledReplicatedMapIterationContext.this.closeTheSegmentIndex(); - CompiledReplicatedMapIterationContext.this.closeEntryRemovedOnThisIteration(); CompiledReplicatedMapIterationContext.this.closeUsed(); + CompiledReplicatedMapIterationContext.this.closeAllocatedChunks(); + CompiledReplicatedMapIterationContext.this.closeReplicationUpdate(); + CompiledReplicatedMapIterationContext.this.wrappedValueInstanceValue.closeValue(); + CompiledReplicatedMapIterationContext.this.closeEntryRemovedOnThisIteration(); + CompiledReplicatedMapIterationContext.this.closeTheSegmentIndex(); + CompiledReplicatedMapIterationContext.this.closeHashLookupPos(); + CompiledReplicatedMapIterationContext.this.wrappedValueInstanceValue.closeNext(); + CompiledReplicatedMapIterationContext.this.closeOwnerThreadHolderCheckAccessingFromOwnerThreadDependants(); CompiledReplicatedMapIterationContext.this.closeReplicatedMapEntryStagesEntryBytesAccessOffsetDependants(); - CompiledReplicatedMapIterationContext.this.closeMapSegmentIterationCheckEntryNotRemovedOnThisIterationDependants(); CompiledReplicatedMapIterationContext.this.closeReplicatedChronicleMapHolderImplContextAtIndexInChainDependants(); CompiledReplicatedMapIterationContext.this.closeValueBytesInteropValueMetaInteropDependants(); - CompiledReplicatedMapIterationContext.this.closeOwnerThreadHolderCheckAccessingFromOwnerThreadDependants(); + CompiledReplicatedMapIterationContext.this.closeReplicatedMapSegmentIterationCheckEntryNotRemovedOnThisIterationDependants(); } public void incrementModCountGuarded() { @@ -98,23 +98,24 @@ public class CompiledReplicatedMapIterationContext)(entryBytesAccessor.access(entryBytes))); this.copies = ThreadLocalCopies.get(); this.valueInterop = CompiledReplicatedMapIterationContext.this.m().valueInteropProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.m().originalValueInterop); this.valueReader = CompiledReplicatedMapIterationContext.this.m().valueReaderProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.m().originalValueReader); this.keyInterop = CompiledReplicatedMapIterationContext.this.h().keyInteropProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.h().originalKeyInterop); this.keyReader = CompiledReplicatedMapIterationContext.this.h().keyReaderProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.h().originalKeyReader); - this.innerReadLock = new ReadLock(); - this.entryBytes = CompiledReplicatedMapIterationContext.this.h().ms.bytes(); - this.entryBytesAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(entryBytes); - this.entryBytesAccessHandle = ((T)(entryBytesAccessor.handle(entryBytes))); - this.entryBytesAccess = ((Access)(entryBytesAccessor.access(entryBytes))); - this.wrappedValueInstanceValue = new WrappedValueInstanceValue(); this.entryValue = new EntryValueBytesValue(); - this.innerUpdateLock = new UpdateLock(); this.deprecatedMapKeyContextOnIteration = new DeprecatedMapKeyContextOnIteration(); - this.entryKey = new EntryKeyBytesValue(); + this.owner = Thread.currentThread(); + this.wrappedValueInstanceValue = new WrappedValueInstanceValue(); this.innerWriteLock = new WriteLock(); + this.dummyValue = new DummyValueZeroValue(); + this.innerReadLock = new ReadLock(); + this.innerUpdateLock = new UpdateLock(); } public CompiledReplicatedMapIterationContext(CompiledReplicatedMapIterationContext c) { @@ -122,23 +123,24 @@ public class CompiledReplicatedMapIterationContext)(c.m)); - this.owner = Thread.currentThread(); + this.entryKey = new EntryKeyBytesValue(); + this.entryBytes = CompiledReplicatedMapIterationContext.this.h().ms.bytes(); + this.entryBytesAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(entryBytes); + this.entryBytesAccessHandle = ((T)(entryBytesAccessor.handle(entryBytes))); + this.entryBytesAccess = ((Access)(entryBytesAccessor.access(entryBytes))); this.copies = ThreadLocalCopies.get(); this.valueInterop = CompiledReplicatedMapIterationContext.this.m().valueInteropProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.m().originalValueInterop); this.valueReader = CompiledReplicatedMapIterationContext.this.m().valueReaderProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.m().originalValueReader); this.keyInterop = CompiledReplicatedMapIterationContext.this.h().keyInteropProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.h().originalKeyInterop); this.keyReader = CompiledReplicatedMapIterationContext.this.h().keyReaderProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.h().originalKeyReader); - this.innerReadLock = new ReadLock(); - this.entryBytes = CompiledReplicatedMapIterationContext.this.h().ms.bytes(); - this.entryBytesAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(entryBytes); - this.entryBytesAccessHandle = ((T)(entryBytesAccessor.handle(entryBytes))); - this.entryBytesAccess = ((Access)(entryBytesAccessor.access(entryBytes))); - this.wrappedValueInstanceValue = new WrappedValueInstanceValue(); this.entryValue = new EntryValueBytesValue(); - this.innerUpdateLock = new UpdateLock(); this.deprecatedMapKeyContextOnIteration = new DeprecatedMapKeyContextOnIteration(); - this.entryKey = new EntryKeyBytesValue(); + this.owner = Thread.currentThread(); + this.wrappedValueInstanceValue = new WrappedValueInstanceValue(); this.innerWriteLock = new WriteLock(); + this.dummyValue = new DummyValueZeroValue(); + this.innerReadLock = new ReadLock(); + this.innerUpdateLock = new UpdateLock(); } public class EntryKeyBytesValue extends AbstractValue { @@ -154,6 +156,12 @@ public class CompiledReplicatedMapIterationContext { @@ -505,12 +507,6 @@ public class CompiledReplicatedMapIterationContext { - @Override - public void close() { - throw new UnsupportedOperationException("close() is not supported during iteration"); - } - - @NotNull - @Override - public K key() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.key().get(); - } - - @Override - public boolean containsKey() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return true; - } - - @Override - public boolean valueEqualTo(V value) { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return Value.bytesEquivalent(CompiledReplicatedMapIterationContext.this.entryValue, CompiledReplicatedMapIterationContext.this.context().wrapValueAsValue(value)); - } - - @Override - public V getUsing(V usingValue) { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.value().getUsing(usingValue); - } - - @Override - public V get() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.value().get(); - } - - @Override - public boolean put(V newValue) { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - CompiledReplicatedMapIterationContext.this.replaceValue(CompiledReplicatedMapIterationContext.this, CompiledReplicatedMapIterationContext.this.context().wrapValueAsValue(newValue)); - return true; - } - - @Override - public boolean remove() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - CompiledReplicatedMapIterationContext.this.remove(CompiledReplicatedMapIterationContext.this); - return true; - } - - @NotNull - @Override - public Bytes entry() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.entryBytes; - } - @NotNull private UnsupportedOperationException unsupportedLocks() { return new UnsupportedOperationException("Lock operations are not supported (and not needed!) during iteration"); @@ -760,9 +705,60 @@ public class CompiledReplicatedMapIterationContext { + @Override + public Void handle() { + return null; + } + + @Override + public ReadAccess access() { + return ReadAccess.zeros(); + } + + @Override + public V getUsing(V usingInstance) { + throw new UnsupportedOperationException(); + } + + @Override + public V get() { + throw new UnsupportedOperationException(); + } + + @Override + public long size() { + return CompiledReplicatedMapIterationContext.this.m().valueSizeMarshaller.minEncodableSize(); + } + + @Override + public long offset() { + return 0; + } + } + private void _MapEntryStages_writeValue(Value value) { value.writeTo(entryBytesAccess, entryBytesAccessHandle, entryBytesAccessOffset(valueOffset)); } @@ -1003,6 +1037,12 @@ public class CompiledReplicatedMapIterationContext context() { return CompiledReplicatedMapIterationContext.this; } + public void checkEntryNotRemovedOnThisIteration() { + if (entryRemovedOnThisIterationInit()) + throw new IllegalStateException("Entry was already removed on this iteration"); + + } + + public void closeReplicatedMapSegmentIterationCheckEntryNotRemovedOnThisIterationDependants() { + CompiledReplicatedMapIterationContext.this.closeInterationCheckOnEachPublicOperationCheckOnEachPublicOperationDependants(); + } + + public MVI valueMetaInterop(V value) { + return CompiledReplicatedMapIterationContext.this.m().metaValueInteropProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.m().originalMetaValueInterop, valueInterop, value); + } + + public void closeValueBytesInteropValueMetaInteropDependants() { + CompiledReplicatedMapIterationContext.this.wrappedValueInstanceValue.closeBuffer(); + } + + public T contextAtIndexInChain(int index) { + return ((T)(contextChain.get(index))); + } + + public void closeReplicatedChronicleMapHolderImplContextAtIndexInChainDependants() { + CompiledReplicatedMapIterationContext.this.closeSegmentStagesTryFindInitLocksOfThisSegmentDependants(); + } + + public void incrementSegmentEntriesIfNeeded() { + } + + private long _MapEntryStages_sizeOfEverythingBeforeValue(long keySize, long valueSize) { + return (((CompiledReplicatedMapIterationContext.this.m().metaDataBytes) + (CompiledReplicatedMapIterationContext.this.m().keySizeMarshaller.sizeEncodingSize(keySize))) + keySize) + (CompiledReplicatedMapIterationContext.this.m().valueSizeMarshaller.sizeEncodingSize(valueSize)); + } + + long sizeOfEverythingBeforeValue(long keySize, long valueSize) { + return (_MapEntryStages_sizeOfEverythingBeforeValue(keySize, valueSize)) + (ReplicatedChronicleMap.ADDITIONAL_ENTRY_BYTES); + } + + public final long entrySize(long keySize, long valueSize) { + long sizeOfEverythingBeforeValue = sizeOfEverythingBeforeValue(keySize, valueSize); + return innerEntrySize(sizeOfEverythingBeforeValue, valueSize); + } + + public long entryBytesAccessOffset(long offset) { + return entryBytesAccessor.offset(entryBytes, offset); + } + + public void closeReplicatedMapEntryStagesEntryBytesAccessOffsetDependants() { + CompiledReplicatedMapIterationContext.this.closeEntry(); + } + public void checkAccessingFromOwnerThread() { if ((owner) != (Thread.currentThread())) { throw new ConcurrentModificationException("Context shouldn\'t be accessed from multiple threads"); @@ -1099,12 +1203,90 @@ public class CompiledReplicatedMapIterationContext wrapValueAsValue(V value) { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + WrappedValueInstanceValue wrapped = CompiledReplicatedMapIterationContext.this.wrappedValueInstanceValue; + wrapped = wrapped.getUnusedWrappedValueGuarded(); + wrapped.initValue(value); + return wrapped; + } + + @Override + public R remove(@NotNull + MapEntry entry) { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapIterationContext.this.m().entryOperations.remove(entry); + } + + @Override + public R replaceValue(@NotNull + MapEntry entry, Value newValue) { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapIterationContext.this.m().entryOperations.replaceValue(entry, newValue); + } + + @Override + public Value defaultValue(@NotNull + MapAbsentEntry absentEntry) { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapIterationContext.this.m().entryOperations.defaultValue(absentEntry); + } + + @NotNull + @Override + public Value value() { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapIterationContext.this.entryValue; + } + + @NotNull + @Override + public InterProcessLock readLock() { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapIterationContext.this.innerReadLock; + } + + @NotNull + @Override + public InterProcessLock updateLock() { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapIterationContext.this.innerUpdateLock; + } + + @NotNull + @Override + public Value key() { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapIterationContext.this.entryKey; + } + + @Override + public R insert(@NotNull + MapAbsentEntry absentEntry, Value value) { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapIterationContext.this.m().entryOperations.insert(absentEntry, value); } private CompiledReplicatedMapIterationContext _Chaining_createChaining() { @@ -1128,184 +1310,26 @@ public class CompiledReplicatedMapIterationContext= 0; } - public void incrementSegmentEntriesIfNeeded() { + public void initHashLookupPos(long hashLookupPos) { + this.hashLookupPos = hashLookupPos; } - public T contextAtIndexInChain(int index) { - return ((T)(contextChain.get(index))); + public long hashLookupPos() { + assert this.hashLookupPosInit() : "HashLookupPos should be init"; + return this.hashLookupPos; } - public void closeReplicatedChronicleMapHolderImplContextAtIndexInChainDependants() { - CompiledReplicatedMapIterationContext.this.closeSegmentStagesTryFindInitLocksOfThisSegmentDependants(); - } - - public void checkEntryNotRemovedOnThisIteration() { - if (entryRemovedOnThisIterationInit()) - throw new IllegalStateException("Entry was already removed on this iteration"); - - } - - public void closeMapSegmentIterationCheckEntryNotRemovedOnThisIterationDependants() { - CompiledReplicatedMapIterationContext.this.closeInterationCheckOnEachPublicOperationCheckOnEachPublicOperationDependants(); - } - - private void _CheckOnEachPublicOperation_checkOnEachPublicOperation() { - CompiledReplicatedMapIterationContext.this.checkAccessingFromOwnerThread(); - } - - public void checkOnEachPublicOperation() { - _CheckOnEachPublicOperation_checkOnEachPublicOperation(); - CompiledReplicatedMapIterationContext.this.checkEntryNotRemovedOnThisIteration(); - } - - public void closeInterationCheckOnEachPublicOperationCheckOnEachPublicOperationDependants() { - CompiledReplicatedMapIterationContext.this.entryKey.closeEntryKeyBytesValueSizeDependants(); - CompiledReplicatedMapIterationContext.this.entryValue.closeEntryValueBytesValueSizeDependants(); - } - - @Override - public Value defaultValue(@NotNull - MapAbsentEntry absentEntry) { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.m().entryOperations.defaultValue(absentEntry); - } - - @Override - public Value wrapValueAsValue(V value) { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - WrappedValueInstanceValue wrapped = CompiledReplicatedMapIterationContext.this.wrappedValueInstanceValue; - wrapped = wrapped.getUnusedWrappedValueGuarded(); - wrapped.initValue(value); - return wrapped; - } - - @NotNull - @Override - public InterProcessLock updateLock() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.innerUpdateLock; - } - - @NotNull - @Override - public Value key() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.entryKey; - } - - @Override - public R replaceValue(@NotNull - MapEntry entry, Value newValue) { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.m().entryOperations.replaceValue(entry, newValue); - } - - @Override - public R remove(@NotNull - MapEntry entry) { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.m().entryOperations.remove(entry); - } - - @NotNull - @Override - public InterProcessLock readLock() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.innerReadLock; - } - - @Override - public R insert(@NotNull - MapAbsentEntry absentEntry, Value value) { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.m().entryOperations.insert(absentEntry, value); - } - - @NotNull - @Override - public Value value() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.entryValue; - } - - @NotNull - @Override - public InterProcessLock writeLock() { - CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapIterationContext.this.innerWriteLock; - } - - public boolean entryIsPresent() { - return true; - } - - public long entryBytesAccessOffset(long offset) { - return entryBytesAccessor.offset(entryBytes, offset); - } - - public void closeReplicatedMapEntryStagesEntryBytesAccessOffsetDependants() { - CompiledReplicatedMapIterationContext.this.closeEntry(); - } - - private long _MapEntryStages_sizeOfEverythingBeforeValue(long keySize, long valueSize) { - return (((CompiledReplicatedMapIterationContext.this.m().metaDataBytes) + (CompiledReplicatedMapIterationContext.this.m().keySizeMarshaller.sizeEncodingSize(keySize))) + keySize) + (CompiledReplicatedMapIterationContext.this.m().valueSizeMarshaller.sizeEncodingSize(valueSize)); - } - - long sizeOfEverythingBeforeValue(long keySize, long valueSize) { - return (_MapEntryStages_sizeOfEverythingBeforeValue(keySize, valueSize)) + (ReplicatedChronicleMap.ADDITIONAL_ENTRY_BYTES); - } - - public final long entrySize(long keySize, long valueSize) { - long sizeOfEverythingBeforeValue = sizeOfEverythingBeforeValue(keySize, valueSize); - return innerEntrySize(sizeOfEverythingBeforeValue, valueSize); - } - - public MKI keyMetaInterop(K key) { - return CompiledReplicatedMapIterationContext.this.h().metaKeyInteropProvider.get(CompiledReplicatedMapIterationContext.this.copies, CompiledReplicatedMapIterationContext.this.h().originalMetaKeyInterop, keyInterop, key); - } - - boolean used; - - public boolean usedInit() { - return used; - } - - public void initUsed(boolean used) { - this.used = used; - } - - void closeUsed() { - if (!(this.usedInit())) + public void closeHashLookupPos() { + if (!(this.hashLookupPosInit())) return ; - used = false; - } - - public boolean entryRemovedOnThisIteration = false; - - boolean entryRemovedOnThisIterationInit() { - return (this.entryRemovedOnThisIteration) != false; - } - - private void initEntryRemovedOnThisIteration(boolean entryRemovedOnThisIteration) { - this.entryRemovedOnThisIteration = entryRemovedOnThisIteration; - } - - public void closeEntryRemovedOnThisIteration() { - if (!(this.entryRemovedOnThisIterationInit())) - return ; - - this.entryRemovedOnThisIteration = false; + this.hashLookupPos = -1; } public int segmentIndex = -1; @@ -1333,215 +1357,11 @@ public class CompiledReplicatedMapIterationContext= 0; - } - - public void initSegmentHashLookup() { - long hashLookupOffset = CompiledReplicatedMapIterationContext.this.h().segmentOffset(CompiledReplicatedMapIterationContext.this.segmentIndex()); - innerInitSegmentHashLookup(((CompiledReplicatedMapIterationContext.this.h().ms.address()) + hashLookupOffset), CompiledReplicatedMapIterationContext.this.h().segmentHashLookupCapacity, CompiledReplicatedMapIterationContext.this.h().segmentHashLookupEntrySize, CompiledReplicatedMapIterationContext.this.h().segmentHashLookupKeyBits, CompiledReplicatedMapIterationContext.this.h().segmentHashLookupValueBits); - } - - public void initSegmentHashLookup(long address, long capacity, int entrySize, int keyBits, int valueBits) { - innerInitSegmentHashLookup(address, capacity, entrySize, keyBits, valueBits); - } - - public int hashLookupEntrySize() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.hashLookupEntrySize; - } - - public int keyBits() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.keyBits; - } - - public long address() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.address; - } - - public long capacityMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.capacityMask; - } - - public long capacityMask2() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.capacityMask2; - } - - public long entryMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.entryMask; - } - - public long keyMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.keyMask; - } - - public long valueMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.valueMask; - } - - public void closeSegmentHashLookup() { - if (!(this.segmentHashLookupInit())) - return ; - - this.address = -1; - } - - public long stepBack(long pos) { - return (pos -= hashLookupEntrySize()) >= 0 ? pos : capacityMask2(); - } - - public long step(long pos) { - return (pos += hashLookupEntrySize()) <= (capacityMask2()) ? pos : 0L; - } - - public long key(long entry) { - return entry & (keyMask()); - } - - public boolean empty(long entry) { - return (entry & (entryMask())) == (UNSET_ENTRY); - } - - long indexToPos(long index) { - return index * (hashLookupEntrySize()); - } - - public long maskUnsetKey(long key) { - return (key &= keyMask()) != (UNSET_KEY) ? key : keyMask(); - } - - long entry(long key, long value) { - return key | (value << (keyBits())); - } - - public void writeEntryVolatile(long pos, long prevEntry, long key, long value) { - long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); - NativeBytes.UNSAFE.putLongVolatile(null, ((address()) + pos), entry); - } - - public void writeEntry(long pos, long prevEntry, long key, long value) { - long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); - NativeBytes.UNSAFE.putLong(((address()) + pos), entry); - } - - void writeEntry(long pos, long prevEntry, long anotherEntry) { - long entry = (prevEntry & (~(entryMask()))) | (anotherEntry & (entryMask())); - NativeBytes.UNSAFE.putLong(((address()) + pos), entry); - } - - public void clearHashLookup() { - NativeBytes.UNSAFE.setMemory(address(), ((capacityMask2()) + (hashLookupEntrySize())), ((byte)(0))); - } - - public long hlPos(long key) { - return indexToPos((key & (capacityMask()))); - } - - public void checkValueForPut(long value) { - assert (value & (~(valueMask()))) == 0L : "Value out of range, was " + value; - } - - public long readEntry(long pos) { - return NativeBytes.UNSAFE.getLong(((address()) + pos)); - } - - public void putValueVolatile(long pos, long value) { - checkValueForPut(value); - long currentEntry = readEntry(pos); - writeEntryVolatile(pos, currentEntry, key(currentEntry), value); - } - - void clearEntry(long pos, long prevEntry) { - long entry = prevEntry & (~(entryMask())); - NativeBytes.UNSAFE.putLong(((address()) + pos), entry); - } - - public long remove(long posToRemove) { - long entryToRemove = readEntry(posToRemove); - long posToShift = posToRemove; - while (true) { - posToShift = step(posToShift); - long entryToShift = readEntry(posToShift); - if (empty(entryToShift)) - break; - - long insertPos = hlPos(key(entryToShift)); - boolean cond1 = insertPos <= posToRemove; - boolean cond2 = posToRemove <= posToShift; - if ((cond1 && cond2) || ((posToShift < insertPos) && (cond1 || cond2))) { - writeEntry(posToRemove, entryToRemove, entryToShift); - posToRemove = posToShift; - entryToRemove = entryToShift; - } - } - clearEntry(posToRemove, entryToRemove); - return posToRemove; - } - - public long value(long entry) { - return (entry >>> (keyBits())) & (valueMask()); - } - - void forEach(EntryConsumer action) { - for (long pos = 0L ; pos <= (capacityMask2()) ; pos += hashLookupEntrySize()) { - long entry = readEntry(pos); - if (!(empty(entry))) - action.accept(key(entry), value(entry)); - - } - } - - String hashLookupToString() { - final StringBuilder sb = new StringBuilder("{"); - forEach((long key,long value) -> sb.append(key).append('=').append(value).append(',')); - sb.append('}'); - return sb.toString(); } long entrySpaceOffset = 0; @@ -1662,19 +1482,11 @@ public class CompiledReplicatedMapIterationContext= 0; + } + + public void initSegmentHashLookup() { + long hashLookupOffset = CompiledReplicatedMapIterationContext.this.h().segmentOffset(CompiledReplicatedMapIterationContext.this.segmentIndex()); + innerInitSegmentHashLookup(((CompiledReplicatedMapIterationContext.this.h().ms.address()) + hashLookupOffset), CompiledReplicatedMapIterationContext.this.h().segmentHashLookupCapacity, CompiledReplicatedMapIterationContext.this.h().segmentHashLookupEntrySize, CompiledReplicatedMapIterationContext.this.h().segmentHashLookupKeyBits, CompiledReplicatedMapIterationContext.this.h().segmentHashLookupValueBits); + } + + public void initSegmentHashLookup(long address, long capacity, int entrySize, int keyBits, int valueBits) { + innerInitSegmentHashLookup(address, capacity, entrySize, keyBits, valueBits); + } + + public int hashLookupEntrySize() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.hashLookupEntrySize; + } + + public int keyBits() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.keyBits; + } + + public long address() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.address; + } + + public long capacityMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.capacityMask; + } + + public long capacityMask2() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.capacityMask2; + } + + public long entryMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.entryMask; + } + + public long keyMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.keyMask; + } + + public long valueMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.valueMask; + } + + public void closeSegmentHashLookup() { + if (!(this.segmentHashLookupInit())) + return ; + + this.address = -1; + } + + public void checkValueForPut(long value) { + assert (value & (~(valueMask()))) == 0L : "Value out of range, was " + value; + } + + long indexToPos(long index) { + return index * (hashLookupEntrySize()); + } + + public long key(long entry) { + return entry & (keyMask()); + } + + public long value(long entry) { + return (entry >>> (keyBits())) & (valueMask()); + } + + public boolean empty(long entry) { + return (entry & (entryMask())) == (UNSET_ENTRY); + } + + public long maskUnsetKey(long key) { + return (key &= keyMask()) != (UNSET_KEY) ? key : keyMask(); + } + + long entry(long key, long value) { + return key | (value << (keyBits())); + } + + public void writeEntry(long pos, long prevEntry, long key, long value) { + long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); + NativeBytes.UNSAFE.putLong(((address()) + pos), entry); + } + + public void writeEntryVolatile(long pos, long prevEntry, long key, long value) { + long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); + NativeBytes.UNSAFE.putLongVolatile(null, ((address()) + pos), entry); + } + + void clearEntry(long pos, long prevEntry) { + long entry = prevEntry & (~(entryMask())); + NativeBytes.UNSAFE.putLong(((address()) + pos), entry); + } + + void writeEntry(long pos, long prevEntry, long anotherEntry) { + long entry = (prevEntry & (~(entryMask()))) | (anotherEntry & (entryMask())); + NativeBytes.UNSAFE.putLong(((address()) + pos), entry); + } + + public long readEntry(long pos) { + return NativeBytes.UNSAFE.getLong(((address()) + pos)); + } + + public void putValueVolatile(long pos, long value) { + checkValueForPut(value); + long currentEntry = readEntry(pos); + writeEntryVolatile(pos, currentEntry, key(currentEntry), value); + } + + void forEach(EntryConsumer action) { + for (long pos = 0L ; pos <= (capacityMask2()) ; pos += hashLookupEntrySize()) { + long entry = readEntry(pos); + if (!(empty(entry))) + action.accept(key(entry), value(entry)); + + } + } + + String hashLookupToString() { + final StringBuilder sb = new StringBuilder("{"); + forEach((long key,long value) -> sb.append(key).append('=').append(value).append(',')); + sb.append('}'); + return sb.toString(); + } + + public long step(long pos) { + return (pos += hashLookupEntrySize()) <= (capacityMask2()) ? pos : 0L; + } + + public void clearHashLookup() { + NativeBytes.UNSAFE.setMemory(address(), ((capacityMask2()) + (hashLookupEntrySize())), ((byte)(0))); + } + + public long hlPos(long key) { + return indexToPos((key & (capacityMask()))); + } + + public long remove(long posToRemove) { + long entryToRemove = readEntry(posToRemove); + long posToShift = posToRemove; + while (true) { + posToShift = step(posToShift); + long entryToShift = readEntry(posToShift); + if (empty(entryToShift)) + break; + + long insertPos = hlPos(key(entryToShift)); + boolean cond1 = insertPos <= posToRemove; + boolean cond2 = posToRemove <= posToShift; + if ((cond1 && cond2) || ((posToShift < insertPos) && (cond1 || cond2))) { + writeEntry(posToRemove, entryToRemove, entryToShift); + posToRemove = posToShift; + entryToRemove = entryToShift; + } + } + clearEntry(posToRemove, entryToRemove); + return posToRemove; + } + + public long stepBack(long pos) { + return (pos -= hashLookupEntrySize()) >= 0 ? pos : capacityMask2(); + } + long segmentHeaderAddress; SegmentHeader segmentHeader = null; @@ -1815,10 +1839,30 @@ public class CompiledReplicatedMapIterationContext= (CompiledReplicatedMapIterationContext.this.h().actualChunksPerSegment)) - nextPosToSearchFrom = 0L; - - nextPosToSearchFrom(nextPosToSearchFrom); - } - - public long alloc(int chunks) { - VanillaChronicleHash h = CompiledReplicatedMapIterationContext.this.h(); - if (chunks > (h.maxChunksPerEntry)) - throw new IllegalArgumentException((((("Entry is too large: requires " + chunks) + " entry size chucks, ") + (h.maxChunksPerEntry)) + " is maximum.")); - - long ret = freeList().setNextNContinuousClearBits(nextPosToSearchFrom(), chunks); - if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { - if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) - freeList().clear(ret, h.actualChunksPerSegment); - - ret = freeList().setNextNContinuousClearBits(0L, chunks); - if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { - if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) - freeList().clear(ret, h.actualChunksPerSegment); - - if (chunks == 1) { - throw new IllegalStateException("Segment is full, no free entries found"); - } else { - throw new IllegalStateException((("Segment is full or has no ranges of " + chunks) + " continuous free chunks")); - } - } - updateNextPosToSearchFrom(ret, chunks); - } else { - if ((chunks == 1) || (freeList().isSet(nextPosToSearchFrom()))) { - updateNextPosToSearchFrom(ret, chunks); - } - } - return ret; - } - - public void free(long fromPos, int chunks) { - freeList().clear(fromPos, (fromPos + chunks)); - if (fromPos < (nextPosToSearchFrom())) - nextPosToSearchFrom(fromPos); - - } - int totalReadLockCount; int totalUpdateLockCount; @@ -1973,75 +1948,13 @@ public class CompiledReplicatedMapIterationContext= 0; - } - - public void initHashLookupPos(long hashLookupPos) { - this.hashLookupPos = hashLookupPos; - } - - public long hashLookupPos() { - assert this.hashLookupPosInit() : "HashLookupPos should be init"; - return this.hashLookupPos; - } - - public void closeHashLookupPos() { - if (!(this.hashLookupPosInit())) - return ; - - this.hashLookupPos = -1; - } - public boolean forEachRemoving(Predicate> action) { CompiledReplicatedMapIterationContext.this.innerUpdateLock.lock(); - long size = CompiledReplicatedMapIterationContext.this.size(); - if (size == 0) - return true; - try { + long size = CompiledReplicatedMapIterationContext.this.size(); + if (size == 0) + return true; + boolean interrupted = false; long startPos = 0L; while (!(CompiledReplicatedMapIterationContext.this.empty(CompiledReplicatedMapIterationContext.this.readEntry(startPos)))) { @@ -2073,6 +1986,92 @@ public class CompiledReplicatedMapIterationContext= (CompiledReplicatedMapIterationContext.this.h().actualChunksPerSegment)) + nextPosToSearchFrom = 0L; + + nextPosToSearchFrom(nextPosToSearchFrom); + } + + public long alloc(int chunks) { + VanillaChronicleHash h = CompiledReplicatedMapIterationContext.this.h(); + if (chunks > (h.maxChunksPerEntry)) + throw new IllegalArgumentException((((("Entry is too large: requires " + chunks) + " entry size chucks, ") + (h.maxChunksPerEntry)) + " is maximum.")); + + long ret = freeList().setNextNContinuousClearBits(nextPosToSearchFrom(), chunks); + if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { + if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) + freeList().clear(ret, h.actualChunksPerSegment); + + ret = freeList().setNextNContinuousClearBits(0L, chunks); + if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { + if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) + freeList().clear(ret, h.actualChunksPerSegment); + + if (chunks == 1) { + throw new IllegalStateException("Segment is full, no free entries found"); + } else { + throw new IllegalStateException((("Segment is full or has no ranges of " + chunks) + " continuous free chunks")); + } + } + updateNextPosToSearchFrom(ret, chunks); + } else { + if ((chunks == 1) || (freeList().isSet(nextPosToSearchFrom()))) { + updateNextPosToSearchFrom(ret, chunks); + } + } + return ret; + } + + public void free(long fromPos, int chunks) { + freeList().clear(fromPos, (fromPos + chunks)); + if (fromPos < (nextPosToSearchFrom())) + nextPosToSearchFrom(fromPos); + + } + + public void dropChange() { + CompiledReplicatedMapIterationContext.this.m().dropChange(CompiledReplicatedMapIterationContext.this.segmentIndex(), CompiledReplicatedMapIterationContext.this.pos()); + } + + public void closeReplicationUpdateDropChangeDependants() { + CompiledReplicatedMapIterationContext.this.closeReplicationUpdateUpdateChangeDependants(); + } + + public boolean entryRemovedOnThisIteration = false; + + boolean entryRemovedOnThisIterationInit() { + return (this.entryRemovedOnThisIteration) != false; + } + + protected void initEntryRemovedOnThisIteration(boolean entryRemovedOnThisIteration) { + this.entryRemovedOnThisIteration = entryRemovedOnThisIteration; + } + + public void closeEntryRemovedOnThisIteration() { + if (!(this.entryRemovedOnThisIterationInit())) + return ; + + this.entryRemovedOnThisIteration = false; + } + public long newTimestamp; public byte newIdentifier = ((byte)(0)); @@ -2125,8 +2124,14 @@ public class CompiledReplicatedMapIterationContext value) { + initValue(value); + freeExtraAllocatedChunks(); + CompiledReplicatedMapIterationContext.this.putValueVolatile(CompiledReplicatedMapIterationContext.this.hashLookupPos(), pos()); + } + + public void initEntryAndKeyCopying(long entrySize, long bytesToCopy) { + initAllocatedChunks(CompiledReplicatedMapIterationContext.this.h().inChunks(entrySize)); + CompiledReplicatedMapIterationContext.this.initEntryCopying(CompiledReplicatedMapIterationContext.this.alloc(allocatedChunks()), bytesToCopy); + incrementSegmentEntriesIfNeeded(); + } + private void _MapEntryStages_putValueDeletedEntry(Value newValue) { assert CompiledReplicatedMapIterationContext.this.innerUpdateLock.isHeldByCurrentThread(); int newSizeInChunks; @@ -2339,12 +2411,6 @@ public class CompiledReplicatedMapIterationContext value) { - initValue(value); - freeExtraAllocatedChunks(); - CompiledReplicatedMapIterationContext.this.putValueVolatile(CompiledReplicatedMapIterationContext.this.hashLookupPos(), pos()); - } - private void _MapEntryStages_relocation(Value newValue, long newSizeOfEverythingBeforeValue) { CompiledReplicatedMapIterationContext.this.free(pos(), entrySizeInChunks()); long entrySize = innerEntrySize(newSizeOfEverythingBeforeValue, newValue.size()); @@ -2357,27 +2423,6 @@ public class CompiledReplicatedMapIterationContext newValue) { assert CompiledReplicatedMapIterationContext.this.innerUpdateLock.isHeldByCurrentThread(); boolean newValueSizeIsDifferent = (newValue.size()) != (this.valueSize()); @@ -2412,8 +2457,38 @@ public class CompiledReplicatedMapIterationContext newValue) { + public void doRemove() { + CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); + initEntryRemovedOnThisIteration(true); + try { + if ((CompiledReplicatedMapIterationContext.this.valueSize()) > (CompiledReplicatedMapIterationContext.this.dummyValue.size())) + CompiledReplicatedMapIterationContext.this.innerDefaultReplaceValue(CompiledReplicatedMapIterationContext.this.dummyValue); + + CompiledReplicatedMapIterationContext.this.initUpdatedReplicationState(); + CompiledReplicatedMapIterationContext.this.writeEntryDeleted(); + CompiledReplicatedMapIterationContext.this.updateChange(); + CompiledReplicatedMapIterationContext.this.deleted(((CompiledReplicatedMapIterationContext.this.deleted()) + 1)); + } finally { + CompiledReplicatedMapIterationContext.this.innerWriteLock.unlock(); + } + } + + private void _MapSegmentIteration_doReplaceValue(Value newValue) { CompiledReplicatedMapIterationContext.this.checkOnEachPublicOperation(); try { CompiledReplicatedMapIterationContext.this.innerDefaultReplaceValue(newValue); @@ -2423,35 +2498,24 @@ public class CompiledReplicatedMapIterationContext newValue) { + _MapSegmentIteration_doReplaceValue(newValue); } - private boolean updatedReplicationState = false; + boolean used; - public boolean updatedReplicationStateInit() { - return (this.updatedReplicationState) != false; + public boolean usedInit() { + return used; } - public void initUpdatedReplicationState() { - initReplicationState(CompiledReplicatedMapIterationContext.this.newTimestamp(), CompiledReplicatedMapIterationContext.this.newIdentifier()); - updatedReplicationState = true; + public void initUsed(boolean used) { + this.used = used; } - public void closeUpdatedReplicationState() { - if (!(this.updatedReplicationStateInit())) + void closeUsed() { + if (!(this.usedInit())) return ; - this.updatedReplicationState = false; - } - - private boolean testTimeStampInSensibleRange() { - if ((CompiledReplicatedMapIterationContext.this.m().timeProvider) == (TimeProvider.SYSTEM)) { - long currentTime = TimeProvider.SYSTEM.currentTime(); - assert (Math.abs((currentTime - (timestamp())))) <= 100000000 : "unrealistic timestamp: " + (timestamp()); - assert (Math.abs((currentTime - (CompiledReplicatedMapIterationContext.this.newTimestamp())))) <= 100000000 : "unrealistic newTimestamp: " + (CompiledReplicatedMapIterationContext.this.newTimestamp()); - } - return true; + used = false; } } diff --git a/src/main/java/net/openhft/chronicle/map/impl/CompiledReplicatedMapQueryContext.java b/src/main/java/net/openhft/chronicle/map/impl/CompiledReplicatedMapQueryContext.java index c300f0196..02b722fb5 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/CompiledReplicatedMapQueryContext.java +++ b/src/main/java/net/openhft/chronicle/map/impl/CompiledReplicatedMapQueryContext.java @@ -19,8 +19,10 @@ import net.openhft.chronicle.hash.replication.TimeProvider; import net.openhft.chronicle.hash.serialization.BytesReader; import net.openhft.chronicle.hash.serialization.internal.MetaBytesInterop; import net.openhft.chronicle.map.*; +import net.openhft.chronicle.map.TcpReplicator.TcpSocketChannelEntryWriter; import net.openhft.chronicle.map.impl.ret.InstanceReturnValue; import net.openhft.chronicle.map.impl.ret.UsableReturnValue; +import net.openhft.chronicle.map.impl.stage.query.MapAbsentEntryHolder; import net.openhft.chronicle.map.impl.value.instance.ValueInitableValue; import net.openhft.chronicle.map.replication.MapRemoteQueryContext; import net.openhft.chronicle.map.replication.MapReplicableEntry; @@ -43,23 +45,24 @@ import java.util.ConcurrentModificationException; import java.util.List; import java.util.concurrent.TimeUnit; -public class CompiledReplicatedMapQueryContext, V, VI, MVI extends MetaBytesInterop, R, T> implements AutoCloseable , HashEntry , InterProcessReadWriteUpdateLock , RemoteOperationContext , ExternalMapQueryContext , MapAbsentEntry , MapContext , MapEntry , QueryContextInterface , ReplicatedChronicleMapHolder , MapRemoteQueryContext , MapReplicableEntry { +public class CompiledReplicatedMapQueryContext, V, VI, MVI extends MetaBytesInterop, R, T> implements AutoCloseable , HashEntry , InterProcessReadWriteUpdateLock , RemoteOperationContext , ExternalMapQueryContext , MapAbsentEntry , MapContext , MapEntry , QueryContextInterface , ReplicatedChronicleMapHolder , MapAbsentEntryHolder , MapRemoteQueryContext , MapReplicableEntry { public void close() { + CompiledReplicatedMapQueryContext.this.closeAllocatedChunks(); + CompiledReplicatedMapQueryContext.this.closeReplicatedInputBytes(); CompiledReplicatedMapQueryContext.this.closeReplicationUpdate(); - CompiledReplicatedMapQueryContext.this.closeInputKey(); - CompiledReplicatedMapQueryContext.this.wrappedValueInstanceValue.closeValue(); + CompiledReplicatedMapQueryContext.this.closeReplicationInput(); + CompiledReplicatedMapQueryContext.this.defaultReturnValue.closeDefaultReturnedValue(); + CompiledReplicatedMapQueryContext.this.inputValueInstanceValue.closeValue(); CompiledReplicatedMapQueryContext.this.wrappedValueInstanceValue.closeNext(); CompiledReplicatedMapQueryContext.this.closeUsed(); - CompiledReplicatedMapQueryContext.this.closeAllocatedChunks(); - CompiledReplicatedMapQueryContext.this.closeInputBytes(); - CompiledReplicatedMapQueryContext.this.closeReplicatedInputBytes(); - CompiledReplicatedMapQueryContext.this.usingReturnValue.closeUsingReturnValue(); + CompiledReplicatedMapQueryContext.this.closeInputKey(); CompiledReplicatedMapQueryContext.this.inputKeyInstanceValue.closeKey(); - CompiledReplicatedMapQueryContext.this.inputValueInstanceValue.closeValue(); - CompiledReplicatedMapQueryContext.this.defaultReturnValue.closeDefaultReturnedValue(); - CompiledReplicatedMapQueryContext.this.closeKeyBytesInteropKeyMetaInteropDependants(); - CompiledReplicatedMapQueryContext.this.innerUpdateLock.closeUpdateLockForbiddenUpgradeDependants(); + CompiledReplicatedMapQueryContext.this.bytesReturnValue.closeOutput(); + CompiledReplicatedMapQueryContext.this.wrappedValueInstanceValue.closeValue(); + CompiledReplicatedMapQueryContext.this.closeInputBytes(); + CompiledReplicatedMapQueryContext.this.usingReturnValue.closeUsingReturnValue(); CompiledReplicatedMapQueryContext.this.closeValueBytesInteropValueMetaInteropDependants(); + CompiledReplicatedMapQueryContext.this.closeKeyBytesInteropKeyMetaInteropDependants(); CompiledReplicatedMapQueryContext.this.closeReplicatedChronicleMapHolderImplContextAtIndexInChainDependants(); CompiledReplicatedMapQueryContext.this.closeOwnerThreadHolderCheckAccessingFromOwnerThreadDependants(); CompiledReplicatedMapQueryContext.this.closeReplicatedMapEntryStagesEntryBytesAccessOffsetDependants(); @@ -114,34 +117,36 @@ public class CompiledReplicatedMapQueryContext)(entryBytesAccessor.access(entryBytes))); + this.bytesReturnValue = new BytesReturnValue(); + this.innerWriteLock = new WriteLock(); + this.replicatedInputKeyBytesValue = new ReplicatedInputKeyBytesValue(); + this.inputFirstValueBytesValue = new InputFirstValueBytesValue(); this.copies = ThreadLocalCopies.get(); this.valueInterop = CompiledReplicatedMapQueryContext.this.m().valueInteropProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.m().originalValueInterop); this.valueReader = CompiledReplicatedMapQueryContext.this.m().valueReaderProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.m().originalValueReader); this.keyInterop = CompiledReplicatedMapQueryContext.this.h().keyInteropProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.h().originalKeyInterop); this.keyReader = CompiledReplicatedMapQueryContext.this.h().keyReaderProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.h().originalKeyReader); - this.defaultReturnValue = new DefaultReturnValue(); - this.innerUpdateLock = new UpdateLock(); - this.owner = Thread.currentThread(); + this.absentDelegating = new ReplicatedMapAbsentDelegating(); this.inputKeyInstanceValue = new InputKeyInstanceValue(); + this.usingReturnValue = new UsingReturnValue(); + this.defaultReturnValue = new DefaultReturnValue(); + this.inputKeyBytesValue = new InputKeyBytesValue(); + this.inputSecondValueBytesValue = new InputSecondValueBytesValue(); + this.innerReadLock = new ReadLock(); + this.deprecatedMapAcquireContext = new DeprecatedMapAcquireContextOnQuery(); + this.entryKey = new EntryKeyBytesValue(); + this.replicatedInputValueBytesValue = new ReplicatedInputValueBytesValue(); this.wrappedValueInstanceValue = new WrappedValueInstanceValue(); - this.entryBytes = CompiledReplicatedMapQueryContext.this.h().ms.bytes(); - this.entryBytesAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(entryBytes); - this.entryBytesAccessHandle = ((T)(entryBytesAccessor.handle(entryBytes))); - this.entryBytesAccess = ((Access)(entryBytesAccessor.access(entryBytes))); - this.innerWriteLock = new WriteLock(); + this.owner = Thread.currentThread(); + this.entryValue = new EntryValueBytesValue(); + this.dummyValue = new DummyValueZeroValue(); } public CompiledReplicatedMapQueryContext(CompiledReplicatedMapQueryContext c) { @@ -149,42 +154,39 @@ public class CompiledReplicatedMapQueryContext)(c.m)); - this.inputSecondValueBytesValue = new InputSecondValueBytesValue(); - this.innerReadLock = new ReadLock(); - this.inputFirstValueBytesValue = new InputFirstValueBytesValue(); - this.inputValueInstanceValue = new InputValueInstanceValue(); - this.inputKeyBytesValue = new InputKeyBytesValue(); - this.deprecatedMapAcquireContext = new DeprecatedMapAcquireContextOnQuery(); - this.replicatedInputValueBytesValue = new ReplicatedInputValueBytesValue(); - this.replicatedInputKeyBytesValue = new ReplicatedInputKeyBytesValue(); - this.entryKey = new EntryKeyBytesValue(); - this.usingReturnValue = new UsingReturnValue(); + this.innerUpdateLock = new UpdateLock(); this.deprecatedMapKeyContext = new DeprecatedMapKeyContextOnQuery(); - this.entryValue = new EntryValueBytesValue(); - this.dummyValue = new DummyValueZeroValue(); + this.inputValueInstanceValue = new InputValueInstanceValue(); + this.entryBytes = CompiledReplicatedMapQueryContext.this.h().ms.bytes(); + this.entryBytesAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(entryBytes); + this.entryBytesAccessHandle = ((T)(entryBytesAccessor.handle(entryBytes))); + this.entryBytesAccess = ((Access)(entryBytesAccessor.access(entryBytes))); + this.bytesReturnValue = new BytesReturnValue(); + this.innerWriteLock = new WriteLock(); + this.replicatedInputKeyBytesValue = new ReplicatedInputKeyBytesValue(); + this.inputFirstValueBytesValue = new InputFirstValueBytesValue(); this.copies = ThreadLocalCopies.get(); this.valueInterop = CompiledReplicatedMapQueryContext.this.m().valueInteropProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.m().originalValueInterop); this.valueReader = CompiledReplicatedMapQueryContext.this.m().valueReaderProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.m().originalValueReader); this.keyInterop = CompiledReplicatedMapQueryContext.this.h().keyInteropProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.h().originalKeyInterop); this.keyReader = CompiledReplicatedMapQueryContext.this.h().keyReaderProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.h().originalKeyReader); - this.defaultReturnValue = new DefaultReturnValue(); - this.innerUpdateLock = new UpdateLock(); - this.owner = Thread.currentThread(); + this.absentDelegating = new ReplicatedMapAbsentDelegating(); this.inputKeyInstanceValue = new InputKeyInstanceValue(); + this.usingReturnValue = new UsingReturnValue(); + this.defaultReturnValue = new DefaultReturnValue(); + this.inputKeyBytesValue = new InputKeyBytesValue(); + this.inputSecondValueBytesValue = new InputSecondValueBytesValue(); + this.innerReadLock = new ReadLock(); + this.deprecatedMapAcquireContext = new DeprecatedMapAcquireContextOnQuery(); + this.entryKey = new EntryKeyBytesValue(); + this.replicatedInputValueBytesValue = new ReplicatedInputValueBytesValue(); this.wrappedValueInstanceValue = new WrappedValueInstanceValue(); - this.entryBytes = CompiledReplicatedMapQueryContext.this.h().ms.bytes(); - this.entryBytesAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(entryBytes); - this.entryBytesAccessHandle = ((T)(entryBytesAccessor.handle(entryBytes))); - this.entryBytesAccess = ((Access)(entryBytesAccessor.access(entryBytes))); - this.innerWriteLock = new WriteLock(); + this.owner = Thread.currentThread(); + this.entryValue = new EntryValueBytesValue(); + this.dummyValue = new DummyValueZeroValue(); } public class InputKeyBytesValue extends AbstractValue { - @Override - public T handle() { - return ((T)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.inputBytes()).handle(CompiledReplicatedMapQueryContext.this.inputBytes()))); - } - @Override public ReadAccess access() { return ((ReadAccess)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.inputBytes()).access(CompiledReplicatedMapQueryContext.this.inputBytes()))); @@ -199,11 +201,6 @@ public class CompiledReplicatedMapQueryContext { - @Override - public T handle() { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapQueryContext.this.entryBytesAccessHandle; - } - @Override public ReadAccess access() { CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); @@ -280,6 +281,12 @@ public class CompiledReplicatedMapQueryContext { + @NotNull + @Override + public Value defaultValue() { + return CompiledReplicatedMapQueryContext.this.defaultValue(); + } + + @NotNull + @Override + public MapContext context() { + return CompiledReplicatedMapQueryContext.this.context(); + } + + @NotNull + @Override + public Value absentKey() { + return CompiledReplicatedMapQueryContext.this.absentKey(); + } + + @Override + public void doInsert(Value value) { + CompiledReplicatedMapQueryContext.this.doInsert(value); + } } public class EntryValueBytesValue extends AbstractValue { @@ -624,12 +648,6 @@ public class CompiledReplicatedMapQueryContext { - @Override - public T handle() { - return ((T)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.inputBytes()).handle(CompiledReplicatedMapQueryContext.this.inputBytes()))); - } - - @Override - public ReadAccess access() { - return ((ReadAccess)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.inputBytes()).access(CompiledReplicatedMapQueryContext.this.inputBytes()))); - } - @Override public long size() { return CompiledReplicatedMapQueryContext.this.firstInputValueSize(); @@ -751,10 +765,20 @@ public class CompiledReplicatedMapQueryContext access() { + return ((ReadAccess)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.inputBytes()).access(CompiledReplicatedMapQueryContext.this.inputBytes()))); + } + @Override public long offset() { return JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.inputBytes()).offset(CompiledReplicatedMapQueryContext.this.inputBytes(), CompiledReplicatedMapQueryContext.this.firstInputValueOffset()); } + + @Override + public T handle() { + return ((T)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.inputBytes()).handle(CompiledReplicatedMapQueryContext.this.inputBytes()))); + } } public class InputSecondValueBytesValue extends AbstractValue { @@ -768,6 +792,11 @@ public class CompiledReplicatedMapQueryContext)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.inputBytes()).access(CompiledReplicatedMapQueryContext.this.inputBytes()))); } + @Override + public long offset() { + return JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.inputBytes()).offset(CompiledReplicatedMapQueryContext.this.inputBytes(), CompiledReplicatedMapQueryContext.this.secondInputValueOffset()); + } + @Override public long size() { return CompiledReplicatedMapQueryContext.this.secondInputValueSize(); @@ -818,11 +847,6 @@ public class CompiledReplicatedMapQueryContext { @@ -841,18 +865,6 @@ public class CompiledReplicatedMapQueryContext { + @NotNull + @Override + public InterProcessLock writeLock() { + return CompiledReplicatedMapQueryContext.this.writeLock(); + } + @NotNull @Override public InterProcessLock readLock() { @@ -946,31 +976,6 @@ public class CompiledReplicatedMapQueryContext entry = CompiledReplicatedMapQueryContext.this.entry(); + if (entry != null) { + CompiledReplicatedMapQueryContext.this.remove(entry); + return true; + } else { + return false; + } } @Override @@ -1019,17 +1030,30 @@ public class CompiledReplicatedMapQueryContext entry = CompiledReplicatedMapQueryContext.this.entry(); - if (entry != null) { - CompiledReplicatedMapQueryContext.this.remove(entry); - return true; - } else { - return false; - } + return CompiledReplicatedMapQueryContext.this.entryBytes; + } + + @Override + public long keyOffset() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.keyOffset(); + } + + @Override + public void close() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + CompiledReplicatedMapQueryContext.this.close(); + } + + @NotNull + @Override + public K key() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.queriedKey().get(); } @Override @@ -1064,18 +1088,6 @@ public class CompiledReplicatedMapQueryContext { - @Override - public T handle() { - return ((T)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()).handle(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()))); - } - - @Override - public ReadAccess access() { - return ((ReadAccess)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()).access(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()))); - } - @Override public long size() { return CompiledReplicatedMapQueryContext.this.riKeySize(); @@ -1179,6 +1193,11 @@ public class CompiledReplicatedMapQueryContext { - @Override - public ReadAccess access() { - return ((ReadAccess)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()).access(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()))); - } - @Override public T handle() { return ((T)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()).handle(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()))); } @Override - public long offset() { - return JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()).offset(CompiledReplicatedMapQueryContext.this.replicatedInputBytes(), CompiledReplicatedMapQueryContext.this.riValueOffset()); + public ReadAccess access() { + return ((ReadAccess)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()).access(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()))); } + } + public class ReplicatedInputValueBytesValue extends AbstractValue { @Override public long size() { return CompiledReplicatedMapQueryContext.this.riValueSize(); @@ -1253,6 +1262,11 @@ public class CompiledReplicatedMapQueryContext access() { + return ((ReadAccess)(JavaLangBytesAccessors.uncheckedBytesAccessor(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()).access(CompiledReplicatedMapQueryContext.this.replicatedInputBytes()))); + } + @Override public V getUsing(V usingValue) { Bytes inputBytes = CompiledReplicatedMapQueryContext.this.replicatedInputBytes(); @@ -1295,9 +1309,24 @@ public class CompiledReplicatedMapQueryContext { + @Override + public V getUsing(V usingInstance) { + throw new UnsupportedOperationException(); + } + @Override public long offset() { return 0; @@ -1308,16 +1337,6 @@ public class CompiledReplicatedMapQueryContext access() { return ReadAccess.zeros(); } + + @Override + public Void handle() { + return null; + } } public enum SearchState { @@ -1568,11 +1592,6 @@ public class CompiledReplicatedMapQueryContext { + long startOutputPos; + + TcpSocketChannelEntryWriter output = null; + + public boolean outputInit() { + return (this.output) != null; + } + + public void initOutput(TcpSocketChannelEntryWriter output) { + this.output = output; + startOutputPos = output.in().position(); + } + + public long startOutputPos() { + assert this.outputInit() : "Output should be init"; + return this.startOutputPos; + } + + public TcpSocketChannelEntryWriter output() { + assert this.outputInit() : "Output should be init"; + return this.output; + } + + public void closeOutput() { + if (!(this.outputInit())) + return ; + + this.output = null; + } + + @Override + public void close() { + if ((output().in().position()) == (startOutputPos())) { + output().ensureBufferSize(1L); + output().in().writeBoolean(true); + } + } + + @Override + public void returnValue(@NotNull + Value value) { + long valueSize = value.size(); + long totalSize = (1L + (CompiledReplicatedMapQueryContext.this.m().valueSizeMarshaller.sizeEncodingSize(valueSize))) + valueSize; + output().ensureBufferSize(totalSize); + Bytes out = output().in(); + out.writeBoolean(false); + CompiledReplicatedMapQueryContext.this.m().valueSizeMarshaller.writeSize(out, valueSize); + Full outAccessor = JavaLangBytesAccessors.uncheckedBytesAccessor(out); + long outPosition = out.position(); + out.skip(valueSize); + value.writeTo(outAccessor, out, outPosition); + } } public class DefaultReturnValue implements InstanceReturnValue { @@ -1810,6 +1889,12 @@ public class CompiledReplicatedMapQueryContext m() { - return m; - } - - @Override - public ChronicleMap map() { - return m(); + public MapAbsentEntry absent() { + return CompiledReplicatedMapQueryContext.this; } public void checkAccessingFromOwnerThread() { @@ -1991,48 +2067,24 @@ public class CompiledReplicatedMapQueryContextT contextAtIndexInChain(int index) { - return ((T)(contextChain.get(index))); - } - - public void closeReplicatedChronicleMapHolderImplContextAtIndexInChainDependants() { - CompiledReplicatedMapQueryContext.this.closeQuerySegmentStagesTryFindInitLocksOfThisSegmentDependants(); - } - - private long _MapEntryStages_sizeOfEverythingBeforeValue(long keySize, long valueSize) { - return (((CompiledReplicatedMapQueryContext.this.m().metaDataBytes) + (CompiledReplicatedMapQueryContext.this.m().keySizeMarshaller.sizeEncodingSize(keySize))) + keySize) + (CompiledReplicatedMapQueryContext.this.m().valueSizeMarshaller.sizeEncodingSize(valueSize)); - } - - long sizeOfEverythingBeforeValue(long keySize, long valueSize) { - return (_MapEntryStages_sizeOfEverythingBeforeValue(keySize, valueSize)) + (ReplicatedChronicleMap.ADDITIONAL_ENTRY_BYTES); - } - - public final long entrySize(long keySize, long valueSize) { - long sizeOfEverythingBeforeValue = sizeOfEverythingBeforeValue(keySize, valueSize); - return innerEntrySize(sizeOfEverythingBeforeValue, valueSize); - } - - @NotNull @Override - public MapContext context() { - return this; + public ReplicatedChronicleMap m() { + return m; } - public MVI valueMetaInterop(V value) { - return CompiledReplicatedMapQueryContext.this.m().metaValueInteropProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.m().originalMetaValueInterop, valueInterop, value); + @Override + public ChronicleMap map() { + return m(); } - public void closeValueBytesInteropValueMetaInteropDependants() { - CompiledReplicatedMapQueryContext.this.wrappedValueInstanceValue.closeBuffer(); - CompiledReplicatedMapQueryContext.this.inputValueInstanceValue.closeBuffer(); - } - - public MKI keyMetaInterop(K key) { - return CompiledReplicatedMapQueryContext.this.h().metaKeyInteropProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.h().originalMetaKeyInterop, keyInterop, key); - } - - public void closeKeyBytesInteropKeyMetaInteropDependants() { - CompiledReplicatedMapQueryContext.this.inputKeyInstanceValue.closeBuffer(); + public long innerEntrySize(long sizeOfEverythingBeforeValue, long valueSize) { + if (CompiledReplicatedMapQueryContext.this.m().constantlySizedEntry) { + return CompiledReplicatedMapQueryContext.this.m().alignment.alignAddr((sizeOfEverythingBeforeValue + valueSize)); + } else if (CompiledReplicatedMapQueryContext.this.m().couldNotDetermineAlignmentBeforeAllocation) { + return (sizeOfEverythingBeforeValue + (CompiledReplicatedMapQueryContext.this.m().worstAlignment)) + valueSize; + } else { + return (CompiledReplicatedMapQueryContext.this.m().alignment.alignAddr(sizeOfEverythingBeforeValue)) + valueSize; + } } private CompiledReplicatedMapQueryContext _Chaining_createChaining() { @@ -2056,33 +2108,42 @@ public class CompiledReplicatedMapQueryContextT contextAtIndexInChain(int index) { + return ((T)(contextChain.get(index))); } - public void initReplicatedInputBytes(Bytes replicatedInputBytes) { - this.replicatedInputBytes = replicatedInputBytes; - this.closeReplicatedInputBytesDependants(); + public void closeReplicatedChronicleMapHolderImplContextAtIndexInChainDependants() { + CompiledReplicatedMapQueryContext.this.closeQuerySegmentStagesTryFindInitLocksOfThisSegmentDependants(); } - public Bytes replicatedInputBytes() { - assert this.replicatedInputBytesInit() : "ReplicatedInputBytes should be init"; - return this.replicatedInputBytes; + public MKI keyMetaInterop(K key) { + return CompiledReplicatedMapQueryContext.this.h().metaKeyInteropProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.h().originalMetaKeyInterop, keyInterop, key); } - public void closeReplicatedInputBytes() { - if (!(this.replicatedInputBytesInit())) - return ; - - this.closeReplicatedInputBytesDependants(); - this.replicatedInputBytes = null; + public void closeKeyBytesInteropKeyMetaInteropDependants() { + CompiledReplicatedMapQueryContext.this.inputKeyInstanceValue.closeBuffer(); } - public void closeReplicatedInputBytesDependants() { - CompiledReplicatedMapQueryContext.this.replicatedInputKeyBytesValue.closeReplicatedInputKeyBytesValueGetUsingDependants(); - CompiledReplicatedMapQueryContext.this.replicatedInputValueBytesValue.closeReplicatedInputValueBytesValueGetUsingDependants(); + private long _MapEntryStages_sizeOfEverythingBeforeValue(long keySize, long valueSize) { + return (((CompiledReplicatedMapQueryContext.this.m().metaDataBytes) + (CompiledReplicatedMapQueryContext.this.m().keySizeMarshaller.sizeEncodingSize(keySize))) + keySize) + (CompiledReplicatedMapQueryContext.this.m().valueSizeMarshaller.sizeEncodingSize(valueSize)); + } + + long sizeOfEverythingBeforeValue(long keySize, long valueSize) { + return (_MapEntryStages_sizeOfEverythingBeforeValue(keySize, valueSize)) + (ReplicatedChronicleMap.ADDITIONAL_ENTRY_BYTES); + } + + public final long entrySize(long keySize, long valueSize) { + long sizeOfEverythingBeforeValue = sizeOfEverythingBeforeValue(keySize, valueSize); + return innerEntrySize(sizeOfEverythingBeforeValue, valueSize); + } + + public MVI valueMetaInterop(V value) { + return CompiledReplicatedMapQueryContext.this.m().metaValueInteropProvider.get(CompiledReplicatedMapQueryContext.this.copies, CompiledReplicatedMapQueryContext.this.m().originalMetaValueInterop, valueInterop, value); + } + + public void closeValueBytesInteropValueMetaInteropDependants() { + CompiledReplicatedMapQueryContext.this.wrappedValueInstanceValue.closeBuffer(); + CompiledReplicatedMapQueryContext.this.inputValueInstanceValue.closeBuffer(); } public Bytes inputBytes = null; @@ -2113,9 +2174,9 @@ public class CompiledReplicatedMapQueryContext inputKey = null; public boolean inputKeyInit() { @@ -2311,7 +2333,6 @@ public class CompiledReplicatedMapQueryContext 0; + } + + void initSegment() { + VanillaChronicleHash h = CompiledReplicatedMapQueryContext.this.h(); + long hashLookupOffset = h.segmentOffset(segmentIndex()); + long freeListOffset = hashLookupOffset + (h.segmentHashLookupOuterSize); + freeListBytes.storePositionAndSize(h.ms, freeListOffset, h.segmentFreeListInnerSize); + freeList.reuse(freeListBytes); + entrySpaceOffset = (freeListOffset + (h.segmentFreeListOuterSize)) + (h.segmentEntrySpaceInnerOffset); + this.closeSegmentDependants(); + } + + public long entrySpaceOffset() { + if (!(this.segmentInit())) + this.initSegment(); + + return this.entrySpaceOffset; + } + + public SingleThreadedDirectBitSet freeList() { + if (!(this.segmentInit())) + this.initSegment(); + + return this.freeList; + } + + void closeSegment() { + if (!(this.segmentInit())) + return ; + + this.closeSegmentDependants(); + entrySpaceOffset = 0; + } + + public void closeSegmentDependants() { + CompiledReplicatedMapQueryContext.this.closeEntry(); + } + + public long pos = -1; + + public long keySizeOffset; + + public long keySize; + + public long keyOffset; + + public boolean entryInit() { + return (this.pos) >= 0; + } + + public void initEntry(long pos) { + initKeySizeOffset(pos); + entryBytes.position(keySizeOffset); + keySize = CompiledReplicatedMapQueryContext.this.h().keySizeMarshaller.readSize(entryBytes); + keyOffset = entryBytes.position(); + this.pos = pos; + this.closeEntryDependants(); + } + + public void initEntry(long pos, Value key) { + initKeySizeOffset(pos); + entryBytes.position(keySizeOffset); + keySize = key.size(); + CompiledReplicatedMapQueryContext.this.h().keySizeMarshaller.writeSize(entryBytes, keySize); + keyOffset = entryBytes.position(); + key.writeTo(entryBytesAccessor, entryBytes, keyOffset); + this.pos = pos; + this.closeEntryDependants(); + } + + public void initEntryCopying(long newPos, long bytesToCopy) { + long oldKeySizeOffset = keySizeOffset; + initKeySizeOffset(newPos); + long oldKeyOffset = keyOffset; + keyOffset = (keySizeOffset) + (oldKeyOffset - oldKeySizeOffset); + Access.copy(entryBytesAccess, entryBytesAccessHandle, entryBytesAccessOffset(oldKeySizeOffset), entryBytesAccess, entryBytesAccessHandle, entryBytesAccessOffset(keySizeOffset), bytesToCopy); + pos = newPos; + this.closeEntryDependants(); + } + + public long keyOffset() { + assert this.entryInit() : "Entry should be init"; + return this.keyOffset; + } + + public long keySize() { + assert this.entryInit() : "Entry should be init"; + return this.keySize; + } + + public long keySizeOffset() { + assert this.entryInit() : "Entry should be init"; + return this.keySizeOffset; + } + + public long pos() { + assert this.entryInit() : "Entry should be init"; + return this.pos; + } + + public void closeEntry() { + if (!(this.entryInit())) + return ; + + this.closeEntryDependants(); + this.pos = -1; + } + + public void closeEntryDependants() { + CompiledReplicatedMapQueryContext.this.closeReplicatedMapQueryKeyEqualsDependants(); + CompiledReplicatedMapQueryContext.this.closeReplicatedMapEntryStagesKeyEndDependants(); + CompiledReplicatedMapQueryContext.this.closeReplicationUpdateDropChangeDependants(); + CompiledReplicatedMapQueryContext.this.closeReplicationUpdateUpdateChangeDependants(); + CompiledReplicatedMapQueryContext.this.closeReplicatedMapEntryStagesEntrySizeDependants(); + CompiledReplicatedMapQueryContext.this.closeReplicatedMapQueryDropSearchIfNestedContextsAndPresentHashLookupSlotCheckFailedDependants(); + CompiledReplicatedMapQueryContext.this.entryKey.closeEntryKeyBytesValueSizeDependants(); + CompiledReplicatedMapQueryContext.this.entryKey.closeEntryKeyBytesValueInnerGetUsingDependants(); + } + + public long keyEnd() { + return (keyOffset()) + (keySize()); + } + + public void closeReplicatedMapEntryStagesKeyEndDependants() { + CompiledReplicatedMapQueryContext.this.closeReplicatedMapEntryStagesCountValueSizeOffsetDependants(); + CompiledReplicatedMapQueryContext.this.closeReplicatedMapEntryStagesEntryEndDependants(); + CompiledReplicatedMapQueryContext.this.closeReplicationState(); + } + + private long _MapEntryStages_countValueSizeOffset() { + return keyEnd(); + } + + long countValueSizeOffset() { + return (_MapEntryStages_countValueSizeOffset()) + (ReplicatedChronicleMap.ADDITIONAL_ENTRY_BYTES); + } + + public void closeReplicatedMapEntryStagesCountValueSizeOffsetDependants() { + CompiledReplicatedMapQueryContext.this.closeValue(); + } + + long replicationBytesOffset = -1; + + public boolean replicationStateInit() { + return (this.replicationBytesOffset) >= 0; + } + + void initReplicationState() { + replicationBytesOffset = keyEnd(); + } + + void initReplicationState(long timestamp, byte identifier) { + replicationBytesOffset = keyEnd(); + entryBytes.position(replicationBytesOffset); + entryBytes.writeLong(timestamp); + entryBytes.writeByte(identifier); + } + + public long replicationBytesOffset() { + if (!(this.replicationStateInit())) + this.initReplicationState(); + + return this.replicationBytesOffset; + } + + public void closeReplicationState() { + if (!(this.replicationStateInit())) + return ; + + this.replicationBytesOffset = -1; + } + + private long timestampOffset() { + return replicationBytesOffset(); + } + + long timestamp() { + return entryBytesAccess.readLong(entryBytesAccessHandle, timestampOffset()); + } + + private long entryDeletedOffset() { + return (replicationBytesOffset()) + 9L; + } + + public void writeEntryPresent() { + entryBytesAccess.writeBoolean(entryBytesAccessHandle, entryDeletedOffset(), false); + } + + public boolean entryDeleted() { + return entryBytesAccess.readBoolean(entryBytesAccessHandle, entryDeletedOffset()); + } + + public void writeEntryDeleted() { + entryBytesAccess.writeBoolean(entryBytesAccessHandle, entryDeletedOffset(), true); + } + + private long identifierOffset() { + return (replicationBytesOffset()) + 8L; + } + + byte identifier() { + return entryBytesAccess.readByte(entryBytesAccessHandle, identifierOffset()); + } + + public void dropChange() { + CompiledReplicatedMapQueryContext.this.m().dropChange(CompiledReplicatedMapQueryContext.this.segmentIndex(), CompiledReplicatedMapQueryContext.this.pos()); + } + + public void closeReplicationUpdateDropChangeDependants() { + CompiledReplicatedMapQueryContext.this.closeReplicationUpdateUpdateChangeDependants(); + } + + int hashLookupEntrySize; + + int keyBits; + + long address = -1; + + long capacityMask; + + long capacityMask2; + + long keyMask; + + long valueMask; + + long entryMask; + + public boolean segmentHashLookupInit() { + return (this.address) >= 0; + } + + public void initSegmentHashLookup() { + long hashLookupOffset = CompiledReplicatedMapQueryContext.this.h().segmentOffset(CompiledReplicatedMapQueryContext.this.segmentIndex()); + innerInitSegmentHashLookup(((CompiledReplicatedMapQueryContext.this.h().ms.address()) + hashLookupOffset), CompiledReplicatedMapQueryContext.this.h().segmentHashLookupCapacity, CompiledReplicatedMapQueryContext.this.h().segmentHashLookupEntrySize, CompiledReplicatedMapQueryContext.this.h().segmentHashLookupKeyBits, CompiledReplicatedMapQueryContext.this.h().segmentHashLookupValueBits); + this.closeSegmentHashLookupDependants(); + } + + public void initSegmentHashLookup(long address, long capacity, int entrySize, int keyBits, int valueBits) { + innerInitSegmentHashLookup(address, capacity, entrySize, keyBits, valueBits); + this.closeSegmentHashLookupDependants(); + } + + public int hashLookupEntrySize() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.hashLookupEntrySize; + } + + public int keyBits() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.keyBits; + } + + public long address() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.address; + } + + public long capacityMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.capacityMask; + } + + public long capacityMask2() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.capacityMask2; + } + + public long entryMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.entryMask; + } + + public long keyMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.keyMask; + } + + public long valueMask() { + if (!(this.segmentHashLookupInit())) + this.initSegmentHashLookup(); + + return this.valueMask; + } + + public void closeSegmentHashLookup() { + if (!(this.segmentHashLookupInit())) + return ; + + this.closeSegmentHashLookupDependants(); + this.address = -1; + } + + public void closeSegmentHashLookupDependants() { + CompiledReplicatedMapQueryContext.this.closeHashLookupValueDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupStepDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupReadEntryDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupEmptyDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupStepBackDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupIndexToPosDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupHlPosDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupKeyDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupMaskUnsetKeyDependants(); + } + + public long value(long entry) { + return (entry >>> (keyBits())) & (valueMask()); + } + + public void closeHashLookupValueDependants() { + CompiledReplicatedMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + } + + public long step(long pos) { + return (pos += hashLookupEntrySize()) <= (capacityMask2()) ? pos : 0L; + } + + public void closeHashLookupStepDependants() { + CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + } + + public long readEntry(long pos) { + return NativeBytes.UNSAFE.getLong(((address()) + pos)); + } + + public void closeHashLookupReadEntryDependants() { + CompiledReplicatedMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + } + + public boolean empty(long entry) { + return (entry & (entryMask())) == (UNSET_ENTRY); + } + + public void closeHashLookupEmptyDependants() { + CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + } + + public void checkValueForPut(long value) { + assert (value & (~(valueMask()))) == 0L : "Value out of range, was " + value; + } + + long entry(long key, long value) { + return key | (value << (keyBits())); + } + + public void writeEntryVolatile(long pos, long prevEntry, long key, long value) { + long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); + NativeBytes.UNSAFE.putLongVolatile(null, ((address()) + pos), entry); + } + + void writeEntry(long pos, long prevEntry, long anotherEntry) { + long entry = (prevEntry & (~(entryMask()))) | (anotherEntry & (entryMask())); + NativeBytes.UNSAFE.putLong(((address()) + pos), entry); + } + + public long stepBack(long pos) { + return (pos -= hashLookupEntrySize()) >= 0 ? pos : capacityMask2(); + } + + public void closeHashLookupStepBackDependants() { + CompiledReplicatedMapQueryContext.this.closeHashLookupSearchFoundDependants(); + } + + long indexToPos(long index) { + return index * (hashLookupEntrySize()); + } + + public void closeHashLookupIndexToPosDependants() { + CompiledReplicatedMapQueryContext.this.closeHashLookupHlPosDependants(); + } + + public long hlPos(long key) { + return indexToPos((key & (capacityMask()))); + } + + public void closeHashLookupHlPosDependants() { + CompiledReplicatedMapQueryContext.this.closeSearchKey(); + } + + public void clearHashLookup() { + NativeBytes.UNSAFE.setMemory(address(), ((capacityMask2()) + (hashLookupEntrySize())), ((byte)(0))); + } + + public long key(long entry) { + return entry & (keyMask()); + } + + public void closeHashLookupKeyDependants() { + CompiledReplicatedMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); + CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); + } + + public void putValueVolatile(long pos, long value) { + checkValueForPut(value); + long currentEntry = readEntry(pos); + writeEntryVolatile(pos, currentEntry, key(currentEntry), value); + } + + void forEach(EntryConsumer action) { + for (long pos = 0L ; pos <= (capacityMask2()) ; pos += hashLookupEntrySize()) { + long entry = readEntry(pos); + if (!(empty(entry))) + action.accept(key(entry), value(entry)); + + } + } + + String hashLookupToString() { + final StringBuilder sb = new StringBuilder("{"); + forEach((long key,long value) -> sb.append(key).append('=').append(value).append(',')); + sb.append('}'); + return sb.toString(); + } + + public void writeEntry(long pos, long prevEntry, long key, long value) { + long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); + NativeBytes.UNSAFE.putLong(((address()) + pos), entry); + } + + void clearEntry(long pos, long prevEntry) { + long entry = prevEntry & (~(entryMask())); + NativeBytes.UNSAFE.putLong(((address()) + pos), entry); + } + + public long remove(long posToRemove) { + long entryToRemove = readEntry(posToRemove); + long posToShift = posToRemove; + while (true) { + posToShift = step(posToShift); + long entryToShift = readEntry(posToShift); + if (empty(entryToShift)) + break; + + long insertPos = hlPos(key(entryToShift)); + boolean cond1 = insertPos <= posToRemove; + boolean cond2 = posToRemove <= posToShift; + if ((cond1 && cond2) || ((posToShift < insertPos) && (cond1 || cond2))) { + writeEntry(posToRemove, entryToRemove, entryToShift); + posToRemove = posToShift; + entryToRemove = entryToShift; + } + } + clearEntry(posToRemove, entryToRemove); + return posToRemove; + } + + public long maskUnsetKey(long key) { + return (key &= keyMask()) != (UNSET_KEY) ? key : keyMask(); + } + + public void closeHashLookupMaskUnsetKeyDependants() { + CompiledReplicatedMapQueryContext.this.closeSearchKey(); } long segmentHeaderAddress; @@ -2424,33 +2923,27 @@ public class CompiledReplicatedMapQueryContext= (CompiledReplicatedMapQueryContext.this.h().actualChunksPerSegment)) @@ -2459,6 +2952,36 @@ public class CompiledReplicatedMapQueryContext h = CompiledReplicatedMapQueryContext.this.h(); + if (chunks > (h.maxChunksPerEntry)) + throw new IllegalArgumentException((((("Entry is too large: requires " + chunks) + " entry size chucks, ") + (h.maxChunksPerEntry)) + " is maximum.")); + + long ret = freeList().setNextNContinuousClearBits(nextPosToSearchFrom(), chunks); + if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { + if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) + freeList().clear(ret, h.actualChunksPerSegment); + + ret = freeList().setNextNContinuousClearBits(0L, chunks); + if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { + if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) + freeList().clear(ret, h.actualChunksPerSegment); + + if (chunks == 1) { + throw new IllegalStateException("Segment is full, no free entries found"); + } else { + throw new IllegalStateException((("Segment is full or has no ranges of " + chunks) + " continuous free chunks")); + } + } + updateNextPosToSearchFrom(ret, chunks); + } else { + if ((chunks == 1) || (freeList().isSet(nextPosToSearchFrom()))) { + updateNextPosToSearchFrom(ret, chunks); + } + } + return ret; + } + boolean tryFindInitLocksOfThisSegment(Object thisContext, int index) { CompiledReplicatedMapQueryContext c = CompiledReplicatedMapQueryContext.this.contextAtIndexInChain(index); if ((((c.segmentHeader()) != null) && ((c.segmentHeaderAddress()) == (segmentHeaderAddress()))) && ((c.rootContextOnThisSegment()) != null)) { @@ -2472,10 +2995,6 @@ public class CompiledReplicatedMapQueryContext= 0; + public void clearSegment() { + CompiledReplicatedMapQueryContext.this.innerWriteLock.lock(); + CompiledReplicatedMapQueryContext.this.clearHashLookup(); + freeList().clear(); + nextPosToSearchFrom(0L); + entries(0L); } - public void initSegmentHashLookup() { - long hashLookupOffset = CompiledReplicatedMapQueryContext.this.h().segmentOffset(CompiledReplicatedMapQueryContext.this.segmentIndex()); - innerInitSegmentHashLookup(((CompiledReplicatedMapQueryContext.this.h().ms.address()) + hashLookupOffset), CompiledReplicatedMapQueryContext.this.h().segmentHashLookupCapacity, CompiledReplicatedMapQueryContext.this.h().segmentHashLookupEntrySize, CompiledReplicatedMapQueryContext.this.h().segmentHashLookupKeyBits, CompiledReplicatedMapQueryContext.this.h().segmentHashLookupValueBits); - this.closeSegmentHashLookupDependants(); + public void clear() { + clearSegment(); } - public void initSegmentHashLookup(long address, long capacity, int entrySize, int keyBits, int valueBits) { - innerInitSegmentHashLookup(address, capacity, entrySize, keyBits, valueBits); - this.closeSegmentHashLookupDependants(); + public void deleted(long deleted) { + segmentHeader().deleted(segmentHeaderAddress(), deleted); } - public int hashLookupEntrySize() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.hashLookupEntrySize; + public long entries() { + return segmentHeader().size(segmentHeaderAddress()); } - public int keyBits() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.keyBits; + public long deleted() { + return segmentHeader().deleted(segmentHeaderAddress()); } - public long address() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.address; - } - - public long capacityMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.capacityMask; - } - - public long capacityMask2() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.capacityMask2; - } - - public long entryMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.entryMask; - } - - public long keyMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.keyMask; - } - - public long valueMask() { - if (!(this.segmentHashLookupInit())) - this.initSegmentHashLookup(); - - return this.valueMask; - } - - public void closeSegmentHashLookup() { - if (!(this.segmentHashLookupInit())) - return ; - - this.closeSegmentHashLookupDependants(); - this.address = -1; - } - - public void closeSegmentHashLookupDependants() { - CompiledReplicatedMapQueryContext.this.closeHashLookupStepBackDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupIndexToPosDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupMaskUnsetKeyDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupStepDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupKeyDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupEmptyDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupReadEntryDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupHlPosDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupValueDependants(); - } - - public long stepBack(long pos) { - return (pos -= hashLookupEntrySize()) >= 0 ? pos : capacityMask2(); - } - - public void closeHashLookupStepBackDependants() { - CompiledReplicatedMapQueryContext.this.closeHashLookupSearchFoundDependants(); - } - - long entry(long key, long value) { - return key | (value << (keyBits())); - } - - long indexToPos(long index) { - return index * (hashLookupEntrySize()); - } - - public void closeHashLookupIndexToPosDependants() { - CompiledReplicatedMapQueryContext.this.closeHashLookupHlPosDependants(); - } - - public long maskUnsetKey(long key) { - return (key &= keyMask()) != (UNSET_KEY) ? key : keyMask(); - } - - public void closeHashLookupMaskUnsetKeyDependants() { - CompiledReplicatedMapQueryContext.this.closeSearchKey(); - } - - void clearEntry(long pos, long prevEntry) { - long entry = prevEntry & (~(entryMask())); - NativeBytes.UNSAFE.putLong(((address()) + pos), entry); - } - - void writeEntry(long pos, long prevEntry, long anotherEntry) { - long entry = (prevEntry & (~(entryMask()))) | (anotherEntry & (entryMask())); - NativeBytes.UNSAFE.putLong(((address()) + pos), entry); - } - - public void checkValueForPut(long value) { - assert (value & (~(valueMask()))) == 0L : "Value out of range, was " + value; - } - - public long step(long pos) { - return (pos += hashLookupEntrySize()) <= (capacityMask2()) ? pos : 0L; - } - - public void closeHashLookupStepDependants() { - CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - } - - public void clearHashLookup() { - NativeBytes.UNSAFE.setMemory(address(), ((capacityMask2()) + (hashLookupEntrySize())), ((byte)(0))); - } - - public long key(long entry) { - return entry & (keyMask()); - } - - public void closeHashLookupKeyDependants() { - CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); - } - - public boolean empty(long entry) { - return (entry & (entryMask())) == (UNSET_ENTRY); - } - - public void closeHashLookupEmptyDependants() { - CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - } - - public long readEntry(long pos) { - return NativeBytes.UNSAFE.getLong(((address()) + pos)); - } - - public void closeHashLookupReadEntryDependants() { - CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); - } - - public void writeEntry(long pos, long prevEntry, long key, long value) { - long entry = (prevEntry & (~(entryMask()))) | (entry(key, value)); - NativeBytes.UNSAFE.putLong(((address()) + pos), entry); - } - - public long hlPos(long key) { - return indexToPos((key & (capacityMask()))); - } - - public void closeHashLookupHlPosDependants() { - CompiledReplicatedMapQueryContext.this.closeSearchKey(); - } - - public long remove(long posToRemove) { - long entryToRemove = readEntry(posToRemove); - long posToShift = posToRemove; - while (true) { - posToShift = step(posToShift); - long entryToShift = readEntry(posToShift); - if (empty(entryToShift)) - break; - - long insertPos = hlPos(key(entryToShift)); - boolean cond1 = insertPos <= posToRemove; - boolean cond2 = posToRemove <= posToShift; - if ((cond1 && cond2) || ((posToShift < insertPos) && (cond1 || cond2))) { - writeEntry(posToRemove, entryToRemove, entryToShift); - posToRemove = posToShift; - entryToRemove = entryToShift; - } - } - clearEntry(posToRemove, entryToRemove); - return posToRemove; + public long size() { + return (entries()) - (deleted()); } long searchKey = UNSET_KEY; @@ -2837,8 +3161,8 @@ public class CompiledReplicatedMapQueryContext>> (keyBits())) & (valueMask()); - } - - public void closeHashLookupValueDependants() { - CompiledReplicatedMapQueryContext.this.closeHashLookupSearchNextPosDependants(); - CompiledReplicatedMapQueryContext.this.closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants(); - } - public long nextPos() { long pos = CompiledReplicatedMapQueryContext.this.hashLookupPos(); while (true) { @@ -2957,188 +3270,6 @@ public class CompiledReplicatedMapQueryContext sb.append(key).append('=').append(value).append(',')); - sb.append('}'); - return sb.toString(); - } - - public boolean checkSlotContainsExpectedKeyAndValue(long value) { - long entry = CompiledReplicatedMapQueryContext.this.readEntry(CompiledReplicatedMapQueryContext.this.hashLookupPos()); - return ((CompiledReplicatedMapQueryContext.this.key(entry)) == (searchKey())) && ((CompiledReplicatedMapQueryContext.this.value(entry)) == value); - } - - public void closeHashLookupSearchCheckSlotContainsExpectedKeyAndValueDependants() { - CompiledReplicatedMapQueryContext.this.closeReplicatedMapQueryDropSearchIfNestedContextsAndPresentHashLookupSlotCheckFailedDependants(); - } - - long entrySpaceOffset = 0; - - MultiStoreBytes freeListBytes = new MultiStoreBytes(); - - public SingleThreadedDirectBitSet freeList = new SingleThreadedDirectBitSet(); - - boolean segmentInit() { - return (entrySpaceOffset) > 0; - } - - void initSegment() { - VanillaChronicleHash h = CompiledReplicatedMapQueryContext.this.h(); - long hashLookupOffset = h.segmentOffset(segmentIndex()); - long freeListOffset = hashLookupOffset + (h.segmentHashLookupOuterSize); - freeListBytes.storePositionAndSize(h.ms, freeListOffset, h.segmentFreeListInnerSize); - freeList.reuse(freeListBytes); - entrySpaceOffset = (freeListOffset + (h.segmentFreeListOuterSize)) + (h.segmentEntrySpaceInnerOffset); - this.closeSegmentDependants(); - } - - public long entrySpaceOffset() { - if (!(this.segmentInit())) - this.initSegment(); - - return this.entrySpaceOffset; - } - - public SingleThreadedDirectBitSet freeList() { - if (!(this.segmentInit())) - this.initSegment(); - - return this.freeList; - } - - void closeSegment() { - if (!(this.segmentInit())) - return ; - - this.closeSegmentDependants(); - entrySpaceOffset = 0; - } - - public void closeSegmentDependants() { - CompiledReplicatedMapQueryContext.this.closeEntry(); - } - - public long alloc(int chunks) { - VanillaChronicleHash h = CompiledReplicatedMapQueryContext.this.h(); - if (chunks > (h.maxChunksPerEntry)) - throw new IllegalArgumentException((((("Entry is too large: requires " + chunks) + " entry size chucks, ") + (h.maxChunksPerEntry)) + " is maximum.")); - - long ret = freeList().setNextNContinuousClearBits(nextPosToSearchFrom(), chunks); - if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { - if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) - freeList().clear(ret, h.actualChunksPerSegment); - - ret = freeList().setNextNContinuousClearBits(0L, chunks); - if ((ret == (DirectBitSet.NOT_FOUND)) || ((ret + chunks) > (h.actualChunksPerSegment))) { - if (((ret != (DirectBitSet.NOT_FOUND)) && ((ret + chunks) > (h.actualChunksPerSegment))) && (ret < (h.actualChunksPerSegment))) - freeList().clear(ret, h.actualChunksPerSegment); - - if (chunks == 1) { - throw new IllegalStateException("Segment is full, no free entries found"); - } else { - throw new IllegalStateException((("Segment is full or has no ranges of " + chunks) + " continuous free chunks")); - } - } - updateNextPosToSearchFrom(ret, chunks); - } else { - if ((chunks == 1) || (freeList().isSet(nextPosToSearchFrom()))) { - updateNextPosToSearchFrom(ret, chunks); - } - } - return ret; - } - - public long pos = -1; - - public long keySizeOffset; - - public long keySize; - - public long keyOffset; - - public boolean entryInit() { - return (this.pos) >= 0; - } - - public void initEntry(long pos) { - initKeySizeOffset(pos); - entryBytes.position(keySizeOffset); - keySize = CompiledReplicatedMapQueryContext.this.h().keySizeMarshaller.readSize(entryBytes); - keyOffset = entryBytes.position(); - this.pos = pos; - this.closeEntryDependants(); - } - - public void initEntry(long pos, Value key) { - initKeySizeOffset(pos); - entryBytes.position(keySizeOffset); - keySize = key.size(); - CompiledReplicatedMapQueryContext.this.h().keySizeMarshaller.writeSize(entryBytes, keySize); - keyOffset = entryBytes.position(); - key.writeTo(entryBytesAccessor, entryBytes, keyOffset); - this.pos = pos; - this.closeEntryDependants(); - } - - public void initEntryCopying(long newPos, long bytesToCopy) { - long oldKeySizeOffset = keySizeOffset; - initKeySizeOffset(newPos); - long oldKeyOffset = keyOffset; - keyOffset = (keySizeOffset) + (oldKeyOffset - oldKeySizeOffset); - Access.copy(entryBytesAccess, entryBytesAccessHandle, entryBytesAccessOffset(oldKeySizeOffset), entryBytesAccess, entryBytesAccessHandle, entryBytesAccessOffset(keySizeOffset), bytesToCopy); - pos = newPos; - this.closeEntryDependants(); - } - - public long keyOffset() { - assert this.entryInit() : "Entry should be init"; - return this.keyOffset; - } - - public long keySize() { - assert this.entryInit() : "Entry should be init"; - return this.keySize; - } - - public long keySizeOffset() { - assert this.entryInit() : "Entry should be init"; - return this.keySizeOffset; - } - - public long pos() { - assert this.entryInit() : "Entry should be init"; - return this.pos; - } - - public void closeEntry() { - if (!(this.entryInit())) - return ; - - this.closeEntryDependants(); - this.pos = -1; - } - - public void closeEntryDependants() { - CompiledReplicatedMapQueryContext.this.closeReplicatedMapQueryKeyEqualsDependants(); - CompiledReplicatedMapQueryContext.this.closeReplicatedMapQueryDropSearchIfNestedContextsAndPresentHashLookupSlotCheckFailedDependants(); - CompiledReplicatedMapQueryContext.this.entryKey.closeEntryKeyBytesValueSizeDependants(); - CompiledReplicatedMapQueryContext.this.entryKey.closeEntryKeyBytesValueInnerGetUsingDependants(); - CompiledReplicatedMapQueryContext.this.closeReplicationUpdateDropChangeDependants(); - CompiledReplicatedMapQueryContext.this.closeReplicationUpdateUpdateChangeDependants(); - CompiledReplicatedMapQueryContext.this.closeReplicatedMapEntryStagesKeyEndDependants(); - CompiledReplicatedMapQueryContext.this.closeReplicatedMapEntryStagesEntrySizeDependants(); - } - boolean keyEquals() { return ((inputKey().size()) == (CompiledReplicatedMapQueryContext.this.keySize())) && (inputKey().equivalent(((ReadAccess)(CompiledReplicatedMapQueryContext.this.entryBytesAccess)), CompiledReplicatedMapQueryContext.this.entryBytesAccessHandle, CompiledReplicatedMapQueryContext.this.entryBytesAccessOffset(CompiledReplicatedMapQueryContext.this.keyOffset()))); } @@ -3186,6 +3317,10 @@ public class CompiledReplicatedMapQueryContext entry, Value newValue) { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapQueryContext.this.m().entryOperations.replaceValue(entry, newValue); - } - - @Override - public R remove(@NotNull - MapEntry entry) { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapQueryContext.this.m().entryOperations.remove(entry); - } - - @NotNull - @Override - public Value defaultValue() { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return wrapValueAsValue(CompiledReplicatedMapQueryContext.this.m().defaultValue(CompiledReplicatedMapQueryContext.this.deprecatedMapKeyContext)); - } - - @NotNull - @Override - public Value key() { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapQueryContext.this.entryKey; - } - - @Override - public Value defaultValue(@NotNull - MapAbsentEntry absentEntry) { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapQueryContext.this.m().entryOperations.defaultValue(absentEntry); + CompiledReplicatedMapQueryContext.this.entryKey.closeEntryKeyBytesValueSizeDependants(); } @NotNull @@ -3285,37 +3385,10 @@ public class CompiledReplicatedMapQueryContext queriedKey() { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return inputKey(); - } - @Override - public R insert(@NotNull - MapAbsentEntry absentEntry, Value value) { + public long originTimestamp() { CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapQueryContext.this.m().entryOperations.insert(absentEntry, value); - } - - @NotNull - @Override - public InterProcessLock writeLock() { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapQueryContext.this.innerWriteLock; - } - - @NotNull - @Override - public Value value() { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapQueryContext.this.entryValue; - } - - @NotNull - @Override - public InterProcessLock updateLock() { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return CompiledReplicatedMapQueryContext.this.innerUpdateLock; + return timestamp(); } @Override @@ -3327,106 +3400,40 @@ public class CompiledReplicatedMapQueryContext= 0; - } - - void initReplicationState() { - replicationBytesOffset = keyEnd(); - } - - void initReplicationState(long timestamp, byte identifier) { - replicationBytesOffset = keyEnd(); - entryBytes.position(replicationBytesOffset); - entryBytes.writeLong(timestamp); - entryBytes.writeByte(identifier); - } - - public long replicationBytesOffset() { - if (!(this.replicationStateInit())) - this.initReplicationState(); - - return this.replicationBytesOffset; - } - - public void closeReplicationState() { - if (!(this.replicationStateInit())) - return ; - - this.replicationBytesOffset = -1; - } - - private long timestampOffset() { - return replicationBytesOffset(); - } - - long timestamp() { - return entryBytesAccess.readLong(entryBytesAccessHandle, timestampOffset()); - } - @Override - public long originTimestamp() { + public Value defaultValue(@NotNull + MapAbsentEntry absentEntry) { CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return timestamp(); + return CompiledReplicatedMapQueryContext.this.m().entryOperations.defaultValue(absentEntry); } - private long entryDeletedOffset() { - return (replicationBytesOffset()) + 9L; + @NotNull + @Override + public InterProcessLock updateLock() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.innerUpdateLock; } - public void writeEntryDeleted() { - entryBytesAccess.writeBoolean(entryBytesAccessHandle, entryDeletedOffset(), true); - } - - public boolean entryDeleted() { - return entryBytesAccess.readBoolean(entryBytesAccessHandle, entryDeletedOffset()); - } - - private boolean _MapQuery_entryPresent() { - return searchStatePresent(); - } - - protected boolean entryPresent() { - return (_MapQuery_entryPresent()) && (!(CompiledReplicatedMapQueryContext.this.entryDeleted())); + @Nullable + private MapAbsentEntry _MapQuery_absentEntry() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return entryPresent() ? null : CompiledReplicatedMapQueryContext.this.absent(); } @Nullable @Override public MapAbsentEntry absentEntry() { CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return entryPresent() ? null : this; + if (entryPresent()) { + return null; + } else { + if (!(searchStatePresent())) { + return CompiledReplicatedMapQueryContext.this.absentDelegating; + } else { + assert CompiledReplicatedMapQueryContext.this.entryDeleted(); + return CompiledReplicatedMapQueryContext.this.absent(); + } + } } private MapEntry _MapQuery_entry() { @@ -3439,16 +3446,65 @@ public class CompiledReplicatedMapQueryContext)(_MapQuery_entry())); } - public void writeEntryPresent() { - entryBytesAccess.writeBoolean(entryBytesAccessHandle, entryDeletedOffset(), false); + @NotNull + @Override + public Value defaultValue() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.wrapValueAsValue(CompiledReplicatedMapQueryContext.this.m().defaultValue(CompiledReplicatedMapQueryContext.this.deprecatedMapKeyContext)); } - private long identifierOffset() { - return (replicationBytesOffset()) + 8L; + @NotNull + @Override + public Value value() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.entryValue; } - byte identifier() { - return entryBytesAccess.readByte(entryBytesAccessHandle, identifierOffset()); + public Value queriedKey() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return inputKey(); + } + + @NotNull + @Override + public InterProcessLock writeLock() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.innerWriteLock; + } + + @Override + public R insert(@NotNull + MapAbsentEntry absentEntry, Value value) { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.m().entryOperations.insert(absentEntry, value); + } + + @Override + public R remove(@NotNull + MapEntry entry) { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.m().entryOperations.remove(entry); + } + + @Override + public R replaceValue(@NotNull + MapEntry entry, Value newValue) { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.m().entryOperations.replaceValue(entry, newValue); + } + + @NotNull + @Override + public Value key() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return CompiledReplicatedMapQueryContext.this.entryKey; + } + + @NotNull + @Override + public MapContext context() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return this; } @Override @@ -3457,50 +3513,40 @@ public class CompiledReplicatedMapQueryContext absentKey() { CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return inputKey(); + return CompiledReplicatedMapQueryContext.this.inputKey(); + } + + protected void putPrefix() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + boolean underUpdatedLockIsHeld = !(CompiledReplicatedMapQueryContext.this.innerUpdateLock.isHeldByCurrentThread()); + if (underUpdatedLockIsHeld) + CompiledReplicatedMapQueryContext.this.innerUpdateLock.lock(); + + boolean searchResultsNotTrusted = underUpdatedLockIsHeld || (CompiledReplicatedMapQueryContext.this.concurrentSameThreadContexts()); + if (((CompiledReplicatedMapQueryContext.this.hashLookupPosInit()) && (searchStateAbsent())) && searchResultsNotTrusted) + CompiledReplicatedMapQueryContext.this.closeHashLookupPos(); + + } + + boolean used; + + public boolean usedInit() { + return used; + } + + public void initUsed(boolean used) { + this.used = used; + } + + void closeUsed() { + if (!(this.usedInit())) + return ; + + used = false; } public long riKeySize = -1; @@ -3511,6 +3557,12 @@ public class CompiledReplicatedMapQueryContext= 0; } @@ -3519,41 +3571,25 @@ public class CompiledReplicatedMapQueryContext newValue) { - return ((valueSizeOffset()) + (CompiledReplicatedMapQueryContext.this.m().valueSizeMarshaller.sizeEncodingSize(newValue.size()))) - (keySizeOffset()); + CompiledReplicatedMapQueryContext.this.closeReplicatedMapEntryStagesEntryEndDependants(); } private long _HashEntryStages_entryEnd() { @@ -3792,6 +3842,88 @@ public class CompiledReplicatedMapQueryContext newValue) { + return ((valueSizeOffset()) + (CompiledReplicatedMapQueryContext.this.m().valueSizeMarshaller.sizeEncodingSize(newValue.size()))) - (keySizeOffset()); + } + + @Override + public long remoteTimestamp() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return newTimestamp(); + } + + @Override + public byte remoteIdentifier() { + CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); + return newIdentifier(); + } + + private boolean testTimeStampInSensibleRange() { + if ((CompiledReplicatedMapQueryContext.this.m().timeProvider) == (TimeProvider.SYSTEM)) { + long currentTime = TimeProvider.SYSTEM.currentTime(); + assert (Math.abs((currentTime - (timestamp())))) <= 100000000 : "unrealistic timestamp: " + (timestamp()); + assert (Math.abs((currentTime - (CompiledReplicatedMapQueryContext.this.newTimestamp())))) <= 100000000 : "unrealistic newTimestamp: " + (CompiledReplicatedMapQueryContext.this.newTimestamp()); + } + return true; + } + + public Bytes replicatedInputBytes = null; + + public boolean replicatedInputBytesInit() { + return (this.replicatedInputBytes) != null; + } + + public void initReplicatedInputBytes(Bytes replicatedInputBytes) { + this.replicatedInputBytes = replicatedInputBytes; + this.closeReplicatedInputBytesDependants(); + } + + public Bytes replicatedInputBytes() { + assert this.replicatedInputBytesInit() : "ReplicatedInputBytes should be init"; + return this.replicatedInputBytes; + } + + public void closeReplicatedInputBytes() { + if (!(this.replicatedInputBytesInit())) + return ; + + this.closeReplicatedInputBytesDependants(); + this.replicatedInputBytes = null; + } + + public void closeReplicatedInputBytesDependants() { + CompiledReplicatedMapQueryContext.this.replicatedInputKeyBytesValue.closeReplicatedInputKeyBytesValueGetUsingDependants(); + CompiledReplicatedMapQueryContext.this.replicatedInputValueBytesValue.closeReplicatedInputValueBytesValueGetUsingDependants(); + } + + public int allocatedChunks = 0; + + public boolean allocatedChunksInit() { + return (this.allocatedChunks) != 0; + } + + public void initAllocatedChunks(int allocatedChunks) { + this.allocatedChunks = allocatedChunks; + } + + public int allocatedChunks() { + assert this.allocatedChunksInit() : "AllocatedChunks should be init"; + return this.allocatedChunks; + } + + public void closeAllocatedChunks() { + if (!(this.allocatedChunksInit())) + return ; + + this.allocatedChunks = 0; + } + public final void freeExtraAllocatedChunks() { if (((!(CompiledReplicatedMapQueryContext.this.m().constantlySizedEntry)) && (CompiledReplicatedMapQueryContext.this.m().couldNotDetermineAlignmentBeforeAllocation)) && ((entrySizeInChunks()) < (CompiledReplicatedMapQueryContext.this.allocatedChunks()))) { CompiledReplicatedMapQueryContext.this.free(((pos()) + (entrySizeInChunks())), ((CompiledReplicatedMapQueryContext.this.allocatedChunks()) - (entrySizeInChunks()))); @@ -3800,90 +3932,31 @@ public class CompiledReplicatedMapQueryContext value) { - assert searchStateAbsent(); - long entrySize = CompiledReplicatedMapQueryContext.this.entrySize(inputKey().size(), value.size()); - CompiledReplicatedMapQueryContext.this.initEntryAndKey(entrySize); - CompiledReplicatedMapQueryContext.this.initValue(value); - CompiledReplicatedMapQueryContext.this.freeExtraAllocatedChunks(); - CompiledReplicatedMapQueryContext.this.putNewVolatile(CompiledReplicatedMapQueryContext.this.pos()); - } - public void writeValueAndPutPos(Value value) { initValue(value); freeExtraAllocatedChunks(); CompiledReplicatedMapQueryContext.this.putValueVolatile(CompiledReplicatedMapQueryContext.this.hashLookupPos(), pos()); } - private void _MapEntryStages_putValueDeletedEntry(Value newValue) { - assert CompiledReplicatedMapQueryContext.this.innerUpdateLock.isHeldByCurrentThread(); - int newSizeInChunks; - long entryStartOffset = keySizeOffset(); - long newSizeOfEverythingBeforeValue = -1; - boolean newValueSizeIsDifferent = (newValue.size()) != (valueSize()); - if (newValueSizeIsDifferent) { - newSizeOfEverythingBeforeValue = newSizeOfEverythingBeforeValue(newValue); - long newValueOffset = CompiledReplicatedMapQueryContext.this.m().alignment.alignAddr((entryStartOffset + newSizeOfEverythingBeforeValue)); - long newEntrySize = (newValueOffset + (newValue.size())) - entryStartOffset; - newSizeInChunks = CompiledReplicatedMapQueryContext.this.m().inChunks(newEntrySize); - } else { - newSizeInChunks = entrySizeInChunks(); - } - if ((((pos()) + newSizeInChunks) < (CompiledReplicatedMapQueryContext.this.freeList().size())) && (CompiledReplicatedMapQueryContext.this.freeList().allClear(pos(), ((pos()) + newSizeInChunks)))) { - CompiledReplicatedMapQueryContext.this.freeList().set(pos(), ((pos()) + newSizeInChunks)); - CompiledReplicatedMapQueryContext.this.innerWriteLock.lock(); - CompiledReplicatedMapQueryContext.this.incrementSegmentEntriesIfNeeded(); - if (newValueSizeIsDifferent) { - initValueAgain(newValue); - } else { - writeValueGuarded(newValue); - } - } else { - if (newValueSizeIsDifferent) { - assert newSizeOfEverythingBeforeValue >= 0; - } else { - newSizeOfEverythingBeforeValue = newSizeOfEverythingBeforeValue(newValue); - } - long entrySize = innerEntrySize(newSizeOfEverythingBeforeValue, newValue.size()); - if (newValueSizeIsDifferent) { - CompiledReplicatedMapQueryContext.this.initEntryAndKeyCopying(entrySize, ((valueSizeOffset()) - entryStartOffset)); - initValue(newValue); - } else { - long oldValueSizeOffset = valueSizeOffset(); - long oldValueSize = valueSize(); - long oldValueOffset = valueOffset(); - CompiledReplicatedMapQueryContext.this.initEntryAndKeyCopying(entrySize, ((valueOffset()) - entryStartOffset)); - initValueWithoutSize(newValue, oldValueSizeOffset, oldValueSize, oldValueOffset); - } - freeExtraAllocatedChunks(); - } - CompiledReplicatedMapQueryContext.this.putValueVolatile(CompiledReplicatedMapQueryContext.this.hashLookupPos(), pos()); + public void initEntryAndKey(long entrySize) { + initAllocatedChunks(CompiledReplicatedMapQueryContext.this.h().inChunks(entrySize)); + CompiledReplicatedMapQueryContext.this.initEntry(CompiledReplicatedMapQueryContext.this.alloc(allocatedChunks()), CompiledReplicatedMapQueryContext.this.inputKey()); + incrementSegmentEntriesIfNeeded(); } - public void putValueDeletedEntry(Value newValue) { - throw new AssertionError("Replicated Map doesn\'t remove entries truly, yet"); + void putEntry(Value value) { + assert CompiledReplicatedMapQueryContext.this.searchStateAbsent(); + long entrySize = CompiledReplicatedMapQueryContext.this.entrySize(CompiledReplicatedMapQueryContext.this.inputKey().size(), value.size()); + CompiledReplicatedMapQueryContext.this.initEntryAndKey(entrySize); + CompiledReplicatedMapQueryContext.this.initValue(value); + CompiledReplicatedMapQueryContext.this.freeExtraAllocatedChunks(); + CompiledReplicatedMapQueryContext.this.putNewVolatile(CompiledReplicatedMapQueryContext.this.pos()); } - @Override - public void doInsert(Value value) { - putPrefix(); - if (!(searchStatePresent())) { - if (searchStateDeleted()) { - CompiledReplicatedMapQueryContext.this.putValueDeletedEntry(value); - } else { - putEntry(value); - } - CompiledReplicatedMapQueryContext.this.incrementModCountGuarded(); - setSearchStateGuarded(SearchState.PRESENT); - } else { - throw new IllegalStateException("Entry is present in the map when doInsert() is called"); - } - } - - public void innerRemoveEntryExceptHashLookupUpdate() { - CompiledReplicatedMapQueryContext.this.free(pos(), entrySizeInChunks()); - CompiledReplicatedMapQueryContext.this.entries(((CompiledReplicatedMapQueryContext.this.entries()) - 1L)); - CompiledReplicatedMapQueryContext.this.incrementModCountGuarded(); + public void initEntryAndKeyCopying(long entrySize, long bytesToCopy) { + initAllocatedChunks(CompiledReplicatedMapQueryContext.this.h().inChunks(entrySize)); + CompiledReplicatedMapQueryContext.this.initEntryCopying(CompiledReplicatedMapQueryContext.this.alloc(allocatedChunks()), bytesToCopy); + incrementSegmentEntriesIfNeeded(); } private void _MapEntryStages_relocation(Value newValue, long newSizeOfEverythingBeforeValue) { @@ -3957,12 +4030,47 @@ public class CompiledReplicatedMapQueryContext newValue) { putPrefix(); @@ -3975,42 +4083,85 @@ public class CompiledReplicatedMapQueryContext newValue) { + assert CompiledReplicatedMapQueryContext.this.innerUpdateLock.isHeldByCurrentThread(); + int newSizeInChunks; + long entryStartOffset = keySizeOffset(); + long newSizeOfEverythingBeforeValue = -1; + boolean newValueSizeIsDifferent = (newValue.size()) != (valueSize()); + if (newValueSizeIsDifferent) { + newSizeOfEverythingBeforeValue = newSizeOfEverythingBeforeValue(newValue); + long newValueOffset = CompiledReplicatedMapQueryContext.this.m().alignment.alignAddr((entryStartOffset + newSizeOfEverythingBeforeValue)); + long newEntrySize = (newValueOffset + (newValue.size())) - entryStartOffset; + newSizeInChunks = CompiledReplicatedMapQueryContext.this.m().inChunks(newEntrySize); + } else { + newSizeInChunks = entrySizeInChunks(); + } + if ((((pos()) + newSizeInChunks) < (CompiledReplicatedMapQueryContext.this.freeList().size())) && (CompiledReplicatedMapQueryContext.this.freeList().allClear(pos(), ((pos()) + newSizeInChunks)))) { + CompiledReplicatedMapQueryContext.this.freeList().set(pos(), ((pos()) + newSizeInChunks)); + CompiledReplicatedMapQueryContext.this.innerWriteLock.lock(); + CompiledReplicatedMapQueryContext.this.incrementSegmentEntriesIfNeeded(); + if (newValueSizeIsDifferent) { + initValueAgain(newValue); + } else { + writeValueGuarded(newValue); + } + } else { + if (newValueSizeIsDifferent) { + assert newSizeOfEverythingBeforeValue >= 0; + } else { + newSizeOfEverythingBeforeValue = newSizeOfEverythingBeforeValue(newValue); + } + long entrySize = innerEntrySize(newSizeOfEverythingBeforeValue, newValue.size()); + if (newValueSizeIsDifferent) { + CompiledReplicatedMapQueryContext.this.initEntryAndKeyCopying(entrySize, ((valueSizeOffset()) - entryStartOffset)); + initValue(newValue); + } else { + long oldValueSizeOffset = valueSizeOffset(); + long oldValueSize = valueSize(); + long oldValueOffset = valueOffset(); + CompiledReplicatedMapQueryContext.this.initEntryAndKeyCopying(entrySize, ((valueOffset()) - entryStartOffset)); + initValueWithoutSize(newValue, oldValueSizeOffset, oldValueSize, oldValueOffset); + } + freeExtraAllocatedChunks(); + } + CompiledReplicatedMapQueryContext.this.putValueVolatile(CompiledReplicatedMapQueryContext.this.hashLookupPos(), pos()); } - private boolean testTimeStampInSensibleRange() { - if ((CompiledReplicatedMapQueryContext.this.m().timeProvider) == (TimeProvider.SYSTEM)) { - long currentTime = TimeProvider.SYSTEM.currentTime(); - assert (Math.abs((currentTime - (timestamp())))) <= 100000000 : "unrealistic timestamp: " + (timestamp()); - assert (Math.abs((currentTime - (CompiledReplicatedMapQueryContext.this.newTimestamp())))) <= 100000000 : "unrealistic newTimestamp: " + (CompiledReplicatedMapQueryContext.this.newTimestamp()); + public void putValueDeletedEntry(Value newValue) { + throw new AssertionError("Replicated Map doesn\'t remove entries truly, yet"); + } + + private void _MapAbsent_doInsert(Value value) { + CompiledReplicatedMapQueryContext.this.putPrefix(); + if (!(CompiledReplicatedMapQueryContext.this.searchStatePresent())) { + if (CompiledReplicatedMapQueryContext.this.searchStateDeleted()) { + CompiledReplicatedMapQueryContext.this.putValueDeletedEntry(value); + } else { + putEntry(value); + } + CompiledReplicatedMapQueryContext.this.incrementModCountGuarded(); + CompiledReplicatedMapQueryContext.this.setSearchStateGuarded(SearchState.PRESENT); + } else { + throw new IllegalStateException("Entry is present in the map when doInsert() is called"); } - return true; } @Override - public long remoteTimestamp() { - CompiledReplicatedMapQueryContext.this.checkOnEachPublicOperation(); - return newTimestamp(); - } - - private boolean updatedReplicationState = false; - - public boolean updatedReplicationStateInit() { - return (this.updatedReplicationState) != false; - } - - public void initUpdatedReplicationState() { - initReplicationState(CompiledReplicatedMapQueryContext.this.newTimestamp(), CompiledReplicatedMapQueryContext.this.newIdentifier()); - updatedReplicationState = true; - } - - public void closeUpdatedReplicationState() { - if (!(this.updatedReplicationStateInit())) - return ; - - this.updatedReplicationState = false; + public void doInsert(Value value) { + CompiledReplicatedMapQueryContext.this.putPrefix(); + if (!(CompiledReplicatedMapQueryContext.this.entryPresent())) { + if (!(CompiledReplicatedMapQueryContext.this.searchStatePresent())) { + putEntry(value); + CompiledReplicatedMapQueryContext.this.setSearchStateGuarded(SearchState.PRESENT); + } else { + CompiledReplicatedMapQueryContext.this.innerDefaultReplaceValue(value); + CompiledReplicatedMapQueryContext.this.deleted(((CompiledReplicatedMapQueryContext.this.deleted()) - 1)); + } + CompiledReplicatedMapQueryContext.this.incrementModCountGuarded(); + CompiledReplicatedMapQueryContext.this.writeEntryPresent(); + } else { + throw new IllegalStateException("Entry is absent in the map when doInsert() is called"); + } } } diff --git a/src/main/java/net/openhft/chronicle/set/SetFromMap.java b/src/main/java/net/openhft/chronicle/set/SetFromMap.java index 6b9358da4..039d353c8 100644 --- a/src/main/java/net/openhft/chronicle/set/SetFromMap.java +++ b/src/main/java/net/openhft/chronicle/set/SetFromMap.java @@ -19,6 +19,7 @@ package net.openhft.chronicle.set; import net.openhft.chronicle.hash.KeyContext; +import net.openhft.chronicle.hash.Value; import net.openhft.chronicle.hash.serialization.internal.DummyValue; import net.openhft.chronicle.map.ChronicleMap; import org.jetbrains.annotations.NotNull; @@ -136,7 +137,14 @@ class SetFromMap extends AbstractSet @Override public ExternalSetQueryContext queryContext(E key) { //TODO - return null; + throw new UnsupportedOperationException(); + } + + @NotNull + @Override + public ExternalSetQueryContext queryContext(Value key) { + //TODO + throw new UnsupportedOperationException(); } @Override diff --git a/src/test/java/net/openhft/chronicle/map/BiMapTest.java b/src/test/java/net/openhft/chronicle/map/BiMapTest.java index d48c52541..65e2eb73b 100644 --- a/src/test/java/net/openhft/chronicle/map/BiMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/BiMapTest.java @@ -151,7 +151,7 @@ public class BiMapTest { @Override public DualLockSuccess remove(@NotNull MapEntry entry) { - try (ExternalMapQueryContext rq = reverse.queryContext(entry.value().get())) { + try (ExternalMapQueryContext rq = reverse.queryContext(entry.value())) { if (!rq.updateLock().tryLock()) { if (entry.context() instanceof MapQueryContext) return FAIL; @@ -178,7 +178,7 @@ public class BiMapTest { @Override public DualLockSuccess insert(@NotNull MapAbsentEntry absentEntry, Value value) { - try (ExternalMapQueryContext rq = reverse.queryContext(value.get())) { + try (ExternalMapQueryContext rq = reverse.queryContext(value)) { if (!rq.updateLock().tryLock()) return FAIL; MapAbsentEntry reverseAbsentEntry = rq.absentEntry(); diff --git a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java index 2ae17629e..5cd6e1a35 100644 --- a/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java +++ b/src/test/java/net/openhft/chronicle/map/CHMUseCasesTest.java @@ -78,9 +78,9 @@ public class CHMUseCasesTest { TypeOfMap.SIMPLE }, -// { -// TypeOfMap.REPLICATED -// }, + { + TypeOfMap.REPLICATED + }, // it pointless to run these test as the Function and UnaryOperator are not // serializable as inner classes adn the getUsingLock is not supported by the diff --git a/src/test/java/net/openhft/chronicle/map/ChannelReplicationTest.java b/src/test/java/net/openhft/chronicle/map/ChannelReplicationTest.java index e89818ca7..bdc25201a 100644 --- a/src/test/java/net/openhft/chronicle/map/ChannelReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/ChannelReplicationTest.java @@ -35,7 +35,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class ChannelReplicationTest { private ChronicleMap map1a; diff --git a/src/test/java/net/openhft/chronicle/map/PostChannelCreationTest.java b/src/test/java/net/openhft/chronicle/map/PostChannelCreationTest.java index bf6da7620..0fc9d8f8d 100644 --- a/src/test/java/net/openhft/chronicle/map/PostChannelCreationTest.java +++ b/src/test/java/net/openhft/chronicle/map/PostChannelCreationTest.java @@ -35,7 +35,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class PostChannelCreationTest { private ChronicleMap map1a; diff --git a/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest.java index 9cb189e44..28ab09fef 100644 --- a/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/ReplicatedChronicleMapTest.java @@ -34,7 +34,6 @@ import static org.junit.Assert.*; * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. Then modified by the Open HFT team. */ -@Ignore public class ReplicatedChronicleMapTest extends JSR166TestCase { ChronicleMap newShmIntString() throws IOException { return ChronicleMapBuilder.of(Integer.class, CharSequence.class) diff --git a/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java b/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java index 909d54eb1..d76acf671 100644 --- a/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java +++ b/src/test/java/net/openhft/chronicle/map/ReplicationCheckingMap.java @@ -18,6 +18,7 @@ package net.openhft.chronicle.map; +import net.openhft.chronicle.hash.Value; import net.openhft.chronicle.hash.function.SerializableFunction; import org.jetbrains.annotations.NotNull; import org.junit.Assert; @@ -292,6 +293,12 @@ public class ReplicationCheckingMap implements ChronicleMap { throw new UnsupportedOperationException(); } + @NotNull + @Override + public ExternalMapQueryContext queryContext(Value key) { + throw new UnsupportedOperationException(); + } + @Override public File file() { throw new UnsupportedOperationException(); diff --git a/src/test/java/net/openhft/chronicle/map/SingleMapChannelTest.java b/src/test/java/net/openhft/chronicle/map/SingleMapChannelTest.java index da2ddedbb..4b64cdd3d 100644 --- a/src/test/java/net/openhft/chronicle/map/SingleMapChannelTest.java +++ b/src/test/java/net/openhft/chronicle/map/SingleMapChannelTest.java @@ -35,7 +35,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class SingleMapChannelTest { private ChronicleMap map1a; diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3VoidValueTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3VoidValueTest.java index 4461feee8..a12bbb574 100755 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3VoidValueTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3VoidValueTest.java @@ -39,7 +39,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class TCPSocketReplication3VoidValueTest { private ChronicleMap map1; diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayPutReturnsNullTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayPutReturnsNullTest.java index 614dd8830..975ff78f5 100755 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayPutReturnsNullTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayPutReturnsNullTest.java @@ -38,7 +38,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class TCPSocketReplication3wayPutReturnsNullTest { private ChronicleMap map1; diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayTest.java index 901a1d8ad..5be0f4e7d 100755 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication3wayTest.java @@ -36,7 +36,6 @@ import static org.junit.Assert.*; * * @author Rob Austin. */ -@Ignore public class TCPSocketReplication3wayTest { private ChronicleMap map1; diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication4WayMapTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication4WayMapTest.java index e335b63d1..930f99310 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplication4WayMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplication4WayMapTest.java @@ -37,7 +37,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class TCPSocketReplication4WayMapTest { private ChronicleMap map1; @@ -124,7 +123,6 @@ public class TCPSocketReplication4WayMapTest { @Test - @Ignore("TODO fix this test.") public void testBufferOverflowPutIfAbsent() throws IOException, InterruptedException { for (int i = 0; i < 1024; i++) { map1.putIfAbsent(i, "EXAMPLE-1"); diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java index e85789a7a..7b65e9163 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationIntValueTest.java @@ -44,7 +44,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class TCPSocketReplicationIntValueTest { static int s_port = 8010; diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java index 83701a605..1ce65f2c6 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationNativeTest.java @@ -40,7 +40,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class TCPSocketReplicationNativeTest { private ChronicleMap map1; diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationPostConnectionTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationPostConnectionTest.java index e418d53d0..78e714f82 100755 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationPostConnectionTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationPostConnectionTest.java @@ -37,7 +37,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class TCPSocketReplicationPostConnectionTest { private static int s_port = 13050; diff --git a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java index 7e13832cb..fe1e394be 100644 --- a/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/TCPSocketReplicationTest.java @@ -40,7 +40,6 @@ import static org.junit.Assert.assertTrue; * * @author Rob Austin. */ -@Ignore public class TCPSocketReplicationTest { static int s_port = 12050; @@ -122,7 +121,6 @@ public class TCPSocketReplicationTest { assertTrue(!map1.isEmpty()); } - @Ignore("failed on TC") @Test public void test2() throws IOException, InterruptedException { diff --git a/src/test/java/net/openhft/chronicle/map/TcpReplicationLargeEntryTest.java b/src/test/java/net/openhft/chronicle/map/TcpReplicationLargeEntryTest.java index 1b4537cc2..ada4f0d9b 100644 --- a/src/test/java/net/openhft/chronicle/map/TcpReplicationLargeEntryTest.java +++ b/src/test/java/net/openhft/chronicle/map/TcpReplicationLargeEntryTest.java @@ -17,7 +17,6 @@ import java.util.concurrent.TimeUnit; /** * @author Rob Austin. */ -@Ignore public class TcpReplicationLargeEntryTest { private ChronicleMap map1; diff --git a/src/test/java/net/openhft/chronicle/map/TcpReplicationSoakTest.java b/src/test/java/net/openhft/chronicle/map/TcpReplicationSoakTest.java index ba390aa5f..3daef1239 100644 --- a/src/test/java/net/openhft/chronicle/map/TcpReplicationSoakTest.java +++ b/src/test/java/net/openhft/chronicle/map/TcpReplicationSoakTest.java @@ -21,7 +21,6 @@ import java.util.concurrent.TimeUnit; /** * @author Rob Austin. */ -@Ignore public class TcpReplicationSoakTest { diff --git a/src/test/java/net/openhft/chronicle/map/TcpReplicationWithAcquireContextTest.java b/src/test/java/net/openhft/chronicle/map/TcpReplicationWithAcquireContextTest.java index 4a342d225..d051aa8bc 100644 --- a/src/test/java/net/openhft/chronicle/map/TcpReplicationWithAcquireContextTest.java +++ b/src/test/java/net/openhft/chronicle/map/TcpReplicationWithAcquireContextTest.java @@ -11,7 +11,6 @@ import java.net.InetSocketAddress; /** * @author Rob Austin. */ -@Ignore public class TcpReplicationWithAcquireContextTest { diff --git a/src/test/java/net/openhft/chronicle/map/TcpTimeBasedReplicationSoakTest.java b/src/test/java/net/openhft/chronicle/map/TcpTimeBasedReplicationSoakTest.java index ba5547286..7d7ff6292 100644 --- a/src/test/java/net/openhft/chronicle/map/TcpTimeBasedReplicationSoakTest.java +++ b/src/test/java/net/openhft/chronicle/map/TcpTimeBasedReplicationSoakTest.java @@ -19,7 +19,6 @@ import java.util.concurrent.TimeUnit; /** * @author Rob Austin. */ -@Ignore public class TcpTimeBasedReplicationSoakTest { @@ -102,7 +101,6 @@ public class TcpTimeBasedReplicationSoakTest { } - @Ignore("fails on TC") @Test public void testSoakTestWithRandomData() throws IOException, InterruptedException { diff --git a/src/test/java/net/openhft/chronicle/map/TestEntriesSpanningSeveralChunksReplicationHub.java b/src/test/java/net/openhft/chronicle/map/TestEntriesSpanningSeveralChunksReplicationHub.java index 420f68cc4..77bc73711 100644 --- a/src/test/java/net/openhft/chronicle/map/TestEntriesSpanningSeveralChunksReplicationHub.java +++ b/src/test/java/net/openhft/chronicle/map/TestEntriesSpanningSeveralChunksReplicationHub.java @@ -19,7 +19,6 @@ import static org.junit.Assert.assertEquals; /** * @author Rob Austin. */ -@Ignore public class TestEntriesSpanningSeveralChunksReplicationHub { diff --git a/src/test/java/net/openhft/chronicle/map/TestReplication.java b/src/test/java/net/openhft/chronicle/map/TestReplication.java index 73b415b5f..1a573b3f9 100644 --- a/src/test/java/net/openhft/chronicle/map/TestReplication.java +++ b/src/test/java/net/openhft/chronicle/map/TestReplication.java @@ -12,7 +12,6 @@ import java.net.InetSocketAddress; /** * Created by Rob Austin */ -@Ignore public class TestReplication { diff --git a/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java b/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java index e6f82939b..4edcec24b 100644 --- a/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/TimeBasedReplicationTest.java @@ -40,7 +40,6 @@ import static org.junit.Assert.assertEquals; /** * @author Rob Austin. */ -@Ignore public class TimeBasedReplicationTest extends JSR166TestCase { public static final byte IDENTIFIER = 1; diff --git a/src/test/java/net/openhft/chronicle/map/TwoMapOnDifferentServersTest.java b/src/test/java/net/openhft/chronicle/map/TwoMapOnDifferentServersTest.java index c5688f3b9..0839ebfad 100644 --- a/src/test/java/net/openhft/chronicle/map/TwoMapOnDifferentServersTest.java +++ b/src/test/java/net/openhft/chronicle/map/TwoMapOnDifferentServersTest.java @@ -36,7 +36,6 @@ import static org.junit.Assert.assertTrue; /** * @author Rob Austin. */ -@Ignore public class TwoMapOnDifferentServersTest { private ChronicleMap map1; diff --git a/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java b/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java index ade7c7e5a..ba4614e19 100644 --- a/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java +++ b/src/test/java/net/openhft/chronicle/map/UDPSocketReplicationTest.java @@ -34,7 +34,6 @@ import java.net.Inet4Address; * * @author Rob Austin. */ -@Ignore public class UDPSocketReplicationTest { private ChronicleMap map2; @@ -78,7 +77,6 @@ public class UDPSocketReplicationTest { } @Test - @Ignore public void testBufferOverflow() throws IOException, InterruptedException { for (int i = 0; i < 1024; i++) { diff --git a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java index 33d85f070..f5a64fd73 100755 --- a/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java +++ b/src/test/java/net/openhft/chronicle/map/jsr166/map/StatelessChronicleMapTest.java @@ -18,6 +18,7 @@ package net.openhft.chronicle.map.jsr166.map; +import net.openhft.chronicle.hash.Value; import net.openhft.chronicle.hash.function.SerializableFunction; import net.openhft.chronicle.hash.replication.TcpTransportAndNetworkConfig; import net.openhft.chronicle.map.*; @@ -120,6 +121,12 @@ public class StatelessChronicleMapTest extends JSR166TestCase { return d.queryContext(key); } + @NotNull + @Override + public ExternalMapQueryContext queryContext(Value key) { + return d.queryContext(key); + } + @Override public V get(Object key) { return d.get(key); diff --git a/src/test/java/net/openhft/lang/values/DoubleArrayTest.java b/src/test/java/net/openhft/lang/values/DoubleArrayTest.java index 307914e6e..1a466daa6 100755 --- a/src/test/java/net/openhft/lang/values/DoubleArrayTest.java +++ b/src/test/java/net/openhft/lang/values/DoubleArrayTest.java @@ -68,7 +68,6 @@ public class DoubleArrayTest { } @Test - @Ignore public void addToAMap2() { DoubleArray.HACK = false; DoubleArray a = new DoubleArray(10);