diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index fbf324179..75c17d086 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -3624,6 +3624,29 @@ jlong Java_org_rocksdb_Options_ttl( return static_cast(opts->ttl); } +/* + * Class: org_rocksdb_Options + * Method: setPeriodicCompactionSeconds + * Signature: (JJ)V + */ +void Java_org_rocksdb_Options_setPeriodicCompactionSeconds( + JNIEnv*, jobject, jlong jhandle, jlong jperiodicCompactionSeconds) { + auto* opts = reinterpret_cast(jhandle); + opts->periodic_compaction_seconds = + static_cast(jperiodicCompactionSeconds); +} + +/* + * Class: org_rocksdb_Options + * Method: periodicCompactionSeconds + * Signature: (J)J + */ +jlong Java_org_rocksdb_Options_periodicCompactionSeconds(JNIEnv*, jobject, + jlong jhandle) { + auto* opts = reinterpret_cast(jhandle); + return static_cast(opts->periodic_compaction_seconds); +} + /* * Class: org_rocksdb_Options * Method: setCompactionOptionsUniversal @@ -5148,6 +5171,32 @@ JNIEXPORT jlong JNICALL Java_org_rocksdb_ColumnFamilyOptions_ttl( return static_cast(cf_opts->ttl); } +/* + * Class: org_rocksdb_ColumnFamilyOptions + * Method: setPeriodicCompactionSeconds + * Signature: (JJ)V + */ +void Java_org_rocksdb_ColumnFamilyOptions_setPeriodicCompactionSeconds( + JNIEnv*, jobject, jlong jhandle, jlong jperiodicCompactionSeconds) { + auto* cf_opts = + reinterpret_cast(jhandle); + cf_opts->periodic_compaction_seconds = + static_cast(jperiodicCompactionSeconds); +} + +/* + * Class: org_rocksdb_ColumnFamilyOptions + * Method: periodicCompactionSeconds + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL +Java_org_rocksdb_ColumnFamilyOptions_periodicCompactionSeconds(JNIEnv*, jobject, + jlong jhandle) { + auto* cf_opts = + reinterpret_cast(jhandle); + return static_cast(cf_opts->periodic_compaction_seconds); +} + /* * Class: org_rocksdb_ColumnFamilyOptions * Method: setCompactionOptionsUniversal diff --git a/java/src/main/java/org/rocksdb/AdvancedMutableColumnFamilyOptionsInterface.java b/java/src/main/java/org/rocksdb/AdvancedMutableColumnFamilyOptionsInterface.java index 5b581ca1d..e5de201c8 100644 --- a/java/src/main/java/org/rocksdb/AdvancedMutableColumnFamilyOptionsInterface.java +++ b/java/src/main/java/org/rocksdb/AdvancedMutableColumnFamilyOptionsInterface.java @@ -461,4 +461,52 @@ public interface AdvancedMutableColumnFamilyOptionsInterface< * @return the time-to-live. */ long ttl(); + + /** + * Files older than this value will be picked up for compaction, and + * re-written to the same level as they were before. + * One main use of the feature is to make sure a file goes through compaction + * filters periodically. Users can also use the feature to clear up SST + * files using old format. + * + * A file's age is computed by looking at file_creation_time or creation_time + * table properties in order, if they have valid non-zero values; if not, the + * age is based on the file's last modified time (given by the underlying + * Env). + * + * Supported in Level and FIFO compaction. + * In FIFO compaction, this option has the same meaning as TTL and whichever + * stricter will be used. + * Pre-req: max_open_file == -1. + * unit: seconds. Ex: 7 days = 7 * 24 * 60 * 60 + * + * Values: + * 0: Turn off Periodic compactions. + * UINT64_MAX - 1 (i.e 0xfffffffffffffffe): Let RocksDB control this feature + * as needed. For now, RocksDB will change this value to 30 days + * (i.e 30 * 24 * 60 * 60) so that every file goes through the compaction + * process at least once every 30 days if not compacted sooner. + * In FIFO compaction, since the option has the same meaning as ttl, + * when this value is left default, and ttl is left to 0, 30 days will be + * used. Otherwise, min(ttl, periodic_compaction_seconds) will be used. + * + * Default: 0xfffffffffffffffe (allow RocksDB to auto-tune) + * + * Dynamically changeable through + * {@link RocksDB#setOptions(ColumnFamilyHandle, MutableColumnFamilyOptions)}. + * + * @param periodicCompactionSeconds the periodic compaction in seconds. + * + * @return the reference to the current options. + */ + T setPeriodicCompactionSeconds(final long periodicCompactionSeconds); + + /** + * Get the periodicCompactionSeconds. + * + * See {@link #setPeriodicCompactionSeconds(long)}. + * + * @return the periodic compaction in seconds. + */ + long periodicCompactionSeconds(); } diff --git a/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java b/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java index 72149bf26..07ed52721 100644 --- a/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java +++ b/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java @@ -857,6 +857,17 @@ public class ColumnFamilyOptions extends RocksObject return ttl(nativeHandle_); } + @Override + public ColumnFamilyOptions setPeriodicCompactionSeconds(final long periodicCompactionSeconds) { + setPeriodicCompactionSeconds(nativeHandle_, periodicCompactionSeconds); + return this; + } + + @Override + public long periodicCompactionSeconds() { + return periodicCompactionSeconds(nativeHandle_); + } + @Override public ColumnFamilyOptions setCompactionOptionsUniversal( final CompactionOptionsUniversal compactionOptionsUniversal) { @@ -1083,6 +1094,9 @@ public class ColumnFamilyOptions extends RocksObject private native boolean reportBgIoStats(final long handle); private native void setTtl(final long handle, final long ttl); private native long ttl(final long handle); + private native void setPeriodicCompactionSeconds( + final long handle, final long periodicCompactionSeconds); + private native long periodicCompactionSeconds(final long handle); private native void setCompactionOptionsUniversal(final long handle, final long compactionOptionsUniversalHandle); private native void setCompactionOptionsFIFO(final long handle, diff --git a/java/src/main/java/org/rocksdb/MutableColumnFamilyOptions.java b/java/src/main/java/org/rocksdb/MutableColumnFamilyOptions.java index 1d9ca0817..34a6d823e 100644 --- a/java/src/main/java/org/rocksdb/MutableColumnFamilyOptions.java +++ b/java/src/main/java/org/rocksdb/MutableColumnFamilyOptions.java @@ -117,7 +117,8 @@ public class MutableColumnFamilyOptions max_bytes_for_level_base(ValueType.LONG), max_bytes_for_level_multiplier(ValueType.INT), max_bytes_for_level_multiplier_additional(ValueType.INT_ARRAY), - ttl(ValueType.LONG); + ttl(ValueType.LONG), + periodic_compaction_seconds(ValueType.LONG); private final ValueType valueType; CompactionOption(final ValueType valueType) { @@ -465,5 +466,16 @@ public class MutableColumnFamilyOptions public long ttl() { return getLong(CompactionOption.ttl); } + + @Override + public MutableColumnFamilyOptionsBuilder setPeriodicCompactionSeconds( + final long periodicCompactionSeconds) { + return setLong(CompactionOption.periodic_compaction_seconds, periodicCompactionSeconds); + } + + @Override + public long periodicCompactionSeconds() { + return getLong(CompactionOption.periodic_compaction_seconds); + } } } diff --git a/java/src/main/java/org/rocksdb/Options.java b/java/src/main/java/org/rocksdb/Options.java index 57f3aeffb..2efe44bc1 100644 --- a/java/src/main/java/org/rocksdb/Options.java +++ b/java/src/main/java/org/rocksdb/Options.java @@ -1848,6 +1848,17 @@ public class Options extends RocksObject return ttl(nativeHandle_); } + @Override + public Options setPeriodicCompactionSeconds(final long periodicCompactionSeconds) { + setPeriodicCompactionSeconds(nativeHandle_, periodicCompactionSeconds); + return this; + } + + @Override + public long periodicCompactionSeconds() { + return periodicCompactionSeconds(nativeHandle_); + } + @Override public Options setCompactionOptionsUniversal( final CompactionOptionsUniversal compactionOptionsUniversal) { @@ -2383,6 +2394,9 @@ public class Options extends RocksObject private native boolean reportBgIoStats(final long handle); private native void setTtl(final long handle, final long ttl); private native long ttl(final long handle); + private native void setPeriodicCompactionSeconds( + final long handle, final long periodicCompactionSeconds); + private native long periodicCompactionSeconds(final long handle); private native void setCompactionOptionsUniversal(final long handle, final long compactionOptionsUniversalHandle); private native void setCompactionOptionsFIFO(final long handle, diff --git a/java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java b/java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java index 830df3f8a..0919d7485 100644 --- a/java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java +++ b/java/src/test/java/org/rocksdb/ColumnFamilyOptionsTest.java @@ -589,6 +589,14 @@ public class ColumnFamilyOptionsTest { } } + @Test + public void periodicCompactionSeconds() { + try (final ColumnFamilyOptions options = new ColumnFamilyOptions()) { + options.setPeriodicCompactionSeconds(1000 * 60); + assertThat(options.periodicCompactionSeconds()).isEqualTo(1000 * 60); + } + } + @Test public void compactionOptionsUniversal() { try (final ColumnFamilyOptions opt = new ColumnFamilyOptions(); diff --git a/java/src/test/java/org/rocksdb/OptionsTest.java b/java/src/test/java/org/rocksdb/OptionsTest.java index e402cb474..5d732adc8 100644 --- a/java/src/test/java/org/rocksdb/OptionsTest.java +++ b/java/src/test/java/org/rocksdb/OptionsTest.java @@ -1256,6 +1256,14 @@ public class OptionsTest { } } + @Test + public void periodicCompactionSeconds() { + try (final Options options = new Options()) { + options.setPeriodicCompactionSeconds(1000 * 60); + assertThat(options.periodicCompactionSeconds()).isEqualTo(1000 * 60); + } + } + @Test public void compactionOptionsUniversal() { try (final Options options = new Options();