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).
12 #include "rocksdb/rocksdb_namespace.h"
13 #include "rocksdb/slice.h"
15 namespace ROCKSDB_NAMESPACE
{
19 enum PartitionerResult
: char {
20 // Partitioner does not require to create new file
22 // Partitioner is requesting forcefully to create new file
24 // Additional constants can be added
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(¤t_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
;
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.
44 class SstPartitioner
{
46 virtual ~SstPartitioner() {}
48 // Return the name of this partitioner.
49 virtual const char* Name() const = 0;
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;
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;
64 // Context information of a compaction run
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
73 // Smallest key for compaction
74 Slice smallest_user_key
;
75 // Largest key for compaction
76 Slice largest_user_key
;
80 class SstPartitionerFactory
{
82 virtual ~SstPartitionerFactory() {}
84 virtual std::unique_ptr
<SstPartitioner
> CreatePartitioner(
85 const SstPartitioner::Context
& context
) const = 0;
87 // Returns a name that identifies this partitioner factory.
88 virtual const char* Name() const = 0;
92 * Fixed key prefix partitioner. It splits the output SST files when prefix
93 * defined by size changes.
95 class SstPartitionerFixedPrefix
: public SstPartitioner
{
97 explicit SstPartitionerFixedPrefix(size_t len
) : len_(len
) {}
99 virtual ~SstPartitionerFixedPrefix() override
{}
101 const char* Name() const override
{ return "SstPartitionerFixedPrefix"; }
103 PartitionerResult
ShouldPartition(const PartitionerRequest
& request
) override
;
105 bool CanDoTrivialMove(const Slice
& smallest_user_key
,
106 const Slice
& largest_user_key
) override
;
113 * Factory for fixed prefix partitioner.
115 class SstPartitionerFixedPrefixFactory
: public SstPartitionerFactory
{
117 explicit SstPartitionerFixedPrefixFactory(size_t len
) : len_(len
) {}
119 virtual ~SstPartitionerFixedPrefixFactory() {}
121 const char* Name() const override
{
122 return "SstPartitionerFixedPrefixFactory";
125 std::unique_ptr
<SstPartitioner
> CreatePartitioner(
126 const SstPartitioner::Context
& /* context */) const override
;
132 extern std::shared_ptr
<SstPartitionerFactory
>
133 NewSstPartitionerFixedPrefixFactory(size_t prefix_len
);
135 } // namespace ROCKSDB_NAMESPACE