From 5ace988277bbf2ab6d4f6639bc5aa7fc869cf987 Mon Sep 17 00:00:00 2001 From: Per Minborg Date: Wed, 2 Sep 2020 10:42:29 +0200 Subject: [PATCH] Cleanup classes --- .../openhft/chronicle/hash/ChronicleHash.java | 6 +- .../hash/impl/VanillaChronicleHash.java | 317 +++++++++--------- 2 files changed, 162 insertions(+), 161 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java b/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java index ecf893395..36a3a801d 100644 --- a/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java +++ b/src/main/java/net/openhft/chronicle/hash/ChronicleHash.java @@ -23,6 +23,7 @@ import net.openhft.chronicle.map.MapMethods; import net.openhft.chronicle.map.MapQueryContext; import net.openhft.chronicle.set.ChronicleSet; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.File; import java.lang.reflect.Type; @@ -43,6 +44,7 @@ public interface ChronicleHash, SC extends HashSegment * @return the file this {@link ChronicleMap} or {@link ChronicleSet} is mapped to, or {@code null} if it is not mapped to any file * @see ChronicleHashBuilder#createPersistedTo(File) */ + @Nullable File file(); /** @@ -50,6 +52,7 @@ public interface ChronicleHash, SC extends HashSegment * * @return the name of this this {@link ChronicleMap} or {@link ChronicleSet} */ + @Nullable String name(); /** @@ -67,6 +70,7 @@ public interface ChronicleHash, SC extends HashSegment * * @return a {@code String}, useful for identifying this {@code ChronicleHash} in debugging, logging, and error reporting */ + @NotNull String toIdentityString(); /** @@ -207,4 +211,4 @@ public interface ChronicleHash, SC extends HashSegment * @return {@code true} is {@link #close()} is not yet called */ boolean isOpen(); -} +} \ No newline at end of file diff --git a/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java b/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java index ebbfd9a63..ff5a67f15 100755 --- a/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java @@ -22,6 +22,7 @@ import net.openhft.chronicle.bytes.MappedBytesStoreFactory; import net.openhft.chronicle.bytes.NativeBytesStore; import net.openhft.chronicle.core.Maths; import net.openhft.chronicle.core.OS; +import net.openhft.chronicle.core.io.ReferenceOwner; import net.openhft.chronicle.hash.*; import net.openhft.chronicle.hash.impl.util.BuildVersion; import net.openhft.chronicle.hash.impl.util.Cleaner; @@ -40,6 +41,7 @@ import net.openhft.chronicle.wire.Marshallable; import net.openhft.chronicle.wire.WireIn; import net.openhft.chronicle.wire.WireOut; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.io.Closeable; import java.io.File; @@ -65,8 +67,8 @@ import static net.openhft.chronicle.map.ChronicleHashCorruptionImpl.format; import static net.openhft.chronicle.map.ChronicleHashCorruptionImpl.report; public abstract class VanillaChronicleHash, SC extends HashSegmentContext, - ECQ extends ExternalHashQueryContext> + C extends HashEntry, SC extends HashSegmentContext, + ECQ extends ExternalHashQueryContext> implements ChronicleHash, Marshallable { public static final long TIER_COUNTERS_AREA_SIZE = 64; @@ -79,13 +81,11 @@ public abstract class VanillaChronicleHash globalMutableStateLockTryAcquireOperation = + static final TryAcquireOperation GLOBAL_MUTABLE_STATE_LOCK_TRY_ACQUIRE_OPERATION = TryAcquireOperations.lock(); - static final - AcquisitionStrategy - globalMutableStateLockAcquisitionStrategy = + static final AcquisitionStrategy GLOBAL_MUTABLE_STATE_LOCK_ACQUISITION_STRATEGY = AcquisitionStrategies.spinLoopOrFail(2, TimeUnit.SECONDS); private static final long GLOBAL_MUTABLE_STATE_LOCK_OFFSET = 0L; private static final long GLOBAL_MUTABLE_STATE_VALUE_OFFSET = 8L; @@ -126,8 +126,8 @@ public abstract class VanillaChronicleHash builder) { + public VanillaChronicleHash(@NotNull final ChronicleMapBuilder builder) { // Version dataFileVersion = BuildVersion.version(); - createdOrInMemory = true; @SuppressWarnings({"deprecation", "unchecked"}) - ChronicleHashBuilderPrivateAPI privateAPI = - (ChronicleHashBuilderPrivateAPI) builder.privateAPI(); + ChronicleHashBuilderPrivateAPI privateAPI = (ChronicleHashBuilderPrivateAPI) builder.privateAPI(); // Data model SerializationBuilder keyBuilder = privateAPI.keyBuilder(); @@ -182,8 +180,7 @@ public abstract class VanillaChronicleHash 4) { throw new IllegalStateException("aligned64BitMemoryOperationsAtomic() == false, " + "but hash lookup slot is " + tierHashLookupSlotSize); @@ -193,14 +190,12 @@ public abstract class VanillaChronicleHash "dataFileVersion").text(); // Previously this assignment was done in default field initializer, but with Wire @@ -292,8 +287,8 @@ public abstract class VanillaChronicleHash "dataFileVersion").text(dataFileVersion); wireOut.write(() -> "keyClass").typeLiteral(keyClass); @@ -343,11 +338,11 @@ public abstract class VanillaChronicleHash GIGABYTES.toBytes(1)) { segmentHeadersOffset = computeSegmentHeadersOffset(); } - long sizeInBytesWithoutTiers = computeSizeInBytesWithoutTiers(segmentHeadersOffset); + final long sizeInBytesWithoutTiers = computeSizeInBytesWithoutTiers(segmentHeadersOffset); long dataStoreSize = globalMutableState().getDataStoreSize(); int allocatedExtraTierBulks = globalMutableState().getAllocatedExtraTierBulks(); if (dataStoreSize < sizeInBytesWithoutTiers || @@ -533,12 +529,11 @@ public abstract class VanillaChronicleHash format("global mutable state lock of map at {} is not clear: {}", @@ -548,9 +543,9 @@ public abstract class VanillaChronicleHash format("allocated extra tier bulks counter corrupted, or the map file {} " + @@ -562,9 +557,9 @@ public abstract class VanillaChronicleHash format("segment headers offset of map at {} corrupted. stored: {}, should be: {}", @@ -574,9 +569,9 @@ public abstract class VanillaChronicleHash format("data store size of map at {} corrupted. stored: {}, should be: {}", @@ -613,20 +608,19 @@ public abstract class VanillaChronicleHash bytesStore, final long tierOffset) { // Zero out hash lookup, tier data and free list bit set. Leave entry space dirty. bytesStore.zeroOut(tierOffset, tierOffset + tierSize - tierEntrySpaceOuterSize); } public void onHeaderCreated() { throwExceptionIfClosed(); - - } + } /** * @return the version of Chronicle Map that was used to create the current data file @@ -634,7 +628,7 @@ public abstract class VanillaChronicleHash keyClass = keyClass(); + public final void checkKey(final Object key) { + final Class keyClass = keyClass(); if (!keyClass.isInstance(key)) { if (key == null) throw new NullPointerException("null key not supported"); @@ -714,25 +709,25 @@ public abstract class VanillaChronicleHash 0; + return globalMutableState.getAllocatedExtraTierBulks() > 0; } @Override public long offHeapMemoryUsed() { throwExceptionIfClosed(); - return resources.totalMemory(); + return resources.totalMemory(); } public long allocateTier() { throwExceptionIfClosed(); - globalMutableStateLock(); + globalMutableStateLock(); try { long tiersInUse = globalMutableState.getExtraTiersInUse(); if (tiersInUse >= maxExtraTiers) { @@ -829,11 +825,11 @@ public abstract class VanillaChronicleHash allocatedTierBytes = tierBytesStore(firstFreeTierIndex); + final long allocatedTierOffset = tierBytesOffset(firstFreeTierIndex); + final long tierBaseAddr = allocatedTierBytes.addressForRead(0) + allocatedTierOffset; + final long tierCountersAreaAddr = tierBaseAddr + tierHashLookupOuterSize; + final long nextFreeTierIndex = TierCountersArea.nextTierIndex(tierCountersAreaAddr); globalMutableState.setFirstFreeTierIndex(nextFreeTierIndex); return firstFreeTierIndex; } finally { @@ -842,26 +838,26 @@ public abstract class VanillaChronicleHash tierBytesStore = tierBytesStore(firstTierIndex); + final long firstTierOffset = tierBytesOffset(firstTierIndex); if (tierBulkInnerOffsetToTiers > 0) { // These bytes are bit sets in Replicated version tierBytesStore.zeroOut(firstTierOffset - tierBulkInnerOffsetToTiers, firstTierOffset); } - long lastTierIndex = firstTierIndex + tiersInBulk - 1; + final long lastTierIndex = firstTierIndex + tiersInBulk - 1; linkAndZeroOutFreeTiers(firstTierIndex, lastTierIndex); // see HCOLL-397 if (persisted()) { - long address = tierBytesStore.addressForRead(firstTierOffset - tierBulkInnerOffsetToTiers); - long endAddress = tierBytesStore.addressForRead(tierBytesOffset(lastTierIndex)) + tierSize; - long length = endAddress - address; + final long address = tierBytesStore.addressForRead(firstTierOffset - tierBulkInnerOffsetToTiers); + final long endAddress = tierBytesStore.addressForRead(tierBytesOffset(lastTierIndex)) + tierSize; + final long length = endAddress - address; msync(address, length); } @@ -874,7 +870,7 @@ public abstract class VanillaChronicleHash tierBytesStore = tierBytesStore(tierIndex); zeroOutNewlyMappedTier(tierBytesStore, tierOffset); if (tierIndex < lastTierIndex) { - long tierCountersAreaOffset = tierOffset + tierHashLookupOuterSize; + final long tierCountersAreaOffset = tierOffset + tierHashLookupOuterSize; TierCountersArea.nextTierIndex(tierBytesStore.addressForRead(0) + tierCountersAreaOffset, tierIndex + 1); } } } - public long extraTierIndexToTierIndex(long extraTierIndex) { + public long extraTierIndexToTierIndex(final long extraTierIndex) { throwExceptionIfClosed(); - return actualSegments + extraTierIndex + 1; + return actualSegments + extraTierIndex + 1; } - public long tierIndexToBaseAddr(long tierIndex) { + public long tierIndexToBaseAddr(final long tierIndex) { throwExceptionIfClosed(); // tiers are 1-counted, to allow tierIndex = 0 to be un-initialized in off-heap memory, // convert into 0-based form - long tierIndexMinusOne = tierIndex - 1; + final long tierIndexMinusOne = tierIndex - 1; if (tierIndexMinusOne < actualSegments) return segmentBaseAddr((int) tierIndexMinusOne); return extraTierIndexToBaseAddr(tierIndexMinusOne); @@ -928,7 +924,7 @@ public abstract class VanillaChronicleHash> log2TiersInBulk); + final long extraTierIndex = tierIndexMinusOne - actualSegments; + final int bulkIndex = (int) (extraTierIndex >> log2TiersInBulk); if (bulkIndex >= tierBulkOffsets.size()) mapTierBulks(bulkIndex); return tierBulkOffsets.get(bulkIndex).offset + tierBulkInnerOffsetToTiers + (extraTierIndex & (tiersInBulk - 1)) * tierSize; } - private TierBulkData tierBulkData(long tierIndexMinusOne) { - long extraTierIndex = tierIndexMinusOne - actualSegments; - int bulkIndex = (int) (extraTierIndex >> log2TiersInBulk); + private TierBulkData tierBulkData(final long tierIndexMinusOne) { + final long extraTierIndex = tierIndexMinusOne - actualSegments; + final int bulkIndex = (int) (extraTierIndex >> log2TiersInBulk); if (bulkIndex >= tierBulkOffsets.size()) mapTierBulks(bulkIndex); return tierBulkOffsets.get(bulkIndex); } - private long extraTierIndexToBaseAddr(long tierIndexMinusOne) { - long extraTierIndex = tierIndexMinusOne - actualSegments; - int bulkIndex = (int) (extraTierIndex >> log2TiersInBulk); + private long extraTierIndexToBaseAddr(final long tierIndexMinusOne) { + final long extraTierIndex = tierIndexMinusOne - actualSegments; + final int bulkIndex = (int) (extraTierIndex >> log2TiersInBulk); if (bulkIndex >= tierBulkOffsets.size()) mapTierBulks(bulkIndex); - TierBulkData tierBulkData = tierBulkOffsets.get(bulkIndex); - long tierIndexOffsetWithinBulk = extraTierIndex & (tiersInBulk - 1); + final TierBulkData tierBulkData = tierBulkOffsets.get(bulkIndex); + final long tierIndexOffsetWithinBulk = extraTierIndex & (tiersInBulk - 1); return tierAddr(tierBulkData, tierIndexOffsetWithinBulk); } - protected long tierAddr(TierBulkData tierBulkData, long tierIndexOffsetWithinBulk) { + protected long tierAddr(@NotNull final TierBulkData tierBulkData, final long tierIndexOffsetWithinBulk) { return tierBulkData.bytesStore.addressForRead(0) + tierBulkData.offset + tierBulkInnerOffsetToTiers + tierIndexOffsetWithinBulk * tierSize; } - private void mapTierBulks(int upToBulkIndex) { + private void mapTierBulks(final int upToBulkIndex) { if (persisted()) { try { mapTierBulksMapped(upToBulkIndex); @@ -984,12 +980,12 @@ public abstract class VanillaChronicleHash(address, capacity, null, false); } - private void appendBulkData(int firstBulkToMapIndex, int upToBulkIndex, BytesStore extraStore, + private void appendBulkData(final int firstBulkToMapIndex, + final int upToBulkIndex, + final BytesStore extraStore, long offsetWithinMapping) { - TierBulkData firstMappedBulkData = new TierBulkData(extraStore, offsetWithinMapping); + final TierBulkData firstMappedBulkData = new TierBulkData(extraStore, offsetWithinMapping); tierBulkOffsets.add(firstMappedBulkData); for (int bulkIndex = firstBulkToMapIndex + 1; bulkIndex <= upToBulkIndex; bulkIndex++) { - tierBulkOffsets.add(new TierBulkData(firstMappedBulkData, - offsetWithinMapping += tierBulkSizeInBytes)); + tierBulkOffsets.add(new TierBulkData(firstMappedBulkData, offsetWithinMapping += tierBulkSizeInBytes)); } } - protected void addContext(ContextHolder contextHolder) { + protected void addContext(final ContextHolder contextHolder) { resources.addContext(contextHolder); } - public void addCloseable(Closeable closeable) { + public void addCloseable(final Closeable closeable) { throwExceptionIfClosed(); - resources.addCloseable(closeable); + resources.addCloseable(closeable); } /** @@ -1083,19 +1080,19 @@ public abstract class VanillaChronicleHash> allContexts() { throwExceptionIfClosed(); - return Collections.unmodifiableList(resources.contexts()); + return Collections.unmodifiableList(resources.contexts()); } - public static class TierBulkData { + public static final class TierBulkData { public final BytesStore bytesStore; public final long offset; - public TierBulkData(BytesStore bytesStore, long offset) { + public TierBulkData(final BytesStore bytesStore, final long offset) { this.bytesStore = bytesStore; this.offset = offset; } - public TierBulkData(TierBulkData data, long offset) { + public TierBulkData(final TierBulkData data, final long offset) { this.bytesStore = data.bytesStore; this.offset = offset; } @@ -1105,11 +1102,11 @@ public abstract class VanillaChronicleHash