diff --git a/java/Makefile b/java/Makefile index 8ef86b1e3..1c3ff5a96 100644 --- a/java/Makefile +++ b/java/Makefile @@ -1,4 +1,5 @@ -NATIVE_JAVA_CLASSES = org.rocksdb.AbstractComparator\ +NATIVE_JAVA_CLASSES = org.rocksdb.AbstractCompactionFilter\ + org.rocksdb.AbstractComparator\ org.rocksdb.AbstractSlice\ org.rocksdb.BackupEngine\ org.rocksdb.BackupableDB\ @@ -24,6 +25,7 @@ NATIVE_JAVA_CLASSES = org.rocksdb.AbstractComparator\ org.rocksdb.Options\ org.rocksdb.PlainTableConfig\ org.rocksdb.ReadOptions\ + org.rocksdb.RemoveEmptyValueCompactionFilter\ org.rocksdb.RestoreBackupableDB\ org.rocksdb.RestoreOptions\ org.rocksdb.RocksDB\ diff --git a/java/rocksjni/compaction_filter.cc b/java/rocksjni/compaction_filter.cc new file mode 100644 index 000000000..5fa52c0dc --- /dev/null +++ b/java/rocksjni/compaction_filter.cc @@ -0,0 +1,24 @@ +// Copyright (c) 2015, 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. +// +// This file implements the "bridge" between Java and C++ for +// rocksdb::CompactionFilter. + +#include + +#include "rocksdb/compaction_filter.h" + +// + +/* + * Class: org_rocksdb_AbstractCompactionFilter + * Method: disposeInternal + * Signature: (J)V + */ +void Java_org_rocksdb_AbstractCompactionFilter_disposeInternal( + JNIEnv* env, jobject jobj, jlong handle) { + delete reinterpret_cast(handle); +} +// diff --git a/java/rocksjni/options.cc b/java/rocksjni/options.cc index 228e0f45c..ffb0a4e56 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -2041,6 +2041,19 @@ void Java_org_rocksdb_ColumnFamilyOptions_setMergeOperator( (mergeOperatorHandle)); } +/* + * Class: org_rocksdb_ColumnFamilyOptions + * Method: setCompactionFilterHandle + * Signature: (JJ)V + */ +void Java_org_rocksdb_ColumnFamilyOptions_setCompactionFilterHandle__JJ( + JNIEnv* env, jobject jobj, jlong jopt_handle, + jlong jcompactionfilter_handle) { + reinterpret_cast(jopt_handle)-> + compaction_filter = reinterpret_cast + (jcompactionfilter_handle); +} + /* * Class: org_rocksdb_ColumnFamilyOptions * Method: setWriteBufferSize diff --git a/java/rocksjni/remove_emptyvalue_compactionfilterjni.cc b/java/rocksjni/remove_emptyvalue_compactionfilterjni.cc new file mode 100644 index 000000000..e442d8daf --- /dev/null +++ b/java/rocksjni/remove_emptyvalue_compactionfilterjni.cc @@ -0,0 +1,27 @@ +// Copyright (c) 2015, 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. + +#include + +#include "include/org_rocksdb_RemoveEmptyValueCompactionFilter.h" +#include "utilities/compaction_filters/remove_emptyvalue_compactionfilter.h" + + +/* + * Class: org_rocksdb_RemoveEmptyValueCompactionFilter + * Method: createNewRemoveEmptyValueCompactionFilter0 + * Signature: ()V + */ +void Java_org_rocksdb_RemoveEmptyValueCompactionFilter_createNewRemoveEmptyValueCompactionFilter0( + JNIEnv* env, jobject jobj) { + const rocksdb::RemoveEmptyValueCompactionFilter* compaction_filter = + new rocksdb::RemoveEmptyValueCompactionFilter(); + + // set the native handle to our native compaction filter + static jclass jclazz = + env->FindClass("org/rocksdb/RemoveEmptyValueCompactionFilter"); + static jfieldID fid = env->GetFieldID(jclazz, "nativeHandle_", "J"); + env->SetLongField(jobj, fid, reinterpret_cast(compaction_filter)); +} diff --git a/java/src/main/java/org/rocksdb/AbstractCompactionFilter.java b/java/src/main/java/org/rocksdb/AbstractCompactionFilter.java new file mode 100644 index 000000000..2b78deddb --- /dev/null +++ b/java/src/main/java/org/rocksdb/AbstractCompactionFilter.java @@ -0,0 +1,29 @@ +// Copyright (c) 2015, 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; + +/** + * A CompactionFilter allows an application to modify/delete a key-value at + * the time of compaction. + * + * At present we just permit an overriding Java class to wrap a C++ implementation + */ +public abstract class AbstractCompactionFilter> + extends RocksObject { + + /** + * Deletes underlying C++ comparator pointer. + * + * Note that this function should be called only after all + * RocksDB instances referencing the comparator are closed. + * Otherwise an undefined behavior will occur. + */ + @Override protected void disposeInternal() { + assert(isInitialized()); + disposeInternal(nativeHandle_); + } + + private native void disposeInternal(long handle); +} diff --git a/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java b/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java index 32cb341db..4304f589a 100644 --- a/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java +++ b/java/src/main/java/org/rocksdb/ColumnFamilyOptions.java @@ -145,6 +145,13 @@ public class ColumnFamilyOptions extends RocksObject return this; } + public ColumnFamilyOptions setCompactionFilter( + final AbstractCompactionFilter> compactionFilter) { + setCompactionFilterHandle(nativeHandle_, compactionFilter.nativeHandle_); + compactionFilter_ = compactionFilter; + return this; + } + @Override public ColumnFamilyOptions setWriteBufferSize(final long writeBufferSize) { assert(isInitialized()); @@ -686,6 +693,7 @@ public class ColumnFamilyOptions extends RocksObject long handle, String name); private native void setMergeOperator( long handle, long mergeOperatorHandle); + private native void setCompactionFilterHandle(long handle, long compactionFilterHandle); private native void setWriteBufferSize(long handle, long writeBufferSize) throws IllegalArgumentException; private native long writeBufferSize(long handle); @@ -808,4 +816,5 @@ public class ColumnFamilyOptions extends RocksObject MemTableConfig memTableConfig_; TableFormatConfig tableFormatConfig_; AbstractComparator> comparator_; + AbstractCompactionFilter> compactionFilter_; } diff --git a/java/src/main/java/org/rocksdb/RemoveEmptyValueCompactionFilter.java b/java/src/main/java/org/rocksdb/RemoveEmptyValueCompactionFilter.java new file mode 100644 index 000000000..61c46131b --- /dev/null +++ b/java/src/main/java/org/rocksdb/RemoveEmptyValueCompactionFilter.java @@ -0,0 +1,18 @@ +// 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; + +/** + * Just a Java wrapper around EmptyValueCompactionFilter implemented in C++ + */ +public class RemoveEmptyValueCompactionFilter extends AbstractCompactionFilter { + public RemoveEmptyValueCompactionFilter() { + super(); + createNewRemoveEmptyValueCompactionFilter0(); + } + + private native void createNewRemoveEmptyValueCompactionFilter0(); +} diff --git a/src.mk b/src.mk index 00b940d9a..8311a77ed 100644 --- a/src.mk +++ b/src.mk @@ -99,6 +99,7 @@ LIB_SOURCES = \ util/iostats_context.cc \ utilities/backupable/backupable_db.cc \ utilities/checkpoint/checkpoint.cc \ + utilities/compaction_filters/remove_emptyvalue_compactionfilter.cc \ utilities/document/document_db.cc \ utilities/document/json_document_builder.cc \ utilities/document/json_document.cc \ @@ -251,6 +252,7 @@ JNI_NATIVE_SOURCES = \ java/rocksjni/backupablejni.cc \ java/rocksjni/checkpoint.cc \ java/rocksjni/columnfamilyhandle.cc \ + java/rocksjni/compaction_filter.cc \ java/rocksjni/comparator.cc \ java/rocksjni/comparatorjnicallback.cc \ java/rocksjni/env.cc \ @@ -261,6 +263,7 @@ JNI_NATIVE_SOURCES = \ java/rocksjni/merge_operator.cc \ java/rocksjni/options.cc \ java/rocksjni/ratelimiterjni.cc \ + java/rocksjni/remove_emptyvalue_compactionfilterjni.cc \ java/rocksjni/restorejni.cc \ java/rocksjni/rocksjni.cc \ java/rocksjni/slice.cc \ diff --git a/utilities/compaction_filters/remove_emptyvalue_compactionfilter.cc b/utilities/compaction_filters/remove_emptyvalue_compactionfilter.cc new file mode 100644 index 000000000..4ef4edf92 --- /dev/null +++ b/utilities/compaction_filters/remove_emptyvalue_compactionfilter.cc @@ -0,0 +1,30 @@ +// Copyright (c) 2015, 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. + +#ifndef ROCKSDB_LITE + +#include + +#include "rocksdb/slice.h" +#include "utilities/compaction_filters/remove_emptyvalue_compactionfilter.h" + +namespace rocksdb { + +const char* RemoveEmptyValueCompactionFilter::Name() const { + return "RemoveEmptyValueCompactionFilter"; +} + +bool RemoveEmptyValueCompactionFilter::Filter(int level, + const Slice& key, + const Slice& existing_value, + std::string* new_value, + bool* value_changed) const { + + // remove kv pairs that have empty values + return existing_value.empty(); +} + +} // namespace rocksdb +#endif // !ROCKSDB_LITE diff --git a/utilities/compaction_filters/remove_emptyvalue_compactionfilter.h b/utilities/compaction_filters/remove_emptyvalue_compactionfilter.h new file mode 100644 index 000000000..ec9342d38 --- /dev/null +++ b/utilities/compaction_filters/remove_emptyvalue_compactionfilter.h @@ -0,0 +1,27 @@ +// Copyright (c) 2015, 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. + +#ifndef ROCKSDB_LITE + +#pragma once + +#include + +#include "rocksdb/compaction_filter.h" +#include "rocksdb/slice.h" + +namespace rocksdb { + +class RemoveEmptyValueCompactionFilter : public CompactionFilter { + public: + const char* Name() const override; + bool Filter(int level, + const Slice& key, + const Slice& existing_value, + std::string* new_value, + bool* value_changed) const override; +}; +} // namespace rocksdb +#endif // !ROCKSDB_LITE