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