From a269bed9f4e628cb556d5a54ad1b60b82d44a5dc Mon Sep 17 00:00:00 2001 From: Vibhatha Lakmal Abeykoon Date: Mon, 29 Jul 2024 07:57:43 +0530 Subject: [PATCH] fix: adding sample code --- .../org/apache/arrow/memory/TestArrowBuf.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestArrowBuf.java b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestArrowBuf.java index 60e118d557cf6..a370aaa9fc8db 100644 --- a/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestArrowBuf.java +++ b/java/memory/memory-core/src/test/java/org/apache/arrow/memory/TestArrowBuf.java @@ -72,6 +72,63 @@ public void testSliceWithinBoundsLength_ReturnsSlice() { } } + private void printArrowBuf(ArrowBuf buf, int length) { + for (int i = 0; i < length; i++) { + System.out.print(buf.getByte(i) + ", "); + } + System.out.println(); + } + + private void setValues(ArrowBuf buf, int multiplier) { + final long capacity = buf.capacity(); + for (int i = 0; i < capacity; i++) { + buf.setByte(i, (byte) i * multiplier); + } + } + + @Test + public void testDerivedBuffer() { + try (BufferAllocator allocator = new RootAllocator(128)) { + ArrowBuf buf = allocator.buffer(5); + buf.setByte(0, 1); + buf.setByte(1, 2); + buf.setByte(2, 3); + buf.setByte(3, 4); + buf.setByte(4, 5); + ReferenceManager referenceManager1 = buf.getReferenceManager(); + ArrowBuf newBuf = buf.getReferenceManager().deriveBuffer(buf, 2, 2); + ReferenceManager referenceManager2 = newBuf.getReferenceManager(); + assert referenceManager1 instanceof BufferLedger; + assert referenceManager2 instanceof BufferLedger; + BufferLedger ledger1 = (BufferLedger) referenceManager1; + BufferLedger ledger2 = (BufferLedger) referenceManager2; + + System.out.println("Buf 1 Ledger: " + ledger1.isOwningLedger()); + System.out.println("Buf 2 Ledger: " + ledger2.isOwningLedger()); + System.out.println("Buf 1: " + buf); + System.out.println("Buf 2: " + newBuf); + System.out.println("Buf 1 ref count(1): " + buf.refCnt()); + System.out.println("Buf 2 ref count(1): " + newBuf.refCnt()); + + System.out.println("Buf 1 data: "); + printArrowBuf(buf, 5); + System.out.println("Buf 2 data: "); + printArrowBuf(newBuf, 2); + + ArrowBuf buf3 = ((BufferLedger) referenceManager1).newArrowBuf(3, null); + setValues(buf3, 3); + System.out.println("Buf 3 data: "); + printArrowBuf(buf3, 3); + + System.out.println("Buf 1 data (again): "); + printArrowBuf(buf, 5); + System.out.println("Buf 2 data (again): "); + printArrowBuf(newBuf, 2); + + ledger1.release(); + } + } + @Test public void testSetBytesSliced() { int arrLength = 64;