Skip to content

Commit fb4a492

Browse files
committed
Merge pull request XRPLF#311 from ankgup87/master
[Java] Add remaining options to BlockBasedTableConfig and add getProperty() API
2 parents cdaf44f + 611e286 commit fb4a492

File tree

5 files changed

+208
-19
lines changed

5 files changed

+208
-19
lines changed

java/RocksDBSample.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,23 @@ public static void main(String[] args) {
8383
BlockBasedTableConfig table_options = new BlockBasedTableConfig();
8484
table_options.setBlockCacheSize(64 * SizeUnit.KB)
8585
.setFilterBitsPerKey(10)
86-
.setCacheNumShardBits(6);
86+
.setCacheNumShardBits(6)
87+
.setBlockSizeDeviation(5)
88+
.setBlockRestartInterval(10)
89+
.setCacheIndexAndFilterBlocks(true)
90+
.setHashIndexAllowCollision(false)
91+
.setBlockCacheCompressedSize(64 * SizeUnit.KB)
92+
.setBlockCacheCompressedNumShardBits(10);
93+
8794
assert(table_options.blockCacheSize() == 64 * SizeUnit.KB);
8895
assert(table_options.cacheNumShardBits() == 6);
96+
assert(table_options.blockSizeDeviation() == 5);
97+
assert(table_options.blockRestartInterval() == 10);
98+
assert(table_options.cacheIndexAndFilterBlocks() == true);
99+
assert(table_options.hashIndexAllowCollision() == false);
100+
assert(table_options.blockCacheCompressedSize() == 64 * SizeUnit.KB);
101+
assert(table_options.blockCacheCompressedNumShardBits() == 10);
102+
89103
options.setTableFormatConfig(table_options);
90104
assert(options.tableFactoryName().equals("BlockBasedTable"));
91105

@@ -94,6 +108,8 @@ public static void main(String[] args) {
94108
db.put("hello".getBytes(), "world".getBytes());
95109
byte[] value = db.get("hello".getBytes());
96110
assert("world".equals(new String(value)));
111+
String str = db.getProperty("rocksdb.stats");
112+
assert(str != null && str != "");
97113
} catch (RocksDBException e) {
98114
System.out.format("[ERROR] caught the unexpceted exception -- %s\n", e);
99115
assert(db == null);

java/org/rocksdb/BlockBasedTableConfig.java

+126-13
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ public class BlockBasedTableConfig extends TableFormatConfig {
1414
public BlockBasedTableConfig() {
1515
noBlockCache_ = false;
1616
blockCacheSize_ = 8 * 1024 * 1024;
17-
blockSize_ = 4 * 1024;
18-
blockSizeDeviation_ =10;
19-
blockRestartInterval_ =16;
17+
blockSize_ = 4 * 1024;
18+
blockSizeDeviation_ = 10;
19+
blockRestartInterval_ = 16;
2020
wholeKeyFiltering_ = true;
21-
bitsPerKey_ = 0;
21+
bitsPerKey_ = 10;
22+
cacheIndexAndFilterBlocks_ = false;
23+
hashIndexAllowCollision_ = true;
24+
blockCacheCompressedSize_ = 0;
2225
}
2326

2427
/**
@@ -71,8 +74,8 @@ public long blockCacheSize() {
7174
* number means use default settings."
7275
* @return the reference to the current option.
7376
*/
74-
public BlockBasedTableConfig setCacheNumShardBits(int numShardBits) {
75-
numShardBits_ = numShardBits;
77+
public BlockBasedTableConfig setCacheNumShardBits(int blockCacheNumShardBits) {
78+
blockCacheNumShardBits_ = blockCacheNumShardBits;
7679
return this;
7780
}
7881

@@ -84,7 +87,7 @@ public BlockBasedTableConfig setCacheNumShardBits(int numShardBits) {
8487
* @return the number of shard bits used in the block cache.
8588
*/
8689
public int cacheNumShardBits() {
87-
return numShardBits_;
90+
return blockCacheNumShardBits_;
8891
}
8992

9093
/**
@@ -186,25 +189,135 @@ public BlockBasedTableConfig setFilterBitsPerKey(int bitsPerKey) {
186189
bitsPerKey_ = bitsPerKey;
187190
return this;
188191
}
192+
193+
/**
194+
* Indicating if we'd put index/filter blocks to the block cache.
195+
If not specified, each "table reader" object will pre-load index/filter
196+
block during table initialization.
197+
*
198+
* @return if index and filter blocks should be put in block cache.
199+
*/
200+
public boolean cacheIndexAndFilterBlocks() {
201+
return cacheIndexAndFilterBlocks_;
202+
}
203+
204+
/**
205+
* Indicating if we'd put index/filter blocks to the block cache.
206+
If not specified, each "table reader" object will pre-load index/filter
207+
block during table initialization.
208+
*
209+
* @param index and filter blocks should be put in block cache.
210+
* @return the reference to the current config.
211+
*/
212+
public BlockBasedTableConfig setCacheIndexAndFilterBlocks(
213+
boolean cacheIndexAndFilterBlocks) {
214+
cacheIndexAndFilterBlocks_ = cacheIndexAndFilterBlocks;
215+
return this;
216+
}
217+
218+
/**
219+
* Influence the behavior when kHashSearch is used.
220+
if false, stores a precise prefix to block range mapping
221+
if true, does not store prefix and allows prefix hash collision
222+
(less memory consumption)
223+
*
224+
* @return if hash collisions should be allowed.
225+
*/
226+
public boolean hashIndexAllowCollision() {
227+
return hashIndexAllowCollision_;
228+
}
229+
230+
/**
231+
* Influence the behavior when kHashSearch is used.
232+
if false, stores a precise prefix to block range mapping
233+
if true, does not store prefix and allows prefix hash collision
234+
(less memory consumption)
235+
*
236+
* @param if hash collisions should be allowed.
237+
* @return the reference to the current config.
238+
*/
239+
public BlockBasedTableConfig setHashIndexAllowCollision(
240+
boolean hashIndexAllowCollision) {
241+
hashIndexAllowCollision_ = hashIndexAllowCollision;
242+
return this;
243+
}
244+
245+
/**
246+
* Size of compressed block cache. If 0, then block_cache_compressed is set
247+
* to null.
248+
*
249+
* @return size of compressed block cache.
250+
*/
251+
public long blockCacheCompressedSize() {
252+
return blockCacheCompressedSize_;
253+
}
254+
255+
/**
256+
* Size of compressed block cache. If 0, then block_cache_compressed is set
257+
* to null.
258+
*
259+
* @param size of compressed block cache.
260+
* @return the reference to the current config.
261+
*/
262+
public BlockBasedTableConfig setBlockCacheCompressedSize(
263+
long blockCacheCompressedSize) {
264+
blockCacheCompressedSize_ = blockCacheCompressedSize;
265+
return this;
266+
}
267+
268+
/**
269+
* Controls the number of shards for the block compressed cache.
270+
* This is applied only if blockCompressedCacheSize is set to non-negative.
271+
*
272+
* @return numShardBits the number of shard bits. The resulting
273+
* number of shards would be 2 ^ numShardBits. Any negative
274+
* number means use default settings.
275+
*/
276+
public int blockCacheCompressedNumShardBits() {
277+
return blockCacheCompressedNumShardBits_;
278+
}
279+
280+
/**
281+
* Controls the number of shards for the block compressed cache.
282+
* This is applied only if blockCompressedCacheSize is set to non-negative.
283+
*
284+
* @param numShardBits the number of shard bits. The resulting
285+
* number of shards would be 2 ^ numShardBits. Any negative
286+
* number means use default settings."
287+
* @return the reference to the current option.
288+
*/
289+
public BlockBasedTableConfig setBlockCacheCompressedNumShardBits(
290+
int blockCacheCompressedNumShardBits) {
291+
blockCacheCompressedNumShardBits_ = blockCacheCompressedNumShardBits;
292+
return this;
293+
}
189294

190295
@Override protected long newTableFactoryHandle() {
191-
return newTableFactoryHandle(noBlockCache_, blockCacheSize_, numShardBits_,
192-
blockSize_, blockSizeDeviation_, blockRestartInterval_,
193-
wholeKeyFiltering_, bitsPerKey_);
296+
return newTableFactoryHandle(noBlockCache_, blockCacheSize_,
297+
blockCacheNumShardBits_, blockSize_, blockSizeDeviation_,
298+
blockRestartInterval_, wholeKeyFiltering_, bitsPerKey_,
299+
cacheIndexAndFilterBlocks_, hashIndexAllowCollision_,
300+
blockCacheCompressedSize_, blockCacheCompressedNumShardBits_);
194301
}
195302

196303
private native long newTableFactoryHandle(
197-
boolean noBlockCache, long blockCacheSize, int numShardbits,
304+
boolean noBlockCache, long blockCacheSize, int blockCacheNumShardBits,
198305
long blockSize, int blockSizeDeviation, int blockRestartInterval,
199-
boolean wholeKeyFiltering, int bitsPerKey);
306+
boolean wholeKeyFiltering, int bitsPerKey,
307+
boolean cacheIndexAndFilterBlocks, boolean hashIndexAllowCollision,
308+
long blockCacheCompressedSize, int blockCacheCompressedNumShardBits);
200309

201310
private boolean noBlockCache_;
202311
private long blockCacheSize_;
203-
private int numShardBits_;
312+
private int blockCacheNumShardBits_;
204313
private long shard;
205314
private long blockSize_;
206315
private int blockSizeDeviation_;
207316
private int blockRestartInterval_;
208317
private boolean wholeKeyFiltering_;
209318
private int bitsPerKey_;
319+
private boolean cacheIndexAndFilterBlocks_;
320+
private boolean hashIndexAllowCollision_;
321+
private long blockCacheCompressedSize_;
322+
private int blockCacheCompressedNumShardBits_;
210323
}

java/org/rocksdb/RocksDB.java

+22
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,26 @@ public void remove(WriteOptions writeOpt, byte[] key)
324324
throws RocksDBException {
325325
remove(nativeHandle_, writeOpt.nativeHandle_, key, key.length);
326326
}
327+
328+
/**
329+
* DB implementations can export properties about their state
330+
via this method. If "property" is a valid property understood by this
331+
DB implementation, fills "*value" with its current value and returns
332+
true. Otherwise returns false.
333+
334+
335+
Valid property names include:
336+
337+
"rocksdb.num-files-at-level<N>" - return the number of files at level <N>,
338+
where <N> is an ASCII representation of a level number (e.g. "0").
339+
"rocksdb.stats" - returns a multi-line string that describes statistics
340+
about the internal operation of the DB.
341+
"rocksdb.sstables" - returns a multi-line string that describes all
342+
of the sstables that make up the db contents.
343+
*/
344+
public String getProperty(String property) throws RocksDBException {
345+
return getProperty0(nativeHandle_, property, property.length());
346+
}
327347

328348
/**
329349
* Return a heap-allocated iterator over the contents of the database.
@@ -378,6 +398,8 @@ protected native void remove(
378398
protected native void remove(
379399
long handle, long writeOptHandle,
380400
byte[] key, int keyLen) throws RocksDBException;
401+
protected native String getProperty0(long nativeHandle,
402+
String property, int propertyLength) throws RocksDBException;
381403
protected native long iterator0(long optHandle);
382404
private native void disposeInternal(long handle);
383405

java/rocksjni/rocksjni.cc

+24
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,27 @@ jlong Java_org_rocksdb_RocksDB_iterator0(
425425
rocksdb::Iterator* iterator = db->NewIterator(rocksdb::ReadOptions());
426426
return reinterpret_cast<jlong>(iterator);
427427
}
428+
429+
/*
430+
* Class: org_rocksdb_RocksDB
431+
* Method: getProperty0
432+
* Signature: (JLjava/lang/String;I)Ljava/lang/String;
433+
*/
434+
jstring Java_org_rocksdb_RocksDB_getProperty0(
435+
JNIEnv* env, jobject jdb, jlong db_handle, jstring jproperty,
436+
jint jproperty_len) {
437+
auto db = reinterpret_cast<rocksdb::DB*>(db_handle);
438+
439+
const char* property = env->GetStringUTFChars(jproperty, 0);
440+
rocksdb::Slice property_slice(property, jproperty_len);
441+
442+
std::string property_value;
443+
bool retCode = db->GetProperty(property_slice, &property_value);
444+
env->ReleaseStringUTFChars(jproperty, property);
445+
446+
if (!retCode) {
447+
rocksdb::RocksDBExceptionJni::ThrowNew(env, rocksdb::Status::NotFound());
448+
}
449+
450+
return env->NewStringUTF(property_value.data());
451+
}

java/rocksjni/table.cc

+19-5
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,22 @@ jlong Java_org_rocksdb_PlainTableConfig_newTableFactoryHandle(
3131
/*
3232
* Class: org_rocksdb_BlockBasedTableConfig
3333
* Method: newTableFactoryHandle
34-
* Signature: (ZJIJIIZI)J
34+
* Signature: (ZJIJIIZIZZJI)J
3535
*/
3636
jlong Java_org_rocksdb_BlockBasedTableConfig_newTableFactoryHandle(
3737
JNIEnv* env, jobject jobj, jboolean no_block_cache, jlong block_cache_size,
38-
jint num_shardbits, jlong block_size, jint block_size_deviation,
38+
jint block_cache_num_shardbits, jlong block_size, jint block_size_deviation,
3939
jint block_restart_interval, jboolean whole_key_filtering,
40-
jint bits_per_key) {
40+
jint bits_per_key, jboolean cache_index_and_filter_blocks,
41+
jboolean hash_index_allow_collision, jlong block_cache_compressed_size,
42+
jint block_cache_compressd_num_shard_bits) {
4143
rocksdb::BlockBasedTableOptions options;
4244
options.no_block_cache = no_block_cache;
4345

4446
if (!no_block_cache && block_cache_size > 0) {
45-
if (num_shardbits > 0) {
47+
if (block_cache_num_shardbits > 0) {
4648
options.block_cache =
47-
rocksdb::NewLRUCache(block_cache_size, num_shardbits);
49+
rocksdb::NewLRUCache(block_cache_size, block_cache_num_shardbits);
4850
} else {
4951
options.block_cache = rocksdb::NewLRUCache(block_cache_size);
5052
}
@@ -56,5 +58,17 @@ jlong Java_org_rocksdb_BlockBasedTableConfig_newTableFactoryHandle(
5658
if (bits_per_key > 0) {
5759
options.filter_policy.reset(rocksdb::NewBloomFilterPolicy(bits_per_key));
5860
}
61+
options.cache_index_and_filter_blocks = cache_index_and_filter_blocks;
62+
options.hash_index_allow_collision = hash_index_allow_collision;
63+
if (block_cache_compressed_size > 0) {
64+
if (block_cache_compressd_num_shard_bits > 0) {
65+
options.block_cache =
66+
rocksdb::NewLRUCache(block_cache_compressed_size,
67+
block_cache_compressd_num_shard_bits);
68+
} else {
69+
options.block_cache = rocksdb::NewLRUCache(block_cache_compressed_size);
70+
}
71+
}
72+
5973
return reinterpret_cast<jlong>(rocksdb::NewBlockBasedTableFactory(options));
6074
}

0 commit comments

Comments
 (0)