diff options
Diffstat (limited to '')
4 files changed, 151 insertions, 0 deletions
diff --git a/src/rocksdb/utilities/compaction_filters.cc b/src/rocksdb/utilities/compaction_filters.cc new file mode 100644 index 000000000..8763901c3 --- /dev/null +++ b/src/rocksdb/utilities/compaction_filters.cc @@ -0,0 +1,56 @@ +// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. See the AUTHORS file for names of contributors. + +#include <memory> + +#include "rocksdb/compaction_filter.h" +#include "rocksdb/options.h" +#include "rocksdb/utilities/customizable_util.h" +#include "rocksdb/utilities/options_type.h" +#include "utilities/compaction_filters/layered_compaction_filter_base.h" +#include "utilities/compaction_filters/remove_emptyvalue_compactionfilter.h" + +namespace ROCKSDB_NAMESPACE { +#ifndef ROCKSDB_LITE +static int RegisterBuiltinCompactionFilters(ObjectLibrary& library, + const std::string& /*arg*/) { + library.AddFactory<CompactionFilter>( + RemoveEmptyValueCompactionFilter::kClassName(), + [](const std::string& /*uri*/, + std::unique_ptr<CompactionFilter>* /*guard*/, + std::string* /*errmsg*/) { + return new RemoveEmptyValueCompactionFilter(); + }); + return 1; +} +#endif // ROCKSDB_LITE +Status CompactionFilter::CreateFromString(const ConfigOptions& config_options, + const std::string& value, + const CompactionFilter** result) { +#ifndef ROCKSDB_LITE + static std::once_flag once; + std::call_once(once, [&]() { + RegisterBuiltinCompactionFilters(*(ObjectLibrary::Default().get()), ""); + }); +#endif // ROCKSDB_LITE + CompactionFilter* filter = const_cast<CompactionFilter*>(*result); + Status status = LoadStaticObject<CompactionFilter>(config_options, value, + nullptr, &filter); + if (status.ok()) { + *result = const_cast<CompactionFilter*>(filter); + } + return status; +} + +Status CompactionFilterFactory::CreateFromString( + const ConfigOptions& config_options, const std::string& value, + std::shared_ptr<CompactionFilterFactory>* result) { + // Currently there are no builtin CompactionFilterFactories. + // If any are introduced, they need to be registered here. + Status status = LoadSharedObject<CompactionFilterFactory>( + config_options, value, nullptr, result); + return status; +} +} // namespace ROCKSDB_NAMESPACE diff --git a/src/rocksdb/utilities/compaction_filters/layered_compaction_filter_base.h b/src/rocksdb/utilities/compaction_filters/layered_compaction_filter_base.h new file mode 100644 index 000000000..803fa94ae --- /dev/null +++ b/src/rocksdb/utilities/compaction_filters/layered_compaction_filter_base.h @@ -0,0 +1,41 @@ +// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. See the AUTHORS file for names of contributors. + +#pragma once +#include <memory> + +#include "rocksdb/compaction_filter.h" + +namespace ROCKSDB_NAMESPACE { + +// Abstract base class for building layered compaction filter on top of +// user compaction filter. +// See BlobIndexCompactionFilter or TtlCompactionFilter for a basic usage. +class LayeredCompactionFilterBase : public CompactionFilter { + public: + LayeredCompactionFilterBase( + const CompactionFilter* _user_comp_filter, + std::unique_ptr<const CompactionFilter> _user_comp_filter_from_factory) + : user_comp_filter_(_user_comp_filter), + user_comp_filter_from_factory_( + std::move(_user_comp_filter_from_factory)) { + if (!user_comp_filter_) { + user_comp_filter_ = user_comp_filter_from_factory_.get(); + } + } + + // Return a pointer to user compaction filter + const CompactionFilter* user_comp_filter() const { return user_comp_filter_; } + + const Customizable* Inner() const override { return user_comp_filter_; } + + protected: + const CompactionFilter* user_comp_filter_; + + private: + std::unique_ptr<const CompactionFilter> user_comp_filter_from_factory_; +}; + +} // namespace ROCKSDB_NAMESPACE diff --git a/src/rocksdb/utilities/compaction_filters/remove_emptyvalue_compactionfilter.cc b/src/rocksdb/utilities/compaction_filters/remove_emptyvalue_compactionfilter.cc new file mode 100644 index 000000000..b788dbf9b --- /dev/null +++ b/src/rocksdb/utilities/compaction_filters/remove_emptyvalue_compactionfilter.cc @@ -0,0 +1,26 @@ +// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. +// This source code is licensed under both the GPLv2 (found in the +// COPYING file in the root directory) and Apache 2.0 License +// (found in the LICENSE.Apache file in the root directory). + +#ifndef ROCKSDB_LITE + +#include "utilities/compaction_filters/remove_emptyvalue_compactionfilter.h" + +#include <string> + +#include "rocksdb/slice.h" + +namespace ROCKSDB_NAMESPACE { + +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_NAMESPACE +#endif // !ROCKSDB_LITE diff --git a/src/rocksdb/utilities/compaction_filters/remove_emptyvalue_compactionfilter.h b/src/rocksdb/utilities/compaction_filters/remove_emptyvalue_compactionfilter.h new file mode 100644 index 000000000..864ad15ff --- /dev/null +++ b/src/rocksdb/utilities/compaction_filters/remove_emptyvalue_compactionfilter.h @@ -0,0 +1,28 @@ +// Copyright (c) 2011-present, Facebook, Inc. All rights reserved. +// This source code is licensed under both the GPLv2 (found in the +// COPYING file in the root directory) and Apache 2.0 License +// (found in the LICENSE.Apache file in the root directory). + +#ifndef ROCKSDB_LITE + +#pragma once + +#include <string> + +#include "rocksdb/compaction_filter.h" +#include "rocksdb/slice.h" + +namespace ROCKSDB_NAMESPACE { + +class RemoveEmptyValueCompactionFilter : public CompactionFilter { + public: + static const char* kClassName() { return "RemoveEmptyValueCompactionFilter"; } + + const char* Name() const override { return kClassName(); } + + bool Filter(int level, const Slice& key, const Slice& existing_value, + std::string* new_value, bool* value_changed) const override; +}; + +} // namespace ROCKSDB_NAMESPACE +#endif // !ROCKSDB_LITE |