]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/java/adapter/orc/src/main/java/org/apache/arrow/adapter/orc/OrcReferenceManager.java
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / java / adapter / orc / src / main / java / org / apache / arrow / adapter / orc / OrcReferenceManager.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.arrow.adapter.orc;
19
20 import java.util.concurrent.atomic.AtomicInteger;
21
22 import org.apache.arrow.memory.ArrowBuf;
23 import org.apache.arrow.memory.BufferAllocator;
24 import org.apache.arrow.memory.OwnershipTransferResult;
25 import org.apache.arrow.memory.ReferenceManager;
26 import org.apache.arrow.util.Preconditions;
27
28 /**
29 * A simple reference manager implementation for memory allocated by native code.
30 * The underlying memory will be released when reference count reach zero.
31 */
32 public class OrcReferenceManager implements ReferenceManager {
33 private final AtomicInteger bufRefCnt = new AtomicInteger(0);
34
35 private OrcMemoryJniWrapper memory;
36
37 OrcReferenceManager(OrcMemoryJniWrapper memory) {
38 this.memory = memory;
39 }
40
41 @Override
42 public int getRefCount() {
43 return bufRefCnt.get();
44 }
45
46 @Override
47 public boolean release() {
48 return release(1);
49 }
50
51 @Override
52 public boolean release(int decrement) {
53 Preconditions.checkState(decrement >= 1,
54 "ref count decrement should be greater than or equal to 1");
55 // decrement the ref count
56 final int refCnt;
57 synchronized (this) {
58 refCnt = bufRefCnt.addAndGet(-decrement);
59 if (refCnt == 0) {
60 // refcount of this reference manager has dropped to 0
61 // release the underlying memory
62 memory.close();
63 }
64 }
65 // the new ref count should be >= 0
66 Preconditions.checkState(refCnt >= 0, "RefCnt has gone negative");
67 return refCnt == 0;
68 }
69
70 @Override
71 public void retain() {
72 retain(1);
73 }
74
75 @Override
76 public void retain(int increment) {
77 Preconditions.checkArgument(increment > 0, "retain(%s) argument is not positive", increment);
78 bufRefCnt.addAndGet(increment);
79 }
80
81 @Override
82 public ArrowBuf retain(ArrowBuf srcBuffer, BufferAllocator targetAllocator) {
83 retain();
84 return srcBuffer;
85 }
86
87 @Override
88 public ArrowBuf deriveBuffer(ArrowBuf sourceBuffer, long index, long length) {
89 final long derivedBufferAddress = sourceBuffer.memoryAddress() + index;
90
91 // create new ArrowBuf
92 final ArrowBuf derivedBuf = new ArrowBuf(
93 this,
94 null,
95 length, // length (in bytes) in the underlying memory chunk for this new ArrowBuf
96 derivedBufferAddress // starting byte address in the underlying memory for this new ArrowBuf,
97 );
98
99 return derivedBuf;
100 }
101
102 @Override
103 public OwnershipTransferResult transferOwnership(ArrowBuf sourceBuffer, BufferAllocator targetAllocator) {
104 throw new UnsupportedOperationException();
105 }
106
107 @Override
108 public BufferAllocator getAllocator() {
109 return null;
110 }
111
112 @Override
113 public long getSize() {
114 return memory.getSize();
115 }
116
117 @Override
118 public long getAccountedSize() {
119 return 0;
120 }
121 }