|
| 1 | +// Copyright (c) 2014, 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 | + * An iterator yields a sequence of key/value pairs from a source. |
| 10 | + * The following class defines the interface. Multiple implementations |
| 11 | + * are provided by this library. In particular, iterators are provided |
| 12 | + * to access the contents of a Table or a DB. |
| 13 | + * |
| 14 | + * Multiple threads can invoke const methods on an Iterator without |
| 15 | + * external synchronization, but if any of the threads may call a |
| 16 | + * non-const method, all threads accessing the same Iterator must use |
| 17 | + * external synchronization. |
| 18 | + */ |
| 19 | +public class Iterator { |
| 20 | + private long nativeHandle_; |
| 21 | + |
| 22 | + public Iterator(long nativeHandle) { |
| 23 | + nativeHandle_ = nativeHandle; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * An iterator is either positioned at a key/value pair, or |
| 28 | + * not valid. This method returns true iff the iterator is valid. |
| 29 | + * @return true if iterator is valid. |
| 30 | + */ |
| 31 | + public boolean isValid() { |
| 32 | + assert(isInitialized()); |
| 33 | + return isValid0(nativeHandle_); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Position at the first key in the source. The iterator is Valid() |
| 38 | + * after this call iff the source is not empty. |
| 39 | + */ |
| 40 | + public void seekToFirst() { |
| 41 | + assert(isInitialized()); |
| 42 | + seekToFirst0(nativeHandle_); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Position at the last key in the source. The iterator is |
| 47 | + * Valid() after this call iff the source is not empty. |
| 48 | + */ |
| 49 | + public void seekToLast() { |
| 50 | + assert(isInitialized()); |
| 51 | + seekToLast0(nativeHandle_); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Moves to the next entry in the source. After this call, Valid() is |
| 56 | + * true iff the iterator was not positioned at the last entry in the source. |
| 57 | + * REQUIRES: Valid() |
| 58 | + */ |
| 59 | + public void next() { |
| 60 | + assert(isInitialized()); |
| 61 | + next0(nativeHandle_); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Moves to the previous entry in the source. After this call, Valid() is |
| 66 | + * true iff the iterator was not positioned at the first entry in source. |
| 67 | + * REQUIRES: Valid() |
| 68 | + */ |
| 69 | + public void prev() { |
| 70 | + assert(isInitialized()); |
| 71 | + prev0(nativeHandle_); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Return the key for the current entry. The underlying storage for |
| 76 | + * the returned slice is valid only until the next modification of |
| 77 | + * the iterator. |
| 78 | + * REQUIRES: Valid() |
| 79 | + * @return key for the current entry. |
| 80 | + */ |
| 81 | + public byte[] key() { |
| 82 | + assert(isInitialized()); |
| 83 | + return key0(nativeHandle_); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Return the value for the current entry. The underlying storage for |
| 88 | + * the returned slice is valid only until the next modification of |
| 89 | + * the iterator. |
| 90 | + * REQUIRES: !AtEnd() && !AtStart() |
| 91 | + * @return value for the current entry. |
| 92 | + */ |
| 93 | + public byte[] value() { |
| 94 | + assert(isInitialized()); |
| 95 | + return value0(nativeHandle_); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Position at the first key in the source that at or past target |
| 100 | + * The iterator is Valid() after this call iff the source contains |
| 101 | + * an entry that comes at or past target. |
| 102 | + */ |
| 103 | + public void seek(byte[] target) { |
| 104 | + assert(isInitialized()); |
| 105 | + seek0(nativeHandle_, target, target.length); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * If an error has occurred, return it. Else return an ok status. |
| 110 | + * If non-blocking IO is requested and this operation cannot be |
| 111 | + * satisfied without doing some IO, then this returns Status::Incomplete(). |
| 112 | + * |
| 113 | + */ |
| 114 | + public void status() throws RocksDBException { |
| 115 | + assert(isInitialized()); |
| 116 | + status0(nativeHandle_); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Deletes underlying C++ iterator pointer. |
| 121 | + */ |
| 122 | + public synchronized void close() { |
| 123 | + if(nativeHandle_ != 0) { |
| 124 | + close0(nativeHandle_); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Override protected void finalize() { |
| 129 | + close(); |
| 130 | + } |
| 131 | + |
| 132 | + private boolean isInitialized() { |
| 133 | + return (nativeHandle_ != 0); |
| 134 | + } |
| 135 | + |
| 136 | + private native boolean isValid0(long handle); |
| 137 | + private native void close0(long handle); |
| 138 | + private native void seekToFirst0(long handle); |
| 139 | + private native void seekToLast0(long handle); |
| 140 | + private native void next0(long handle); |
| 141 | + private native void prev0(long handle); |
| 142 | + private native byte[] key0(long handle); |
| 143 | + private native byte[] value0(long handle); |
| 144 | + private native void seek0(long handle, byte[] target, int targetLen); |
| 145 | + private native void status0(long handle); |
| 146 | +} |
0 commit comments