@nxtedition/rocksdb 8.0.0 → 8.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BUILDING.md +2 -2
- package/binding.cc +2 -7
- package/deps/rocksdb/rocksdb/CMakeLists.txt +10 -9
- package/deps/rocksdb/rocksdb/Makefile +2 -2
- package/deps/rocksdb/rocksdb/TARGETS +4 -2
- package/deps/rocksdb/rocksdb/cache/cache_bench_tool.cc +0 -5
- package/deps/rocksdb/rocksdb/cache/cache_test.cc +8 -29
- package/deps/rocksdb/rocksdb/cache/clock_cache.cc +146 -0
- package/deps/rocksdb/rocksdb/cache/clock_cache.h +13 -1
- package/deps/rocksdb/rocksdb/cache/lru_cache_test.cc +57 -146
- package/deps/rocksdb/rocksdb/cache/secondary_cache.cc +32 -0
- package/deps/rocksdb/rocksdb/db/blob/blob_counting_iterator.h +11 -0
- package/deps/rocksdb/rocksdb/db/column_family.cc +11 -9
- package/deps/rocksdb/rocksdb/db/column_family.h +20 -0
- package/deps/rocksdb/rocksdb/db/compaction/clipping_iterator.h +5 -0
- package/deps/rocksdb/rocksdb/db/compaction/compaction.cc +13 -33
- package/deps/rocksdb/rocksdb/db/compaction/compaction.h +5 -0
- package/deps/rocksdb/rocksdb/db/compaction/compaction_iterator.cc +27 -8
- package/deps/rocksdb/rocksdb/db/compaction/compaction_iterator.h +17 -1
- package/deps/rocksdb/rocksdb/db/compaction/compaction_job.cc +2 -1
- package/deps/rocksdb/rocksdb/db/compaction/compaction_job.h +4 -2
- package/deps/rocksdb/rocksdb/db/compaction/compaction_job_test.cc +8 -6
- package/deps/rocksdb/rocksdb/db/compaction/compaction_outputs.cc +65 -7
- package/deps/rocksdb/rocksdb/db/compaction/compaction_outputs.h +5 -0
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker.cc +10 -32
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker.h +28 -47
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_fifo.cc +28 -22
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_fifo.h +8 -14
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_level.cc +8 -8
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_level.h +5 -4
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_test.cc +170 -140
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_universal.cc +5 -1
- package/deps/rocksdb/rocksdb/db/compaction/compaction_picker_universal.h +5 -4
- package/deps/rocksdb/rocksdb/db/compaction/compaction_service_job.cc +8 -2
- package/deps/rocksdb/rocksdb/db/compaction/subcompaction_state.h +8 -0
- package/deps/rocksdb/rocksdb/db/compaction/tiered_compaction_test.cc +266 -138
- package/deps/rocksdb/rocksdb/db/corruption_test.cc +86 -1
- package/deps/rocksdb/rocksdb/db/db_basic_test.cc +72 -5
- package/deps/rocksdb/rocksdb/db/db_block_cache_test.cc +119 -10
- package/deps/rocksdb/rocksdb/db/db_compaction_test.cc +585 -264
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl.cc +46 -18
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl.h +5 -1
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_compaction_flush.cc +6 -15
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_debug.cc +1 -1
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_experimental.cc +1 -1
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_files.cc +3 -0
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_open.cc +8 -8
- package/deps/rocksdb/rocksdb/db/db_impl/db_impl_write.cc +10 -0
- package/deps/rocksdb/rocksdb/db/db_iter.cc +57 -36
- package/deps/rocksdb/rocksdb/db/db_iter.h +2 -1
- package/deps/rocksdb/rocksdb/db/db_range_del_test.cc +250 -2
- package/deps/rocksdb/rocksdb/db/db_test.cc +3 -0
- package/deps/rocksdb/rocksdb/db/db_test2.cc +307 -8
- package/deps/rocksdb/rocksdb/db/db_wal_test.cc +129 -0
- package/deps/rocksdb/rocksdb/db/db_with_timestamp_compaction_test.cc +21 -0
- package/deps/rocksdb/rocksdb/db/dbformat.cc +25 -0
- package/deps/rocksdb/rocksdb/db/dbformat.h +2 -0
- package/deps/rocksdb/rocksdb/db/experimental.cc +1 -1
- package/deps/rocksdb/rocksdb/db/external_sst_file_ingestion_job.cc +5 -2
- package/deps/rocksdb/rocksdb/db/flush_job.cc +5 -2
- package/deps/rocksdb/rocksdb/db/history_trimming_iterator.h +4 -0
- package/deps/rocksdb/rocksdb/db/import_column_family_job.cc +56 -53
- package/deps/rocksdb/rocksdb/db/import_column_family_test.cc +3 -4
- package/deps/rocksdb/rocksdb/db/memtable.cc +55 -9
- package/deps/rocksdb/rocksdb/db/merge_helper.cc +76 -102
- package/deps/rocksdb/rocksdb/db/merge_helper.h +2 -11
- package/deps/rocksdb/rocksdb/db/periodic_task_scheduler_test.cc +10 -10
- package/deps/rocksdb/rocksdb/db/repair.cc +64 -22
- package/deps/rocksdb/rocksdb/db/repair_test.cc +54 -0
- package/deps/rocksdb/rocksdb/db/seqno_time_test.cc +26 -26
- package/deps/rocksdb/rocksdb/db/table_cache.cc +2 -0
- package/deps/rocksdb/rocksdb/db/table_properties_collector.h +3 -1
- package/deps/rocksdb/rocksdb/db/version_builder.cc +90 -43
- package/deps/rocksdb/rocksdb/db/version_builder.h +20 -0
- package/deps/rocksdb/rocksdb/db/version_builder_test.cc +190 -67
- package/deps/rocksdb/rocksdb/db/version_edit.cc +15 -1
- package/deps/rocksdb/rocksdb/db/version_edit.h +16 -4
- package/deps/rocksdb/rocksdb/db/version_edit_handler.cc +41 -11
- package/deps/rocksdb/rocksdb/db/version_edit_handler.h +27 -12
- package/deps/rocksdb/rocksdb/db/version_edit_test.cc +18 -16
- package/deps/rocksdb/rocksdb/db/version_set.cc +219 -38
- package/deps/rocksdb/rocksdb/db/version_set.h +34 -4
- package/deps/rocksdb/rocksdb/db/version_set_test.cc +45 -25
- package/deps/rocksdb/rocksdb/db/wide/db_wide_basic_test.cc +122 -61
- package/deps/rocksdb/rocksdb/db/write_thread.cc +5 -2
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_common.h +0 -1
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_gflags.cc +0 -4
- package/deps/rocksdb/rocksdb/db_stress_tool/db_stress_test_base.cc +12 -17
- package/deps/rocksdb/rocksdb/db_stress_tool/no_batched_ops_stress.cc +6 -4
- package/deps/rocksdb/rocksdb/file/file_prefetch_buffer.cc +1 -1
- package/deps/rocksdb/rocksdb/file/file_prefetch_buffer.h +1 -0
- package/deps/rocksdb/rocksdb/file/prefetch_test.cc +0 -48
- package/deps/rocksdb/rocksdb/file/random_access_file_reader.cc +8 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/cache.h +196 -171
- package/deps/rocksdb/rocksdb/include/rocksdb/db.h +6 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/metadata.h +9 -3
- package/deps/rocksdb/rocksdb/include/rocksdb/options.h +25 -18
- package/deps/rocksdb/rocksdb/include/rocksdb/secondary_cache.h +27 -5
- package/deps/rocksdb/rocksdb/include/rocksdb/statistics.h +5 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/status.h +3 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/table.h +3 -0
- package/deps/rocksdb/rocksdb/include/rocksdb/version.h +1 -1
- package/deps/rocksdb/rocksdb/logging/logging.h +13 -19
- package/deps/rocksdb/rocksdb/memory/arena.cc +4 -3
- package/deps/rocksdb/rocksdb/memory/arena_test.cc +30 -0
- package/deps/rocksdb/rocksdb/monitoring/statistics.cc +3 -1
- package/deps/rocksdb/rocksdb/monitoring/stats_history_test.cc +26 -26
- package/deps/rocksdb/rocksdb/src.mk +2 -1
- package/deps/rocksdb/rocksdb/table/adaptive/adaptive_table_factory.cc +3 -2
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_builder.cc +2 -10
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_reader.cc +12 -29
- package/deps/rocksdb/rocksdb/table/block_based/block_based_table_reader_test.cc +1 -1
- package/deps/rocksdb/rocksdb/table/block_based/block_like_traits.h +0 -39
- package/deps/rocksdb/rocksdb/table/block_based/filter_block_reader_common.cc +0 -1
- package/deps/rocksdb/rocksdb/table/block_fetcher_test.cc +3 -3
- package/deps/rocksdb/rocksdb/table/compaction_merging_iterator.cc +142 -0
- package/deps/rocksdb/rocksdb/table/compaction_merging_iterator.h +241 -0
- package/deps/rocksdb/rocksdb/table/format.cc +24 -20
- package/deps/rocksdb/rocksdb/table/format.h +5 -2
- package/deps/rocksdb/rocksdb/table/get_context.cc +52 -11
- package/deps/rocksdb/rocksdb/table/merging_iterator.cc +97 -115
- package/deps/rocksdb/rocksdb/table/merging_iterator.h +82 -1
- package/deps/rocksdb/rocksdb/table/meta_blocks.cc +2 -2
- package/deps/rocksdb/rocksdb/table/sst_file_dumper.cc +1 -1
- package/deps/rocksdb/rocksdb/table/table_test.cc +7 -6
- package/deps/rocksdb/rocksdb/test_util/testutil.h +10 -0
- package/deps/rocksdb/rocksdb/tools/db_bench_tool.cc +0 -6
- package/deps/rocksdb/rocksdb/trace_replay/block_cache_tracer.h +2 -2
- package/deps/rocksdb/rocksdb/util/bloom_test.cc +1 -1
- package/deps/rocksdb/rocksdb/util/crc32c.cc +1 -1
- package/deps/rocksdb/rocksdb/util/status.cc +7 -0
- package/deps/rocksdb/rocksdb/utilities/backup/backup_engine.cc +5 -0
- package/deps/rocksdb/rocksdb/utilities/backup/backup_engine_test.cc +4 -0
- package/deps/rocksdb/rocksdb/utilities/cache_dump_load_impl.cc +7 -67
- package/deps/rocksdb/rocksdb/utilities/cache_dump_load_impl.h +1 -3
- package/deps/rocksdb/rocksdb/utilities/checkpoint/checkpoint_impl.cc +1 -0
- package/deps/rocksdb/rocksdb/utilities/transactions/transaction_test.cc +59 -0
- package/deps/rocksdb/rocksdb.gyp +2 -1
- package/package.json +1 -1
- package/prebuilds/darwin-arm64/node.napi.node +0 -0
- package/prebuilds/linux-x64/node.napi.node +0 -0
- package/deps/rocksdb/rocksdb/cache/fast_lru_cache.cc +0 -580
- package/deps/rocksdb/rocksdb/cache/fast_lru_cache.h +0 -476
- package/max_rev_operator.h +0 -100
|
@@ -1,476 +0,0 @@
|
|
|
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
|
-
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
7
|
-
// Use of this source code is governed by a BSD-style license that can be
|
|
8
|
-
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
9
|
-
#pragma once
|
|
10
|
-
|
|
11
|
-
#include <array>
|
|
12
|
-
#include <memory>
|
|
13
|
-
#include <string>
|
|
14
|
-
|
|
15
|
-
#include "cache/cache_key.h"
|
|
16
|
-
#include "cache/sharded_cache.h"
|
|
17
|
-
#include "port/lang.h"
|
|
18
|
-
#include "port/malloc.h"
|
|
19
|
-
#include "port/port.h"
|
|
20
|
-
#include "rocksdb/secondary_cache.h"
|
|
21
|
-
#include "util/autovector.h"
|
|
22
|
-
#include "util/distributed_mutex.h"
|
|
23
|
-
|
|
24
|
-
namespace ROCKSDB_NAMESPACE {
|
|
25
|
-
|
|
26
|
-
namespace fast_lru_cache {
|
|
27
|
-
|
|
28
|
-
// Forward declaration of friend class.
|
|
29
|
-
class FastLRUCacheTest;
|
|
30
|
-
|
|
31
|
-
// LRU cache implementation using an open-address hash table.
|
|
32
|
-
//
|
|
33
|
-
// Every slot in the hash table is an LRUHandle. Because handles can be
|
|
34
|
-
// referenced externally, we can't discard them immediately once they are
|
|
35
|
-
// deleted (via a delete or an LRU eviction) or replaced by a new version
|
|
36
|
-
// (via an insert of the same key). The state of an element is defined by
|
|
37
|
-
// the following two properties:
|
|
38
|
-
// (R) Referenced: An element can be referenced externally (refs > 0), or not.
|
|
39
|
-
// Importantly, an element can be evicted if and only if it's not
|
|
40
|
-
// referenced. In particular, when an element becomes referenced, it's
|
|
41
|
-
// temporarily taken out of the LRU list until all references to it
|
|
42
|
-
// are dropped.
|
|
43
|
-
// (V) Visible: An element can visible for lookups (IS_VISIBLE set), or not.
|
|
44
|
-
// Initially, every element is visible. An element that is not visible is
|
|
45
|
-
// called a ghost.
|
|
46
|
-
// These properties induce 4 different states, with transitions defined as
|
|
47
|
-
// follows:
|
|
48
|
-
// - V --> not V: When a visible element is deleted or replaced by a new
|
|
49
|
-
// version.
|
|
50
|
-
// - Not V --> V: This cannot happen. A ghost remains in that state until it's
|
|
51
|
-
// not referenced any more, at which point it's ready to be removed from the
|
|
52
|
-
// hash table. (A ghost simply waits to transition to the afterlife---it will
|
|
53
|
-
// never be visible again.)
|
|
54
|
-
// - R --> not R: When all references to an element are dropped.
|
|
55
|
-
// - Not R --> R: When an unreferenced element becomes referenced. This can only
|
|
56
|
-
// happen if the element is V, since references to an element can only be
|
|
57
|
-
// created when it's visible.
|
|
58
|
-
//
|
|
59
|
-
// Internally, the cache uses an open-addressed hash table to index the handles.
|
|
60
|
-
// We use tombstone counters to keep track of displacements.
|
|
61
|
-
// Because of the tombstones and the two possible visibility states of an
|
|
62
|
-
// element, the table slots can be in 4 different states:
|
|
63
|
-
// 1. Visible element (IS_ELEMENT set and IS_VISIBLE set): The slot contains a
|
|
64
|
-
// key-value element.
|
|
65
|
-
// 2. Ghost element (IS_ELEMENT set and IS_VISIBLE unset): The slot contains an
|
|
66
|
-
// element that has been removed, but it's still referenced. It's invisible
|
|
67
|
-
// to lookups.
|
|
68
|
-
// 3. Tombstone (IS_ELEMENT unset and displacements > 0): The slot contains a
|
|
69
|
-
// tombstone.
|
|
70
|
-
// 4. Empty (IS_ELEMENT unset and displacements == 0): The slot is unused.
|
|
71
|
-
// A slot that is an element can further have IS_VISIBLE set or not.
|
|
72
|
-
// When a ghost is removed from the table, it can either transition to being a
|
|
73
|
-
// tombstone or an empty slot, depending on the number of displacements of the
|
|
74
|
-
// slot. In any case, the slot becomes available. When a handle is inserted
|
|
75
|
-
// into that slot, it becomes a visible element again.
|
|
76
|
-
|
|
77
|
-
// The load factor p is a real number in (0, 1) such that at all
|
|
78
|
-
// times at most a fraction p of all slots, without counting tombstones,
|
|
79
|
-
// are occupied by elements. This means that the probability that a
|
|
80
|
-
// random probe hits an empty slot is at most p, and thus at most 1/p probes
|
|
81
|
-
// are required on average. For example, p = 70% implies that between 1 and 2
|
|
82
|
-
// probes are needed on average (bear in mind that this reasoning doesn't
|
|
83
|
-
// consider the effects of clustering over time).
|
|
84
|
-
// Because the size of the hash table is always rounded up to the next
|
|
85
|
-
// power of 2, p is really an upper bound on the actual load factor---the
|
|
86
|
-
// actual load factor is anywhere between p/2 and p. This is a bit wasteful,
|
|
87
|
-
// but bear in mind that slots only hold metadata, not actual values.
|
|
88
|
-
// Since space cost is dominated by the values (the LSM blocks),
|
|
89
|
-
// overprovisioning the table with metadata only increases the total cache space
|
|
90
|
-
// usage by a tiny fraction.
|
|
91
|
-
constexpr double kLoadFactor = 0.35;
|
|
92
|
-
|
|
93
|
-
// The user can exceed kLoadFactor if the sizes of the inserted values don't
|
|
94
|
-
// match estimated_value_size, or if strict_capacity_limit == false. To
|
|
95
|
-
// avoid performance to plunge, we set a strict upper bound on the load factor.
|
|
96
|
-
constexpr double kStrictLoadFactor = 0.7;
|
|
97
|
-
|
|
98
|
-
// Arbitrary seeds.
|
|
99
|
-
constexpr uint32_t kProbingSeed1 = 0xbc9f1d34;
|
|
100
|
-
constexpr uint32_t kProbingSeed2 = 0x7a2bb9d5;
|
|
101
|
-
|
|
102
|
-
// An experimental (under development!) alternative to LRUCache
|
|
103
|
-
|
|
104
|
-
struct LRUHandle {
|
|
105
|
-
void* value;
|
|
106
|
-
Cache::DeleterFn deleter;
|
|
107
|
-
LRUHandle* next;
|
|
108
|
-
LRUHandle* prev;
|
|
109
|
-
size_t total_charge; // TODO(opt): Only allow uint32_t?
|
|
110
|
-
// The hash of key(). Used for fast sharding and comparisons.
|
|
111
|
-
uint32_t hash;
|
|
112
|
-
// The number of external refs to this entry.
|
|
113
|
-
uint32_t refs;
|
|
114
|
-
|
|
115
|
-
enum Flags : uint8_t {
|
|
116
|
-
// Whether the handle is visible to Lookups.
|
|
117
|
-
IS_VISIBLE = (1 << 0),
|
|
118
|
-
// Whether the slot is in use by an element.
|
|
119
|
-
IS_ELEMENT = (1 << 1),
|
|
120
|
-
};
|
|
121
|
-
uint8_t flags;
|
|
122
|
-
|
|
123
|
-
// The number of elements that hash to this slot or a lower one,
|
|
124
|
-
// but wind up in a higher slot.
|
|
125
|
-
uint32_t displacements;
|
|
126
|
-
|
|
127
|
-
std::array<char, kCacheKeySize> key_data;
|
|
128
|
-
|
|
129
|
-
LRUHandle() {
|
|
130
|
-
value = nullptr;
|
|
131
|
-
deleter = nullptr;
|
|
132
|
-
next = nullptr;
|
|
133
|
-
prev = nullptr;
|
|
134
|
-
total_charge = 0;
|
|
135
|
-
hash = 0;
|
|
136
|
-
refs = 0;
|
|
137
|
-
flags = 0;
|
|
138
|
-
displacements = 0;
|
|
139
|
-
key_data.fill(0);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
Slice key() const { return Slice(key_data.data(), kCacheKeySize); }
|
|
143
|
-
|
|
144
|
-
// For HandleImpl concept
|
|
145
|
-
uint32_t GetHash() const { return hash; }
|
|
146
|
-
|
|
147
|
-
// Increase the reference count by 1.
|
|
148
|
-
void Ref() { refs++; }
|
|
149
|
-
|
|
150
|
-
// Just reduce the reference count by 1. Return true if it was last reference.
|
|
151
|
-
bool Unref() {
|
|
152
|
-
assert(refs > 0);
|
|
153
|
-
refs--;
|
|
154
|
-
return refs == 0;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// Return true if there are external refs, false otherwise.
|
|
158
|
-
bool HasRefs() const { return refs > 0; }
|
|
159
|
-
|
|
160
|
-
bool IsVisible() const { return flags & IS_VISIBLE; }
|
|
161
|
-
|
|
162
|
-
void SetIsVisible(bool is_visible) {
|
|
163
|
-
if (is_visible) {
|
|
164
|
-
flags |= IS_VISIBLE;
|
|
165
|
-
} else {
|
|
166
|
-
flags &= ~IS_VISIBLE;
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
bool IsElement() const { return flags & IS_ELEMENT; }
|
|
171
|
-
|
|
172
|
-
void SetIsElement(bool is_element) {
|
|
173
|
-
if (is_element) {
|
|
174
|
-
flags |= IS_ELEMENT;
|
|
175
|
-
} else {
|
|
176
|
-
flags &= ~IS_ELEMENT;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
void FreeData() {
|
|
181
|
-
assert(refs == 0);
|
|
182
|
-
if (deleter) {
|
|
183
|
-
(*deleter)(key(), value);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// Calculate the memory usage by metadata.
|
|
188
|
-
inline size_t CalcMetaCharge(
|
|
189
|
-
CacheMetadataChargePolicy metadata_charge_policy) const {
|
|
190
|
-
if (metadata_charge_policy != kFullChargeCacheMetadata) {
|
|
191
|
-
return 0;
|
|
192
|
-
} else {
|
|
193
|
-
// #ifdef ROCKSDB_MALLOC_USABLE_SIZE
|
|
194
|
-
// return malloc_usable_size(
|
|
195
|
-
// const_cast<void*>(static_cast<const void*>(this)));
|
|
196
|
-
// #else
|
|
197
|
-
// TODO(Guido) malloc_usable_size only works when we call it on
|
|
198
|
-
// a pointer allocated with malloc. Because our handles are all
|
|
199
|
-
// allocated in a single shot as an array, the user can't call
|
|
200
|
-
// CalcMetaCharge (or CalcTotalCharge or GetCharge) on a handle
|
|
201
|
-
// pointer returned by the cache. Moreover, malloc_usable_size
|
|
202
|
-
// expects a heap-allocated handle, but sometimes in our code we
|
|
203
|
-
// wish to pass a stack-allocated handle (this is only a performance
|
|
204
|
-
// concern).
|
|
205
|
-
// What is the right way to compute metadata charges with pre-allocated
|
|
206
|
-
// handles?
|
|
207
|
-
return sizeof(LRUHandle);
|
|
208
|
-
// #endif
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
inline void CalcTotalCharge(
|
|
213
|
-
size_t charge, CacheMetadataChargePolicy metadata_charge_policy) {
|
|
214
|
-
total_charge = charge + CalcMetaCharge(metadata_charge_policy);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
inline size_t GetCharge(
|
|
218
|
-
CacheMetadataChargePolicy metadata_charge_policy) const {
|
|
219
|
-
size_t meta_charge = CalcMetaCharge(metadata_charge_policy);
|
|
220
|
-
assert(total_charge >= meta_charge);
|
|
221
|
-
return total_charge - meta_charge;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
inline bool IsEmpty() {
|
|
225
|
-
return !this->IsElement() && this->displacements == 0;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
inline bool IsTombstone() {
|
|
229
|
-
return !this->IsElement() && this->displacements > 0;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
inline bool Matches(const Slice& some_key, uint32_t some_hash) {
|
|
233
|
-
return this->IsElement() && this->hash == some_hash &&
|
|
234
|
-
this->key() == some_key;
|
|
235
|
-
}
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
class LRUHandleTable {
|
|
239
|
-
public:
|
|
240
|
-
explicit LRUHandleTable(int hash_bits);
|
|
241
|
-
~LRUHandleTable();
|
|
242
|
-
|
|
243
|
-
// Returns a pointer to a visible element matching the key/hash, or
|
|
244
|
-
// nullptr if not present.
|
|
245
|
-
LRUHandle* Lookup(const Slice& key, uint32_t hash);
|
|
246
|
-
|
|
247
|
-
// Inserts a copy of h into the hash table.
|
|
248
|
-
// Returns a pointer to the inserted handle, or nullptr if no slot
|
|
249
|
-
// available was found. If an existing visible element matching the
|
|
250
|
-
// key/hash is already present in the hash table, the argument old
|
|
251
|
-
// is set to pointe to it; otherwise, it's set to nullptr.
|
|
252
|
-
LRUHandle* Insert(LRUHandle* h, LRUHandle** old);
|
|
253
|
-
|
|
254
|
-
// Removes h from the hash table. The handle must already be off
|
|
255
|
-
// the LRU list.
|
|
256
|
-
void Remove(LRUHandle* h);
|
|
257
|
-
|
|
258
|
-
// Turns a visible element h into a ghost (i.e., not visible).
|
|
259
|
-
void Exclude(LRUHandle* h);
|
|
260
|
-
|
|
261
|
-
// Assigns a copy of h to the given slot.
|
|
262
|
-
void Assign(int slot, LRUHandle* h);
|
|
263
|
-
|
|
264
|
-
template <typename T>
|
|
265
|
-
void ApplyToEntriesRange(T func, size_t index_begin, size_t index_end) {
|
|
266
|
-
for (size_t i = index_begin; i < index_end; i++) {
|
|
267
|
-
LRUHandle* h = &array_[i];
|
|
268
|
-
if (h->IsVisible()) {
|
|
269
|
-
func(h);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
uint32_t GetTableSize() const { return uint32_t{1} << length_bits_; }
|
|
275
|
-
|
|
276
|
-
int GetLengthBits() const { return length_bits_; }
|
|
277
|
-
|
|
278
|
-
uint32_t GetOccupancyLimit() const { return occupancy_limit_; }
|
|
279
|
-
|
|
280
|
-
uint32_t GetOccupancy() const { return occupancy_; }
|
|
281
|
-
|
|
282
|
-
// Returns x mod 2^{length_bits_}.
|
|
283
|
-
uint32_t ModTableSize(uint32_t x) { return x & length_bits_mask_; }
|
|
284
|
-
|
|
285
|
-
private:
|
|
286
|
-
int FindVisibleElement(const Slice& key, uint32_t hash, int& probe,
|
|
287
|
-
int displacement);
|
|
288
|
-
|
|
289
|
-
int FindAvailableSlot(const Slice& key, int& probe, int displacement);
|
|
290
|
-
|
|
291
|
-
int FindVisibleElementOrAvailableSlot(const Slice& key, uint32_t hash,
|
|
292
|
-
int& probe, int displacement);
|
|
293
|
-
|
|
294
|
-
// Returns the index of the first slot probed (hashing with
|
|
295
|
-
// the given key) with a handle e such that cond(e) is true.
|
|
296
|
-
// Otherwise, if no match is found, returns -1.
|
|
297
|
-
// For every handle e probed except the final slot, updates
|
|
298
|
-
// e->displacements += displacement.
|
|
299
|
-
// The argument probe is modified such that consecutive calls
|
|
300
|
-
// to FindSlot continue probing right after where the previous
|
|
301
|
-
// call left.
|
|
302
|
-
int FindSlot(const Slice& key, std::function<bool(LRUHandle*)> cond,
|
|
303
|
-
int& probe, int displacement);
|
|
304
|
-
|
|
305
|
-
// Number of hash bits used for table index.
|
|
306
|
-
// The size of the table is 1 << length_bits_.
|
|
307
|
-
int length_bits_;
|
|
308
|
-
|
|
309
|
-
const uint32_t length_bits_mask_;
|
|
310
|
-
|
|
311
|
-
// Number of elements in the table.
|
|
312
|
-
uint32_t occupancy_;
|
|
313
|
-
|
|
314
|
-
// Maximum number of elements the user can store in the table.
|
|
315
|
-
uint32_t occupancy_limit_;
|
|
316
|
-
|
|
317
|
-
std::unique_ptr<LRUHandle[]> array_;
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
// A single shard of sharded cache.
|
|
321
|
-
class ALIGN_AS(CACHE_LINE_SIZE) LRUCacheShard final : public CacheShardBase {
|
|
322
|
-
public:
|
|
323
|
-
LRUCacheShard(size_t capacity, size_t estimated_value_size,
|
|
324
|
-
bool strict_capacity_limit,
|
|
325
|
-
CacheMetadataChargePolicy metadata_charge_policy);
|
|
326
|
-
|
|
327
|
-
// For CacheShard concept
|
|
328
|
-
using HandleImpl = LRUHandle;
|
|
329
|
-
|
|
330
|
-
// Keep 32-bit hashing for now (FIXME: upgrade to 64-bit)
|
|
331
|
-
using HashVal = uint32_t;
|
|
332
|
-
using HashCref = uint32_t;
|
|
333
|
-
static inline HashVal ComputeHash(const Slice& key) {
|
|
334
|
-
return Lower32of64(GetSliceNPHash64(key));
|
|
335
|
-
}
|
|
336
|
-
static inline uint32_t HashPieceForSharding(HashCref hash) { return hash; }
|
|
337
|
-
|
|
338
|
-
// Separate from constructor so caller can easily make an array of LRUCache
|
|
339
|
-
// if current usage is more than new capacity, the function will attempt to
|
|
340
|
-
// free the needed space.
|
|
341
|
-
void SetCapacity(size_t capacity);
|
|
342
|
-
|
|
343
|
-
// Set the flag to reject insertion if cache if full.
|
|
344
|
-
void SetStrictCapacityLimit(bool strict_capacity_limit);
|
|
345
|
-
|
|
346
|
-
// Like Cache methods, but with an extra "hash" parameter.
|
|
347
|
-
// Insert an item into the hash table and, if handle is null, insert into
|
|
348
|
-
// the LRU list. Older items are evicted as necessary. If the cache is full
|
|
349
|
-
// and free_handle_on_fail is true, the item is deleted and handle is set to
|
|
350
|
-
// nullptr.
|
|
351
|
-
Status Insert(const Slice& key, uint32_t hash, void* value, size_t charge,
|
|
352
|
-
Cache::DeleterFn deleter, LRUHandle** handle,
|
|
353
|
-
Cache::Priority priority);
|
|
354
|
-
|
|
355
|
-
Status Insert(const Slice& key, uint32_t hash, void* value,
|
|
356
|
-
const Cache::CacheItemHelper* helper, size_t charge,
|
|
357
|
-
LRUHandle** handle, Cache::Priority priority) {
|
|
358
|
-
return Insert(key, hash, value, charge, helper->del_cb, handle, priority);
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
LRUHandle* Lookup(const Slice& key, uint32_t hash,
|
|
362
|
-
const Cache::CacheItemHelper* /*helper*/,
|
|
363
|
-
const Cache::CreateCallback& /*create_cb*/,
|
|
364
|
-
Cache::Priority /*priority*/, bool /*wait*/,
|
|
365
|
-
Statistics* /*stats*/) {
|
|
366
|
-
return Lookup(key, hash);
|
|
367
|
-
}
|
|
368
|
-
LRUHandle* Lookup(const Slice& key, uint32_t hash);
|
|
369
|
-
|
|
370
|
-
bool Release(LRUHandle* handle, bool /*useful*/, bool erase_if_last_ref) {
|
|
371
|
-
return Release(handle, erase_if_last_ref);
|
|
372
|
-
}
|
|
373
|
-
bool IsReady(LRUHandle* /*handle*/) { return true; }
|
|
374
|
-
void Wait(LRUHandle* /*handle*/) {}
|
|
375
|
-
|
|
376
|
-
bool Ref(LRUHandle* handle);
|
|
377
|
-
bool Release(LRUHandle* handle, bool erase_if_last_ref = false);
|
|
378
|
-
void Erase(const Slice& key, uint32_t hash);
|
|
379
|
-
|
|
380
|
-
size_t GetUsage() const;
|
|
381
|
-
size_t GetPinnedUsage() const;
|
|
382
|
-
size_t GetOccupancyCount() const;
|
|
383
|
-
size_t GetTableAddressCount() const;
|
|
384
|
-
|
|
385
|
-
void ApplyToSomeEntries(
|
|
386
|
-
const std::function<void(const Slice& key, void* value, size_t charge,
|
|
387
|
-
DeleterFn deleter)>& callback,
|
|
388
|
-
size_t average_entries_per_lock, size_t* state);
|
|
389
|
-
|
|
390
|
-
void EraseUnRefEntries();
|
|
391
|
-
|
|
392
|
-
private:
|
|
393
|
-
friend class LRUCache;
|
|
394
|
-
friend class FastLRUCacheTest;
|
|
395
|
-
|
|
396
|
-
void LRU_Remove(LRUHandle* e);
|
|
397
|
-
void LRU_Insert(LRUHandle* e);
|
|
398
|
-
|
|
399
|
-
// Free some space following strict LRU policy until enough space
|
|
400
|
-
// to hold (usage_ + charge) is freed or the LRU list is empty
|
|
401
|
-
// This function is not thread safe - it needs to be executed while
|
|
402
|
-
// holding the mutex_.
|
|
403
|
-
void EvictFromLRU(size_t charge, autovector<LRUHandle>* deleted);
|
|
404
|
-
|
|
405
|
-
// Returns the charge of a single handle.
|
|
406
|
-
static size_t CalcEstimatedHandleCharge(
|
|
407
|
-
size_t estimated_value_size,
|
|
408
|
-
CacheMetadataChargePolicy metadata_charge_policy);
|
|
409
|
-
|
|
410
|
-
// Returns the number of bits used to hash an element in the hash
|
|
411
|
-
// table.
|
|
412
|
-
static int CalcHashBits(size_t capacity, size_t estimated_value_size,
|
|
413
|
-
CacheMetadataChargePolicy metadata_charge_policy);
|
|
414
|
-
|
|
415
|
-
// Initialized before use.
|
|
416
|
-
size_t capacity_;
|
|
417
|
-
|
|
418
|
-
// Whether to reject insertion if cache reaches its full capacity.
|
|
419
|
-
bool strict_capacity_limit_;
|
|
420
|
-
|
|
421
|
-
// Dummy head of LRU list.
|
|
422
|
-
// lru.prev is newest entry, lru.next is oldest entry.
|
|
423
|
-
// LRU contains items which can be evicted, ie reference only by cache
|
|
424
|
-
LRUHandle lru_;
|
|
425
|
-
|
|
426
|
-
// Pointer to head of low-pri pool in LRU list.
|
|
427
|
-
LRUHandle* lru_low_pri_;
|
|
428
|
-
|
|
429
|
-
// ------------^^^^^^^^^^^^^-----------
|
|
430
|
-
// Not frequently modified data members
|
|
431
|
-
// ------------------------------------
|
|
432
|
-
//
|
|
433
|
-
// We separate data members that are updated frequently from the ones that
|
|
434
|
-
// are not frequently updated so that they don't share the same cache line
|
|
435
|
-
// which will lead into false cache sharing
|
|
436
|
-
//
|
|
437
|
-
// ------------------------------------
|
|
438
|
-
// Frequently modified data members
|
|
439
|
-
// ------------vvvvvvvvvvvvv-----------
|
|
440
|
-
LRUHandleTable table_;
|
|
441
|
-
|
|
442
|
-
// Memory size for entries residing in the cache.
|
|
443
|
-
size_t usage_;
|
|
444
|
-
|
|
445
|
-
// Memory size for entries residing only in the LRU list.
|
|
446
|
-
size_t lru_usage_;
|
|
447
|
-
|
|
448
|
-
// mutex_ protects the following state.
|
|
449
|
-
// We don't count mutex_ as the cache's internal state so semantically we
|
|
450
|
-
// don't mind mutex_ invoking the non-const actions.
|
|
451
|
-
mutable DMutex mutex_;
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
class LRUCache
|
|
455
|
-
#ifdef NDEBUG
|
|
456
|
-
final
|
|
457
|
-
#endif
|
|
458
|
-
: public ShardedCache<LRUCacheShard> {
|
|
459
|
-
public:
|
|
460
|
-
LRUCache(size_t capacity, size_t estimated_value_size, int num_shard_bits,
|
|
461
|
-
bool strict_capacity_limit,
|
|
462
|
-
CacheMetadataChargePolicy metadata_charge_policy =
|
|
463
|
-
kDontChargeCacheMetadata);
|
|
464
|
-
const char* Name() const override { return "LRUCache"; }
|
|
465
|
-
void* Value(Handle* handle) override;
|
|
466
|
-
size_t GetCharge(Handle* handle) const override;
|
|
467
|
-
DeleterFn GetDeleter(Handle* handle) const override;
|
|
468
|
-
};
|
|
469
|
-
} // namespace fast_lru_cache
|
|
470
|
-
|
|
471
|
-
std::shared_ptr<Cache> NewFastLRUCache(
|
|
472
|
-
size_t capacity, size_t estimated_value_size, int num_shard_bits,
|
|
473
|
-
bool strict_capacity_limit,
|
|
474
|
-
CacheMetadataChargePolicy metadata_charge_policy);
|
|
475
|
-
|
|
476
|
-
} // namespace ROCKSDB_NAMESPACE
|
package/max_rev_operator.h
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include <rocksdb/slice.h>
|
|
4
|
-
#include <rocksdb/merge_operator.h>
|
|
5
|
-
|
|
6
|
-
int compareRev(const rocksdb::Slice& a, const rocksdb::Slice& b) {
|
|
7
|
-
auto indexA = 0UL;
|
|
8
|
-
auto indexB = 0UL;
|
|
9
|
-
const auto endA = a.size();
|
|
10
|
-
const auto endB = b.size();
|
|
11
|
-
|
|
12
|
-
// Compare the revision number
|
|
13
|
-
auto result = 0;
|
|
14
|
-
const auto end = std::min(endA, endB);
|
|
15
|
-
while (indexA < end && indexB < end) {
|
|
16
|
-
const auto ac = a[indexA++];
|
|
17
|
-
const auto bc = b[indexB++];
|
|
18
|
-
|
|
19
|
-
if (ac == '-') {
|
|
20
|
-
if (bc == '-') {
|
|
21
|
-
break;
|
|
22
|
-
}
|
|
23
|
-
return -1;
|
|
24
|
-
} else if (bc == '-') {
|
|
25
|
-
return 1;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (!result) {
|
|
29
|
-
result = ac == bc ? 0 : ac < bc ? -1 : 1;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (result) {
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Compare the rest
|
|
38
|
-
while (indexA < end && indexB < end) {
|
|
39
|
-
const auto ac = a[indexA++];
|
|
40
|
-
const auto bc = b[indexB++];
|
|
41
|
-
if (ac != bc) {
|
|
42
|
-
return ac < bc ? -1 : 1;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return endA - endB;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
class MaxRevOperator : public rocksdb::MergeOperator {
|
|
50
|
-
public:
|
|
51
|
-
bool FullMergeV2(const MergeOperationInput& merge_in,
|
|
52
|
-
MergeOperationOutput* merge_out) const override {
|
|
53
|
-
rocksdb::Slice& max = merge_out->existing_operand;
|
|
54
|
-
if (merge_in.existing_value) {
|
|
55
|
-
max = rocksdb::Slice(merge_in.existing_value->data(),
|
|
56
|
-
merge_in.existing_value->size());
|
|
57
|
-
} else if (max.data() == nullptr) {
|
|
58
|
-
max = rocksdb::Slice();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
for (const auto& op : merge_in.operand_list) {
|
|
62
|
-
if (compareRev(max, op) < 0) {
|
|
63
|
-
max = op;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
bool PartialMerge(const rocksdb::Slice& /*key*/, const rocksdb::Slice& left_operand,
|
|
71
|
-
const rocksdb::Slice& right_operand, std::string* new_value,
|
|
72
|
-
rocksdb::Logger* /*logger*/) const override {
|
|
73
|
-
if (compareRev(left_operand, right_operand) >= 0) {
|
|
74
|
-
new_value->assign(left_operand.data(), left_operand.size());
|
|
75
|
-
} else {
|
|
76
|
-
new_value->assign(right_operand.data(), right_operand.size());
|
|
77
|
-
}
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
bool PartialMergeMulti(const rocksdb::Slice& /*key*/,
|
|
82
|
-
const std::deque<rocksdb::Slice>& operand_list,
|
|
83
|
-
std::string* new_value,
|
|
84
|
-
rocksdb::Logger* /*logger*/) const override {
|
|
85
|
-
rocksdb::Slice max;
|
|
86
|
-
for (const auto& operand : operand_list) {
|
|
87
|
-
if (compareRev(max, operand) < 0) {
|
|
88
|
-
max = operand;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
new_value->assign(max.data(), max.size());
|
|
93
|
-
return true;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
static const char* kClassName() { return "MaxRevOperator"; }
|
|
97
|
-
static const char* kNickName() { return "maxRev"; }
|
|
98
|
-
const char* Name() const override { return kClassName(); }
|
|
99
|
-
const char* NickName() const override { return kNickName(); }
|
|
100
|
-
};
|