@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; } }
/** @param page Page. */ public final void addPage(GridResultPage page) { int pageRowsCnt = page.rowsInPage(); if (pageRowsCnt != 0) addPage0(page); Counter cnt = remainingRows.get(page.source()); int allRows = page.response().allRows(); if (allRows != -1) { // Only the first page contains allRows count and is allowed to init counter. assert !cnt.initialized : "Counter is already initialized."; cnt.addAndGet(allRows); expRowsCnt.addAndGet(allRows); // We need this separate flag to handle case when the first source contains only one page // and it will signal that all remaining counters are zero and fetch is finished. cnt.initialized = true; } if (cnt.addAndGet(-pageRowsCnt) == 0) { // Result can be negative in case of race between messages, it is ok. boolean last = true; for (Counter c : remainingRows.values()) { // Check all the sources. if (c.get() != 0 || !c.initialized) { last = false; break; } } if (last && lastSubmitted.compareAndSet(false, true)) { addPage0( new GridResultPage(null, page.source(), null) { @Override public boolean isLast() { return true; } }); } } }
@Override public int[] getIntBlock() { if (freeBlocks == 0) { bytesUsed.addAndGet(blockSize * RamUsageEstimator.NUM_BYTES_INT); 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 * RamUsageEstimator.NUM_BYTES_INT); assert bytesUsed.get() >= 0; return count; }
@Override public void recycleIntBlocks(int[][] blocks, int start, int end) { final int numBlocks = Math.min(maxBufferedBlocks - freeBlocks, end - start); final int size = freeBlocks + numBlocks; if (size >= freeByteBlocks.length) { final int[][] newBlocks = new int[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 * RamUsageEstimator.NUM_BYTES_INT)); assert bytesUsed.get() >= 0; }
@Override public byte[] getByteBlock() { bytesUsed.addAndGet(blockSize); return new byte[blockSize]; }