1 // Copyright (c) 2011-present, 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 import java.nio.ByteBuffer; 9 10 /** 11 * <p>An iterator that yields a sequence of key/value pairs from a source. 12 * Multiple implementations are provided by this library. 13 * In particular, iterators are provided 14 * to access the contents of a Table or a DB.</p> 15 * 16 * <p>Multiple threads can invoke const methods on an RocksIterator without 17 * external synchronization, but if any of the threads may call a 18 * non-const method, all threads accessing the same RocksIterator must use 19 * external synchronization.</p> 20 * 21 * @see org.rocksdb.RocksObject 22 */ 23 public class RocksIterator extends AbstractRocksIterator<RocksDB> { RocksIterator(RocksDB rocksDB, long nativeHandle)24 protected RocksIterator(RocksDB rocksDB, long nativeHandle) { 25 super(rocksDB, nativeHandle); 26 } 27 28 /** 29 * <p>Return the key for the current entry. The underlying storage for 30 * the returned slice is valid only until the next modification of 31 * the iterator.</p> 32 * 33 * <p>REQUIRES: {@link #isValid()}</p> 34 * 35 * @return key for the current entry. 36 */ key()37 public byte[] key() { 38 assert(isOwningHandle()); 39 return key0(nativeHandle_); 40 } 41 42 /** 43 * <p>Return the key for the current entry. The underlying storage for 44 * the returned slice is valid only until the next modification of 45 * the iterator.</p> 46 * 47 * <p>REQUIRES: {@link #isValid()}</p> 48 * 49 * @param key the out-value to receive the retrieved key. 50 * It is using position and limit. Limit is set according to key size. 51 * Supports direct buffer only. 52 * @return The size of the actual key. If the return key is greater than the 53 * length of {@code key}, then it indicates that the size of the 54 * input buffer {@code key} is insufficient and partial result will 55 * be returned. 56 */ key(ByteBuffer key)57 public int key(ByteBuffer key) { 58 assert (isOwningHandle() && key.isDirect()); 59 int result = keyDirect0(nativeHandle_, key, key.position(), key.remaining()); 60 key.limit(Math.min(key.position() + result, key.limit())); 61 return result; 62 } 63 64 /** 65 * <p>Return the value for the current entry. The underlying storage for 66 * the returned slice is valid only until the next modification of 67 * the iterator.</p> 68 * 69 * <p>REQUIRES: !AtEnd() && !AtStart()</p> 70 * @return value for the current entry. 71 */ value()72 public byte[] value() { 73 assert(isOwningHandle()); 74 return value0(nativeHandle_); 75 } 76 77 /** 78 * <p>Return the value for the current entry. The underlying storage for 79 * the returned slice is valid only until the next modification of 80 * the iterator.</p> 81 * 82 * <p>REQUIRES: {@link #isValid()}</p> 83 * 84 * @param value the out-value to receive the retrieved value. 85 * It is using position and limit. Limit is set according to value size. 86 * Supports direct buffer only. 87 * @return The size of the actual value. If the return value is greater than the 88 * length of {@code value}, then it indicates that the size of the 89 * input buffer {@code value} is insufficient and partial result will 90 * be returned. 91 */ value(ByteBuffer value)92 public int value(ByteBuffer value) { 93 assert (isOwningHandle() && value.isDirect()); 94 int result = valueDirect0(nativeHandle_, value, value.position(), value.remaining()); 95 value.limit(Math.min(value.position() + result, value.limit())); 96 return result; 97 } 98 disposeInternal(final long handle)99 @Override protected final native void disposeInternal(final long handle); isValid0(long handle)100 @Override final native boolean isValid0(long handle); seekToFirst0(long handle)101 @Override final native void seekToFirst0(long handle); seekToLast0(long handle)102 @Override final native void seekToLast0(long handle); next0(long handle)103 @Override final native void next0(long handle); prev0(long handle)104 @Override final native void prev0(long handle); seek0(long handle, byte[] target, int targetLen)105 @Override final native void seek0(long handle, byte[] target, int targetLen); seekForPrev0(long handle, byte[] target, int targetLen)106 @Override final native void seekForPrev0(long handle, byte[] target, int targetLen); 107 @Override seekDirect0(long handle, ByteBuffer target, int targetOffset, int targetLen)108 final native void seekDirect0(long handle, ByteBuffer target, int targetOffset, int targetLen); 109 @Override seekForPrevDirect0( long handle, ByteBuffer target, int targetOffset, int targetLen)110 final native void seekForPrevDirect0( 111 long handle, ByteBuffer target, int targetOffset, int targetLen); status0(long handle)112 @Override final native void status0(long handle) throws RocksDBException; 113 key0(long handle)114 private native byte[] key0(long handle); value0(long handle)115 private native byte[] value0(long handle); keyDirect0(long handle, ByteBuffer buffer, int bufferOffset, int bufferLen)116 private native int keyDirect0(long handle, ByteBuffer buffer, int bufferOffset, int bufferLen); valueDirect0(long handle, ByteBuffer buffer, int bufferOffset, int bufferLen)117 private native int valueDirect0(long handle, ByteBuffer buffer, int bufferOffset, int bufferLen); 118 } 119