/** * Builds a block, providing the pool for the block to be returned to. * * @param objectPool The pool that this block should be returned to. * @param blockFile The file that this data block is from. * @param blockSize The size of the data. * @param blockId The ID of the block in the file. * @param byteOffset The offset of the data for this block, from the start of the buffer. * @param bb The buffer containing the data. * @param sbb A cached read-only buffer, useful to use as a source for writing operations * which require a buffer of data. * @param ib The 32 bit integer offset of the data for this block, from the start of the buffer. * @param lb The 64 bit long offset of the data for this block, from the start of the buffer. */ private Block( ObjectPool objectPool, BlockFile blockFile, int blockSize, long blockId, int byteOffset, ByteBuffer bb, ByteBuffer sbb, IntBuffer ib, LongBuffer lb ) { init(objectPool, blockFile, blockSize, blockId, byteOffset, bb, sbb, ib, lb); }
/** * Initializes a block for use. * * @param objectPool The pool that this block should be returned to. * @param blockFile The file that this data block is from. * @param blockSize The size of the data. * @param blockId The ID of the block in the file. * @param byteOffset The offset of the data for this block, from the start of the buffer. * @param bb The buffer containing the data. * @param sbb A cached read-only buffer, useful to use as a source for writing operations * which require a buffer of data. * @param ib The 32 bit integer offset of the data for this block, from the start of the buffer. * @param lb The 64 bit long offset of the data for this block, from the start of the buffer. */ private void init( ObjectPool objectPool, BlockFile blockFile, int blockSize, long blockId, int byteOffset, ByteBuffer bb, ByteBuffer sbb, IntBuffer ib, LongBuffer lb ) { assert this.objectPool == null; this.objectPool = objectPool; this.blockFile = blockFile; this.blockSize = blockSize; init(blockId, byteOffset, bb, sbb, ib, lb); }
/** * Factory method to create a new block, allocating a new data buffer. Uses the objectPool * if possible, or else creates a new block that will be returned to the object pool. * * @param objectPool The pool of blocks to get the block from. * @param blockFile The file to get the block from. * @param blockSize The size of the data in the block. * @param blockId The ID of the block from the file. * @param byteOrder Represents little endian or big endian byte ordering. * @return The block from the pool, or a new block which will be returned to the pool. */ public static Block newInstance( ObjectPool objectPool, BlockFile blockFile, int blockSize, long blockId, ByteOrder byteOrder ) { Block block = (Block) objectPool.get(ObjectPool.TYPE_S_BLOCK, blockSize); if (block != null) { block.init(objectPool, blockFile, blockId); } else { block = Block.newInstance( objectPool, blockFile, blockSize, blockId, 0, ByteBuffer.allocateDirect(blockSize).order(byteOrder), null, null, null ); block.ownsBuffer = true; } return block; }
block.init( objectPool, blockFile, blockSize, blockId, byteOffset, bb, sbb, ib, lb
/** * Allocates a new block for dstBlockId, copies the contents of srcBlock to * it, releases srcBlock and returns the new Block. * * @param block The block to copy data from. This block will be set to the * destination block before returning, and acts as the return value. * @param dstBlockId the ID of the new Block to be allocated. */ public void copyBlock(Block block, long dstBlockId) { assert(block.getBlockId() >= 0) && (block.getBlockId() < nrBlocks); assert(dstBlockId >= 0) && (dstBlockId < nrBlocks); assert block.getBlockId() != dstBlockId; int byteOffset = block.getByteOffset(); ByteBuffer srcBuffer = block.getSourceBuffer(); srcBuffer.limit(byteOffset + blockSize); srcBuffer.position(byteOffset); long dstFileOffset = dstBlockId * blockSize; int dstRegionNr = (int) (dstFileOffset / stride); int dstByteOffset = (int) (dstFileOffset % stride); ByteBuffer dstBuffer = mappedByteBuffers[dstRegionNr]; dstBuffer.position(dstByteOffset); // Copy the bytes. dstBuffer.put(srcBuffer); block.init( dstBlockId, dstByteOffset, dstBuffer, srcByteBuffers[dstRegionNr], intBuffers[dstRegionNr], longBuffers[dstRegionNr] ); }