1 // Copyright (c) 2016, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root 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 
RocksMutableObject()25   protected RocksMutableObject() {
26   }
27 
RocksMutableObject(final long nativeHandle)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    */
resetNativeHandle(final long newNativeHandle, final boolean owningNativeHandle)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    */
setNativeHandle(final long nativeHandle, final boolean owningNativeHandle)51   public synchronized void setNativeHandle(final long nativeHandle,
52       final boolean owningNativeHandle) {
53     this.nativeHandle_ = nativeHandle;
54     this.owningHandle_ = owningNativeHandle;
55   }
56 
57   @Override
isOwningHandle()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    */
getNativeHandle()68   protected synchronized long getNativeHandle() {
69     assert (this.nativeHandle_ != 0);
70     return this.nativeHandle_;
71   }
72 
73   @Override
close()74   public synchronized final void close() {
75     if (isOwningHandle()) {
76       disposeInternal();
77       this.owningHandle_ = false;
78       this.nativeHandle_ = 0;
79     }
80   }
81 
disposeInternal()82   protected void disposeInternal() {
83     disposeInternal(nativeHandle_);
84   }
85 
disposeInternal(final long handle)86   protected abstract void disposeInternal(final long handle);
87 }
88