Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
11 years ago
|
|
|
// 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.
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.lang.*;
|
|
|
|
import org.rocksdb.*;
|
|
|
|
import org.rocksdb.util.SizeUnit;
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
11 years ago
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class RocksDBSample {
|
|
|
|
static {
|
|
|
|
System.loadLibrary("rocksdbjni");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
if (args.length < 1) {
|
|
|
|
System.out.println("usage: RocksDBSample db_path");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
String db_path = args[0];
|
|
|
|
String db_path_not_found = db_path + "_not_found";
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
11 years ago
|
|
|
|
|
|
|
System.out.println("RocksDBSample");
|
|
|
|
RocksDB db = null;
|
|
|
|
Options options = new Options();
|
|
|
|
try {
|
|
|
|
db = RocksDB.open(options, db_path_not_found);
|
|
|
|
assert(false);
|
|
|
|
} catch (RocksDBException e) {
|
|
|
|
System.out.format("caught the expceted exception -- %s\n", e);
|
|
|
|
assert(db == null);
|
|
|
|
}
|
|
|
|
|
|
|
|
options.setCreateIfMissing(true)
|
|
|
|
.createStatistics()
|
|
|
|
.setWriteBufferSize(8 * SizeUnit.KB)
|
|
|
|
.setMaxWriteBufferNumber(3)
|
|
|
|
.setDisableSeekCompaction(true)
|
|
|
|
.setBlockSize(64 * SizeUnit.KB)
|
|
|
|
.setMaxBackgroundCompactions(10);
|
|
|
|
Statistics stats = new Statistics(options.statisticsPtr());
|
|
|
|
|
|
|
|
assert(options.createIfMissing() == true);
|
|
|
|
assert(options.writeBufferSize() == 8 * SizeUnit.KB);
|
|
|
|
assert(options.maxWriteBufferNumber() == 3);
|
|
|
|
assert(options.disableSeekCompaction() == true);
|
|
|
|
assert(options.blockSize() == 64 * SizeUnit.KB);
|
|
|
|
assert(options.maxBackgroundCompactions() == 10);
|
|
|
|
|
|
|
|
try {
|
|
|
|
db = RocksDB.open(options, db_path_not_found);
|
|
|
|
db.put("hello".getBytes(), "world".getBytes());
|
|
|
|
byte[] value = db.get("hello".getBytes());
|
|
|
|
assert("world".equals(new String(value)));
|
|
|
|
} catch (RocksDBException e) {
|
|
|
|
System.out.format("[ERROR] caught the unexpceted exception -- %s\n", e);
|
|
|
|
assert(db == null);
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
// be sure to release the c++ pointer
|
|
|
|
db.close();
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
11 years ago
|
|
|
|
|
|
|
try {
|
|
|
|
db = RocksDB.open(options, db_path);
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
11 years ago
|
|
|
db.put("hello".getBytes(), "world".getBytes());
|
|
|
|
byte[] value = db.get("hello".getBytes());
|
|
|
|
System.out.format("Get('hello') = %s\n",
|
|
|
|
new String(value));
|
|
|
|
|
|
|
|
for (int i = 1; i <= 9; ++i) {
|
|
|
|
for (int j = 1; j <= 9; ++j) {
|
|
|
|
db.put(String.format("%dx%d", i, j).getBytes(),
|
|
|
|
String.format("%d", i * j).getBytes());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i <= 9; ++i) {
|
|
|
|
for (int j = 1; j <= 9; ++j) {
|
|
|
|
System.out.format("%s ", new String(db.get(
|
|
|
|
String.format("%dx%d", i, j).getBytes())));
|
|
|
|
}
|
|
|
|
System.out.println("");
|
|
|
|
}
|
|
|
|
|
|
|
|
value = db.get("1x1".getBytes());
|
|
|
|
assert(value != null);
|
|
|
|
value = db.get("world".getBytes());
|
|
|
|
assert(value == null);
|
|
|
|
|
|
|
|
byte[] testKey = "asdf".getBytes();
|
|
|
|
byte[] testValue =
|
|
|
|
"asdfghjkl;'?><MNBVCXZQWERTYUIOP{+_)(*&^%$#@".getBytes();
|
|
|
|
db.put(testKey, testValue);
|
|
|
|
byte[] testResult = db.get(testKey);
|
|
|
|
assert(testResult != null);
|
|
|
|
assert(Arrays.equals(testValue, testResult));
|
|
|
|
assert(new String(testValue).equals(new String(testResult)));
|
|
|
|
|
|
|
|
byte[] insufficientArray = new byte[10];
|
|
|
|
byte[] enoughArray = new byte[50];
|
|
|
|
int len;
|
|
|
|
len = db.get(testKey, insufficientArray);
|
|
|
|
assert(len > insufficientArray.length);
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
11 years ago
|
|
|
len = db.get("asdfjkl;".getBytes(), enoughArray);
|
|
|
|
assert(len == RocksDB.NOT_FOUND);
|
|
|
|
len = db.get(testKey, enoughArray);
|
|
|
|
assert(len == testValue.length);
|
|
|
|
|
|
|
|
db.remove(testKey);
|
|
|
|
len = db.get(testKey, enoughArray);
|
|
|
|
assert(len == RocksDB.NOT_FOUND);
|
|
|
|
|
|
|
|
// repeat the test with WriteOptions
|
|
|
|
WriteOptions writeOpts = new WriteOptions();
|
|
|
|
writeOpts.setSync(true);
|
|
|
|
writeOpts.setDisableWAL(true);
|
|
|
|
db.put(writeOpts, testKey, testValue);
|
|
|
|
len = db.get(testKey, enoughArray);
|
|
|
|
assert(len == testValue.length);
|
|
|
|
assert(new String(testValue).equals(
|
|
|
|
new String(enoughArray, 0, len)));
|
|
|
|
writeOpts.dispose();
|
|
|
|
|
|
|
|
try {
|
|
|
|
for(StatisticsType statsType : StatisticsType.values()) {
|
|
|
|
stats.getTickerCount(statsType);
|
|
|
|
}
|
|
|
|
System.out.println("getTickerCount() passed.");
|
|
|
|
}
|
|
|
|
catch(Exception e) {
|
|
|
|
System.out.println("Failed in call to getTickerCount()");
|
|
|
|
assert(false); //Should never reach here.
|
|
|
|
}
|
|
|
|
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
11 years ago
|
|
|
} catch (RocksDBException e) {
|
|
|
|
System.err.println(e);
|
|
|
|
}
|
|
|
|
if (db != null) {
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
// be sure to dispose c++ pointers
|
|
|
|
options.dispose();
|
Add a jni library for rocksdb which supports Open, Get, Put, and Close.
Summary:
This diff contains a simple jni library for rocksdb which supports open, get,
put and closeusing default options (including Options, ReadOptions, and
WriteOptions.) In the usual case, Java developers can use the c++ rocksdb
library in the way similar to the following:
RocksDB db = RocksDB.open(path_to_db);
...
db.put("hello".getBytes(), "world".getBytes();
byte[] value = db.get("hello".getBytes());
...
db.close();
Specifically, this diff has the following major classes:
* RocksDB: a Java wrapper class which forwards the operations
from the java side to c++ rocksdb library.
* RocksDBException: ncapsulates the error of an operation.
This exception type is used to describe an internal error from
the c++ rocksdb library.
This diff also include a simple java sample code calling c++ rocksdb library.
To build the rocksdb jni library, simply run make jni, and make jtest will try to
build and run the sample code.
Note that if the rocksdb is not built with the default glibc that Java uses,
java will try to load the wrong glibc during the run time. As a result,
the sample code might not work properly during the run time.
Test Plan:
* make jni
* make jtest
Reviewers: haobo, dhruba, sdong, igor, ljin
Reviewed By: dhruba
CC: leveldb, xjin
Differential Revision: https://reviews.facebook.net/D17109
11 years ago
|
|
|
}
|
|
|
|
}
|