Skip to content

Commit e316af5

Browse files
committed
[Java] Add Java binding and Java test for ReadOptions.
Summary: Add Java binding and test for rocksdb::ReadOptions. Test Plan: make rocksdbjava make jtest Reviewers: haobo, dhruba, sdong, ankgup87, rsumbaly, swapnilghike, zzbennett Reviewed By: haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D18129
1 parent d0939cd commit e316af5

File tree

8 files changed

+476
-17
lines changed

8 files changed

+476
-17
lines changed

java/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
NATIVE_JAVA_CLASSES = org.rocksdb.RocksDB org.rocksdb.Options org.rocksdb.WriteBatch org.rocksdb.WriteBatchInternal org.rocksdb.WriteBatchTest org.rocksdb.WriteOptions org.rocksdb.BackupableDB org.rocksdb.BackupableDBOptions org.rocksdb.Statistics org.rocksdb.Iterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig
1+
NATIVE_JAVA_CLASSES = org.rocksdb.RocksDB org.rocksdb.Options org.rocksdb.WriteBatch org.rocksdb.WriteBatchInternal org.rocksdb.WriteBatchTest org.rocksdb.WriteOptions org.rocksdb.BackupableDB org.rocksdb.BackupableDBOptions org.rocksdb.Statistics org.rocksdb.Iterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig org.rocksdb.ReadOptions
22
NATIVE_INCLUDE = ./include
33
ROCKSDB_JAR = rocksdbjni.jar
44

@@ -25,6 +25,7 @@ test: java
2525
java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.WriteBatchTest
2626
java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.BackupableDBTest
2727
java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.OptionsTest
28+
java -ea -Djava.library.path=.:../ -cp "$(ROCKSDB_JAR):.:./*" org.rocksdb.test.ReadOptionsTest
2829

