]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/include/rocksdb/sst_partitioner.h
import quincy beta 17.1.0
[ceph.git] / ceph / src / rocksdb / include / rocksdb / sst_partitioner.h
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 #pragma once
8
9 #include <memory>
10 #include <string>
11
12 #include "rocksdb/rocksdb_namespace.h"
13 #include "rocksdb/slice.h"
14
15 namespace ROCKSDB_NAMESPACE {
16
17 class Slice;
18
19 enum PartitionerResult : char {
20 // Partitioner does not require to create new file
21 kNotRequired = 0x0,
22 // Partitioner is requesting forcefully to create new file
23 kRequired = 0x1
24 // Additional constants can be added
25 };
26
27 struct PartitionerRequest {
28 PartitionerRequest(const Slice& prev_user_key_,
29 const Slice& current_user_key_,
30 uint64_t current_output_file_size_)
31 : prev_user_key(&prev_user_key_),
32 current_user_key(&current_user_key_),
33 current_output_file_size(current_output_file_size_) {}
34 const Slice* prev_user_key;
35 const Slice* current_user_key;
36 uint64_t current_output_file_size;
37 };
38
39 /*
40 * A SstPartitioner is a generic pluggable way of defining the partition
41 * of SST files. Compaction job will split the SST files on partition boundary
42 * to lower the write amplification during SST file promote to higher level.
43 */
44 class SstPartitioner {
45 public:
46 virtual ~SstPartitioner() {}
47
48 // Return the name of this partitioner.
49 virtual const char* Name() const = 0;
50
51 // It is called for all keys in compaction. When partitioner want to create
52 // new SST file it needs to return true. It means compaction job will finish
53 // current SST file where last key is "prev_user_key" parameter and start new
54 // SST file where first key is "current_user_key". Returns decission if
55 // partition boundary was detected and compaction should create new file.
56 virtual PartitionerResult ShouldPartition(
57 const PartitionerRequest& request) = 0;
58
59 // Called with smallest and largest keys in SST file when compation try to do
60 // trivial move. Returns true is partitioner allows to do trivial move.
61 virtual bool CanDoTrivialMove(const Slice& smallest_user_key,
62 const Slice& largest_user_key) = 0;
63
64 // Context information of a compaction run
65 struct Context {
66 // Does this compaction run include all data files
67 bool is_full_compaction;
68 // Is this compaction requested by the client (true),
69 // or is it occurring as an automatic compaction process
70 bool is_manual_compaction;
71 // Output level for this compaction
72 int output_level;
73 // Smallest key for compaction
74 Slice smallest_user_key;
75 // Largest key for compaction
76 Slice largest_user_key;
77 };
78 };
79
80 class SstPartitionerFactory {
81 public:
82 virtual ~SstPartitionerFactory() {}
83
84 virtual std::unique_ptr<SstPartitioner> CreatePartitioner(
85 const SstPartitioner::Context& context) const = 0;
86
87 // Returns a name that identifies this partitioner factory.
88 virtual const char* Name() const = 0;
89 };
90
91 /*
92 * Fixed key prefix partitioner. It splits the output SST files when prefix
93 * defined by size changes.
94 */
95 class SstPartitionerFixedPrefix : public SstPartitioner {
96 public:
97 explicit SstPartitionerFixedPrefix(size_t len) : len_(len) {}
98
99 virtual ~SstPartitionerFixedPrefix() override {}
100
101 const char* Name() const override { return "SstPartitionerFixedPrefix"; }
102
103 PartitionerResult ShouldPartition(const PartitionerRequest& request) override;
104
105 bool CanDoTrivialMove(const Slice& smallest_user_key,
106 const Slice& largest_user_key) override;
107
108 private:
109 size_t len_;
110 };
111
112 /*
113 * Factory for fixed prefix partitioner.
114 */
115 class SstPartitionerFixedPrefixFactory : public SstPartitionerFactory {
116 public:
117 explicit SstPartitionerFixedPrefixFactory(size_t len) : len_(len) {}
118
119 virtual ~SstPartitionerFixedPrefixFactory() {}
120
121 const char* Name() const override {
122 return "SstPartitionerFixedPrefixFactory";
123 }
124
125 std::unique_ptr<SstPartitioner> CreatePartitioner(
126 const SstPartitioner::Context& /* context */) const override;
127
128 private:
129 size_t len_;
130 };
131
132 extern std::shared_ptr<SstPartitionerFactory>
133 NewSstPartitionerFixedPrefixFactory(size_t prefix_len);
134
135 } // namespace ROCKSDB_NAMESPACE