Repository: arrow
Updated Branches:
  refs/heads/master 1a73c352d -> 0bee8040e


ARROW-888: Transfer ownership of buffer in BitVector transferTo()

Author: Steven Phillips <ste...@dremio.com>

Closes #594 from StevenMPhillips/bitVectorOwnership and squashes the following 
commits:

117f6b2 [Steven Phillips] ARROW-888: Transfer ownership of buffer in BitVector 
transferTo()


Project: http://git-wip-us.apache.org/repos/asf/arrow/repo
Commit: http://git-wip-us.apache.org/repos/asf/arrow/commit/0bee8040
Tree: http://git-wip-us.apache.org/repos/asf/arrow/tree/0bee8040
Diff: http://git-wip-us.apache.org/repos/asf/arrow/diff/0bee8040

Branch: refs/heads/master
Commit: 0bee8040e29ebbb4542bc267804f56dcf7feaf4e
Parents: 1a73c35
Author: Steven Phillips <ste...@dremio.com>
Authored: Tue Apr 25 11:36:32 2017 -0700
Committer: Julien Le Dem <jul...@apache.org>
Committed: Tue Apr 25 11:36:32 2017 -0700

----------------------------------------------------------------------
 .../java/org/apache/arrow/vector/BitVector.java |  6 +-
 .../vector/TestBufferOwnershipTransfer.java     | 65 ++++++++++++++++++++
 2 files changed, 66 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/arrow/blob/0bee8040/java/vector/src/main/java/org/apache/arrow/vector/BitVector.java
----------------------------------------------------------------------
diff --git a/java/vector/src/main/java/org/apache/arrow/vector/BitVector.java 
b/java/vector/src/main/java/org/apache/arrow/vector/BitVector.java
index ed57433..82cbd47 100644
--- a/java/vector/src/main/java/org/apache/arrow/vector/BitVector.java
+++ b/java/vector/src/main/java/org/apache/arrow/vector/BitVector.java
@@ -251,11 +251,7 @@ public final class BitVector extends BaseDataValueVector 
implements FixedWidthVe
 
   public void transferTo(BitVector target) {
     target.clear();
-    if (target.data != null) {
-      target.data.release();
-    }
-    target.data = data;
-    target.data.retain(1);
+    target.data = data.transferOwnership(target.allocator).buffer;
     target.valueCount = valueCount;
     clear();
   }

http://git-wip-us.apache.org/repos/asf/arrow/blob/0bee8040/java/vector/src/test/java/org/apache/arrow/vector/TestBufferOwnershipTransfer.java
----------------------------------------------------------------------
diff --git 
a/java/vector/src/test/java/org/apache/arrow/vector/TestBufferOwnershipTransfer.java
 
b/java/vector/src/test/java/org/apache/arrow/vector/TestBufferOwnershipTransfer.java
new file mode 100644
index 0000000..fa65787
--- /dev/null
+++ 
b/java/vector/src/test/java/org/apache/arrow/vector/TestBufferOwnershipTransfer.java
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.arrow.vector;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+import org.junit.Test;
+
+public class TestBufferOwnershipTransfer {
+
+  @Test
+  public void testTransferFixedWidth() {
+    BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
+    BufferAllocator childAllocator1 = allocator.newChildAllocator("child1", 
100000, 100000);
+    BufferAllocator childAllocator2 = allocator.newChildAllocator("child2", 
100000, 100000);
+
+    NullableIntVector v1 = new NullableIntVector("v1", childAllocator1);
+    v1.allocateNew();
+    v1.getMutator().setValueCount(4095);
+
+    NullableIntVector v2 = new NullableIntVector("v2", childAllocator2);
+
+    v1.makeTransferPair(v2).transfer();
+
+    assertEquals(0, childAllocator1.getAllocatedMemory());
+    assertEquals(5 * 4096, childAllocator2.getAllocatedMemory());
+  }
+
+  @Test
+  public void testTransferVariableidth() {
+    BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE);
+    BufferAllocator childAllocator1 = allocator.newChildAllocator("child1", 
100000, 100000);
+    BufferAllocator childAllocator2 = allocator.newChildAllocator("child2", 
100000, 100000);
+
+    NullableVarCharVector v1 = new NullableVarCharVector("v1", 
childAllocator1);
+    v1.allocateNew();
+    v1.getMutator().setSafe(4094, "hello world".getBytes(), 0, 11);
+    v1.getMutator().setValueCount(4001);
+
+    NullableVarCharVector v2 = new NullableVarCharVector("v2", 
childAllocator2);
+
+    v1.makeTransferPair(v2).transfer();
+
+    assertEquals(0, childAllocator1.getAllocatedMemory());
+    int expected = 8*4096 + 4*4096 + 4096;
+    assertEquals(expected, childAllocator2.getAllocatedMemory());
+  }
+}

Reply via email to