2930
db_bench: java
3031
javac org/rocksdb/benchmark/*.java

java/RocksDBSample.java

+17
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ public static void main(String[] args) {
8484
// be sure to release the c++ pointer
8585
db.close();
8686

87+
ReadOptions readOptions = new ReadOptions();
88+
readOptions.setFillCache(false);
89+
8790
try {
8891
db = RocksDB.open(options, db_path);
8992
db.put("hello".getBytes(), "world".getBytes());
@@ -110,6 +113,8 @@ public static void main(String[] args) {
110113
assert(value != null);
111114
value = db.get("world".getBytes());
112115
assert(value == null);
116+
value = db.get(readOptions, "world".getBytes());
117+
assert(value == null);
113118

114119
byte[] testKey = "asdf".getBytes();
115120
byte[] testValue =
@@ -119,6 +124,10 @@ public static void main(String[] args) {
119124
assert(testResult != null);
120125
assert(Arrays.equals(testValue, testResult));
121126
assert(new String(testValue).equals(new String(testResult)));
127+
testResult = db.get(readOptions, testKey);
128+
assert(testResult != null);
129+
assert(Arrays.equals(testValue, testResult));
130+
assert(new String(testValue).equals(new String(testResult)));
122131

123132
byte[] insufficientArray = new byte[10];
124133
byte[] enoughArray = new byte[50];
@@ -130,6 +139,13 @@ public static void main(String[] args) {
130139
len = db.get(testKey, enoughArray);
131140
assert(len == testValue.length);
132141

142+
len = db.get(readOptions, testKey, insufficientArray);
143+
assert(len > insufficientArray.length);
144+
len = db.get(readOptions, "asdfjkl;".getBytes(), enoughArray);
145+
assert(len == RocksDB.NOT_FOUND);
146+
len = db.get(readOptions, testKey, enoughArray);
147+
assert(len == testValue.length);
148+
133149
db.remove(testKey);
134150
len = db.get(testKey, enoughArray);
135151
assert(len == RocksDB.NOT_FOUND);
@@ -207,5 +223,6 @@ public static void main(String[] args) {
207223
}
208224
// be sure to dispose c++ pointers
209225
options.dispose();
226+
readOptions.dispose();
210227
}
211228
}

java/org/rocksdb/ReadOptions.java

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under the BSD-style license found in the
3+
// LICENSE file in the root directory of this source tree. An additional grant
4+
// of patent rights can be found in the PATENTS file in the same directory.
5+
6+
package org.rocksdb;
7+
8+
/**
9+
* The class that controls the get behavior.
10+
*
11+
* Note that dispose() must be called before an Options instance
12+
* become out-of-scope to release the allocated memory in c++.
13+
*/
14+
public class ReadOptions {
15+
public ReadOptions() {
16+
nativeHandle_ = 0;
17+
newReadOptions();
18+
}
19+
private native void newReadOptions();
20+
21+
/**
22+
* Release the memory allocated for the current instance
23+
* in the c++ side.
24+
*
25+
* Calling other methods after dispose() leads to undefined behavior.
26+
*/
27+
public synchronized void dispose() {
28+
if (isInitialized()) {
29+
dispose(nativeHandle_);
30+
}
31+
}
32+
private native void dispose(long handle);
33+
34+
/**
35+
* If true, all data read from underlying storage will be
36+
* verified against corresponding checksums.
37+
* Default: true
38+
*
39+
* @return true if checksum verification is on.
40+
*/
41+
public boolean verifyChecksums() {
42+
assert(isInitialized());
43+
return verifyChecksums(nativeHandle_);
44+
}
45+
private native boolean verifyChecksums(long handle);
46+
47+
/**
48+
* If true, all data read from underlying storage will be
49+
* verified against corresponding checksums.
50+
* Default: true
51+
*
52+
* @param verifyChecksums if true, then checksum verification
53+
* will be performed on every read.
54+
* @return the reference to the current ReadOptions.
55+
*/
56+
public ReadOptions setVerifyChecksums(boolean verifyChecksums) {
57+
assert(isInitialized());
58+
setVerifyChecksums(nativeHandle_, verifyChecksums);
59+
return this;
60+
}
61+
private native void setVerifyChecksums(
62+
long handle, boolean verifyChecksums);
63+
64+
// TODO(yhchiang): this option seems to be block-based table only.
65+
// move this to a better place?
66+
/**
67+
* Fill the cache when loading the block-based sst formated db.
68+
* Callers may wish to set this field to false for bulk scans.
69+
* Default: true
70+
*
71+
* @return true if the fill-cache behavior is on.
72+
*/
73+
public boolean fillCache() {
74+
assert(isInitialized());
75+
return fillCache(nativeHandle_);
76+
}
77+
private native boolean fillCache(long handle);
78+
79+
/**
80+
* Fill the cache when loading the block-based sst formated db.
81+
* Callers may wish to set this field to false for bulk scans.
82+
* Default: true
83+
*
84+
* @param fillCache if true, then fill-cache behavior will be
85+
* performed.
86+
* @return the reference to the current ReadOptions.
87+
*/
88+
public ReadOptions setFillCache(boolean fillCache) {
89+
assert(isInitialized());
90+
setFillCache(nativeHandle_, fillCache);
91+
return this;
92+
}
93+
private native void setFillCache(
94+
long handle, boolean fillCache);
95+
96+
/**
97+
* If this option is set and memtable implementation allows, Seek
98+
* might only return keys with the same prefix as the seek-key
99+
* Default: false
100+
*
101+
* @return true if prefix-seek is enabled.
102+
*/
103+
public boolean prefixSeek() {
104+
assert(isInitialized());
105+
return prefixSeek(nativeHandle_);
106+
}
107+
private native boolean prefixSeek(long handle);
108+
109+
/**
110+
* If this option is set and memtable implementation allows, Seek
111+
* might only return keys with the same prefix as the seek-key
112+
*
113+
* @param prefixSeek if true, then prefix-seek will be enabled.
114+
* @return the reference to the current ReadOptions.
115+
*/
116+
public ReadOptions setPrefixSeek(boolean prefixSeek) {
117+
assert(isInitialized());
118+
setPrefixSeek(nativeHandle_, prefixSeek);
119+
return this;
120+
}
121+
private native void setPrefixSeek(
122+
long handle, boolean prefixSeek);
123+
124+
/**
125+
* Specify to create a tailing iterator -- a special iterator that has a
126+
* view of the complete database (i.e. it can also be used to read newly
127+
* added data) and is optimized for sequential reads. It will return records
128+
* that were inserted into the database after the creation of the iterator.
129+
* Default: false
130+
* Not supported in ROCKSDB_LITE mode!
131+
*
132+
* @return true if tailing iterator is enabled.
133+
*/
134+
public boolean tailing() {
135+
assert(isInitialized());
136+
return tailing(nativeHandle_);
137+
}
138+
private native boolean tailing(long handle);
139+
140+
/**
141+
* Specify to create a tailing iterator -- a special iterator that has a
142+
* view of the complete database (i.e. it can also be used to read newly
143+
* added data) and is optimized for sequential reads. It will return records
144+
* that were inserted into the database after the creation of the iterator.
145+
* Default: false
146+
* Not supported in ROCKSDB_LITE mode!
147+
*
148+
* @param tailing if true, then tailing iterator will be enabled.
149+
* @return the reference to the current ReadOptions.
150+
*/
151+
public ReadOptions setTailing(boolean tailing) {
152+
assert(isInitialized());
153+
setTailing(nativeHandle_, tailing);
154+
return this;
155+
}
156+
private native void setTailing(
157+
long handle, boolean tailing);
158+
159+
protected long nativeHandle_;
160+
161+
private boolean isInitialized() {
162+
return nativeHandle_ != 0;
163+
}
164+
}
165+

