From 3634d6d0b364ca70c4f5b84949699ad443c94625 Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Fri, 26 Jun 2015 14:58:35 +0100 Subject: [PATCH] Fix test for replication --- .../chronicle/map/VanillaChronicleMap.java | 1000 +++++++++-------- 1 file changed, 511 insertions(+), 489 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java index 6dba0ac35..af2ce9369 100755 --- a/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/VanillaChronicleMap.java @@ -262,6 +262,7 @@ class VanillaChronicleMap, "of more than 4 GB. The configured map requires %.2f GB of off-heap memory.\n", offHeapMapSizeInGb); } + if (Boolean.getBoolean("win.check")) try { long freePhysicalMemory = Jvm.freePhysicalMemoryOnWindowsInBytes(); if (offHeapMapSize > freePhysicalMemory * 0.9) { @@ -279,6 +280,37 @@ class VanillaChronicleMap, } } + + void warnOnWindows() { + if (!Jvm.isWindows()) + return; + long offHeapMapSize = sizeInBytes(); + long oneGb = GIGABYTES.toBytes(1L); + double offHeapMapSizeInGb = offHeapMapSize * 1.0 / oneGb; + if (offHeapMapSize > GIGABYTES.toBytes(4L)) { + System.out.printf( + "WARNING: On Windows, you probably cannot create a ChronicleMap\n" + + "of more than 4 GB. The configured map requires %.2f GB of off-heap memory.\n", + offHeapMapSizeInGb); + } + if (Boolean.getBoolean("win.check")) + try { + long freePhysicalMemory = Jvm.freePhysicalMemoryOnWindowsInBytes(); + if (offHeapMapSize > freePhysicalMemory * 0.9) { + double freePhysicalMemoryInGb = freePhysicalMemory * 1.0 / oneGb; + System.out.printf( + "WARNING: On Windows, you probably cannot create a ChronicleMap\n" + + "of more than 90%% of available free memory in the system.\n" + + "The configured map requires %.2f GB of off-heap memory.\n" + + "There is only %.2f GB of free physical memory in the system.\n", + offHeapMapSizeInGb, freePhysicalMemoryInGb); + + } + } catch (IOException e) { + // ignore -- anyway we just warn the user + } + } + final long createMappedStoreAndSegments(File file) throws IOException { warnOnWindows(); return createMappedStoreAndSegments(new MappedStore(file, FileChannel.MapMode.READ_WRITE, @@ -445,81 +477,6 @@ class VanillaChronicleMap, true, readValueToLazyBytes, true); } - static abstract class ReadValueToBytes implements ReadValue { - SizeMarshaller valueSizeMarshaller; - - abstract Bytes bytes(long valueSize); - - void valueSizeMarshaller(SizeMarshaller valueSizeMarshaller) { - this.valueSizeMarshaller = valueSizeMarshaller; - } - - @Override - public Bytes readValue(@NotNull ThreadLocalCopies copies, Bytes entry, Bytes usingBytes, - long valueSize) { - usingBytes = bytes(valueSize); - valueSizeMarshaller.writeSize(usingBytes, valueSize); - usingBytes.write(entry, entry.position(), valueSize); - entry.skip(valueSize); - return usingBytes; - } - } - - private static class ReadValueToOutputBytes extends ReadValueToBytes { - private TcpReplicator.TcpSocketChannelEntryWriter output; - - void reuse(SizeMarshaller valueSizeMarshaller, - TcpReplicator.TcpSocketChannelEntryWriter output) { - valueSizeMarshaller(valueSizeMarshaller); - this.output = output; - } - - @Override - Bytes bytes(long valueSize) { - return output.in(); - } - - @Override - public Bytes readValue(@NotNull ThreadLocalCopies copies, Bytes entry, Bytes usingBytes, - long valueSize) { - long totalSize = 1 + valueSizeMarshaller.sizeEncodingSize(valueSize) + valueSize; - output.ensureBufferSize(totalSize); - output.in().writeBoolean(false); - return super.readValue(copies, entry, usingBytes, valueSize); - } - - @Override - public Bytes readNull() { - output.ensureBufferSize(1); - output.in().writeBoolean(true); - return null; - } - } - - private static class ReadValueToLazyBytes extends ReadValueToBytes { - DirectBytes lazyBytes; - - @Override - Bytes bytes(long valueSize) { - valueSize = valueSizeMarshaller.sizeEncodingSize(valueSize) + valueSize; - if (lazyBytes != null) { - if (lazyBytes.capacity() < valueSize) { - DirectStore store = (DirectStore) lazyBytes.store(); - store.resize(valueSize, false); - lazyBytes = store.bytes(); - } - lazyBytes.clear(); - return lazyBytes; - } - return lazyBytes = new DirectStore(valueSize).bytes(); - } - - @Override - public Bytes readNull() { - return null; - } - } - @Override public final V put(K key, V value) { return put1(key, value, true); @@ -564,8 +521,6 @@ class VanillaChronicleMap, segmentHash, replaceIfPresent, readValue, resultUnused); } - enum LockType {READ_LOCK, WRITE_LOCK} - @Override @SuppressWarnings("unchecked") public final V get(Object key) { @@ -694,7 +649,6 @@ class VanillaChronicleMap, } } - @Override public synchronized void getAll(File toFile) throws IOException { JsonSerializer.getAll(toFile, this, emptyList()); @@ -719,17 +673,6 @@ class VanillaChronicleMap, return newInstance(kClass, true); } - @Override - public Class keyClass() { - return kClass; - } - - @Override - public Class valueClass() { - return vClass; - } - - static T newInstance(Class aClass, boolean isKey) { try { return isKey ? DataValueClasses.newInstance(aClass) : @@ -762,6 +705,15 @@ class VanillaChronicleMap, } } + @Override + public Class keyClass() { + return kClass; + } + + @Override + public Class valueClass() { + return vClass; + } @NotNull @Override @@ -839,11 +791,6 @@ class VanillaChronicleMap, return this; } - @SuppressWarnings("unchecked") - final InstanceOrBytesToInstance valueIdentity() { - return this; - } - transient InstanceOrBytesToInstance keyBytesToInstance = new InstanceOrBytesToInstance() { @Override @@ -854,6 +801,11 @@ class VanillaChronicleMap, } }; + @SuppressWarnings("unchecked") + final InstanceOrBytesToInstance valueIdentity() { + return this; + } + transient InstanceOrBytesToInstance valueBytesToInstance = new InstanceOrBytesToInstance() { @Override @@ -862,15 +814,6 @@ class VanillaChronicleMap, } }; - transient InstanceOrBytesToInstance outputValueBytesToInstance = - new InstanceOrBytesToInstance() { - @Override - public V toInstance(@NotNull ThreadLocalCopies copies, Bytes outputBytes, long size) { - outputBytes.position(outputBytes.position() - size); - return readValue(copies, outputBytes, null, size); - } - }; - @Override public final boolean containsKey(final Object k) { checkKey(k); @@ -889,6 +832,15 @@ class VanillaChronicleMap, } } + transient InstanceOrBytesToInstance outputValueBytesToInstance = + new InstanceOrBytesToInstance() { + @Override + public V toInstance(@NotNull ThreadLocalCopies copies, Bytes outputBytes, long size) { + outputBytes.position(outputBytes.position() - size); + return readValue(copies, outputBytes, null, size); + } + }; + final boolean containsBytesKey(Bytes key) throws InterruptedException { return containsKey(null, DelegatingMetaBytesInterop.>instance(), @@ -950,87 +902,6 @@ class VanillaChronicleMap, return valueInteropProvider.get(copies, originalValueInterop); } - static class GetRemoteBytesValueInterops - implements GetValueInterops, - MetaBytesInterop { - private static final long serialVersionUID = 0L; - private final MultiStoreBytes valueBytes = new MultiStoreBytes(); - - private Bytes remoteEntryBytes; - private long offset; - private SizeMarshaller valueSizeMarshaller; - - private long valueSize; - - MultiStoreBytes getValueBytes(Bytes remoteEntryBytes, long offset) { - this.remoteEntryBytes = remoteEntryBytes; - this.offset = offset; - this.valueSizeMarshaller = null; - valueSize = -1L; - return valueBytes; - } - - void valueSize(long valueSize) { - this.valueSize = valueSize; - } - - void valueSizeMarshaller(SizeMarshaller valueSizeMarshaller) { - this.valueSizeMarshaller = valueSizeMarshaller; - } - - @Override - public GetRemoteBytesValueInterops getMetaValueInterop( - @NotNull ThreadLocalCopies copies, Void valueInterop, MultiStoreBytes value) { - value.setBytesOffset(remoteEntryBytes, offset); - if (valueSize == -1L) - valueSize = valueSizeMarshaller.readSize(value); - value.limit(value.position() + valueSize); - return this; - } - - @Override - public Void getValueInterop(@NotNull ThreadLocalCopies copies) { - return null; - } - - @Override - public boolean startsWith(Void interop, Bytes bytes, Bytes value) { - return bytes.startsWith(value); - } - - @Override - public long hash(Void interop, Bytes value) { - return BytesBytesInterop.INSTANCE.hash(value); - } - - @Override - public long size(Void writer, Bytes value) { - return valueSize; - } - - @Override - public void write(Void writer, Bytes bytes, Bytes value) { - bytes.write(value, value.position(), value.remaining()); - } - } - - enum GetRemoteSeparateBytesInterops - implements GetValueInterops, - DelegatingMetaBytesInterop>> { - INSTANCE; - - @Override - public DelegatingMetaBytesInterop> getMetaValueInterop( - @NotNull ThreadLocalCopies copies, BytesInterop valueInterop, Bytes value) { - return DelegatingMetaBytesInterop.instance(); - } - - @Override - public BytesInterop getValueInterop(@NotNull ThreadLocalCopies copies) { - return BytesBytesInterop.INSTANCE; - } - } - @Override public final MVI getMetaValueInterop( @NotNull ThreadLocalCopies copies, VI valueInterop, V value) { @@ -1341,11 +1212,215 @@ class VanillaChronicleMap, // do nothing } + void shouldNotBeCalledFromReplicatedChronicleMap(String method) { + // do nothing + } + + enum LockType {READ_LOCK, WRITE_LOCK} + + enum GetRemoteSeparateBytesInterops + implements GetValueInterops, + DelegatingMetaBytesInterop>> { + INSTANCE; + + @Override + public DelegatingMetaBytesInterop> getMetaValueInterop( + @NotNull ThreadLocalCopies copies, BytesInterop valueInterop, Bytes value) { + return DelegatingMetaBytesInterop.instance(); + } + + @Override + public BytesInterop getValueInterop(@NotNull ThreadLocalCopies copies) { + return BytesBytesInterop.INSTANCE; + } + } + + static abstract class ReadValueToBytes implements ReadValue { + SizeMarshaller valueSizeMarshaller; + + abstract Bytes bytes(long valueSize); + + void valueSizeMarshaller(SizeMarshaller valueSizeMarshaller) { + this.valueSizeMarshaller = valueSizeMarshaller; + } + + @Override + public Bytes readValue(@NotNull ThreadLocalCopies copies, Bytes entry, Bytes usingBytes, + long valueSize) { + usingBytes = bytes(valueSize); + valueSizeMarshaller.writeSize(usingBytes, valueSize); + usingBytes.write(entry, entry.position(), valueSize); + entry.skip(valueSize); + return usingBytes; + } + } + + private static class ReadValueToOutputBytes extends ReadValueToBytes { + private TcpReplicator.TcpSocketChannelEntryWriter output; + + void reuse(SizeMarshaller valueSizeMarshaller, + TcpReplicator.TcpSocketChannelEntryWriter output) { + valueSizeMarshaller(valueSizeMarshaller); + this.output = output; + } + + @Override + Bytes bytes(long valueSize) { + return output.in(); + } + + @Override + public Bytes readValue(@NotNull ThreadLocalCopies copies, Bytes entry, Bytes usingBytes, + long valueSize) { + long totalSize = 1 + valueSizeMarshaller.sizeEncodingSize(valueSize) + valueSize; + output.ensureBufferSize(totalSize); + output.in().writeBoolean(false); + return super.readValue(copies, entry, usingBytes, valueSize); + } + + @Override + public Bytes readNull() { + output.ensureBufferSize(1); + output.in().writeBoolean(true); + return null; + } + } + + static void segmentStateNotNullImpliesCopiesNotNull( + @Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState) { + assert copies != null || segmentState == null; // segmentState != null -> copies != null + } + + private static class ReadValueToLazyBytes extends ReadValueToBytes { + DirectBytes lazyBytes; + + @Override + Bytes bytes(long valueSize) { + valueSize = valueSizeMarshaller.sizeEncodingSize(valueSize) + valueSize; + if (lazyBytes != null) { + if (lazyBytes.capacity() < valueSize) { + DirectStore store = (DirectStore) lazyBytes.store(); + store.resize(valueSize, false); + lazyBytes = store.bytes(); + } + lazyBytes.clear(); + return lazyBytes; + } + return lazyBytes = new DirectStore(valueSize).bytes(); + } + + @Override + public Bytes readNull() { + return null; + } + } + + static void expectedValueNotNullImpliesBooleanResult( + @Nullable Object expectedValue, boolean booleanResult) { + assert booleanResult || expectedValue == null; + } + + static class GetRemoteBytesValueInterops + implements GetValueInterops, + MetaBytesInterop { + private static final long serialVersionUID = 0L; + private final MultiStoreBytes valueBytes = new MultiStoreBytes(); + + private Bytes remoteEntryBytes; + private long offset; + private SizeMarshaller valueSizeMarshaller; + + private long valueSize; + + MultiStoreBytes getValueBytes(Bytes remoteEntryBytes, long offset) { + this.remoteEntryBytes = remoteEntryBytes; + this.offset = offset; + this.valueSizeMarshaller = null; + valueSize = -1L; + return valueBytes; + } + + void valueSize(long valueSize) { + this.valueSize = valueSize; + } + + void valueSizeMarshaller(SizeMarshaller valueSizeMarshaller) { + this.valueSizeMarshaller = valueSizeMarshaller; + } + + @Override + public GetRemoteBytesValueInterops getMetaValueInterop( + @NotNull ThreadLocalCopies copies, Void valueInterop, MultiStoreBytes value) { + value.setBytesOffset(remoteEntryBytes, offset); + if (valueSize == -1L) + valueSize = valueSizeMarshaller.readSize(value); + value.limit(value.position() + valueSize); + return this; + } + + @Override + public Void getValueInterop(@NotNull ThreadLocalCopies copies) { + return null; + } + + @Override + public boolean startsWith(Void interop, Bytes bytes, Bytes value) { + return bytes.startsWith(value); + } + + @Override + public long hash(Void interop, Bytes value) { + return BytesBytesInterop.INSTANCE.hash(value); + } + + @Override + public long size(Void writer, Bytes value) { + return valueSize; + } + + @Override + public void write(Void writer, Bytes bytes, Bytes value) { + bytes.write(value, value.position(), value.remaining()); + } + } + + static final boolean checkSegmentMultiMapsAndBitSetsConsistencyOnBootstrap = Boolean.getBoolean( + "chronicleMap.checkSegmentMultiMapsAndBitSetsConsistencyOnBootstrap"); + public static final class SegmentState implements StatefulCopyable, AutoCloseable { private static final Provider segmentStateProvider = Provider.of(SegmentState.class); private static final SegmentState originalSegmentState = new SegmentState(0); + final MultiStoreBytes tmpBytes = new MultiStoreBytes(); + final GetRemoteBytesValueInterops getRemoteBytesValueInterops = + new GetRemoteBytesValueInterops(); + final GetRemoteBytesValueInterops getRemoteBytesValueInterops2 = + new GetRemoteBytesValueInterops(); + final ReadValueToOutputBytes readValueToOutputBytes = new ReadValueToOutputBytes(); + final ReadValueToBytes readValueToLazyBytes = new ReadValueToLazyBytes(); + final SearchState searchState = new SearchState(); + // inner state + private final int depth; + // segment state (reusable objects/fields) + private final ReadLocked readLocked = new ReadLocked(this); + private final NativeWriteLocked nativeWriteLocked = new NativeWriteLocked(this); + private final HeapWriteLocked heapWriteLocked = new HeapWriteLocked(this); + long pos; + long valueSizePos; + private boolean used; + private SegmentState next; + + private SegmentState(int depth) { + if (depth > (1 << 10)) + throw new IllegalStateException("More than " + (1 << 10) + + " nested ChronicleMap contexts are not supported. Very probable that you " + + "simply forgot to close context somewhere (recommended to use " + + "try-with-resources statement). " + + "Otherwise this is a ChronicleMap bug, please report with this " + + "stack trace on https://github.com/OpenHFT/Chronicle-Map/issues"); + this.depth = depth; + } static @NotNull @@ -1360,40 +1435,6 @@ class VanillaChronicleMap, return segmentStateProvider.get(copies, originalSegmentState).get(); } - // inner state - private final int depth; - private boolean used; - private SegmentState next; - - // segment state (reusable objects/fields) - private final ReadLocked readLocked = new ReadLocked(this); - private final NativeWriteLocked nativeWriteLocked = new NativeWriteLocked(this); - private final HeapWriteLocked heapWriteLocked = new HeapWriteLocked(this); - - final MultiStoreBytes tmpBytes = new MultiStoreBytes(); - final GetRemoteBytesValueInterops getRemoteBytesValueInterops = - new GetRemoteBytesValueInterops(); - final GetRemoteBytesValueInterops getRemoteBytesValueInterops2 = - new GetRemoteBytesValueInterops(); - final ReadValueToOutputBytes readValueToOutputBytes = new ReadValueToOutputBytes(); - final ReadValueToBytes readValueToLazyBytes = new ReadValueToLazyBytes(); - - long pos; - long valueSizePos; - - final SearchState searchState = new SearchState(); - - private SegmentState(int depth) { - if (depth > (1 << 10)) - throw new IllegalStateException("More than " + (1 << 10) + - " nested ChronicleMap contexts are not supported. Very probable that you " + - "simply forgot to close context somewhere (recommended to use " + - "try-with-resources statement). " + - "Otherwise this is a ChronicleMap bug, please report with this " + - "stack trace on https://github.com/OpenHFT/Chronicle-Map/issues"); - this.depth = depth; - } - private SegmentState get() { if (!used) { used = true; @@ -1446,18 +1487,251 @@ class VanillaChronicleMap, } } - static void segmentStateNotNullImpliesCopiesNotNull( - @Nullable ThreadLocalCopies copies, @Nullable SegmentState segmentState) { - assert copies != null || segmentState == null; // segmentState != null -> copies != null + static abstract class MutableLockedEntry< + K, KI, MKI extends MetaBytesInterop, + V, VI, MVI extends MetaBytesInterop> implements Context { + final SegmentState segmentState; + + private VanillaChronicleMap map; + private VanillaChronicleMap.Segment segment; + private ThreadLocalCopies copies; + private MKI metaKeyInterop; + private KI keyInterop; + private long keySize; + private long segmentHash; + + private K key; + private V value; + + MutableLockedEntry(SegmentState segmentState) { + this.segmentState = segmentState; + } + + final void initSegment(VanillaChronicleMap.Segment segment) { + this.map = segment.map(); + this.segment = segment; + init(); + } + + final void initKey(ThreadLocalCopies copies, MKI metaKeyInterop, KI keyInterop, + long keySize, long segmentHash, K key) { + this.copies = copies; + this.metaKeyInterop = metaKeyInterop; + this.keyInterop = keyInterop; + this.keySize = keySize; + this.segmentHash = segmentHash; + this.key = key; + init(); + } + + void init() { + // nothing + } + + final VanillaChronicleMap map() { + return map; + } + + final VanillaChronicleMap.Segment segment() { + return segment; + } + + final ThreadLocalCopies copies() { + return copies; + } + + final MKI metaKeyInterop() { + return metaKeyInterop; + } + + final KI keyInterop() { + return keyInterop; + } + + final long keySize() { + return keySize; + } + + final long segmentHash() { + return segmentHash; + } + + public final K key() { + return key; + } + + public final V value() { + return value; + } + + final void value(V value) { + this.value = value; + } + + @Override + public void close() { + segmentState.close(); + segment().readUnlock(); + } } - static void expectedValueNotNullImpliesBooleanResult( - @Nullable Object expectedValue, boolean booleanResult) { - assert booleanResult || expectedValue == null; + private static class ReadLocked, + V, VI, MVI extends MetaBytesInterop> + extends MutableLockedEntry + implements ReadContext { + + ReadLocked(SegmentState segmentState) { + super(segmentState); + } + + /** + * @return if the value is not null + */ + public boolean present() { + return value() != null; + } } - static final boolean checkSegmentMultiMapsAndBitSetsConsistencyOnBootstrap = Boolean.getBoolean( - "chronicleMap.checkSegmentMultiMapsAndBitSetsConsistencyOnBootstrap"); + static abstract class WriteLocked, + V, VI, MVI extends MetaBytesInterop> + extends MutableLockedEntry + implements WriteContext { + + boolean removed; + private boolean created; + + WriteLocked(SegmentState segmentState) { + super(segmentState); + } + + @Override + void init() { + super.init(); + created = false; + removed = false; + } + + /** + * @return if the entry was created + */ + public boolean created() { + return created; + } + + public void created(boolean created) { + this.created = created; + } + } + + private static class NativeWriteLocked, + V, VI, MVI extends MetaBytesInterop> + extends WriteLocked { + + NativeWriteLocked(SegmentState segmentState) { + super(segmentState); + } + + @Override + public void close() { + if (!removed) { + // TODO optimize -- keep replication bytes offset in SegmentState to jump directly + long pos = segmentState.pos; + VanillaChronicleMap.Segment segment = segment(); + VanillaChronicleMap map = map(); + long offset = segment.offsetFromPos(pos); + MultiStoreBytes entry = segment.reuse(segmentState.tmpBytes, offset); + long keySize = map.keySizeMarshaller.readSize(entry); + entry.skip(keySize); + segment.manageReplicationBytes(entry, true, false); + map.onPut(segment, pos); + } + super.close(); + } + + @Override + public void dontPutOnClose() { + throw new IllegalStateException( + "dontPutOnClose() method is not supported for native value classes"); + } + + @Override + public void removeEntry() { + removed = true; + VanillaChronicleMap map = map(); + segment().removeWithoutLock(copies(), segmentState, + metaKeyInterop(), keyInterop(), key(), keySize(), map.keyIdentity(), + map, null, map.valueIdentity(), segmentHash(), map, true); + } + } + + private static class HeapWriteLocked, + V, VI, MVI extends MetaBytesInterop> + extends WriteLocked { + + private boolean putOnClose; + + HeapWriteLocked(SegmentState segmentState) { + super(segmentState); + } + + @Override + void init() { + super.init(); + putOnClose = true; + } + + @Override + public void close() { + VanillaChronicleMap.Segment segment = segment(); + if (putOnClose) { + assert !removed; + // TODO optimize -- keep keySize, valueSizePos, entryEndAddr, etc. inside + // segmentState + long pos = segmentState.pos; + VanillaChronicleMap map = map(); + ThreadLocalCopies copies = copies(); + long offset = segment.offsetFromPos(pos); + MultiStoreBytes entry = segment.reuse(segmentState.tmpBytes, offset); + long keySize = map.keySizeMarshaller.readSize(entry); + entry.skip(keySize); + segment.manageReplicationBytes(entry, true, false); + long valueSizePos = entry.position(); + long valueSize = map.valueSizeMarshaller.readSize(entry); + long sizeOfEverythingBeforeValue = entry.position(); + map.alignment.alignPositionAddr(entry); + long entryEndAddr = entry.positionAddr() + valueSize; + VI valueInterop = map.valueInteropProvider.get(copies, map.originalValueInterop); + V value = value(); + MVI metaValueInterop = map.metaValueInteropProvider + .get(copies, map.originalMetaValueInterop, valueInterop, value); + long newValueSize = metaValueInterop.size(valueInterop, value); + segment.putValue(pos, offset, entry, valueSizePos, entryEndAddr, removed, + segmentState, + metaValueInterop, valueInterop, value, newValueSize, segment.hashLookup(), + sizeOfEverythingBeforeValue); + map.onPut(segment, segmentState.pos); + } + super.close(); + } + + + @Override + public void dontPutOnClose() { + if (removed) + throw new IllegalStateException("Shouldn't call this method after removeEntry()"); + putOnClose = false; + } + + @Override + public void removeEntry() { + putOnClose = false; + removed = true; + VanillaChronicleMap map = map(); + segment().removeWithoutLock(copies(), segmentState, + metaKeyInterop(), keyInterop(), key(), keySize(), map.keyIdentity(), + map, null, map.valueIdentity(), segmentHash(), map, true); + } + } // these methods should be package local, not public or private. class Segment implements SharedSegment { @@ -1476,13 +1750,10 @@ class VanillaChronicleMap, final long entriesOffset; private final int index; private final SingleThreadedDirectBitSet freeList; + long startWriteLock = 0; private MultiMap hashLookup; private long nextPosToSearchFrom = 0L; - private VanillaChronicleMap map() { - return VanillaChronicleMap.this; - } - /** * @param index the index of this segment held by the map */ @@ -1508,6 +1779,10 @@ class VanillaChronicleMap, checkMultiMapsAndBitSetsConsistency(); } + private VanillaChronicleMap map() { + return VanillaChronicleMap.this; + } + final MultiMap hashLookup() { return hashLookup; } @@ -1549,14 +1824,14 @@ class VanillaChronicleMap, } } - public final int getIndex() { - return index; - } - /* Methods with private access modifier considered private to Segment * class, although Java allows to access them from outer class anyway. */ + public final int getIndex() { + return index; + } + /** * increments the size by one */ @@ -1617,8 +1892,6 @@ class VanillaChronicleMap, } } - long startWriteLock = 0; - final WriteLocked writeLock() { return writeLock(null, false); } @@ -2535,20 +2808,15 @@ class VanillaChronicleMap, } } - void shouldNotBeCalledFromReplicatedChronicleMap(String method) { - // do nothing - } - class EntryIterator implements Iterator> { Entry returnedEntry; + // todo get rid of this + MultiStoreBytes entry = new MultiStoreBytes(); private int returnedSeg = -1; private long returnedPos = -1L; private int nextSeg; private long nextPos; - // todo get rid of this - MultiStoreBytes entry = new MultiStoreBytes(); - EntryIterator() { advance(nextSeg = segments.length - 1, nextPos = -1L); } @@ -2736,250 +3004,4 @@ class VanillaChronicleMap, } } - static abstract class MutableLockedEntry< - K, KI, MKI extends MetaBytesInterop, - V, VI, MVI extends MetaBytesInterop> implements Context { - final SegmentState segmentState; - - private VanillaChronicleMap map; - private VanillaChronicleMap.Segment segment; - private ThreadLocalCopies copies; - private MKI metaKeyInterop; - private KI keyInterop; - private long keySize; - private long segmentHash; - - private K key; - private V value; - - MutableLockedEntry(SegmentState segmentState) { - this.segmentState = segmentState; - } - - final void initSegment(VanillaChronicleMap.Segment segment) { - this.map = segment.map(); - this.segment = segment; - init(); - } - - final void initKey(ThreadLocalCopies copies, MKI metaKeyInterop, KI keyInterop, - long keySize, long segmentHash, K key) { - this.copies = copies; - this.metaKeyInterop = metaKeyInterop; - this.keyInterop = keyInterop; - this.keySize = keySize; - this.segmentHash = segmentHash; - this.key = key; - init(); - } - - void init() { - // nothing - } - - final VanillaChronicleMap map() { - return map; - } - - final VanillaChronicleMap.Segment segment() { - return segment; - } - - final ThreadLocalCopies copies() { - return copies; - } - - final MKI metaKeyInterop() { - return metaKeyInterop; - } - - final KI keyInterop() { - return keyInterop; - } - - final long keySize() { - return keySize; - } - - final long segmentHash() { - return segmentHash; - } - - public final K key() { - return key; - } - - public final V value() { - return value; - } - - final void value(V value) { - this.value = value; - } - - @Override - public void close() { - segmentState.close(); - segment().readUnlock(); - } - } - - private static class ReadLocked, - V, VI, MVI extends MetaBytesInterop> - extends MutableLockedEntry - implements ReadContext { - - ReadLocked(SegmentState segmentState) { - super(segmentState); - } - - /** - * @return if the value is not null - */ - public boolean present() { - return value() != null; - } - } - - static abstract class WriteLocked, - V, VI, MVI extends MetaBytesInterop> - extends MutableLockedEntry - implements WriteContext { - - private boolean created; - boolean removed; - - WriteLocked(SegmentState segmentState) { - super(segmentState); - } - - @Override - void init() { - super.init(); - created = false; - removed = false; - } - - /** - * @return if the entry was created - */ - public boolean created() { - return created; - } - - public void created(boolean created) { - this.created = created; - } - } - - private static class NativeWriteLocked, - V, VI, MVI extends MetaBytesInterop> - extends WriteLocked { - - NativeWriteLocked(SegmentState segmentState) { - super(segmentState); - } - - @Override - public void close() { - if (!removed) { - // TODO optimize -- keep replication bytes offset in SegmentState to jump directly - long pos = segmentState.pos; - VanillaChronicleMap.Segment segment = segment(); - VanillaChronicleMap map = map(); - long offset = segment.offsetFromPos(pos); - MultiStoreBytes entry = segment.reuse(segmentState.tmpBytes, offset); - long keySize = map.keySizeMarshaller.readSize(entry); - entry.skip(keySize); - segment.manageReplicationBytes(entry, true, false); - map.onPut(segment, pos); - } - super.close(); - } - - @Override - public void dontPutOnClose() { - throw new IllegalStateException( - "dontPutOnClose() method is not supported for native value classes"); - } - - @Override - public void removeEntry() { - removed = true; - VanillaChronicleMap map = map(); - segment().removeWithoutLock(copies(), segmentState, - metaKeyInterop(), keyInterop(), key(), keySize(), map.keyIdentity(), - map, null, map.valueIdentity(), segmentHash(), map, true); - } - } - - private static class HeapWriteLocked, - V, VI, MVI extends MetaBytesInterop> - extends WriteLocked { - - private boolean putOnClose; - - HeapWriteLocked(SegmentState segmentState) { - super(segmentState); - } - - @Override - void init() { - super.init(); - putOnClose = true; - } - - @Override - public void close() { - VanillaChronicleMap.Segment segment = segment(); - if (putOnClose) { - assert !removed; - // TODO optimize -- keep keySize, valueSizePos, entryEndAddr, etc. inside - // segmentState - long pos = segmentState.pos; - VanillaChronicleMap map = map(); - ThreadLocalCopies copies = copies(); - long offset = segment.offsetFromPos(pos); - MultiStoreBytes entry = segment.reuse(segmentState.tmpBytes, offset); - long keySize = map.keySizeMarshaller.readSize(entry); - entry.skip(keySize); - segment.manageReplicationBytes(entry, true, false); - long valueSizePos = entry.position(); - long valueSize = map.valueSizeMarshaller.readSize(entry); - long sizeOfEverythingBeforeValue = entry.position(); - map.alignment.alignPositionAddr(entry); - long entryEndAddr = entry.positionAddr() + valueSize; - VI valueInterop = map.valueInteropProvider.get(copies, map.originalValueInterop); - V value = value(); - MVI metaValueInterop = map.metaValueInteropProvider - .get(copies, map.originalMetaValueInterop, valueInterop, value); - long newValueSize = metaValueInterop.size(valueInterop, value); - segment.putValue(pos, offset, entry, valueSizePos, entryEndAddr, removed, - segmentState, - metaValueInterop, valueInterop, value, newValueSize, segment.hashLookup(), - sizeOfEverythingBeforeValue); - map.onPut(segment, segmentState.pos); - } - super.close(); - } - - - @Override - public void dontPutOnClose() { - if (removed) - throw new IllegalStateException("Shouldn't call this method after removeEntry()"); - putOnClose = false; - } - - @Override - public void removeEntry() { - putOnClose = false; - removed = true; - VanillaChronicleMap map = map(); - segment().removeWithoutLock(copies(), segmentState, - metaKeyInterop(), keyInterop(), key(), keySize(), map.keyIdentity(), - map, null, map.valueIdentity(), segmentHash(), map, true); - } - } - }