Skip to content

Commit 66f62e5

Browse files
author
Lei Jin
committed
JNI changes corresponding to BlockBasedTableOptions migration
Summary: as title Test Plan: tested on my mac make rocksdbjava make jtest Reviewers: sdong, igor, yhchiang Reviewed By: yhchiang Subscribers: leveldb Differential Revision: https://reviews.facebook.net/D21963
1 parent 3844001 commit 66f62e5

10 files changed

+284
-472
lines changed

java/Makefile

+1-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.RocksIterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig org.rocksdb.ReadOptions org.rocksdb.Filter org.rocksdb.BloomFilter org.rocksdb.RestoreOptions org.rocksdb.RestoreBackupableDB org.rocksdb.RocksEnv
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.RocksIterator org.rocksdb.VectorMemTableConfig org.rocksdb.SkipListMemTableConfig org.rocksdb.HashLinkedListMemTableConfig org.rocksdb.HashSkipListMemTableConfig org.rocksdb.PlainTableConfig org.rocksdb.BlockBasedTableConfig org.rocksdb.ReadOptions org.rocksdb.Filter org.rocksdb.BloomFilter org.rocksdb.RestoreOptions org.rocksdb.RestoreBackupableDB org.rocksdb.RocksEnv
22

33
NATIVE_INCLUDE = ./include
44
ROCKSDB_JAR = rocksdbjni.jar

java/RocksDBSample.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,19 @@ public static void main(String[] args) {
3535
assert(db == null);
3636
}
3737

38-
Filter filter = new BloomFilter(10);
3938
options.setCreateIfMissing(true)
4039
.createStatistics()
4140
.setWriteBufferSize(8 * SizeUnit.KB)
4241
.setMaxWriteBufferNumber(3)
43-
.setDisableSeekCompaction(true)
44-
.setBlockSize(64 * SizeUnit.KB)
4542
.setMaxBackgroundCompactions(10)
46-
.setFilter(filter)
47-
.setCacheNumShardBits(6)
4843
.setCompressionType(CompressionType.SNAPPY_COMPRESSION)
4944
.setCompactionStyle(CompactionStyle.UNIVERSAL);
5045
Statistics stats = options.statisticsPtr();
5146

5247
assert(options.createIfMissing() == true);
5348
assert(options.writeBufferSize() == 8 * SizeUnit.KB);
5449
assert(options.maxWriteBufferNumber() == 3);
55-
assert(options.disableSeekCompaction() == true);
56-
assert(options.blockSize() == 64 * SizeUnit.KB);
5750
assert(options.maxBackgroundCompactions() == 10);
58-
assert(options.cacheNumShardBits() == 6);
5951
assert(options.compressionType() == CompressionType.SNAPPY_COMPRESSION);
6052
assert(options.compactionStyle() == CompactionStyle.UNIVERSAL);
6153

