Summary: * [java] Add a java api for rocksdb::Options, currently only supports create_if_missing. * [java] Add a test for RocksDBException in RocksDBSample. Test Plan: make jtest Reviewers: haobo, sdong Reviewed By: haobo CC: leveldb, dhruba Differential Revision: https://reviews.facebook.net/D17385main
parent
e0a87c4cf1
commit
8c4a3bfa5b
@ -0,0 +1,69 @@ |
||||
// 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; |
||||
|
||||
/** |
||||
* Options to control the behavior of a database. It will be used |
||||
* during the creation of a RocksDB (i.e., RocksDB::Open()). |
||||
* |
||||
* Note that dispose() must be called before an Options instance |
||||
* become out-of-scope to release the allocated memory in c++. |
||||
*/ |
||||
public class Options { |
||||
/** |
||||
* Construct options for opening a RocksDB. |
||||
* |
||||
* This constructor will create (by allocating a block of memory) |
||||
* an rocksdb::Options in the c++ side. |
||||
*/ |
||||
public Options() { |
||||
nativeHandle_ = 0; |
||||
newOptions(); |
||||
} |
||||
|
||||
/** |
||||
* If this value is set to true, then the database will be created |
||||
* if it is missing during RocksDB::Open(). |
||||
* Default: false |
||||
* |
||||
* @param flag a flag indicating whether to create a database the |
||||
* specified database in RocksDB::Open() operation is missing. |
||||
* @see RocksDB::Open() |
||||
*/ |
||||
public void setCreateIfMissing(boolean flag) { |
||||
assert(nativeHandle_ != 0); |
||||
setCreateIfMissing(nativeHandle_, flag); |
||||
} |
||||
|
||||
/** |
||||
* Return true if the create_if_missing flag is set to true. |
||||
* If true, the database will be created if it is missing. |
||||
* |
||||
* @return return true if the create_if_missing flag is set to true. |
||||
* @see setCreateIfMissing() |
||||
*/ |
||||
public boolean craeteIfMissing() { |
||||
assert(nativeHandle_ != 0); |
||||
return createIfMissing(nativeHandle_); |
||||
} |
||||
|
||||
/** |
||||
* Release the memory allocated for the current instance |
||||
* in the c++ side. |
||||
*/ |
||||
public synchronized void dispose() { |
||||
if (nativeHandle_ != 0) { |
||||
dispose0(); |
||||
} |
||||
} |
||||
|
||||
private native void newOptions(); |
||||
private native void dispose0(); |
||||
private native void setCreateIfMissing(long handle, boolean flag); |
||||
private native boolean createIfMissing(long handle); |
||||
|
||||
long nativeHandle_; |
||||
} |
@ -0,0 +1,57 @@ |
||||
// 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++ for rocksdb::Options.
|
||||
|
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <jni.h> |
||||
#include <string> |
||||
|
||||
#include "include/org_rocksdb_Options.h" |
||||
#include "rocksjni/portal.h" |
||||
#include "rocksdb/db.h" |
||||
|
||||
/*
|
||||
* Class: org_rocksdb_Options |
||||
* Method: newOptions |
||||
* Signature: ()V |
||||
*/ |
||||
void Java_org_rocksdb_Options_newOptions(JNIEnv* env, jobject jobj) { |
||||
rocksdb::Options* op = new rocksdb::Options(); |
||||
rocksdb::OptionsJni::setHandle(env, jobj, op); |
||||
} |
||||
|
||||
/*
|
||||
* Class: org_rocksdb_Options |
||||
* Method: dispose0 |
||||
* Signature: ()V |
||||
*/ |
||||
void Java_org_rocksdb_Options_dispose0(JNIEnv* env, jobject jobj) { |
||||
rocksdb::Options* op = rocksdb::OptionsJni::getHandle(env, jobj); |
||||
delete op; |
||||
|
||||
rocksdb::OptionsJni::setHandle(env, jobj, op); |
||||
} |
||||
|
||||
/*
|
||||
* Class: org_rocksdb_Options |
||||
* Method: setCreateIfMissing |
||||
* Signature: (JZ)V |
||||
*/ |
||||
void Java_org_rocksdb_Options_setCreateIfMissing( |
||||
JNIEnv* env, jobject jobj, jlong jhandle, jboolean flag) { |
||||
reinterpret_cast<rocksdb::Options*>(jhandle)->create_if_missing = flag; |
||||
} |
||||
|
||||
/*
|
||||
* Class: org_rocksdb_Options |
||||
* Method: createIfMissing |
||||
* Signature: (J)Z |
||||
*/ |
||||
jboolean Java_org_rocksdb_Options_createIfMissing( |
||||
JNIEnv* env, jobject jobj, jlong jhandle) { |
||||
return reinterpret_cast<rocksdb::Options*>(jhandle)->create_if_missing; |
||||
} |
Loading…
Reference in new issue