Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: Add copy constructors for various option classes #3450

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions java/rocksjni/options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ jlong Java_org_rocksdb_Options_newOptions__JJ(JNIEnv* env, jclass jcls,
return reinterpret_cast<jlong>(op);
}

/*
* Class: org_rocksdb_Options
* Method: copyOptions
* Signature: (J)J
*/
jlong Java_org_rocksdb_Options_copyOptions(JNIEnv* env, jclass jcls,
jlong jhandle) {
auto new_opt = new rocksdb::Options(
*(reinterpret_cast<rocksdb::Options*>(jhandle)));
return reinterpret_cast<jlong>(new_opt);
}

/*
* Class: org_rocksdb_Options
* Method: disposeInternal
Expand Down Expand Up @@ -2848,6 +2860,18 @@ jlong Java_org_rocksdb_ColumnFamilyOptions_newColumnFamilyOptions(
return reinterpret_cast<jlong>(op);
}

/*
* Class: org_rocksdb_ColumnFamilyOptions
* Method: copyColumnFamilyOptions
* Signature: (J)J
*/
jlong Java_org_rocksdb_ColumnFamilyOptions_copyColumnFamilyOptions(
JNIEnv* env, jclass jcls, jlong jhandle) {
auto new_opt = new rocksdb::ColumnFamilyOptions(
*(reinterpret_cast<rocksdb::ColumnFamilyOptions*>(jhandle)));
return reinterpret_cast<jlong>(new_opt);
}

/*
* Class: org_rocksdb_ColumnFamilyOptions
* Method: getColumnFamilyOptionsFromProps
Expand Down Expand Up @@ -4161,6 +4185,18 @@ jlong Java_org_rocksdb_DBOptions_newDBOptions(JNIEnv* env,
return reinterpret_cast<jlong>(dbop);
}

/*
* Class: org_rocksdb_DBOptions
* Method: copyDBOptions
* Signature: (J)J
*/
jlong Java_org_rocksdb_DBOptions_copyDBOptions(JNIEnv* env, jclass jcls,
jlong jhandle) {
auto new_opt = new rocksdb::DBOptions(
*(reinterpret_cast<rocksdb::DBOptions*>(jhandle)));
return reinterpret_cast<jlong>(new_opt);
}

/*
* Class: org_rocksdb_DBOptions
* Method: getDBOptionsFromProps
Expand Down Expand Up @@ -5690,6 +5726,18 @@ jlong Java_org_rocksdb_WriteOptions_newWriteOptions(
return reinterpret_cast<jlong>(op);
}

/*
* Class: org_rocksdb_WriteOptions
* Method: copyWriteOptions
* Signature: (J)J
*/
jlong Java_org_rocksdb_WriteOptions_copyWriteOptions(
JNIEnv* env, jclass jcls, jlong jhandle) {
auto new_opt = new rocksdb::WriteOptions(
*(reinterpret_cast<rocksdb::WriteOptions*>(jhandle)));
return reinterpret_cast<jlong>(new_opt);
}

/*
* Class: org_rocksdb_WriteOptions
* Method: disposeInternal
Expand Down Expand Up @@ -5808,10 +5856,9 @@ jlong Java_org_rocksdb_ReadOptions_newReadOptions(
*/
jlong Java_org_rocksdb_ReadOptions_copyReadOptions(
JNIEnv* env, jclass jcls, jlong jhandle) {
auto old_read_opt = reinterpret_cast<rocksdb::ReadOptions*>(jhandle);
auto new_read_opt = new rocksdb::ReadOptions();
*new_read_opt = *old_read_opt;
return reinterpret_cast<jlong>(new_read_opt);
auto new_opt = new rocksdb::ReadOptions(
*(reinterpret_cast<rocksdb::ReadOptions*>(jhandle)));
return reinterpret_cast<jlong>(new_opt);
}

/*
Expand Down
24 changes: 23 additions & 1 deletion java/src/main/java/org/rocksdb/ColumnFamilyOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,32 @@ public class ColumnFamilyOptions extends RocksObject
* Construct ColumnFamilyOptions.
*
* This constructor will create (by allocating a block of memory)
* an {@code rocksdb::DBOptions} in the c++ side.
* an {@code rocksdb::ColumnFamilyOptions} in the c++ side.
*/
public ColumnFamilyOptions() {
super(newColumnFamilyOptions());
}

/**
* Copy constructor for ColumnFamilyOptions.
*
* NOTE: This does a shallow copy, which means comparator, merge_operator, compaction_filter,
* compaction_filter_factory and other pointers will be cloned!
*
* @param other The ColumnFamilyOptions to copy.
*/
public ColumnFamilyOptions(ColumnFamilyOptions other) {
super(copyColumnFamilyOptions(other.nativeHandle_));
this.memTableConfig_ = other.memTableConfig_;
this.tableFormatConfig_ = other.tableFormatConfig_;
this.comparator_ = other.comparator_;
this.compactionFilter_ = other.compactionFilter_;
this.compactionFilterFactory_ = other.compactionFilterFactory_;
this.compactionOptionsUniversal_ = other.compactionOptionsUniversal_;
this.compactionOptionsFIFO_ = other.compactionOptionsFIFO_;
this.compressionOptions_ = other.compressionOptions_;
}

