proj_name
stringclasses
157 values
relative_path
stringlengths
30
228
class_name
stringlengths
1
68
func_name
stringlengths
1
49
masked_class
stringlengths
68
9.82k
func_body
stringlengths
46
9.61k
len_input
int64
27
2.01k
len_output
int64
14
1.94k
total
int64
55
2.05k
relevant_context
stringlengths
0
38.4k
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/InMemoryChronicleHashResources.java
InMemoryChronicleHashResources
releaseMemoryResource
class InMemoryChronicleHashResources extends ChronicleHashResources { @Override void releaseMemoryResource(final MemoryResource allocation) {<FILL_FUNCTION_BODY>} }
assert SKIP_ASSERTIONS || assertAddress(allocation.address); assert SKIP_ASSERTIONS || assertPosition(allocation.size); OS.memory().freeMemory(allocation.address, allocation.size);
46
57
103
<methods>public non-sealed void <init>() ,public final boolean releaseManually() ,public void run() ,public final void setChronicleHashIdentityString(java.lang.String) <variables>private static final int COMPLETELY_CLOSED,private static final int OPEN,private static final int PARTIALLY_CLOSED,private java.lang.String chronicleHashIdentityString,private List<java.io.Closeable> closeables,private List<WeakReference<net.openhft.chronicle.hash.impl.ContextHolder>> contexts,private List<net.openhft.chronicle.hash.impl.MemoryResource> memoryResources,private volatile int state
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/IntCompactOffHeapLinearHashTable.java
IntCompactOffHeapLinearHashTable
writeEntryVolatile
class IntCompactOffHeapLinearHashTable extends CompactOffHeapLinearHashTable { private static final long SCALE = 4L; /** * Must not store {@code h} in a field, to avoid memory leaks. * * @see net.openhft.chronicle.hash.impl.stage.hash.Chaining#initMap */ IntCompactOffHeapLinearHashTable(VanillaChronicleHash h) { super(h); } @Override long indexToPos(long index) { return index * SCALE; } @Override public long step(long pos) { return (pos + SCALE) & capacityMask2; } @Override public long stepBack(long pos) { return (pos - SCALE) & capacityMask2; } @Override public long readEntry(final long address, final long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); return OS.memory().readInt(address + pos); } @Override public long readEntryVolatile(final long address, final long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); return OS.memory().readVolatileInt(address + pos); } @Override public void writeEntryVolatile(final long address, final long pos, final long key, final long value) {<FILL_FUNCTION_BODY>} @Override public void writeEntry(long address, long pos, long newEntry) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeInt(address + pos, (int) newEntry); } @Override public void clearEntry(long address, long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeInt(address + pos, 0); } }
assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeVolatileInt(address + pos, (int) entry(key, value));
557
57
614
<methods>public static long capacityFor(long) ,public void checkValueForPut(long) ,public abstract void clearEntry(long, long) ,public boolean empty(long) ,public long entry(long, long) ,public static int entrySize(int, int) ,public long hlPos(long) ,public long key(long) ,public static int keyBits(long, int) ,public static long mask(int) ,public long maskUnsetKey(long) ,public abstract long readEntry(long, long) ,public abstract long readEntryVolatile(long, long) ,public long remove(long, long) ,public abstract long step(long) ,public abstract long stepBack(long) ,public long value(long) ,public static int valueBits(long) ,public abstract void writeEntry(long, long, long) ,public abstract void writeEntryVolatile(long, long, long, long) <variables>public static final double MAX_LOAD_FACTOR,public static final int MAX_TIER_CHUNKS,public static final int MAX_TIER_ENTRIES,public static final double MAX_UPPER_BOUND_LOAD_FACTOR,public static final long UNSET_ENTRY,public static final long UNSET_KEY,private final non-sealed long capacityMask,final non-sealed long capacityMask2,private final non-sealed int keyBits,private final non-sealed long keyMask,private final non-sealed long valueMask
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/LongCompactOffHeapLinearHashTable.java
LongCompactOffHeapLinearHashTable
clearEntry
class LongCompactOffHeapLinearHashTable extends CompactOffHeapLinearHashTable { private static final long SCALE = 8L; /** * Must not store {@code h} in a field, to avoid memory leaks. * * @see net.openhft.chronicle.hash.impl.stage.hash.Chaining#initMap */ LongCompactOffHeapLinearHashTable(VanillaChronicleHash h) { super(h); } @Override long indexToPos(long index) { return index * SCALE; } @Override public long step(long pos) { return (pos + SCALE) & capacityMask2; } @Override public long stepBack(long pos) { return (pos - SCALE) & capacityMask2; } @Override public long readEntry(final long address, final long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); return OS.memory().readLong(address + pos); } @Override public long readEntryVolatile(final long address, final long pos) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); return OS.memory().readVolatileLong(address + pos); } @Override public void writeEntryVolatile(final long address, final long pos, final long key, final long value) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeVolatileLong(address + pos, entry(key, value)); } @Override public void writeEntry(final long address, final long pos, final long newEntry) { assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeLong(address + pos, newEntry); } @Override public void clearEntry(final long address, final long pos) {<FILL_FUNCTION_BODY>} }
assert SKIP_ASSERTIONS || assertAddress(address); assert SKIP_ASSERTIONS || assertPosition(pos); OS.memory().writeLong(address + pos, 0L);
570
50
620
<methods>public static long capacityFor(long) ,public void checkValueForPut(long) ,public abstract void clearEntry(long, long) ,public boolean empty(long) ,public long entry(long, long) ,public static int entrySize(int, int) ,public long hlPos(long) ,public long key(long) ,public static int keyBits(long, int) ,public static long mask(int) ,public long maskUnsetKey(long) ,public abstract long readEntry(long, long) ,public abstract long readEntryVolatile(long, long) ,public long remove(long, long) ,public abstract long step(long) ,public abstract long stepBack(long) ,public long value(long) ,public static int valueBits(long) ,public abstract void writeEntry(long, long, long) ,public abstract void writeEntryVolatile(long, long, long, long) <variables>public static final double MAX_LOAD_FACTOR,public static final int MAX_TIER_CHUNKS,public static final int MAX_TIER_ENTRIES,public static final double MAX_UPPER_BOUND_LOAD_FACTOR,public static final long UNSET_ENTRY,public static final long UNSET_KEY,private final non-sealed long capacityMask,final non-sealed long capacityMask2,private final non-sealed int keyBits,private final non-sealed long keyMask,private final non-sealed long valueMask
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/PersistedChronicleHashResources.java
PersistedChronicleHashResources
releaseExtraSystemResources
class PersistedChronicleHashResources extends ChronicleHashResources { private File file; public PersistedChronicleHashResources(File file) { this.file = file; OS.memory().storeFence(); // Emulate final semantics of the file field } @Override void releaseMemoryResource(MemoryResource mapping) throws IOException { OS.unmap(mapping.address, mapping.size); } @Override Throwable releaseExtraSystemResources() {<FILL_FUNCTION_BODY>} }
if (file == null) return null; Throwable thrown = null; try { CanonicalRandomAccessFiles.release(file); file = null; } catch (Throwable t) { thrown = t; } return thrown;
136
71
207
<methods>public non-sealed void <init>() ,public final boolean releaseManually() ,public void run() ,public final void setChronicleHashIdentityString(java.lang.String) <variables>private static final int COMPLETELY_CLOSED,private static final int OPEN,private static final int PARTIALLY_CLOSED,private java.lang.String chronicleHashIdentityString,private List<java.io.Closeable> closeables,private List<WeakReference<net.openhft.chronicle.hash.impl.ContextHolder>> contexts,private List<net.openhft.chronicle.hash.impl.MemoryResource> memoryResources,private volatile int state
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/data/bytes/EntryKeyBytesData.java
EntryKeyBytesData
innerGetUsing
class EntryKeyBytesData<K> extends AbstractData<K> { @StageRef VanillaChronicleHashHolder<K> hh; @StageRef KeyBytesInterop<K> ki; @StageRef SegmentStages s; @StageRef HashEntryStages<K> entry; @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @Stage("CachedEntryKey") private K cachedEntryKey; @Stage("CachedEntryKey") private boolean cachedEntryKeyRead = false; private void initCachedEntryKey() { cachedEntryKey = innerGetUsing(cachedEntryKey); cachedEntryKeyRead = true; } @Override public RandomDataInput bytes() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return s.segmentBS; } @Override public long offset() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return entry.keyOffset; } @Override public long size() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return entry.keySize; } @Override public long hash(LongHashFunction f) { checkOnEachPublicOperation.checkOnEachPublicOperation(); return super.hash(f); } @Override public K get() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return cachedEntryKey; } @Override public K getUsing(K using) { checkOnEachPublicOperation.checkOnEachPublicOperation(); return innerGetUsing(using); } private K innerGetUsing(K usingKey) {<FILL_FUNCTION_BODY>} }
Bytes bytes = s.segmentBytesForRead(); bytes.readPosition(entry.keyOffset); return ki.keyReader.read(bytes, size(), usingKey);
450
47
497
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/data/bytes/InputKeyBytesData.java
InputKeyBytesData
initInputKeyBytesStore
class InputKeyBytesData<K> extends AbstractData<K> { @Stage("InputKeyBytes") private final VanillaBytes inputKeyBytes = VanillaBytes.vanillaBytes();; @StageRef KeyBytesInterop<K> ki; @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @Stage("InputKeyBytesStore") private BytesStore inputKeyBytesStore = null; @Stage("InputKeyBytesStore") private long inputKeyBytesOffset; @Stage("InputKeyBytesStore") private long inputKeyBytesSize; @Stage("InputKeyBytes") private boolean inputKeyBytesUsed = false; @Stage("CachedInputKey") private K cachedInputKey; @Stage("CachedInputKey") private boolean cachedInputKeyRead = false; public void initInputKeyBytesStore(BytesStore bytesStore, long offset, long size) {<FILL_FUNCTION_BODY>} boolean inputKeyBytesInit() { return inputKeyBytesUsed; } void initInputKeyBytes() { inputKeyBytes.bytesStore(inputKeyBytesStore, inputKeyBytesOffset, inputKeyBytesSize); inputKeyBytesUsed = true; } void closeInputKeyBytes() { inputKeyBytes.bytesStore(BytesStore.empty(), 0, 0); inputKeyBytesUsed = false; } private void initCachedInputKey() { cachedInputKey = innerGetUsing(cachedInputKey); cachedInputKeyRead = true; } @Override public RandomDataInput bytes() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return inputKeyBytes.bytesStore(); } @Override public long offset() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return inputKeyBytesOffset; } @Override public long size() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return inputKeyBytesSize; } @Override public K get() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return cachedInputKey; } @Override public K getUsing(K using) { checkOnEachPublicOperation.checkOnEachPublicOperation(); return innerGetUsing(using); } private K innerGetUsing(K usingKey) { inputKeyBytes.readPosition(inputKeyBytesOffset); return ki.keyReader.read(inputKeyBytes, inputKeyBytesSize, usingKey); } }
inputKeyBytesStore = bytesStore; inputKeyBytesOffset = offset; inputKeyBytesSize = size;
642
31
673
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/AllocatedChunks.java
AllocatedChunks
initEntryAndKeyCopying
class AllocatedChunks { @StageRef public VanillaChronicleHashHolder<?> hh; @StageRef public SegmentStages s; @StageRef public HashEntryStages<?> entry; @StageRef public Alloc alloc; public int allocatedChunks = 0; public void initAllocatedChunks(int allocatedChunks) { this.allocatedChunks = allocatedChunks; } /** * @return {@code true} is tier has changed */ public boolean initEntryAndKeyCopying( long entrySize, long bytesToCopy, long prevPos, int prevChunks) {<FILL_FUNCTION_BODY>} }
initAllocatedChunks(hh.h().inChunks(entrySize)); long oldSegmentTierBaseAddr = s.tierBaseAddr; long oldKeySizeAddr = oldSegmentTierBaseAddr + entry.keySizeOffset; long oldKeyAddr = oldSegmentTierBaseAddr + entry.keyOffset; int tierBeforeAllocation = s.tier; long pos = alloc.alloc(allocatedChunks, prevPos, prevChunks); entry.copyExistingEntry(pos, bytesToCopy, oldKeyAddr, oldKeySizeAddr); return s.tier != tierBeforeAllocation;
188
153
341
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashEntryChecksumStrategy.java
HashEntryChecksumStrategy
computeChecksum
class HashEntryChecksumStrategy implements ChecksumStrategy { @StageRef SegmentStages s; @StageRef HashEntryStages<?> e; @StageRef KeyHashCode h; @Override public void computeAndStoreChecksum() { int checksum = computeChecksum(); s.segmentBS.writeInt(e.entryEnd(), checksum); } @Override public int computeChecksum() {<FILL_FUNCTION_BODY>} @Override public boolean innerCheckSum() { int oldChecksum = storedChecksum(); int checksum = computeChecksum(); return oldChecksum == checksum; } @Override public int storedChecksum() { return s.segmentBS.readInt(e.entryEnd()); } @Override public long extraEntryBytes() { return CHECKSUM_STORED_BYTES; } }
long keyHashCode = h.keyHashCode(); long keyEnd = e.keyEnd(); long len = e.entryEnd() - keyEnd; long checksum; if (len > 0) { long addr = s.tierBaseAddr + keyEnd; long payloadChecksum = LongHashFunction.xx_r39().hashMemory(addr, len); checksum = hash8To16Bytes(e.keySize, keyHashCode, payloadChecksum); } else { // non replicated ChronicleSet has no payload checksum = keyHashCode; } return (int) ((checksum >>> 32) ^ checksum);
257
173
430
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashEntryStages.java
HashEntryStages
checkSum
class HashEntryStages<K> implements HashEntry<K>, ChecksumEntry { @StageRef public VanillaChronicleHashHolder<?> hh; @StageRef public SegmentStages s; @StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef public HashLookupPos hlp; public long pos = -1; @Stage("EntryOffset") public long keySizeOffset = -1; public long keySize = -1; public long keyOffset = -1; public boolean delayedUpdateChecksum = false; @StageRef public EntryKeyBytesData<K> entryKey; @Stage("EntrySizeInChunks") public int entrySizeInChunks = 0; @StageRef HashEntryChecksumStrategy hashEntryChecksumStrategy; public final ChecksumStrategy checksumStrategy = hh.h().checksumEntries ? hashEntryChecksumStrategy : NoChecksumStrategy.INSTANCE; public void initPos(long pos) { this.pos = pos; } public abstract void closePos(); public abstract boolean entryOffsetInit(); public void initEntryOffset(long keySizeOffset) { this.keySizeOffset = keySizeOffset; } public void initEntryOffset() { keySizeOffset = s.entrySpaceOffset + pos * hh.h().chunkSize; } public abstract void closeEntryOffset(); public void initKeySize(long keySize) { this.keySize = keySize; } public abstract void closeKeySize(); public void initKeyOffset(long keyOffset) { this.keyOffset = keyOffset; } public abstract void closeKeyOffset(); public void readExistingEntry(long pos) { initPos(pos); Bytes segmentBytes = s.segmentBytesForRead(); segmentBytes.readPosition(keySizeOffset); initKeySize(hh.h().keySizeMarshaller.readSize(segmentBytes)); initKeyOffset(segmentBytes.readPosition()); } public void readFoundEntry(long pos, long keySizeOffset, long keySize, long keyOffset) { initPos(pos); initEntryOffset(keySizeOffset); initKeySize(keySize); initKeyOffset(keyOffset); } public void closeEntry() { closePos(); closeEntryOffset(); closeKeySize(); closeKeyOffset(); } public void writeNewEntry(long pos, Data<?> key) { initPos(pos); initKeySize(key.size()); Bytes segmentBytes = s.segmentBytesForWrite(); segmentBytes.writePosition(keySizeOffset); hh.h().keySizeMarshaller.writeSize(segmentBytes, keySize); initKeyOffset(segmentBytes.writePosition()); key.writeTo(s.segmentBS, keyOffset); } public void copyExistingEntry( long newPos, long bytesToCopy, long oldKeyAddr, long oldKeySizeAddr) { initPos(newPos); initKeyOffset(keySizeOffset + (oldKeyAddr - oldKeySizeAddr)); // Calling Access.copy() which is probably slower because not of abstractions, // because there is no BytesStore.write(off, addr, len) method. Alternative is // to make a final BytesStore rawMemoryStore = new PointerBytesStore().set(0, Long.MAX_V) // and here: s.segmentBS.write(keySizeOffset, rawMemoryStore, keySizeAddr, bytesToCopy) Access.copy( nativeAccess(), null, oldKeySizeAddr, checkedBytesStoreAccess(), s.segmentBS, keySizeOffset, bytesToCopy); } public long keyEnd() { return keyOffset + keySize; } public long entryEnd() { return keyEnd(); } public void initDelayedUpdateChecksum(boolean delayedUpdateChecksum) { // makes delayedUpdateChecksum dependent on keySizeOffset and Locks stages, to trigger // delayedUpdateChecksum close on these stages' close assert entryOffsetInit() && keySizeOffset >= 0; assert s.locksInit() && s.localLockState != LocalLockState.UNLOCKED; assert delayedUpdateChecksum; // doesn't make sense to init to "uninit" false value this.delayedUpdateChecksum = true; } abstract boolean delayedUpdateChecksumInit(); public void closeDelayedUpdateChecksum() { if (hh.h().checksumEntries) hashEntryChecksumStrategy.computeAndStoreChecksum(); delayedUpdateChecksum = false; } @Override public void updateChecksum() { checkOnEachPublicOperation.checkOnEachPublicOperation(); if (!hh.h().checksumEntries) { throw new UnsupportedOperationException(hh.h().toIdentityString() + ": Checksum is not stored in this Chronicle Hash"); } s.innerUpdateLock.lock(); initDelayedUpdateChecksum(true); } @Override public boolean checkSum() {<FILL_FUNCTION_BODY>} long entrySize() { return checksumStrategy.extraEntryBytes() + entryEnd() - keySizeOffset; } @NotNull @Override public Data<K> key() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return entryKey; } void initEntrySizeInChunks() { entrySizeInChunks = hh.h().inChunks(entrySize()); } public void initEntrySizeInChunks(int actuallyUsedChunks) { entrySizeInChunks = actuallyUsedChunks; } public void innerRemoveEntryExceptHashLookupUpdate() { s.free(pos, entrySizeInChunks); s.incrementModCount(); } }
checkOnEachPublicOperation.checkOnEachPublicOperation(); if (!hh.h().checksumEntries) { throw new UnsupportedOperationException(hh.h().toIdentityString() + ": Checksum is not stored in this Chronicle Hash"); } // This is needed, because a concurrent update lock holder might perform an entry update, // but not yet written a checksum (because checksum write is delayed to the update unlock). // So checkSum() on read lock level might result to false negative results. s.innerUpdateLock.lock(); return delayedUpdateChecksumInit() || checksumStrategy.innerCheckSum();
1,527
158
1,685
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupPos.java
HashLookupPos
initHashLookupPos
class HashLookupPos { public long hashLookupPos = -1; @StageRef HashLookupSearch hls; @StageRef SegmentStages s; public abstract boolean hashLookupPosInit(); public void initHashLookupPos() {<FILL_FUNCTION_BODY>} public void initHashLookupPos(long hashLookupPos) { this.hashLookupPos = hashLookupPos; } @Stage("HashLookupPos") public void setHashLookupPos(long hashLookupPos) { this.hashLookupPos = hashLookupPos; } public abstract void closeHashLookupPos(); }
// Validation + make hashLookupPos a dependant of tier. This is needed, because after // tier change should re-perform hashLookupSearch, starting from the searchStartPos. // Not an assert statement, because segmentTier stage should be initialized regardless // assertions enabled or not. if (s.tier < 0) throw new AssertionError(); s.innerReadLock.lock(); this.hashLookupPos = hls.searchStartPos;
180
121
301
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/HashLookupSearch.java
HashLookupSearch
nextPos
class HashLookupSearch { @StageRef public VanillaChronicleHashHolder<?> hh; @Stage("SearchKey") public long searchStartPos; @StageRef SegmentStages s; @StageRef HashLookupPos hlp; @StageRef KeySearch<?> ks; @StageRef MapEntryStages<?, ?> e; @Stage("SearchKey") long searchKey = UNSET_KEY; public CompactOffHeapLinearHashTable hl() { return hh.h().hashLookup; } public void initSearchKey(long searchKey) { this.searchKey = searchKey; searchStartPos = hl().hlPos(searchKey); } private long addr() { return s.tierBaseAddr; } public long nextPos() {<FILL_FUNCTION_BODY>} public void found() { hlp.setHashLookupPos(hl().stepBack(hlp.hashLookupPos)); } public void remove() { hlp.setHashLookupPos(hl().remove(addr(), hlp.hashLookupPos)); } public void putNewVolatile(long entryPos) { // Correctness check + make putNewVolatile() dependant on keySearch, this, in turn, // is needed for hlp.hashLookupPos re-initialization after nextTier(). // Not an assert statement, because ks.searchStatePresent() should run regardless assertions // enabled or not. boolean keySearchReInit = !ks.keySearchInit(); if (ks.searchStatePresent()) throw new AssertionError(); if (keySearchReInit) { // if key search was re-init, entry was re-init too during the search e.readExistingEntry(entryPos); } hl().checkValueForPut(entryPos); hl().writeEntryVolatile(addr(), hlp.hashLookupPos, searchKey, entryPos); } public boolean checkSlotContainsExpectedKeyAndValue(long value) { // volatile read not needed here because this method is for verifying within-thread // invariants long entry = hl().readEntry(addr(), hlp.hashLookupPos); return hl().key(entry) == searchKey && hl().value(entry) == value; } }
long pos = hlp.hashLookupPos; CompactOffHeapLinearHashTable hl = hl(); while (true) { // read volatile to make a happens-before edge between entry insertion from concurrent // thread under update lock and this thread (reading the entry) long entry = hl.readEntryVolatile(addr(), pos); if (hl.empty(entry)) { hlp.setHashLookupPos(pos); return -1L; } pos = hl.step(pos); if (pos == searchStartPos) break; if (hl.key(entry) == searchKey) { hlp.setHashLookupPos(pos); return hl.value(entry); } } throw new IllegalStateException(hh.h().toIdentityString() + ": HashLookup overflow should never occur");
618
222
840
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/ReadLock.java
ReadLock
unlock
class ReadLock implements InterProcessLock { @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef SegmentStages s; @StageRef HashEntryStages entry; @StageRef HashLookupPos hlp; @Override public boolean isHeldByCurrentThread() { checkOnEachPublicOperation.checkOnEachLockOperation(); return s.localLockState.read; } @Override public void lock() { checkOnEachPublicOperation.checkOnEachLockOperation(); if (s.localLockState == UNLOCKED) { if (s.readZero() && s.updateZero() && s.writeZero()) { try { s.segmentHeader.readLock(s.segmentHeaderAddress); } catch (InterProcessDeadLockException e) { throw s.debugContextsAndLocks(e); } } s.incrementRead(); s.setLocalLockState(READ_LOCKED); } } @Override public void lockInterruptibly() throws InterruptedException { checkOnEachPublicOperation.checkOnEachLockOperation(); if (Thread.interrupted()) throw new InterruptedException(); if (s.localLockState == UNLOCKED) { if (s.readZero() && s.updateZero() && s.writeZero()) { try { s.segmentHeader.readLockInterruptibly(s.segmentHeaderAddress); } catch (InterProcessDeadLockException e) { throw s.debugContextsAndLocks(e); } } s.incrementRead(); s.setLocalLockState(READ_LOCKED); } } @Override public boolean tryLock() { checkOnEachPublicOperation.checkOnEachLockOperation(); if (s.localLockState == UNLOCKED) { if (!s.readZero() || !s.updateZero() || !s.writeZero() || s.segmentHeader.tryReadLock(s.segmentHeaderAddress)) { s.incrementRead(); s.setLocalLockState(READ_LOCKED); return true; } else { return false; } } else { return true; } } @Override public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException { checkOnEachPublicOperation.checkOnEachLockOperation(); if (Thread.interrupted()) throw new InterruptedException(); if (s.localLockState == UNLOCKED) { if (!s.readZero() || !s.updateZero() || !s.writeZero() || s.segmentHeader.tryReadLock(s.segmentHeaderAddress, time, unit)) { s.incrementRead(); s.setLocalLockState(READ_LOCKED); return true; } else { return false; } } else { return true; } } @Override public void unlock() {<FILL_FUNCTION_BODY>} @Override public boolean isHeld() { return s.localLockState != null && s.localLockState != UNLOCKED; } }
checkOnEachPublicOperation.checkOnEachLockOperation(); if (s.localLockState != UNLOCKED) { // TODO what should close here? hlp.closeHashLookupPos(); entry.closeEntry(); } s.readUnlockAndDecrementCount(); s.setLocalLockState(UNLOCKED);
825
89
914
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/entry/UpdateLock.java
UpdateLock
unlock
class UpdateLock implements InterProcessLock { @StageRef VanillaChronicleHashHolder<?> hh; @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef SegmentStages s; @StageRef HashEntryStages<?> entry; @Override public boolean isHeldByCurrentThread() { checkOnEachPublicOperation.checkOnEachLockOperation(); return s.localLockState.update; } @Override public void lock() { checkOnEachPublicOperation.checkOnEachLockOperation(); switch (s.localLockState) { case UNLOCKED: s.checkIterationContextNotLockedInThisThread(); if (s.updateZero() && s.writeZero()) { if (!s.readZero()) throw forbiddenUpdateLockWhenOuterContextReadLocked(); try { s.segmentHeader.updateLock(s.segmentHeaderAddress); } catch (InterProcessDeadLockException e) { throw s.debugContextsAndLocks(e); } } s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return; case READ_LOCKED: throw forbiddenUpgrade(); case UPDATE_LOCKED: case WRITE_LOCKED: // do nothing } } /** * Non-static because after compilation it becomes inner class which forbids static methods */ @NotNull private IllegalMonitorStateException forbiddenUpgrade() { return new IllegalMonitorStateException( hh.h().toIdentityString() + ": Cannot upgrade from read to update lock"); } /** * Non-static because after compilation it becomes inner class which forbids static methods */ @NotNull private IllegalStateException forbiddenUpdateLockWhenOuterContextReadLocked() { return new IllegalStateException(hh.h().toIdentityString() + ": Cannot acquire update lock, because outer context holds read lock. " + "In this case you should acquire update lock in the outer context up front"); } @Override public void lockInterruptibly() throws InterruptedException { checkOnEachPublicOperation.checkOnEachLockOperation(); if (Thread.interrupted()) throw new InterruptedException(); switch (s.localLockState) { case UNLOCKED: s.checkIterationContextNotLockedInThisThread(); if (s.updateZero() && s.writeZero()) { if (!s.readZero()) throw forbiddenUpdateLockWhenOuterContextReadLocked(); try { s.segmentHeader.updateLockInterruptibly(s.segmentHeaderAddress); } catch (InterProcessDeadLockException e) { throw s.debugContextsAndLocks(e); } } s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return; case READ_LOCKED: throw forbiddenUpgrade(); case UPDATE_LOCKED: case WRITE_LOCKED: // do nothing } } @Override public boolean tryLock() { checkOnEachPublicOperation.checkOnEachLockOperation(); switch (s.localLockState) { case UNLOCKED: s.checkIterationContextNotLockedInThisThread(); if (s.updateZero() && s.writeZero()) { if (!s.readZero()) throw forbiddenUpdateLockWhenOuterContextReadLocked(); if (s.segmentHeader.tryUpdateLock(s.segmentHeaderAddress)) { s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return true; } else { return false; } } else { s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return true; } case READ_LOCKED: throw forbiddenUpgrade(); case UPDATE_LOCKED: case WRITE_LOCKED: return true; default: throw new IllegalStateException(hh.h().toIdentityString() + ": unexpected localLockState=" + s.localLockState); } } @Override public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException { checkOnEachPublicOperation.checkOnEachLockOperation(); if (Thread.interrupted()) throw new InterruptedException(); switch (s.localLockState) { case UNLOCKED: s.checkIterationContextNotLockedInThisThread(); if (s.updateZero() && s.writeZero()) { if (!s.readZero()) throw forbiddenUpdateLockWhenOuterContextReadLocked(); if (s.segmentHeader.tryUpdateLock(s.segmentHeaderAddress, time, unit)) { s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return true; } else { return false; } } else { s.incrementUpdate(); s.setLocalLockState(UPDATE_LOCKED); return true; } case READ_LOCKED: throw forbiddenUpgrade(); case UPDATE_LOCKED: case WRITE_LOCKED: return true; default: throw new IllegalStateException(hh.h().toIdentityString() + ": unexpected localLockState=" + s.localLockState); } } @Override public void unlock() {<FILL_FUNCTION_BODY>} @Override public boolean isHeld() { return s.localLockState != null && s.localLockState != UNLOCKED; } }
checkOnEachPublicOperation.checkOnEachLockOperation(); switch (s.localLockState) { case UNLOCKED: case READ_LOCKED: return; case UPDATE_LOCKED: entry.closeDelayedUpdateChecksum(); if (s.decrementUpdate() == 0 && s.writeZero()) { s.segmentHeader.downgradeUpdateToReadLock(s.segmentHeaderAddress); } break; case WRITE_LOCKED: entry.closeDelayedUpdateChecksum(); if (s.decrementWrite() == 0) { if (!s.updateZero()) { s.segmentHeader.downgradeWriteToUpdateLock(s.segmentHeaderAddress); } else { s.segmentHeader.downgradeWriteToReadLock(s.segmentHeaderAddress); } } } s.incrementRead(); s.setLocalLockState(READ_LOCKED);
1,445
242
1,687
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/hash/Chaining.java
Chaining
initUsed
class Chaining extends ChainingInterface { public final List<ChainingInterface> contextChain; public final int indexInContextChain; /** * First context, ever created in this thread. rootContextInThisThread === contextChain.get(0). */ public final ChainingInterface rootContextInThisThread; @Stage("Used") public boolean used; @Stage("Used") private boolean firstContextLockedInThisThread; public Chaining(VanillaChronicleMap map) { contextChain = new ArrayList<>(); contextChain.add(this); indexInContextChain = 0; rootContextInThisThread = this; initMap(map); } public Chaining(ChainingInterface rootContextInThisThread, VanillaChronicleMap map) { contextChain = rootContextInThisThread.getContextChain(); indexInContextChain = contextChain.size(); contextChain.add(this); this.rootContextInThisThread = rootContextInThisThread; initMap(map); } private static <T extends ChainingInterface> T initUsedAndReturn( VanillaChronicleMap map, ChainingInterface context) { try { context.initUsed(true, map); //noinspection unchecked return (T) context; } catch (Throwable throwable) { try { ((AutoCloseable) context).close(); } catch (Throwable t) { throwable.addSuppressed(t); } throw throwable; } } @Override public List<ChainingInterface> getContextChain() { return contextChain; } public <T> T contextAtIndexInChain(int index) { //noinspection unchecked return (T) contextChain.get(index); } /** * This method stores a reference to the context's owner ChronicleMap into a field of the * context in the beginning of each usage of the context. Previously, this field was final and * set only once during context creation. It was preventing ChronicleMap objects from becoming * dead and collected by the GC, while any thread, from which the ChronicleMap was accessed * (hence a thread local context created), is alive. * <p> * The chain of strong references: * 1) Thread -> * 2) ThreadLocalMap -> * 3) Entry with ThreadLocal {@link net.openhft.chronicle.map.VanillaChronicleMap#cxt} as weak * referent and a context (e. g. {@link net.openhft.chronicle.map.impl.CompiledMapQueryContext}) * as value (a simple field, not a weak reference!) -> * 4) final reference to the owner {@link VanillaChronicleMap} -> * 5) ThreadLocal {@link net.openhft.chronicle.map.VanillaChronicleMap#cxt} (a strong reference * this time! note that this ThreadLocal is an instance field of VanillaChronicleMap) * <p> * So in order to break this chain at step 4), contexts store references to their owner * ChronicleMaps only when contexts are used. */ public abstract void initMap(VanillaChronicleMap map); @Override public boolean usedInit() { return used; } /** * Init method parameterized to avoid automatic initialization. {@code used} argument should * always be {@code true}. */ @Override public void initUsed(boolean used, VanillaChronicleMap map) {<FILL_FUNCTION_BODY>} @SuppressWarnings("unused") void closeUsed() { used = false; if (firstContextLockedInThisThread) rootContextInThisThread.unlockContextLocally(); } @Override public <T extends ChainingInterface> T getContext( Class<? extends T> contextClass, BiFunction<ChainingInterface, VanillaChronicleMap, T> createChaining, VanillaChronicleMap map) { for (int i = 0; i < contextChain.size(); i++) { ChainingInterface context = contextChain.get(i); if (context.getClass() == contextClass && !context.usedInit()) { return initUsedAndReturn(map, context); } } int maxNestedContexts = 1 << 10; if (contextChain.size() > maxNestedContexts) { throw new IllegalStateException(map.toIdentityString() + ": More than " + maxNestedContexts + " nested ChronicleHash contexts\n" + "are not supported. Very probable that you simply forgot to close context\n" + "somewhere (recommended to use try-with-resources statement).\n" + "Otherwise this is a bug, please report with this\n" + "stack trace on https://github.com/OpenHFT/Chronicle-Map/issues"); } //noinspection unchecked T context = createChaining.apply(this, map); return initUsedAndReturn(map, context); } }
assert used; firstContextLockedInThisThread = rootContextInThisThread.lockContextLocally(map); initMap(map); this.used = true;
1,304
46
1,350
<methods>public non-sealed void <init>() ,public abstract T getContext(Class<? extends T>, BiFunction<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface,VanillaChronicleMap#RAW,T>, VanillaChronicleMap#RAW) ,public abstract List<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface> getContextChain() ,public abstract void initUsed(boolean, VanillaChronicleMap#RAW) ,public abstract boolean usedInit() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/hash/OwnerThreadHolder.java
OwnerThreadHolder
checkAccessingFromOwnerThread
class OwnerThreadHolder { final Thread owner = Thread.currentThread(); @StageRef VanillaChronicleHashHolder<?> hh; public void checkAccessingFromOwnerThread() {<FILL_FUNCTION_BODY>} }
if (owner != Thread.currentThread()) { throw new ConcurrentModificationException(hh.h().toIdentityString() + ": Context shouldn't be accessed from multiple threads"); }
67
52
119
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/hash/ThreadLocalState.java
ThreadLocalState
closeContext
class ThreadLocalState { private static final Memory MEMORY = OS.memory(); private static final long CONTEXT_LOCK_OFFSET; private static final int CONTEXT_UNLOCKED = 0; private static final int CONTEXT_LOCKED_LOCALLY = 1; private static final int CONTEXT_CLOSED = 2; static { try { Field contextLockField = ThreadLocalState.class.getDeclaredField("contextLock"); contextLockField.setAccessible(true); CONTEXT_LOCK_OFFSET = MEMORY.getFieldOffset(contextLockField); } catch (NoSuchFieldException e) { throw new AssertionError(e); } } public boolean iterationContextLockedInThisThread; private volatile int contextLock = CONTEXT_UNLOCKED; /** * Returns {@code true} if this is the outer context lock in this thread, {@code false} if this * is a nested context. */ public boolean lockContextLocally(ChronicleHash<?, ?, ?, ?> hash) { // hash().isOpen() check guarantees no starvation of a thread calling chMap.close() and // trying to close this context by closeContext() method below, while the thread owning this // context frequently locks and unlocks it (e. g. in a loop). This is also the only check // for chMap openness during the whole context usage lifecycle. if (hash.isOpen() && MEMORY.compareAndSwapInt(this, CONTEXT_LOCK_OFFSET, CONTEXT_UNLOCKED, CONTEXT_LOCKED_LOCALLY)) { return true; } else { if (contextLock == CONTEXT_LOCKED_LOCALLY) return false; // Don't extract this hash().isOpen() and the one above, because they could return // different results: the first (above) could return true, the second (below) - false. if (contextLock == CONTEXT_CLOSED || !hash.isOpen()) throw new ChronicleHashClosedException(hash); throw new AssertionError("Unknown context lock state: " + contextLock); } } public void unlockContextLocally() { // Ensure all reads from mapped memory are done before thread calling chronicleMap.close() // frees resources potentially unmapping some memory from where those reads are performed. MEMORY.loadFence(); // Avoid volatile write to avoid expensive store-load barrier MEMORY.writeOrderedInt(this, CONTEXT_LOCK_OFFSET, CONTEXT_UNLOCKED); } public void closeContext(String chronicleHashIdentityString) {<FILL_FUNCTION_BODY>} private boolean tryCloseContext() { return MEMORY.compareAndSwapInt(this, CONTEXT_LOCK_OFFSET, CONTEXT_UNLOCKED, CONTEXT_CLOSED); } public abstract Thread owner(); /** * Returns if this ThreadLocalState prevents a Map from being closed (which may be an asynchronous snapshot operation). * <p> * This method returns a snapshot value from a potentially volatile source that may change at any time. * * @return if prevents closing */ public boolean preventClose() { return isLocked(); } /** * Returns if this ThreadLocalState is locked (asynchronous snapshot operation). * <p> * This method returns a snapshot value from a volatile source that may change at any time. * * @return if locked */ protected boolean isLocked() { return contextLock == CONTEXT_LOCKED_LOCALLY; } }
if (tryCloseContext()) return; // Unless there are bugs in this codebase, it could happen that // contextLock == CONTEXT_CLOSED here only if closeContext() has succeed, and the subsequent // contextHolder.clear() has failed in ChronicleHashResources.closeContext(), though this is // hardly imaginable: contextHolder.clear() couldn't fail with OutOfMemoryError (because // there are no allocations in this method) and StackOverflowError (because in this case // closeContext() would fail with StackOverflowError before). But anyway it's probably // a good idea to make this check rather than not to make. if (contextLock == CONTEXT_CLOSED) return; // If first attempt of closing a context (i. e. moving from unused to closed state) failed, // it means that the context is still in use. If this context belongs to the current thread, // this is a bug, because we cannot "wait" until context is unused in the same thread: if (owner() == Thread.currentThread()) { throw new IllegalStateException(chronicleHashIdentityString + ": Attempt to close a Chronicle Hash in the context " + "of not yet finished query or iteration"); } // If the context belongs to a different thread, wait until that thread finishes it's work // with the context: // Double the current timeout for segment locks "without timeout", that effectively // specifies maximum lock (hence context) holding time long timeoutMillis = TimeUnit.SECONDS.toMillis(LOCK_TIMEOUT_SECONDS) * 2; long lastTime = System.currentTimeMillis(); do { if (tryCloseContext()) return; // Unless there are bugs in this codebase, this should never happen. But anyway it's // probably a good idea to make this check rather than not to make. if (contextLock == CONTEXT_CLOSED) return; Thread.yield(); long now = System.currentTimeMillis(); if (now != lastTime) { lastTime = now; timeoutMillis--; } } while (timeoutMillis >= 0); throw new RuntimeException(chronicleHashIdentityString + ": Failed to close a context, belonging to the thread\n" + owner() + ", in the state: " + owner().getState() + "\n" + "Possible reasons:\n" + "- The context owner thread exited before closing this context. Ensure that you\n" + "always close opened Chronicle Map's contexts, the best way to do this is to use\n" + "try-with-resources blocks." + "- The context owner thread runs some context operation (e. g. a query) for\n" + "unexpectedly long time (at least " + LOCK_TIMEOUT_SECONDS + " seconds).\n" + "You should either redesign your logic to spend less time in Chronicle Map\n" + "contexts (recommended) or synchronize map.close() with queries externally,\n" + "so that close() is called only after all query operations finished.\n" + "- Iteration over a large Chronicle Map takes more than " + LOCK_TIMEOUT_SECONDS + " seconds.\n" + "In this case you should synchronize map.close() with iterations over the map\n" + "externally, so that close() is called only after all iterations are finished.\n" + "- This is a dead lock involving the context owner thread and this thread (from\n" + "which map.close() method is called. Make sure you always close Chronicle Map\n" + "contexts, preferably using try-with-resources blocks.");
912
923
1,835
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/HashSegmentIteration.java
HashSegmentIteration
forEachTierEntryWhile
class HashSegmentIteration<K, E extends HashEntry<K>> implements HashEntry<K>, HashSegmentContext<K, E> { @StageRef public IterationSegmentStages s; @StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation; public boolean entryRemovedOnThisIteration = false; public long hashLookupEntry = 0; @StageRef protected HashLookupPos hlp; @StageRef HashEntryStages<K> e; @StageRef VanillaChronicleHashHolder<?> hh; public boolean shouldTestEntry() { return true; } public Object entryForIteration() { return this; } public long tierEntriesForIteration() { throwExceptionIfClosed(); return s.tierEntries(); } abstract boolean entryRemovedOnThisIterationInit(); protected void initEntryRemovedOnThisIteration(boolean entryRemovedOnThisIteration) { this.entryRemovedOnThisIteration = entryRemovedOnThisIteration; } public abstract boolean hashLookupEntryInit(); public void initHashLookupEntry(long entry) { throwExceptionIfClosed(); hashLookupEntry = entry; } abstract void closeHashLookupEntry(); @Override public boolean forEachSegmentEntryWhile(Predicate<? super E> predicate) { throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerUpdateLock.lock(); return innerForEachSegmentEntryWhile(predicate); } public <T> boolean innerForEachSegmentEntryWhile(Predicate<? super T> predicate) { try { s.goToLastTier(); while (true) { int currentTier = s.tier; long currentTierBaseAddr = s.tierBaseAddr; long currentTierIndex = s.tierIndex; boolean interrupted = forEachTierEntryWhile( predicate, currentTier, currentTierBaseAddr, currentTierIndex); if (interrupted) return false; if (currentTier == 0) return true; s.prevTier(); } } finally { closeHashLookupEntry(); s.innerReadLock.unlock(); initEntryRemovedOnThisIteration(false); } } public <T> boolean forEachTierEntryWhile( Predicate<? super T> predicate, int currentTier, long currentTierBaseAddr, long tierIndex) {<FILL_FUNCTION_BODY>} public void hookAfterEachIteration() { throwExceptionIfClosed(); } @Override public void forEachSegmentEntry(Consumer<? super E> action) { throwExceptionIfClosed(); forEachSegmentEntryWhile(e -> { action.accept(e); return true; }); } public void checkEntryNotRemovedOnThisIteration() { throwExceptionIfClosed(); if (entryRemovedOnThisIterationInit()) { throw new IllegalStateException( hh.h().toIdentityString() + ": Entry was already removed on this iteration"); } } @Override public void doRemove() { throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerWriteLock.lock(); try { iterationRemove(); } finally { s.innerWriteLock.unlock(); } initEntryRemovedOnThisIteration(true); } 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, // to consume the shifted entry hlp.setHashLookupPos(hh.h().hashLookup.stepBack(hlp.hashLookupPos)); } e.innerRemoveEntryExceptHashLookupUpdate(); } }
long leftEntries = tierEntriesForIteration(); boolean interrupted = false; long startPos = 0L; CompactOffHeapLinearHashTable hashLookup = hh.h().hashLookup; // volatile read not needed because iteration is performed at least under update lock while (!hashLookup.empty(hashLookup.readEntry(currentTierBaseAddr, startPos))) { startPos = hashLookup.step(startPos); } hlp.initHashLookupPos(startPos); long currentHashLookupPos; int steps = 0; do { // Step from hlp.hashLookupPos, not currentHashLookupPos (with additional initialization // of this local variable to startPos outside the loop), because if e.remove() is // called in the `predicate`, hlp.hashLookupPos is stepped back in doRemove(), and // currentHashLookupPos become invalid currentHashLookupPos = hashLookup.step(hlp.hashLookupPos); steps++; hlp.setHashLookupPos(currentHashLookupPos); // volatile read not needed because iteration is performed at least under update lock long entry = hashLookup.readEntry(currentTierBaseAddr, currentHashLookupPos); initHashLookupEntry(entry); if (!hashLookup.empty(entry)) { e.readExistingEntry(hashLookup.value(entry)); if (shouldTestEntry()) { initEntryRemovedOnThisIteration(false); try { if (!predicate.test((T) entryForIteration())) { interrupted = true; break; } else { if (--leftEntries == 0) break; } } finally { hookAfterEachIteration(); // if doReplaceValue() -> relocation() -> alloc() -> nextTier() // was called, restore the tier we were iterating over if (s.tier != currentTier) { s.initSegmentTier_WithBaseAddr( currentTier, currentTierBaseAddr, tierIndex); // To cover shift deleted slot, at the next step forward. // hash lookup entry is relocated to the next chained tier, and the // slot in _current_ tier's hash lookup is shift deleted, see // relocation() currentHashLookupPos = hashLookup.stepBack(currentHashLookupPos); steps--; hlp.initHashLookupPos(currentHashLookupPos); } s.innerWriteLock.unlock(); // force entry checksum update (delayedUpdateChecksum depends on keyOffset) e.closeKeyOffset(); } } } // the `steps == 0` condition and this variable updates in the loop fix the bug, when // shift deletion occurs on the first entry of the tier, and the currentHashLookupPos // becomes equal to start pos without making the whole loop, but only visiting a single // entry } while (currentHashLookupPos != startPos || steps == 0); if (!interrupted && leftEntries > 0) { throw new IllegalStateException(hh.h().toIdentityString() + ": We went through a tier without interruption, " + "but according to tier counters there should be " + leftEntries + " more entries. Size diverged?"); } return interrupted;
1,080
851
1,931
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationAlloc.java
IterationAlloc
alloc
class IterationAlloc implements Alloc { @StageRef public SegmentStages s; /** * Move only to next tiers, to avoid double visiting of relocated entries during iteration */ @Override public long alloc(int chunks, long prevPos, int prevChunks) {<FILL_FUNCTION_BODY>} }
long ret = s.allocReturnCode(chunks); if (prevPos >= 0) s.free(prevPos, prevChunks); if (ret >= 0) return ret; while (true) { s.nextTier(); ret = s.allocReturnCode(chunks); if (ret >= 0) return ret; }
91
96
187
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationKeyHashCode.java
IterationKeyHashCode
initKeyHash
class IterationKeyHashCode implements KeyHashCode { @StageRef VanillaChronicleHashHolder<?> hh; @StageRef SegmentStages s; @StageRef HashEntryStages<?> e; long keyHash = 0; void initKeyHash() {<FILL_FUNCTION_BODY>} @Override public long keyHashCode() { return keyHash; } }
long addr = s.tierBaseAddr + e.keyOffset; long len = e.keySize; keyHash = LongHashFunction.xx_r39().hashMemory(addr, len);
120
52
172
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/IterationSegmentStages.java
IterationSegmentStages
checkNestedContextsQueryDifferentKeys
class IterationSegmentStages extends SegmentStages { @StageRef VanillaChronicleHashHolder<?> hh; @StageRef HashSegmentIteration it; @StageRef HashLookupSearch hls; /** * During iteration, nextTier() is called in doReplaceValue() -> relocation() -> alloc(). * When the entry is relocated to the next tier, an entry should be inserted into hash * lookup. To insert an entry into hashLookup, should 1) locate empty slot, see {@link * KeySearch#initKeySearch()}, and 2) know the part of the hash code to insert, we know * it during iteration */ @Override public void nextTier() { super.nextTier(); if (it.hashLookupEntryInit()) hls.initSearchKey(hh.h().hashLookup.key(it.hashLookupEntry)); } public void initSegmentTier_WithBaseAddr(int tier, long tierBaseAddr, long tierIndex) { this.tier = tier; this.tierIndex = tierIndex; this.tierBaseAddr = tierBaseAddr; } @Override public void checkNestedContextsQueryDifferentKeys( LocksInterface innermostContextOnThisSegment) {<FILL_FUNCTION_BODY>} }
// this check is relevant only for query contexts
356
14
370
<methods>public non-sealed void <init>() ,public long allocReturnCode(int) ,public int changeAndGetLatestSameThreadSegmentModCount(int) ,public int changeAndGetTotalReadLockCount(int) ,public int changeAndGetTotalUpdateLockCount(int) ,public int changeAndGetTotalWriteLockCount(int) ,public void checkIterationContextNotLockedInThisThread() ,public void checkNestedContextsQueryDifferentKeys(net.openhft.chronicle.hash.impl.stage.entry.LocksInterface) ,public java.lang.RuntimeException debugContextsAndLocks(net.openhft.chronicle.hash.locks.InterProcessDeadLockException) ,public java.lang.String debugLocksState() ,public int decrementRead() ,public int decrementUpdate() ,public int decrementWrite() ,public void free(long, int) ,public void freeExtra(long, int, int) ,public void goToFirstTier() ,public void goToLastTier() ,public boolean hasNextTier() ,public void incrementModCount() ,public void incrementRead() ,public void incrementUpdate() ,public void incrementWrite() ,public void initSegmentIndex(int) ,public void initSegmentTier() ,public void initSegmentTier(int, long) ,public void initSegmentTier(int, long, long) ,public abstract boolean locksInit() ,public long lowestPossiblyFreeChunk() ,public void lowestPossiblyFreeChunk(long) ,public void nextTier() ,public long nextTierIndex() ,public void nextTierIndex(long) ,public void prevTier() ,public long prevTierIndex() ,public void prevTierIndex(long) ,public net.openhft.chronicle.hash.locks.InterProcessLock readLock() ,public void readUnlockAndDecrementCount() ,public boolean readZero() ,public boolean realloc(long, int, int) ,public Bytes#RAW segmentBytesForRead() ,public Bytes#RAW segmentBytesForWrite() ,public abstract boolean segmentIndexInit() ,public abstract boolean segmentTierInit() ,public void setLocalLockState(net.openhft.chronicle.hash.impl.LocalLockState) ,public void setNestedContextsLockedOnSameSegment(boolean) ,public void setNextNode(net.openhft.chronicle.hash.impl.stage.entry.LocksInterface) ,public long size() ,public long tierCountersAreaAddr() ,public long tierDeleted() ,public void tierDeleted(long) ,public long tierEntries() ,public void tierEntries(long) ,public net.openhft.chronicle.hash.locks.InterProcessLock updateLock() ,public boolean updateZero() ,public void verifyTierCountersAreaData() ,public net.openhft.chronicle.hash.locks.InterProcessLock writeLock() ,public boolean writeZero() <variables>net.openhft.chronicle.hash.impl.stage.hash.Chaining chaining,public net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation checkOnEachPublicOperation,public int contextModCount,public long entrySpaceOffset,public final net.openhft.chronicle.algo.bitset.ReusableBitSet freeList,public VanillaChronicleHashHolder<?> hh,public net.openhft.chronicle.hash.impl.stage.entry.ReadLock innerReadLock,public net.openhft.chronicle.hash.impl.stage.entry.UpdateLock innerUpdateLock,public net.openhft.chronicle.hash.impl.stage.entry.WriteLock innerWriteLock,public int latestSameThreadSegmentModCount,net.openhft.chronicle.hash.impl.LocalLockState localLockState,public boolean nestedContextsLockedOnSameSegment,net.openhft.chronicle.hash.impl.stage.entry.LocksInterface nextNode,public net.openhft.chronicle.hash.impl.stage.entry.LocksInterface rootContextLockedOnThisSegment,public final net.openhft.chronicle.bytes.PointerBytesStore segmentBS,public final Bytes#RAW segmentBytes,public net.openhft.chronicle.hash.impl.SegmentHeader segmentHeader,public long segmentHeaderAddress,public int segmentIndex,public int tier,public long tierBaseAddr,public long tierIndex,int totalReadLockCount,int totalUpdateLockCount,int totalWriteLockCount
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/iter/SegmentsRecovery.java
SegmentsRecovery
zeroOutFirstSegmentTierCountersArea
class SegmentsRecovery implements IterationContext { @StageRef VanillaChronicleHashHolder<?> hh; @StageRef SegmentStages s; @StageRef TierRecovery tierRecovery; @Override public void recoverSegments( ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption) { throwExceptionIfClosed(); VanillaChronicleHash<?, ?, ?, ?> h = hh.h(); for (int segmentIndex = 0; segmentIndex < h.actualSegments; segmentIndex++) { s.initSegmentIndex(segmentIndex); resetSegmentLock(corruptionListener, corruption); zeroOutFirstSegmentTierCountersArea(corruptionListener, corruption); tierRecovery.recoverTier(segmentIndex, corruptionListener, corruption); } VanillaGlobalMutableState globalMutableState = h.globalMutableState(); long storedExtraTiersInUse = globalMutableState.getExtraTiersInUse(); long allocatedExtraTiers = globalMutableState.getAllocatedExtraTierBulks() * h.tiersInBulk; long expectedExtraTiersInUse = Math.max(0, Math.min(storedExtraTiersInUse, allocatedExtraTiers)); long actualExtraTiersInUse = 0; long firstFreeExtraTierIndex = -1; for (long extraTierIndex = 0; extraTierIndex < expectedExtraTiersInUse; extraTierIndex++) { long tierIndex = h.extraTierIndexToTierIndex(extraTierIndex); // `tier` is unused in recoverTier(), 0 should be a safe value s.initSegmentTier(0, tierIndex); int segmentIndex = tierRecovery.recoverTier( -1, corruptionListener, corruption); if (segmentIndex >= 0) { long tierCountersAreaAddr = s.tierCountersAreaAddr(); int storedSegmentIndex = TierCountersArea.segmentIndex(tierCountersAreaAddr); if (storedSegmentIndex != segmentIndex) { report(corruptionListener, corruption, segmentIndex, () -> format("wrong segment index stored in tier counters area " + "of tier with index {}: {}, should be, based on entries: {}", tierIndex, storedSegmentIndex, segmentIndex) ); TierCountersArea.segmentIndex(tierCountersAreaAddr, segmentIndex); } TierCountersArea.nextTierIndex(tierCountersAreaAddr, 0); s.initSegmentIndex(segmentIndex); s.goToLastTier(); s.nextTierIndex(tierIndex); TierCountersArea.prevTierIndex(tierCountersAreaAddr, s.tierIndex); TierCountersArea.tier(tierCountersAreaAddr, s.tier + 1); actualExtraTiersInUse = extraTierIndex + 1; } else { firstFreeExtraTierIndex = extraTierIndex; break; } } if (storedExtraTiersInUse != actualExtraTiersInUse) { long finalActualExtraTiersInUse = actualExtraTiersInUse; report(corruptionListener, corruption, -1, () -> format("wrong number of actual tiers in use in global mutable state, stored: {}, " + "should be: " + storedExtraTiersInUse, finalActualExtraTiersInUse) ); globalMutableState.setExtraTiersInUse(actualExtraTiersInUse); } long firstFreeTierIndex; if (firstFreeExtraTierIndex == -1) { if (allocatedExtraTiers > expectedExtraTiersInUse) { firstFreeTierIndex = h.extraTierIndexToTierIndex(expectedExtraTiersInUse); } else { firstFreeTierIndex = 0; } } else { firstFreeTierIndex = h.extraTierIndexToTierIndex(firstFreeExtraTierIndex); } if (firstFreeTierIndex > 0) { long lastTierIndex = h.extraTierIndexToTierIndex(allocatedExtraTiers - 1); h.linkAndZeroOutFreeTiers(firstFreeTierIndex, lastTierIndex); } long storedFirstFreeTierIndex = globalMutableState.getFirstFreeTierIndex(); if (storedFirstFreeTierIndex != firstFreeTierIndex) { report(corruptionListener, corruption, -1, () -> format("wrong first free tier index in global mutable state, stored: {}, " + "should be: " + storedFirstFreeTierIndex, firstFreeTierIndex) ); globalMutableState.setFirstFreeTierIndex(firstFreeTierIndex); } removeDuplicatesInSegments(corruptionListener, corruption); } private void removeDuplicatesInSegments( ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption) { VanillaChronicleHash<?, ?, ?, ?> h = hh.h(); for (int segmentIndex = 0; segmentIndex < h.actualSegments; segmentIndex++) { s.initSegmentIndex(segmentIndex); s.initSegmentTier(); s.goToLastTier(); while (true) { tierRecovery.removeDuplicatesInSegment(corruptionListener, corruption); if (s.tier > 0) { s.prevTier(); } else { break; } } } } private void resetSegmentLock( ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption) { long lockState = s.segmentHeader.getLockState(s.segmentHeaderAddress); if (lockState != s.segmentHeader.resetLockState()) { report(corruptionListener, corruption, s.segmentIndex, () -> format("lock of segment {} is not clear: {}", s.segmentIndex, s.segmentHeader.lockStateToString(lockState)) ); s.segmentHeader.resetLock(s.segmentHeaderAddress); } } private void zeroOutFirstSegmentTierCountersArea( ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption) {<FILL_FUNCTION_BODY>} }
s.nextTierIndex(0); if (s.prevTierIndex() != 0) { report(corruptionListener, corruption, s.segmentIndex, () -> format("stored prev tier index in first tier of segment {}: {}, should be 0", s.segmentIndex, s.prevTierIndex()) ); s.prevTierIndex(0); } long tierCountersAreaAddr = s.tierCountersAreaAddr(); if (TierCountersArea.segmentIndex(tierCountersAreaAddr) != 0) { report(corruptionListener, corruption, s.segmentIndex, () -> format("stored segment index in first tier of segment {}: {}, should be 0", s.segmentIndex, TierCountersArea.segmentIndex(tierCountersAreaAddr)) ); TierCountersArea.segmentIndex(tierCountersAreaAddr, 0); } if (TierCountersArea.tier(tierCountersAreaAddr) != 0) { report(corruptionListener, corruption, s.segmentIndex, () -> format("stored tier in first tier of segment {}: {}, should be 0", s.segmentIndex, TierCountersArea.tier(tierCountersAreaAddr)) ); TierCountersArea.tier(tierCountersAreaAddr, 0); }
1,660
364
2,024
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/query/HashQuery.java
HashQuery
tieredEntryPresent
class HashQuery<K> implements SetEntry<K> { @StageRef public VanillaChronicleHashHolder<K> hh; final DataAccess<K> innerInputKeyDataAccess = hh.h().keyDataAccess.copy(); @StageRef public SegmentStages s; @StageRef public HashEntryStages<K> entry; @StageRef public HashLookupSearch hashLookupSearch; @StageRef public CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef public HashLookupPos hlp; @StageRef public KeySearch<K> ks; /** * This stage exists for hooking {@link #innerInputKeyDataAccess} usage, to trigger {@link * DataAccess#uninit()} on context exit */ @Stage("InputKeyDataAccess") private boolean inputKeyDataAccessInitialized = false; @Stage("PresenceOfEntry") private EntryPresence entryPresence = null; void initInputKeyDataAccess() { inputKeyDataAccessInitialized = true; } void closeInputKeyDataAccess() { innerInputKeyDataAccess.uninit(); inputKeyDataAccessInitialized = false; } public DataAccess<K> inputKeyDataAccess() { initInputKeyDataAccess(); return innerInputKeyDataAccess; } public void dropSearchIfNestedContextsAndPresentHashLookupSlotCheckFailed() { if (s.locksInit()) { if (s.nestedContextsLockedOnSameSegment && s.rootContextLockedOnThisSegment.latestSameThreadSegmentModCount() != s.contextModCount) { if (ks.keySearchInit() && ks.searchStatePresent() && !hashLookupSearch.checkSlotContainsExpectedKeyAndValue(entry.pos)) { hlp.closeHashLookupPos(); } } } } public Data<K> queriedKey() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return ks.inputKey; } private void initPresenceOfEntry() { if (ks.searchStatePresent() || tieredEntryPresent()) { entryPresence = EntryPresence.PRESENT; } else { entryPresence = EntryPresence.ABSENT; } } public void initPresenceOfEntry(EntryPresence entryPresence) { this.entryPresence = entryPresence; } private boolean tieredEntryPresent() {<FILL_FUNCTION_BODY>} public boolean entryPresent() { return entryPresence == EntryPresence.PRESENT; } @Override public void doRemove() { checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerWriteLock.lock(); if (ks.searchStatePresent()) { entry.innerRemoveEntryExceptHashLookupUpdate(); hashLookupSearch.remove(); ks.setSearchState(ABSENT); initPresenceOfEntry(EntryPresence.ABSENT); } else { throw new IllegalStateException( hh.h().toIdentityString() + ": Entry is absent when doRemove() is called"); } } public enum EntryPresence {PRESENT, ABSENT} }
int firstTier = s.tier; long firstTierBaseAddr = s.tierBaseAddr; while (true) { if (s.hasNextTier()) { s.nextTier(); } else { if (s.tier != 0) s.initSegmentTier(); // loop to the root tier } if (s.tierBaseAddr == firstTierBaseAddr) break; if (ks.searchStatePresent()) return true; } // not found if (firstTier != 0) { // key is absent; probably are going to allocate a new entry; // start trying from the root tier s.initSegmentTier(); } return false;
849
194
1,043
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/query/KeySearch.java
KeySearch
initKeySearch
class KeySearch<K> { @StageRef public SegmentStages s; @StageRef public HashLookupSearch hashLookupSearch; @StageRef public HashEntryStages<K> entry; public Data<K> inputKey = null; @Stage("KeySearch") protected SearchState searchState = null; @StageRef VanillaChronicleMapHolder<?, ?, ?> mh; public abstract boolean inputKeyInit(); public void initInputKey(Data<K> inputKey) { this.inputKey = inputKey; } public abstract boolean keySearchInit(); @Stage("KeySearch") public void setSearchState(SearchState newSearchState) { this.searchState = newSearchState; } public void initKeySearch() {<FILL_FUNCTION_BODY>} boolean keyEquals(long keySize, long keyOffset) { return inputKey.size() == keySize && inputKey.equivalent(s.segmentBS, keyOffset); } public boolean searchStatePresent() { return searchState == PRESENT; } public boolean searchStateAbsent() { return searchState == ABSENT; } public enum SearchState { PRESENT, ABSENT } }
for (long pos; (pos = hashLookupSearch.nextPos()) >= 0L; ) { // otherwise we are inside iteration relocation. // During iteration, key search occurs when doReplaceValue() exhausts space in // the current segment, and insertion into the tiered segment requires to locate // an empty slot in the hashLookup. if (inputKeyInit()) { long keySizeOffset = s.entrySpaceOffset + pos * mh.m().chunkSize; Bytes segmentBytes = s.segmentBytesForRead(); segmentBytes.readPosition(keySizeOffset); long keySize = mh.h().keySizeMarshaller.readSize(segmentBytes); long keyOffset = segmentBytes.readPosition(); if (!keyEquals(keySize, keyOffset)) continue; hashLookupSearch.found(); entry.readFoundEntry(pos, keySizeOffset, keySize, keyOffset); searchState = PRESENT; return; } } searchState = SearchState.ABSENT;
343
259
602
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/query/QueryAlloc.java
QueryAlloc
alloc
class QueryAlloc implements Alloc { @StageRef public SegmentStages s; @Override public long alloc(int chunks, long prevPos, int prevChunks) {<FILL_FUNCTION_BODY>} }
long ret = s.allocReturnCode(chunks); if (prevPos >= 0) s.free(prevPos, prevChunks); if (ret >= 0) return ret; int alreadyAttemptedTier = s.tier; s.goToFirstTier(); while (true) { if (s.tier != alreadyAttemptedTier) { ret = s.allocReturnCode(chunks); if (ret >= 0) return ret; } s.nextTier(); }
63
142
205
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/stage/query/SearchAllocatedChunks.java
SearchAllocatedChunks
initEntryAndKey
class SearchAllocatedChunks extends AllocatedChunks { @StageRef KeySearch<?> ks; /** * @return {@code true} if tier has changed */ public boolean initEntryAndKey(long entrySize) {<FILL_FUNCTION_BODY>} }
initAllocatedChunks(hh.h().inChunks(entrySize)); int tierBeforeAllocation = s.tier; long pos = alloc.alloc(allocatedChunks, -1, 0); entry.writeNewEntry(pos, ks.inputKey); return s.tier != tierBeforeAllocation;
80
87
167
<methods>public non-sealed void <init>() ,public void initAllocatedChunks(int) ,public boolean initEntryAndKeyCopying(long, long, long, int) <variables>public net.openhft.chronicle.hash.impl.stage.entry.Alloc alloc,public int allocatedChunks,public HashEntryStages<?> entry,public VanillaChronicleHashHolder<?> hh,public net.openhft.chronicle.hash.impl.stage.entry.SegmentStages s
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/BuildVersion.java
BuildVersion
getVersionFromPom
class BuildVersion { private static String version = null; private BuildVersion() { } public static void main(String[] args) { System.out.println(version()); } /** * @return version of ChronicleMap being used, or NULL if its not known */ public synchronized static String version() { if (version != null) { return version; } try { // the best way to get the version is to read the map.version file InputStream resource = BuildVersion.class.getClassLoader().getResourceAsStream("map" + ".version"); BufferedReader in = new BufferedReader(new InputStreamReader(resource, StandardCharsets.UTF_8)); version = in.readLine().trim(); if (!"${project.version}".equals(version())) return version; return version; } catch (Exception e) { // do nothing } // another way to get the version is to read it from the manifest final String versionFromManifest = getVersionFromManifest(); if (versionFromManifest != null) { version = versionFromManifest; return version; } // as a fall back for development, we will read the version from the pom file version = getVersionFromPom(); return version; } /** * This should be used by everyone that has install chronicle map as a JAR * * @return gets the version out of the manifest, or null if it can not be read */ private static String getVersionFromManifest() { return ChronicleMapBuilder.class.getPackage().getImplementationVersion(); } /** * reads the pom file to get this version, only to be used for development or within the IDE. * * @return gets the version from the pom.xml */ private static String getVersionFromPom() {<FILL_FUNCTION_BODY>} }
final String absolutePath = new File(BuildVersion.class.getResource(BuildVersion.class .getSimpleName() + ".class").getPath()) .getParentFile().getParentFile().getParentFile().getParentFile().getParentFile() .getParentFile().getParentFile().getAbsolutePath(); final File file = new File(absolutePath + "/pom.xml"); try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) { final MavenXpp3Reader xpp3Reader = new MavenXpp3Reader(); Model model = xpp3Reader.read(reader); return model.getVersion(); } catch (NoClassDefFoundError e) { // if you want to get the version possibly in development add in to your pom // pax-url-aether.jar return null; } catch (Exception e) { return null; }
500
240
740
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/CharSequences.java
CharSequences
equivalent
class CharSequences { private CharSequences() { } public static boolean equivalent(@NotNull CharSequence a, @NotNull CharSequence b) {<FILL_FUNCTION_BODY>} public static int hash(@NotNull CharSequence cs) { if (cs instanceof String) return cs.hashCode(); int h = 0; for (int i = 0, len = cs.length(); i < len; i++) { h = 31 * h + cs.charAt(i); } return h; } }
if (a.equals(b)) return true; if (a instanceof String) return ((String) a).contentEquals(b); if (b instanceof String) return ((String) b).contentEquals(a); int len = a.length(); if (len != b.length()) return false; for (int i = 0; i < len; i++) { if (a.charAt(i) != b.charAt(i)) return false; } return true;
145
135
280
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/CleanerUtils.java
CleanerUtils
doClean
class CleanerUtils { private static final Method CREATE_METHOD; private static final Method CLEAN_METHOD; static { try { Class<?> cleanerClass = Class.forName(Jvm.isJava9Plus() ? "jdk.internal.ref.Cleaner" : "sun.misc.Cleaner"); CREATE_METHOD = cleanerClass.getDeclaredMethod("create", Object.class, Runnable.class); Jvm.setAccessible(CREATE_METHOD); CLEAN_METHOD = cleanerClass.getDeclaredMethod("clean"); Jvm.setAccessible(CLEAN_METHOD); } catch (ClassNotFoundException | NoSuchMethodException e) { Jvm.error().on(CleanerUtils.class, "Unable to initialise CleanerUtils", e); throw new RuntimeException(e); } } public static Cleaner createCleaner(Object ob, Runnable thunk) { try { Object cleanerInstance = CREATE_METHOD.invoke(null, ob, thunk); return () -> doClean(cleanerInstance); } catch (IllegalAccessException | InvocationTargetException e) { Jvm.error().on(CleanerUtils.class, "Unable to create cleaner", e); throw new RuntimeException(e); } } private static void doClean(Object cleanerInstance) {<FILL_FUNCTION_BODY>} }
try { CLEAN_METHOD.invoke(cleanerInstance); } catch (IllegalAccessException | InvocationTargetException e) { Jvm.warn().on(CleanerUtils.class, "Failed to clean buffer", e); }
355
64
419
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/FileIOUtils.java
FileIOUtils
readFully
class FileIOUtils { private FileIOUtils() { } public static void readFully(FileChannel fileChannel, long filePosition, ByteBuffer buffer) throws IOException {<FILL_FUNCTION_BODY>} public static void writeFully(FileChannel fileChannel, long filePosition, ByteBuffer buffer) throws IOException { int startBufferPosition = buffer.position(); while (buffer.remaining() > 0) { fileChannel.write(buffer, filePosition + buffer.position() - startBufferPosition); } } }
int startBufferPosition = buffer.position(); while (buffer.remaining() > 0 && buffer.position() < fileChannel.size()) { int bytesRead = fileChannel.read(buffer, filePosition + buffer.position() - startBufferPosition); if (bytesRead == -1) break; }
139
83
222
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/Objects.java
Objects
builderEquals
class Objects { private Objects() { } public static int hash(Object... values) { return Arrays.hashCode(values); } public static boolean equal(@Nullable Object a, @Nullable Object b) { return a != null ? a.equals(b) : b == null; } public static boolean builderEquals(@NotNull Object builder, @Nullable Object o) {<FILL_FUNCTION_BODY>} public static void requireNonNull(Object obj) { if (obj == null) throw new NullPointerException(); } }
return builder == o || o != null && builder.getClass() == o.getClass() && builder.toString().equals(o.toString());
148
41
189
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/Throwables.java
Throwables
returnOrSuppress
class Throwables { private Throwables() { } public static RuntimeException propagate(Throwable t) { // Avoid calling Objects.requireNonNull(), StackOverflowError-sensitive if (t == null) throw new NullPointerException(); if (t instanceof Error) throw (Error) t; if (t instanceof RuntimeException) throw (RuntimeException) t; throw new RuntimeException(t); } public static <T extends Throwable> T propagateNotWrapping( Throwable t, Class<T> notWrappingThrowableType) throws T { Objects.requireNonNull(t); Objects.requireNonNull(notWrappingThrowableType); if (t instanceof Error) throw (Error) t; if (t instanceof RuntimeException) throw (RuntimeException) t; if (notWrappingThrowableType.isInstance(t)) throw notWrappingThrowableType.cast(t); throw new RuntimeException(t); } public static Throwable returnOrSuppress(Throwable thrown, Throwable t) {<FILL_FUNCTION_BODY>} }
if (thrown == null) { return t; } else { if (t != null) thrown.addSuppressed(t); return thrown; }
295
50
345
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/jna/PosixFallocate.java
PosixFallocate
fallocate
class PosixFallocate { private PosixFallocate() { } public static void fallocate(FileDescriptor descriptor, long offset, long length) throws IOException {<FILL_FUNCTION_BODY>} private static int getNativeFileDescriptor(FileDescriptor descriptor) throws IOException { try { final Field field = descriptor.getClass().getDeclaredField("fd"); Jvm.setAccessible(field); return (int) field.get(descriptor); } catch (final Exception e) { throw new IOException("unsupported FileDescriptor implementation", e); } } }
int fd = getNativeFileDescriptor(descriptor); if (fd != -1) { int ret = PosixAPI.posix().fallocate(getNativeFileDescriptor(descriptor), 0, offset, length); if (ret != 0) { throw new IOException("posix_fallocate() returned " + ret); } }
153
93
246
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/math/ContinuedFraction.java
ContinuedFraction
evaluate
class ContinuedFraction { /** * Access the n-th a coefficient of the continued fraction. Since a can be * a function of the evaluation point, x, that is passed in as well. * * @param n the coefficient index to retrieve. * @param x the evaluation point. * @return the n-th a coefficient. */ protected abstract double getA(int n, double x); /** * Access the n-th b coefficient of the continued fraction. Since b can be * a function of the evaluation point, x, that is passed in as well. * * @param n the coefficient index to retrieve. * @param x the evaluation point. * @return the n-th b coefficient. */ protected abstract double getB(int n, double x); /** * Evaluates the continued fraction at the value x. * <p> * The implementation of this method is based on the modified Lentz algorithm as described * on page 18 ff. in: * <ul> * <li> * I. J. Thompson, A. R. Barnett. "Coulomb and Bessel Functions of Complex Arguments and Order." * <a target="_blank" href="http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf"> * http://www.fresco.org.uk/papers/Thompson-JCP64p490.pdf</a> * </li> * </ul> * <b>Note:</b> the implementation uses the terms a<sub>i</sub> and b<sub>i</sub> as defined in * <a href="http://mathworld.wolfram.com/ContinuedFraction.html">Continued Fraction @ MathWorld</a>. * * @param x the evaluation point. * @param epsilon maximum error allowed. * @param maxIterations maximum number of convergents * @return the value of the continued fraction evaluated at x. * @throws IllegalStateException if the algorithm fails to converge. * @throws IllegalStateException if maximal number of iterations is reached */ public double evaluate(double x, double epsilon, int maxIterations) {<FILL_FUNCTION_BODY>} }
final double small = 1e-50; double hPrev = getA(0, x); // use the value of small as epsilon criteria for zero checks if (Precision.isEquals(hPrev, 0.0, small)) { hPrev = small; } int n = 1; double dPrev = 0.0; double cPrev = hPrev; double hN = hPrev; while (n < maxIterations) { final double a = getA(n, x); final double b = getB(n, x); double dN = a + b * dPrev; if (Precision.isEquals(dN, 0.0, small)) { dN = small; } double cN = a + b / cPrev; if (Precision.isEquals(cN, 0.0, small)) { cN = small; } dN = 1 / dN; final double deltaN = cN * dN; hN = hPrev * deltaN; if (Double.isInfinite(hN)) { throw new IllegalStateException( "Continued fraction convergents diverged to +/- infinity for value " + x); } if (Double.isNaN(hN)) { throw new IllegalStateException( "Continued fraction diverged to NaN for value " + x); } if (Math.abs(deltaN - 1.0) < epsilon) { break; } dPrev = dN; cPrev = cN; hPrev = hN; n++; } if (n >= maxIterations) { throw new IllegalStateException( "Continued fraction convergents failed to converge (in less than " + maxIterations + " iterations) for value " + x); } return hN;
599
491
1,090
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/math/PoissonDistribution.java
PoissonDistribution
solveInverseCumulativeProbability
class PoissonDistribution { private static final double EPSILON = 1e-12; /** * Poisson distribution is used to estimate segment fillings. Segments are not bound with * Integer.MAX_VALUE, but it's not clear if algorithms from Commons Math could work with such * big values as Long.MAX_VALUES */ private static final long UPPER_BOUND = 1L << 36; private static final int MAX_ITERATIONS = 10000000; public static double cumulativeProbability(double mean, long x) { if (x < 0) { return 0; } if (x >= UPPER_BOUND) { return 1; } return Gamma.regularizedGammaQ((double) x + 1, mean, EPSILON, MAX_ITERATIONS); } public static long inverseCumulativeProbability(double mean, double p) { checkProbability(p); long lower = 0; if (p == 0.0) { return lower; } lower -= 1; // this ensures cumulativeProbability(lower) < p, which // is important for the solving step long upper = UPPER_BOUND; if (p == 1.0) { return upper; } // use the one-sided Chebyshev inequality to narrow the bracket // cf. AbstractRealDistribution.inverseCumulativeProbability(double) final double mu = mean; // in Poisson distribution, variance == mean double variance = mean; final double sigma = Math.sqrt(variance); final boolean chebyshevApplies = !(Double.isInfinite(mu) || Double.isNaN(mu) || Double.isInfinite(sigma) || Double.isNaN(sigma) || sigma == 0.0); if (chebyshevApplies) { double k = Math.sqrt((1.0 - p) / p); double tmp = mu - k * sigma; if (tmp > lower) { lower = ((int) Math.ceil(tmp)) - 1L; } k = 1.0 / k; tmp = mu + k * sigma; if (tmp < upper) { upper = ((int) Math.ceil(tmp)) - 1L; } } return solveInverseCumulativeProbability(mean, p, lower, upper); } private static void checkProbability(double p) { if (p < 0.0 || p > 1.0) { throw new IllegalArgumentException("probability should be in [0.0, 1.0] bounds, " + p + " given"); } } /** * This is a utility function used by {@link * #inverseCumulativeProbability(double, double)}. It assumes {@code 0 < p < 1} and * that the inverse cumulative probability lies in the bracket {@code * (lower, upper]}. The implementation does simple bisection to find the * smallest {@code p}-quantile {@code inf{x in Z | P(X<=x) >= p}}. * * @param p the cumulative probability * @param lower a value satisfying {@code cumulativeProbability(lower) < p} * @param upper a value satisfying {@code p <= cumulativeProbability(upper)} * @return the smallest {@code p}-quantile of this distribution */ private static long solveInverseCumulativeProbability(double mean, final double p, long lower, long upper) {<FILL_FUNCTION_BODY>} public static double meanByCumulativeProbabilityAndValue(double p, long x, double precision) { checkProbability(p); assert x > 0 && x < UPPER_BOUND; double lower = 0; double upper = UPPER_BOUND; while (lower + precision < upper) { double m = (lower + upper) / 2; double pm = checkedCumulativeProbability(m, x); if (pm < p) { upper = m; } else { lower = m; } } return lower; } /** * Computes the cumulative probability function and checks for {@code NaN} * values returned. Throws {@code MathInternalError} if the value is * {@code NaN}. Rethrows any exception encountered evaluating the cumulative * probability function. Throws {@code MathInternalError} if the cumulative * probability function returns {@code NaN}. * * @param argument input value * @return the cumulative probability * @throws AssertionError if the cumulative probability is {@code NaN} */ private static double checkedCumulativeProbability(double mean, long argument) { double result = cumulativeProbability(mean, argument); if (Double.isNaN(result)) { throw new AssertionError("Discrete cumulative probability function returned NaN " + "for argument " + argument); } return result; } }
while (lower + 1 < upper) { long xm = (lower + upper) / 2; if (xm < lower || xm > upper) { /* * Overflow. * There will never be an overflow in both calculation methods * for xm at the same time */ xm = lower + (upper - lower) / 2; } double pm = checkedCumulativeProbability(mean, xm); if (pm >= p) { upper = xm; } else { lower = xm; } } return upper;
1,285
156
1,441
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/impl/util/math/Precision.java
Precision
isEquals
class Precision { /** * Offset to order signed double numbers lexicographically. */ private static final long SGN_MASK = 0x8000000000000000L; /** * Positive zero bits. */ private static final long POSITIVE_ZERO_DOUBLE_BITS = Double.doubleToRawLongBits(+0.0); /** * Negative zero bits. */ private static final long NEGATIVE_ZERO_DOUBLE_BITS = Double.doubleToRawLongBits(-0.0); /** * Returns {@code true} if there is no double value strictly between the * arguments or the difference between them is within the range of allowed * error (inclusive). * * @param x First value. * @param y Second value. * @param eps Amount of allowed absolute error. * @return {@code true} if the values are two adjacent floating point * numbers or they are within range of each other. */ public static boolean isEquals(double x, double y, double eps) { return isEquals(x, y, 1) || Math.abs(y - x) <= eps; } /** * Returns {@code true} if both arguments are equal or within the range of allowed * error (inclusive). * <p> * Two float numbers are considered equal if there are {@code (maxUlps - 1)} * (or fewer) floating point numbers between them, i.e. two adjacent * floating point numbers are considered equal. * <p> * Adapted from <a * href="http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/"> * Bruce Dawson</a> * * @param x first value * @param y second value * @param maxUlps {@code (maxUlps - 1)} is the number of floating point * values between {@code x} and {@code y}. * @return {@code true} if there are fewer than {@code maxUlps} floating * point values between {@code x} and {@code y}. */ public static boolean isEquals(final double x, final double y, final int maxUlps) {<FILL_FUNCTION_BODY>} }
final long xInt = Double.doubleToRawLongBits(x); final long yInt = Double.doubleToRawLongBits(y); final boolean isEqual; if (((xInt ^ yInt) & SGN_MASK) == 0L) { // number have same sign, there is no risk of overflow isEqual = Math.abs(xInt - yInt) <= maxUlps; } else { // number have opposite signs, take care of overflow final long deltaPlus; final long deltaMinus; if (xInt < yInt) { deltaPlus = yInt - POSITIVE_ZERO_DOUBLE_BITS; deltaMinus = xInt - NEGATIVE_ZERO_DOUBLE_BITS; } else { deltaPlus = xInt - POSITIVE_ZERO_DOUBLE_BITS; deltaMinus = yInt - NEGATIVE_ZERO_DOUBLE_BITS; } if (deltaPlus > maxUlps) { isEqual = false; } else { isEqual = deltaMinus <= (maxUlps - deltaPlus); } } return isEqual && !Double.isNaN(x) && !Double.isNaN(y);
624
332
956
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/replication/DefaultEventualConsistencyStrategy.java
DefaultEventualConsistencyStrategy
decideOnRemoteModification
class DefaultEventualConsistencyStrategy { private DefaultEventualConsistencyStrategy() { } /** * Returns the acceptance decision, should be made about the modification operation in the * given {@code context}, aiming to modify the given {@code entry}. This method doesn't do any * changes to {@code entry} nor {@code context} state. {@link MapRemoteOperations} and * {@link SetRemoteOperations} method implementations should guide the result of calling this * method to do something to <i>actually</i> apply the remote operation. * * @param entry the entry to be modified * @param context the remote operation context * @return if the remote operation should be accepted or discarded */ public static AcceptanceDecision decideOnRemoteModification( ReplicableEntry entry, RemoteOperationContext<?> context) {<FILL_FUNCTION_BODY>} /** * Decision, if {@link MapRemoteOperations remote modification operation} should be accepted * or discarded. Used in {@link DefaultEventualConsistencyStrategy}. */ public enum AcceptanceDecision { /** * Acceptance decision -- the remote modification operation is applied to the local * {@link ChronicleHash} state. */ ACCEPT, /** * Discard decision -- the remote modification operation is rejected. */ DISCARD } }
long remoteTimestamp = context.remoteTimestamp(); long originTimestamp = entry.originTimestamp(); // Last write wins if (remoteTimestamp > originTimestamp) return ACCEPT; if (remoteTimestamp < originTimestamp) return DISCARD; // remoteTimestamp == originTimestamp below byte remoteIdentifier = context.remoteIdentifier(); byte originIdentifier = entry.originIdentifier(); // Lower identifier wins if (remoteIdentifier < originIdentifier) return ACCEPT; if (remoteIdentifier > originIdentifier) return DISCARD; // remoteTimestamp == originTimestamp && remoteIdentifier == originIdentifier below // This could be, only if a node with the origin identifier was lost, a new Chronicle Hash // instance was started up, but with system time which for some reason is very late, so // that it provides the same time, as the "old" node with this identifier, before it was // lost. (This is almost a theoretical situation.) In this case, give advantage to fresh // entry updates to the "new" node. Entries with the same id and timestamp, bootstrapped // "back" from other nodes in the system, are discarded on this new node (this is the // of the condition originIdentifier == currentNodeIdentifier). But those new updates // should win on other nodes. // // Another case, in which we could have remoteTimestamp == originTimestamp && // remoteIdentifier == originIdentifier, is replication of barely the same entry, if an // entry is bootstrapped "back" from remote node to it's origin node. In this case the // following condition also plays right (the update is discarded, due to it's redundancy). return originIdentifier == context.currentNodeIdentifier() ? DISCARD : ACCEPT;
351
418
769
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/replication/TimeProvider.java
TimeProvider
systemTimeIntervalBetween
class TimeProvider { private static final AtomicLong lastTimeHolder = new AtomicLong(); private static LongSupplier millisecondSupplier = System::currentTimeMillis; private TimeProvider() { } /** * Returns a non-decreasing number, assumed to be used as a "timestamp". * <p> * Approximate system time interval between two calls of this method is retrievable via * {@link #systemTimeIntervalBetween(long, long, TimeUnit)}, applied to the returned values * from those {@code currentTime()} calls. * <p> * Safe and scalable for concurrent use from multiple threads. * * @return the current timestamp */ public static long currentTime() { long now = MILLISECONDS.toNanos(millisecondSupplier.getAsLong()); while (true) { long lastTime = lastTimeHolder.get(); if (now <= lastTime) return lastTime; if (lastTimeHolder.compareAndSet(lastTime, now)) return now; Jvm.nanoPause(); } } /** * Returns system time interval (i. e. wall time interval) between two time values, taken using * {@link #currentTime()} method, with the highest possible precision, in the given time units. * * @param earlierTime {@link #currentTime()} result, taken at some moment in the past (earlier) * @param laterTime {@link #currentTime()} result, taken at some moment in the past, but later * than {@code earlierTime} was taken ("later" means there is a happens-before relationship * between the two subject {@code currentTime()} calls) * @param systemTimeIntervalUnit the time units to return system time interval in * @return wall time interval between the specified moments in the given time unit */ public static long systemTimeIntervalBetween( long earlierTime, long laterTime, TimeUnit systemTimeIntervalUnit) {<FILL_FUNCTION_BODY>} static void overrideMillisecondSupplier(final LongSupplier millisecondSupplier) { TimeProvider.millisecondSupplier = millisecondSupplier; } }
long intervalNanos = laterTime - earlierTime; return systemTimeIntervalUnit.convert(intervalNanos, NANOSECONDS);
549
39
588
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/ListMarshaller.java
ListMarshaller
read
class ListMarshaller<T> implements BytesReader<List<T>>, BytesWriter<List<T>>, StatefulCopyable<ListMarshaller<T>> { // Config fields private BytesReader<T> elementReader; private BytesWriter<? super T> elementWriter; /** * Constructs a {@code ListMarshaller} with the given list elements' serializers. * <p> * Use static factory {@link #of(BytesReader, BytesWriter)} instead of this constructor * directly. * * @param elementReader list elements' reader * @param elementWriter list elements' writer */ public ListMarshaller(BytesReader<T> elementReader, BytesWriter<? super T> elementWriter) { this.elementReader = elementReader; this.elementWriter = elementWriter; } /** * Returns a {@code ListMarshaller} which uses the given list elements' serializers. * * @param elementReader list elements' reader * @param elementWriter list elements' writer * @param <T> type of list elements * @return a {@code ListMarshaller} which uses the given list elements' serializers */ public static <T> ListMarshaller<T> of( BytesReader<T> elementReader, BytesWriter<? super T> elementWriter) { return new ListMarshaller<>(elementReader, elementWriter); } /** * Returns a {@code ListMarshaller} which uses the given marshaller as both reader and writer of * list elements. Example: <pre><code> * ChronicleMap * .of(String.class,{@code (Class<List<Integer>>)} ((Class) List.class)) * .valueMarshaller(ListMarshaller.of(IntegerMarshaller.INSTANCE)) * ...</code></pre> * * @param elementMarshaller list elements' marshaller * @param <T> type of list elements * @param <M> type of list elements' marshaller * @return a {@code ListMarshaller} which uses the given list elements' marshaller */ public static <T, M extends BytesReader<T> & BytesWriter<? super T>> ListMarshaller<T> of( M elementMarshaller) { return of(elementMarshaller, elementMarshaller); } @NotNull @Override public List<T> read(Bytes in, @Nullable List<T> using) {<FILL_FUNCTION_BODY>} @Override public void write(Bytes out, @NotNull List<T> toWrite) { out.writeInt(toWrite.size()); // indexed loop to avoid garbage creation //noinspection ForLoopReplaceableByForEach for (int i = 0; i < toWrite.size(); i++) { elementWriter.write(out, toWrite.get(i)); } } @Override public ListMarshaller<T> copy() { if (elementReader instanceof StatefulCopyable || elementWriter instanceof StatefulCopyable) { return new ListMarshaller<>(copyIfNeeded(elementReader), copyIfNeeded(elementWriter)); } else { return this; } } @Override public void readMarshallable(@NotNull WireIn wireIn) { elementReader = wireIn.read(() -> "elementReader").typedMarshallable(); elementWriter = wireIn.read(() -> "elementWriter").typedMarshallable(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write(() -> "elementReader").typedMarshallable(elementReader); wireOut.write(() -> "elementWriter").typedMarshallable(elementWriter); } }
int size = in.readInt(); if (using == null) { using = new ArrayList<>(size); for (int i = 0; i < size; i++) { using.add(null); } } else if (using.size() < size) { while (using.size() < size) { using.add(null); } } else if (using.size() > size) { using.subList(size, using.size()).clear(); } for (int i = 0; i < size; i++) { using.set(i, elementReader.read(in, using.get(i))); } return using;
970
175
1,145
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/MapMarshaller.java
MapMarshaller
read
class MapMarshaller<K, V> implements BytesReader<Map<K, V>>, BytesWriter<Map<K, V>>, StatefulCopyable<MapMarshaller<K, V>> { // Config fields private BytesReader<K> keyReader; private BytesWriter<? super K> keyWriter; private BytesReader<V> valueReader; private BytesWriter<? super V> valueWriter; // Cache fields private transient Deque<K> orderedKeys; private transient Deque<V> orderedValues; /** * Constructs a {@code MapMarshaller} with the given map keys' and values' serializers. * * @param keyReader map keys' reader * @param keyWriter map keys' writer * @param valueReader map values' reader * @param valueWriter map values' writer */ public MapMarshaller( BytesReader<K> keyReader, BytesWriter<? super K> keyWriter, BytesReader<V> valueReader, BytesWriter<? super V> valueWriter) { this.keyReader = keyReader; this.keyWriter = keyWriter; this.valueReader = valueReader; this.valueWriter = valueWriter; initTransients(); } private void initTransients() { orderedKeys = new ArrayDeque<>(); orderedValues = new ArrayDeque<>(); } @NotNull @Override public Map<K, V> read(Bytes in, @Nullable Map<K, V> using) {<FILL_FUNCTION_BODY>} @Override public void write(Bytes out, @NotNull Map<K, V> toWrite) { out.writeInt(toWrite.size()); toWrite.forEach((k, v) -> { keyWriter.write(out, k); valueWriter.write(out, v); }); } @Override public MapMarshaller<K, V> copy() { return new MapMarshaller<>(copyIfNeeded(keyReader), copyIfNeeded(keyWriter), copyIfNeeded(valueReader), copyIfNeeded(valueWriter)); } @Override public void readMarshallable(@NotNull WireIn wireIn) { keyReader = wireIn.read(() -> "keyReader").typedMarshallable(); keyWriter = wireIn.read(() -> "keyWriter").typedMarshallable(); valueReader = wireIn.read(() -> "valueReader").typedMarshallable(); valueWriter = wireIn.read(() -> "valueWriter").typedMarshallable(); initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write(() -> "keyReader").typedMarshallable(keyReader); wireOut.write(() -> "keyWriter").typedMarshallable(keyWriter); wireOut.write(() -> "valueReader").typedMarshallable(valueReader); wireOut.write(() -> "valueWriter").typedMarshallable(valueWriter); } }
int size = in.readInt(); if (using == null) { using = new HashMap<>(((int) (size / 0.75))); for (int i = 0; i < size; i++) { using.put(keyReader.read(in, null), valueReader.read(in, null)); } } else { using.forEach((k, v) -> { orderedKeys.add(k); orderedValues.add(v); }); using.clear(); for (int i = 0; i < size; i++) { using.put(keyReader.read(in, orderedKeys.pollFirst()), valueReader.read(in, orderedValues.pollFirst())); } orderedKeys.clear(); // for GC, avoid zombie object links orderedValues.clear(); } return using;
780
220
1,000
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/SetMarshaller.java
SetMarshaller
read
class SetMarshaller<T> implements BytesReader<Set<T>>, BytesWriter<Set<T>>, StatefulCopyable<SetMarshaller<T>> { // Config fields private BytesReader<T> elementReader; private BytesWriter<? super T> elementWriter; /** * Cache field */ private transient Deque<T> orderedElements; /** * Constructs a {@code SetMarshaller} with the given set elements' serializers. * <p> * Use static factory {@link #of(BytesReader, BytesWriter)} instead of this constructor * directly. * * @param elementReader set elements' reader * @param elementWriter set elements' writer */ public SetMarshaller(BytesReader<T> elementReader, BytesWriter<? super T> elementWriter) { this.elementReader = elementReader; this.elementWriter = elementWriter; initTransients(); } /** * Returns a {@code SetMarshaller} which uses the given set elements' serializers. * * @param elementReader set elements' reader * @param elementWriter set elements' writer * @param <T> type of set elements * @return a {@code SetMarshaller} which uses the given set elements' serializers */ public static <T> SetMarshaller<T> of( BytesReader<T> elementReader, BytesWriter<? super T> elementWriter) { return new SetMarshaller<>(elementReader, elementWriter); } /** * Returns a {@code SetMarshaller} which uses the given marshaller as both reader and writer of * set elements. Example: <pre><code> * ChronicleMap * .of(String.class,{@code (Class<Set<Integer>>)} ((Class) Set.class)) * .valueMarshaller(SetMarshaller.of(IntegerMarshaller.INSTANCE)) * ...</code></pre> * * @param elementMarshaller set elements' marshaller * @param <T> type of set elements * @param <M> type of set elements' marshaller * @return a {@code SetMarshaller} which uses the given set elements' marshaller */ public static <T, M extends BytesReader<T> & BytesWriter<? super T>> SetMarshaller<T> of( M elementMarshaller) { return of(elementMarshaller, elementMarshaller); } private void initTransients() { orderedElements = new ArrayDeque<>(); } @NotNull @Override public Set<T> read(Bytes in, @Nullable Set<T> using) {<FILL_FUNCTION_BODY>} @Override public void write(Bytes out, @NotNull Set<T> toWrite) { out.writeInt(toWrite.size()); toWrite.forEach(e -> elementWriter.write(out, e)); } @Override public SetMarshaller<T> copy() { return new SetMarshaller<>(copyIfNeeded(elementReader), copyIfNeeded(elementWriter)); } @Override public void readMarshallable(@NotNull WireIn wireIn) { elementReader = wireIn.read(() -> "elementReader").typedMarshallable(); elementWriter = wireIn.read(() -> "elementWriter").typedMarshallable(); initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write(() -> "elementReader").typedMarshallable(elementReader); wireOut.write(() -> "elementWriter").typedMarshallable(elementWriter); } }
int size = in.readInt(); if (using == null) { using = new HashSet<>((int) (size / 0.75)); for (int i = 0; i < size; i++) { using.add(elementReader.read(in, null)); } } else { orderedElements.addAll(using); using.clear(); for (int i = 0; i < size; i++) { using.add(elementReader.read(in, orderedElements.pollFirst())); } orderedElements.clear(); // for GC, avoid zombie object links } return using;
952
163
1,115
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ByteArrayDataAccess.java
ByteArrayDataAccess
getUsing
class ByteArrayDataAccess extends AbstractData<byte[]> implements DataAccess<byte[]> { /** * Cache field */ private transient BytesStore<?, ?> bs; /** * State field */ private transient byte[] array; public ByteArrayDataAccess() { initTransients(); } private void initTransients() { bs = null; } @Override public RandomDataInput bytes() { return bs; } @Override public long offset() { return bs.start(); } @Override public long size() { return bs.capacity(); } @Override public byte[] get() { return array; } @Override public byte[] getUsing(@Nullable byte[] using) {<FILL_FUNCTION_BODY>} @Override public Data<byte[]> getData(@NotNull byte[] instance) { array = instance; bs = BytesStore.wrap(array); return this; } @Override public void uninit() { array = null; bs = null; } @Override public DataAccess<byte[]> copy() { return new ByteArrayDataAccess(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no fields to write } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no fields to read initTransients(); } @Override public String toString() { return new String(array, StandardCharsets.UTF_8); } }
if (using == null || using.length != array.length) using = new byte[array.length]; System.arraycopy(array, 0, using, 0, array.length); return using;
439
57
496
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ByteArraySizedReader.java
ByteArraySizedReader
read
class ByteArraySizedReader implements SizedReader<byte[]>, EnumMarshallable<ByteArraySizedReader> { public static final ByteArraySizedReader INSTANCE = new ByteArraySizedReader(); private ByteArraySizedReader() { } @NotNull @Override public byte[] read(@NotNull Bytes in, long size, @Nullable byte[] using) {<FILL_FUNCTION_BODY>} @NotNull @Override public ByteArraySizedReader readResolve() { return INSTANCE; } }
if (size < 0L || size > (long) Integer.MAX_VALUE) { throw new IORuntimeException("byte[] size should be non-negative int, " + size + " given. Memory corruption?"); } int arrayLength = (int) size; if (using == null || arrayLength != using.length) using = new byte[arrayLength]; in.read(using); return using;
144
109
253
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ByteBufferDataAccess.java
ByteBufferDataAccess
getData
class ByteBufferDataAccess extends AbstractData<ByteBuffer> implements DataAccess<ByteBuffer> { // Cache fields private transient VanillaBytes<Void> bytes; // State fields private transient ByteBuffer bb; private transient BytesStore bytesStore; public ByteBufferDataAccess() { initTransients(); } private void initTransients() { bytes = VanillaBytes.vanillaBytes(); } @Override public RandomDataInput bytes() { return bytesStore; } @Override public long offset() { return bb.position(); } @Override public long size() { return bb.remaining(); } @Override public ByteBuffer get() { return bb; } @Override public ByteBuffer getUsing(@Nullable ByteBuffer using) { if (using == null || using.capacity() < bb.remaining()) { using = ByteBuffer.allocate(bb.remaining()); } else { using.position(0); using.limit(bb.remaining()); } bytes.bytesStore(bytesStore, bb.position(), bb.remaining()); bytes.read(using); using.flip(); return using; } @Override public Data<ByteBuffer> getData(@NotNull ByteBuffer instance) {<FILL_FUNCTION_BODY>} @Override public void uninit() { bb = null; bytesStore.release(ReferenceOwner.INIT); bytesStore = null; } @Override public DataAccess<ByteBuffer> copy() { return new ByteBufferDataAccess(); } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no fields to read initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no fields to write } }
bb = instance; ByteOrder originalOrder = instance.order(); bytesStore = BytesStore.follow(instance); instance.order(originalOrder); return this;
507
48
555
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ByteBufferSizedReader.java
ByteBufferSizedReader
read
class ByteBufferSizedReader implements SizedReader<ByteBuffer>, EnumMarshallable<ByteBufferSizedReader> { public static final ByteBufferSizedReader INSTANCE = new ByteBufferSizedReader(); private ByteBufferSizedReader() { } @NotNull @Override public ByteBuffer read(@NotNull Bytes in, long size, @Nullable ByteBuffer using) {<FILL_FUNCTION_BODY>} @NotNull @Override public ByteBufferSizedReader readResolve() { return INSTANCE; } }
if (size < 0L || size > (long) Integer.MAX_VALUE) throw new IllegalArgumentException("ByteBuffer size should be non-negative int, " + size + " given. Memory corruption?"); int bufferCap = (int) size; if (using == null || using.capacity() < bufferCap) { using = ByteBuffer.allocate(bufferCap); } else { using.position(0); using.limit(bufferCap); } in.read(using); using.flip(); return using;
143
140
283
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ByteableDataAccess.java
ByteableDataAccess
getUsing
class ByteableDataAccess<T extends Byteable> extends InstanceCreatingMarshaller<T> implements DataAccess<T>, Data<T> { /** * State field */ private transient T instance; public ByteableDataAccess(Type tClass) { super(tClass); } @Override public RandomDataInput bytes() { return instance.bytesStore(); } @Override public long offset() { return instance.offset(); } @Override public long size() { return instance.maxSize(); } @Override public T get() { return instance; } @Override public T getUsing(@Nullable T using) {<FILL_FUNCTION_BODY>} @Override public int hashCode() { return dataHashCode(); } @Override public boolean equals(Object obj) { return dataEquals(obj); } @Override public String toString() { return get().toString(); } @Override public Data<T> getData(@NotNull T instance) { this.instance = instance; return this; } @Override public void uninit() { instance = null; } @Override public DataAccess<T> copy() { return new ByteableDataAccess<>(tType()); } }
if (using == null) using = createInstance(); using.bytesStore(instance.bytesStore(), offset(), size()); return using;
363
39
402
<methods>public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private java.lang.reflect.Type tClass
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ByteableSizedReader.java
ByteableSizedReader
read
class ByteableSizedReader<T extends Byteable> extends InstanceCreatingMarshaller<T> implements SizedReader<T> { public ByteableSizedReader(Class<T> tClass) { super(tClass); } @NotNull @Override public final T read(@NotNull Bytes in, long size, @Nullable T using) {<FILL_FUNCTION_BODY>} }
if (using == null) using = createInstance(); using.bytesStore(in.bytesStore(), in.readPosition(), size); return using;
106
42
148
<methods>public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private java.lang.reflect.Type tClass
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/BytesAsSizedReader.java
BytesAsSizedReader
copy
class BytesAsSizedReader<T> implements SizedReader<T>, StatefulCopyable<BytesAsSizedReader<T>> { /** * Config field */ private BytesReader<T> reader; public BytesAsSizedReader(BytesReader<T> reader) { this.reader = reader; } @NotNull @Override public T read(Bytes in, long size, @Nullable T using) { return reader.read(in, using); } @Override public BytesAsSizedReader<T> copy() {<FILL_FUNCTION_BODY>} @Override public void readMarshallable(@NotNull WireIn wireIn) { reader = wireIn.read(() -> "reader").typedMarshallable(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write(() -> "reader").typedMarshallable(reader); } }
if (reader instanceof StatefulCopyable) { return new BytesAsSizedReader<>(StatefulCopyable.copyIfNeeded(reader)); } else { return this; }
251
52
303
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/BytesMarshallableDataAccess.java
BytesMarshallableDataAccess
initBytes
class BytesMarshallableDataAccess<T extends BytesMarshallable> extends InstanceCreatingMarshaller<T> implements DataAccess<T>, Data<T> { // Cache fields private transient boolean bytesInit; private transient Bytes bytes; private transient VanillaBytes targetBytes; /** * State field */ private transient T instance; public BytesMarshallableDataAccess(Class<T> tClass) { this(tClass, DEFAULT_BYTES_CAPACITY); } private BytesMarshallableDataAccess(Type tClass, long bytesCapacity) { super(tClass); initTransients(bytesCapacity); } @Override public void readMarshallable(@NotNull WireIn wireIn) { super.readMarshallable(wireIn); initTransients(DEFAULT_BYTES_CAPACITY); } private void initTransients(long bytesCapacity) { bytes = DefaultElasticBytes.allocateDefaultElasticBytes(bytesCapacity); targetBytes = VanillaBytes.vanillaBytes(); } @Override public RandomDataInput bytes() { initBytes(); return bytes.bytesStore(); } private void initBytes() {<FILL_FUNCTION_BODY>} @Override public long offset() { return 0; } @Override public long size() { initBytes(); return bytes.readRemaining(); } @Override public void writeTo(RandomDataOutput target, long targetOffset) { if (bytesInit) { target.write(targetOffset, bytes(), offset(), size()); } else { targetBytes.bytesStore((BytesStore) target, targetOffset, target.capacity() - targetOffset); targetBytes.writePosition(targetOffset); instance.writeMarshallable(targetBytes); targetBytes.bytesStore(BytesStore.empty(), 0, 0); } } @Override public T get() { return instance; } @Override public T getUsing(@Nullable T using) { if (using == null) using = createInstance(); initBytes(); using.readMarshallable(bytes); bytes.readPosition(0); return using; } @Override public int hashCode() { return dataHashCode(); } @Override public boolean equals(Object obj) { return dataEquals(obj); } @Override public String toString() { return get().toString(); } @Override public Data<T> getData(@NotNull T instance) { this.instance = instance; bytesInit = false; return this; } @Override public void uninit() { instance = null; } @Override public DataAccess<T> copy() { return new BytesMarshallableDataAccess<>(tType(), bytes.realCapacity()); } }
if (!bytesInit) { bytes.clear(); instance.writeMarshallable(bytes); bytesInit = true; }
771
38
809
<methods>public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private java.lang.reflect.Type tClass
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/BytesMarshallableReader.java
BytesMarshallableReader
read
class BytesMarshallableReader<T extends BytesMarshallable> extends InstanceCreatingMarshaller<T> implements SizedReader<T>, BytesReader<T> { public BytesMarshallableReader(Class<T> tClass) { super(tClass); } @NotNull @Override public T read(@NotNull Bytes in, long size, @Nullable T using) { return read(in, using); } @NotNull @Override public T read(Bytes in, @Nullable T using) {<FILL_FUNCTION_BODY>} }
if (using == null) using = createInstance(); using.readMarshallable(in); return using;
153
34
187
<methods>public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private java.lang.reflect.Type tClass
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/BytesMarshallableReaderWriter.java
BytesMarshallableReaderWriter
read
class BytesMarshallableReaderWriter<V extends BytesMarshallable> extends CachingCreatingMarshaller<V> { private static final ThreadLocal<VanillaBytes> VANILLA_BYTES_TL = ThreadLocal.withInitial(VanillaBytes::vanillaBytes); public BytesMarshallableReaderWriter(Class<V> vClass) { super(vClass); } @NotNull @Override public V read(Bytes in, long size, @Nullable V using) {<FILL_FUNCTION_BODY>} @Override protected void writeToWire(Wire wire, @NotNull V toWrite) { toWrite.writeMarshallable(wire.bytes()); } }
if (using == null) using = createInstance(); VanillaBytes vanillaBytes = VANILLA_BYTES_TL.get(); vanillaBytes.bytesStore(in.bytesStore(), in.readPosition(), size); using.readMarshallable(vanillaBytes); return using;
184
78
262
<methods>public void <init>(Class<V>) ,public long size(V) ,public void write(Bytes#RAW, long, V) <variables>static final ThreadLocal<java.lang.Object> LAST_TL,static final ThreadLocal<net.openhft.chronicle.wire.Wire> WIRE_TL
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/BytesSizedMarshaller.java
BytesSizedMarshaller
read
class BytesSizedMarshaller implements SizedReader<Bytes<?>>, SizedWriter<Bytes<?>> { @Override public Bytes<?> read(Bytes in, long size, Bytes<?> using) {<FILL_FUNCTION_BODY>} @Override public long size(Bytes<?> toWrite) { return toWrite.readRemaining(); } @Override public void write(Bytes out, long size, Bytes<?> toWrite) { out.write(toWrite, toWrite.readPosition(), size); } }
final int size0 = Maths.toInt32(size); if (using == null) using = Bytes.allocateElasticOnHeap(size0); in.read(using, size0); return using;
148
63
211
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/CachingCreatingMarshaller.java
CachingCreatingMarshaller
write
class CachingCreatingMarshaller<V> extends InstanceCreatingMarshaller<V> implements SizedReader<V>, SizedWriter<V> { static final ThreadLocal<Wire> WIRE_TL = ThreadLocal.withInitial( () -> WireType.BINARY_LIGHT.apply(Bytes.allocateElasticOnHeap(128))); static final ThreadLocal<Object> LAST_TL = new ThreadLocal<>(); public CachingCreatingMarshaller(Class<V> vClass) { super(vClass); } @Override public long size(@NotNull V toWrite) { Wire wire = WIRE_TL.get(); wire.bytes().clear(); writeToWire(wire, toWrite); LAST_TL.set(toWrite); return wire.bytes().readRemaining(); } protected abstract void writeToWire(Wire wire, @NotNull V toWrite); @Override public void write(Bytes out, long size, @NotNull V toWrite) {<FILL_FUNCTION_BODY>} }
if (LAST_TL.get() == toWrite) { Wire wire = WIRE_TL.get(); if (wire.bytes().readRemaining() == size) { out.write(wire.bytes()); wire.bytes().clear(); LAST_TL.remove(); return; } } BinaryWire wire = Wires.binaryWireForWrite(out, out.writePosition(), size); writeToWire(wire, toWrite);
282
124
406
<methods>public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private java.lang.reflect.Type tClass
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceBytesReader.java
CharSequenceBytesReader
read
class CharSequenceBytesReader implements BytesReader<CharSequence>, StatefulCopyable<CharSequenceBytesReader>, EnumMarshallable<CharSequenceBytesReader> { public static final CharSequenceBytesReader INSTANCE = new CharSequenceBytesReader(); private CharSequenceBytesReader() { } @NotNull @Override public CharSequence read(Bytes in, @Nullable CharSequence using) {<FILL_FUNCTION_BODY>} @Override public CharSequenceBytesReader copy() { return INSTANCE; } @NotNull @Override public CharSequenceBytesReader readResolve() { return INSTANCE; } }
StringBuilder usingSB; if (using instanceof StringBuilder) { usingSB = (StringBuilder) using; } else { usingSB = new StringBuilder(); } if (in.readUtf8(usingSB)) { return usingSB; } else { throw new NullPointerException("BytesReader couldn't read null"); }
168
93
261
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceSizedReader.java
CharSequenceSizedReader
read
class CharSequenceSizedReader implements SizedReader<CharSequence>, StatefulCopyable<CharSequenceSizedReader>, ReadResolvable<CharSequenceSizedReader> { public static final CharSequenceSizedReader INSTANCE = new CharSequenceSizedReader(); private CharSequenceSizedReader() { } @NotNull @Override public CharSequence read( @NotNull Bytes in, long size, @Nullable CharSequence using) {<FILL_FUNCTION_BODY>} @Override public CharSequenceSizedReader copy() { return INSTANCE; } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no fields to read } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no fields to write } @NotNull @Override public CharSequenceSizedReader readResolve() { return INSTANCE; } }
if (0 > size || size > Integer.MAX_VALUE) throw new IllegalStateException("positive int size expected, " + size + " given"); int csLen = (int) size; StringBuilder usingSB; if (using instanceof StringBuilder) { usingSB = ((StringBuilder) using); usingSB.setLength(0); usingSB.ensureCapacity(csLen); } else { usingSB = new StringBuilder(csLen); } BytesUtil.parseUtf8(in, usingSB, csLen); return usingSB;
247
147
394
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/CharSequenceUtf8DataAccess.java
CharSequenceUtf8DataAccess
getUsing
class CharSequenceUtf8DataAccess extends AbstractCharSequenceUtf8DataAccess<CharSequence> { public CharSequenceUtf8DataAccess() { this(DefaultElasticBytes.DEFAULT_BYTES_CAPACITY); } private CharSequenceUtf8DataAccess(long bytesCapacity) { super(bytesCapacity); } @Override public CharSequence getUsing(@Nullable CharSequence using) {<FILL_FUNCTION_BODY>} @Override public DataAccess<CharSequence> copy() { return new CharSequenceUtf8DataAccess(bytes().realCapacity()); } }
StringBuilder sb; if (using instanceof StringBuilder) { sb = (StringBuilder) using; sb.setLength(0); } else { sb = new StringBuilder(cs.length()); } sb.append(cs); return sb;
160
71
231
<methods>public net.openhft.chronicle.bytes.RandomDataInput bytes() ,public java.lang.CharSequence get() ,public Data<java.lang.CharSequence> getData(java.lang.CharSequence) ,public long offset() ,public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public long size() ,public void uninit() ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private transient Bytes#RAW bytes,transient java.lang.CharSequence cs
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/CommonMarshallableReaderWriter.java
CommonMarshallableReaderWriter
read
class CommonMarshallableReaderWriter<V extends CommonMarshallable> extends CachingCreatingMarshaller<V> { public CommonMarshallableReaderWriter(Class<V> vClass) { super(vClass); } @NotNull @Override public V read(Bytes in, long size, @Nullable V using) {<FILL_FUNCTION_BODY>} @Override protected void writeToWire(Wire wire, @NotNull V toWrite) { if (toWrite.usesSelfDescribingMessage()) { ((WriteMarshallable) toWrite).writeMarshallable(wire); } else { ((WriteBytesMarshallable) toWrite).writeMarshallable(wire.bytes()); } } }
if (using == null) using = createInstance(); if (using.usesSelfDescribingMessage()) { ((ReadMarshallable) using).readMarshallable(Wires.binaryWireForRead(in, in.readPosition(), size)); } else { ((ReadBytesMarshallable) using).readMarshallable(in); } return using;
190
96
286
<methods>public void <init>(Class<V>) ,public long size(V) ,public void write(Bytes#RAW, long, V) <variables>static final ThreadLocal<java.lang.Object> LAST_TL,static final ThreadLocal<net.openhft.chronicle.wire.Wire> WIRE_TL
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ConstantSizeMarshaller.java
ConstantSizeMarshaller
writeSize
class ConstantSizeMarshaller implements SizeMarshaller { /** * Config field */ private long constantSize; public ConstantSizeMarshaller(long constantSize) { this.constantSize = constantSize; } @Override public int storingLength(long size) { return 0; } @Override public long minStorableSize() { return constantSize; } @Override public long maxStorableSize() { return constantSize; } @Override public int minStoringLengthOfSizesInRange(long minSize, long maxSize) { return 0; } @Override public int maxStoringLengthOfSizesInRange(long minSize, long maxSize) { return 0; } @Override public void writeSize(Bytes out, long sizeToWrite) {<FILL_FUNCTION_BODY>} @Override public long readSize(Bytes in) { return constantSize; } @Override public void readMarshallable(@NotNull WireIn wireIn) { constantSize = wireIn.read(() -> "constantSize").int64(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write(() -> "constantSize").int64(constantSize); } }
if (sizeToWrite != constantSize) { throw new IllegalArgumentException( "sizeToWrite: " + sizeToWrite + ", constant size should be: " + constantSize); } // do nothing
357
55
412
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/DefaultElasticBytes.java
DefaultElasticBytes
allocateDefaultElasticBytes
class DefaultElasticBytes { static final int DEFAULT_BYTES_CAPACITY = 32; private DefaultElasticBytes() { } static Bytes<?> allocateDefaultElasticBytes(long bytesCapacity) {<FILL_FUNCTION_BODY>} }
if (bytesCapacity <= 0x7FFFFFF0) { return Bytes.elasticHeapByteBuffer((int) bytesCapacity); } else { return Bytes.allocateElasticDirect(bytesCapacity); }
75
61
136
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/DoubleDataAccess.java
DoubleDataAccess
bytes
class DoubleDataAccess extends AbstractData<Double> implements DataAccess<Double>, Data<Double> { // Cache fields private transient boolean bsInit; private transient BytesStore bs; /** * State field */ private transient Double instance; public DoubleDataAccess() { initTransients(); } private void initTransients() { bs = BytesStore.wrap(new byte[8]); } @Override public RandomDataInput bytes() {<FILL_FUNCTION_BODY>} @Override public long offset() { return 0; } @Override public long size() { return 8; } @Override public Double get() { return instance; } @Override public Double getUsing(@Nullable Double using) { return instance; } @Override public long hash(LongHashFunction f) { return f.hashLong(Double.doubleToRawLongBits(instance)); } @Override public boolean equivalent(RandomDataInput source, long sourceOffset) { return source.readLong(sourceOffset) == Double.doubleToRawLongBits(instance); } @Override public void writeTo(RandomDataOutput target, long targetOffset) { target.writeDouble(targetOffset, instance); } @Override public Data<Double> getData(@NotNull Double instance) { this.instance = instance; bsInit = false; return this; } @Override public void uninit() { instance = null; } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no config fields to read initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no config fields to write } @Override public DataAccess<Double> copy() { return new DoubleDataAccess(); } }
if (!bsInit) { bs.writeDouble(0, instance); bsInit = true; } return bs;
519
40
559
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ExternalBytesMarshallableDataAccess.java
ExternalBytesMarshallableDataAccess
readMarshallable
class ExternalBytesMarshallableDataAccess<T> extends InstanceCreatingMarshaller<T> implements DataAccess<T>, Data<T> { // Config fields private SizedReader<T> reader; private BytesWriter<? super T> writer; /** * Cache field */ private transient Bytes bytes; /** * State field */ private transient T instance; public ExternalBytesMarshallableDataAccess( Class<T> tClass, SizedReader<T> reader, BytesWriter<? super T> writer) { this(tClass, reader, writer, DEFAULT_BYTES_CAPACITY); } private ExternalBytesMarshallableDataAccess( Type tClass, SizedReader<T> reader, BytesWriter<? super T> writer, long bytesCapacity) { super(tClass); this.writer = writer; this.reader = reader; initTransients(bytesCapacity); } private void initTransients(long bytesCapacity) { bytes = DefaultElasticBytes.allocateDefaultElasticBytes(bytesCapacity); } @Override public RandomDataInput bytes() { return bytes.bytesStore(); } @Override public long offset() { return 0; } @Override public long size() { return bytes.readRemaining(); } @Override public T get() { return instance; } @Override public T getUsing(@Nullable T using) { if (using == null) using = createInstance(); T result = reader.read(bytes, size(), using); bytes.readPosition(0); return result; } @Override public int hashCode() { return dataHashCode(); } @Override public boolean equals(Object obj) { return dataEquals(obj); } @Override public String toString() { return get().toString(); } @Override public Data<T> getData(@NotNull T instance) { this.instance = instance; bytes.clear(); writer.write(bytes, instance); return this; } @Override public void uninit() { instance = null; } @Override public DataAccess<T> copy() { return new ExternalBytesMarshallableDataAccess<>( tType(), copyIfNeeded(reader), copyIfNeeded(writer), bytes.realCapacity()); } @Override public void readMarshallable(@NotNull WireIn wireIn) {<FILL_FUNCTION_BODY>} @Override public void writeMarshallable(@NotNull WireOut wireOut) { super.writeMarshallable(wireOut); wireOut.write(() -> "reader").typedMarshallable(reader); wireOut.write(() -> "writer").typedMarshallable(writer); } }
super.readMarshallable(wireIn); reader = wireIn.read(() -> "reader").typedMarshallable(); writer = wireIn.read(() -> "writer").typedMarshallable(); initTransients(DEFAULT_BYTES_CAPACITY);
760
69
829
<methods>public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private java.lang.reflect.Type tClass
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ExternalizableDataAccess.java
ExternalizableDataAccess
createInstance
class ExternalizableDataAccess<T extends Externalizable> extends SerializableDataAccess<T> { /** * Config field */ private Class<T> tClass; public ExternalizableDataAccess(Class<T> tClass) { this(tClass, DEFAULT_BYTES_CAPACITY); } private ExternalizableDataAccess(Class<T> tClass, long bytesCapacity) { super(bytesCapacity); this.tClass = tClass; } protected Class<T> tClass() { return tClass; } protected T createInstance() {<FILL_FUNCTION_BODY>} @Override public T getUsing(@Nullable T using) { if (using == null) using = createInstance(); try { using.readExternal(new ObjectInputStream(in)); bytes.readPosition(0); return using; } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } } @Override public Data<T> getData(@NotNull T instance) { this.instance = instance; bytes.clear(); try { ObjectOutputStream out = new ObjectOutputStream(this.out); instance.writeExternal(out); out.flush(); return this; } catch (IOException e) { throw new RuntimeException(e); } } @Override public DataAccess<T> copy() { return new ExternalizableDataAccess<>(tClass, bytes.realCapacity()); } @Override public void readMarshallable(@NotNull WireIn wireIn) { tClass = wireIn.read(() -> "tClass").typeLiteral(); initTransients(DEFAULT_BYTES_CAPACITY); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write(() -> "tClass").typeLiteral(tClass); } }
try { return tClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException( "Externalizable " + tClass + " must have a public no-arg constructor", e); }
501
64
565
<methods>public void <init>() ,public net.openhft.chronicle.bytes.RandomDataInput bytes() ,public DataAccess<T> copy() ,public T get() ,public Data<T> getData(T) ,public T getUsing(T) ,public long offset() ,public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public long size() ,public void uninit() ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>transient Bytes#RAW bytes,transient java.io.InputStream in,transient T instance,transient java.io.OutputStream out
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ExternalizableReader.java
ExternalizableReader
read
class ExternalizableReader<T extends Externalizable> extends InstanceCreatingMarshaller<T> implements SizedReader<T>, BytesReader<T> { public ExternalizableReader(Class<T> tClass) { super(tClass); } @NotNull @Override public T read(@NotNull Bytes in, long size, @Nullable T using) { return read(in, using); } @NotNull @Override public T read(Bytes in, @Nullable T using) {<FILL_FUNCTION_BODY>} }
if (using == null) using = createInstance(); try { using.readExternal(new ObjectInputStream(in.inputStream())); return using; } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); }
147
68
215
<methods>public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private java.lang.reflect.Type tClass
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/InstanceCreatingMarshaller.java
InstanceCreatingMarshaller
createInstance
class InstanceCreatingMarshaller<T> implements Marshallable { private Type tClass; /** * Constructor for use in subclasses. * * @param tClass the class of objects deserialized */ protected InstanceCreatingMarshaller(Class<T> tClass) { this.tClass = tClass; } protected InstanceCreatingMarshaller(Type tClass) { this.tClass = tClass; } /** * Returns the class of objects deserialized. */ protected Class<T> tClass() { return (Class<T>) tClass; } protected Type tType() { return tClass; } /** * Creates a new {@code T} instance by calling {@link Class#newInstance()}. If you need * different logic, i. e. calling a constructor with parameter, override this method in a * subclass of the specific {@link DataAccess} or {@link SizedReader} and configure in {@link * ChronicleMapBuilder}. * * @return a new instance to return from {@link Data#getUsing(Object)} or {@link * SizedReader#read(net.openhft.chronicle.bytes.Bytes, long, Object)} method */ protected T createInstance() {<FILL_FUNCTION_BODY>} @Override public void readMarshallable(@NotNull WireIn wireIn) { //noinspection unchecked tClass = wireIn.read(() -> "tClass").lenientTypeLiteral(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write(() -> "tClass").typeLiteral(tClass); } }
try { return ObjectUtils.newInstance(tClass()); } catch (Exception e) { throw new IllegalStateException("Some of default marshallers, chosen for the type\n" + tClass + " by default, delegate to \n" + this.getClass().getName() + " which assumes the type has a public no-arg\n" + "constructor. If this is not true, you should either extend the marshaller,\n" + "overriding createInstance() and copy() (if defined), and the extending\n" + "class shouldn't be inner, because such classes couldn't be Serializable\n" + "that is a requirement for marshaller classes, or write and configure your\n" + "own marshaller for " + tClass + " type from scratch, and configure for the\n" + "Chronicle Map via keyMarshaller[s]() or valueMarshaller[s]() methods", e); }
444
240
684
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/IntegerDataAccess_3_13.java
IntegerDataAccess_3_13
bytes
class IntegerDataAccess_3_13 extends AbstractData<Integer> implements DataAccess<Integer>, Data<Integer> { // Cache fields private transient boolean bsInit; private transient BytesStore bs; /** * State field */ private transient Integer instance; public IntegerDataAccess_3_13() { initTransients(); } private void initTransients() { bs = BytesStore.wrap(new byte[4]); } @Override public RandomDataInput bytes() {<FILL_FUNCTION_BODY>} @Override public long offset() { return 0; } @Override public long size() { return 4; } @Override public Integer get() { return instance; } @Override public Integer getUsing(@Nullable Integer using) { return instance; } @Override public long hash(LongHashFunction f) { return f.hashInt(instance); } @Override public boolean equivalent(RandomDataInput source, long sourceOffset) { return source.readInt(sourceOffset) == instance; } @Override public void writeTo(RandomDataOutput target, long targetOffset) { target.writeInt(targetOffset, instance); } @Override public Data<Integer> getData(@NotNull Integer instance) { this.instance = instance; bsInit = false; return this; } @Override public void uninit() { instance = null; } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no config fields to read initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no config fields to write } @Override public DataAccess<Integer> copy() { return new IntegerDataAccess_3_13(); } }
if (!bsInit) { bs.writeInt(0, instance); bsInit = true; } return bs;
516
40
556
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/LongDataAccess.java
LongDataAccess
bytes
class LongDataAccess extends AbstractData<Long> implements DataAccess<Long>, Data<Long> { // Cache fields private transient boolean bsInit; private transient BytesStore bs; /** * State field */ private transient Long instance; public LongDataAccess() { initTransients(); } private void initTransients() { bs = BytesStore.wrap(new byte[8]); } @Override public RandomDataInput bytes() {<FILL_FUNCTION_BODY>} @Override public long offset() { return 0; } @Override public long size() { return 8; } @Override public Long get() { return instance; } @Override public Long getUsing(@Nullable Long using) { return instance; } @Override public long hash(LongHashFunction f) { return f.hashLong(instance); } @Override public boolean equivalent(RandomDataInput source, long sourceOffset) { return source.readLong(sourceOffset) == instance; } @Override public void writeTo(RandomDataOutput target, long targetOffset) { target.writeLong(targetOffset, instance); } @Override public Data<Long> getData(@NotNull Long instance) { this.instance = instance; bsInit = false; return this; } @Override public void uninit() { instance = null; } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no config fields to read initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no config fields to write } @Override public DataAccess<Long> copy() { return new LongDataAccess(); } }
if (!bsInit) { bs.writeLong(0, instance); bsInit = true; } return bs;
501
40
541
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/MarshallableReaderWriter.java
MarshallableReaderWriter
read
class MarshallableReaderWriter<V extends Marshallable> extends CachingCreatingMarshaller<V> { public MarshallableReaderWriter(Class<V> vClass) { super(vClass); } @NotNull @Override public V read(Bytes in, long size, @Nullable V using) {<FILL_FUNCTION_BODY>} @Override protected void writeToWire(Wire wire, @NotNull V toWrite) { toWrite.writeMarshallable(wire); } }
if (using == null) using = createInstance(); using.readMarshallable(Wires.binaryWireForRead(in, in.readPosition(), size)); return using;
133
51
184
<methods>public void <init>(Class<V>) ,public long size(V) ,public void write(Bytes#RAW, long, V) <variables>static final ThreadLocal<java.lang.Object> LAST_TL,static final ThreadLocal<net.openhft.chronicle.wire.Wire> WIRE_TL
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializableDataAccess.java
SerializableDataAccess
getUsing
class SerializableDataAccess<T extends Serializable> extends AbstractData<T> implements DataAccess<T> { // Cache fields transient Bytes bytes; transient OutputStream out; transient InputStream in; /** * State field */ transient T instance; public SerializableDataAccess() { this(DEFAULT_BYTES_CAPACITY); } SerializableDataAccess(long bytesCapacity) { initTransients(bytesCapacity); } void initTransients(long bytesCapacity) { bytes = DefaultElasticBytes.allocateDefaultElasticBytes(bytesCapacity); out = bytes.outputStream(); in = bytes.inputStream(); } @Override public RandomDataInput bytes() { return bytes.bytesStore(); } @Override public long offset() { return 0; } @Override public long size() { return bytes.readRemaining(); } @Override public T get() { return instance; } // TODO reuse using object @Override public T getUsing(@Nullable T using) {<FILL_FUNCTION_BODY>} @Override public Data<T> getData(@NotNull T instance) { this.instance = instance; bytes.clear(); try { ObjectOutputStream out = new ObjectOutputStream(this.out); out.writeObject(instance); out.flush(); return this; } catch (IOException e) { throw new RuntimeException(e); } } @Override public void uninit() { instance = null; } @Override public DataAccess<T> copy() { return new SerializableDataAccess<>(bytes.realCapacity()); } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no fields to read initTransients(DEFAULT_BYTES_CAPACITY); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no fields to write } }
try { T result = (T) new ObjectInputStream(in).readObject(); bytes.readPosition(0); return result; } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); }
543
63
606
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/SerializableReader.java
SerializableReader
read
class SerializableReader<T extends Serializable> implements SizedReader<T>, BytesReader<T> { @NotNull @Override public T read(@NotNull Bytes in, long size, @Nullable T using) { return read(in, using); } @NotNull @Override public T read(Bytes in, @Nullable T using) {<FILL_FUNCTION_BODY>} @Override public void readMarshallable(@NotNull WireIn wireIn) { // no fields to read } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no fields to write } }
try { return (T) new ObjectInputStream(in.inputStream()).readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); }
169
50
219
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/SizedMarshallableDataAccess.java
SizedMarshallableDataAccess
getUsing
class SizedMarshallableDataAccess<T> extends InstanceCreatingMarshaller<T> implements DataAccess<T>, Data<T> { // Config fields private SizedReader<T> sizedReader; private SizedWriter<? super T> sizedWriter; // Cache fields private transient boolean bytesInit; private transient Bytes bytes; private transient long size; private transient VanillaBytes targetBytes; /** * State field */ private transient T instance; public SizedMarshallableDataAccess( Class<T> tClass, SizedReader<T> sizedReader, SizedWriter<? super T> sizedWriter) { this(tClass, sizedReader, sizedWriter, DEFAULT_BYTES_CAPACITY); } protected SizedMarshallableDataAccess( Type tClass, SizedReader<T> sizedReader, SizedWriter<? super T> sizedWriter, long bytesCapacity) { super(tClass); this.sizedWriter = sizedWriter; this.sizedReader = sizedReader; initTransients(bytesCapacity); } SizedReader<T> sizedReader() { return sizedReader; } SizedWriter<? super T> sizedWriter() { return sizedWriter; } private void initTransients(long bytesCapacity) { bytes = DefaultElasticBytes.allocateDefaultElasticBytes(bytesCapacity); targetBytes = VanillaBytes.vanillaBytes(); } @Override public RandomDataInput bytes() { if (!bytesInit) { bytes.clear(); sizedWriter.write(bytes, size, instance); bytesInit = true; } return bytes.bytesStore(); } @Override public long offset() { return 0; } @Override public long size() { return size; } @Override public void writeTo(RandomDataOutput target, long targetOffset) { if (bytesInit || !(target instanceof BytesStore)) { target.write(targetOffset, bytes(), offset(), size); } else { targetBytes.bytesStore((BytesStore) target, targetOffset, size); targetBytes.writePosition(targetOffset); sizedWriter.write(targetBytes, size, instance); targetBytes.bytesStore(BytesStore.empty(), 0, 0); } } @Override public T get() { return instance; } @Override public T getUsing(@Nullable T using) {<FILL_FUNCTION_BODY>} @Override public int hashCode() { return dataHashCode(); } @Override public boolean equals(Object obj) { return dataEquals(obj); } @Override public String toString() { return get().toString(); } @Override public Data<T> getData(@NotNull T instance) { this.instance = instance; this.size = sizedWriter.size(instance); bytesInit = false; return this; } @Override public void uninit() { instance = null; } @Override public DataAccess<T> copy() { return new SizedMarshallableDataAccess<>( tType(), copyIfNeeded(sizedReader), copyIfNeeded(sizedWriter), bytes.realCapacity()); } @Override public void readMarshallable(@NotNull WireIn wireIn) { super.readMarshallable(wireIn); sizedReader = wireIn.read(() -> "sizedReader").object(SizedReader.class); sizedWriter = wireIn.read(() -> "sizedWriter").object(SizedWriter.class); initTransients(DEFAULT_BYTES_CAPACITY); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { super.writeMarshallable(wireOut); wireOut.write(() -> "sizedReader").typedMarshallable(sizedReader); wireOut.write(() -> "sizedWriter").typedMarshallable(sizedWriter); } }
if (using == null) using = createInstance(); T result = sizedReader.read(bytes, size(), using); bytes.readPosition(0); return result;
1,049
48
1,097
<methods>public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private java.lang.reflect.Type tClass
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/StopBitSizeMarshaller.java
StopBitSizeMarshaller
minStoringLengthOfSizesInRange
class StopBitSizeMarshaller implements SizeMarshaller, EnumMarshallable<StopBitSizeMarshaller> { public static final StopBitSizeMarshaller INSTANCE = new StopBitSizeMarshaller(); private static final long MIN_ENCODABLE_SIZE = Long.MIN_VALUE; private static final long MAX_ENCODABLE_SIZE = Long.MAX_VALUE; private StopBitSizeMarshaller() { } private static void rangeChecks(long minSize, long maxSize) { if (minSize > maxSize) throw new IllegalArgumentException("minSize = " + minSize + ", maxSize = " + maxSize); } @Override public int storingLength(long size) { return BytesUtil.stopBitLength(size); } @Override public long minStorableSize() { return MIN_ENCODABLE_SIZE; } @Override public long maxStorableSize() { return MAX_ENCODABLE_SIZE; } @Override public int minStoringLengthOfSizesInRange(long minSize, long maxSize) {<FILL_FUNCTION_BODY>} @Override public int maxStoringLengthOfSizesInRange(long minSize, long maxSize) { rangeChecks(minSize, maxSize); return max(storingLength(minSize), storingLength(maxSize)); } @Override public void writeSize(Bytes out, long sizeToWrite) { BytesUtil.writeStopBit(out, sizeToWrite); } @Override public long readSize(Bytes in) { return BytesUtil.readStopBit(in); } @NotNull @Override public StopBitSizeMarshaller readResolve() { return INSTANCE; } }
rangeChecks(minSize, maxSize); // different signs if (minSize * maxSize < 0) { // the range includes 0 which encoding length is 1 return 1; } return min(storingLength(minSize), storingLength(maxSize));
466
74
540
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBuilderSizedReader.java
StringBuilderSizedReader
read
class StringBuilderSizedReader implements SizedReader<StringBuilder>, EnumMarshallable<StringBuilderSizedReader> { public static final StringBuilderSizedReader INSTANCE = new StringBuilderSizedReader(); private StringBuilderSizedReader() { } @NotNull @Override public StringBuilder read(Bytes in, long size, @Nullable StringBuilder using) {<FILL_FUNCTION_BODY>} @NotNull @Override public StringBuilderSizedReader readResolve() { return INSTANCE; } }
if (0 > size || size > Integer.MAX_VALUE) throw new IllegalStateException("positive int size expected, " + size + " given"); int csLen = (int) size; if (using == null) { using = new StringBuilder(csLen); } else { using.setLength(0); using.ensureCapacity(csLen); } BytesUtil.parseUtf8(in, using, csLen); return using;
141
123
264
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBuilderUtf8DataAccess.java
StringBuilderUtf8DataAccess
getUsing
class StringBuilderUtf8DataAccess extends AbstractCharSequenceUtf8DataAccess<StringBuilder> { public StringBuilderUtf8DataAccess() { this(DEFAULT_BYTES_CAPACITY); } private StringBuilderUtf8DataAccess(long bytesCapacity) { super(bytesCapacity); } @Override public StringBuilder getUsing(@Nullable StringBuilder using) {<FILL_FUNCTION_BODY>} @Override public DataAccess<StringBuilder> copy() { return new StringBuilderUtf8DataAccess(bytes().realCapacity()); } }
if (using != null) { using.setLength(0); } else { using = new StringBuilder(cs.length()); } using.append(cs); return using;
155
55
210
<methods>public net.openhft.chronicle.bytes.RandomDataInput bytes() ,public java.lang.StringBuilder get() ,public Data<java.lang.StringBuilder> getData(java.lang.StringBuilder) ,public long offset() ,public void readMarshallable(net.openhft.chronicle.wire.WireIn) ,public long size() ,public void uninit() ,public void writeMarshallable(net.openhft.chronicle.wire.WireOut) <variables>private transient Bytes#RAW bytes,transient java.lang.StringBuilder cs
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringBytesReader.java
StringBytesReader
read
class StringBytesReader implements BytesReader<String>, StatefulCopyable<StringBytesReader> { /** * Cache field */ private transient StringBuilder sb; public StringBytesReader() { initTransients(); } private void initTransients() { sb = new StringBuilder(); } @NotNull @Override public String read(Bytes in, @Nullable String using) {<FILL_FUNCTION_BODY>} @Override public StringBytesReader copy() { return new StringBytesReader(); } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no fields to read initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no fields to write } }
if (in.readUtf8(sb)) { return sb.toString(); } else { throw new NullPointerException("BytesReader couldn't read null"); }
215
48
263
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/StringSizedReader.java
StringSizedReader
read
class StringSizedReader implements SizedReader<String>, StatefulCopyable<StringSizedReader> { /** * Cache field */ private transient StringBuilder sb; public StringSizedReader() { initTransients(); } private void initTransients() { sb = new StringBuilder(); } @NotNull @Override public String read(@NotNull Bytes in, long size, @Nullable String using) {<FILL_FUNCTION_BODY>} @Override public StringSizedReader copy() { return new StringSizedReader(); } @Override public void readMarshallable(@NotNull WireIn wireIn) { // no fields to read initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { // no fields to write } }
if (0 > size || size > Integer.MAX_VALUE) throw new IllegalStateException("positive int size expected, " + size + " given"); sb.setLength(0); BytesUtil.parseUtf8(in, sb, (int) size); return sb.toString();
225
75
300
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/TypedMarshallableReaderWriter.java
TypedMarshallableReaderWriter
read
class TypedMarshallableReaderWriter<V extends Marshallable> extends CachingCreatingMarshaller<V> { public TypedMarshallableReaderWriter(Class<V> vClass) { super(vClass); } @NotNull @Override public V read(Bytes in, long size, @Nullable V using) {<FILL_FUNCTION_BODY>} protected void writeToWire(Wire wire, @NotNull V toWrite) { wire.getValueOut().object(toWrite); } }
BinaryWire wire = Wires.binaryWireForRead(in, in.readPosition(), size); return (V) wire.getValueIn().object(using, tClass());
136
47
183
<methods>public void <init>(Class<V>) ,public long size(V) ,public void write(Bytes#RAW, long, V) <variables>static final ThreadLocal<java.lang.Object> LAST_TL,static final ThreadLocal<net.openhft.chronicle.wire.Wire> WIRE_TL
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ValueDataAccess.java
ValueDataAccess
allocateBytesStoreForInstance
class ValueDataAccess<T> extends AbstractData<T> implements DataAccess<T> { /** * Config field */ private Class<T> valueType; // Cache fields private transient Class<? extends T> nativeClass; private transient Class<? extends T> heapClass; private transient Byteable nativeInstance; private transient Copyable nativeInstanceAsCopyable; /** * State field */ private transient Byteable instance; public ValueDataAccess(Class<T> valueType) { this.valueType = valueType; initTransients(); } /** * Returns the interface of values serialized. */ protected Class<T> valueType() { return valueType; } protected Class<? extends T> nativeClass() { return nativeClass; } protected Class<? extends T> heapClass() { return heapClass; } private void initTransients() { nativeInstance = (Byteable) Values.newNativeReference(valueType); nativeInstanceAsCopyable = (Copyable) nativeInstance; nativeClass = (Class<? extends T>) nativeInstance.getClass(); heapClass = Values.heapClassFor(valueType); nativeInstance.bytesStore(allocateBytesStoreForInstance(), 0, nativeInstance.maxSize()); } private BytesStore allocateBytesStoreForInstance() {<FILL_FUNCTION_BODY>} protected T createInstance() { try { return heapClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } @Override public RandomDataInput bytes() { return instance.bytesStore(); } @Override public long offset() { return instance.offset(); } @Override public long size() { return instance.maxSize(); } @Override public T get() { return (T) instance; } @Override public T getUsing(@Nullable T using) { if (using == null) using = createInstance(); ((Copyable) using).copyFrom(instance); return using; } @Override public Data<T> getData(@NotNull T instance) { if (instance.getClass() == nativeClass) { this.instance = (Byteable) instance; } else { nativeInstanceAsCopyable.copyFrom(instance); this.instance = nativeInstance; } return this; } @Override public void uninit() { instance = null; } @Override public DataAccess<T> copy() { return new ValueDataAccess<>(valueType); } @Override public void readMarshallable(@NotNull WireIn wireIn) { valueType = wireIn.read("valueType").typeLiteral(); initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write("valueType").typeLiteral(valueType); } }
long instanceSize = nativeInstance.maxSize(); if (instanceSize > 0x7FFFFFF0) { return BytesStore.nativeStoreWithFixedCapacity(instanceSize); } else { return BytesStore.wrap(ByteBuffer.allocate(Maths.toUInt31(instanceSize))); }
794
82
876
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/hash/serialization/impl/ValueReader.java
ValueReader
initTransients
class ValueReader<T> implements SizedReader<T>, BytesReader<T>, StatefulCopyable<ValueReader<T>> { /** * Config field */ private Class<T> valueType; // Cache fields private transient Class<? extends T> nativeClass; private transient Class<? extends T> heapClass; private transient Byteable nativeReference; public ValueReader(Class<T> valueType) { this.valueType = valueType; initTransients(); } /** * Returns the interface of values deserialized. */ protected Class<T> valueType() { return valueType; } protected Class<? extends T> nativeClass() { return nativeClass; } protected Class<? extends T> heapClass() { return heapClass; } private void initTransients() {<FILL_FUNCTION_BODY>} protected T createInstance() { try { return heapClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { throw new RuntimeException(e); } } @NotNull @Override public T read(@NotNull Bytes in, long size, @Nullable T using) { if (size != nativeReference.maxSize()) throw new IllegalArgumentException(); return read(in, using); } @NotNull @Override public T read(Bytes in, @Nullable T using) { if (using != null && using.getClass() == nativeClass) { ((Byteable) using).bytesStore(in.bytesStore(), in.readPosition(), nativeReference.maxSize()); return using; } if (using == null) using = createInstance(); nativeReference.bytesStore(in.bytesStore(), in.readPosition(), nativeReference.maxSize()); ((Copyable) using).copyFrom(nativeReference); return using; } @Override public ValueReader<T> copy() { return new ValueReader<>(valueType); } @Override public void readMarshallable(@NotNull WireIn wireIn) { valueType = wireIn.read(() -> "valueType").typeLiteral(); initTransients(); } @Override public void writeMarshallable(@NotNull WireOut wireOut) { wireOut.write(() -> "valueType").typeLiteral(valueType); } }
nativeClass = Values.nativeClassFor(valueType); heapClass = Values.heapClassFor(valueType); nativeReference = (Byteable) Values.newNativeReference(valueType);
622
53
675
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/ChronicleHashCorruptionImpl.java
ChronicleHashCorruptionImpl
set
class ChronicleHashCorruptionImpl implements ChronicleHashCorruption { private int segmentIndex; private Supplier<String> messageSupplier; private Throwable exception; private String message; public static void report( ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption, int segmentIndex, Supplier<String> messageSupplier) { corruption.set(segmentIndex, messageSupplier, null); corruptionListener.onCorruption(corruption); } public static void reportException( ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption, int segmentIndex, Supplier<String> messageSupplier, Throwable exception) { corruption.set(segmentIndex, messageSupplier, exception); corruptionListener.onCorruption(corruption); } public static String format(String message, Object... args) { return MessageFormatter.arrayFormat(message, args).getMessage(); } private void set(int segmentIndex, Supplier<String> messageSupplier, Throwable exception) {<FILL_FUNCTION_BODY>} @Override public String message() { if (message == null) { message = messageSupplier.get(); } return message; } @Nullable @Override public Throwable exception() { return exception; } @Override public int segmentIndex() { return segmentIndex; } }
this.segmentIndex = segmentIndex; this.messageSupplier = messageSupplier; this.exception = exception; this.message = null;
384
42
426
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/ChronicleMapEntrySet.java
ChronicleMapEntrySet
contains
class ChronicleMapEntrySet<K, V> extends AbstractSet<Map.Entry<K, V>> { private final AbstractChronicleMap<K, V> map; public ChronicleMapEntrySet(AbstractChronicleMap<K, V> map) { this.map = map; } @NotNull public Iterator<Map.Entry<K, V>> iterator() { return new ChronicleMapIterator.OfEntries<>(map); } public final boolean contains(Object o) {<FILL_FUNCTION_BODY>} public final boolean remove(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; try { Object key = e.getKey(); Object value = e.getValue(); return map.remove(key, value); } catch (ClassCastException | NullPointerException ex) { return false; } } public final int size() { return map.size(); } public final boolean isEmpty() { return map.isEmpty(); } public final void clear() { map.clear(); } }
if (!(o instanceof Map.Entry)) return false; Map.Entry<?, ?> e = (Map.Entry<?, ?>) o; try { V v = map.get(e.getKey()); return v != null && v.equals(e.getValue()); } catch (ClassCastException | NullPointerException ex) { return false; }
311
98
409
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public boolean removeAll(Collection<?>) <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/ChronicleMapIterator.java
ChronicleMapIterator
remove
class ChronicleMapIterator<K, V, E> implements Iterator<E>, Consumer<MapEntry<K, V>> { final AbstractChronicleMap<K, V> map; private final Thread ownerThread = Thread.currentThread(); private final Queue<E> entryBuffer = new ArrayDeque<>(); E returned; private int segmentIndex; ChronicleMapIterator(AbstractChronicleMap<K, V> map) { this.map = map; segmentIndex = map.segments() - 1; } private void checkSingleThreaded() { if (ownerThread != Thread.currentThread()) { throw new IllegalStateException(map.toIdentityString() + ": Iterator should be accessed only from a single thread"); } } private void fillEntryBuffer() { if (!entryBuffer.isEmpty()) return; while (true) { if (segmentIndex < 0) return; try (MapSegmentContext<K, V, ?> c = map.segmentContext(segmentIndex)) { segmentIndex--; if (c.size() == 0) continue; c.forEachSegmentEntry(this); return; } } } @Override public void accept(MapEntry<K, V> e) { entryBuffer.add(read(e)); } abstract E read(MapEntry<K, V> entry); @Override public boolean hasNext() { checkSingleThreaded(); fillEntryBuffer(); return !entryBuffer.isEmpty(); } @Override public E next() { checkSingleThreaded(); fillEntryBuffer(); E e; if ((e = entryBuffer.poll()) == null) throw new NoSuchElementException(map.toIdentityString()); return returned = e; } @Override public void remove() {<FILL_FUNCTION_BODY>} abstract void removeReturned(); static class OfEntries<K, V> extends ChronicleMapIterator<K, V, Entry<K, V>> { OfEntries(AbstractChronicleMap<K, V> map) { super(map); } @Override Entry<K, V> read(MapEntry<K, V> entry) { K key = entry.key().getUsing(null); V value = entry.value().getUsing(null); return new WriteThroughEntry<>(map, key, value); } @Override void removeReturned() { map.remove(returned.getKey(), returned.getValue()); } } static class OfKeys<K, V> extends ChronicleMapIterator<K, V, K> { OfKeys(AbstractChronicleMap<K, V> map) { super(map); } @Override K read(MapEntry<K, V> entry) { return entry.key().getUsing(null); } @Override void removeReturned() { map.remove(returned); } } }
checkSingleThreaded(); if (returned == null) throw new IllegalStateException(map.toIdentityString()); removeReturned(); returned = null;
789
45
834
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/JsonSerializer.java
JsonSerializer
xStream
class JsonSerializer { private JsonSerializer() { } static final String LOG_ERROR_SUGGEST_X_STREAM = "map.getAll(<file>) and map.putAll(<file>) methods require the JSON XStream serializer, " + "we don't include these artifacts by default as some users don't require this functionality. " + "Please add the following artifacts to your project\n" + "<dependency>\n" + " <groupId>xstream</groupId>\n" + " <artifactId>xstream</artifactId>\n" + " <version>1.2.2</version>\n" + "</dependency>\n" + "<dependency>\n" + " <groupId>org.codehaus.jettison</groupId>\n" + " <artifactId>jettison</artifactId>\n" + " <version>1.3.6</version>\n" + "</dependency>\n"; static synchronized <K, V> void getAll(final File toFile, final Map<K, V> map, final List<?> jsonConverters) throws IOException { final XStream xstream = xStream(map, jsonConverters); try (OutputStream outputStream = createOutputStream(toFile)) { xstream.toXML(map, outputStream); } } static synchronized <K, V> void putAll(final File fromFile, final Map<K, V> map, final List<?> jsonConverters) throws IOException { final XStream xstream = xStream(map, jsonConverters); try (InputStream inputStream = createInputStream(fromFile)) { xstream.fromXML(inputStream); } } private static InputStream createInputStream(final File toFile) throws IOException { if (toFile.getName().toLowerCase().endsWith(".gz")) return new GZIPInputStream(new FileInputStream(toFile)); else return new FileInputStream(toFile); } private static OutputStream createOutputStream(final File toFile) throws IOException { if (toFile.getName().toLowerCase().endsWith(".gz")) return new GZIPOutputStream(new FileOutputStream(toFile)); else return new FileOutputStream(toFile); } private static <K, V> XStream xStream(final Map<K, V> map, final List<?> jsonConverters) {<FILL_FUNCTION_BODY>} private static <K, V> void registerChronicleMapConverter(final Map<K, V> map, final XStream xstream) { xstream.registerConverter(new VanillaChronicleMapConverter<>(map)); } }
try { final XStream xstream = new XStream(new JettisonMappedXmlDriver()); xstream.setMode(XStream.NO_REFERENCES); xstream.alias("cmap", map.getClass()); registerChronicleMapConverter(map, xstream); xstream.registerConverter(new ByteBufferConverter()); xstream.registerConverter(new ValueConverter()); xstream.registerConverter(new StringBuilderConverter()); xstream.registerConverter(new CharSequenceConverter()); for (Object c : jsonConverters) { if (c instanceof Converter) { xstream.registerConverter((Converter) c); } else { Jvm.warn().on(JsonSerializer.class, "Skipping Converter of type class=" + c.getClass().getName() + " as " + " expecting an object of type com.thoughtworks.xstream.converters" + ".Converter"); } } return xstream; } catch (NoClassDefFoundError e) { throw new RuntimeException(LOG_ERROR_SUGGEST_X_STREAM, e); }
702
286
988
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/MapDiagnostics.java
MapDiagnostics
printMapStats
class MapDiagnostics { private MapDiagnostics() { } public static void main(String[] args) throws IOException { String mapFile = args[0]; try (ChronicleMap map = ChronicleMap.of(Object.class, Object.class) .createPersistedTo(new File(mapFile))) { printMapStats(map); } } public static <K, V> void printMapStats(ChronicleMap<K, V> map) {<FILL_FUNCTION_BODY>} }
for (int i = 0; i < map.segments(); i++) { try (MapSegmentContext<K, V, ?> c = map.segmentContext(i)) { System.out.printf("segment %d contains %d entries\n", i, c.size()); c.forEachSegmentEntry(e -> System.out.printf("%s, %d bytes -> %s, %d bytes\n", e.key(), e.key().size(), e.value(), e.value().size())); } }
137
134
271
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/MapMethodsSupport.java
MapMethodsSupport
returnCurrentValueIfPresent
class MapMethodsSupport { private MapMethodsSupport() { } static <V> void returnCurrentValueIfPresent( MapQueryContext<?, V, ?> q, ReturnValue<V> returnValue) {<FILL_FUNCTION_BODY>} static <V> boolean tryReturnCurrentValueIfPresent( MapQueryContext<?, V, ?> q, ReturnValue<V> returnValue) { if (q.readLock().tryLock()) { MapEntry<?, V> entry = q.entry(); if (entry != null) { returnValue.returnValue(entry.value()); return true; } // Key is absent q.readLock().unlock(); } q.updateLock().lock(); MapEntry<?, V> entry = q.entry(); if (entry != null) { returnValue.returnValue(entry.value()); return true; } return false; } }
MapEntry<?, V> entry = q.entry(); if (entry != null) returnValue.returnValue(entry.value());
241
38
279
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/OldDeletedEntriesCleanupThread.java
OldDeletedEntriesCleanupThread
cleanupSegment
class OldDeletedEntriesCleanupThread extends Thread implements MapClosable, Predicate<ReplicableEntry> { /** * Don't store a strong ref to a map in order to avoid it's leaking, if the user forgets to close() map, from where this thread is shut down * explicitly. Dereference map within a single method, {@link #cleanupSegment()}. The map has a chance to be collected by GC when this thread is * sleeping after cleaning up a segment. */ private final WeakReference<ReplicatedChronicleMap<?, ?, ?>> mapRef; /** * {@code cleanupTimeout}, {@link #cleanupTimeoutUnit} and {@link #segments} are parts of the * cleaned Map's state, extracted in order to minimize accesses to the map. * * @see ChronicleHashBuilderPrivateAPI#removedEntryCleanupTimeout(long, TimeUnit) */ private final long cleanupTimeout; private final TimeUnit cleanupTimeoutUnit; private final int segments; /** * {@code segmentsPermutation} and {@link #inverseSegmentsPermutation} determine random order, * in which segments are cleaned up. */ private final int[] segmentsPermutation; private final int[] inverseSegmentsPermutation; /** * This object is used to determine that this thread is parked from {@link #sleepMillis(long)} * or {@link #sleepNanos(long)}, not somewhere inside ChronicleMap logic, to interrupt() * selectively in {@link #close()}. */ private final Object cleanupSleepingHandle = new Object(); private volatile boolean shutdown; private long prevSegment0ScanStart = -1; private long removedCompletely; private long startTime = System.currentTimeMillis(); OldDeletedEntriesCleanupThread(ReplicatedChronicleMap<?, ?, ?> map) { super("Cleanup Thread for " + map.toIdentityString()); setDaemon(true); this.mapRef = new WeakReference<>(map); cleanupTimeout = map.cleanupTimeout; cleanupTimeoutUnit = map.cleanupTimeoutUnit; segments = map.segments(); segmentsPermutation = randomPermutation(map.segments()); inverseSegmentsPermutation = inversePermutation(segmentsPermutation); } private static int[] randomPermutation(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = i; } shuffle(a); return a; } // Implementing Fisher–Yates shuffle private static void shuffle(int[] a) { SecureRandom rnd = new SecureRandom(); for (int i = a.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); int e = a[index]; a[index] = a[i]; a[i] = e; } } private static int[] inversePermutation(int[] permutation) { int n = permutation.length; int[] inverse = new int[n]; for (int i = 0; i < n; i++) { inverse[permutation[i]] = i; } return inverse; } @Override public void run() { throwExceptionIfClosed(); if (System.currentTimeMillis() - startTime < 1_000) return; while (!shutdown) { int nextSegmentIndex; try { nextSegmentIndex = cleanupSegment(); } catch (Exception e) { if (shutdown) break; throw e; } if (nextSegmentIndex == -1) return; if (nextSegmentIndex == 0) { long currentTime = currentTime(); long mapScanTime = systemTimeIntervalBetween( prevSegment0ScanStart, currentTime, cleanupTimeoutUnit); Jvm.debug().on(getClass(), "Old deleted entries scan time: " + mapScanTime + " " + cleanupTimeoutUnit); if (mapScanTime < cleanupTimeout) { long timeToSleep = cleanupTimeoutUnit.toMillis(cleanupTimeout - mapScanTime); if (timeToSleep > 0) { sleepMillis(timeToSleep); } else { sleepNanos(cleanupTimeoutUnit.toNanos(cleanupTimeout - mapScanTime)); } } } } } /** * @return next segment index to cleanup, or -1 if cleanup thread should be shut down */ private int cleanupSegment() {<FILL_FUNCTION_BODY>} @Override public boolean test(ReplicableEntry e) { throwExceptionIfClosed(); if (shutdown) return false; if (e instanceof MapAbsentEntry) { long deleteTimeout = systemTimeIntervalBetween( e.originTimestamp(), currentTime(), cleanupTimeoutUnit); if (deleteTimeout > cleanupTimeout && !e.isChanged()) { e.doRemoveCompletely(); removedCompletely++; } } return true; } private void sleepMillis(long millis) { long deadline = System.currentTimeMillis() + millis; while (System.currentTimeMillis() < deadline && !shutdown) LockSupport.parkUntil(cleanupSleepingHandle, deadline); } private void sleepNanos(long nanos) { long deadline = System.nanoTime() + nanos; while (System.nanoTime() < deadline && !shutdown) LockSupport.parkNanos(cleanupSleepingHandle, deadline); } @Override public void close() { shutdown = true; // this means blocked in sleepMillis() or sleepNanos() if (LockSupport.getBlocker(this) == cleanupSleepingHandle) this.interrupt(); // unblock } private int nextSegmentIndex(int segmentIndex) { int permutationIndex = inverseSegmentsPermutation[segmentIndex]; int nextPermutationIndex = (permutationIndex + 1) % segments; return segmentsPermutation[nextPermutationIndex]; } }
ReplicatedChronicleMap<?, ?, ?> map = mapRef.get(); if (map == null) return -1; int segmentIndex = map.globalMutableState().getCurrentCleanupSegmentIndex(); int nextSegmentIndex; try (MapSegmentContext<?, ?, ?> context = map.segmentContext(segmentIndex)) { if (segmentIndex == 0) prevSegment0ScanStart = currentTime(); removedCompletely = 0; if (((ReplicatedHashSegmentContext<?, ?>) context) .forEachSegmentReplicableEntryWhile(this)) { Jvm.debug().on(getClass(), "Removed " + removedCompletely + " old deleted entries " + "in the segment " + segmentIndex); nextSegmentIndex = nextSegmentIndex(segmentIndex); map.globalMutableState().setCurrentCleanupSegmentIndex(nextSegmentIndex); return nextSegmentIndex; } else { // forEachWhile returned false => interrupted => shutdown = true assert shutdown; return -1; } }
1,619
272
1,891
<methods>public void <init>() ,public void <init>(java.lang.Runnable) ,public void <init>(java.lang.String) ,public void <init>(java.lang.ThreadGroup, java.lang.Runnable) ,public void <init>(java.lang.ThreadGroup, java.lang.String) ,public void <init>(java.lang.Runnable, java.lang.String) ,public void <init>(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String) ,public void <init>(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String, long) ,public void <init>(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String, long, boolean) ,public static int activeCount() ,public final void checkAccess() ,public int countStackFrames() ,public static native java.lang.Thread currentThread() ,public static void dumpStack() ,public static int enumerate(java.lang.Thread[]) ,public static Map<java.lang.Thread,java.lang.StackTraceElement[]> getAllStackTraces() ,public java.lang.ClassLoader getContextClassLoader() ,public static java.lang.Thread.UncaughtExceptionHandler getDefaultUncaughtExceptionHandler() ,public long getId() ,public final java.lang.String getName() ,public final int getPriority() ,public java.lang.StackTraceElement[] getStackTrace() ,public java.lang.Thread.State getState() ,public final java.lang.ThreadGroup getThreadGroup() ,public java.lang.Thread.UncaughtExceptionHandler getUncaughtExceptionHandler() ,public static native boolean holdsLock(java.lang.Object) ,public void interrupt() ,public static boolean interrupted() ,public final boolean isAlive() ,public final boolean isDaemon() ,public boolean isInterrupted() ,public final void join() throws java.lang.InterruptedException,public final synchronized void join(long) throws java.lang.InterruptedException,public final synchronized void join(long, int) throws java.lang.InterruptedException,public static void onSpinWait() ,public final void resume() ,public void run() ,public void setContextClassLoader(java.lang.ClassLoader) ,public final void setDaemon(boolean) ,public static void setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) ,public final synchronized void setName(java.lang.String) ,public final void setPriority(int) ,public void setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) ,public static native void sleep(long) throws java.lang.InterruptedException,public static void sleep(long, int) throws java.lang.InterruptedException,public synchronized void start() ,public final void stop() ,public final void suspend() ,public java.lang.String toString() ,public static native void yield() <variables>private static final java.lang.StackTraceElement[] EMPTY_STACK_TRACE,public static final int MAX_PRIORITY,public static final int MIN_PRIORITY,public static final int NORM_PRIORITY,private volatile sun.nio.ch.Interruptible blocker,private final java.lang.Object blockerLock,private java.lang.ClassLoader contextClassLoader,private boolean daemon,private static volatile java.lang.Thread.UncaughtExceptionHandler defaultUncaughtExceptionHandler,private volatile long eetop,private java.lang.ThreadGroup group,java.lang.ThreadLocal.ThreadLocalMap inheritableThreadLocals,private java.security.AccessControlContext inheritedAccessControlContext,private volatile boolean interrupted,private volatile java.lang.String name,volatile java.lang.Object parkBlocker,private int priority,private final long stackSize,private boolean stillborn,private java.lang.Runnable target,private static int threadInitNumber,int threadLocalRandomProbe,int threadLocalRandomSecondarySeed,long threadLocalRandomSeed,java.lang.ThreadLocal.ThreadLocalMap threadLocals,private static long threadSeqNumber,private volatile int threadStatus,private final long tid,private volatile java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/SelectedSelectionKeySet.java
SelectedSelectionKeySet
flip
class SelectedSelectionKeySet extends AbstractSet<SelectionKey> { private SelectionKey[] keysA; private int keysASize; private SelectionKey[] keysB; private int keysBSize; private boolean isA = true; SelectedSelectionKeySet() { keysA = new SelectionKey[1024]; keysB = keysA.clone(); } @Override public boolean add(SelectionKey o) { if (o == null) { return false; } if (isA) { int size = keysASize; keysA[size++] = o; keysASize = size; if (size == keysA.length) { doubleCapacityA(); } } else { int size = keysBSize; keysB[size++] = o; keysBSize = size; if (size == keysB.length) { doubleCapacityB(); } } return true; } private void doubleCapacityA() { SelectionKey[] newKeysA = new SelectionKey[keysA.length << 1]; System.arraycopy(keysA, 0, newKeysA, 0, keysASize); keysA = newKeysA; } private void doubleCapacityB() { SelectionKey[] newKeysB = new SelectionKey[keysB.length << 1]; System.arraycopy(keysB, 0, newKeysB, 0, keysBSize); keysB = newKeysB; } SelectionKey[] flip() {<FILL_FUNCTION_BODY>} @Override public int size() { if (isA) { return keysASize; } else { return keysBSize; } } @Override public boolean remove(Object o) { return false; } @Override public boolean contains(Object o) { return false; } @NotNull @Override public Iterator<SelectionKey> iterator() { throw new UnsupportedOperationException(); } }
if (isA) { isA = false; keysA[keysASize] = null; keysBSize = 0; return keysA; } else { isA = true; keysB[keysBSize] = null; keysASize = 0; return keysB; }
545
85
630
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public boolean removeAll(Collection<?>) <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/channel/MapHandler.java
MapHandler
createMapHandler
class MapHandler<VALUE, REPLY> extends AbstractHandler<MapHandler<VALUE, REPLY>> { protected MapService<VALUE, REPLY> mapService; private String mapName; protected MapHandler(String mapName) { this.mapName = mapName; } public static <V, O> MapHandler<V, O> createMapHandler(String mapName, MapService<V, O> mapService) {<FILL_FUNCTION_BODY>} @Override public void run(ChronicleContext context, ChronicleChannel channel) { try (ChronicleMap<Bytes<?>, VALUE> map = MapChannel.createMap(mapName, mapService, context)) { REPLY REPLY = channel.methodWriter(mapService().replyClass()); mapService.map(map); mapService.reply(REPLY); try (AffinityLock lock = context.affinityLock()) { channel.eventHandlerAsRunnable(mapService).run(); } } catch (IOException ioe) { throw Jvm.rethrow(ioe); } } @Override public ChronicleChannel asInternalChannel(ChronicleContext context, ChronicleChannelCfg channelCfg) { return new MapChannel(mapName, mapService, context, channelCfg); } protected MapService<VALUE, REPLY> mapService() { return mapService; } }
MapHandler<V, O> mh = new MapHandler<>(mapName); mh.mapService = mapService; return mh;
365
40
405
<methods>public void <init>() ,public java.lang.Boolean buffered() ,public MapHandler<VALUE,REPLY> buffered(java.lang.Boolean) <variables>private java.lang.Boolean buffered
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/channel/internal/MapChannel.java
MapChannel
createMap
class MapChannel<VALUE, REPLY> extends SimpleCloseable implements ChronicleChannel { private static final OkHeader OK = new OkHeader(); private final String mapName; private final MapService<VALUE, REPLY> mapService; private final ChronicleChannelCfg channelCfg; private final ChronicleMap<Bytes<?>, VALUE> map; // TODO FIX this runs out of memory. private final Wire replyWire = WireType.BINARY_LIGHT.apply(Bytes.allocateElasticOnHeap()); public MapChannel(String mapName, MapService<VALUE, REPLY> mapService, ChronicleContext context, ChronicleChannelCfg channelCfg) { this.mapName = mapName; this.mapService = mapService; this.channelCfg = channelCfg; try { map = createMap(mapName, mapService, context); mapService.map(map); REPLY reply = replyWire.methodWriter(mapService.replyClass()); mapService.reply(reply); } catch (IOException e) { throw new IORuntimeException(e); } } public static <VALUE, REPLY> ChronicleMap<Bytes<?>, VALUE> createMap(String mapName, MapService<VALUE, REPLY> mapService, ChronicleContext context) throws IOException {<FILL_FUNCTION_BODY>} @Override public ChronicleChannelCfg channelCfg() { return channelCfg; } @Override public ChannelHeader headerOut() { return OK; } @Override public ChannelHeader headerIn() { return OK; } @Override public void testMessage(long now) { throw new UnsupportedOperationException(); } @Override public long lastTestMessage() { throw new UnsupportedOperationException(); } @Override public @NotNull DocumentContext readingDocument() { return replyWire.readingDocument(); } @Override public <T> @NotNull T methodWriter(@NotNull Class<T> tClass, Class... additional) { // if the class doesn't match it throws a ClassCastException' return (T) mapService; } @Override public DocumentContext writingDocument(boolean metaData) throws UnrecoverableTimeoutException { throw new UnsupportedOperationException(); } @Override public DocumentContext acquireWritingDocument(boolean metaData) throws UnrecoverableTimeoutException { throw new UnsupportedOperationException(); } }
// assume it has to already exist, but if not take a guess on sizes final Class<VALUE> valueClass = mapService.valueClass(); final Class<Bytes<?>> bytesClass = (Class) Bytes.class; final ChronicleMapBuilder<Bytes<?>, VALUE> builder = ChronicleMap.of(bytesClass, valueClass) .keyMarshaller(new BytesSizedMarshaller()) .averageKeySize(32) .averageValueSize(256) .entries(1000000) .putReturnsNull(true) .removeReturnsNull(true); if (BytesMarshallable.class.isAssignableFrom(valueClass)) { //noinspection unchecked,rawtypes builder.valueMarshaller(new BytesMarshallableReaderWriter<>((Class) valueClass)); } else if (Marshallable.class.isAssignableFrom(valueClass)) { //noinspection unchecked,rawtypes builder.valueMarshaller(new MarshallableReaderWriter<>((Class) valueClass)); } return builder .createPersistedTo(context.toFile(mapName + ".cm3"));
645
296
941
<methods>public final void close() ,public boolean isClosed() <variables>private volatile transient boolean closed
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/data/DummyValueZeroData.java
DummyValueZeroData
getUsing
class DummyValueZeroData<V> extends AbstractData<V> { private final Bytes zeroBytes = ZeroBytesStore.INSTANCE.bytesForRead(); @StageRef VanillaChronicleMapHolder<?, ?, ?> mh; @StageRef ValueBytesInterop<V> vi; @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @Override public RandomDataInput bytes() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return ZeroBytesStore.INSTANCE; } @Override public long offset() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return 0; } @Override public long size() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return Math.max(0, mh.m().valueSizeMarshaller.minStorableSize()); } @Override public V get() { checkOnEachPublicOperation.checkOnEachPublicOperation(); // Not optimized and creates garbage, because this isn't the primary // use case. Zero data should only be used in bytes form return getUsing(null); } @Override public V getUsing(V using) {<FILL_FUNCTION_BODY>} private IllegalStateException zeroReadException(Exception cause) { return new IllegalStateException(mh.h().toIdentityString() + ": Most probable cause of this exception - zero bytes of\n" + "the minimum positive encoding length, supported by the specified or default\n" + "valueSizeMarshaller() is not correct serialized form of any value. You should\n" + "configure defaultValueProvider() in ChronicleMapBuilder", cause); } }
checkOnEachPublicOperation.checkOnEachPublicOperation(); zeroBytes.readPosition(0); try { return vi.valueReader.read(zeroBytes, size(), using); } catch (Exception e) { throw zeroReadException(e); }
443
69
512
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/data/bytes/EntryValueBytesData.java
EntryValueBytesData
innerGetUsing
class EntryValueBytesData<V> extends AbstractData<V> { @StageRef VanillaChronicleMapHolder<?, V, ?> mh; @StageRef ValueBytesInterop<V> vi; @StageRef SegmentStages s; @StageRef MapEntryStages<?, V> entry; @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @Stage("CachedEntryValue") private V cachedEntryValue = mh.m().valueType() == CharSequence.class ? (V) new StringBuilder() : null; @Stage("CachedEntryValue") private boolean cachedEntryValueRead = false; private void initCachedEntryValue() { cachedEntryValue = innerGetUsing(cachedEntryValue); cachedEntryValueRead = true; } public boolean cachedEntryValueInit() { return cachedEntryValueRead; } public void closeCachedEntryValue() { cachedEntryValueRead = false; } @Override public RandomDataInput bytes() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return s.segmentBS; } @Override public long offset() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return entry.valueOffset; } @Override public long size() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return entry.valueSize; } @Override public V get() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return cachedEntryValue; } @Override public V getUsing(V using) { checkOnEachPublicOperation.checkOnEachPublicOperation(); return innerGetUsing(using); } private V innerGetUsing(V usingValue) {<FILL_FUNCTION_BODY>} }
Bytes segmentBytes = s.segmentBytesForRead(); segmentBytes.readPosition(entry.valueOffset); return vi.valueReader.read(segmentBytes, size(), usingValue);
482
50
532
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/data/bytes/WrappedValueBytesData.java
WrappedValueBytesData
getUnusedWrappedValueBytesData
class WrappedValueBytesData<V> extends AbstractData<V> { @Stage("WrappedValueBytes") private final VanillaBytes wrappedValueBytes = VanillaBytes.vanillaBytes(); @StageRef ValueBytesInterop<V> vi; @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; private WrappedValueBytesData<V> next; @Stage("WrappedValueBytesStore") private BytesStore wrappedValueBytesStore; @Stage("WrappedValueBytesStore") private long wrappedValueBytesOffset; @Stage("WrappedValueBytesStore") private long wrappedValueBytesSize; @Stage("WrappedValueBytes") private boolean wrappedValueBytesUsed = false; @Stage("CachedWrappedValue") private V cachedWrappedValue; @Stage("CachedWrappedValue") private boolean cachedWrappedValueRead = false; boolean nextInit() { return true; } void closeNext() { // do nothing } @Stage("Next") public WrappedValueBytesData<V> getUnusedWrappedValueBytesData() {<FILL_FUNCTION_BODY>} boolean wrappedValueBytesStoreInit() { return wrappedValueBytesStore != null; } public void initWrappedValueBytesStore(BytesStore bytesStore, long offset, long size) { wrappedValueBytesStore = bytesStore; wrappedValueBytesOffset = offset; wrappedValueBytesSize = size; } void closeWrappedValueBytesStore() { wrappedValueBytesStore = null; if (next != null) next.closeWrappedValueBytesStore(); } boolean wrappedValueBytesInit() { return wrappedValueBytesUsed; } void initWrappedValueBytes() { wrappedValueBytes.bytesStore( wrappedValueBytesStore, wrappedValueBytesOffset, wrappedValueBytesSize); wrappedValueBytesUsed = true; } void closeWrappedValueBytes() { wrappedValueBytes.bytesStore(BytesStore.empty(), 0, 0); wrappedValueBytesUsed = false; } private void initCachedWrappedValue() { cachedWrappedValue = innerGetUsing(cachedWrappedValue); cachedWrappedValueRead = true; } @Override public RandomDataInput bytes() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return wrappedValueBytes.bytesStore(); } @Override public long offset() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return wrappedValueBytesOffset; } @Override public long size() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return wrappedValueBytesSize; } @Override public V get() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return cachedWrappedValue; } @Override public V getUsing(V using) { checkOnEachPublicOperation.checkOnEachPublicOperation(); return innerGetUsing(using); } private V innerGetUsing(V usingValue) { wrappedValueBytes.readPosition(wrappedValueBytesOffset); return vi.valueReader.read(wrappedValueBytes, wrappedValueBytesSize, usingValue); } }
if (!wrappedValueBytesStoreInit()) return this; if (next == null) next = new WrappedValueBytesData<>(); return next.getUnusedWrappedValueBytesData();
834
53
887
<methods>public boolean equals(java.lang.Object) ,public int hashCode() ,public java.lang.String toString() <variables>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/data/instance/WrappedValueInstanceDataHolder.java
WrappedValueInstanceDataHolder
getUnusedWrappedValueHolder
class WrappedValueInstanceDataHolder<V> { public Data<V> wrappedData = null; @StageRef VanillaChronicleMapHolder<?, V, ?> mh; private final DataAccess<V> wrappedValueDataAccess = mh.m().valueDataAccess.copy(); private WrappedValueInstanceDataHolder<V> next; private V value; boolean nextInit() { return true; } void closeNext() { // do nothing } @Stage("Next") public WrappedValueInstanceDataHolder<V> getUnusedWrappedValueHolder() {<FILL_FUNCTION_BODY>} public boolean valueInit() { return value != null; } public void initValue(V value) { mh.m().checkValue(value); this.value = value; } public void closeValue() { value = null; if (next != null) next.closeValue(); } private void initWrappedData() { wrappedData = wrappedValueDataAccess.getData(value); } private void closeWrappedData() { wrappedData = null; wrappedValueDataAccess.uninit(); } }
if (!valueInit()) return this; if (next == null) next = new WrappedValueInstanceDataHolder<>(); return next.getUnusedWrappedValueHolder();
319
49
368
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/entry/ReplicatedMapEntryStages.java
ReplicatedMapEntryStages
updatedReplicationStateOnPresentEntry
class ReplicatedMapEntryStages<K, V> extends MapEntryStages<K, V> implements MapReplicableEntry<K, V> { @StageRef ReplicatedChronicleMapHolder<?, ?, ?> mh; @StageRef ReplicationUpdate ru; @Stage("ReplicationState") long replicationBytesOffset = -1; void initReplicationState() { replicationBytesOffset = keyEnd(); } void updateReplicationState(byte identifier, long timestamp) { initDelayedUpdateChecksum(true); Bytes segmentBytes = s.segmentBytesForWrite(); segmentBytes.writePosition(replicationBytesOffset); segmentBytes.writeLong(timestamp); segmentBytes.writeByte(identifier); } private long timestampOffset() { return replicationBytesOffset; } public long timestamp() { return s.segmentBS.readLong(replicationBytesOffset); } private long identifierOffset() { return replicationBytesOffset + 8L; } byte identifier() { return s.segmentBS.readByte(identifierOffset()); } private long entryDeletedOffset() { return replicationBytesOffset + 9L; } @Override public boolean entryDeleted() { return s.segmentBS.readBoolean(entryDeletedOffset()); } public void writeEntryPresent() { s.segmentBS.writeBoolean(entryDeletedOffset(), false); } public void writeEntryDeleted() { s.segmentBS.writeBoolean(entryDeletedOffset(), true); } @Override public byte originIdentifier() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return identifier(); } @Override public long originTimestamp() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return timestamp(); } @Override long countValueSizeOffset() { return super.countValueSizeOffset() + ADDITIONAL_ENTRY_BYTES; } @Override public void updateOrigin(byte newIdentifier, long newTimestamp) { checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerWriteLock.lock(); updateReplicationState(newIdentifier, newTimestamp); } @Override public void dropChanged() { checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerUpdateLock.lock(); ru.dropChange(); } @Override public void dropChangedFor(byte remoteIdentifier) { checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerUpdateLock.lock(); ru.dropChangeFor(remoteIdentifier); } @Override public void raiseChanged() { checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerUpdateLock.lock(); ru.raiseChange(); } @Override public void raiseChangedFor(byte remoteIdentifier) { checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerUpdateLock.lock(); ru.raiseChangeFor(remoteIdentifier); } @Override public void raiseChangedForAllExcept(byte remoteIdentifier) { checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerUpdateLock.lock(); ru.raiseChangeForAllExcept(remoteIdentifier); } @Override public boolean isChanged() { checkOnEachPublicOperation.checkOnEachPublicOperation(); s.innerReadLock.lock(); return ru.changed(); } public void updatedReplicationStateOnPresentEntry() {<FILL_FUNCTION_BODY>} public void updatedReplicationStateOnAbsentEntry() { if (!ru.replicationUpdateInit()) { s.innerWriteLock.lock(); updateReplicationState(mh.m().identifier(), currentTime()); } } @Override protected void relocation(Data<V> newValue, long newEntrySize) { long oldPos = pos; long oldTierIndex = s.tierIndex; super.relocation(newValue, newEntrySize); ru.moveChange(oldTierIndex, oldPos, pos); } @Override long sizeOfEverythingBeforeValue(long keySize, long valueSize) { return super.sizeOfEverythingBeforeValue(keySize, valueSize) + ADDITIONAL_ENTRY_BYTES; } }
if (!ru.replicationUpdateInit()) { s.innerWriteLock.lock(); long timestamp = Math.max(timestamp() + 1, currentTime()); updateReplicationState(mh.m().identifier(), timestamp); }
1,123
61
1,184
<methods>public non-sealed void <init>() ,public boolean entryDeleted() ,public long entryEnd() ,public final long entrySize(long, long) ,public final void freeExtraAllocatedChunks() ,public void initValue(Data<?>) ,public void innerDefaultReplaceValue(Data<V>) ,public long innerEntrySize(long, long) ,public long newEntrySize(Data<V>, long, long) ,public long newSizeOfEverythingBeforeValue(Data<V>) ,public Data<V> value() ,public void writeValue(Data<?>) <variables>public net.openhft.chronicle.hash.impl.stage.entry.AllocatedChunks allocatedChunks,public EntryValueBytesData<V> entryValue,KeySearch<K> ks,public VanillaChronicleMapHolder<?,?,?> mh,public long valueOffset,public long valueSize,public long valueSizeOffset
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/input/ReplicatedInput.java
ReplicatedInput
processReplicatedEvent
class ReplicatedInput<K, V, R> implements RemoteOperationContext<K>, MapRemoteQueryContext<K, V, R>, Replica.QueryContext<K, V> { @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef ReplicatedChronicleMapHolder<K, V, R> mh; @StageRef ReplicationUpdate<K> ru; @StageRef ReplicatedMapQuery<K, V, ?> q; @StageRef SegmentStages s; @StageRef DummyValueZeroData<V> dummyValue; @Override public Data<V> dummyZeroValue() { checkOnEachPublicOperation.checkOnEachPublicOperation(); return dummyValue; } public void processReplicatedEvent(byte remoteNodeIdentifier, Bytes replicatedInputBytes) {<FILL_FUNCTION_BODY>} @Override public void remotePut( Data<V> newValue, byte remoteEntryIdentifier, long remoteEntryTimestamp, byte remoteNodeIdentifier) { ru.initReplicationUpdate(remoteEntryIdentifier, remoteEntryTimestamp, remoteNodeIdentifier); s.innerUpdateLock.lock(); mh.m().remoteOperations.put(this, newValue); } @Override public void remoteRemove( byte remoteEntryIdentifier, long remoteEntryTimestamp, byte remoteNodeIdentifier) { ru.initReplicationUpdate(remoteEntryIdentifier, remoteEntryTimestamp, remoteNodeIdentifier); s.innerWriteLock.lock(); mh.m().remoteOperations.remove(this); } }
long timestamp = replicatedInputBytes.readStopBit(); byte identifier = replicatedInputBytes.readByte(); ru.initReplicationUpdate(identifier, timestamp, remoteNodeIdentifier); boolean isDeleted = replicatedInputBytes.readBoolean(); long keySize = mh.m().keySizeMarshaller.readSize(replicatedInputBytes); long keyOffset = replicatedInputBytes.readPosition(); q.initInputKey(q.getInputKeyBytesAsData(replicatedInputBytes, keyOffset, keySize)); replicatedInputBytes.readSkip(keySize); if (isDeleted) { s.innerUpdateLock.lock(); mh.m().remoteOperations.remove(this); } else { long valueSize = mh.m().valueSizeMarshaller.readSize(replicatedInputBytes); long valueOffset = replicatedInputBytes.readPosition(); Data<V> value = q.wrapValueBytesAsData(replicatedInputBytes, valueOffset, valueSize); replicatedInputBytes.readSkip(valueSize); s.innerWriteLock.lock(); mh.m().remoteOperations.put(this, value); }
414
296
710
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/iter/MapSegmentIteration.java
MapSegmentIteration
doReplaceValue
class MapSegmentIteration<K, V, R> extends HashSegmentIteration<K, MapEntry<K, V>> implements MapEntry<K, V>, IterationContext<K, V, R> { @StageRef MapEntryStages<K, V> entry; @StageRef WrappedValueInstanceDataHolder<V> wrappedValueInstanceDataHolder; @StageRef WrappedValueInstanceDataHolderAccess<K, V, ?> wrappedValueInstanceDataHolderAccess; @Override public void hookAfterEachIteration() { throwExceptionIfClosed(); wrappedValueInstanceDataHolder.closeValue(); } @Override public void doReplaceValue(Data<V> newValue) {<FILL_FUNCTION_BODY>} @NotNull @Override public WrappedValueInstanceDataHolderAccess<K, V, ?> context() { return wrappedValueInstanceDataHolderAccess; } }
throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); try { entry.innerDefaultReplaceValue(newValue); } finally { s.innerWriteLock.unlock(); }
238
60
298
<methods>public non-sealed void <init>() ,public void checkEntryNotRemovedOnThisIteration() ,public void doRemove() ,public java.lang.Object entryForIteration() ,public void forEachSegmentEntry(Consumer<? super MapEntry<K,V>>) ,public boolean forEachSegmentEntryWhile(Predicate<? super MapEntry<K,V>>) ,public boolean forEachTierEntryWhile(Predicate<? super T>, int, long, long) ,public abstract boolean hashLookupEntryInit() ,public void hookAfterEachIteration() ,public void initHashLookupEntry(long) ,public boolean innerForEachSegmentEntryWhile(Predicate<? super T>) ,public void iterationRemove() ,public boolean shouldTestEntry() ,public long tierEntriesForIteration() <variables>public net.openhft.chronicle.hash.impl.stage.hash.CheckOnEachPublicOperation checkOnEachPublicOperation,HashEntryStages<K> e,public boolean entryRemovedOnThisIteration,public long hashLookupEntry,VanillaChronicleHashHolder<?> hh,protected net.openhft.chronicle.hash.impl.stage.entry.HashLookupPos hlp,public net.openhft.chronicle.hash.impl.stage.iter.IterationSegmentStages s
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedMapSegmentIteration.java
ReplicatedMapSegmentIteration
doInsert
class ReplicatedMapSegmentIteration<K, V, R> extends MapSegmentIteration<K, V, R> implements ReplicatedIterationContext<K, V, R>, ReplicableEntry, ReplicatedHashSegmentContext<K, MapEntry<K, V>> { @StageRef VanillaChronicleMapHolder<K, V, R> mh; @StageRef ReplicatedMapEntryStages<K, V> e; @StageRef ReplicationUpdate<K> ru; @StageRef DummyValueZeroData<V> dummyValue; @StageRef ReplicatedMapAbsentDelegatingForIteration<K, V> absentEntryDelegating; @StageRef ReplicatedMapEntryDelegating<K, V> entryDelegating; EntriesToTest entriesToTest = null; void initEntriesToTest(EntriesToTest entriesToTest) { this.entriesToTest = entriesToTest; } @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<V> newValue) { throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); try { entry.innerDefaultReplaceValue(newValue); e.updatedReplicationStateOnPresentEntry(); ru.updateChange(); } finally { s.innerWriteLock.unlock(); } } @Override public boolean forEachSegmentEntryWhile(Predicate<? super MapEntry<K, V>> predicate) { throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); initEntriesToTest(PRESENT); s.innerUpdateLock.lock(); return innerForEachSegmentEntryWhile(predicate); } @Override public boolean forEachSegmentReplicableEntryWhile( Predicate<? super ReplicableEntry> predicate) { throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); initEntriesToTest(ALL); s.innerUpdateLock.lock(); return innerForEachSegmentEntryWhile(predicate); } @Override public void forEachSegmentReplicableEntry(Consumer<? super ReplicableEntry> action) { throwExceptionIfClosed(); forEachSegmentReplicableEntryWhile(e -> { action.accept(e); return true; }); } @Override public void doRemove() { throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); try { if (e.valueSize > dummyValue.size()) e.innerDefaultReplaceValue(dummyValue); e.updatedReplicationStateOnPresentEntry(); e.writeEntryDeleted(); ru.updateChange(); s.tierDeleted(s.tierDeleted() + 1); } finally { s.innerWriteLock.unlock(); } initEntryRemovedOnThisIteration(true); } @Override public void doRemoveCompletely() { throwExceptionIfClosed(); boolean wasDeleted = e.entryDeleted(); super.doRemove(); ru.dropChange(); if (wasDeleted) s.tierDeleted(s.tierDeleted() - 1); } public void doInsert(Data<V> value) { throwExceptionIfClosed(); checkOnEachPublicOperation.checkOnEachPublicOperation(); if (e.entryDeleted()) { try { s.tierDeleted(s.tierDeleted() - 1); e.innerDefaultReplaceValue(value); s.incrementModCount(); e.writeEntryPresent(); e.updatedReplicationStateOnPresentEntry(); ru.updateChange(); } finally { s.innerWriteLock.unlock(); } } else { throw new IllegalStateException(mh.h().toIdentityString() + ": Entry is present in the map when doInsert() is called"); } } public void doInsert() {<FILL_FUNCTION_BODY>} enum EntriesToTest {PRESENT, ALL} }
throwExceptionIfClosed(); if (mh.set() == null) throw new IllegalStateException(mh.h().toIdentityString() + ": Called SetAbsentEntry.doInsert() from Map context"); doInsert((Data<V>) DummyValueData.INSTANCE);
1,219
78
1,297
<methods>public non-sealed void <init>() ,public WrappedValueInstanceDataHolderAccess<K,V,?> context() ,public void doReplaceValue(Data<V>) ,public void hookAfterEachIteration() <variables>MapEntryStages<K,V> entry,WrappedValueInstanceDataHolder<V> wrappedValueInstanceDataHolder,WrappedValueInstanceDataHolderAccess<K,V,?> wrappedValueInstanceDataHolderAccess
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/iter/ReplicatedTierRecovery.java
ReplicatedTierRecovery
cleanupModificationIterationBits
class ReplicatedTierRecovery extends TierRecovery { @StageRef ReplicatedChronicleMapHolder<?, ?, ?> rh; @StageRef SegmentStages s; @StageRef ReplicatedMapEntryStages<?, ?> e; @Override public void removeDuplicatesInSegment( ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption) { super.removeDuplicatesInSegment(corruptionListener, corruption); recoverTierDeleted(corruptionListener, corruption); cleanupModificationIterationBits(); } private void recoverTierDeleted( ChronicleHashCorruption.Listener corruptionListener, ChronicleHashCorruptionImpl corruption) { VanillaChronicleHash<?, ?, ?, ?> h = rh.h(); CompactOffHeapLinearHashTable hl = h.hashLookup; long hlAddr = s.tierBaseAddr; long deleted = 0; long hlPos = 0; do { long hlEntry = hl.readEntry(hlAddr, hlPos); if (!hl.empty(hlEntry)) { e.readExistingEntry(hl.value(hlEntry)); if (e.entryDeleted()) { deleted++; } } hlPos = hl.step(hlPos); } while (hlPos != 0); if (s.tierDeleted() != deleted) { long finalDeleted = deleted; report(corruptionListener, corruption, s.segmentIndex, () -> format("wrong deleted counter for tier with index {}, stored: {}, should be: {}", s.tierIndex, s.tierDeleted(), finalDeleted) ); s.tierDeleted(deleted); } } private void cleanupModificationIterationBits() {<FILL_FUNCTION_BODY>} }
ReplicatedChronicleMap<?, ?, ?> m = rh.m(); ReplicatedChronicleMap<?, ?, ?>.ModificationIterator[] its = m.acquireAllModificationIterators(); ReusableBitSet freeList = s.freeList; for (long pos = 0; pos < m.actualChunksPerSegmentTier; ) { long nextPos = freeList.nextSetBit(pos); if (nextPos > pos) { for (ReplicatedChronicleMap<?, ?, ?>.ModificationIterator it : its) { it.clearRange0(s.tierIndex, pos, nextPos); } } if (nextPos > 0) { e.readExistingEntry(nextPos); if (e.entrySizeInChunks > 1) { for (ReplicatedChronicleMap<?, ?, ?>.ModificationIterator it : its) { it.clearRange0(s.tierIndex, nextPos + 1, nextPos + e.entrySizeInChunks); } } pos = nextPos + e.entrySizeInChunks; } else { for (ReplicatedChronicleMap<?, ?, ?>.ModificationIterator it : its) { it.clearRange0(s.tierIndex, pos, m.actualChunksPerSegmentTier); } break; } }
506
356
862
<methods>public non-sealed void <init>() ,public int recoverTier(int, net.openhft.chronicle.hash.ChronicleHashCorruption.Listener, net.openhft.chronicle.map.ChronicleHashCorruptionImpl) ,public void removeDuplicatesInSegment(net.openhft.chronicle.hash.ChronicleHashCorruption.Listener, net.openhft.chronicle.map.ChronicleHashCorruptionImpl) <variables>MapEntryStages<?,?> e,net.openhft.chronicle.hash.impl.stage.iter.IterationKeyHashCode khc,VanillaChronicleMapHolder<?,?,?> mh,net.openhft.chronicle.hash.impl.stage.entry.SegmentStages s
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/map/ReplicatedChronicleMapHolderImpl.java
ReplicatedChronicleMapHolderImpl
initMap
class ReplicatedChronicleMapHolderImpl<K, V, R> extends Chaining implements ReplicatedChronicleMapHolder<K, V, R> { @Stage("Map") private ReplicatedChronicleMap<K, V, R> m = null; public ReplicatedChronicleMapHolderImpl(VanillaChronicleMap map) { super(map); } public ReplicatedChronicleMapHolderImpl( ChainingInterface rootContextInThisThread, VanillaChronicleMap map) { super(rootContextInThisThread, map); } @Override public void initMap(VanillaChronicleMap map) {<FILL_FUNCTION_BODY>} @Override public ReplicatedChronicleMap<K, V, R> m() { return m; } @Override public VanillaChronicleHash<K, ?, ?, ?> h() { return m; } @Override public ChronicleMap<K, V> map() { return m; } @Override public ChronicleSet<K> set() { return m.chronicleSet; } public ChronicleHash<K, ?, ?, ?> hash() { return set() != null ? set() : map(); } }
// alternative to this "unsafe" casting approach is proper generalization // of Chaining/ChainingInterface, but this causes issues with current version // of stage-compiler. // TODO generalize Chaining with <M extends VanillaCM> when stage-compiler is improved. //noinspection unchecked m = (ReplicatedChronicleMap<K, V, R>) map;
350
97
447
<methods>public void <init>(VanillaChronicleMap#RAW) ,public void <init>(net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface, VanillaChronicleMap#RAW) ,public T contextAtIndexInChain(int) ,public T getContext(Class<? extends T>, BiFunction<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface,VanillaChronicleMap#RAW,T>, VanillaChronicleMap#RAW) ,public List<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface> getContextChain() ,public abstract void initMap(VanillaChronicleMap#RAW) ,public void initUsed(boolean, VanillaChronicleMap#RAW) ,public boolean usedInit() <variables>public final non-sealed List<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface> contextChain,private boolean firstContextLockedInThisThread,public final non-sealed int indexInContextChain,public final non-sealed net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface rootContextInThisThread,public boolean used
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/map/VanillaChronicleMapHolderImpl.java
VanillaChronicleMapHolderImpl
initMap
class VanillaChronicleMapHolderImpl<K, V, R> extends Chaining implements VanillaChronicleMapHolder<K, V, R> { @Stage("Map") private VanillaChronicleMap<K, V, R> m = null; public VanillaChronicleMapHolderImpl(VanillaChronicleMap map) { super(map); } public VanillaChronicleMapHolderImpl( ChainingInterface rootContextInThisThread, VanillaChronicleMap map) { super(rootContextInThisThread, map); } @Override public void initMap(VanillaChronicleMap map) {<FILL_FUNCTION_BODY>} @Override public VanillaChronicleMap<K, V, R> m() { return m; } @Override public VanillaChronicleHash<K, ?, ?, ?> h() { return m; } @Override public ChronicleMap<K, V> map() { return m; } @Override public ChronicleSet<K> set() { return m.chronicleSet; } public ChronicleHash<K, ?, ?, ?> hash() { return set() != null ? set() : map(); } }
// alternative to this "unsafe" casting approach is proper generalization // of Chaining/ChainingInterface, but this causes issues with current version // of stage-compiler. // TODO generalize Chaining with <M extends VanillaCM> when stage-compiler is improved. //noinspection unchecked m = map;
344
81
425
<methods>public void <init>(VanillaChronicleMap#RAW) ,public void <init>(net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface, VanillaChronicleMap#RAW) ,public T contextAtIndexInChain(int) ,public T getContext(Class<? extends T>, BiFunction<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface,VanillaChronicleMap#RAW,T>, VanillaChronicleMap#RAW) ,public List<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface> getContextChain() ,public abstract void initMap(VanillaChronicleMap#RAW) ,public void initUsed(boolean, VanillaChronicleMap#RAW) ,public boolean usedInit() <variables>public final non-sealed List<net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface> contextChain,private boolean firstContextLockedInThisThread,public final non-sealed int indexInContextChain,public final non-sealed net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface rootContextInThisThread,public boolean used
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/map/WrappedValueBytesDataAccess.java
WrappedValueBytesDataAccess
wrapValueBytesAsData
class WrappedValueBytesDataAccess<K, V, R> implements MapContext<K, V, R> { @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef WrappedValueBytesData<V> wrappedValueBytesData; @Override public Data<V> wrapValueBytesAsData(BytesStore bytesStore, long offset, long size) {<FILL_FUNCTION_BODY>} }
Objects.requireNonNull(bytesStore); checkOnEachPublicOperation.checkOnEachPublicOperation(); WrappedValueBytesData<V> wrapped = this.wrappedValueBytesData; wrapped = wrapped.getUnusedWrappedValueBytesData(); wrapped.initWrappedValueBytesStore(bytesStore, offset, size); return wrapped;
111
87
198
<no_super_class>
OpenHFT_Chronicle-Map
Chronicle-Map/src/main/java/net/openhft/chronicle/map/impl/stage/map/WrappedValueInstanceDataHolderAccess.java
WrappedValueInstanceDataHolderAccess
wrapValueAsData
class WrappedValueInstanceDataHolderAccess<K, V, R> implements MapContext<K, V, R>, SetContext<K, R> { @StageRef CheckOnEachPublicOperation checkOnEachPublicOperation; @StageRef WrappedValueInstanceDataHolder<V> wrappedValueInstanceDataHolder; @Override public Data<V> wrapValueAsData(V value) {<FILL_FUNCTION_BODY>} }
checkOnEachPublicOperation.checkOnEachPublicOperation(); WrappedValueInstanceDataHolder<V> wrapped = this.wrappedValueInstanceDataHolder; wrapped = wrapped.getUnusedWrappedValueHolder(); wrapped.initValue(value); return wrapped.wrappedData;
115
71
186
<no_super_class>