public VanillaMappedBytes put(T key, File path, long size, long index) throws IOException { VanillaMappedBytes data = this.cache.get(key); if (data != null) { if (!data.unmapped()) { data.cleanup(); throw new IllegalStateException( "Buffer at " + data.index() + " has a count of " + +data.refCount() ); } } data = VanillaMappedFile.readWriteBytes(path, size, index, fileLifecycleListener); this.cache.put(key, data); return data; }
public static long append(final VanillaMappedBytes bytes, final long indexValue, final boolean synchronous) { // Position can be changed by another thread, so take a snapshot each loop // so that buffer overflows are not generated when advancing to the next // position. As a result, the position could step backwards when this method // is called concurrently, but the compareAndSwapLong call ensures that // data is never overwritten. if (bytes != null) { boolean endOfFile = false; long position = bytes.position(); while (!endOfFile) { endOfFile = (bytes.limit() - position) < 8; if (!endOfFile) { if (bytes.compareAndSwapLong(position, 0L, indexValue)) { bytes.lazyPosition(position + 8); if (synchronous) { bytes.force(); } return position; } } position += 8; } } return -1; }
int offset = (int) (startAddr - dataBytes.address()); long dataOffset = dataBytes.index() * builder.dataBlockSize() + offset; long indexValue = ((long) lastThreadId << INDEX_DATA_OFFSET_BITS) + dataOffset; if (position < 0) { if (indexBytes != null) { lastIndexIndex = (int) indexBytes.index() + 1; indexBytes.release(); indexBytes = null; lastIndexIndex = (int) indexBytes.index(); setLastWrittenIndex(appenderCycle, indexBytes.index(), positionArr[0]); setLastWrittenIndex(appenderCycle, indexBytes.index(), position); dataBytes.positionAddr(positionAddr); dataBytes.alignPositionAddr(4); dataBytes.force();
if (lastCycle != cycle || lastIndexCount != indexCount || indexBytes == null) { if (indexBytes != null) { indexBytes.release(); indexBytes = null; dataBytes.release(); dataBytes = null; assert indexBytes.refCount() > 1; lastCycle = cycle; lastIndexCount = indexCount; indexValue = indexBytes.readVolatileLong(indexOffset << 3); } catch (FileNotFoundException e) { return false; if (lastThreadId != threadId || lastDataCount != dataCount || indexFileChange) { if (dataBytes != null) { dataBytes.release(); dataBytes = null; int len = dataBytes.readVolatileInt(dataOffset - 4); if (len == 0) return false; startAddr = positionAddr = dataBytes.startAddr() + dataOffset; limitAddr = startAddr + ~len; index = nextIndex;
protected VanillaMappedBytes acquire0(long index) throws IOException { if (this.mb1 != null) { this.mb1.release(); } this.mb1 = this.mb0; this.mb0 = this.mappedFile.bytes(index * this.blockSize, this.blockSize, index); this.mb0.reserve(); bytes.add(this.mb0); for (int i = bytes.size() - 1; i >= 0; i--) { if (bytes.get(i).unmapped()) { bytes.remove(i); } } return this.mb0; }
@Override public void close() { final Iterator<Map.Entry<T, VanillaMappedBytes>> it = this.cache.entrySet().iterator(); while (it.hasNext()) { Map.Entry<T, VanillaMappedBytes> entry = it.next(); entry.getValue().release(); if (this.cleanOnClose && !entry.getValue().unmapped()) { entry.getValue().cleanup(); entry.getValue().close(); it.remove(); } else if (entry.getValue().unmapped()) { entry.getValue().close(); it.remove(); } } this.cache.clear(); }
@Override public synchronized void close() throws IOException { if (this.mb0 != null && !this.mb0.unmapped()) { this.mb0.release(); this.mb0 = null; } if (this.mb1 != null && !this.mb1.unmapped()) { this.mb1.release(); this.mb1 = null; } for (int i = bytes.size() - 1; i >= 0; i--) { bytes.get(i).cleanup(); } this.bytes.clear(); this.mappedFile.close(); }
if (block > 0 && mbb.readLong(0) == 0) { mbb.release(); continue; for (int pos = 0; pos < indexBlockSize; pos += cacheLineSize) { if (pos + cacheLineSize >= indexBlockSize || mbb.readLong(pos + cacheLineSize) == 0) { if (mbb.readInt(pos + pos2) == 0) { break; mbb.release(); return (block * indexBlockSize + pos) / cacheLineSize * (cacheLineSize / 4 - 2) + pos2 / 4 - 3; mbb.release(); return (block + 1) * indexBlockSize / cacheLineSize * (cacheLineSize / 4 - 2);
dataBytes.release(); dataBytes = null; indexBytes.release(); indexBytes = null; if (dataBytes.remaining() < capacity + 4) { dataBytes.release(); dataBytes = null; dataCount++; startAddr = positionAddr = dataBytes.positionAddr() + 4; limitAddr = startAddr + capacity; nextSynchronous = config.synchronous();
public synchronized VanillaMappedBytes dataFor(int cycle, int threadId, int dataCount, boolean forWrite) throws IOException { key.cycle = cycle; key.threadId = threadId; key.dataCount = dataCount; VanillaMappedBytes vmb = this.cache.get(key); if (vmb == null || vmb.refCount() < 1) { long start = System.nanoTime(); File file = VanillaChronicleUtils.mkFiles( dateCache.valueFor(cycle).path, FILE_NAME_PREFIX + threadId + "-" + dataCount, forWrite); vmb = this.cache.put(key.clone(), file, 1L << blockBits, dataCount); fileLifecycleListener.onEvent(EventType.NEW, file, System.nanoTime() - start); } vmb.reserve(); return vmb; }
private void findEndOfData(final VanillaMappedBytes buffer) { for (int i = 0, max = 1 << blockBits; i < max; i += 4) { int len = buffer.readInt(buffer.position()); if (len == 0) { return; } int len2 = nextWordAlignment(~len); if (len2 < 0) { throw new IllegalStateException("Corrupted length " + Integer.toHexString(len)); } buffer.position(buffer.position() + len2 + 4); } throw new AssertionError(); }