/**
* <p>Method to get a options instance by using pre-configured
* property values. If one or many values are undefined in
Expand Down Expand Up @@ -783,6 +803,7 @@ private static native long getColumnFamilyOptionsFromProps(
String optString);

private static native long newColumnFamilyOptions();
private static native long copyColumnFamilyOptions(long handle);
@Override protected final native void disposeInternal(final long handle);

private native void optimizeForSmallDb(final long handle);
Expand Down Expand Up @@ -934,6 +955,7 @@ private native void setForceConsistencyChecks(final long handle,
private native boolean forceConsistencyChecks(final long handle);

// instance variables
// NOTE: If you add new member variables, please update the copy constructor above!
private MemTableConfig memTableConfig_;
private TableFormatConfig tableFormatConfig_;
private AbstractComparator<? extends AbstractSlice<?>> comparator_;
Expand Down
18 changes: 18 additions & 0 deletions java/src/main/java/org/rocksdb/DBOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ public DBOptions() {
numShardBits_ = DEFAULT_NUM_SHARD_BITS;
}

/**
* Copy constructor for DBOptions.
*
* NOTE: This does a shallow copy, which means env, rate_limiter, sst_file_manager,
* info_log and other pointers will be cloned!
*
* @param other The DBOptions to copy.
*/
public DBOptions(DBOptions other) {
super(copyDBOptions(other.nativeHandle_));
this.env_ = other.env_;
this.numShardBits_ = other.numShardBits_;
this.rateLimiter_ = other.rateLimiter_;
this.rowCache_ = other.rowCache_;
}

/**
* <p>Method to get a options instance by using pre-configured
* property values. If one or many values are undefined in
Expand Down Expand Up @@ -954,6 +970,7 @@ private static native long getDBOptionsFromProps(
String optString);

private native static long newDBOptions();
private native static long copyDBOptions(long handle);
@Override protected final native void disposeInternal(final long handle);

private native void optimizeForSmallDb(final long handle);
Expand Down Expand Up @@ -1127,6 +1144,7 @@ private native void setAvoidFlushDuringShutdown(final long handle,
private native boolean avoidFlushDuringShutdown(final long handle);

// instance variables
// NOTE: If you add new member variables, please update the copy constructor above!
private Env env_;
private int numShardBits_;
private RateLimiter rateLimiter_;
Expand Down
23 changes: 23 additions & 0 deletions java/src/main/java/org/rocksdb/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ public Options(final DBOptions dbOptions,
env_ = Env.getDefault();
}

/**
* Copy constructor for ColumnFamilyOptions.
*
* NOTE: This does a shallow copy, which means comparator, merge_operator
* and other pointers will be cloned!
*
* @param other The Options to copy.
*/
public Options(Options other) {
super(copyOptions(other.nativeHandle_));
this.env_ = other.env_;
this.memTableConfig_ = other.memTableConfig_;
this.tableFormatConfig_ = other.tableFormatConfig_;
this.rateLimiter_ = other.rateLimiter_;
this.comparator_ = other.comparator_;
this.compactionOptionsUniversal_ = other.compactionOptionsUniversal_;
this.compactionOptionsFIFO_ = other.compactionOptionsFIFO_;
this.compressionOptions_ = other.compressionOptions_;
this.rowCache_ = other.rowCache_;
}

