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-12-27 20:56:27 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
#include <algorithm>
|
2014-10-27 21:50:21 +00:00
|
|
|
#include <atomic>
|
2021-09-08 14:45:59 +00:00
|
|
|
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "db/memtable.h"
|
2019-05-31 00:39:43 +00:00
|
|
|
#include "memory/arena.h"
|
2017-04-06 20:59:31 +00:00
|
|
|
#include "memtable/skiplist.h"
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "monitoring/histogram.h"
|
|
|
|
#include "port/port.h"
|
2013-12-27 20:56:27 +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"
|
2019-04-08 20:24:29 +00:00
|
|
|
#include "util/hash.h"
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2013-12-27 20:56:27 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-09-07 18:31:12 +00:00
|
|
|
using Key = const char*;
|
|
|
|
using MemtableSkipList = SkipList<Key, const MemTableRep::KeyComparator&>;
|
|
|
|
using Pointer = std::atomic<void*>;
|
2014-07-01 18:05:05 +00:00
|
|
|
|
|
|
|
// A data structure used as the header of a link list of a hash bucket.
|
|
|
|
struct BucketHeader {
|
|
|
|
Pointer next;
|
2015-01-28 23:16:25 +00:00
|
|
|
std::atomic<uint32_t> num_entries;
|
2014-07-01 18:05:05 +00:00
|
|
|
|
|
|
|
explicit BucketHeader(void* n, uint32_t count)
|
|
|
|
: next(n), num_entries(count) {}
|
|
|
|
|
2014-10-27 21:50:21 +00:00
|
|
|
bool IsSkipListBucket() {
|
|
|
|
return next.load(std::memory_order_relaxed) == this;
|
|
|
|
}
|
2015-01-28 23:16:25 +00:00
|
|
|
|
|
|
|
uint32_t GetNumEntries() const {
|
|
|
|
return num_entries.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// REQUIRES: called from single-threaded Insert()
|
|
|
|
void IncNumEntries() {
|
|
|
|
// Only one thread can do write at one time. No need to do atomic
|
|
|
|
// incremental. Update it with relaxed load and store.
|
|
|
|
num_entries.store(GetNumEntries() + 1, std::memory_order_relaxed);
|
|
|
|
}
|
2014-07-01 18:05:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// A data structure used as the header of a skip list of a hash bucket.
|
|
|
|
struct SkipListBucketHeader {
|
|
|
|
BucketHeader Counting_header;
|
|
|
|
MemtableSkipList skip_list;
|
|
|
|
|
|
|
|
explicit SkipListBucketHeader(const MemTableRep::KeyComparator& cmp,
|
2017-06-02 21:13:59 +00:00
|
|
|
Allocator* allocator, uint32_t count)
|
2014-07-01 18:05:05 +00:00
|
|
|
: Counting_header(this, // Pointing to itself to indicate header type.
|
|
|
|
count),
|
2014-12-02 20:09:20 +00:00
|
|
|
skip_list(cmp, allocator) {}
|
2014-07-01 18:05:05 +00:00
|
|
|
};
|
2013-12-27 20:56:27 +00:00
|
|
|
|
|
|
|
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.
|
2014-10-27 21:50:21 +00:00
|
|
|
return next_.load(std::memory_order_acquire);
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
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.
|
2014-10-27 21:50:21 +00:00
|
|
|
next_.store(x, std::memory_order_release);
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
// No-barrier variants that can be safely used in a few locations.
|
2022-10-28 20:16:50 +00:00
|
|
|
Node* NoBarrier_Next() { return next_.load(std::memory_order_relaxed); }
|
2014-04-04 22:37:28 +00:00
|
|
|
|
2014-10-27 21:50:21 +00:00
|
|
|
void NoBarrier_SetNext(Node* x) { next_.store(x, std::memory_order_relaxed); }
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2015-07-01 23:13:49 +00:00
|
|
|
// Needed for placement new below which is fine
|
|
|
|
Node() {}
|
|
|
|
|
2014-04-04 22:37:28 +00:00
|
|
|
private:
|
2014-10-27 21:50:21 +00:00
|
|
|
std::atomic<Node*> next_;
|
|
|
|
|
2015-07-01 23:13:49 +00:00
|
|
|
// Prohibit copying due to the below
|
|
|
|
Node(const Node&) = delete;
|
|
|
|
Node& operator=(const Node&) = delete;
|
|
|
|
|
2014-04-04 22:37:28 +00:00
|
|
|
public:
|
2015-07-01 23:13:49 +00:00
|
|
|
char key[1];
|
2013-12-27 20:56:27 +00:00
|
|
|
};
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
// Memory structure of the mem table:
|
|
|
|
// It is a hash table, each bucket points to one entry, a linked list or a
|
|
|
|
// skip list. In order to track total number of records in a bucket to determine
|
|
|
|
// whether should switch to skip list, a header is added just to indicate
|
|
|
|
// number of entries in the bucket.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// +-----> NULL Case 1. Empty bucket
|
|
|
|
// |
|
|
|
|
// |
|
|
|
|
// | +---> +-------+
|
|
|
|
// | | | Next +--> NULL
|
|
|
|
// | | +-------+
|
|
|
|
// +-----+ | | | | Case 2. One Entry in bucket.
|
|
|
|
// | +-+ | | Data | next pointer points to
|
|
|
|
// +-----+ | | | NULL. All other cases
|
|
|
|
// | | | | | next pointer is not NULL.
|
|
|
|
// +-----+ | +-------+
|
|
|
|
// | +---+
|
|
|
|
// +-----+ +-> +-------+ +> +-------+ +-> +-------+
|
|
|
|
// | | | | Next +--+ | Next +--+ | Next +-->NULL
|
|
|
|
// +-----+ | +-------+ +-------+ +-------+
|
|
|
|
// | +-----+ | Count | | | | |
|
|
|
|
// +-----+ +-------+ | Data | | Data |
|
|
|
|
// | | | | | |
|
|
|
|
// +-----+ Case 3. | | | |
|
|
|
|
// | | A header +-------+ +-------+
|
|
|
|
// +-----+ points to
|
|
|
|
// | | a linked list. Count indicates total number
|
|
|
|
// +-----+ of rows in this bucket.
|
|
|
|
// | |
|
|
|
|
// +-----+ +-> +-------+ <--+
|
|
|
|
// | | | | Next +----+
|
|
|
|
// +-----+ | +-------+ Case 4. A header points to a skip
|
|
|
|
// | +----+ | Count | list and next pointer points to
|
|
|
|
// +-----+ +-------+ itself, to distinguish case 3 or 4.
|
|
|
|
// | | | | Count still is kept to indicates total
|
|
|
|
// +-----+ | Skip +--> of entries in the bucket for debugging
|
|
|
|
// | | | List | Data purpose.
|
|
|
|
// | | | +-->
|
|
|
|
// +-----+ | |
|
|
|
|
// | | +-------+
|
|
|
|
// +-----+
|
|
|
|
//
|
|
|
|
// We don't have data race when changing cases because:
|
|
|
|
// (1) When changing from case 2->3, we create a new bucket header, put the
|
|
|
|
// single node there first without changing the original node, and do a
|
|
|
|
// release store when changing the bucket pointer. In that case, a reader
|
|
|
|
// who sees a stale value of the bucket pointer will read this node, while
|
|
|
|
// a reader sees the correct value because of the release store.
|
|
|
|
// (2) When changing case 3->4, a new header is created with skip list points
|
|
|
|
// to the data, before doing an acquire store to change the bucket pointer.
|
|
|
|
// The old header and nodes are never changed, so any reader sees any
|
|
|
|
// of those existing pointers will guarantee to be able to iterate to the
|
|
|
|
// end of the linked list.
|
|
|
|
// (3) Header's next pointer in case 3 might change, but they are never equal
|
|
|
|
// to itself, so no matter a reader sees any stale or newer value, it will
|
|
|
|
// be able to correctly distinguish case 3 and 4.
|
|
|
|
//
|
|
|
|
// The reason that we use case 2 is we want to make the format to be efficient
|
|
|
|
// when the utilization of buckets is relatively low. If we use case 3 for
|
|
|
|
// single entry bucket, we will need to waste 12 bytes for every entry,
|
|
|
|
// which can be significant decrease of memory utilization.
|
2013-12-27 20:56:27 +00:00
|
|
|
class HashLinkListRep : public MemTableRep {
|
|
|
|
public:
|
2014-12-02 20:09:20 +00:00
|
|
|
HashLinkListRep(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, uint32_t threshold_use_skiplist,
|
|
|
|
size_t huge_page_tlb_size, Logger* logger,
|
|
|
|
int bucket_entries_logging_threshold,
|
2014-06-17 22:58:35 +00:00
|
|
|
bool if_log_bucket_dist_when_flash);
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
KeyHandle Allocate(const size_t len, char** buf) override;
|
2014-04-04 22:37:28 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
void Insert(KeyHandle handle) override;
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
bool Contains(const char* key) const override;
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
size_t ApproximateMemoryUsage() override;
|
2013-12-27 20:56:27 +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
|
|
|
~HashLinkListRep() override;
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
MemTableRep::Iterator* GetIterator(Arena* arena = nullptr) override;
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
size_t bucket_size_;
|
|
|
|
|
|
|
|
// Maps slices (which are transformed user keys) to buckets of keys sharing
|
|
|
|
// the same transform.
|
2014-10-27 21:50:21 +00:00
|
|
|
Pointer* buckets_;
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
const uint32_t threshold_use_skiplist_;
|
|
|
|
|
2013-12-27 20:56:27 +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-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-07-01 18:05:05 +00:00
|
|
|
bool LinkListContains(Node* head, const Slice& key) const;
|
|
|
|
|
2022-07-25 18:33:28 +00:00
|
|
|
bool IsEmptyBucket(Pointer& bucket_pointer) const {
|
|
|
|
return bucket_pointer.load(std::memory_order_acquire) == nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Precondition: GetLinkListFirstNode() must have been called first and return
|
|
|
|
// null so that it must be a skip list bucket
|
|
|
|
SkipListBucketHeader* GetSkipListBucketHeader(Pointer& bucket_pointer) const;
|
2014-07-01 18:05:05 +00:00
|
|
|
|
2022-07-25 18:33:28 +00:00
|
|
|
// Returning nullptr indicates it is a skip list bucket.
|
|
|
|
Node* GetLinkListFirstNode(Pointer& bucket_pointer) const;
|
2014-01-25 01:50:59 +00:00
|
|
|
|
|
|
|
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 {
|
2020-09-28 18:33:31 +00:00
|
|
|
return GetSliceRangedNPHash(slice, bucket_size_);
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 18:33:28 +00:00
|
|
|
Pointer& GetBucket(size_t i) const { return buckets_[i]; }
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2022-07-25 18:33:28 +00:00
|
|
|
Pointer& GetBucket(const Slice& slice) const {
|
2013-12-27 20:56:27 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-09-28 01:20:57 +00:00
|
|
|
bool KeyIsAfterOrAtNode(const Slice& internal_key, const Node* n) const {
|
|
|
|
// nullptr n is considered infinite
|
|
|
|
return (n != nullptr) && (compare_(n->key, internal_key) <= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KeyIsAfterOrAtNode(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;
|
2016-09-28 01:20:57 +00:00
|
|
|
Node* FindLessOrEqualInBucket(Node* head, const Slice& key) const;
|
2013-12-27 20:56:27 +00:00
|
|
|
|
|
|
|
class FullListIterator : public MemTableRep::Iterator {
|
|
|
|
public:
|
2014-12-02 20:09:20 +00:00
|
|
|
explicit FullListIterator(MemtableSkipList* list, Allocator* allocator)
|
|
|
|
: iter_(list), full_list_(list), allocator_(allocator) {}
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
~FullListIterator() override {}
|
2013-12-27 20:56:27 +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 iter_.Valid(); }
|
2013-12-27 20:56:27 +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-12-27 20:56:27 +00:00
|
|
|
assert(Valid());
|
|
|
|
return iter_.key();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the next position.
|
|
|
|
// REQUIRES: Valid()
|
2019-02-14 21:52:47 +00:00
|
|
|
void Next() override {
|
2013-12-27 20:56:27 +00:00
|
|
|
assert(Valid());
|
|
|
|
iter_.Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the previous position.
|
|
|
|
// REQUIRES: Valid()
|
2019-02-14 21:52:47 +00:00
|
|
|
void Prev() override {
|
2013-12-27 20:56:27 +00:00
|
|
|
assert(Valid());
|
|
|
|
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 {
|
2022-10-28 20:16:50 +00:00
|
|
|
const char* encoded_key = (memtable_key != nullptr)
|
|
|
|
? memtable_key
|
|
|
|
: EncodeKey(&tmp_, internal_key);
|
2013-12-27 20:56:27 +00:00
|
|
|
iter_.Seek(encoded_key);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
const char* encoded_key = (memtable_key != nullptr)
|
|
|
|
? memtable_key
|
|
|
|
: EncodeKey(&tmp_, internal_key);
|
|
|
|
iter_.SeekForPrev(encoded_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.
|
2019-02-14 21:52:47 +00:00
|
|
|
void SeekToFirst() override { iter_.SeekToFirst(); }
|
2013-12-27 20:56:27 +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 { iter_.SeekToLast(); }
|
|
|
|
|
2013-12-27 20:56:27 +00:00
|
|
|
private:
|
2014-07-01 18:05:05 +00:00
|
|
|
MemtableSkipList::Iterator iter_;
|
2014-01-10 06:25:03 +00:00
|
|
|
// To destruct with the iterator.
|
2014-07-01 18:05:05 +00:00
|
|
|
std::unique_ptr<MemtableSkipList> full_list_;
|
2014-12-02 20:09:20 +00:00
|
|
|
std::unique_ptr<Allocator> allocator_;
|
2022-10-28 20:16:50 +00:00
|
|
|
std::string tmp_; // For passing to EncodeKey
|
2013-12-27 20:56:27 +00:00
|
|
|
};
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
class LinkListIterator : public MemTableRep::Iterator {
|
2013-12-27 20:56:27 +00:00
|
|
|
public:
|
2014-07-01 18:05:05 +00:00
|
|
|
explicit LinkListIterator(const HashLinkListRep* const hash_link_list_rep,
|
|
|
|
Node* head)
|
|
|
|
: hash_link_list_rep_(hash_link_list_rep),
|
|
|
|
head_(head),
|
|
|
|
node_(nullptr) {}
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
~LinkListIterator() override {}
|
2013-12-27 20:56:27 +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 node_ != nullptr; }
|
2013-12-27 20:56:27 +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-12-27 20:56:27 +00:00
|
|
|
assert(Valid());
|
|
|
|
return node_->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the next position.
|
|
|
|
// REQUIRES: Valid()
|
2019-02-14 21:52:47 +00:00
|
|
|
void Next() override {
|
2013-12-27 20:56:27 +00:00
|
|
|
assert(Valid());
|
|
|
|
node_ = node_->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances to the previous position.
|
|
|
|
// REQUIRES: Valid()
|
2019-02-14 21:52:47 +00:00
|
|
|
void Prev() override {
|
2013-12-27 20:56:27 +00:00
|
|
|
// 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
|
2019-02-14 21:52:47 +00:00
|
|
|
void Seek(const Slice& internal_key,
|
|
|
|
const char* /*memtable_key*/) override {
|
2022-10-28 20:16:50 +00:00
|
|
|
node_ =
|
|
|
|
hash_link_list_rep_->FindGreaterOrEqualInBucket(head_, internal_key);
|
2013-12-27 20:56:27 +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
|
|
|
// Since we do not support Prev()
|
|
|
|
// We simply do not support SeekForPrev
|
|
|
|
Reset(nullptr);
|
|
|
|
}
|
|
|
|
|
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.
|
2019-02-14 21:52:47 +00:00
|
|
|
void SeekToFirst() override {
|
2013-12-27 20:56:27 +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-12-27 20:56:27 +00:00
|
|
|
// 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;
|
|
|
|
}
|
2022-10-28 20:16:50 +00:00
|
|
|
|
2013-12-27 20:56:27 +00:00
|
|
|
private:
|
|
|
|
friend class HashLinkListRep;
|
|
|
|
const HashLinkListRep* const hash_link_list_rep_;
|
|
|
|
Node* head_;
|
|
|
|
Node* node_;
|
|
|
|
|
2022-10-28 20:16:50 +00:00
|
|
|
virtual void SeekToHead() { node_ = head_; }
|
2013-12-27 20:56:27 +00:00
|
|
|
};
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
class DynamicIterator : public HashLinkListRep::LinkListIterator {
|
2013-12-27 20:56:27 +00:00
|
|
|
public:
|
|
|
|
explicit DynamicIterator(HashLinkListRep& memtable_rep)
|
2014-07-01 18:05:05 +00:00
|
|
|
: HashLinkListRep::LinkListIterator(&memtable_rep, nullptr),
|
|
|
|
memtable_rep_(memtable_rep) {}
|
2013-12-27 20:56:27 +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_.GetPrefix(k);
|
2022-07-25 18:33:28 +00:00
|
|
|
Pointer& bucket = memtable_rep_.GetBucket(transformed);
|
|
|
|
|
|
|
|
if (memtable_rep_.IsEmptyBucket(bucket)) {
|
|
|
|
skip_list_iter_.reset();
|
|
|
|
Reset(nullptr);
|
|
|
|
} else {
|
|
|
|
Node* first_linked_list_node =
|
|
|
|
memtable_rep_.GetLinkListFirstNode(bucket);
|
|
|
|
if (first_linked_list_node != nullptr) {
|
|
|
|
// The bucket is organized as a linked list
|
|
|
|
skip_list_iter_.reset();
|
|
|
|
Reset(first_linked_list_node);
|
|
|
|
HashLinkListRep::LinkListIterator::Seek(k, memtable_key);
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
} else {
|
2022-07-25 18:33:28 +00:00
|
|
|
SkipListBucketHeader* skip_list_header =
|
|
|
|
memtable_rep_.GetSkipListBucketHeader(bucket);
|
|
|
|
assert(skip_list_header != nullptr);
|
|
|
|
// The bucket is organized as a skip list
|
|
|
|
if (!skip_list_iter_) {
|
|
|
|
skip_list_iter_.reset(
|
|
|
|
new MemtableSkipList::Iterator(&skip_list_header->skip_list));
|
|
|
|
} else {
|
|
|
|
skip_list_iter_->SetList(&skip_list_header->skip_list);
|
|
|
|
}
|
|
|
|
if (memtable_key != nullptr) {
|
|
|
|
skip_list_iter_->Seek(memtable_key);
|
|
|
|
} else {
|
|
|
|
IterKey encoded_key;
|
|
|
|
encoded_key.EncodeLengthPrefixedKey(k);
|
|
|
|
skip_list_iter_->Seek(encoded_key.GetUserKey().data());
|
|
|
|
}
|
2014-07-01 18:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
bool Valid() const override {
|
2014-07-01 18:05:05 +00:00
|
|
|
if (skip_list_iter_) {
|
|
|
|
return skip_list_iter_->Valid();
|
|
|
|
}
|
|
|
|
return HashLinkListRep::LinkListIterator::Valid();
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
const char* key() const override {
|
2014-07-01 18:05:05 +00:00
|
|
|
if (skip_list_iter_) {
|
|
|
|
return skip_list_iter_->key();
|
|
|
|
}
|
|
|
|
return HashLinkListRep::LinkListIterator::key();
|
|
|
|
}
|
|
|
|
|
2019-02-14 21:52:47 +00:00
|
|
|
void Next() override {
|
2014-07-01 18:05:05 +00:00
|
|
|
if (skip_list_iter_) {
|
|
|
|
skip_list_iter_->Next();
|
|
|
|
} else {
|
|
|
|
HashLinkListRep::LinkListIterator::Next();
|
|
|
|
}
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// the underlying memtable
|
|
|
|
const HashLinkListRep& memtable_rep_;
|
2014-07-01 18:05:05 +00:00
|
|
|
std::unique_ptr<MemtableSkipList::Iterator> skip_list_iter_;
|
2013-12-27 20:56:27 +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-12-27 20:56:27 +00:00
|
|
|
assert(false);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-02-14 21:52:47 +00:00
|
|
|
void Next() override {}
|
|
|
|
void Prev() override {}
|
|
|
|
void Seek(const Slice& /*user_key*/,
|
|
|
|
const char* /*memtable_key*/) override {}
|
|
|
|
void SeekForPrev(const Slice& /*user_key*/,
|
|
|
|
const char* /*memtable_key*/) override {}
|
|
|
|
void SeekToFirst() override {}
|
|
|
|
void SeekToLast() override {}
|
2015-02-26 19:28:41 +00:00
|
|
|
|
2013-12-27 20:56:27 +00:00
|
|
|
private:
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-06-02 21:13:59 +00:00
|
|
|
HashLinkListRep::HashLinkListRep(
|
|
|
|
const MemTableRep::KeyComparator& compare, Allocator* allocator,
|
|
|
|
const SliceTransform* transform, size_t bucket_size,
|
|
|
|
uint32_t threshold_use_skiplist, size_t huge_page_tlb_size, Logger* logger,
|
|
|
|
int bucket_entries_logging_threshold, bool if_log_bucket_dist_when_flash)
|
2014-12-02 20:09:20 +00:00
|
|
|
: MemTableRep(allocator),
|
2014-05-04 20:55:53 +00:00
|
|
|
bucket_size_(bucket_size),
|
2014-07-01 18:05:05 +00:00
|
|
|
// Threshold to use skip list doesn't make sense if less than 3, so we
|
|
|
|
// force it to be minimum of 3 to simplify implementation.
|
|
|
|
threshold_use_skiplist_(std::max(threshold_use_skiplist, 3U)),
|
2014-05-04 20:55:53 +00:00
|
|
|
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-12-02 20:09:20 +00:00
|
|
|
char* mem = allocator_->AllocateAligned(sizeof(Pointer) * bucket_size,
|
2022-10-28 20:16:50 +00:00
|
|
|
huge_page_tlb_size, logger);
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2014-10-27 21:50:21 +00:00
|
|
|
buckets_ = new (mem) Pointer[bucket_size];
|
2013-12-27 20:56:27 +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-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 20:16:50 +00:00
|
|
|
HashLinkListRep::~HashLinkListRep() {}
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2014-04-04 22:37:28 +00:00
|
|
|
KeyHandle HashLinkListRep::Allocate(const size_t len, char** buf) {
|
2014-12-02 20:09:20 +00:00
|
|
|
char* mem = allocator_->AllocateAligned(sizeof(Node) + len);
|
2014-04-04 22:37:28 +00:00
|
|
|
Node* x = new (mem) Node();
|
|
|
|
*buf = x->key;
|
|
|
|
return static_cast<void*>(x);
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
SkipListBucketHeader* HashLinkListRep::GetSkipListBucketHeader(
|
2022-07-25 18:33:28 +00:00
|
|
|
Pointer& bucket_pointer) const {
|
|
|
|
Pointer* first_next_pointer =
|
|
|
|
static_cast<Pointer*>(bucket_pointer.load(std::memory_order_acquire));
|
|
|
|
assert(first_next_pointer != nullptr);
|
|
|
|
assert(first_next_pointer->load(std::memory_order_relaxed) != nullptr);
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
// Counting header
|
|
|
|
BucketHeader* header = reinterpret_cast<BucketHeader*>(first_next_pointer);
|
2022-07-25 18:33:28 +00:00
|
|
|
assert(header->IsSkipListBucket());
|
|
|
|
assert(header->GetNumEntries() > threshold_use_skiplist_);
|
|
|
|
auto* skip_list_bucket_header =
|
|
|
|
reinterpret_cast<SkipListBucketHeader*>(header);
|
|
|
|
assert(skip_list_bucket_header->Counting_header.next.load(
|
|
|
|
std::memory_order_relaxed) == header);
|
|
|
|
return skip_list_bucket_header;
|
2014-07-01 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 18:33:28 +00:00
|
|
|
Node* HashLinkListRep::GetLinkListFirstNode(Pointer& bucket_pointer) const {
|
|
|
|
Pointer* first_next_pointer =
|
|
|
|
static_cast<Pointer*>(bucket_pointer.load(std::memory_order_acquire));
|
|
|
|
assert(first_next_pointer != nullptr);
|
2014-10-27 21:50:21 +00:00
|
|
|
if (first_next_pointer->load(std::memory_order_relaxed) == nullptr) {
|
2014-07-01 18:05:05 +00:00
|
|
|
// Single entry bucket
|
|
|
|
return reinterpret_cast<Node*>(first_next_pointer);
|
|
|
|
}
|
2022-07-25 18:33:28 +00:00
|
|
|
|
|
|
|
// It is possible that after we fetch first_next_pointer it is modified
|
|
|
|
// and the next is not null anymore. In this case, the bucket should have been
|
|
|
|
// modified to a counting header, so we should reload the first_next_pointer
|
|
|
|
// to make sure we see the update.
|
|
|
|
first_next_pointer =
|
|
|
|
static_cast<Pointer*>(bucket_pointer.load(std::memory_order_acquire));
|
2014-07-01 18:05:05 +00:00
|
|
|
// Counting header
|
|
|
|
BucketHeader* header = reinterpret_cast<BucketHeader*>(first_next_pointer);
|
|
|
|
if (!header->IsSkipListBucket()) {
|
2015-01-28 23:16:25 +00:00
|
|
|
assert(header->GetNumEntries() <= threshold_use_skiplist_);
|
2014-10-27 21:50:21 +00:00
|
|
|
return reinterpret_cast<Node*>(
|
2015-01-28 23:16:25 +00:00
|
|
|
header->next.load(std::memory_order_acquire));
|
2014-07-01 18:05:05 +00:00
|
|
|
}
|
2015-01-28 23:16:25 +00:00
|
|
|
assert(header->GetNumEntries() > threshold_use_skiplist_);
|
2014-07-01 18:05:05 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-02-16 01:12:48 +00:00
|
|
|
void HashLinkListRep::Insert(KeyHandle handle) {
|
2014-04-04 22:37:28 +00:00
|
|
|
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)];
|
2014-10-27 21:50:21 +00:00
|
|
|
Pointer* first_next_pointer =
|
|
|
|
static_cast<Pointer*>(bucket.load(std::memory_order_relaxed));
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
if (first_next_pointer == nullptr) {
|
|
|
|
// Case 1. empty bucket
|
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(nullptr);
|
2014-10-27 21:50:21 +00:00
|
|
|
bucket.store(x, std::memory_order_release);
|
2018-02-16 01:12:48 +00:00
|
|
|
return;
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
BucketHeader* header = nullptr;
|
2014-10-27 21:50:21 +00:00
|
|
|
if (first_next_pointer->load(std::memory_order_relaxed) == nullptr) {
|
2014-07-01 18:05:05 +00:00
|
|
|
// Case 2. only one entry in the bucket
|
|
|
|
// Need to convert to a Counting bucket and turn to case 4.
|
|
|
|
Node* first = reinterpret_cast<Node*>(first_next_pointer);
|
|
|
|
// Need to add a bucket header.
|
|
|
|
// We have to first convert it to a bucket with header before inserting
|
|
|
|
// the new node. Otherwise, we might need to change next pointer of first.
|
|
|
|
// In that case, a reader might sees the next pointer is NULL and wrongly
|
|
|
|
// think the node is a bucket header.
|
2014-12-02 20:09:20 +00:00
|
|
|
auto* mem = allocator_->AllocateAligned(sizeof(BucketHeader));
|
2014-07-01 18:05:05 +00:00
|
|
|
header = new (mem) BucketHeader(first, 1);
|
2014-10-27 21:50:21 +00:00
|
|
|
bucket.store(header, std::memory_order_release);
|
2014-07-01 18:05:05 +00:00
|
|
|
} else {
|
|
|
|
header = reinterpret_cast<BucketHeader*>(first_next_pointer);
|
|
|
|
if (header->IsSkipListBucket()) {
|
|
|
|
// Case 4. Bucket is already a skip list
|
2015-01-28 23:16:25 +00:00
|
|
|
assert(header->GetNumEntries() > threshold_use_skiplist_);
|
2014-07-01 18:05:05 +00:00
|
|
|
auto* skip_list_bucket_header =
|
|
|
|
reinterpret_cast<SkipListBucketHeader*>(header);
|
2015-01-28 23:16:25 +00:00
|
|
|
// Only one thread can execute Insert() at one time. No need to do atomic
|
|
|
|
// incremental.
|
|
|
|
skip_list_bucket_header->Counting_header.IncNumEntries();
|
2014-07-01 18:05:05 +00:00
|
|
|
skip_list_bucket_header->skip_list.Insert(x->key);
|
2018-02-16 01:12:48 +00:00
|
|
|
return;
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
if (bucket_entries_logging_threshold_ > 0 &&
|
2015-01-28 23:16:25 +00:00
|
|
|
header->GetNumEntries() ==
|
2014-07-01 18:05:05 +00:00
|
|
|
static_cast<uint32_t>(bucket_entries_logging_threshold_)) {
|
2022-10-28 20:16:50 +00:00
|
|
|
Info(logger_,
|
|
|
|
"HashLinkedList bucket %" ROCKSDB_PRIszt
|
|
|
|
" has more than %d "
|
|
|
|
"entries. Key to insert: %s",
|
2015-01-28 23:16:25 +00:00
|
|
|
GetHash(transformed), header->GetNumEntries(),
|
2014-07-01 18:05:05 +00:00
|
|
|
GetLengthPrefixedSlice(x->key).ToString(true).c_str());
|
|
|
|
}
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2015-01-28 23:16:25 +00:00
|
|
|
if (header->GetNumEntries() == threshold_use_skiplist_) {
|
2014-07-01 18:05:05 +00:00
|
|
|
// Case 3. number of entries reaches the threshold so need to convert to
|
|
|
|
// skip list.
|
|
|
|
LinkListIterator bucket_iter(
|
2014-10-27 21:50:21 +00:00
|
|
|
this, reinterpret_cast<Node*>(
|
|
|
|
first_next_pointer->load(std::memory_order_relaxed)));
|
2014-12-02 20:09:20 +00:00
|
|
|
auto mem = allocator_->AllocateAligned(sizeof(SkipListBucketHeader));
|
2014-07-01 18:05:05 +00:00
|
|
|
SkipListBucketHeader* new_skip_list_header = new (mem)
|
2015-01-28 23:16:25 +00:00
|
|
|
SkipListBucketHeader(compare_, allocator_, header->GetNumEntries() + 1);
|
2014-07-01 18:05:05 +00:00
|
|
|
auto& skip_list = new_skip_list_header->skip_list;
|
|
|
|
|
|
|
|
// Add all current entries to the skip list
|
|
|
|
for (bucket_iter.SeekToHead(); bucket_iter.Valid(); bucket_iter.Next()) {
|
|
|
|
skip_list.Insert(bucket_iter.key());
|
|
|
|
}
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
// insert the new entry
|
|
|
|
skip_list.Insert(x->key);
|
|
|
|
// Set the bucket
|
2014-10-27 21:50:21 +00:00
|
|
|
bucket.store(new_skip_list_header, std::memory_order_release);
|
2013-12-27 20:56:27 +00:00
|
|
|
} else {
|
2014-07-01 18:05:05 +00:00
|
|
|
// Case 5. Need to insert to the sorted linked list without changing the
|
|
|
|
// header.
|
2014-10-27 21:50:21 +00:00
|
|
|
Node* first =
|
|
|
|
reinterpret_cast<Node*>(header->next.load(std::memory_order_relaxed));
|
2014-07-01 18:05:05 +00:00
|
|
|
assert(first != nullptr);
|
|
|
|
// Advance counter unless the bucket needs to be advanced to skip list.
|
|
|
|
// In that case, we need to make sure the previous count never exceeds
|
|
|
|
// threshold_use_skiplist_ to avoid readers to cast to wrong format.
|
2015-01-28 23:16:25 +00:00
|
|
|
header->IncNumEntries();
|
2014-07-01 18:05:05 +00:00
|
|
|
|
|
|
|
Node* cur = first;
|
|
|
|
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 == first) || (next == nullptr) ||
|
|
|
|
KeyIsAfterNode(next->key, cur));
|
|
|
|
if (KeyIsAfterNode(internal_key, cur)) {
|
|
|
|
// Keep searching in this list
|
|
|
|
prev = cur;
|
|
|
|
cur = next;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Our data structure does not allow duplicate insertion
|
|
|
|
assert(cur == nullptr || !Equal(x->key, cur->key));
|
|
|
|
|
|
|
|
// 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 {
|
2014-10-27 21:50:21 +00:00
|
|
|
header->next.store(static_cast<void*>(x), std::memory_order_release);
|
2014-07-01 18:05:05 +00:00
|
|
|
}
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-07-25 18:33:28 +00:00
|
|
|
Pointer& bucket = GetBucket(transformed);
|
|
|
|
if (IsEmptyBucket(bucket)) {
|
2013-12-27 20:56:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-07-01 18:05:05 +00:00
|
|
|
|
2022-07-25 18:33:28 +00:00
|
|
|
Node* linked_list_node = GetLinkListFirstNode(bucket);
|
|
|
|
if (linked_list_node != nullptr) {
|
|
|
|
return LinkListContains(linked_list_node, internal_key);
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:05:05 +00:00
|
|
|
SkipListBucketHeader* skip_list_header = GetSkipListBucketHeader(bucket);
|
|
|
|
if (skip_list_header != nullptr) {
|
|
|
|
return skip_list_header->skip_list.Contains(key);
|
|
|
|
}
|
2022-07-25 18:33:28 +00:00
|
|
|
return false;
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t HashLinkListRep::ApproximateMemoryUsage() {
|
2014-12-02 20:09:20 +00:00
|
|
|
// Memory is always allocated from the allocator.
|
2013-12-27 20:56:27 +00:00
|
|
|
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());
|
2022-07-25 18:33:28 +00:00
|
|
|
Pointer& bucket = GetBucket(transformed);
|
2014-07-01 18:05:05 +00:00
|
|
|
|
2022-07-25 18:33:28 +00:00
|
|
|
if (IsEmptyBucket(bucket)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto* link_list_head = GetLinkListFirstNode(bucket);
|
|
|
|
if (link_list_head != nullptr) {
|
|
|
|
LinkListIterator iter(this, link_list_head);
|
|
|
|
for (iter.Seek(k.internal_key(), nullptr);
|
2014-02-11 17:46:30 +00:00
|
|
|
iter.Valid() && callback_func(callback_args, iter.key());
|
|
|
|
iter.Next()) {
|
|
|
|
}
|
2014-07-01 18:05:05 +00:00
|
|
|
} else {
|
2022-07-25 18:33:28 +00:00
|
|
|
auto* skip_list_header = GetSkipListBucketHeader(bucket);
|
|
|
|
if (skip_list_header != nullptr) {
|
|
|
|
// Is a skip list
|
|
|
|
MemtableSkipList::Iterator iter(&skip_list_header->skip_list);
|
|
|
|
for (iter.Seek(k.memtable_key().data());
|
2014-07-01 18:05:05 +00:00
|
|
|
iter.Valid() && callback_func(callback_args, iter.key());
|
|
|
|
iter.Next()) {
|
|
|
|
}
|
|
|
|
}
|
2014-02-11 17:46:30 +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::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
|
2014-12-02 20:09:20 +00:00
|
|
|
Arena* new_arena = new Arena(allocator_->BlockSize());
|
2014-07-01 18:05:05 +00:00
|
|
|
auto list = new MemtableSkipList(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;
|
2022-07-25 18:33:28 +00:00
|
|
|
Pointer& bucket = GetBucket(i);
|
|
|
|
if (!IsEmptyBucket(bucket)) {
|
|
|
|
auto* link_list_head = GetLinkListFirstNode(bucket);
|
|
|
|
if (link_list_head != nullptr) {
|
|
|
|
LinkListIterator itr(this, link_list_head);
|
|
|
|
for (itr.SeekToHead(); itr.Valid(); itr.Next()) {
|
|
|
|
list->Insert(itr.key());
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto* skip_list_header = GetSkipListBucketHeader(bucket);
|
|
|
|
assert(skip_list_header != nullptr);
|
2014-07-01 18:05:05 +00:00
|
|
|
// Is a skip list
|
|
|
|
MemtableSkipList::Iterator itr(&skip_list_header->skip_list);
|
|
|
|
for (itr.SeekToFirst(); itr.Valid(); itr.Next()) {
|
|
|
|
list->Insert(itr.key());
|
|
|
|
count++;
|
2022-10-28 20:16:50 +00:00
|
|
|
}
|
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-07-01 18:05:05 +00:00
|
|
|
bool HashLinkListRep::LinkListContains(Node* head,
|
|
|
|
const Slice& user_key) const {
|
2014-01-25 01:50:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:45:59 +00:00
|
|
|
struct HashLinkListRepOptions {
|
|
|
|
static const char* kName() { return "HashLinkListRepFactoryOptions"; }
|
|
|
|
size_t bucket_count;
|
|
|
|
uint32_t threshold_use_skiplist;
|
|
|
|
size_t huge_page_tlb_size;
|
|
|
|
int bucket_entries_logging_threshold;
|
|
|
|
bool if_log_bucket_dist_when_flash;
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::unordered_map<std::string, OptionTypeInfo> hash_linklist_info = {
|
|
|
|
{"bucket_count",
|
|
|
|
{offsetof(struct HashLinkListRepOptions, bucket_count), OptionType::kSizeT,
|
|
|
|
OptionVerificationType::kNormal, OptionTypeFlags::kNone}},
|
|
|
|
{"threshold",
|
|
|
|
{offsetof(struct HashLinkListRepOptions, threshold_use_skiplist),
|
|
|
|
OptionType::kUInt32T, OptionVerificationType::kNormal,
|
|
|
|
OptionTypeFlags::kNone}},
|
|
|
|
{"huge_page_size",
|
|
|
|
{offsetof(struct HashLinkListRepOptions, huge_page_tlb_size),
|
|
|
|
OptionType::kSizeT, OptionVerificationType::kNormal,
|
|
|
|
OptionTypeFlags::kNone}},
|
|
|
|
{"logging_threshold",
|
|
|
|
{offsetof(struct HashLinkListRepOptions, bucket_entries_logging_threshold),
|
|
|
|
OptionType::kInt, OptionVerificationType::kNormal,
|
|
|
|
OptionTypeFlags::kNone}},
|
|
|
|
{"log_when_flash",
|
|
|
|
{offsetof(struct HashLinkListRepOptions, if_log_bucket_dist_when_flash),
|
|
|
|
OptionType::kBoolean, OptionVerificationType::kNormal,
|
|
|
|
OptionTypeFlags::kNone}},
|
|
|
|
};
|
|
|
|
|
|
|
|
class HashLinkListRepFactory : public MemTableRepFactory {
|
|
|
|
public:
|
|
|
|
explicit HashLinkListRepFactory(size_t bucket_count,
|
|
|
|
uint32_t threshold_use_skiplist,
|
|
|
|
size_t huge_page_tlb_size,
|
|
|
|
int bucket_entries_logging_threshold,
|
|
|
|
bool if_log_bucket_dist_when_flash) {
|
|
|
|
options_.bucket_count = bucket_count;
|
|
|
|
options_.threshold_use_skiplist = threshold_use_skiplist;
|
|
|
|
options_.huge_page_tlb_size = huge_page_tlb_size;
|
|
|
|
options_.bucket_entries_logging_threshold =
|
|
|
|
bucket_entries_logging_threshold;
|
|
|
|
options_.if_log_bucket_dist_when_flash = if_log_bucket_dist_when_flash;
|
|
|
|
RegisterOptions(&options_, &hash_linklist_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
using MemTableRepFactory::CreateMemTableRep;
|
|
|
|
virtual MemTableRep* CreateMemTableRep(
|
|
|
|
const MemTableRep::KeyComparator& compare, Allocator* allocator,
|
|
|
|
const SliceTransform* transform, Logger* logger) override;
|
|
|
|
|
|
|
|
static const char* kClassName() { return "HashLinkListRepFactory"; }
|
|
|
|
static const char* kNickName() { return "hash_linkedlist"; }
|
|
|
|
virtual const char* Name() const override { return kClassName(); }
|
|
|
|
virtual const char* NickName() const override { return kNickName(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
HashLinkListRepOptions options_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
2013-12-27 20:56:27 +00:00
|
|
|
|
2014-01-16 07:12:31 +00:00
|
|
|
MemTableRep* HashLinkListRepFactory::CreateMemTableRep(
|
2017-06-02 21:13:59 +00:00
|
|
|
const MemTableRep::KeyComparator& compare, Allocator* allocator,
|
2014-05-04 22:52:23 +00:00
|
|
|
const SliceTransform* transform, Logger* logger) {
|
2021-09-08 14:45:59 +00:00
|
|
|
return new HashLinkListRep(
|
|
|
|
compare, allocator, transform, options_.bucket_count,
|
|
|
|
options_.threshold_use_skiplist, options_.huge_page_tlb_size, logger,
|
|
|
|
options_.bucket_entries_logging_threshold,
|
|
|
|
options_.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,
|
2014-07-01 18:05:05 +00:00
|
|
|
int bucket_entries_logging_threshold, bool if_log_bucket_dist_when_flash,
|
|
|
|
uint32_t threshold_use_skiplist) {
|
|
|
|
return new HashLinkListRepFactory(
|
|
|
|
bucket_count, threshold_use_skiplist, huge_page_tlb_size,
|
|
|
|
bucket_entries_logging_threshold, if_log_bucket_dist_when_flash);
|
2013-12-27 20:56:27 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|