2016-04-26 19:41:07 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
2016-08-11 18:54:17 +00:00
|
|
|
#include <utility>
|
2016-04-26 19:41:07 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "table/internal_iterator.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
// PinnedIteratorsManager will be notified whenever we need to pin an Iterator
|
|
|
|
// and it will be responsible for deleting pinned Iterators when they are
|
|
|
|
// not needed anymore.
|
|
|
|
class PinnedIteratorsManager {
|
|
|
|
public:
|
2016-08-11 18:54:17 +00:00
|
|
|
PinnedIteratorsManager() : pinning_enabled(false) {}
|
|
|
|
~PinnedIteratorsManager() {
|
|
|
|
if (pinning_enabled) {
|
|
|
|
ReleasePinnedData();
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 19:41:07 +00:00
|
|
|
|
|
|
|
// Enable Iterators pinning
|
|
|
|
void StartPinning() {
|
2016-08-11 18:54:17 +00:00
|
|
|
assert(pinning_enabled == false);
|
|
|
|
pinning_enabled = true;
|
2016-04-26 19:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is pinning enabled ?
|
|
|
|
bool PinningEnabled() { return pinning_enabled; }
|
|
|
|
|
2016-08-11 18:54:17 +00:00
|
|
|
// Take ownership of iter and delete it when ReleasePinnedData() is called
|
|
|
|
void PinIterator(InternalIterator* iter, bool arena = false) {
|
|
|
|
if (arena) {
|
|
|
|
PinPtr(iter, &PinnedIteratorsManager::ReleaseArenaInternalIterator);
|
|
|
|
} else {
|
|
|
|
PinPtr(iter, &PinnedIteratorsManager::ReleaseInternalIterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef void (*ReleaseFunction)(void* arg1);
|
|
|
|
void PinPtr(void* ptr, ReleaseFunction release_func) {
|
|
|
|
assert(pinning_enabled);
|
|
|
|
if (ptr == nullptr) {
|
2016-04-26 19:41:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-11 18:54:17 +00:00
|
|
|
pinned_ptrs_.emplace_back(ptr, release_func);
|
2016-04-26 19:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release pinned Iterators
|
2016-08-11 18:54:17 +00:00
|
|
|
inline void ReleasePinnedData() {
|
|
|
|
assert(pinning_enabled == true);
|
|
|
|
pinning_enabled = false;
|
|
|
|
|
|
|
|
// Remove duplicate pointers
|
|
|
|
std::sort(pinned_ptrs_.begin(), pinned_ptrs_.end());
|
|
|
|
std::unique(pinned_ptrs_.begin(), pinned_ptrs_.end());
|
2016-04-26 19:41:07 +00:00
|
|
|
|
2016-08-11 18:54:17 +00:00
|
|
|
for (size_t i = 0; i < pinned_ptrs_.size(); i++) {
|
|
|
|
assert(i == 0 || pinned_ptrs_[i].first != pinned_ptrs_[i - 1].first);
|
2016-04-26 19:41:07 +00:00
|
|
|
|
2016-08-11 18:54:17 +00:00
|
|
|
void* ptr = pinned_ptrs_[i].first;
|
|
|
|
ReleaseFunction release_func = pinned_ptrs_[i].second;
|
|
|
|
release_func(ptr);
|
2016-04-26 19:41:07 +00:00
|
|
|
}
|
2016-08-11 18:54:17 +00:00
|
|
|
pinned_ptrs_.clear();
|
2016-04-26 19:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-08-11 18:54:17 +00:00
|
|
|
static void ReleaseInternalIterator(void* ptr) {
|
|
|
|
delete reinterpret_cast<InternalIterator*>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReleaseArenaInternalIterator(void* ptr) {
|
|
|
|
reinterpret_cast<InternalIterator*>(ptr)->~InternalIterator();
|
|
|
|
}
|
|
|
|
|
2016-04-26 19:41:07 +00:00
|
|
|
bool pinning_enabled;
|
2016-08-11 18:54:17 +00:00
|
|
|
std::vector<std::pair<void*, ReleaseFunction>> pinned_ptrs_;
|
2016-04-26 19:41:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rocksdb
|