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 1d613653b..5ec3561c0 100755 --- a/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/VanillaChronicleHash.java @@ -38,7 +38,6 @@ import net.openhft.chronicle.map.ChronicleMapBuilder; import net.openhft.chronicle.values.Values; import net.openhft.chronicle.wire.Marshallable; import net.openhft.chronicle.wire.WireIn; -import net.openhft.chronicle.wire.WireInternal; import net.openhft.chronicle.wire.WireOut; import org.jetbrains.annotations.NotNull; @@ -233,11 +232,13 @@ public abstract class VanillaChronicleHash "dataFileVersion").text(dataFileVersion); wireOut.write(() -> "keyClass").typeLiteral(keyClass); @@ -338,6 +340,7 @@ public abstract class VanillaChronicleHash 0; } @Override public long offHeapMemoryUsed() { + throwExceptionIfClosed(); return resources.totalMemory(); } public long allocateTier() { + throwExceptionIfClosed(); globalMutableStateLock(); try { long tiersInUse = globalMutableState.getExtraTiersInUse(); @@ -837,6 +855,7 @@ public abstract class VanillaChronicleHash> allContexts() { + throwExceptionIfClosed(); return Collections.unmodifiableList(resources.contexts()); } @@ -1056,6 +1082,7 @@ public abstract class VanillaChronicleHash> } public long tierEntriesForIteration() { + throwExceptionIfClosed(); return s.tierEntries(); } @@ -67,6 +68,7 @@ public abstract class HashSegmentIteration> public abstract boolean hashLookupEntryInit(); public void initHashLookupEntry(long entry) { + throwExceptionIfClosed(); hashLookupEntry = entry; } @@ -74,6 +76,7 @@ public abstract class HashSegmentIteration> @Override public boolean forEachSegmentEntryWhile(Predicate predicate) { + throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerUpdateLock.lock(); return innerForEachSegmentEntryWhile(predicate); @@ -174,10 +177,12 @@ public abstract class HashSegmentIteration> } public void hookAfterEachIteration() { + throwExceptionIfClosed(); } @Override public void forEachSegmentEntry(Consumer action) { + throwExceptionIfClosed(); forEachSegmentEntryWhile(e -> { action.accept(e); return true; @@ -185,6 +190,7 @@ public abstract class HashSegmentIteration> } public void checkEntryNotRemovedOnThisIteration() { + throwExceptionIfClosed(); if (entryRemovedOnThisIterationInit()) { throw new IllegalStateException( hh.h().toIdentityString() + ": Entry was already removed on this iteration"); @@ -193,6 +199,7 @@ public abstract class HashSegmentIteration> @Override public void doRemove() { + throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerWriteLock.lock(); try { @@ -204,6 +211,7 @@ public abstract class HashSegmentIteration> } public void iterationRemove() { + throwExceptionIfClosed(); // this condition mean -- some other entry taken place of the removed one if (hh.h().hashLookup.remove(s.tierBaseAddr, hlp.hashLookupPos) != hlp.hashLookupPos) { // if so, should make step back, to compensate step forward on the next iteration, diff --git a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/SegmentsRecovery.java b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/SegmentsRecovery.java index b8ce94f24..920ea52a7 100644 --- a/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/SegmentsRecovery.java +++ b/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/SegmentsRecovery.java @@ -44,6 +44,7 @@ public abstract class SegmentsRecovery implements IterationContext { public void recoverSegments( ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption) { + throwExceptionIfClosed(); VanillaChronicleHash h = hh.h(); for (int segmentIndex = 0; segmentIndex < h.actualSegments; segmentIndex++) { s.initSegmentIndex(segmentIndex); diff --git a/src/main/java/net/openhft/chronicle/map/OldDeletedEntriesCleanupThread.java b/src/main/java/net/openhft/chronicle/map/OldDeletedEntriesCleanupThread.java index b6dbbcebf..6c2fbb4b9 100644 --- a/src/main/java/net/openhft/chronicle/map/OldDeletedEntriesCleanupThread.java +++ b/src/main/java/net/openhft/chronicle/map/OldDeletedEntriesCleanupThread.java @@ -114,6 +114,7 @@ class OldDeletedEntriesCleanupThread extends Thread @Override public void run() { + throwExceptionIfClosed(); if (System.currentTimeMillis() - startTime < 1_000) return; @@ -175,6 +176,7 @@ class OldDeletedEntriesCleanupThread extends Thread @Override public boolean test(ReplicableEntry e) { + throwExceptionIfClosed(); if (shutdown) return false; if (e instanceof MapAbsentEntry) { diff --git a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java index a551cdc16..16fce38eb 100644 --- a/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java +++ b/src/main/java/net/openhft/chronicle/map/ReplicatedChronicleMap.java @@ -154,6 +154,7 @@ public class ReplicatedChronicleMap extends VanillaChronicleMap "tierModIterBitSetSizeInBits").int64(tierModIterBitSetSizeInBits); @@ -171,11 +172,13 @@ public class ReplicatedChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap raise change for all changeCount.incrementAndGet(); raiseChangeForAllExcept(tierIndex, pos, (byte) -1); } public void raiseChangeFor(long tierIndex, long pos, byte remoteIdentifier) { + throwExceptionIfClosed(); acquireModificationIterator(remoteIdentifier).raiseChange0(tierIndex, pos); } public void raiseChangeForAllExcept(long tierIndex, long pos, byte remoteIdentifier) { + throwExceptionIfClosed(); updateModificationIteratorsArray(); if (tierIndex <= actualSegments) { long segmentIndex = tierIndex - 1; @@ -373,6 +389,7 @@ public class ReplicatedChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap keys) { + throwExceptionIfClosed(); if (payload != null) writePayload(payload, destination); if (entry != null) @@ -571,6 +593,7 @@ public class ReplicatedChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap extends VanillaChronicleMap c = iterationContext()) { // iterate over all the segments and mark bit in the modification iterator // that correspond to entries with an older timestamp @@ -934,6 +961,7 @@ public class ReplicatedChronicleMap extends VanillaChronicleMap @Override public void writeMarshallable(@NotNull WireOut wireOut) { + ; super.writeMarshallable(wireOut); wireOut.write(() -> "valueClass").typeLiteral(valueClass); @@ -161,6 +162,7 @@ public class VanillaChronicleMap @Override public void initTransients() { + throwExceptionIfClosed(); super.initTransients(); initOwnTransients(); } @@ -168,6 +170,7 @@ public class VanillaChronicleMap public void recover( ChronicleHashResources resources, ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption) throws IOException { + throwExceptionIfClosed(); basicRecover(resources, corruptionListener, corruption); try (IterationContext iterationContext = iterationContext()) { iterationContext.recoverSegments(corruptionListener, corruption); @@ -212,21 +215,25 @@ public class VanillaChronicleMap @Override public Class keyClass() { + throwExceptionIfClosed(); return (Class) keyClass; } @Override public Type keyType() { + throwExceptionIfClosed(); return keyClass; } @Override public Class valueClass() { + throwExceptionIfClosed(); return (Class) valueClass; } @Override public Type valueType() { + throwExceptionIfClosed(); return valueClass; } @@ -240,11 +247,13 @@ public class VanillaChronicleMap @Override public int remainingAutoResizes() { + throwExceptionIfClosed(); return (int) (maxTiers() - tiersUsed()); } @Override public short percentageFreeSpace() { + throwExceptionIfClosed(); double totalUsed = 0; double totalSize = 0; @@ -310,11 +319,13 @@ public class VanillaChronicleMap @Override public int hashCode() { + throwExceptionIfClosed(); return mapHashCode(); } @Override public boolean equals(Object obj) { + throwExceptionIfClosed(); return mapEquals(obj); } @@ -330,6 +341,7 @@ public class VanillaChronicleMap @Override public String toIdentityString() { + throwExceptionIfClosed(); if (identityString == null) identityString = makeIdentityString(); return identityString; @@ -347,6 +359,7 @@ public class VanillaChronicleMap @Override public void clear() { + throwExceptionIfClosed(); forEachEntry(c -> c.context().remove(c)); } @@ -357,6 +370,7 @@ public class VanillaChronicleMap } public void alignReadPosition(Bytes entry) { + throwExceptionIfClosed(); long positionAddr = entry.addressForRead(entry.readPosition()); long skip = alignAddr(positionAddr, alignment) - positionAddr; if (skip > 0) @@ -520,6 +534,7 @@ public class VanillaChronicleMap @Override public V get(Object key) { + throwExceptionIfClosed(); return defaultEntryOperationsAndMethods ? optimizedGet(key, null) : defaultGet(key); } @@ -693,6 +708,7 @@ public class VanillaChronicleMap @Override public V getUsing(K key, V usingValue) { + throwExceptionIfClosed(); return defaultEntryOperationsAndMethods ? optimizedGet(key, usingValue) : defaultGetUsing(key, usingValue); } @@ -707,6 +723,7 @@ public class VanillaChronicleMap @Override public V acquireUsing(K key, V usingValue) { + throwExceptionIfClosed(); try (QueryContextInterface q = queryContext(key)) { return acquireUsingBody(q, usingValue); } @@ -720,6 +737,7 @@ public class VanillaChronicleMap @Override public V putIfAbsent(K key, V value) { + throwExceptionIfClosed(); checkValue(value); try (QueryContextInterface q = queryContext(key)) { methods.putIfAbsent(q, q.inputValueDataAccess().getData(value), q.defaultReturnValue()); @@ -729,6 +747,7 @@ public class VanillaChronicleMap @Override public boolean remove(Object key, Object value) { + throwExceptionIfClosed(); if (value == null) return false; // ConcurrentHashMap compatibility V v = checkValue(value); @@ -739,6 +758,7 @@ public class VanillaChronicleMap @Override public boolean replace(K key, V oldValue, V newValue) { + throwExceptionIfClosed(); checkValue(oldValue); checkValue(newValue); try (QueryContextInterface q = queryContext(key)) { @@ -749,6 +769,7 @@ public class VanillaChronicleMap @Override public V replace(K key, V value) { + throwExceptionIfClosed(); checkValue(value); try (QueryContextInterface q = queryContext(key)) { methods.replace(q, q.inputValueDataAccess().getData(value), q.defaultReturnValue()); @@ -758,6 +779,7 @@ public class VanillaChronicleMap @Override public boolean containsKey(Object key) { + throwExceptionIfClosed(); try (QueryContextInterface q = queryContext(key)) { return methods.containsKey(q); } @@ -765,6 +787,7 @@ public class VanillaChronicleMap @Override public V put(K key, V value) { + throwExceptionIfClosed(); checkValue(value); try (QueryContextInterface q = queryContext(key)) { Data valueData = q.inputValueDataAccess().getData(value); @@ -777,6 +800,7 @@ public class VanillaChronicleMap @Override public V remove(Object key) { + throwExceptionIfClosed(); try (QueryContextInterface q = queryContext(key)) { InstanceReturnValue returnValue = removeReturnsNull ? NullReturnValue.get() : q.defaultReturnValue(); @@ -788,6 +812,7 @@ public class VanillaChronicleMap @Override public V merge(K key, V value, BiFunction remappingFunction) { + throwExceptionIfClosed(); try (QueryContextInterface q = queryContext(key)) { methods.merge(q, q.inputValueDataAccess().getData(value), remappingFunction, q.defaultReturnValue()); @@ -797,6 +822,7 @@ public class VanillaChronicleMap @Override public V compute(K key, BiFunction remappingFunction) { + throwExceptionIfClosed(); try (QueryContextInterface q = queryContext(key)) { methods.compute(q, remappingFunction, q.defaultReturnValue()); return q.defaultReturnValue().returnValue(); @@ -805,6 +831,7 @@ public class VanillaChronicleMap @Override public V computeIfAbsent(K key, Function mappingFunction) { + throwExceptionIfClosed(); try (QueryContextInterface q = queryContext(key)) { methods.computeIfAbsent(q, mappingFunction, q.defaultReturnValue()); return q.defaultReturnValue().returnValue(); @@ -814,6 +841,7 @@ public class VanillaChronicleMap @Override public V computeIfPresent(K key, BiFunction remappingFunction) { + throwExceptionIfClosed(); try (QueryContextInterface q = queryContext(key)) { methods.computeIfPresent(q, remappingFunction, q.defaultReturnValue()); return q.defaultReturnValue().returnValue(); @@ -821,6 +849,7 @@ public class VanillaChronicleMap } public void verifyTierCountersAreaData() { + throwExceptionIfClosed(); for (int i = 0; i < actualSegments; i++) { try (MapSegmentContext c = segmentContext(i)) { Method verifyTierCountersAreaData = diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/iter/MapSegmentIteration.java b/src/main/java/net/openhft/chronicle/map/impl/stage/iter/MapSegmentIteration.java index 4532c782b..72540a242 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/iter/MapSegmentIteration.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/iter/MapSegmentIteration.java @@ -40,11 +40,13 @@ public abstract class MapSegmentIteration extends HashSegmentIteration< @Override public void hookAfterEachIteration() { + throwExceptionIfClosed(); wrappedValueInstanceDataHolder.closeValue(); } @Override public void doReplaceValue(Data newValue) { + throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); try { entry.innerDefaultReplaceValue(newValue); diff --git a/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedMapSegmentIteration.java b/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedMapSegmentIteration.java index 46b1a8c55..d4c5c5f1a 100644 --- a/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedMapSegmentIteration.java +++ b/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedMapSegmentIteration.java @@ -60,21 +60,25 @@ public abstract class ReplicatedMapSegmentIteration extends MapSegmentI @Override public boolean shouldTestEntry() { + throwExceptionIfClosed(); return entriesToTest == ALL || !e.entryDeleted(); } @Override public Object entryForIteration() { + throwExceptionIfClosed(); return !e.entryDeleted() ? entryDelegating : absentEntryDelegating; } @Override public long tierEntriesForIteration() { + throwExceptionIfClosed(); return entriesToTest == ALL ? s.tierEntries() : s.tierEntries() - s.tierDeleted(); } @Override public void doReplaceValue(Data newValue) { + throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); try { entry.innerDefaultReplaceValue(newValue); @@ -87,6 +91,7 @@ public abstract class ReplicatedMapSegmentIteration extends MapSegmentI @Override public boolean forEachSegmentEntryWhile(Predicate> predicate) { + throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); initEntriesToTest(PRESENT); s.innerUpdateLock.lock(); @@ -96,6 +101,7 @@ public abstract class ReplicatedMapSegmentIteration extends MapSegmentI @Override public boolean forEachSegmentReplicableEntryWhile( Predicate predicate) { + throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); initEntriesToTest(ALL); s.innerUpdateLock.lock(); @@ -104,6 +110,7 @@ public abstract class ReplicatedMapSegmentIteration extends MapSegmentI @Override public void forEachSegmentReplicableEntry(Consumer action) { + throwExceptionIfClosed(); forEachSegmentReplicableEntryWhile(e -> { action.accept(e); return true; @@ -112,6 +119,7 @@ public abstract class ReplicatedMapSegmentIteration extends MapSegmentI @Override public void doRemove() { + throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); try { if (e.valueSize > dummyValue.size()) @@ -128,6 +136,7 @@ public abstract class ReplicatedMapSegmentIteration extends MapSegmentI @Override public void doRemoveCompletely() { + throwExceptionIfClosed(); boolean wasDeleted = e.entryDeleted(); super.doRemove(); ru.dropChange(); @@ -136,6 +145,7 @@ public abstract class ReplicatedMapSegmentIteration extends MapSegmentI } public void doInsert(Data value) { + throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); if (e.entryDeleted()) { try { @@ -155,6 +165,7 @@ public abstract class ReplicatedMapSegmentIteration extends MapSegmentI } public void doInsert() { + throwExceptionIfClosed(); if (mh.set() == null) throw new IllegalStateException(mh.h().toIdentityString() + ": Called SetAbsentEntry.doInsert() from Map context"); diff --git a/src/main/java/net/openhft/chronicle/set/SetFromMap.java b/src/main/java/net/openhft/chronicle/set/SetFromMap.java index c31204622..2e5bf508d 100644 --- a/src/main/java/net/openhft/chronicle/set/SetFromMap.java +++ b/src/main/java/net/openhft/chronicle/set/SetFromMap.java @@ -51,34 +51,42 @@ class SetFromMap extends AbstractSet implements ChronicleSet { } public void clear() { + throwExceptionIfClosed(); m.clear(); } public int size() { + throwExceptionIfClosed(); return m.size(); } public boolean isEmpty() { + throwExceptionIfClosed(); return m.isEmpty(); } public boolean contains(Object o) { + throwExceptionIfClosed(); return m.containsKey(o); } public boolean remove(Object o) { + throwExceptionIfClosed(); return m.remove(o, DUMMY_VALUE); } public boolean add(E e) { + throwExceptionIfClosed(); return m.putIfAbsent(e, DUMMY_VALUE) == null; } public Iterator iterator() { + throwExceptionIfClosed(); return s.iterator(); } public Object[] toArray() { + throwExceptionIfClosed(); return s.toArray(); } @@ -92,6 +100,7 @@ class SetFromMap extends AbstractSet implements ChronicleSet { @Override public String toIdentityString() { + throwExceptionIfClosed(); return "ChronicleSet{" + "name=" + name() + ", file=" + file() + @@ -100,43 +109,52 @@ class SetFromMap extends AbstractSet implements ChronicleSet { } public int hashCode() { + throwExceptionIfClosed(); return s.hashCode(); } public boolean equals(Object o) { + throwExceptionIfClosed(); return o == this || s.equals(o); } public boolean containsAll(Collection c) { + throwExceptionIfClosed(); return s.containsAll(c); } public boolean removeAll(Collection c) { + throwExceptionIfClosed(); return s.removeAll(c); } public boolean retainAll(Collection c) { + throwExceptionIfClosed(); return s.retainAll(c); } // addAll is the only inherited implementation @Override public long longSize() { + throwExceptionIfClosed(); return m.longSize(); } @Override public long offHeapMemoryUsed() { + throwExceptionIfClosed(); return m.offHeapMemoryUsed(); } @Override public Class keyClass() { + throwExceptionIfClosed(); return m.keyClass(); } @Override public Type keyType() { + throwExceptionIfClosed(); return m.keyType(); } @@ -171,6 +189,7 @@ class SetFromMap extends AbstractSet implements ChronicleSet { @Override public int segments() { + throwExceptionIfClosed(); return m.segments(); } @@ -178,23 +197,27 @@ class SetFromMap extends AbstractSet implements ChronicleSet { @Override public boolean forEachEntryWhile(Predicate> predicate) { + throwExceptionIfClosed(); Objects.requireNonNull(predicate); return m.forEachEntryWhile(e -> predicate.test(((SetEntry) e))); } @Override public void forEachEntry(Consumer> action) { + throwExceptionIfClosed(); Objects.requireNonNull(action); m.forEachEntry(e -> action.accept(((SetEntry) e))); } @Override public File file() { + throwExceptionIfClosed(); return m.file(); } @Override public String name() { + throwExceptionIfClosed(); return m.name(); } @@ -205,6 +228,7 @@ class SetFromMap extends AbstractSet implements ChronicleSet { @Override public boolean isOpen() { + throwExceptionIfClosed(); return m.isOpen(); } }