]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | |
6 | package org.rocksdb; | |
7 | ||
11fdf7f2 TL |
8 | import java.util.Arrays; |
9 | import java.util.Objects; | |
10 | ||
7c673cae FG |
11 | /** |
12 | * ColumnFamilyHandle class to hold handles to underlying rocksdb | |
13 | * ColumnFamily Pointers. | |
14 | */ | |
15 | public class ColumnFamilyHandle extends RocksObject { | |
16 | ColumnFamilyHandle(final RocksDB rocksDB, | |
17 | final long nativeHandle) { | |
18 | super(nativeHandle); | |
19 | // rocksDB must point to a valid RocksDB instance; | |
20 | assert(rocksDB != null); | |
21 | // ColumnFamilyHandle must hold a reference to the related RocksDB instance | |
22 | // to guarantee that while a GC cycle starts ColumnFamilyHandle instances | |
23 | // are freed prior to RocksDB instances. | |
24 | this.rocksDB_ = rocksDB; | |
25 | } | |
26 | ||
11fdf7f2 TL |
27 | /** |
28 | * Gets the name of the Column Family. | |
29 | * | |
30 | * @return The name of the Column Family. | |
31 | * | |
32 | * @throws RocksDBException if an error occurs whilst retrieving the name. | |
33 | */ | |
34 | public byte[] getName() throws RocksDBException { | |
35 | return getName(nativeHandle_); | |
36 | } | |
37 | ||
38 | /** | |
39 | * Gets the ID of the Column Family. | |
40 | * | |
41 | * @return the ID of the Column Family. | |
42 | */ | |
43 | public int getID() { | |
44 | return getID(nativeHandle_); | |
45 | } | |
46 | ||
47 | /** | |
48 | * Gets the up-to-date descriptor of the column family | |
49 | * associated with this handle. Since it fills "*desc" with the up-to-date | |
50 | * information, this call might internally lock and release DB mutex to | |
51 | * access the up-to-date CF options. In addition, all the pointer-typed | |
52 | * options cannot be referenced any longer than the original options exist. | |
53 | * | |
54 | * Note that this function is not supported in RocksDBLite. | |
55 | * | |
56 | * @return the up-to-date descriptor. | |
57 | * | |
58 | * @throws RocksDBException if an error occurs whilst retrieving the | |
59 | * descriptor. | |
60 | */ | |
61 | public ColumnFamilyDescriptor getDescriptor() throws RocksDBException { | |
62 | assert(isOwningHandle()); | |
63 | return getDescriptor(nativeHandle_); | |
64 | } | |
65 | ||
66 | @Override | |
67 | public boolean equals(final Object o) { | |
68 | if (this == o) { | |
69 | return true; | |
70 | } | |
71 | if (o == null || getClass() != o.getClass()) { | |
72 | return false; | |
73 | } | |
74 | ||
75 | final ColumnFamilyHandle that = (ColumnFamilyHandle) o; | |
76 | try { | |
77 | return rocksDB_.nativeHandle_ == that.rocksDB_.nativeHandle_ && | |
78 | getID() == that.getID() && | |
79 | Arrays.equals(getName(), that.getName()); | |
80 | } catch (RocksDBException e) { | |
81 | throw new RuntimeException("Cannot compare column family handles", e); | |
82 | } | |
83 | } | |
84 | ||
85 | @Override | |
86 | public int hashCode() { | |
87 | try { | |
88 | return Objects.hash(getName(), getID(), rocksDB_.nativeHandle_); | |
89 | } catch (RocksDBException e) { | |
90 | throw new RuntimeException("Cannot calculate hash code of column family handle", e); | |
91 | } | |
92 | } | |
93 | ||
7c673cae FG |
94 | /** |
95 | * <p>Deletes underlying C++ iterator pointer.</p> | |
96 | * | |
97 | * <p>Note: the underlying handle can only be safely deleted if the RocksDB | |
98 | * instance related to a certain ColumnFamilyHandle is still valid and | |
99 | * initialized. Therefore {@code disposeInternal()} checks if the RocksDB is | |
100 | * initialized before freeing the native handle.</p> | |
101 | */ | |
102 | @Override | |
103 | protected void disposeInternal() { | |
104 | if(rocksDB_.isOwningHandle()) { | |
105 | disposeInternal(nativeHandle_); | |
106 | } | |
107 | } | |
108 | ||
11fdf7f2 TL |
109 | private native byte[] getName(final long handle) throws RocksDBException; |
110 | private native int getID(final long handle); | |
111 | private native ColumnFamilyDescriptor getDescriptor(final long handle) throws RocksDBException; | |
7c673cae FG |
112 | @Override protected final native void disposeInternal(final long handle); |
113 | ||
114 | private final RocksDB rocksDB_; | |
115 | } |