/** * Construct the ByteBufferIOEngine with the given capacity * @param capacity * @throws IOException ideally here no exception to be thrown from the allocator */ public ByteBufferIOEngine(long capacity) throws IOException { this.capacity = capacity; ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; bufferArray = new ByteBufferArray(capacity, allocator); }
public FileMmapEngine(String filePath, long capacity) throws IOException { this.path = filePath; this.size = capacity; long fileSize = 0; try { raf = new RandomAccessFile(filePath, "rw"); fileSize = roundUp(capacity, ByteBufferArray.DEFAULT_BUFFER_SIZE); raf.setLength(fileSize); fileChannel = raf.getChannel(); LOG.info("Allocating " + StringUtils.byteDesc(fileSize) + ", on the path:" + filePath); } catch (java.io.FileNotFoundException fex) { LOG.error("Can't create bucket cache file " + filePath, fex); throw fex; } catch (IOException ioex) { LOG.error("Can't extend bucket cache file; insufficient space for " + StringUtils.byteDesc(fileSize), ioex); shutdown(); throw ioex; } ByteBufferAllocator allocator = new ByteBufferAllocator() { AtomicInteger pos = new AtomicInteger(0); @Override public ByteBuffer allocate(long size) throws IOException { ByteBuffer buffer = fileChannel.map(java.nio.channels.FileChannel.MapMode.READ_WRITE, pos.getAndIncrement() * size, size); return buffer; } }; bufferArray = new ByteBufferArray(fileSize, allocator); }
@Test public void testByteBufferCreation() throws Exception { int capacity = 470 * 1021 * 1023; ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; ByteBufferArray array = new ByteBufferArray(capacity, allocator); assertEquals(119, array.buffers.length); for (int i = 0; i < array.buffers.length; i++) { if (i == array.buffers.length - 1) { assertEquals(0, array.buffers[i].capacity()); } else { assertEquals(ByteBufferArray.DEFAULT_BUFFER_SIZE, array.buffers[i].capacity()); } } }
@Test public void testAsSubBufferWhenEndOffsetLandInLastBuffer() throws Exception { int capacity = 4 * 1024 * 1024; ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; ByteBufferArray array = new ByteBufferArray(capacity, allocator); ByteBuff subBuf = array.asSubByteBuff(0, capacity); subBuf.position(capacity - 1);// Position to the last byte assertTrue(subBuf.hasRemaining()); // Read last byte subBuf.get(); assertFalse(subBuf.hasRemaining()); }
/** * Construct the ByteBufferIOEngine with the given capacity * @param capacity * @param direct true if allocate direct buffer * @throws IOException */ public ByteBufferIOEngine(long capacity, boolean direct) throws IOException { this.capacity = capacity; this.direct = direct; bufferArray = new ByteBufferArray(capacity, direct); }
@Test public void testByteBufferCreation() throws Exception { int capacity = 470 * 1021 * 1023; ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; ByteBufferArray array = new ByteBufferArray(capacity, allocator); assertEquals(119, array.buffers.length); for (int i = 0; i < array.buffers.length; i++) { if (i == array.buffers.length - 1) { assertEquals(0, array.buffers[i].capacity()); } else { assertEquals(ByteBufferArray.DEFAULT_BUFFER_SIZE, array.buffers[i].capacity()); } } }
@Test public void testByteBufferCreation() throws Exception { int capacity = 470 * 1021 * 1023; ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; ByteBufferArray array = new ByteBufferArray(capacity, allocator); assertEquals(119, array.buffers.length); for (int i = 0; i < array.buffers.length; i++) { if (i == array.buffers.length - 1) { assertEquals(0, array.buffers[i].capacity()); } else { assertEquals(ByteBufferArray.DEFAULT_BUFFER_SIZE, array.buffers[i].capacity()); } } }
@Test public void testAsSubBufferWhenEndOffsetLandInLastBuffer() throws Exception { int capacity = 4 * 1024 * 1024; ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; ByteBufferArray array = new ByteBufferArray(capacity, allocator); ByteBuff subBuf = array.asSubByteBuff(0, capacity); subBuf.position(capacity - 1);// Position to the last byte assertTrue(subBuf.hasRemaining()); // Read last byte subBuf.get(); assertFalse(subBuf.hasRemaining()); }
@Test public void testAsSubBufferWhenEndOffsetLandInLastBuffer() throws Exception { int capacity = 4 * 1024 * 1024; ByteBufferAllocator allocator = new ByteBufferAllocator() { @Override public ByteBuffer allocate(long size) throws IOException { return ByteBuffer.allocateDirect((int) size); } }; ByteBufferArray array = new ByteBufferArray(capacity, allocator); ByteBuff subBuf = array.asSubByteBuff(0, capacity); subBuf.position(capacity - 1);// Position to the last byte assertTrue(subBuf.hasRemaining()); // Read last byte subBuf.get(); assertFalse(subBuf.hasRemaining()); }