@Override
public Options setIncreaseParallelism(final int totalThreads) {
assert(isOwningHandle());
Expand Down Expand Up @@ -1548,6 +1569,7 @@ public boolean forceConsistencyChecks() {
private native static long newOptions();
private native static long newOptions(long dbOptHandle,
long cfOptHandle);
private native static long copyOptions(long handle);
@Override protected final native void disposeInternal(final long handle);
private native void setEnv(long optHandle, long envHandle);
private native void prepareForBulkLoad(long handle);
Expand Down Expand Up @@ -1868,6 +1890,7 @@ private native void setForceConsistencyChecks(final long handle,
private native boolean forceConsistencyChecks(final long handle);

// instance variables
// NOTE: If you add new member variables, please update the copy constructor above!
private Env env_;
private MemTableConfig memTableConfig_;
private TableFormatConfig tableFormatConfig_;
Expand Down
3 changes: 3 additions & 0 deletions java/src/main/java/org/rocksdb/ReadOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ public Slice iterateUpperBound() {
return null;
}

// instance variables
// NOTE: If you add new member variables, please update the copy constructor above!
//
// Hold a reference to any iterate upper bound that was set on this object
// until we're destroyed or it's overwritten. That way the caller can freely
// leave scope without us losing the Java Slice object, which during close()
Expand Down
14 changes: 14 additions & 0 deletions java/src/main/java/org/rocksdb/WriteOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ public WriteOptions() {

}

/**
* Copy constructor for WriteOptions.
*
* NOTE: This does a shallow copy, which means comparator, merge_operator, compaction_filter,
* compaction_filter_factory and other pointers will be cloned!
*
* @param other The ColumnFamilyOptions to copy.
*/
public WriteOptions(WriteOptions other) {
super(copyWriteOptions(other.nativeHandle_));
}


/**
* If true, the write will be flushed from the operating system
* buffer cache (by calling WritableFile::Sync()) before the write
Expand Down Expand Up @@ -145,6 +158,7 @@ public boolean noSlowdown() {
}

private native static long newWriteOptions();
private native static long copyWriteOptions(long handle);
private native void setSync(long handle, boolean flag);
private native boolean sync(long handle);
private native void setDisableWAL(long handle, boolean flag);
Expand Down
12 changes: 12 additions & 0 deletions java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public class ColumnFamilyOptionsTest {
public static final Random rand = PlatformRandomHelper.
getPlatformSpecificRandomFactory();

@Test
public void copyConstructor() {
ColumnFamilyOptions origOpts = new ColumnFamilyOptions();
origOpts.setNumLevels(rand.nextInt(8));
origOpts.setTargetFileSizeMultiplier(rand.nextInt(100));
origOpts.setLevel0StopWritesTrigger(rand.nextInt(50));
ColumnFamilyOptions copyOpts = new ColumnFamilyOptions(origOpts);
assertThat(origOpts.numLevels()).isEqualTo(copyOpts.numLevels());
assertThat(origOpts.targetFileSizeMultiplier()).isEqualTo(copyOpts.targetFileSizeMultiplier());
assertThat(origOpts.level0StopWritesTrigger()).isEqualTo(copyOpts.level0StopWritesTrigger());
}

@Test
public void getColumnFamilyOptionsFromProps() {
Properties properties = new Properties();
Expand Down
13 changes: 13 additions & 0 deletions java/src/test/java/org/rocksdb/DBOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ public class DBOptionsTest {
public static final Random rand = PlatformRandomHelper.
getPlatformSpecificRandomFactory();

@Test
public void copyConstructor() {
DBOptions origOpts = new DBOptions();
origOpts.setCreateIfMissing(rand.nextBoolean());
origOpts.setAllow2pc(rand.nextBoolean());
origOpts.setBaseBackgroundCompactions(rand.nextInt(10));
DBOptions copyOpts = new DBOptions(origOpts);
assertThat(origOpts.createIfMissing()).isEqualTo(copyOpts.createIfMissing());
assertThat(origOpts.allow2pc()).isEqualTo(copyOpts.allow2pc());
assertThat(origOpts.baseBackgroundCompactions()).isEqualTo(
copyOpts.baseBackgroundCompactions());
}

@Test
public void getDBOptionsFromProps() {
// setup sample properties
Expand Down
12 changes: 12 additions & 0 deletions java/src/test/java/org/rocksdb/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ public class OptionsTest {
public static final Random rand = PlatformRandomHelper.
getPlatformSpecificRandomFactory();

@Test
public void copyConstructor() {
Options origOpts = new Options();
origOpts.setNumLevels(rand.nextInt(8));
origOpts.setTargetFileSizeMultiplier(rand.nextInt(100));
origOpts.setLevel0StopWritesTrigger(rand.nextInt(50));
Options copyOpts = new Options(origOpts);
assertThat(origOpts.numLevels()).isEqualTo(copyOpts.numLevels());
assertThat(origOpts.targetFileSizeMultiplier()).isEqualTo(copyOpts.targetFileSizeMultiplier());
assertThat(origOpts.level0StopWritesTrigger()).isEqualTo(copyOpts.level0StopWritesTrigger());
}

@Test
public void setIncreaseParallelism() {
try (final Options opt = new Options()) {
Expand Down
19 changes: 19 additions & 0 deletions java/src/test/java/org/rocksdb/WriteOptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.junit.ClassRule;
import org.junit.Test;

import java.util.Random;

import static org.assertj.core.api.Assertions.assertThat;

public class WriteOptionsTest {
Expand All @@ -16,6 +18,9 @@ public class WriteOptionsTest {
public static final RocksMemoryResource rocksMemoryResource =
new RocksMemoryResource();

public static final Random rand = PlatformRandomHelper.
getPlatformSpecificRandomFactory();

@Test
public void writeOptions() {
try (final WriteOptions writeOptions = new WriteOptions()) {
Expand All @@ -42,4 +47,18 @@ public void writeOptions() {
assertThat(writeOptions.noSlowdown()).isFalse();
}
}

@Test
public void copyConstructor() {
WriteOptions origOpts = new WriteOptions();
origOpts.setDisableWAL(rand.nextBoolean());
origOpts.setIgnoreMissingColumnFamilies(rand.nextBoolean());
origOpts.setSync(rand.nextBoolean());
WriteOptions copyOpts = new WriteOptions(origOpts);
assertThat(origOpts.disableWAL()).isEqualTo(copyOpts.disableWAL());
assertThat(origOpts.ignoreMissingColumnFamilies()).isEqualTo(
copyOpts.ignoreMissingColumnFamilies());
assertThat(origOpts.sync()).isEqualTo(copyOpts.sync());
}

}