]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/main/java/org/rocksdb/WBWIRocksIterator.java
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / java / src / main / java / org / rocksdb / WBWIRocksIterator.java
1 // Copyright (c) 2011-present, 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 public class WBWIRocksIterator
9 extends AbstractRocksIterator<WriteBatchWithIndex> {
10 private final WriteEntry entry = new WriteEntry();
11
12 protected WBWIRocksIterator(final WriteBatchWithIndex wbwi,
13 final long nativeHandle) {
14 super(wbwi, nativeHandle);
15 }
16
17 /**
18 * Get the current entry
19 *
20 * The WriteEntry is only valid
21 * until the iterator is repositioned.
22 * If you want to keep the WriteEntry across iterator
23 * movements, you must make a copy of its data!
24 *
25 * Note - This method is not thread-safe with respect to the WriteEntry
26 * as it performs a non-atomic update across the fields of the WriteEntry
27 *
28 * @return The WriteEntry of the current entry
29 */
30 public WriteEntry entry() {
31 assert(isOwningHandle());
32 final long ptrs[] = entry1(nativeHandle_);
33
34 entry.type = WriteType.fromId((byte)ptrs[0]);
35 entry.key.resetNativeHandle(ptrs[1], ptrs[1] != 0);
36 entry.value.resetNativeHandle(ptrs[2], ptrs[2] != 0);
37
38 return entry;
39 }
40
41 @Override protected final native void disposeInternal(final long handle);
42 @Override final native boolean isValid0(long handle);
43 @Override final native void seekToFirst0(long handle);
44 @Override final native void seekToLast0(long handle);
45 @Override final native void next0(long handle);
46 @Override final native void prev0(long handle);
47 @Override final native void seek0(long handle, byte[] target, int targetLen);
48 @Override final native void status0(long handle) throws RocksDBException;
49
50 private native long[] entry1(final long handle);
51
52 /**
53 * Enumeration of the Write operation
54 * that created the record in the Write Batch
55 */
56 public enum WriteType {
57 PUT((byte)0x1),
58 MERGE((byte)0x2),
59 DELETE((byte)0x4),
60 LOG((byte)0x8);
61
62 final byte id;
63 WriteType(final byte id) {
64 this.id = id;
65 }
66
67 public static WriteType fromId(final byte id) {
68 for(final WriteType wt : WriteType.values()) {
69 if(id == wt.id) {
70 return wt;
71 }
72 }
73 throw new IllegalArgumentException("No WriteType with id=" + id);
74 }
75 }
76
77 @Override
78 public void close() {
79 entry.close();
80 super.close();
81 }
82
83 /**
84 * Represents an entry returned by
85 * {@link org.rocksdb.WBWIRocksIterator#entry()}
86 *
87 * It is worth noting that a WriteEntry with
88 * the type {@link org.rocksdb.WBWIRocksIterator.WriteType#DELETE}
89 * or {@link org.rocksdb.WBWIRocksIterator.WriteType#LOG}
90 * will not have a value.
91 */
92 public static class WriteEntry implements AutoCloseable {
93 WriteType type = null;
94 final DirectSlice key;
95 final DirectSlice value;
96
97 /**
98 * Intentionally private as this
99 * should only be instantiated in
100 * this manner by the outer WBWIRocksIterator
101 * class; The class members are then modified
102 * by calling {@link org.rocksdb.WBWIRocksIterator#entry()}
103 */
104 private WriteEntry() {
105 key = new DirectSlice();
106 value = new DirectSlice();
107 }
108
109 public WriteEntry(final WriteType type, final DirectSlice key,
110 final DirectSlice value) {
111 this.type = type;
112 this.key = key;
113 this.value = value;
114 }
115
116 /**
117 * Returns the type of the Write Entry
118 *
119 * @return the WriteType of the WriteEntry
120 */
121 public WriteType getType() {
122 return type;
123 }
124
125 /**
126 * Returns the key of the Write Entry
127 *
128 * @return The slice containing the key
129 * of the WriteEntry
130 */
131 public DirectSlice getKey() {
132 return key;
133 }
134
135 /**
136 * Returns the value of the Write Entry
137 *
138 * @return The slice containing the value of
139 * the WriteEntry or null if the WriteEntry has
140 * no value
141 */
142 public DirectSlice getValue() {
143 if(!value.isOwningHandle()) {
144 return null; //TODO(AR) migrate to JDK8 java.util.Optional#empty()
145 } else {
146 return value;
147 }
148 }
149
150 /**
151 * Generates a hash code for the Write Entry. NOTE: The hash code is based
152 * on the string representation of the key, so it may not work correctly
153 * with exotic custom comparators.
154 *
155 * @return The hash code for the Write Entry
156 */
157 @Override
158 public int hashCode() {
159 return (key == null) ? 0 : key.hashCode();
160 }
161
162 @Override
163 public boolean equals(final Object other) {
164 if(other == null) {
165 return false;
166 } else if (this == other) {
167 return true;
168 } else if(other instanceof WriteEntry) {
169 final WriteEntry otherWriteEntry = (WriteEntry)other;
170 return type.equals(otherWriteEntry.type)
171 && key.equals(otherWriteEntry.key)
172 && value.equals(otherWriteEntry.value);
173 } else {
174 return false;
175 }
176 }
177
178 @Override
179 public void close() {
180 value.close();
181 key.close();
182 }
183 }
184 }