Summary: This pull request solves the jlong overflow problem on 32-Bit machines as described in https://github.com/facebook/rocksdb/issues/278: 1. There is a new org.rocksdb.test.PlatformRandomHelper to assist in getting random values. For 32 Bit the getLong method is overriden by xpromaches code above. For 64 Bit it behaves as is. 2. The detection should be cross-platform (Windows is supported though it is not ported completely yet). 3. Every JNI method which sets jlong values must check if the value fits into size_t. If it overflows size_t a InvalidArgument Status object will be returned. If its ok a OK Status will be returned. 4. Setters which have this check will throw a RocksDBException if its no OK Status. Additionally some other parts of code were corrected using the wrong type casts. Test Plan: make rocksdbjava make jtest Differential Revision: https://reviews.facebook.net/D24531main
parent
833357402c
commit
4f5a687254
@ -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() { } |
||||
} |
Loading…
Reference in new issue