sst_partitioner.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. #include "rocksdb/sst_partitioner.h"
  7. #include <algorithm>
  8. #include "rocksdb/utilities/customizable_util.h"
  9. #include "rocksdb/utilities/object_registry.h"
  10. #include "rocksdb/utilities/options_type.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. static std::unordered_map<std::string, OptionTypeInfo>
  13. sst_fixed_prefix_type_info = {
  14. {"length",
  15. {0, OptionType::kSizeT, OptionVerificationType::kNormal,
  16. OptionTypeFlags::kNone}},
  17. };
  18. SstPartitionerFixedPrefixFactory::SstPartitionerFixedPrefixFactory(size_t len)
  19. : len_(len) {
  20. RegisterOptions("Length", &len_, &sst_fixed_prefix_type_info);
  21. }
  22. PartitionerResult SstPartitionerFixedPrefix::ShouldPartition(
  23. const PartitionerRequest& request) {
  24. Slice last_key_fixed(*request.prev_user_key);
  25. if (last_key_fixed.size() > len_) {
  26. last_key_fixed.size_ = len_;
  27. }
  28. Slice current_key_fixed(*request.current_user_key);
  29. if (current_key_fixed.size() > len_) {
  30. current_key_fixed.size_ = len_;
  31. }
  32. return last_key_fixed.compare(current_key_fixed) != 0 ? kRequired
  33. : kNotRequired;
  34. }
  35. bool SstPartitionerFixedPrefix::CanDoTrivialMove(
  36. const Slice& smallest_user_key, const Slice& largest_user_key) {
  37. return ShouldPartition(PartitionerRequest(smallest_user_key, largest_user_key,
  38. 0)) == kNotRequired;
  39. }
  40. std::unique_ptr<SstPartitioner>
  41. SstPartitionerFixedPrefixFactory::CreatePartitioner(
  42. const SstPartitioner::Context& /* context */) const {
  43. return std::unique_ptr<SstPartitioner>(new SstPartitionerFixedPrefix(len_));
  44. }
  45. std::shared_ptr<SstPartitionerFactory> NewSstPartitionerFixedPrefixFactory(
  46. size_t prefix_len) {
  47. return std::make_shared<SstPartitionerFixedPrefixFactory>(prefix_len);
  48. }
  49. namespace {
  50. static int RegisterSstPartitionerFactories(ObjectLibrary& library,
  51. const std::string& /*arg*/) {
  52. library.AddFactory<SstPartitionerFactory>(
  53. SstPartitionerFixedPrefixFactory::kClassName(),
  54. [](const std::string& /*uri*/,
  55. std::unique_ptr<SstPartitionerFactory>* guard,
  56. std::string* /* errmsg */) {
  57. guard->reset(new SstPartitionerFixedPrefixFactory(0));
  58. return guard->get();
  59. });
  60. return 1;
  61. }
  62. } // namespace
  63. Status SstPartitionerFactory::CreateFromString(
  64. const ConfigOptions& options, const std::string& value,
  65. std::shared_ptr<SstPartitionerFactory>* result) {
  66. static std::once_flag once;
  67. std::call_once(once, [&]() {
  68. RegisterSstPartitionerFactories(*(ObjectLibrary::Default().get()), "");
  69. });
  70. return LoadSharedObject<SstPartitionerFactory>(options, value, result);
  71. }
  72. } // namespace ROCKSDB_NAMESPACE