java/org/rocksdb/RocksDB.java

+41-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public void put(byte[] key, byte[] value) throws RocksDBException {
7575
*/
7676
public void put(WriteOptions writeOpts, byte[] key, byte[] value)
7777
throws RocksDBException {
78-
put(nativeHandle_, writeOpts.nativeHandle_, key, key.length, value, value.length);
78+
put(nativeHandle_, writeOpts.nativeHandle_,
79+
key, key.length, value, value.length);
7980
}
8081

8182
/**
@@ -102,6 +103,24 @@ public int get(byte[] key, byte[] value) throws RocksDBException {
102103
return get(nativeHandle_, key, key.length, value, value.length);
103104
}
104105

106+
/**
107+
* Get the value associated with the specified key.
108+
*
109+
* @param key the key to retrieve the value.
110+
* @param value the out-value to receive the retrieved value.
111+
* @return The size of the actual value that matches the specified
112+
* {@code key} in byte. If the return value is greater than the
113+
* length of {@code value}, then it indicates that the size of the
114+
* input buffer {@code value} is insufficient and partial result will
115+
* be returned. RocksDB.NOT_FOUND will be returned if the value not
116+
* found.
117+
*/
118+
public int get(ReadOptions opt, byte[] key, byte[] value)
119+
throws RocksDBException {
120+
return get(nativeHandle_, opt.nativeHandle_,
121+
key, key.length, value, value.length);
122+
}
123+
105124
/**
106125
* The simplified version of get which returns a new byte array storing
107126
* the value associated with the specified input key if any. null will be
@@ -117,6 +136,21 @@ public byte[] get(byte[] key) throws RocksDBException {
117136
return get(nativeHandle_, key, key.length);
118137
}
119138

139+
/**
140+
* The simplified version of get which returns a new byte array storing
141+
* the value associated with the specified input key if any. null will be
142+
* returned if the specified key is not found.
143+
*
144+
* @param key the key retrieve the value.
145+
* @return a byte array storing the value associated with the input key if
146+
* any. null if it does not find the specified key.
147+
*
148+
* @see RocksDBException
149+
*/
150+
public byte[] get(ReadOptions opt, byte[] key) throws RocksDBException {
151+
return get(nativeHandle_, opt.nativeHandle_, key, key.length);
152+
}
153+
120154
/**
121155
* Remove the database entry (if any) for "key". Returns OK on
122156
* success, and a non-OK status on error. It is not an error if "key"
@@ -176,8 +210,14 @@ protected native void write(
176210
protected native int get(
177211
long handle, byte[] key, int keyLen,
178212
byte[] value, int valueLen) throws RocksDBException;
213+
protected native int get(
214+
long handle, long readOptHandle, byte[] key, int keyLen,
215+
byte[] value, int valueLen) throws RocksDBException;
179216
protected native byte[] get(
180217
long handle, byte[] key, int keyLen) throws RocksDBException;
218+
protected native byte[] get(
219+
long handle, long readOptHandle,
220+
byte[] key, int keyLen) throws RocksDBException;
181221
protected native void remove(
182222
long handle, byte[] key, int keyLen) throws RocksDBException;
183223
protected native void remove(
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2014, Facebook, Inc. All rights reserved.
2+
// This source code is licensed under the BSD-style license found in the
3+
// LICENSE file in the root directory of this source tree. An additional grant
4+
// of patent rights can be found in the PATENTS file in the same directory.
5+
6+
package org.rocksdb.test;
7+
8+
import java.util.Random;
9+
import org.rocksdb.*;
10+
11+
public class ReadOptionsTest {
12+
static {
13+
System.loadLibrary("rocksdbjni");
14+
}
15+
public static void main(String[] args) {
16+
ReadOptions opt = new ReadOptions();
17+
Random rand = new Random();
18+
{ // VerifyChecksums test
19+
boolean boolValue = rand.nextBoolean();
20+
opt.setVerifyChecksums(boolValue);
21+
assert(opt.verifyChecksums() == boolValue);
22+
}
23+
24+
{ // FillCache test
25+
boolean boolValue = rand.nextBoolean();
26+
opt.setFillCache(boolValue);
27+
assert(opt.fillCache() == boolValue);
28+
}
29+
30+
{ // PrefixSeek test
31+
boolean boolValue = rand.nextBoolean();
32+
opt.setPrefixSeek(boolValue);
33+
assert(opt.prefixSeek() == boolValue);
34+
}
35+
36+
{ // Tailing test
37+
boolean boolValue = rand.nextBoolean();
38+
opt.setTailing(boolValue);
39+
assert(opt.tailing() == boolValue);
40+
}
41+
42+
opt.dispose();
43+
System.out.println("Passed ReadOptionsTest");
44+
}
45+
}

0 commit comments

Comments
 (0)