Merge pull request #336 from fyrz/32BitRocksJavaBug

32 Bit RocksJava Fix for overflowing jlongs
This commit is contained in:
Yueh-Hsuan Chiang 2014-10-12 19:48:54 -07:00
commit 5908d08a0b
16 changed files with 317 additions and 134 deletions

View File

@ -35,13 +35,18 @@ public class RocksDBSample {
assert(db == null); assert(db == null);
} }
options.setCreateIfMissing(true) try {
.createStatistics() options.setCreateIfMissing(true)
.setWriteBufferSize(8 * SizeUnit.KB) .createStatistics()
.setMaxWriteBufferNumber(3) .setWriteBufferSize(8 * SizeUnit.KB)
.setMaxBackgroundCompactions(10) .setMaxWriteBufferNumber(3)
.setCompressionType(CompressionType.SNAPPY_COMPRESSION) .setMaxBackgroundCompactions(10)
.setCompactionStyle(CompactionStyle.UNIVERSAL); .setCompressionType(CompressionType.SNAPPY_COMPRESSION)
.setCompactionStyle(CompactionStyle.UNIVERSAL);
} catch (RocksDBException e) {
assert(false);
}
Statistics stats = options.statisticsPtr(); Statistics stats = options.statisticsPtr();
assert(options.createIfMissing() == true); assert(options.createIfMissing() == true);
@ -50,36 +55,38 @@ public class RocksDBSample {
assert(options.maxBackgroundCompactions() == 10); assert(options.maxBackgroundCompactions() == 10);
assert(options.compressionType() == CompressionType.SNAPPY_COMPRESSION); assert(options.compressionType() == CompressionType.SNAPPY_COMPRESSION);
assert(options.compactionStyle() == CompactionStyle.UNIVERSAL); assert(options.compactionStyle() == CompactionStyle.UNIVERSAL);
try {
assert(options.memTableFactoryName().equals("SkipListFactory"));
options.setMemTableConfig(
new HashSkipListMemTableConfig()
.setHeight(4)
.setBranchingFactor(4)
.setBucketCount(2000000));
assert(options.memTableFactoryName().equals("HashSkipListRepFactory"));
assert(options.memTableFactoryName().equals("SkipListFactory")); options.setMemTableConfig(
options.setMemTableConfig( new HashLinkedListMemTableConfig()
new HashSkipListMemTableConfig() .setBucketCount(100000));
.setHeight(4) assert(options.memTableFactoryName().equals("HashLinkedListRepFactory"));
.setBranchingFactor(4)
.setBucketCount(2000000));
assert(options.memTableFactoryName().equals("HashSkipListRepFactory"));
options.setMemTableConfig( options.setMemTableConfig(
new HashLinkedListMemTableConfig() new VectorMemTableConfig().setReservedSize(10000));
.setBucketCount(100000)); assert(options.memTableFactoryName().equals("VectorRepFactory"));
assert(options.memTableFactoryName().equals("HashLinkedListRepFactory"));
options.setMemTableConfig( options.setMemTableConfig(new SkipListMemTableConfig());
new VectorMemTableConfig().setReservedSize(10000)); assert(options.memTableFactoryName().equals("SkipListFactory"));
assert(options.memTableFactoryName().equals("VectorRepFactory"));
options.setMemTableConfig(new SkipListMemTableConfig()); options.setTableFormatConfig(new PlainTableConfig());
assert(options.memTableFactoryName().equals("SkipListFactory")); // Plain-Table requires mmap read
options.setAllowMmapReads(true);
options.setTableFormatConfig(new PlainTableConfig()); assert(options.tableFactoryName().equals("PlainTable"));
// Plain-Table requires mmap read
options.setAllowMmapReads(true);
assert(options.tableFactoryName().equals("PlainTable"));
options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000,
10000, 10));
options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000));
options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000,
10000, 10));
options.setRateLimiterConfig(new GenericRateLimiterConfig(10000000));
} catch (RocksDBException e) {
assert(false);
}
Filter bloomFilter = new BloomFilter(10); Filter bloomFilter = new BloomFilter(10);
BlockBasedTableConfig table_options = new BlockBasedTableConfig(); BlockBasedTableConfig table_options = new BlockBasedTableConfig();
table_options.setBlockCacheSize(64 * SizeUnit.KB) table_options.setBlockCacheSize(64 * SizeUnit.KB)

View File

@ -42,11 +42,13 @@ public class HashLinkedListMemTableConfig extends MemTableConfig {
return bucketCount_; return bucketCount_;
} }
@Override protected long newMemTableFactoryHandle() { @Override protected long newMemTableFactoryHandle()
throws RocksDBException {
return newMemTableFactoryHandle(bucketCount_); return newMemTableFactoryHandle(bucketCount_);
} }
private native long newMemTableFactoryHandle(long bucketCount); private native long newMemTableFactoryHandle(long bucketCount)
throws RocksDBException;
private long bucketCount_; private long bucketCount_;
} }

View File

