forked from speedb-io/speedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_pinning_policy.h
139 lines (117 loc) · 5.18 KB
/
table_pinning_policy.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (C) 2023 Speedb Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
//
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.
//
#pragma once
#include "rocksdb/customizable.h"
#include "rocksdb/status.h"
namespace ROCKSDB_NAMESPACE {
struct BlockBasedTableOptions;
struct ConfigOptions;
// Struct that contains information about the table being evaluated for pinning
struct TablePinningOptions {
TablePinningOptions() = default;
TablePinningOptions(int _level, bool _is_last_level_with_data,
size_t _file_size, size_t _max_file_size_for_l0_meta_pin)
: level(_level),
is_last_level_with_data(_is_last_level_with_data),
file_size(_file_size),
max_file_size_for_l0_meta_pin(_max_file_size_for_l0_meta_pin) {}
int level = -1;
bool is_last_level_with_data = false;
size_t file_size = 0;
size_t max_file_size_for_l0_meta_pin = 0;
};
// Struct containing information about an entry that has been pinned
struct PinnedEntry {
PinnedEntry() {}
PinnedEntry(int _level, uint8_t _type, size_t _size,
bool _is_last_level_with_data)
: level(_level),
type(_type),
size(_size),
is_last_level_with_data(_is_last_level_with_data) {}
int level = -1;
uint8_t type = 0;
size_t size = 0;
bool is_last_level_with_data = false;
};
// TablePinningPolicy provides a configurable way to determine when blocks
// should be pinned in memory for the block based tables.
//
// Exceptions MUST NOT propagate out of overridden functions into RocksDB,
// because RocksDB is not exception-safe. This could cause undefined behavior
// including data loss, unreported corruption, deadlocks, and more.
class TablePinningPolicy : public Customizable {
public:
static const uint8_t kTopLevel = 1;
static const uint8_t kPartition = 2;
static const uint8_t kIndex = 3;
static const uint8_t kFilter = 4;
static const uint8_t kDictionary = 5;
static const char* Type() { return "TablePinningPolicy"; }
// Creates/Returns a new TablePinningPolicy based in the input value
static Status CreateFromString(const ConfigOptions& config_options,
const std::string& value,
std::shared_ptr<TablePinningPolicy>* policy);
virtual ~TablePinningPolicy() = default;
// Returns true if the block defined by type and size is a candidate for
// pinning This method indicates that pinning might be possible, but does not
// perform the pinning operation. Returns true if the data is a candidate for
// pinning and false otherwise
virtual bool MayPin(const TablePinningOptions& tpo, uint8_t type,
size_t size) const = 0;
// Attempts to pin the block in memory.
// If successful, pinned returns the pinned block
// Returns true and updates pinned on success and false if the data cannot be
// pinned
virtual bool PinData(const TablePinningOptions& tpo, uint8_t type,
size_t size, std::unique_ptr<PinnedEntry>* pinned) = 0;
// Releases and clears the pinned entry.
virtual void UnPinData(std::unique_ptr<PinnedEntry>&& pinned) = 0;
// Returns the amount of data currently pinned.
virtual size_t GetPinnedUsage() const = 0;
virtual std::string GetPrintableOptions() const { return ""; }
// Returns the info (e.g. statistics) associated with this policy.
virtual std::string ToString() const = 0;
};
class TablePinningPolicyWrapper : public TablePinningPolicy {
public:
explicit TablePinningPolicyWrapper(
const std::shared_ptr<TablePinningPolicy>& t)
: target_(t) {}
bool MayPin(const TablePinningOptions& tpo, uint8_t type,
size_t size) const override {
return target_->MayPin(tpo, type, size);
}
bool PinData(const TablePinningOptions& tpo, uint8_t type, size_t size,
std::unique_ptr<PinnedEntry>* pinned) override {
return target_->PinData(tpo, type, size, pinned);
}
void UnPinData(std::unique_ptr<PinnedEntry>&& pinned) override {
target_->UnPinData(std::move(pinned));
}
size_t GetPinnedUsage() const override { return target_->GetPinnedUsage(); }
protected:
std::shared_ptr<TablePinningPolicy> target_;
};
TablePinningPolicy* NewDefaultPinningPolicy(const BlockBasedTableOptions& bbto);
} // namespace ROCKSDB_NAMESPACE