/** * Creates a new {@link RecyclingIntBlockAllocator} with a block size of * {@link IntBlockPool#INT_BLOCK_SIZE}, upper buffered docs limit of * {@link #DEFAULT_BUFFERED_BLOCKS} ({@value #DEFAULT_BUFFERED_BLOCKS}). * */ public RecyclingIntBlockAllocator() { this(IntBlockPool.INT_BLOCK_SIZE, 64, Counter.newCounter(false)); }
/** * Removes the given number of byte blocks from the buffer if possible. * * @param num * the number of byte blocks to remove * @return the number of actually removed buffers */ public int freeBlocks(int num) { assert num >= 0 : "free blocks must be >= 0 but was: "+ num; final int stop; final int count; if (num > freeBlocks) { stop = 0; count = freeBlocks; } else { stop = freeBlocks - num; count = num; } while (freeBlocks > stop) { freeByteBlocks[--freeBlocks] = null; } bytesUsed.addAndGet(-count*blockSize); assert bytesUsed.get() >= 0; return count; } }
/** * Creates a new {@link BytesRefHash} */ public BytesRefHash(ByteBlockPool pool, int capacity, BytesStartArray bytesStartArray) { hashSize = capacity; hashHalfSize = hashSize >> 1; hashMask = hashSize - 1; this.pool = pool; ids = new int[hashSize]; Arrays.fill(ids, -1); this.bytesStartArray = bytesStartArray; bytesStart = bytesStartArray.init(); bytesUsed = bytesStartArray.bytesUsed() == null? Counter.newCounter() : bytesStartArray.bytesUsed(); bytesUsed.addAndGet(hashSize * Integer.BYTES); }
@Override public int[] getIntBlock() { int[] b = new int[IntBlockPool.INT_BLOCK_SIZE]; bytesUsed.addAndGet(IntBlockPool.INT_BLOCK_SIZE * Integer.BYTES); return b; }
/** * @return the number of bytes currently allocated by this {@link Allocator} */ public long bytesUsed() { return bytesUsed.get(); }
Counter bufferBytesUsed = Counter.newCounter(); buffer = new BytesRefArray(bufferBytesUsed); while (true) { if (bufferBytesUsed.get() > ramBufferSize.bytes) { break;
@Override public byte[] getByteBlock() { if (freeBlocks == 0) { bytesUsed.addAndGet(blockSize); return new byte[blockSize]; } final byte[] b = freeByteBlocks[--freeBlocks]; freeByteBlocks[freeBlocks] = null; return b; }
/** * @return the number of bytes currently allocated by this {@link Allocator} */ public long bytesUsed() { return bytesUsed.get(); }
/** * acquires the basis generation for a new snapshot. Any translog generation above, and including, the returned generation * will not be deleted until the returned {@link Releasable} is closed. */ synchronized Releasable acquireTranslogGen(final long translogGen) { translogRefCounts.computeIfAbsent(translogGen, l -> Counter.newCounter(false)).addAndGet(1); final AtomicBoolean closed = new AtomicBoolean(); assert assertAddTranslogRef(closed); return () -> { if (closed.compareAndSet(false, true)) { releaseTranslogGen(translogGen); assert assertRemoveTranslogRef(closed); } }; }
/** * Creates a new {@link RecyclingByteBlockAllocator}. * * @param blockSize * the block size in bytes * @param maxBufferedBlocks * maximum number of buffered byte block */ public RecyclingByteBlockAllocator(int blockSize, int maxBufferedBlocks) { this(blockSize, maxBufferedBlocks, Counter.newCounter(false)); }
@Override public int[] getIntBlock() { if (freeBlocks == 0) { bytesUsed.addAndGet(blockSize*Integer.BYTES); return new int[blockSize]; } final int[] b = freeByteBlocks[--freeBlocks]; freeByteBlocks[freeBlocks] = null; return b; }
/** * Removes the given number of int blocks from the buffer if possible. * * @param num * the number of int blocks to remove * @return the number of actually removed buffers */ public int freeBlocks(int num) { assert num >= 0 : "free blocks must be >= 0 but was: "+ num; final int stop; final int count; if (num > freeBlocks) { stop = 0; count = freeBlocks; } else { stop = freeBlocks - num; count = num; } while (freeBlocks > stop) { freeByteBlocks[--freeBlocks] = null; } bytesUsed.addAndGet(-count*blockSize*Integer.BYTES); assert bytesUsed.get() >= 0; return count; } }
long bytesUsed() { return bytesUsed.get() + pendingUpdates.bytesUsed.get(); }
/** * Creates a new {@link RecyclingByteBlockAllocator} with a block size of * {@link ByteBlockPool#BYTE_BLOCK_SIZE}, upper buffered docs limit of * {@link #DEFAULT_BUFFERED_BLOCKS} ({@value #DEFAULT_BUFFERED_BLOCKS}). * */ public RecyclingByteBlockAllocator() { this(ByteBlockPool.BYTE_BLOCK_SIZE, 64, Counter.newCounter(false)); }
@Override public void recycleByteBlocks(byte[][] blocks, int start, int end) { bytesUsed.addAndGet(-((end-start)* blockSize)); for (int i = start; i < end; i++) { blocks[i] = null; } } };
@Override public void recycleByteBlocks(byte[][] blocks, int start, int end) { final int numBlocks = Math.min(maxBufferedBlocks - freeBlocks, end - start); final int size = freeBlocks + numBlocks; if (size >= freeByteBlocks.length) { final byte[][] newBlocks = new byte[ArrayUtil.oversize(size, RamUsageEstimator.NUM_BYTES_OBJECT_REF)][]; System.arraycopy(freeByteBlocks, 0, newBlocks, 0, freeBlocks); freeByteBlocks = newBlocks; } final int stop = start + numBlocks; for (int i = start; i < stop; i++) { freeByteBlocks[freeBlocks++] = blocks[i]; blocks[i] = null; } for (int i = stop; i < end; i++) { blocks[i] = null; } bytesUsed.addAndGet(-(end - stop) * blockSize); assert bytesUsed.get() >= 0; }
/** * Syntactic sugar for {@link #setBaseline(long)} using {@link Counter#get()} * on the clock passed to the constructor. */ public void setBaseline() { setBaseline(clock.get()); }
/** * Returns a new counter. The returned counter is not thread-safe. */ public static Counter newCounter() { return newCounter(false); }
@Override public void recycleIntBlocks(int[][] blocks, int offset, int length) { bytesUsed.addAndGet(-(length * (IntBlockPool.INT_BLOCK_SIZE * Integer.BYTES))); }