2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// 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).
|
2013-11-06 19:55:43 +00:00
|
|
|
//
|
|
|
|
|
2014-10-27 21:50:21 +00:00
|
|
|
#include <atomic>
|
|
|
|
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "memory/arena.h"
|
|
|
|
#include "memtable/skiplist.h"
|
|
|
|
#include "port/port.h"
|
2013-11-06 19:55:43 +00:00
|
|
|
#include "rocksdb/memtablerep.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/slice_transform.h"
|
2021-09-08 14:45:59 +00:00
|
|
|
#include "rocksdb/utilities/options_type.h"
|
2013-11-06 19:55:43 +00:00
|
|
|
#include "util/murmurhash.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2013-11-06 19:55:43 +00:00
|
|
|
namespace {
|
|
|
|
|
2013-11-08 08:31:09 +00:00
|
|
|
class HashSkipListRep : public MemTableRep {
|
2013-11-06 19:55:43 +00:00
|
|
|
public:
|
2014-12-02 20:09:20 +00:00
|
|
|
HashSkipListRep(const MemTableRep::KeyComparator& compare,
|
2017-06-02 21:13:59 +00:00
|
|
|
Allocator* allocator, const SliceTransform* transform,
|
2014-12-02 20:09:20 +00:00
|
|
|
size_t bucket_size, int32_t skiplist_height,
|
|
|
|
int32_t skiplist_branching_factor);
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
void Insert(KeyHandle handle) override;
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
bool Contains(const char* key) const override;
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
size_t ApproximateMemoryUsage() override;
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
void Get(const LookupKey& k, void* callback_args,
|
|
|
|
bool (*callback_func)(void* arg, const char* entry)) override;
|
2014-02-11 17:46:30 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
~HashSkipListRep() override;
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
MemTableRep::Iterator* GetIterator(Arena* arena = nullptr) override;
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
MemTableRep::Iterator* GetDynamicPrefixIterator(
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
Arena* arena = nullptr) override;
|
2014-01-16 02:17:58 +00:00
|
|
|
|
2013-11-06 19:55:43 +00:00
|
|
|
private:
|
2013-11-08 08:31:09 +00:00
|
|
|
friend class DynamicIterator;
|
2021-09-07 18:31:12 +00:00
|
|
|
using Bucket = SkipList<const char*, const MemTableRep::KeyComparator&>;
|
2013-11-06 19:55:43 +00:00
|
|
|
|
|
|
|
size_t bucket_size_;
|
|
|
|
|
2013-11-22 21:31:00 +00:00
|
|
|
const int32_t skiplist_height_;
|
|
|
|
const int32_t skiplist_branching_factor_;
|
|
|
|
|
2013-11-06 19:55:43 +00:00
|
|
|
// Maps slices (which are transformed user keys) to buckets of keys sharing
|
|
|
|
// the same transform.
|
2014-10-27 21:50:21 +00:00
|
|
|
std::atomic<Bucket*>* buckets_;
|
2013-11-06 19:55:43 +00:00
|
|
|
|
|
|
|
// The user-supplied transform whose domain is the user keys.
|
|
|
|
const SliceTransform* transform_;
|
|
|
|
|
2014-03-10 19:56:46 +00:00
|
|
|
const MemTableRep::KeyComparator& compare_;
|
2013-11-06 19:55:43 +00:00
|
|
|
// immutable after construction
|
2017-06-02 21:13:59 +00:00
|
|
|
Allocator* const allocator_;
|
2013-11-06 19:55:43 +00:00
|
|
|
|
|
|
|
inline size_t GetHash(const Slice& slice) const {
|
2014-11-11 21:47:22 +00:00
|
|
|
return MurmurHash(slice.data(), static_cast<int>(slice.size()), 0) %
|
|
|
|
bucket_size_;
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
|
|
|
inline Bucket* GetBucket(size_t i) const {
|
2014-10-27 21:50:21 +00:00
|
|
|
return buckets_[i].load(std::memory_order_acquire);
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
|
|
|
inline Bucket* GetBucket(const Slice& slice) const {
|
|
|
|
return GetBucket(GetHash(slice));
|
|
|
|
}
|
|
|
|
// Get a bucket from buckets_. If the bucket hasn't been initialized yet,
|
|
|
|
// initialize it before returning.
|
|
|
|
Bucket* GetInitializedBucket(const Slice& transformed);
|
|
|
|
|
|
|
|
class Iterator : public MemTableRep::Iterator {
|
|
|
|
public:
|
2014-03-14 17:02:04 +00:00
|
|
|
explicit Iterator(Bucket* list, bool own_list = true,
|
|
|
|
Arena* arena = nullptr)
|
|
|
|
: list_(list), iter_(list), own_list_(own_list), arena_(arena) {}
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
~Iterator() override {
|
2013-11-06 19:55:43 +00:00
|
|
|
// if we own the list, we should also delete it
|
|
|
|
if (own_list_) {
|
2013-11-08 08:31:09 +00:00
|
|
|
assert(list_ != nullptr);
|
2013-11-06 19:55:43 +00:00
|
|
|
delete list_;
|
|
|
|
}
|
2013-11-08 08:31:09 +00:00
|
|
|
}
|
2013-11-06 19:55:43 +00:00
|
|
|
|
|
|
|
// Returns true iff the iterator is positioned at a valid node.
|
2019-02-14 21:52:47 +00:00
|
|
|
bool Valid() const override { return list_ != nullptr && iter_.Valid(); }
|
2013-11-06 19:55:43 +00:00
|
|
|
|
|
|
|
// Returns the key at the current position.
|
|
|
|
// REQUIRES: Valid()
|
2019-02-14 21:52:47 +00:00
|
|
|
const char* key() const override {
|
2013-11-08 08:31:09 +00:00
|
|
|
assert(Valid());
|
2013-11-06 19:55:43 +00:00
|
|
|
return iter_.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the next position.
|
|
|
|
// REQUIRES: Valid()
|
2019-02-14 21:52:47 +00:00
|
|
|
void Next() override {
|
2013-11-08 08:31:09 +00:00
|
|
|
assert(Valid());
|
2013-11-06 19:55:43 +00:00
|
|
|
iter_.Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the previous position.
|
|
|
|
// REQUIRES: Valid()
|
2019-02-14 21:52:47 +00:00
|
|
|
void Prev() override {
|
2013-11-08 08:31:09 +00:00
|
|
|
assert(Valid());
|
2013-11-06 19:55:43 +00:00
|
|
|
iter_.Prev();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to the first entry with a key >= target
|
2019-02-14 21:52:47 +00:00
|
|
|
void Seek(const Slice& internal_key, const char* memtable_key) override {
|
2013-11-08 08:31:09 +00:00
|
|
|
if (list_ != nullptr) {
|
2022-10-28 20:16:50 +00:00
|
|
|
const char* encoded_key = (memtable_key != nullptr)
|
|
|
|
? memtable_key
|
|
|
|
: EncodeKey(&tmp_, internal_key);
|
2013-11-21 03:49:27 +00:00
|
|
|
iter_.Seek(encoded_key);
|
2013-11-08 08:31:09 +00:00
|
|
|
}
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 01:20:57 +00:00
|
|
|
// Retreat to the last entry with a key <= target
|
2019-02-14 21:52:47 +00:00
|
|
|
void SeekForPrev(const Slice& /*internal_key*/,
|
|
|
|
const char* /*memtable_key*/) override {
|
2016-09-28 01:20:57 +00:00
|
|
|
// not supported
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
2013-11-06 19:55:43 +00:00
|
|
|
// Position at the first entry in collection.
|
|
|
|
// Final state of iterator is Valid() iff collection is not empty.
|
2019-02-14 21:52:47 +00:00
|
|
|
void SeekToFirst() override {
|
2013-11-08 08:31:09 +00:00
|
|
|
if (list_ != nullptr) {
|
|
|
|
iter_.SeekToFirst();
|
|
|
|
}
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Position at the last entry in collection.
|
|
|
|
// Final state of iterator is Valid() iff collection is not empty.
|
2019-02-14 21:52:47 +00:00
|
|
|
void SeekToLast() override {
|
2013-11-08 08:31:09 +00:00
|
|
|
if (list_ != nullptr) {
|
|
|
|
iter_.SeekToLast();
|
|
|
|
}
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
|
2013-11-08 08:31:09 +00:00
|
|
|
protected:
|
|
|
|
void Reset(Bucket* list) {
|
|
|
|
if (own_list_) {
|
|
|
|
assert(list_ != nullptr);
|
|
|
|
delete list_;
|
|
|
|
}
|
|
|
|
list_ = list;
|
|
|
|
iter_.SetList(list);
|
|
|
|
own_list_ = false;
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
2022-10-28 20:16:50 +00:00
|
|
|
|
2013-11-06 19:55:43 +00:00
|
|
|
private:
|
2013-11-08 08:31:09 +00:00
|
|
|
// if list_ is nullptr, we should NEVER call any methods on iter_
|
|
|
|
// if list_ is nullptr, this Iterator is not Valid()
|
2013-11-06 19:55:43 +00:00
|
|
|
Bucket* list_;
|
|
|
|
Bucket::Iterator iter_;
|
|
|
|
// here we track if we own list_. If we own it, we are also
|
2018-11-09 19:17:34 +00:00
|
|
|
// responsible for it's cleaning. This is a poor man's std::shared_ptr
|
2013-11-06 19:55:43 +00:00
|
|
|
bool own_list_;
|
2014-03-14 17:02:04 +00:00
|
|
|
std::unique_ptr<Arena> arena_;
|
2022-10-28 20:16:50 +00:00
|
|
|
std::string tmp_; // For passing to EncodeKey
|
2013-11-06 19:55:43 +00:00
|
|
|
};
|
|
|
|
|
2013-11-08 08:31:09 +00:00
|
|
|
class DynamicIterator : public HashSkipListRep::Iterator {
|
|
|
|
public:
|
|
|
|
explicit DynamicIterator(const HashSkipListRep& memtable_rep)
|
2022-10-28 20:16:50 +00:00
|
|
|
: HashSkipListRep::Iterator(nullptr, false),
|
|
|
|
memtable_rep_(memtable_rep) {}
|
2013-11-08 08:31:09 +00:00
|
|
|
|
|
|
|
// Advance to the first entry with a key >= target
|
2019-02-14 21:52:47 +00:00
|
|
|
void Seek(const Slice& k, const char* memtable_key) override {
|
2014-01-25 01:50:59 +00:00
|
|
|
auto transformed = memtable_rep_.transform_->Transform(ExtractUserKey(k));
|
2013-11-08 08:31:09 +00:00
|
|
|
Reset(memtable_rep_.GetBucket(transformed));
|
2013-11-21 03:49:27 +00:00
|
|
|
HashSkipListRep::Iterator::Seek(k, memtable_key);
|
2013-11-08 08:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Position at the first entry in collection.
|
|
|
|
// Final state of iterator is Valid() iff collection is not empty.
|
2019-02-14 21:52:47 +00:00
|
|
|
void SeekToFirst() override {
|
2013-11-08 08:31:09 +00:00
|
|
|
// Prefix iterator does not support total order.
|
|
|
|
// We simply set the iterator to invalid state
|
|
|
|
Reset(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position at the last entry in collection.
|
|
|
|
// Final state of iterator is Valid() iff collection is not empty.
|
2019-02-14 21:52:47 +00:00
|
|
|
void SeekToLast() override {
|
2013-11-08 08:31:09 +00:00
|
|
|
// Prefix iterator does not support total order.
|
|
|
|
// We simply set the iterator to invalid state
|
|
|
|
Reset(nullptr);
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
|
2013-11-08 08:31:09 +00:00
|
|
|
private:
|
|
|
|
// the underlying memtable
|
|
|
|
const HashSkipListRep& memtable_rep_;
|
|
|
|
};
|
|
|
|
|
2013-11-06 19:55:43 +00:00
|
|
|
class EmptyIterator : public MemTableRep::Iterator {
|
|
|
|
// This is used when there wasn't a bucket. It is cheaper than
|
|
|
|
// instantiating an empty bucket over which to iterate.
|
|
|
|
public:
|
2022-10-28 20:16:50 +00:00
|
|
|
EmptyIterator() {}
|
2019-02-14 21:52:47 +00:00
|
|
|
bool Valid() const override { return false; }
|
|
|
|
const char* key() const override {
|
2013-11-06 19:55:43 +00:00
|
|
|
assert(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
void Next() override {}
|
|
|
|
void Prev() override {}
|
|
|
|
void Seek(const Slice& /*internal_key*/,
|
|
|
|
const char* /*memtable_key*/) override {}
|
|
|
|
void SeekForPrev(const Slice& /*internal_key*/,
|
|
|
|
const char* /*memtable_key*/) override {}
|
|
|
|
void SeekToFirst() override {}
|
|
|
|
void SeekToLast() override {}
|
2015-02-26 19:28:41 +00:00
|
|
|
|
2013-11-06 19:55:43 +00:00
|
|
|
private:
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-03-10 19:56:46 +00:00
|
|
|
HashSkipListRep::HashSkipListRep(const MemTableRep::KeyComparator& compare,
|
2017-06-02 21:13:59 +00:00
|
|
|
Allocator* allocator,
|
2014-12-02 20:09:20 +00:00
|
|
|
const SliceTransform* transform,
|
2013-11-22 21:31:00 +00:00
|
|
|
size_t bucket_size, int32_t skiplist_height,
|
|
|
|
int32_t skiplist_branching_factor)
|
2014-12-02 20:09:20 +00:00
|
|
|
: MemTableRep(allocator),
|
2014-04-04 22:37:28 +00:00
|
|
|
bucket_size_(bucket_size),
|
2014-01-16 07:12:31 +00:00
|
|
|
skiplist_height_(skiplist_height),
|
|
|
|
skiplist_branching_factor_(skiplist_branching_factor),
|
2014-01-16 02:17:58 +00:00
|
|
|
transform_(transform),
|
|
|
|
compare_(compare),
|
2014-12-02 20:09:20 +00:00
|
|
|
allocator_(allocator) {
|
2022-10-28 20:16:50 +00:00
|
|
|
auto mem =
|
|
|
|
allocator->AllocateAligned(sizeof(std::atomic<void*>) * bucket_size);
|
2014-10-27 21:50:21 +00:00
|
|
|
buckets_ = new (mem) std::atomic<Bucket*>[bucket_size];
|
2013-11-06 19:55:43 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < bucket_size_; ++i) {
|
2014-10-27 21:50:21 +00:00
|
|
|
buckets_[i].store(nullptr, std::memory_order_relaxed);
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 20:16:50 +00:00
|
|
|
HashSkipListRep::~HashSkipListRep() {}
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2013-11-08 08:31:09 +00:00
|
|
|
HashSkipListRep::Bucket* HashSkipListRep::GetInitializedBucket(
|
2013-11-06 19:55:43 +00:00
|
|
|
const Slice& transformed) {
|
|
|
|
size_t hash = GetHash(transformed);
|
|
|
|
auto bucket = GetBucket(hash);
|
|
|
|
if (bucket == nullptr) {
|
2014-12-02 20:09:20 +00:00
|
|
|
auto addr = allocator_->AllocateAligned(sizeof(Bucket));
|
|
|
|
bucket = new (addr) Bucket(compare_, allocator_, skiplist_height_,
|
2013-11-22 21:31:00 +00:00
|
|
|
skiplist_branching_factor_);
|
2014-10-27 21:50:21 +00:00
|
|
|
buckets_[hash].store(bucket, std::memory_order_release);
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
|
|
|
return bucket;
|
|
|
|
}
|
|
|
|
|
2018-02-16 01:12:48 +00:00
|
|
|
void HashSkipListRep::Insert(KeyHandle handle) {
|
2014-04-04 22:37:28 +00:00
|
|
|
auto* key = static_cast<char*>(handle);
|
2013-11-06 19:55:43 +00:00
|
|
|
assert(!Contains(key));
|
|
|
|
auto transformed = transform_->Transform(UserKey(key));
|
|
|
|
auto bucket = GetInitializedBucket(transformed);
|
|
|
|
bucket->Insert(key);
|
|
|
|
}
|
|
|
|
|
2013-11-08 08:31:09 +00:00
|
|
|
bool HashSkipListRep::Contains(const char* key) const {
|
2013-11-06 19:55:43 +00:00
|
|
|
auto transformed = transform_->Transform(UserKey(key));
|
|
|
|
auto bucket = GetBucket(transformed);
|
|
|
|
if (bucket == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return bucket->Contains(key);
|
|
|
|
}
|
|
|
|
|
2022-10-28 20:16:50 +00:00
|
|
|
size_t HashSkipListRep::ApproximateMemoryUsage() { return 0; }
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2014-02-11 17:46:30 +00:00
|
|
|
void HashSkipListRep::Get(const LookupKey& k, void* callback_args,
|
|
|
|
bool (*callback_func)(void* arg, const char* entry)) {
|
|
|
|
auto transformed = transform_->Transform(k.user_key());
|
|
|
|
auto bucket = GetBucket(transformed);
|
|
|
|
if (bucket != nullptr) {
|
|
|
|
Bucket::Iterator iter(bucket);
|
|
|
|
for (iter.Seek(k.memtable_key().data());
|
|
|
|
iter.Valid() && callback_func(callback_args, iter.key());
|
|
|
|
iter.Next()) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
MemTableRep::Iterator* HashSkipListRep::GetIterator(Arena* arena) {
|
2014-03-14 17:02:04 +00:00
|
|
|
// allocate a new arena of similar size to the one currently in use
|
2014-12-02 20:09:20 +00:00
|
|
|
Arena* new_arena = new Arena(allocator_->BlockSize());
|
2014-03-14 17:02:04 +00:00
|
|
|
auto list = new Bucket(compare_, new_arena);
|
2013-11-06 19:55:43 +00:00
|
|
|
for (size_t i = 0; i < bucket_size_; ++i) {
|
|
|
|
auto bucket = GetBucket(i);
|
|
|
|
if (bucket != nullptr) {
|
|
|
|
Bucket::Iterator itr(bucket);
|
|
|
|
for (itr.SeekToFirst(); itr.Valid(); itr.Next()) {
|
|
|
|
list->Insert(itr.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
if (arena == nullptr) {
|
|
|
|
return new Iterator(list, true, new_arena);
|
|
|
|
} else {
|
|
|
|
auto mem = arena->AllocateAligned(sizeof(Iterator));
|
|
|
|
return new (mem) Iterator(list, true, new_arena);
|
|
|
|
}
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
In DB::NewIterator(), try to allocate the whole iterator tree in an arena
Summary:
In this patch, try to allocate the whole iterator tree starting from DBIter from an arena
1. ArenaWrappedDBIter is created when serves as the entry point of an iterator tree, with an arena in it.
2. Add an option to create iterator from arena for following iterators: DBIter, MergingIterator, MemtableIterator, all mem table's iterators, all table reader's iterators and two level iterator.
3. MergeIteratorBuilder is created to incrementally build the tree of internal iterators. It is passed to mem table list and version set and add iterators to it.
Limitations:
(1) Only DB::NewIterator() without tailing uses the arena. Other cases, including readonly DB and compactions are still from malloc
(2) Two level iterator itself is allocated in arena, but not iterators inside it.
Test Plan: make all check
Reviewers: ljin, haobo
Reviewed By: haobo
Subscribers: leveldb, dhruba, yhchiang, igor
Differential Revision: https://reviews.facebook.net/D18513
2014-06-02 23:38:00 +00:00
|
|
|
MemTableRep::Iterator* HashSkipListRep::GetDynamicPrefixIterator(Arena* arena) {
|
|
|
|
if (arena == nullptr) {
|
|
|
|
return new DynamicIterator(*this);
|
|
|
|
} else {
|
|
|
|
auto mem = arena->AllocateAligned(sizeof(DynamicIterator));
|
|
|
|
return new (mem) DynamicIterator(*this);
|
|
|
|
}
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 14:45:59 +00:00
|
|
|
struct HashSkipListRepOptions {
|
|
|
|
static const char* kName() { return "HashSkipListRepFactoryOptions"; }
|
|
|
|
size_t bucket_count;
|
|
|
|
int32_t skiplist_height;
|
|
|
|
int32_t skiplist_branching_factor;
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::unordered_map<std::string, OptionTypeInfo> hash_skiplist_info = {
|
|
|
|
{"bucket_count",
|
|
|
|
{offsetof(struct HashSkipListRepOptions, bucket_count), OptionType::kSizeT,
|
|
|
|
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
|
|
|
|
{"skiplist_height",
|
|
|
|
{offsetof(struct HashSkipListRepOptions, skiplist_height),
|
|
|
|
OptionType::kInt32T, OptionVerificationType::kNormal,
|
|
|
|
OptionTypeFlags::kNone}},
|
|
|
|
{"branching_factor",
|
|
|
|
{offsetof(struct HashSkipListRepOptions, skiplist_branching_factor),
|
|
|
|
OptionType::kInt32T, OptionVerificationType::kNormal,
|
|
|
|
OptionTypeFlags::kNone}},
|
|
|
|
};
|
|
|
|
|
|
|
|
class HashSkipListRepFactory : public MemTableRepFactory {
|
|
|
|
public:
|
|
|
|
explicit HashSkipListRepFactory(size_t bucket_count, int32_t skiplist_height,
|
|
|
|
int32_t skiplist_branching_factor) {
|
|
|
|
options_.bucket_count = bucket_count;
|
|
|
|
options_.skiplist_height = skiplist_height;
|
|
|
|
options_.skiplist_branching_factor = skiplist_branching_factor;
|
|
|
|
RegisterOptions(&options_, &hash_skiplist_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
using MemTableRepFactory::CreateMemTableRep;
|
|
|
|
virtual MemTableRep* CreateMemTableRep(
|
|
|
|
const MemTableRep::KeyComparator& compare, Allocator* allocator,
|
|
|
|
const SliceTransform* transform, Logger* logger) override;
|
|
|
|
|
|
|
|
static const char* kClassName() { return "HashSkipListRepFactory"; }
|
|
|
|
static const char* kNickName() { return "prefix_hash"; }
|
|
|
|
|
|
|
|
virtual const char* Name() const override { return kClassName(); }
|
|
|
|
virtual const char* NickName() const override { return kNickName(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
HashSkipListRepOptions options_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
2013-11-06 19:55:43 +00:00
|
|
|
|
2014-01-16 02:17:58 +00:00
|
|
|
MemTableRep* HashSkipListRepFactory::CreateMemTableRep(
|
2017-06-02 21:13:59 +00:00
|
|
|
const MemTableRep::KeyComparator& compare, Allocator* allocator,
|
2018-03-05 21:08:17 +00:00
|
|
|
const SliceTransform* transform, Logger* /*logger*/) {
|
2021-09-08 14:45:59 +00:00
|
|
|
return new HashSkipListRep(compare, allocator, transform,
|
|
|
|
options_.bucket_count, options_.skiplist_height,
|
|
|
|
options_.skiplist_branching_factor);
|
2013-12-03 20:42:15 +00:00
|
|
|
}
|
2013-11-08 08:31:09 +00:00
|
|
|
|
|
|
|
MemTableRepFactory* NewHashSkipListRepFactory(
|
2014-03-10 19:56:46 +00:00
|
|
|
size_t bucket_count, int32_t skiplist_height,
|
|
|
|
int32_t skiplist_branching_factor) {
|
|
|
|
return new HashSkipListRepFactory(bucket_count, skiplist_height,
|
2022-10-28 20:16:50 +00:00
|
|
|
skiplist_branching_factor);
|
2013-11-06 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|