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.
8 public class WBWIRocksIterator
9 extends AbstractRocksIterator
<WriteBatchWithIndex
> {
10 private final WriteEntry entry
= new WriteEntry();
12 protected WBWIRocksIterator(final WriteBatchWithIndex wbwi
,
13 final long nativeHandle
) {
14 super(wbwi
, nativeHandle
);
18 * Get the current entry
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!
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
28 * @return The WriteEntry of the current entry
30 public WriteEntry
entry() {
31 assert(isOwningHandle());
32 final long ptrs
[] = entry1(nativeHandle_
);
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);
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
;
50 private native long[] entry1(final long handle
);
53 * Enumeration of the Write operation
54 * that created the record in the Write Batch
56 public enum WriteType
{
63 WriteType(final byte id
) {
67 public static WriteType
fromId(final byte id
) {
68 for(final WriteType wt
: WriteType
.values()) {
73 throw new IllegalArgumentException("No WriteType with id=" + id
);
84 * Represents an entry returned by
85 * {@link org.rocksdb.WBWIRocksIterator#entry()}
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.
92 public static class WriteEntry
implements AutoCloseable
{
93 WriteType type
= null;
94 final DirectSlice key
;
95 final DirectSlice value
;
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()}
104 private WriteEntry() {
105 key
= new DirectSlice();
106 value
= new DirectSlice();
109 public WriteEntry(final WriteType type
, final DirectSlice key
,
110 final DirectSlice value
) {
117 * Returns the type of the Write Entry
119 * @return the WriteType of the WriteEntry
121 public WriteType
getType() {
126 * Returns the key of the Write Entry
128 * @return The slice containing the key
131 public DirectSlice
getKey() {
136 * Returns the value of the Write Entry
138 * @return The slice containing the value of
139 * the WriteEntry or null if the WriteEntry has
142 public DirectSlice
getValue() {
143 if(!value
.isOwningHandle()) {
144 return null; //TODO(AR) migrate to JDK8 java.util.Optional#empty()
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.
155 * @return The hash code for the Write Entry
158 public int hashCode() {
159 return (key
== null) ?
0 : key
.hashCode();
163 public boolean equals(final Object other
) {
166 } else if (this == other
) {
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
);
179 public void close() {