@@ -82,6 +74,15 @@ public static void main(String[] args) {
8274
options.setTableFormatConfig(new PlainTableConfig());
8375
assert(options.tableFactoryName().equals("PlainTable"));
8476

77+
BlockBasedTableConfig table_options = new BlockBasedTableConfig();
78+
table_options.setBlockCacheSize(64 * SizeUnit.KB)
79+
.setFilterBitsPerKey(10)
80+
.setCacheNumShardBits(6);
81+
assert(table_options.blockCacheSize() == 64 * SizeUnit.KB);
82+
assert(table_options.cacheNumShardBits() == 6);
83+
options.setTableFormatConfig(table_options);
84+
assert(options.tableFactoryName().equals("BlockBasedTable"));
85+
8586
try {
8687
db = RocksDB.open(options, db_path_not_found);
8788
db.put("hello".getBytes(), "world".getBytes());
@@ -254,6 +255,5 @@ public static void main(String[] args) {
254255
// be sure to dispose c++ pointers
255256
options.dispose();
256257
readOptions.dispose();
257-
filter.dispose();
258258
}
259259
}
+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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+
package org.rocksdb;
6+
7+
/**
8+
* The config for plain table sst format.
9+
*
10+
* BlockBasedTable is a RocksDB's default SST file format.
11+
*/
12+
public class BlockBasedTableConfig extends TableFormatConfig {
13+
14+
public BlockBasedTableConfig() {
15+
noBlockCache_ = false;
16+
blockCacheSize_ = 8 * 1024 * 1024;
17+
blockSize_ = 4 * 1024;
18+
blockSizeDeviation_ =10;
19+
blockRestartInterval_ =16;
20+
wholeKeyFiltering_ = true;
21+
bitsPerKey_ = 0;
22+
}
23+
24+
/**
25+
* Disable block cache. If this is set to true,
26+
* then no block cache should be used, and the block_cache should
27+
* point to a nullptr object.
28+
* Default: false
29+
*
30+
* @param noBlockCache if use block cache
31+
* @return the reference to the current config.
32+
*/
33+
public BlockBasedTableConfig setNoBlockCache(boolean noBlockCache) {
34+
noBlockCache_ = noBlockCache;
35+
return this;
36+
}
37+
38+
/**
39+
* @return if block cache is disabled
40+
*/
41+
public boolean noBlockCache() {
42+
return noBlockCache_;
43+
}
44+
45+
/**
46+
* Set the amount of cache in bytes that will be used by RocksDB.
47+
* If cacheSize is non-positive, then cache will not be used.
48+
* DEFAULT: 8M
49+
*
50+
* @param blockCacheSize block cache size in bytes
51+
* @return the reference to the current config.
52+
*/
53+
public BlockBasedTableConfig setBlockCacheSize(long blockCacheSize) {
54+
blockCacheSize_ = blockCacheSize;
55+
return this;
56+
}
57+
58+
/**
59+
* @return block cache size in bytes
60+
*/
61+
public long blockCacheSize() {
62+
return blockCacheSize_;
63+
}
64+
65+
/**
66+
* Controls the number of shards for the block cache.
67+
* This is applied only if cacheSize is set to non-negative.
68+
*
69+
* @param numShardBits the number of shard bits. The resulting
70+
* number of shards would be 2 ^ numShardBits. Any negative
71+
* number means use default settings."
72+
* @return the reference to the current option.
73+
*/
74+
public BlockBasedTableConfig setCacheNumShardBits(int numShardBits) {
75+
numShardBits_ = numShardBits;
76+
return this;
77+
}
78+
79+
/**
80+
* Returns the number of shard bits used in the block cache.
81+
* The resulting number of shards would be 2 ^ (returned value).
82+
* Any negative number means use default settings.
83+
*
84+
* @return the number of shard bits used in the block cache.
85+
*/
86+
public int cacheNumShardBits() {
87+
return numShardBits_;
88+
}
89+
90+
/**
91+
* Approximate size of user data packed per block. Note that the
92+
* block size specified here corresponds to uncompressed data. The
93+
* actual size of the unit read from disk may be smaller if
94+
* compression is enabled. This parameter can be changed dynamically.
95+
* Default: 4K
96+
*
97+
* @param blockSize block size in bytes
98+
* @return the reference to the current config.
99+
*/
100+
public BlockBasedTableConfig setBlockSize(long blockSize) {
101+
blockSize_ = blockSize;
102+
return this;
103+
}
104+
105+
/**
106+
* @return block size in bytes
107+
*/
108+
public long blockSize() {
109+
return blockSize_;
110+
}
111+
112+
/**
113+
* This is used to close a block before it reaches the configured
114+
* 'block_size'. If the percentage of free space in the current block is less
115+
* than this specified number and adding a new record to the block will
116+
* exceed the configured block size, then this block will be closed and the
117+
* new record will be written to the next block.
118+
* Default is 10.
119+
*
120+
* @param blockSizeDeviation the deviation to block size allowed
121+
* @return the reference to the current config.
122+
*/
123+
public BlockBasedTableConfig setBlockSizeDeviation(int blockSizeDeviation) {
124+
blockSizeDeviation_ = blockSizeDeviation;
125+
return this;
126+
}
127+
128+
/**
129+
* @return the hash table ratio.
130+
*/
131+
public int blockSizeDeviation() {
132+
return blockSizeDeviation_;
133+
}
134+
135+
/**
136+
* Set block restart interval
137+
*
138+
* @param restartInterval block restart interval.
139+
* @return the reference to the current config.
140+
*/
141+
public BlockBasedTableConfig setBlockRestartInterval(int restartInterval) {
142+
blockRestartInterval_ = restartInterval;
143+
return this;
144+
}
145+
146+
/**
147+
* @return block restart interval
148+
*/
149+
public int blockRestartInterval() {
150+
return blockRestartInterval_;
151+
}
152+
153+
/**
154+
* If true, place whole keys in the filter (not just prefixes).
155+
* This must generally be true for gets to be efficient.
156+
* Default: true
157+
*
158+
* @param wholeKeyFiltering if enable whole key filtering
159+
* @return the reference to the current config.
160+
*/
161+
public BlockBasedTableConfig setWholeKeyFiltering(boolean wholeKeyFiltering) {
162+
wholeKeyFiltering_ = wholeKeyFiltering;
163+
return this;
164+
}
165+
166+
/**
167+
* @return if whole key filtering is enabled
168+
*/
169+
public boolean wholeKeyFiltering() {
170+
return wholeKeyFiltering_;
171+
}
172+
173+
/**
174+
* Use the specified filter policy to reduce disk reads.
175+
*
176+
* Filter should not be disposed before options instances using this filter is
177+
* disposed. If dispose() function is not called, then filter object will be
178+
* GC'd automatically.
179+
*
180+
* Filter instance can be re-used in multiple options instances.
181+
*
182+
* @param Filter policy java instance.
183+
* @return the reference to the current config.
184+
*/
185+
public BlockBasedTableConfig setFilterBitsPerKey(int bitsPerKey) {
186+
bitsPerKey_ = bitsPerKey;
187+
return this;
188+
}
189+
190+
@Override protected long newTableFactoryHandle() {
191+
return newTableFactoryHandle(noBlockCache_, blockCacheSize_, numShardBits_,
192+
blockSize_, blockSizeDeviation_, blockRestartInterval_,
193+
wholeKeyFiltering_, bitsPerKey_);
194+
}
195+
196+
private native long newTableFactoryHandle(
197+
boolean noBlockCache, long blockCacheSize, int numShardbits,
198+
long blockSize, int blockSizeDeviation, int blockRestartInterval,
199+
boolean wholeKeyFiltering, int bitsPerKey);
200+
201+
private boolean noBlockCache_;
202+
private long blockCacheSize_;
203+
private int numShardBits_;
204+
private long shard;
205+
private long blockSize_;
206+
private int blockSizeDeviation_;
207+
private int blockRestartInterval_;
208+
private boolean wholeKeyFiltering_;
209+
private int bitsPerKey_;
210+
}

0 commit comments

Comments
 (0)