@ -83,13 +83,15 @@ public class HashSkipListMemTableConfig extends MemTableConfig {
return branchingFactor_; return branchingFactor_;
} }
@Override protected long newMemTableFactoryHandle() { @Override protected long newMemTableFactoryHandle()
throws RocksDBException {
return newMemTableFactoryHandle( return newMemTableFactoryHandle(
bucketCount_, height_, branchingFactor_); bucketCount_, height_, branchingFactor_);
} }
private native long newMemTableFactoryHandle( private native long newMemTableFactoryHandle(
long bucketCount, int height, int branchingFactor); long bucketCount, int height, int branchingFactor)
throws RocksDBException;
private long bucketCount_; private long bucketCount_;
private int branchingFactor_; private int branchingFactor_;

View File

@ -23,5 +23,6 @@ public abstract class MemTableConfig {
* *
* @see Options#setMemTableConfig(MemTableConfig) * @see Options#setMemTableConfig(MemTableConfig)
*/ */
abstract protected long newMemTableFactoryHandle(); abstract protected long newMemTableFactoryHandle()
throws RocksDBException;
} }

View File

@ -118,8 +118,10 @@ public class Options extends RocksObject {
* @param writeBufferSize the size of write buffer. * @param writeBufferSize the size of write buffer.
* @return the instance of the current Options. * @return the instance of the current Options.
* @see org.rocksdb.RocksDB#open(Options, String) * @see org.rocksdb.RocksDB#open(Options, String)
* @throws RocksDBException
*/ */
public Options setWriteBufferSize(long writeBufferSize) { public Options setWriteBufferSize(long writeBufferSize)
throws RocksDBException {
assert(isInitialized()); assert(isInitialized());
setWriteBufferSize(nativeHandle_, writeBufferSize); setWriteBufferSize(nativeHandle_, writeBufferSize);
return this; return this;
@ -561,13 +563,16 @@ public class Options extends RocksObject {
* *
* @param maxLogFileSize the maximum size of a info log file. * @param maxLogFileSize the maximum size of a info log file.
* @return the reference to the current option. * @return the reference to the current option.
* @throws RocksDBException
*/ */
public Options setMaxLogFileSize(long maxLogFileSize) { public Options setMaxLogFileSize(long maxLogFileSize)
throws RocksDBException {
assert(isInitialized()); assert(isInitialized());
setMaxLogFileSize(nativeHandle_, maxLogFileSize); setMaxLogFileSize(nativeHandle_, maxLogFileSize);
return this; return this;
} }
private native void setMaxLogFileSize(long handle, long maxLogFileSize); private native void setMaxLogFileSize(long handle, long maxLogFileSize)
throws RocksDBException;
/** /**
* Returns the time interval for the info log file to roll (in seconds). * Returns the time interval for the info log file to roll (in seconds).
@ -591,14 +596,16 @@ public class Options extends RocksObject {
* *
* @param logFileTimeToRoll the time interval in seconds. * @param logFileTimeToRoll the time interval in seconds.
* @return the reference to the current option. * @return the reference to the current option.
* @throws RocksDBException
*/ */
public Options setLogFileTimeToRoll(long logFileTimeToRoll) { public Options setLogFileTimeToRoll(long logFileTimeToRoll)
throws RocksDBException{
assert(isInitialized()); assert(isInitialized());
setLogFileTimeToRoll(nativeHandle_, logFileTimeToRoll); setLogFileTimeToRoll(nativeHandle_, logFileTimeToRoll);
return this; return this;
} }
private native void setLogFileTimeToRoll( private native void setLogFileTimeToRoll(
long handle, long logFileTimeToRoll); long handle, long logFileTimeToRoll) throws RocksDBException;
/** /**
* Returns the maximum number of info log files to be kept. * Returns the maximum number of info log files to be kept.
@ -618,13 +625,16 @@ public class Options extends RocksObject {
* *
* @param keepLogFileNum the maximum number of info log files to be kept. * @param keepLogFileNum the maximum number of info log files to be kept.
* @return the reference to the current option. * @return the reference to the current option.
* @throws RocksDBException
*/ */
public Options setKeepLogFileNum(long keepLogFileNum) { public Options setKeepLogFileNum(long keepLogFileNum)
throws RocksDBException{
assert(isInitialized()); assert(isInitialized());
setKeepLogFileNum(nativeHandle_, keepLogFileNum); setKeepLogFileNum(nativeHandle_, keepLogFileNum);
return this; return this;
} }
private native void setKeepLogFileNum(long handle, long keepLogFileNum); private native void setKeepLogFileNum(long handle, long keepLogFileNum)
throws RocksDBException;
/** /**
* Manifest file is rolled over on reaching this limit. * Manifest file is rolled over on reaching this limit.
@ -844,14 +854,16 @@ public class Options extends RocksObject {
* *
* @param size the size in byte * @param size the size in byte
* @return the reference to the current option. * @return the reference to the current option.
* @throws RocksDBException
*/ */
public Options setManifestPreallocationSize(long size) { public Options setManifestPreallocationSize(long size)
throws RocksDBException {
assert(isInitialized()); assert(isInitialized());
setManifestPreallocationSize(nativeHandle_, size); setManifestPreallocationSize(nativeHandle_, size);
return this; return this;
} }
private native void setManifestPreallocationSize( private native void setManifestPreallocationSize(
long handle, long size); long handle, long size) throws RocksDBException;
/** /**
* Data being read from file storage may be buffered in the OS * Data being read from file storage may be buffered in the OS
@ -1110,8 +1122,10 @@ public class Options extends RocksObject {
* *
* @param config the mem-table config. * @param config the mem-table config.
* @return the instance of the current Options. * @return the instance of the current Options.
* @throws RocksDBException
*/ */
public Options setMemTableConfig(MemTableConfig config) { public Options setMemTableConfig(MemTableConfig config)
throws RocksDBException {
setMemTableFactory(nativeHandle_, config.newMemTableFactoryHandle()); setMemTableFactory(nativeHandle_, config.newMemTableFactoryHandle());
return this; return this;
} }
@ -1123,6 +1137,7 @@ public class Options extends RocksObject {
* *
* @param config rate limiter config. * @param config rate limiter config.
* @return the instance of the current Options. * @return the instance of the current Options.
* @throws RocksDBException
*/ */
public Options setRateLimiterConfig(RateLimiterConfig config) { public Options setRateLimiterConfig(RateLimiterConfig config) {
setRateLimiter(nativeHandle_, config.newRateLimiterHandle()); setRateLimiter(nativeHandle_, config.newRateLimiterHandle());
@ -1768,13 +1783,15 @@ public class Options extends RocksObject {
* *
* @param arenaBlockSize the size of an arena block * @param arenaBlockSize the size of an arena block
* @return the reference to the current option. * @return the reference to the current option.
* @throws RocksDBException
*/ */
public Options setArenaBlockSize(long arenaBlockSize) { public Options setArenaBlockSize(long arenaBlockSize)
throws RocksDBException {
setArenaBlockSize(nativeHandle_, arenaBlockSize); setArenaBlockSize(nativeHandle_, arenaBlockSize);
return this; return this;
} }
private native void setArenaBlockSize( private native void setArenaBlockSize(
long handle, long arenaBlockSize); long handle, long arenaBlockSize) throws RocksDBException;
/** /**
* Disable automatic compactions. Manual compactions can still * Disable automatic compactions. Manual compactions can still
@ -1977,13 +1994,15 @@ public class Options extends RocksObject {
* @param inplaceUpdateNumLocks the number of locks used for * @param inplaceUpdateNumLocks the number of locks used for
* inplace updates. * inplace updates.
* @return the reference to the current option. * @return the reference to the current option.
* @throws RocksDBException
*/ */
public Options setInplaceUpdateNumLocks(long inplaceUpdateNumLocks) { public Options setInplaceUpdateNumLocks(long inplaceUpdateNumLocks)
throws RocksDBException {
setInplaceUpdateNumLocks(nativeHandle_, inplaceUpdateNumLocks); setInplaceUpdateNumLocks(nativeHandle_, inplaceUpdateNumLocks);
return this; return this;
} }
private native void setInplaceUpdateNumLocks( private native void setInplaceUpdateNumLocks(
long handle, long inplaceUpdateNumLocks); long handle, long inplaceUpdateNumLocks) throws RocksDBException;
/** /**
* Returns the number of bits used in the prefix bloom filter. * Returns the number of bits used in the prefix bloom filter.
@ -2108,13 +2127,15 @@ public class Options extends RocksObject {
* *
* @param maxSuccessiveMerges the maximum number of successive merges. * @param maxSuccessiveMerges the maximum number of successive merges.
* @return the reference to the current option. * @return the reference to the current option.
* @throws RocksDBException
*/ */
public Options setMaxSuccessiveMerges(long maxSuccessiveMerges) { public Options setMaxSuccessiveMerges(long maxSuccessiveMerges)
throws RocksDBException {
setMaxSuccessiveMerges(nativeHandle_, maxSuccessiveMerges); setMaxSuccessiveMerges(nativeHandle_, maxSuccessiveMerges);
return this; return this;
} }
private native void setMaxSuccessiveMerges( private native void setMaxSuccessiveMerges(
long handle, long maxSuccessiveMerges); long handle, long maxSuccessiveMerges) throws RocksDBException;
/** /**
* The minimum number of write buffers that will be merged together * The minimum number of write buffers that will be merged together
@ -2204,7 +2225,8 @@ public class Options extends RocksObject {
private native void disposeInternal(long handle); private native void disposeInternal(long handle);
private native void setCreateIfMissing(long handle, boolean flag); private native void setCreateIfMissing(long handle, boolean flag);
private native boolean createIfMissing(long handle); private native boolean createIfMissing(long handle);
private native void setWriteBufferSize(long handle, long writeBufferSize); private native void setWriteBufferSize(long handle, long writeBufferSize)
throws RocksDBException;
private native long writeBufferSize(long handle); private native long writeBufferSize(long handle);
private native void setMaxWriteBufferNumber( private native void setMaxWriteBufferNumber(
long handle, int maxWriteBufferNumber); long handle, int maxWriteBufferNumber);

View File

@ -489,7 +489,7 @@ public class DbBenchmark {
options.setDisableWAL((Boolean)flags_.get(Flag.disable_wal)); options.setDisableWAL((Boolean)flags_.get(Flag.disable_wal));
} }
private void prepareOptions(Options options) { private void prepareOptions(Options options) throws RocksDBException {
if (!useExisting_) { if (!useExisting_) {
options.setCreateIfMissing(true); options.setCreateIfMissing(true);
} else { } else {

View File

@ -7,15 +7,19 @@ package org.rocksdb.test;
import java.util.Random; import java.util.Random;
import org.rocksdb.RocksDB; import org.rocksdb.RocksDB;
import org.rocksdb.RocksDBException;
import org.rocksdb.Options; import org.rocksdb.Options;
import org.rocksdb.test.PlatformRandomHelper;
public class OptionsTest { public class OptionsTest {
static { static {
RocksDB.loadLibrary(); RocksDB.loadLibrary();
} }
public static void main(String[] args) { public static void main(String[] args) {
Options opt = new Options(); Options opt = new Options();
Random rand = new Random(); Random rand = PlatformRandomHelper.
getPlatformSpecificRandomFactory();
{ // CreateIfMissing test { // CreateIfMissing test
boolean boolValue = rand.nextBoolean(); boolean boolValue = rand.nextBoolean();
opt.setCreateIfMissing(boolValue); opt.setCreateIfMissing(boolValue);
@ -83,21 +87,34 @@ public class OptionsTest {
} }
{ // MaxLogFileSize test { // MaxLogFileSize test
long longValue = rand.nextLong(); try {
opt.setMaxLogFileSize(longValue); long longValue = rand.nextLong();
assert(opt.maxLogFileSize() == longValue); opt.setMaxLogFileSize(longValue);
assert(opt.maxLogFileSize() == longValue);
} catch (RocksDBException e) {
System.out.println(e.getMessage());
assert(false);
}
} }
{ // LogFileTimeToRoll test { // LogFileTimeToRoll test
long longValue = rand.nextLong(); try {
opt.setLogFileTimeToRoll(longValue); long longValue = rand.nextLong();
assert(opt.logFileTimeToRoll() == longValue); opt.setLogFileTimeToRoll(longValue);
assert(opt.logFileTimeToRoll() == longValue);
} catch (RocksDBException e) {
assert(false);
}
} }
{ // KeepLogFileNum test { // KeepLogFileNum test
long longValue = rand.nextLong(); try {
opt.setKeepLogFileNum(longValue); long longValue = rand.nextLong();
assert(opt.keepLogFileNum() == longValue); opt.setKeepLogFileNum(longValue);
assert(opt.keepLogFileNum() == longValue);
} catch (RocksDBException e) {
assert(false);
}
} }
{ // MaxManifestFileSize test { // MaxManifestFileSize test
@ -125,9 +142,13 @@ public class OptionsTest {
} }
{ // ManifestPreallocationSize test { // ManifestPreallocationSize test
long longValue = rand.nextLong(); try {
opt.setManifestPreallocationSize(longValue); long longValue = rand.nextLong();
assert(opt.manifestPreallocationSize() == longValue); opt.setManifestPreallocationSize(longValue);
assert(opt.manifestPreallocationSize() == longValue);
} catch (RocksDBException e) {
assert(false);
}
} }
{ // AllowOsBuffer test { // AllowOsBuffer test
@ -185,9 +206,13 @@ public class OptionsTest {
} }
{ // WriteBufferSize test { // WriteBufferSize test
long longValue = rand.nextLong(); try {
opt.setWriteBufferSize(longValue); long longValue = rand.nextLong();
assert(opt.writeBufferSize() == longValue); opt.setWriteBufferSize(longValue);
assert(opt.writeBufferSize() == longValue);
} catch (RocksDBException e) {
assert(false);
}
} }
{ // MaxWriteBufferNumber test { // MaxWriteBufferNumber test
@ -293,9 +318,13 @@ public class OptionsTest {
} }
{ // ArenaBlockSize test { // ArenaBlockSize test
long longValue = rand.nextLong(); try {
opt.setArenaBlockSize(longValue); long longValue = rand.nextLong();
assert(opt.arenaBlockSize() == longValue); opt.setArenaBlockSize(longValue);
assert(opt.arenaBlockSize() == longValue);
} catch (RocksDBException e) {
assert(false);
}
} }
{ // DisableAutoCompactions test { // DisableAutoCompactions test
@ -335,9 +364,13 @@ public class OptionsTest {
} }
{ // InplaceUpdateNumLocks test { // InplaceUpdateNumLocks test
long longValue = rand.nextLong(); try {
opt.setInplaceUpdateNumLocks(longValue); long longValue = rand.nextLong();
assert(opt.inplaceUpdateNumLocks() == longValue); opt.setInplaceUpdateNumLocks(longValue);
assert(opt.inplaceUpdateNumLocks() == longValue);
} catch (RocksDBException e) {
assert(false);
}
} }
{ // MemtablePrefixBloomBits test { // MemtablePrefixBloomBits test
@ -359,9 +392,13 @@ public class OptionsTest {
} }
{ // MaxSuccessiveMerges test { // MaxSuccessiveMerges test
long longValue = rand.nextLong(); try {
opt.setMaxSuccessiveMerges(longValue); long longValue = rand.nextLong();
assert(opt.maxSuccessiveMerges() == longValue); opt.setMaxSuccessiveMerges(longValue);
assert(opt.maxSuccessiveMerges() == longValue);
} catch (RocksDBException e){
assert(false);
}
} }
{ // MinPartialMergeOperands test { // MinPartialMergeOperands test

View File

@ -0,0 +1,54 @@
// 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.test;
import java.util.Random;
/**
* Helper class to get the appropriate Random class instance dependent
* on the current platform architecture (32bit vs 64bit)
*/
public class PlatformRandomHelper {
/**
* Determine if OS is 32-Bit/64-Bit
*/
public static boolean isOs64Bit(){
boolean is64Bit = false;
if (System.getProperty("os.name").contains("Windows")) {
is64Bit = (System.getenv("ProgramFiles(x86)") != null);
} else {
is64Bit = (System.getProperty("os.arch").indexOf("64") != -1);
}
return is64Bit;
}
/**
* Factory to get a platform specific Random instance
*/
public static Random getPlatformSpecificRandomFactory(){
if (isOs64Bit()) {
return new Random();
}
return new Random32Bit();
}
/**
* Random32Bit is a class which overrides {@code nextLong} to
* provide random numbers which fit in size_t. This workaround
* is necessary because there is no unsigned_int < Java 8
*/
private static class Random32Bit extends Random {
@Override
public long nextLong(){
return this.nextInt(Integer.MAX_VALUE);
}
}
/**
* Utility class constructor
*/
private PlatformRandomHelper() { }
}

View File

@ -20,10 +20,15 @@
jlong Java_org_rocksdb_HashSkipListMemTableConfig_newMemTableFactoryHandle( jlong Java_org_rocksdb_HashSkipListMemTableConfig_newMemTableFactoryHandle(
JNIEnv* env, jobject jobj, jlong jbucket_count, JNIEnv* env, jobject jobj, jlong jbucket_count,
jint jheight, jint jbranching_factor) { jint jheight, jint jbranching_factor) {
return reinterpret_cast<jlong>(rocksdb::NewHashSkipListRepFactory( rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(jbucket_count);
rocksdb::jlong_to_size_t(jbucket_count), if (s.ok()) {
static_cast<int32_t>(jheight), return reinterpret_cast<jlong>(rocksdb::NewHashSkipListRepFactory(
static_cast<int32_t>(jbranching_factor))); static_cast<size_t>(jbucket_count),
static_cast<int32_t>(jheight),
static_cast<int32_t>(jbranching_factor)));
}
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
return 0;
} }
/* /*
@ -33,8 +38,13 @@ jlong Java_org_rocksdb_HashSkipListMemTableConfig_newMemTableFactoryHandle(
*/ */
jlong Java_org_rocksdb_HashLinkedListMemTableConfig_newMemTableFactoryHandle( jlong Java_org_rocksdb_HashLinkedListMemTableConfig_newMemTableFactoryHandle(
JNIEnv* env, jobject jobj, jlong jbucket_count) { JNIEnv* env, jobject jobj, jlong jbucket_count) {
return reinterpret_cast<jlong>(rocksdb::NewHashLinkListRepFactory( rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(jbucket_count);
rocksdb::jlong_to_size_t(jbucket_count))); if (s.ok()) {
return reinterpret_cast<jlong>(rocksdb::NewHashLinkListRepFactory(
static_cast<size_t>(jbucket_count)));
}
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
return 0;
} }
/* /*
@ -44,8 +54,13 @@ jlong Java_org_rocksdb_HashLinkedListMemTableConfig_newMemTableFactoryHandle(
*/ */
jlong Java_org_rocksdb_VectorMemTableConfig_newMemTableFactoryHandle( jlong Java_org_rocksdb_VectorMemTableConfig_newMemTableFactoryHandle(
JNIEnv* env, jobject jobj, jlong jreserved_size) { JNIEnv* env, jobject jobj, jlong jreserved_size) {
return reinterpret_cast<jlong>(new rocksdb::VectorRepFactory( rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(jreserved_size);
rocksdb::jlong_to_size_t(jreserved_size))); if (s.ok()) {
return reinterpret_cast<jlong>(new rocksdb::VectorRepFactory(
static_cast<size_t>(jreserved_size)));
}
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
return 0;
} }
/* /*

View File

@ -71,7 +71,7 @@ jboolean Java_org_rocksdb_Options_createIfMissing(
*/ */
void Java_org_rocksdb_Options_setBuiltinComparator( void Java_org_rocksdb_Options_setBuiltinComparator(
JNIEnv* env, jobject jobj, jlong jhandle, jint builtinComparator) { JNIEnv* env, jobject jobj, jlong jhandle, jint builtinComparator) {
switch (builtinComparator){ switch (builtinComparator) {
case 1: case 1:
reinterpret_cast<rocksdb::Options*>(jhandle)->comparator = reinterpret_cast<rocksdb::Options*>(jhandle)->comparator =
rocksdb::ReverseBytewiseComparator(); rocksdb::ReverseBytewiseComparator();
@ -90,11 +90,15 @@ void Java_org_rocksdb_Options_setBuiltinComparator(
*/ */
void Java_org_rocksdb_Options_setWriteBufferSize( void Java_org_rocksdb_Options_setWriteBufferSize(
JNIEnv* env, jobject jobj, jlong jhandle, jlong jwrite_buffer_size) { JNIEnv* env, jobject jobj, jlong jhandle, jlong jwrite_buffer_size) {
reinterpret_cast<rocksdb::Options*>(jhandle)->write_buffer_size = rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(jwrite_buffer_size);
rocksdb::jlong_to_size_t(jwrite_buffer_size); if (s.ok()) {
reinterpret_cast<rocksdb::Options*>(jhandle)->write_buffer_size =
jwrite_buffer_size;
} else {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
} }
/* /*
* Class: org_rocksdb_Options * Class: org_rocksdb_Options
* Method: writeBufferSize * Method: writeBufferSize
@ -382,8 +386,13 @@ jlong Java_org_rocksdb_Options_maxLogFileSize(
*/ */
void Java_org_rocksdb_Options_setMaxLogFileSize( void Java_org_rocksdb_Options_setMaxLogFileSize(
JNIEnv* env, jobject jobj, jlong jhandle, jlong max_log_file_size) { JNIEnv* env, jobject jobj, jlong jhandle, jlong max_log_file_size) {
reinterpret_cast<rocksdb::Options*>(jhandle)->max_log_file_size = rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(max_log_file_size);
rocksdb::jlong_to_size_t(max_log_file_size); if (s.ok()) {
reinterpret_cast<rocksdb::Options*>(jhandle)->max_log_file_size =
max_log_file_size;
} else {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
} }
/* /*
@ -403,8 +412,14 @@ jlong Java_org_rocksdb_Options_logFileTimeToRoll(
*/ */
void Java_org_rocksdb_Options_setLogFileTimeToRoll( void Java_org_rocksdb_Options_setLogFileTimeToRoll(
JNIEnv* env, jobject jobj, jlong jhandle, jlong log_file_time_to_roll) { JNIEnv* env, jobject jobj, jlong jhandle, jlong log_file_time_to_roll) {
reinterpret_cast<rocksdb::Options*>(jhandle)->log_file_time_to_roll = rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(
rocksdb::jlong_to_size_t(log_file_time_to_roll); log_file_time_to_roll);
if (s.ok()) {
reinterpret_cast<rocksdb::Options*>(jhandle)->log_file_time_to_roll =
log_file_time_to_roll;
} else {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
} }
/* /*
@ -424,8 +439,13 @@ jlong Java_org_rocksdb_Options_keepLogFileNum(
*/ */
void Java_org_rocksdb_Options_setKeepLogFileNum( void Java_org_rocksdb_Options_setKeepLogFileNum(
JNIEnv* env, jobject jobj, jlong jhandle, jlong keep_log_file_num) { JNIEnv* env, jobject jobj, jlong jhandle, jlong keep_log_file_num) {
reinterpret_cast<rocksdb::Options*>(jhandle)->keep_log_file_num = rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(keep_log_file_num);
rocksdb::jlong_to_size_t(keep_log_file_num); if (s.ok()) {
reinterpret_cast<rocksdb::Options*>(jhandle)->keep_log_file_num =
keep_log_file_num;
} else {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
} }
/* /*
@ -542,7 +562,7 @@ void Java_org_rocksdb_Options_useFixedLengthPrefixExtractor(
JNIEnv* env, jobject jobj, jlong jhandle, jint jprefix_length) { JNIEnv* env, jobject jobj, jlong jhandle, jint jprefix_length) {
reinterpret_cast<rocksdb::Options*>(jhandle)->prefix_extractor.reset( reinterpret_cast<rocksdb::Options*>(jhandle)->prefix_extractor.reset(
rocksdb::NewFixedPrefixTransform( rocksdb::NewFixedPrefixTransform(
rocksdb::jlong_to_size_t(jprefix_length))); static_cast<int>(jprefix_length)));
} }
/* /*
@ -605,8 +625,13 @@ jlong Java_org_rocksdb_Options_manifestPreallocationSize(
*/ */
void Java_org_rocksdb_Options_setManifestPreallocationSize( void Java_org_rocksdb_Options_setManifestPreallocationSize(
JNIEnv* env, jobject jobj, jlong jhandle, jlong preallocation_size) { JNIEnv* env, jobject jobj, jlong jhandle, jlong preallocation_size) {
reinterpret_cast<rocksdb::Options*>(jhandle)->manifest_preallocation_size = rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(preallocation_size);
rocksdb::jlong_to_size_t(preallocation_size); if (s.ok()) {
reinterpret_cast<rocksdb::Options*>(jhandle)->manifest_preallocation_size =
preallocation_size;
} else {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
} }
/* /*
@ -1256,8 +1281,13 @@ jlong Java_org_rocksdb_Options_arenaBlockSize(
*/ */
void Java_org_rocksdb_Options_setArenaBlockSize( void Java_org_rocksdb_Options_setArenaBlockSize(
JNIEnv* env, jobject jobj, jlong jhandle, jlong jarena_block_size) { JNIEnv* env, jobject jobj, jlong jhandle, jlong jarena_block_size) {
reinterpret_cast<rocksdb::Options*>(jhandle)->arena_block_size = rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(jarena_block_size);
rocksdb::jlong_to_size_t(jarena_block_size); if (s.ok()) {
reinterpret_cast<rocksdb::Options*>(jhandle)->arena_block_size =
jarena_block_size;
} else {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
} }
/* /*
@ -1420,9 +1450,14 @@ jlong Java_org_rocksdb_Options_inplaceUpdateNumLocks(
void Java_org_rocksdb_Options_setInplaceUpdateNumLocks( void Java_org_rocksdb_Options_setInplaceUpdateNumLocks(
JNIEnv* env, jobject jobj, jlong jhandle, JNIEnv* env, jobject jobj, jlong jhandle,
jlong jinplace_update_num_locks) { jlong jinplace_update_num_locks) {
reinterpret_cast<rocksdb::Options*>( rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(
jhandle)->inplace_update_num_locks = jinplace_update_num_locks);
rocksdb::jlong_to_size_t(jinplace_update_num_locks); if (s.ok()) {
reinterpret_cast<rocksdb::Options*>(jhandle)->inplace_update_num_locks =
jinplace_update_num_locks;
} else {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
} }
/* /*
@ -1512,8 +1547,14 @@ jlong Java_org_rocksdb_Options_maxSuccessiveMerges(
void Java_org_rocksdb_Options_setMaxSuccessiveMerges( void Java_org_rocksdb_Options_setMaxSuccessiveMerges(
JNIEnv* env, jobject jobj, jlong jhandle, JNIEnv* env, jobject jobj, jlong jhandle,
jlong jmax_successive_merges) { jlong jmax_successive_merges) {
reinterpret_cast<rocksdb::Options*>(jhandle)->max_successive_merges = rocksdb::Status s = rocksdb::check_if_jlong_fits_size_t(
rocksdb::jlong_to_size_t(jmax_successive_merges); jmax_successive_merges);
if (s.ok()) {
reinterpret_cast<rocksdb::Options*>(jhandle)->max_successive_merges =
jmax_successive_merges;
} else {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
}
} }
/* /*

View File

@ -14,14 +14,18 @@
#include <limits> #include <limits>
#include "rocksdb/db.h" #include "rocksdb/db.h"
#include "rocksdb/filter_policy.h" #include "rocksdb/filter_policy.h"
#include "rocksdb/status.h"
#include "rocksdb/utilities/backupable_db.h" #include "rocksdb/utilities/backupable_db.h"
namespace rocksdb { namespace rocksdb {
inline size_t jlong_to_size_t(const jlong& jvalue) { // detect if jlong overflows size_t
return static_cast<uint64_t>(jvalue) <= inline Status check_if_jlong_fits_size_t(const jlong& jvalue) {
static_cast<uint64_t>(std::numeric_limits<size_t>::max()) ? Status s = Status::OK();
static_cast<size_t>(jvalue) : std::numeric_limits<size_t>::max(); if (static_cast<uint64_t>(jvalue) > std::numeric_limits<size_t>::max()) {
s = Status::InvalidArgument(Slice("jlong overflows 32 bit value."));
}
return s;
} }
// The portal class for org.rocksdb.RocksDB // The portal class for org.rocksdb.RocksDB

View File

@ -18,7 +18,7 @@ jlong Java_org_rocksdb_GenericRateLimiterConfig_newRateLimiterHandle(
JNIEnv* env, jobject jobj, jlong jrate_bytes_per_second, JNIEnv* env, jobject jobj, jlong jrate_bytes_per_second,
jlong jrefill_period_micros, jint jfairness) { jlong jrefill_period_micros, jint jfairness) {
return reinterpret_cast<jlong>(rocksdb::NewGenericRateLimiter( return reinterpret_cast<jlong>(rocksdb::NewGenericRateLimiter(
rocksdb::jlong_to_size_t(jrate_bytes_per_second), static_cast<int64_t>(jrate_bytes_per_second),
rocksdb::jlong_to_size_t(jrefill_period_micros), static_cast<int64_t>(jrefill_period_micros),
static_cast<int32_t>(jfairness))); static_cast<int32_t>(jfairness)));
} }

View File

@ -10,7 +10,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <jni.h> #include <jni.h>
#include <iostream>
#include <string> #include <string>
#include "include/org_rocksdb_RestoreOptions.h" #include "include/org_rocksdb_RestoreOptions.h"
@ -72,7 +71,7 @@ void Java_org_rocksdb_RestoreBackupableDB_restoreDBFromBackup0(JNIEnv* env,
env->ReleaseStringUTFChars(jdb_dir, cdb_dir); env->ReleaseStringUTFChars(jdb_dir, cdb_dir);
env->ReleaseStringUTFChars(jwal_dir, cwal_dir); env->ReleaseStringUTFChars(jwal_dir, cwal_dir);
if(!s.ok()) { if (!s.ok()) {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s); rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
} }
} }
@ -97,7 +96,7 @@ void Java_org_rocksdb_RestoreBackupableDB_restoreDBFromLatestBackup0(
env->ReleaseStringUTFChars(jdb_dir, cdb_dir); env->ReleaseStringUTFChars(jdb_dir, cdb_dir);
env->ReleaseStringUTFChars(jwal_dir, cwal_dir); env->ReleaseStringUTFChars(jwal_dir, cwal_dir);
if(!s.ok()) { if (!s.ok()) {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s); rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
} }
} }
@ -112,7 +111,7 @@ void Java_org_rocksdb_RestoreBackupableDB_purgeOldBackups0(JNIEnv* env,
auto rdb = reinterpret_cast<rocksdb::RestoreBackupableDB*>(jhandle); auto rdb = reinterpret_cast<rocksdb::RestoreBackupableDB*>(jhandle);
rocksdb::Status s = rdb->PurgeOldBackups(jnum_backups_to_keep); rocksdb::Status s = rdb->PurgeOldBackups(jnum_backups_to_keep);
if(!s.ok()) { if (!s.ok()) {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s); rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
} }
} }
@ -127,7 +126,7 @@ void Java_org_rocksdb_RestoreBackupableDB_deleteBackup0(JNIEnv* env,
auto rdb = reinterpret_cast<rocksdb::RestoreBackupableDB*>(jhandle); auto rdb = reinterpret_cast<rocksdb::RestoreBackupableDB*>(jhandle);
rocksdb::Status s = rdb->DeleteBackup(jbackup_id); rocksdb::Status s = rdb->DeleteBackup(jbackup_id);
if(!s.ok()) { if (!s.ok()) {
rocksdb::RocksDBExceptionJni::ThrowNew(env, s); rocksdb::RocksDBExceptionJni::ThrowNew(env, s);
} }
} }

View File

@ -246,7 +246,7 @@ jobject multi_get_helper(JNIEnv* env, jobject jdb, rocksdb::DB* db,
jkey_list, rocksdb::ListJni::getIteratorMethod(env)); jkey_list, rocksdb::ListJni::getIteratorMethod(env));
// iterate over keys and convert java byte array to slice // iterate over keys and convert java byte array to slice
while(env->CallBooleanMethod( while (env->CallBooleanMethod(
iteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) { iteratorObj, rocksdb::ListJni::getHasNextMethod(env)) == JNI_TRUE) {
jbyteArray jkey = (jbyteArray) env->CallObjectMethod( jbyteArray jkey = (jbyteArray) env->CallObjectMethod(
iteratorObj, rocksdb::ListJni::getNextMethod(env)); iteratorObj, rocksdb::ListJni::getNextMethod(env));
@ -272,23 +272,22 @@ jobject multi_get_helper(JNIEnv* env, jobject jdb, rocksdb::DB* db,
jobject jvalue_list = env->NewObject(jclazz, mid, jkeys_count); jobject jvalue_list = env->NewObject(jclazz, mid, jkeys_count);
// insert in java list // insert in java list
for(std::vector<rocksdb::Status>::size_type i = 0; i != s.size(); i++) { for (std::vector<rocksdb::Status>::size_type i = 0; i != s.size(); i++) {
if(s[i].ok()) { if (s[i].ok()) {
jbyteArray jvalue = env->NewByteArray(values[i].size()); jbyteArray jvalue = env->NewByteArray(values[i].size());
env->SetByteArrayRegion( env->SetByteArrayRegion(
jvalue, 0, values[i].size(), jvalue, 0, values[i].size(),
reinterpret_cast<const jbyte*>(values[i].c_str())); reinterpret_cast<const jbyte*>(values[i].c_str()));
env->CallBooleanMethod( env->CallBooleanMethod(
jvalue_list, rocksdb::ListJni::getListAddMethodId(env), jvalue); jvalue_list, rocksdb::ListJni::getListAddMethodId(env), jvalue);
} } else {
else {
env->CallBooleanMethod( env->CallBooleanMethod(
jvalue_list, rocksdb::ListJni::getListAddMethodId(env), nullptr); jvalue_list, rocksdb::ListJni::getListAddMethodId(env), nullptr);
} }
} }
// free up allocated byte arrays // free up allocated byte arrays
for(std::vector<jbyte*>::size_type i = 0; i != keys_to_free.size(); i++) { for (std::vector<jbyte*>::size_type i = 0; i != keys_to_free.size(); i++) {
delete[] keys_to_free[i]; delete[] keys_to_free[i];
} }
keys_to_free.clear(); keys_to_free.clear();

View File

@ -30,7 +30,7 @@
void Java_org_rocksdb_WriteBatch_newWriteBatch( void Java_org_rocksdb_WriteBatch_newWriteBatch(
JNIEnv* env, jobject jobj, jint jreserved_bytes) { JNIEnv* env, jobject jobj, jint jreserved_bytes) {
rocksdb::WriteBatch* wb = new rocksdb::WriteBatch( rocksdb::WriteBatch* wb = new rocksdb::WriteBatch(
rocksdb::jlong_to_size_t(jreserved_bytes)); static_cast<size_t>(jreserved_bytes));
rocksdb::WriteBatchJni::setHandle(env, jobj, wb); rocksdb::WriteBatchJni::setHandle(env, jobj, wb);
} }