Add doc + refactor + fix formatting

main
Ankit Gupta 11 years ago
parent 1d6c1e018f
commit dc28a726c1
  1. 14
      java/RocksDBSample.java
  2. 30
      java/org/rocksdb/Iterator.java
  3. 2
      java/org/rocksdb/Options.java
  4. 2
      java/org/rocksdb/RocksDB.java
  5. 126
      java/rocksjni/iterator.cc
  6. 8
      java/rocksjni/portal.h
  7. 2
      java/rocksjni/rocksjni.cc

@ -141,9 +141,9 @@ public class RocksDBSample {
System.out.println("Failed in call to geHistogramData()"); System.out.println("Failed in call to geHistogramData()");
assert(false); //Should never reach here. assert(false); //Should never reach here.
} }
Iterator iterator = db.iterator(); Iterator iterator = db.iterator();
boolean seekToFirstPassed = false; boolean seekToFirstPassed = false;
for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) {
iterator.status(); iterator.status();
@ -154,7 +154,7 @@ public class RocksDBSample {
if(seekToFirstPassed) { if(seekToFirstPassed) {
System.out.println("iterator seekToFirst tests passed."); System.out.println("iterator seekToFirst tests passed.");
} }
boolean seekToLastPassed = false; boolean seekToLastPassed = false;
for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) { for (iterator.seekToLast(); iterator.isValid(); iterator.prev()) {
iterator.status(); iterator.status();
@ -162,18 +162,18 @@ public class RocksDBSample {
assert(iterator.value() != null); assert(iterator.value() != null);
seekToLastPassed = true; seekToLastPassed = true;
} }
if(seekToLastPassed) { if(seekToLastPassed) {
System.out.println("iterator seekToLastPassed tests passed."); System.out.println("iterator seekToLastPassed tests passed.");
} }
iterator.seekToFirst(); iterator.seekToFirst();
iterator.seek(iterator.key()); iterator.seek(iterator.key());
assert(iterator.key() != null); assert(iterator.key() != null);
assert(iterator.value() != null); assert(iterator.value() != null);
System.out.println("iterator seek test passed."); System.out.println("iterator seek test passed.");
iterator.close(); iterator.close();
System.out.println("iterator tests passed."); System.out.println("iterator tests passed.");
} catch (RocksDBException e) { } catch (RocksDBException e) {

@ -7,70 +7,70 @@ package org.rocksdb;
public class Iterator { public class Iterator {
private long nativeHandle_; private long nativeHandle_;
public Iterator(long nativeHandle) { public Iterator(long nativeHandle) {
nativeHandle_ = nativeHandle; nativeHandle_ = nativeHandle;
} }
public boolean isValid() { public boolean isValid() {
assert(isInitialized()); assert(isInitialized());
return isValid0(nativeHandle_); return isValid0(nativeHandle_);
} }
public void seekToFirst() { public void seekToFirst() {
assert(isInitialized()); assert(isInitialized());
seekToFirst0(nativeHandle_); seekToFirst0(nativeHandle_);
} }
public void seekToLast() { public void seekToLast() {
assert(isInitialized()); assert(isInitialized());
seekToLast0(nativeHandle_); seekToLast0(nativeHandle_);
} }
public void next() { public void next() {
assert(isInitialized()); assert(isInitialized());
next0(nativeHandle_); next0(nativeHandle_);
} }
public void prev() { public void prev() {
assert(isInitialized()); assert(isInitialized());
prev0(nativeHandle_); prev0(nativeHandle_);
} }
public byte[] key() { public byte[] key() {
assert(isInitialized()); assert(isInitialized());
return key0(nativeHandle_); return key0(nativeHandle_);
} }
public byte[] value() { public byte[] value() {
assert(isInitialized()); assert(isInitialized());
return value0(nativeHandle_); return value0(nativeHandle_);
} }
public void seek(byte[] target) { public void seek(byte[] target) {
assert(isInitialized()); assert(isInitialized());
seek0(nativeHandle_, target, target.length); seek0(nativeHandle_, target, target.length);
} }
public void status(){ public void status(){
assert(isInitialized()); assert(isInitialized());
status0(nativeHandle_); status0(nativeHandle_);
} }
public synchronized void close() { public synchronized void close() {
if(nativeHandle_ != 0) { if(nativeHandle_ != 0) {
close0(nativeHandle_); close0(nativeHandle_);
} }
} }
@Override protected void finalize() { @Override protected void finalize() {
close(); close();
} }
private boolean isInitialized() { private boolean isInitialized() {
return (nativeHandle_ != 0); return (nativeHandle_ != 0);
} }
private native boolean isValid0(long handle); private native boolean isValid0(long handle);
private native void close0(long handle); private native void close0(long handle);
private native void seekToFirst0(long handle); private native void seekToFirst0(long handle);
@ -81,4 +81,4 @@ public class Iterator {
private native byte[] value0(long handle); private native byte[] value0(long handle);
private native void seek0(long handle, byte[] target, int targetLen); private native void seek0(long handle, byte[] target, int targetLen);
private native void status0(long handle); private native void status0(long handle);
} }

@ -508,7 +508,7 @@ public class Options {
return maxBackgroundCompactions(nativeHandle_); return maxBackgroundCompactions(nativeHandle_);
} }
/** /**
* Creates statistics object which collects metrics about database operations. * Creates statistics object which collects metrics about database operations.
Statistics objects should not be shared between DB instances as Statistics objects should not be shared between DB instances as
it does not use any locks to prevent concurrent updates. it does not use any locks to prevent concurrent updates.

@ -135,7 +135,7 @@ public class RocksDB {
throws RocksDBException { throws RocksDBException {
remove(nativeHandle_, writeOpt.nativeHandle_, key, key.length); remove(nativeHandle_, writeOpt.nativeHandle_, key, key.length);
} }
public Iterator iterator() { public Iterator iterator() {
return new Iterator(iterator0(nativeHandle_)); return new Iterator(iterator0(nativeHandle_));
} }

@ -14,53 +14,71 @@
#include "rocksjni/portal.h" #include "rocksjni/portal.h"
#include "rocksdb/iterator.h" #include "rocksdb/iterator.h"
/*
* Class: org_rocksdb_Iterator
* Method: isValid0
* Signature: (J)Z
*/
jboolean Java_org_rocksdb_Iterator_isValid0( jboolean Java_org_rocksdb_Iterator_isValid0(
JNIEnv* env, jobject jobj, jlong handle) { JNIEnv* env, jobject jobj, jlong handle) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr); return it->Valid();
return st->Valid();
} }
/*
* Class: org_rocksdb_Iterator
* Method: close0
* Signature: (J)V
*/
void Java_org_rocksdb_Iterator_seekToFirst0( void Java_org_rocksdb_Iterator_seekToFirst0(
JNIEnv* env, jobject jobj, jlong handle) { JNIEnv* env, jobject jobj, jlong handle) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr); it->SeekToFirst();
st->SeekToFirst();
} }
/*
* Class: org_rocksdb_Iterator
* Method: seekToFirst0
* Signature: (J)V
*/
void Java_org_rocksdb_Iterator_seekToLast0( void Java_org_rocksdb_Iterator_seekToLast0(
JNIEnv* env, jobject jobj, jlong handle) { JNIEnv* env, jobject jobj, jlong handle) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr); it->SeekToLast();
st->SeekToLast();
} }
/*
* Class: org_rocksdb_Iterator
* Method: seekToLast0
* Signature: (J)V
*/
void Java_org_rocksdb_Iterator_next0( void Java_org_rocksdb_Iterator_next0(
JNIEnv* env, jobject jobj, jlong handle) { JNIEnv* env, jobject jobj, jlong handle) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr); it->Next();
st->Next();
} }
/*
* Class: org_rocksdb_Iterator
* Method: next0
* Signature: (J)V
*/
void Java_org_rocksdb_Iterator_prev0( void Java_org_rocksdb_Iterator_prev0(
JNIEnv* env, jobject jobj, jlong handle) { JNIEnv* env, jobject jobj, jlong handle) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr); it->Prev();
st->Prev();
} }
/*
* Class: org_rocksdb_Iterator
* Method: prev0
* Signature: (J)V
*/
jbyteArray Java_org_rocksdb_Iterator_key0( jbyteArray Java_org_rocksdb_Iterator_key0(
JNIEnv* env, jobject jobj, jlong handle) { JNIEnv* env, jobject jobj, jlong handle) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr); rocksdb::Slice key_slice = it->key();
rocksdb::Slice key_slice = st->key();
jbyteArray jkey = env->NewByteArray(key_slice.size()); jbyteArray jkey = env->NewByteArray(key_slice.size());
env->SetByteArrayRegion( env->SetByteArrayRegion(
jkey, 0, key_slice.size(), jkey, 0, key_slice.size(),
@ -68,13 +86,16 @@ jbyteArray Java_org_rocksdb_Iterator_key0(
return jkey; return jkey;
} }
/*
* Class: org_rocksdb_Iterator
* Method: key0
* Signature: (J)[B
*/
jbyteArray Java_org_rocksdb_Iterator_value0( jbyteArray Java_org_rocksdb_Iterator_value0(
JNIEnv* env, jobject jobj, jlong handle) { JNIEnv* env, jobject jobj, jlong handle) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr); rocksdb::Slice value_slice = it->value();
rocksdb::Slice value_slice = st->value();
jbyteArray jvalue = env->NewByteArray(value_slice.size()); jbyteArray jvalue = env->NewByteArray(value_slice.size());
env->SetByteArrayRegion( env->SetByteArrayRegion(
jvalue, 0, value_slice.size(), jvalue, 0, value_slice.size(),
@ -82,41 +103,50 @@ jbyteArray Java_org_rocksdb_Iterator_value0(
return jvalue; return jvalue;
} }
/*
* Class: org_rocksdb_Iterator
* Method: value0
* Signature: (J)[B
*/
void Java_org_rocksdb_Iterator_seek0( void Java_org_rocksdb_Iterator_seek0(
JNIEnv* env, jobject jobj, jlong handle, JNIEnv* env, jobject jobj, jlong handle,
jbyteArray jtarget, jint jtarget_len) { jbyteArray jtarget, jint jtarget_len) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr);
jbyte* target = env->GetByteArrayElements(jtarget, 0); jbyte* target = env->GetByteArrayElements(jtarget, 0);
rocksdb::Slice target_slice( rocksdb::Slice target_slice(
reinterpret_cast<char*>(target), jtarget_len); reinterpret_cast<char*>(target), jtarget_len);
st->Seek(target_slice); it->Seek(target_slice);
env->ReleaseByteArrayElements(jtarget, target, JNI_ABORT); env->ReleaseByteArrayElements(jtarget, target, JNI_ABORT);
} }
/*
* Class: org_rocksdb_Iterator
* Method: seek0
* Signature: (J[BI)V
*/
void Java_org_rocksdb_Iterator_status0( void Java_org_rocksdb_Iterator_status0(
JNIEnv* env, jobject jobj, jlong handle) { JNIEnv* env, jobject jobj, jlong handle) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr); rocksdb::Status s = it->status();
rocksdb::Status s = st->status();
if (s.ok()) { if (s.ok()) {
return; return;
} }
rocksdb::RocksDBExceptionJni::ThrowNew(env, s); rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
} }
/*
* Class: org_rocksdb_Iterator
* Method: status0
* Signature: (J)V
*/
void Java_org_rocksdb_Iterator_close0( void Java_org_rocksdb_Iterator_close0(
JNIEnv* env, jobject jobj, jlong handle) { JNIEnv* env, jobject jobj, jlong handle) {
auto st = reinterpret_cast<rocksdb::Iterator*>(handle); auto it = rocksdb::IteratorJni::getIterator(handle);
assert(st != nullptr); delete it;
delete st;
rocksdb::IteratorJni::setHandle(env, jobj, nullptr); rocksdb::IteratorJni::setHandle(env, jobj, nullptr);
} }

@ -245,6 +245,14 @@ class IteratorJni {
jobj, getHandleFieldID(env), jobj, getHandleFieldID(env),
reinterpret_cast<jlong>(op)); reinterpret_cast<jlong>(op));
} }
// Cast long to rocksdb::Iterator pointer.
static rocksdb::Iterator* getIterator(jlong handle) {
auto it = reinterpret_cast<rocksdb::Iterator*>(handle);
assert(it != nullptr);
return it;
}
}; };
} // namespace rocksdb } // namespace rocksdb
#endif // JAVA_ROCKSJNI_PORTAL_H_ #endif // JAVA_ROCKSJNI_PORTAL_H_

@ -302,4 +302,4 @@ jlong Java_org_rocksdb_RocksDB_iterator0(
auto db = reinterpret_cast<rocksdb::DB*>(db_handle); auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
rocksdb::Iterator* iterator = db->NewIterator(rocksdb::ReadOptions()); rocksdb::Iterator* iterator = db->NewIterator(rocksdb::ReadOptions());
return reinterpret_cast<jlong>(iterator); return reinterpret_cast<jlong>(iterator);
} }

Loading…
Cancel
Save