From 62dec0e2b75bc009d31b5540b7e820a2e1466529 Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Tue, 14 Jul 2015 17:41:20 +0100 Subject: [PATCH 1/3] RemoveEmptyValueCompactionFilter - A compaction filter which removes entries which have an empty value --- src.mk | 1 + .../remove_emptyvalue_compactionfilter.cc | 30 +++++++++++++++++++ .../remove_emptyvalue_compactionfilter.h | 27 +++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 utilities/compaction_filters/remove_emptyvalue_compactionfilter.cc create mode 100644 utilities/compaction_filters/remove_emptyvalue_compactionfilter.h diff --git a/src.mk b/src.mk index 11ca0b2cb..f4a16672c 100644 --- a/src.mk +++ b/src.mk @@ -98,6 +98,7 @@ LIB_SOURCES = \ utilities/backupable/backupable_db.cc \ utilities/convenience/convenience.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 \ 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 From 3d00271e40ca909597559837b62e59cc385053be Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Tue, 14 Jul 2015 17:44:51 +0100 Subject: [PATCH 2/3] The ability to specify a compaction filter via the Java API --- java/Makefile | 3 +- java/rocksjni/compaction_filter.cc | 24 +++++++++++++++ java/rocksjni/options.cc | 13 +++++++++ .../org/rocksdb/AbstractCompactionFilter.java | 29 +++++++++++++++++++ .../java/org/rocksdb/ColumnFamilyOptions.java | 9 ++++++ src.mk | 1 + 6 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 java/rocksjni/compaction_filter.cc create mode 100644 java/src/main/java/org/rocksdb/AbstractCompactionFilter.java diff --git a/java/Makefile b/java/Makefile index 303c46382..fcd441ca4 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.BackupableDB\ org.rocksdb.BackupableDBOptions\ 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 c1d30ee16..98ac12bd3 100644 --- a/java/rocksjni/options.cc +++ b/java/rocksjni/options.cc @@ -2053,6 +2053,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/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 c916ccbd8..ab0d55161 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()); @@ -680,6 +687,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); @@ -803,4 +811,5 @@ public class ColumnFamilyOptions extends RocksObject MemTableConfig memTableConfig_; TableFormatConfig tableFormatConfig_; AbstractComparator> comparator_; + AbstractCompactionFilter> compactionFilter_; } diff --git a/src.mk b/src.mk index f4a16672c..6fc690f32 100644 --- a/src.mk +++ b/src.mk @@ -246,6 +246,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 \ From 91bf1b80ef7d3a4f719e3f815c2d8c202855ebee Mon Sep 17 00:00:00 2001 From: Adam Retter Date: Tue, 14 Jul 2015 17:48:16 +0100 Subject: [PATCH 3/3] Java facility to use the RemoveEmptyValueCompactionFilter --- java/Makefile | 1 + .../remove_emptyvalue_compactionfilterjni.cc | 27 +++++++++++++++++++ .../RemoveEmptyValueCompactionFilter.java | 18 +++++++++++++ src.mk | 1 + 4 files changed, 47 insertions(+) create mode 100644 java/rocksjni/remove_emptyvalue_compactionfilterjni.cc create mode 100644 java/src/main/java/org/rocksdb/RemoveEmptyValueCompactionFilter.java diff --git a/java/Makefile b/java/Makefile index fcd441ca4..dd678f5c3 100644 --- a/java/Makefile +++ b/java/Makefile @@ -24,6 +24,7 @@ NATIVE_JAVA_CLASSES = org.rocksdb.AbstractCompactionFilter\ 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/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/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 6fc690f32..6d60743b6 100644 --- a/src.mk +++ b/src.mk @@ -257,6 +257,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 \