2013-12-27 20:56:27 +00:00
|
|
|
// Copyright (c) 2013, 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.
|
|
|
|
//
|
|
|
|
|
2014-04-15 20:39:26 +00:00
|
|
|
#ifndef ROCKSDB_LITE
|
2013-12-27 20:56:27 +00:00
|
|
|
#include "util/hash_linklist_rep.h"
|
|
|
|
|
|
|
|
#include "rocksdb/memtablerep.h"
|
2014-01-31 01:18:17 +00:00
|
|
|
#include "util/arena.h"
|
2013-12-27 20:56:27 +00:00
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/slice_transform.h"
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "port/atomic_pointer.h"
|
2014-06-17 22:58:35 +00:00
|
|
|
#include "util/histogram.h"
|
2013-12-27 20:56:27 +00:00
|
|
|
#include "util/murmurhash.h"
|
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "db/skiplist.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
typedef const char* Key;
|
|
|
|
|
|
|
|
struct Node {
|
|
|
|
// Accessors/mutators for links. Wrapped in methods so we can
|
|
|
|
// add the appropriate barriers as necessary.
|
|
|
|
Node* Next() {
|
|
|
|
// Use an 'acquire load' so that we observe a fully initialized
|
|
|
|
// version of the returned Node.
|
|
|
|
return reinterpret_cast<Node*>(next_.Acquire_Load());
|
|
|
|
}
|
|
|
|
void SetNext(Node* x) {
|
|
|
|
// Use a 'release store' so that anybody who reads through this
|
|
|
|
// pointer observes a fully initialized version of the inserted node.
|
|
|
|
next_.Release_Store(x);
|
|
|
|
}
|
|
|
|
// No-barrier variants that can be safely used in a few locations.
|
|
|
|
Node* NoBarrier_Next() {
|
|
|
|
return reinterpret_cast<Node*>(next_.NoBarrier_Load());
|
|
|
|
}
|
2014-04-04 22:37:28 +00:00
|
|
|
|
2013-12-27 20:56:27 +00:00
|
|
|
void NoBarrier_SetNext(Node* x) {
|
|
|
|
next_.NoBarrier_Store(x);
|
|
|
|
}
|
|
|
|
|
2014-04-04 22:37:28 +00:00
|
|
|
private:
|
2013-12-27 20:56:27 +00:00
|
|
|
port::AtomicPointer next_;
|
2014-04-04 22:37:28 +00:00
|
|
|
public:
|
|
|
|
char key[0];
|
2013-12-27 20:56:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class HashLinkListRep : public MemTableRep {
|
|
|
|
public:
|
2014-03-10 19:56:46 +00:00
|
|
|
HashLinkListRep(const MemTableRep::KeyComparator& compare, Arena* arena,
|
2014-05-04 20:55:53 +00:00
|
|
|
const SliceTransform* transform, size_t bucket_size,
|
2014-06-17 22:58:35 +00:00
|
|
|
size_t huge_page_tlb_size, Logger* logger,
|
|
|
|
int bucket_entries_logging_threshold,
|
|
|
|
bool if_log_bucket_dist_when_flash);
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2014-04-04 22:37:28 +00:00
|
|
|
virtual KeyHandle Allocate(const size_t len, char** buf) override;
|
|
|
|
|
|
|
|
virtual void Insert(KeyHandle handle) override;
|
2013-12-27 20:56:27 +00:00
|
|
|
|
|
|
|
virtual bool Contains(const char* key) const override;
|
|
|
|
|
|
|
|
virtual size_t ApproximateMemoryUsage() override;
|
|
|
|
|
2014-02-11 17:46:30 +00:00
|
|
|
virtual void Get(const LookupKey& k, void* callback_args,
|
|
|
|
bool (*callback_func)(void* arg,
|
|
|
|
const char* entry)) override;
|
|
|
|
|
2013-12-27 20:56:27 +00:00
|
|
|
virtual ~HashLinkListRep();
|
|
|
|
|
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
|
|
|
virtual MemTableRep::Iterator* GetIterator(Arena* arena = nullptr) override;
|
2013-12-27 20:56:27 +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
|
|
|
virtual MemTableRep::Iterator* GetDynamicPrefixIterator(
|
|
|
|
Arena* arena = nullptr) override;
|
2014-01-16 07:12:31 +00:00
|
|
|
|
2013-12-27 20:56:27 +00:00
|
|
|
private:
|
|
|
|
friend class DynamicIterator;
|
2014-03-10 19:56:46 +00:00
|
|
|
typedef SkipList<const char*, const MemTableRep::KeyComparator&> FullList;
|
2013-12-27 20:56:27 +00:00
|
|
|
|
|
|
|
size_t bucket_size_;
|
|
|
|
|
|
|
|
// Maps slices (which are transformed user keys) to buckets of keys sharing
|
|
|
|
// the same transform.
|
|
|
|
port::AtomicPointer* buckets_;
|
|
|
|
|
|
|
|
// 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-12-27 20:56:27 +00:00
|
|
|
|
2014-06-17 22:58:35 +00:00
|
|
|
Logger* logger_;
|
|
|
|
int bucket_entries_logging_threshold_;
|
|
|
|
bool if_log_bucket_dist_when_flash_;
|
|
|
|
|
2014-01-25 01:50:59 +00:00
|
|
|
bool BucketContains(Node* head, const Slice& key) const;
|
|
|
|
|
|
|
|
Slice GetPrefix(const Slice& internal_key) const {
|
|
|
|
return transform_->Transform(ExtractUserKey(internal_key));
|
|
|
|
}
|
2013-12-27 20:56:27 +00:00
|
|
|
|
|
|
|
size_t GetHash(const Slice& slice) const {
|
|
|
|
return MurmurHash(slice.data(), slice.size(), 0) % bucket_size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* GetBucket(size_t i) const {
|
|
|
|
return static_cast<Node*>(buckets_[i].Acquire_Load());
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* GetBucket(const Slice& slice) const {
|
|
|
|
return GetBucket(GetHash(slice));
|
|
|
|
}
|
|
|
|
|
2014-01-25 01:50:59 +00:00
|
|
|
bool Equal(const Slice& a, const Key& b) const {
|
|
|
|
return (compare_(b, a) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-27 20:56:27 +00:00
|
|
|
bool Equal(const Key& a, const Key& b) const { return (compare_(a, b) == 0); }
|
|
|
|
|
2014-01-25 01:50:59 +00:00
|
|
|
bool KeyIsAfterNode(const Slice& internal_key, const Node* n) const {
|
|
|
|
// nullptr n is considered infinite
|
|
|
|
return (n != nullptr) && (compare_(n->key, internal_key) < 0);
|
|
|
|
}
|
|
|
|
|
2013-12-27 20:56:27 +00:00
|
|
|
bool KeyIsAfterNode(const Key& key, const Node* n) const {
|
|
|
|
// nullptr n is considered infinite
|
|
|
|
return (n != nullptr) && (compare_(n->key, key) < 0);
|
|
|
|
}
|
|
|
|
|
2014-01-25 01:50:59 +00:00
|
|
|
|
|
|
|
Node* FindGreaterOrEqualInBucket(Node* head, const Slice& key) const;
|
2013-12-27 20:56:27 +00:00
|
|
|
|
|
|
|
class FullListIterator : public MemTableRep::Iterator {
|
|
|
|
public:
|
2014-03-14 17:02:04 +00:00
|
|
|
explicit FullListIterator(FullList* list, Arena* arena)
|
|
|
|
: iter_(list), full_list_(list), arena_(arena) {}
|
2013-12-27 20:56:27 +00:00
|
|
|
|
|
|
|
virtual ~FullListIterator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff the iterator is positioned at a valid node.
|
|
|
|
virtual bool Valid() const {
|
|
|
|
return iter_.Valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the key at the current position.
|
|
|
|
// REQUIRES: Valid()
|
|
|
|
virtual const char* key() const {
|
|
|
|
assert(Valid());
|
|
|
|
return iter_.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the next position.
|
|
|
|
// REQUIRES: Valid()
|
|
|
|
virtual void Next() {
|
|
|
|
assert(Valid());
|
|
|
|
iter_.Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the previous position.
|
|
|
|
// REQUIRES: Valid()
|
|
|
|
virtual void Prev() {
|
|
|
|
assert(Valid());
|
|
|
|
iter_.Prev();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to the first entry with a key >= target
|
|
|
|
virtual void Seek(const Slice& internal_key, const char* memtable_key) {
|
|
|
|
const char* encoded_key =
|
|
|
|
(memtable_key != nullptr) ?
|
|
|
|
memtable_key : EncodeKey(&tmp_, internal_key);
|
|
|
|
iter_.Seek(encoded_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position at the first entry in collection.
|
|
|
|
// Final state of iterator is Valid() iff collection is not empty.
|
|
|
|
virtual void SeekToFirst() {
|
|
|
|
iter_.SeekToFirst();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Position at the last entry in collection.
|
|
|
|
// Final state of iterator is Valid() iff collection is not empty.
|
|
|
|
virtual void SeekToLast() {
|
|
|
|
iter_.SeekToLast();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
FullList::Iterator iter_;
|
2014-01-10 06:25:03 +00:00
|
|
|
// To destruct with the iterator.
|
|
|
|
std::unique_ptr<FullList> full_list_;
|
2014-03-14 17:02:04 +00:00
|
|
|
std::unique_ptr<Arena> arena_;
|
2013-12-27 20:56:27 +00:00
|
|
|
std::string tmp_; // For passing to EncodeKey
|
|
|
|
};
|
|
|
|
|
|
|
|
class Iterator : public MemTableRep::Iterator {
|
|
|
|
public:
|
|
|
|
explicit Iterator(const HashLinkListRep* const hash_link_list_rep,
|
|
|
|
Node* head) :
|
|
|
|
hash_link_list_rep_(hash_link_list_rep), head_(head), node_(nullptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~Iterator() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true iff the iterator is positioned at a valid node.
|
|
|
|
virtual bool Valid() const {
|
|
|
|
return node_ != nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the key at the current position.
|
|
|
|
// REQUIRES: Valid()
|
|
|
|
virtual const char* key() const {
|
|
|
|
assert(Valid());
|
|
|
|
return node_->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the next position.
|
|
|
|
// REQUIRES: Valid()
|
|
|
|
virtual void Next() {
|
|
|
|
assert(Valid());
|
|
|
|
node_ = node_->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the previous position.
|
|
|
|
// REQUIRES: Valid()
|
|
|
|
virtual void Prev() {
|
|
|
|
// Prefix iterator does not support total order.
|
|
|
|
// We simply set the iterator to invalid state
|
|
|
|
Reset(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to the first entry with a key >= target
|
|
|
|
virtual void Seek(const Slice& internal_key, const char* memtable_key) {
|
|
|
|
node_ = hash_link_list_rep_->FindGreaterOrEqualInBucket(head_,
|
2014-01-25 01:50:59 +00:00
|
|
|
internal_key);
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Position at the first entry in collection.
|
|
|
|
// Final state of iterator is Valid() iff collection is not empty.
|
|
|
|
virtual void SeekToFirst() {
|
|
|
|
// 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.
|
|
|
|
virtual void SeekToLast() {
|
|
|
|
// Prefix iterator does not support total order.
|
|
|
|
// We simply set the iterator to invalid state
|
|
|
|
Reset(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void Reset(Node* head) {
|
|
|
|
head_ = head;
|
|
|
|
node_ = nullptr;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
friend class HashLinkListRep;
|
|
|
|
const HashLinkListRep* const hash_link_list_rep_;
|
|
|
|
Node* head_;
|
|
|
|
Node* node_;
|
|
|
|
|
|
|
|
virtual void SeekToHead() {
|
|
|
|
node_ = head_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class DynamicIterator : public HashLinkListRep::Iterator {
|
|
|
|
public:
|
|
|
|
explicit DynamicIterator(HashLinkListRep& memtable_rep)
|
|
|
|
: HashLinkListRep::Iterator(&memtable_rep, nullptr),
|
|
|
|
memtable_rep_(memtable_rep) {}
|
|
|
|
|
|
|
|
// Advance to the first entry with a key >= target
|
|
|
|
virtual void Seek(const Slice& k, const char* memtable_key) {
|
2014-01-25 01:50:59 +00:00
|
|
|
auto transformed = memtable_rep_.GetPrefix(k);
|
2013-12-27 20:56:27 +00:00
|
|
|
Reset(memtable_rep_.GetBucket(transformed));
|
|
|
|
HashLinkListRep::Iterator::Seek(k, memtable_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// the underlying memtable
|
|
|
|
const HashLinkListRep& memtable_rep_;
|
|
|
|
};
|
|
|
|
|
|
|
|
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:
|
|
|
|
EmptyIterator() { }
|
|
|
|
virtual bool Valid() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
virtual const char* key() const {
|
|
|
|
assert(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
virtual void Next() { }
|
|
|
|
virtual void Prev() { }
|
|
|
|
virtual void Seek(const Slice& user_key, const char* memtable_key) { }
|
|
|
|
virtual void SeekToFirst() { }
|
|
|
|
virtual void SeekToLast() { }
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-03-10 19:56:46 +00:00
|
|
|
HashLinkListRep::HashLinkListRep(const MemTableRep::KeyComparator& compare,
|
2013-12-27 20:56:27 +00:00
|
|
|
Arena* arena, const SliceTransform* transform,
|
2014-05-04 22:52:23 +00:00
|
|
|
size_t bucket_size, size_t huge_page_tlb_size,
|
2014-06-17 22:58:35 +00:00
|
|
|
Logger* logger,
|
|
|
|
int bucket_entries_logging_threshold,
|
|
|
|
bool if_log_bucket_dist_when_flash)
|
2014-05-04 20:55:53 +00:00
|
|
|
: MemTableRep(arena),
|
|
|
|
bucket_size_(bucket_size),
|
|
|
|
transform_(transform),
|
2014-06-17 22:58:35 +00:00
|
|
|
compare_(compare),
|
|
|
|
logger_(logger),
|
|
|
|
bucket_entries_logging_threshold_(bucket_entries_logging_threshold),
|
|
|
|
if_log_bucket_dist_when_flash_(if_log_bucket_dist_when_flash) {
|
2014-05-04 20:55:53 +00:00
|
|
|
char* mem = arena_->AllocateAligned(sizeof(port::AtomicPointer) * bucket_size,
|
2014-05-04 22:52:23 +00:00
|
|
|
huge_page_tlb_size, logger);
|
2013-12-27 20:56:27 +00:00
|
|
|
|
|
|
|
buckets_ = new (mem) port::AtomicPointer[bucket_size];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < bucket_size_; ++i) {
|
|
|
|
buckets_[i].NoBarrier_Store(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HashLinkListRep::~HashLinkListRep() {
|
|
|
|
}
|
|
|
|
|
2014-04-04 22:37:28 +00:00
|
|
|
KeyHandle HashLinkListRep::Allocate(const size_t len, char** buf) {
|
|
|
|
char* mem = arena_->AllocateAligned(sizeof(Node) + len);
|
|
|
|
Node* x = new (mem) Node();
|
|
|
|
*buf = x->key;
|
|
|
|
return static_cast<void*>(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HashLinkListRep::Insert(KeyHandle handle) {
|
|
|
|
Node* x = static_cast<Node*>(handle);
|
|
|
|
assert(!Contains(x->key));
|
|
|
|
Slice internal_key = GetLengthPrefixedSlice(x->key);
|
2014-01-25 01:50:59 +00:00
|
|
|
auto transformed = GetPrefix(internal_key);
|
2013-12-27 20:56:27 +00:00
|
|
|
auto& bucket = buckets_[GetHash(transformed)];
|
|
|
|
Node* head = static_cast<Node*>(bucket.Acquire_Load());
|
|
|
|
|
|
|
|
if (!head) {
|
|
|
|
// NoBarrier_SetNext() suffices since we will add a barrier when
|
|
|
|
// we publish a pointer to "x" in prev[i].
|
|
|
|
x->NoBarrier_SetNext(nullptr);
|
|
|
|
bucket.Release_Store(static_cast<void*>(x));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* cur = head;
|
|
|
|
Node* prev = nullptr;
|
|
|
|
while (true) {
|
|
|
|
if (cur == nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Node* next = cur->Next();
|
|
|
|
// Make sure the lists are sorted.
|
|
|
|
// If x points to head_ or next points nullptr, it is trivially satisfied.
|
|
|
|
assert((cur == head) || (next == nullptr) ||
|
|
|
|
KeyIsAfterNode(next->key, cur));
|
2014-01-25 01:50:59 +00:00
|
|
|
if (KeyIsAfterNode(internal_key, cur)) {
|
2013-12-27 20:56:27 +00:00
|
|
|
// Keep searching in this list
|
|
|
|
prev = cur;
|
|
|
|
cur = next;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our data structure does not allow duplicate insertion
|
2014-04-04 22:37:28 +00:00
|
|
|
assert(cur == nullptr || !Equal(x->key, cur->key));
|
2013-12-27 20:56:27 +00:00
|
|
|
|
|
|
|
// NoBarrier_SetNext() suffices since we will add a barrier when
|
|
|
|
// we publish a pointer to "x" in prev[i].
|
|
|
|
x->NoBarrier_SetNext(cur);
|
|
|
|
|
|
|
|
if (prev) {
|
|
|
|
prev->SetNext(x);
|
|
|
|
} else {
|
|
|
|
bucket.Release_Store(static_cast<void*>(x));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HashLinkListRep::Contains(const char* key) const {
|
2014-01-25 01:50:59 +00:00
|
|
|
Slice internal_key = GetLengthPrefixedSlice(key);
|
|
|
|
|
|
|
|
auto transformed = GetPrefix(internal_key);
|
2013-12-27 20:56:27 +00:00
|
|
|
auto bucket = GetBucket(transformed);
|
|
|
|
if (bucket == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-25 01:50:59 +00:00
|
|
|
return BucketContains(bucket, internal_key);
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t HashLinkListRep::ApproximateMemoryUsage() {
|
|
|
|
// Memory is always allocated from the arena.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-11 17:46:30 +00:00
|
|
|
void HashLinkListRep::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) {
|
|
|
|
Iterator iter(this, bucket);
|
|
|
|
for (iter.Seek(k.internal_key(), nullptr);
|
|
|
|
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* HashLinkListRep::GetIterator(Arena* alloc_arena) {
|
2014-03-14 17:02:04 +00:00
|
|
|
// allocate a new arena of similar size to the one currently in use
|
|
|
|
Arena* new_arena = new Arena(arena_->BlockSize());
|
|
|
|
auto list = new FullList(compare_, new_arena);
|
2014-06-17 22:58:35 +00:00
|
|
|
HistogramImpl keys_per_bucket_hist;
|
|
|
|
|
2013-12-27 20:56:27 +00:00
|
|
|
for (size_t i = 0; i < bucket_size_; ++i) {
|
2014-06-17 22:58:35 +00:00
|
|
|
int count = 0;
|
|
|
|
bool num_entries_printed = false;
|
2013-12-27 20:56:27 +00:00
|
|
|
auto bucket = GetBucket(i);
|
|
|
|
if (bucket != nullptr) {
|
|
|
|
Iterator itr(this, bucket);
|
|
|
|
for (itr.SeekToHead(); itr.Valid(); itr.Next()) {
|
|
|
|
list->Insert(itr.key());
|
2014-06-17 22:58:35 +00:00
|
|
|
if (logger_ != nullptr &&
|
|
|
|
++count >= bucket_entries_logging_threshold_ &&
|
|
|
|
!num_entries_printed) {
|
|
|
|
num_entries_printed = true;
|
|
|
|
Info(logger_, "HashLinkedList bucket %zu has more than %d "
|
|
|
|
"entries. %dth key: %s",
|
|
|
|
i, count, count,
|
|
|
|
GetLengthPrefixedSlice(itr.key()).ToString(true).c_str());
|
|
|
|
}
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-17 22:58:35 +00:00
|
|
|
if (if_log_bucket_dist_when_flash_) {
|
|
|
|
keys_per_bucket_hist.Add(count);
|
|
|
|
}
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
2014-06-17 22:58:35 +00:00
|
|
|
if (if_log_bucket_dist_when_flash_ && logger_ != nullptr) {
|
|
|
|
Info(logger_, "hashLinkedList Entry distribution among buckets: %s",
|
|
|
|
keys_per_bucket_hist.ToString().c_str());
|
|
|
|
}
|
|
|
|
|
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 (alloc_arena == nullptr) {
|
|
|
|
return new FullListIterator(list, new_arena);
|
|
|
|
} else {
|
|
|
|
auto mem = alloc_arena->AllocateAligned(sizeof(FullListIterator));
|
|
|
|
return new (mem) FullListIterator(list, new_arena);
|
|
|
|
}
|
2013-12-27 20:56:27 +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* HashLinkListRep::GetDynamicPrefixIterator(
|
|
|
|
Arena* alloc_arena) {
|
|
|
|
if (alloc_arena == nullptr) {
|
|
|
|
return new DynamicIterator(*this);
|
|
|
|
} else {
|
|
|
|
auto mem = alloc_arena->AllocateAligned(sizeof(DynamicIterator));
|
|
|
|
return new (mem) DynamicIterator(*this);
|
|
|
|
}
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
2014-01-25 01:50:59 +00:00
|
|
|
bool HashLinkListRep::BucketContains(Node* head, const Slice& user_key) const {
|
|
|
|
Node* x = FindGreaterOrEqualInBucket(head, user_key);
|
|
|
|
return (x != nullptr && Equal(user_key, x->key));
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* HashLinkListRep::FindGreaterOrEqualInBucket(Node* head,
|
2014-01-25 01:50:59 +00:00
|
|
|
const Slice& key) const {
|
2013-12-27 20:56:27 +00:00
|
|
|
Node* x = head;
|
|
|
|
while (true) {
|
|
|
|
if (x == nullptr) {
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
Node* next = x->Next();
|
|
|
|
// Make sure the lists are sorted.
|
|
|
|
// If x points to head_ or next points nullptr, it is trivially satisfied.
|
|
|
|
assert((x == head) || (next == nullptr) || KeyIsAfterNode(next->key, x));
|
|
|
|
if (KeyIsAfterNode(key, x)) {
|
|
|
|
// Keep searching in this list
|
|
|
|
x = next;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anon namespace
|
|
|
|
|
2014-01-16 07:12:31 +00:00
|
|
|
MemTableRep* HashLinkListRepFactory::CreateMemTableRep(
|
2014-03-10 19:56:46 +00:00
|
|
|
const MemTableRep::KeyComparator& compare, Arena* arena,
|
2014-05-04 22:52:23 +00:00
|
|
|
const SliceTransform* transform, Logger* logger) {
|
2014-06-17 22:58:35 +00:00
|
|
|
return new HashLinkListRep(
|
|
|
|
compare, arena, transform, bucket_count_, huge_page_tlb_size_, logger,
|
|
|
|
bucket_entries_logging_threshold_, if_log_bucket_dist_when_flash_);
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
2014-06-17 22:58:35 +00:00
|
|
|
MemTableRepFactory* NewHashLinkListRepFactory(
|
|
|
|
size_t bucket_count, size_t huge_page_tlb_size,
|
|
|
|
int bucket_entries_logging_threshold, bool if_log_bucket_dist_when_flash) {
|
|
|
|
return new HashLinkListRepFactory(bucket_count, huge_page_tlb_size,
|
|
|
|
bucket_entries_logging_threshold,
|
|
|
|
if_log_bucket_dist_when_flash);
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace rocksdb
|
2014-04-15 20:39:26 +00:00
|
|
|
#endif // ROCKSDB_LITE
|