]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/db/pinned_iterators_manager.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / db / pinned_iterators_manager.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 #pragma once
7 #include <algorithm>
8 #include <memory>
9 #include <utility>
10 #include <vector>
11
12 #include "table/internal_iterator.h"
13
14 namespace ROCKSDB_NAMESPACE {
15
16 // PinnedIteratorsManager will be notified whenever we need to pin an Iterator
17 // and it will be responsible for deleting pinned Iterators when they are
18 // not needed anymore.
19 class PinnedIteratorsManager : public Cleanable {
20 public:
21 PinnedIteratorsManager() : pinning_enabled(false) {}
22 ~PinnedIteratorsManager() {
23 if (pinning_enabled) {
24 ReleasePinnedData();
25 }
26 }
27
28 // Move constructor and move assignment is allowed.
29 PinnedIteratorsManager(PinnedIteratorsManager&& other) noexcept = default;
30 PinnedIteratorsManager& operator=(PinnedIteratorsManager&& other) noexcept =
31 default;
32
33 // Enable Iterators pinning
34 void StartPinning() {
35 assert(pinning_enabled == false);
36 pinning_enabled = true;
37 }
38
39 // Is pinning enabled ?
40 bool PinningEnabled() { return pinning_enabled; }
41
42 // Take ownership of iter and delete it when ReleasePinnedData() is called
43 void PinIterator(InternalIterator* iter, bool arena = false) {
44 if (arena) {
45 PinPtr(iter, &PinnedIteratorsManager::ReleaseArenaInternalIterator);
46 } else {
47 PinPtr(iter, &PinnedIteratorsManager::ReleaseInternalIterator);
48 }
49 }
50
51 using ReleaseFunction = void (*)(void* arg1);
52 void PinPtr(void* ptr, ReleaseFunction release_func) {
53 assert(pinning_enabled);
54 if (ptr == nullptr) {
55 return;
56 }
57 pinned_ptrs_.emplace_back(ptr, release_func);
58 }
59
60 // Release pinned Iterators
61 inline void ReleasePinnedData() {
62 assert(pinning_enabled == true);
63 pinning_enabled = false;
64
65 // Remove duplicate pointers
66 std::sort(pinned_ptrs_.begin(), pinned_ptrs_.end());
67 auto unique_end = std::unique(pinned_ptrs_.begin(), pinned_ptrs_.end());
68
69 for (auto i = pinned_ptrs_.begin(); i != unique_end; ++i) {
70 void* ptr = i->first;
71 ReleaseFunction release_func = i->second;
72 release_func(ptr);
73 }
74 pinned_ptrs_.clear();
75 // Also do cleanups from the base Cleanable
76 Cleanable::Reset();
77 }
78
79 private:
80 static void ReleaseInternalIterator(void* ptr) {
81 delete reinterpret_cast<InternalIterator*>(ptr);
82 }
83
84 static void ReleaseArenaInternalIterator(void* ptr) {
85 reinterpret_cast<InternalIterator*>(ptr)->~InternalIterator();
86 }
87
88 bool pinning_enabled;
89 std::vector<std::pair<void*, ReleaseFunction>> pinned_ptrs_;
90 };
91
92 } // namespace ROCKSDB_NAMESPACE