diff --git a/java/RocksDBSample.java b/java/RocksDBSample.java index 406102426..f4afe79cf 100644 --- a/java/RocksDBSample.java +++ b/java/RocksDBSample.java @@ -3,8 +3,7 @@ // LICENSE file in the root directory of this source tree. An additional grant // of patent rights can be found in the PATENTS file in the same directory. -import java.util.*; -import java.lang.*; +import java.util.Arrays; import org.rocksdb.*; import org.rocksdb.util.SizeUnit; import java.io.IOException; @@ -142,6 +141,11 @@ public class RocksDBSample { System.out.println("Failed in call to geHistogramData()"); assert(false); //Should never reach here. } + + Iterator iterator = db.iterator(); + iterator.seekToFirst(); + assert(iterator.isValid()); + iterator.close(); } catch (RocksDBException e) { System.err.println(e); } diff --git a/java/org/rocksdb/Iterator.java b/java/org/rocksdb/Iterator.java new file mode 100644 index 000000000..945514230 --- /dev/null +++ b/java/org/rocksdb/Iterator.java @@ -0,0 +1,42 @@ +// Copyright (c) 2014, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. + +package org.rocksdb; + +public class Iterator { + private long nativeHandle_; + + public Iterator(long nativeHandle) { + nativeHandle_ = nativeHandle; + } + + public boolean isValid() { + assert(isInitialized()); + return isValid0(nativeHandle_); + } + + public void seekToFirst() { + assert(isInitialized()); + seekToFirst0(nativeHandle_); + } + + public synchronized void close() { + if(nativeHandle_ != 0) { + close0(nativeHandle_); + } + } + + @Override protected void finalize() { + close(); + } + + private boolean isInitialized() { + return (nativeHandle_ != 0); + } + + private native boolean isValid0(long handle); + private native void close0(long handle); + private native void seekToFirst0(long handle); +} \ No newline at end of file diff --git a/java/org/rocksdb/RocksDB.java b/java/org/rocksdb/RocksDB.java index 9f93f9986..586331e04 100644 --- a/java/org/rocksdb/RocksDB.java +++ b/java/org/rocksdb/RocksDB.java @@ -5,7 +5,6 @@ package org.rocksdb; -import java.util.*; import java.io.Closeable; import java.io.IOException; @@ -136,6 +135,10 @@ public class RocksDB { throws RocksDBException { remove(nativeHandle_, writeOpt.nativeHandle_, key, key.length); } + + public Iterator iterator() { + return new Iterator(iterator0(nativeHandle_)); + } @Override protected void finalize() { close(); @@ -170,6 +173,7 @@ public class RocksDB { protected native void remove( long handle, long writeOptHandle, byte[] key, int keyLen) throws RocksDBException; + protected native long iterator0(long optHandle); protected native void close0(); protected long nativeHandle_; diff --git a/java/rocksjni/iterator.cc b/java/rocksjni/iterator.cc new file mode 100644 index 000000000..9f6edd0ee --- /dev/null +++ b/java/rocksjni/iterator.cc @@ -0,0 +1,41 @@ +// Copyright (c) 2014, Facebook, Inc. All rights reserved. +// This source code is licensed under the BSD-style license found in the +// LICENSE file in the root directory of this source tree. An additional grant +// of patent rights can be found in the PATENTS file in the same directory. +// +// This file implements the "bridge" between Java and C++ and enables +// calling c++ rocksdb::Iterator methods from Java side. + +#include +#include +#include + +#include "include/org_rocksdb_Iterator.h" +#include "rocksjni/portal.h" +#include "rocksdb/iterator.h" + +jboolean Java_org_rocksdb_Iterator_isValid0( + JNIEnv* env, jobject jobj, jlong handle) { + auto st = reinterpret_cast(handle); + assert(st != nullptr); + + return st->Valid(); +} + +void Java_org_rocksdb_Iterator_seekToFirst0( + JNIEnv* env, jobject jobj, jlong handle) { + auto st = reinterpret_cast(handle); + assert(st != nullptr); + + st->SeekToFirst(); +} + +void Java_org_rocksdb_Iterator_close0( + JNIEnv* env, jobject jobj, jlong handle) { + auto st = reinterpret_cast(handle); + assert(st != nullptr); + + delete st; + + rocksdb::IteratorJni::setHandle(env, jobj, nullptr); +} \ No newline at end of file diff --git a/java/rocksjni/portal.h b/java/rocksjni/portal.h index 024062c0b..32e033aaf 100644 --- a/java/rocksjni/portal.h +++ b/java/rocksjni/portal.h @@ -213,5 +213,38 @@ class BackupableDBOptionsJni { reinterpret_cast(op)); } }; + +class IteratorJni { + public: + // Get the java class id of org.rocksdb.Iteartor. + static jclass getJClass(JNIEnv* env) { + static jclass jclazz = env->FindClass("org/rocksdb/Iterator"); + assert(jclazz != nullptr); + return jclazz; + } + + // Get the field id of the member variable of org.rocksdb.Iterator + // that stores the pointer to rocksdb::Iterator + static jfieldID getHandleFieldID(JNIEnv* env) { + static jfieldID fid = env->GetFieldID( + getJClass(env), "nativeHandle_", "J"); + assert(fid != nullptr); + return fid; + } + + // Get the pointer to rocksdb::Iterator + static rocksdb::Iterator* getHandle(JNIEnv* env, jobject jobj) { + return reinterpret_cast( + env->GetLongField(jobj, getHandleFieldID(env))); + } + + // Pass the rocksdb::Iterator pointer to the java side. + static void setHandle( + JNIEnv* env, jobject jobj, rocksdb::Iterator* op) { + env->SetLongField( + jobj, getHandleFieldID(env), + reinterpret_cast(op)); + } +}; } // namespace rocksdb #endif // JAVA_ROCKSJNI_PORTAL_H_ diff --git a/java/rocksjni/rocksjni.cc b/java/rocksjni/rocksjni.cc index 6602f0cc7..4e4093294 100644 --- a/java/rocksjni/rocksjni.cc +++ b/java/rocksjni/rocksjni.cc @@ -296,3 +296,10 @@ void Java_org_rocksdb_RocksDB_close0( rocksdb::RocksDBJni::setHandle(env, java_db, nullptr); } + +jlong Java_org_rocksdb_RocksDB_iterator0( + JNIEnv* env, jobject jdb, jlong db_handle) { + auto db = reinterpret_cast(db_handle); + rocksdb::Iterator* iterator = db->NewIterator(rocksdb::ReadOptions()); + return reinterpret_cast(iterator); +} \ No newline at end of file diff --git a/java/rocksjni/statistics.cc b/java/rocksjni/statistics.cc index 546b5fec7..bf170c6de 100644 --- a/java/rocksjni/statistics.cc +++ b/java/rocksjni/statistics.cc @@ -13,7 +13,6 @@ #include "include/org_rocksdb_Statistics.h" #include "rocksjni/portal.h" #include "rocksdb/statistics.h" -#include /* * Class: org_rocksdb_Statistics