main
Ankit Gupta 11 years ago
parent dc28a726c1
commit 06b590dd7c
  1. 2
      java/RocksDBSample.java
  2. 61
      java/org/rocksdb/Iterator.java
  3. 10
      java/org/rocksdb/RocksDB.java

@ -142,7 +142,7 @@ public class RocksDBSample {
assert(false); //Should never reach here. assert(false); //Should never reach here.
} }
Iterator iterator = db.iterator(); Iterator iterator = db.newIterator();
boolean seekToFirstPassed = false; boolean seekToFirstPassed = false;
for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) {

@ -5,6 +5,17 @@
package org.rocksdb; package org.rocksdb;
/**
* An iterator yields a sequence of key/value pairs from a source.
* The following class defines the interface. Multiple implementations
* are provided by this library. In particular, iterators are provided
* to access the contents of a Table or a DB.
*
* Multiple threads can invoke const methods on an Iterator without
* external synchronization, but if any of the threads may call a
* non-const method, all threads accessing the same Iterator must use
* external synchronization.
*/
public class Iterator { public class Iterator {
private long nativeHandle_; private long nativeHandle_;
@ -12,51 +23,101 @@ public class Iterator {
nativeHandle_ = nativeHandle; nativeHandle_ = nativeHandle;
} }
/**
* An iterator is either positioned at a key/value pair, or
* not valid. This method returns true iff the iterator is valid.
* @return true if iterator is valid.
*/
public boolean isValid() { public boolean isValid() {
assert(isInitialized()); assert(isInitialized());
return isValid0(nativeHandle_); return isValid0(nativeHandle_);
} }
/**
* Position at the first key in the source. The iterator is Valid()
* after this call iff the source is not empty.
*/
public void seekToFirst() { public void seekToFirst() {
assert(isInitialized()); assert(isInitialized());
seekToFirst0(nativeHandle_); seekToFirst0(nativeHandle_);
} }
/**
* Position at the last key in the source. The iterator is
* Valid() after this call iff the source is not empty.
*/
public void seekToLast() { public void seekToLast() {
assert(isInitialized()); assert(isInitialized());
seekToLast0(nativeHandle_); seekToLast0(nativeHandle_);
} }
/**
* Moves to the next entry in the source. After this call, Valid() is
* true iff the iterator was not positioned at the last entry in the source.
* REQUIRES: Valid()
*/
public void next() { public void next() {
assert(isInitialized()); assert(isInitialized());
next0(nativeHandle_); next0(nativeHandle_);
} }
/**
* Moves to the previous entry in the source. After this call, Valid() is
* true iff the iterator was not positioned at the first entry in source.
* REQUIRES: Valid()
*/
public void prev() { public void prev() {
assert(isInitialized()); assert(isInitialized());
prev0(nativeHandle_); prev0(nativeHandle_);
} }
/**
* Return the key for the current entry. The underlying storage for
* the returned slice is valid only until the next modification of
* the iterator.
* REQUIRES: Valid()
* @return key for the current entry.
*/
public byte[] key() { public byte[] key() {
assert(isInitialized()); assert(isInitialized());
return key0(nativeHandle_); return key0(nativeHandle_);
} }
/**
* Return the value for the current entry. The underlying storage for
* the returned slice is valid only until the next modification of
* the iterator.
* REQUIRES: !AtEnd() && !AtStart()
* @return value for the current entry.
*/
public byte[] value() { public byte[] value() {
assert(isInitialized()); assert(isInitialized());
return value0(nativeHandle_); return value0(nativeHandle_);
} }
/**
* Position at the first key in the source that at or past target
* The iterator is Valid() after this call iff the source contains
* an entry that comes at or past target.
*/
public void seek(byte[] target) { public void seek(byte[] target) {
assert(isInitialized()); assert(isInitialized());
seek0(nativeHandle_, target, target.length); seek0(nativeHandle_, target, target.length);
} }
/**
* If an error has occurred, return it. Else return an ok status.
* If non-blocking IO is requested and this operation cannot be
* satisfied without doing some IO, then this returns Status::Incomplete().
*/
public void status(){ public void status(){
assert(isInitialized()); assert(isInitialized());
status0(nativeHandle_); status0(nativeHandle_);
} }
/**
* Deletes underlying C++ iterator pointer.
*/
public synchronized void close() { public synchronized void close() {
if(nativeHandle_ != 0) { if(nativeHandle_ != 0) {
close0(nativeHandle_); close0(nativeHandle_);

@ -136,7 +136,15 @@ public class RocksDB {
remove(nativeHandle_, writeOpt.nativeHandle_, key, key.length); remove(nativeHandle_, writeOpt.nativeHandle_, key, key.length);
} }
public Iterator iterator() { /**
* Return a heap-allocated iterator over the contents of the database.
* The result of newIterator() is initially invalid (caller must
* call one of the Seek methods on the iterator before using it).
*
* Caller should close the iterator when it is no longer needed.
* The returned iterator should be closed before this db is closed.
*/
public Iterator newIterator() {
return new Iterator(iterator0(nativeHandle_)); return new Iterator(iterator0(nativeHandle_));
} }

Loading…
Cancel
Save