Coverage Report

Created: 2025-07-23 07:17

/src/rocksdb/db/compaction/sst_partitioner.cc
Line
Count
Source (jump to first uncovered line)
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
7
#include "rocksdb/sst_partitioner.h"
8
9
#include <algorithm>
10
11
#include "rocksdb/utilities/customizable_util.h"
12
#include "rocksdb/utilities/object_registry.h"
13
#include "rocksdb/utilities/options_type.h"
14
15
namespace ROCKSDB_NAMESPACE {
16
static std::unordered_map<std::string, OptionTypeInfo>
17
    sst_fixed_prefix_type_info = {
18
        {"length",
19
         {0, OptionType::kSizeT, OptionVerificationType::kNormal,
20
          OptionTypeFlags::kNone}},
21
};
22
23
SstPartitionerFixedPrefixFactory::SstPartitionerFixedPrefixFactory(size_t len)
24
0
    : len_(len) {
25
0
  RegisterOptions("Length", &len_, &sst_fixed_prefix_type_info);
26
0
}
27
28
PartitionerResult SstPartitionerFixedPrefix::ShouldPartition(
29
0
    const PartitionerRequest& request) {
30
0
  Slice last_key_fixed(*request.prev_user_key);
31
0
  if (last_key_fixed.size() > len_) {
32
0
    last_key_fixed.size_ = len_;
33
0
  }
34
0
  Slice current_key_fixed(*request.current_user_key);
35
0
  if (current_key_fixed.size() > len_) {
36
0
    current_key_fixed.size_ = len_;
37
0
  }
38
0
  return last_key_fixed.compare(current_key_fixed) != 0 ? kRequired
39
0
                                                        : kNotRequired;
40
0
}
41
42
bool SstPartitionerFixedPrefix::CanDoTrivialMove(
43
0
    const Slice& smallest_user_key, const Slice& largest_user_key) {
44
0
  return ShouldPartition(PartitionerRequest(smallest_user_key, largest_user_key,
45
0
                                            0)) == kNotRequired;
46
0
}
47
48
std::unique_ptr<SstPartitioner>
49
SstPartitionerFixedPrefixFactory::CreatePartitioner(
50
0
    const SstPartitioner::Context& /* context */) const {
51
0
  return std::unique_ptr<SstPartitioner>(new SstPartitionerFixedPrefix(len_));
52
0
}
53
54
std::shared_ptr<SstPartitionerFactory> NewSstPartitionerFixedPrefixFactory(
55
0
    size_t prefix_len) {
56
0
  return std::make_shared<SstPartitionerFixedPrefixFactory>(prefix_len);
57
0
}
58
59
namespace {
60
static int RegisterSstPartitionerFactories(ObjectLibrary& library,
61
2
                                           const std::string& /*arg*/) {
62
2
  library.AddFactory<SstPartitionerFactory>(
63
2
      SstPartitionerFixedPrefixFactory::kClassName(),
64
2
      [](const std::string& /*uri*/,
65
2
         std::unique_ptr<SstPartitionerFactory>* guard,
66
2
         std::string* /* errmsg */) {
67
0
        guard->reset(new SstPartitionerFixedPrefixFactory(0));
68
0
        return guard->get();
69
0
      });
70
2
  return 1;
71
2
}
72
}  // namespace
73
74
Status SstPartitionerFactory::CreateFromString(
75
    const ConfigOptions& options, const std::string& value,
76
146k
    std::shared_ptr<SstPartitionerFactory>* result) {
77
146k
  static std::once_flag once;
78
146k
  std::call_once(once, [&]() {
79
2
    RegisterSstPartitionerFactories(*(ObjectLibrary::Default().get()), "");
80
2
  });
81
146k
  return LoadSharedObject<SstPartitionerFactory>(options, value, result);
82
146k
}
83
}  // namespace ROCKSDB_NAMESPACE