]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/main/java/org/rocksdb/RocksMutableObject.java
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / java / src / main / java / org / rocksdb / RocksMutableObject.java
1 // Copyright (c) 2016, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
5
6 package org.rocksdb;
7
8 /**
9 * RocksMutableObject is an implementation of {@link AbstractNativeReference}
10 * whose reference to the underlying native C++ object can change.
11 *
12 * <p>The use of {@code RocksMutableObject} should be kept to a minimum, as it
13 * has synchronization overheads and introduces complexity. Instead it is
14 * recommended to use {@link RocksObject} where possible.</p>
15 */
16 public abstract class RocksMutableObject extends AbstractNativeReference {
17
18 /**
19 * An mutable reference to the value of the C++ pointer pointing to some
20 * underlying native RocksDB C++ object.
21 */
22 private long nativeHandle_;
23 private boolean owningHandle_;
24
25 protected RocksMutableObject() {
26 }
27
28 protected RocksMutableObject(final long nativeHandle) {
29 this.nativeHandle_ = nativeHandle;
30 this.owningHandle_ = true;
31 }
32
33 /**
34 * Closes the existing handle, and changes the handle to the new handle
35 *
36 * @param newNativeHandle The C++ pointer to the new native object
37 * @param owningNativeHandle true if we own the new native object
38 */
39 public synchronized void resetNativeHandle(final long newNativeHandle,
40 final boolean owningNativeHandle) {
41 close();
42 setNativeHandle(newNativeHandle, owningNativeHandle);
43 }
44
45 /**
46 * Sets the handle (C++ pointer) of the underlying C++ native object
47 *
48 * @param nativeHandle The C++ pointer to the native object
49 * @param owningNativeHandle true if we own the native object
50 */
51 public synchronized void setNativeHandle(final long nativeHandle,
52 final boolean owningNativeHandle) {
53 this.nativeHandle_ = nativeHandle;
54 this.owningHandle_ = owningNativeHandle;
55 }
56
57 @Override
58 protected synchronized boolean isOwningHandle() {
59 return this.owningHandle_;
60 }
61
62 /**
63 * Gets the value of the C++ pointer pointing to the underlying
64 * native C++ object
65 *
66 * @return the pointer value for the native object
67 */
68 protected synchronized long getNativeHandle() {
69 assert (this.nativeHandle_ != 0);
70 return this.nativeHandle_;
71 }
72
73 @Override
74 public synchronized final void close() {
75 if (isOwningHandle()) {
76 disposeInternal();
77 this.owningHandle_ = false;
78 this.nativeHandle_ = 0;
79 }
80 }
81
82 protected void disposeInternal() {
83 disposeInternal(nativeHandle_);
84 }
85
86 protected abstract void disposeInternal(final long handle);
87 }