fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
822 lines
28 KiB
822 lines
28 KiB
11 years ago
|
// 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.
|
||
|
//
|
||
|
|
||
11 years ago
|
#ifndef ROCKSDB_LITE
|
||
9 years ago
|
#include "memtable/hash_linklist_rep.h"
|
||
11 years ago
|
|
||
11 years ago
|
#include <algorithm>
|
||
10 years ago
|
#include <atomic>
|
||
11 years ago
|
#include "rocksdb/memtablerep.h"
|
||
11 years ago
|
#include "util/arena.h"
|
||
11 years ago
|
#include "rocksdb/slice.h"
|
||
|
#include "rocksdb/slice_transform.h"
|
||
|
#include "port/port.h"
|
||
11 years ago
|
#include "util/histogram.h"
|
||
11 years ago
|
#include "util/murmurhash.h"
|
||
|
#include "db/memtable.h"
|
||
|
#include "db/skiplist.h"
|
||
|
|
||
|
namespace rocksdb {
|
||
|
namespace {
|
||
|
|
||
|
typedef const char* Key;
|
||
11 years ago
|
typedef SkipList<Key, const MemTableRep::KeyComparator&> MemtableSkipList;
|
||
10 years ago
|
typedef std::atomic<void*> Pointer;
|
||
11 years ago
|
|
||
|
// A data structure used as the header of a link list of a hash bucket.
|
||
|
struct BucketHeader {
|
||
|
Pointer next;
|
||
10 years ago
|
std::atomic<uint32_t> num_entries;
|
||
11 years ago
|
|
||
|
explicit BucketHeader(void* n, uint32_t count)
|
||
|
: next(n), num_entries(count) {}
|
||
|
|
||
10 years ago
|
bool IsSkipListBucket() {
|
||
|
return next.load(std::memory_order_relaxed) == this;
|
||
|
}
|
||
10 years ago
|
|
||
|
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);
|
||
|
}
|
||
11 years ago
|
};
|
||
|
|
||
|
// 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,
|
||
10 years ago
|
MemTableAllocator* allocator, uint32_t count)
|
||
11 years ago
|
: Counting_header(this, // Pointing to itself to indicate header type.
|
||
|
count),
|
||
10 years ago
|
skip_list(cmp, allocator) {}
|
||
11 years ago
|
};
|
||
11 years ago
|
|
||
|
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.
|
||
10 years ago
|
return next_.load(std::memory_order_acquire);
|
||
11 years ago
|
}
|
||
|
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.
|
||
10 years ago
|
next_.store(x, std::memory_order_release);
|
||
11 years ago
|
}
|
||
|
// No-barrier variants that can be safely used in a few locations.
|
||
|
Node* NoBarrier_Next() {
|
||
10 years ago
|
return next_.load(std::memory_order_relaxed);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
10 years ago
|
void NoBarrier_SetNext(Node* x) { next_.store(x, std::memory_order_relaxed); }
|
||
11 years ago
|
|
||
10 years ago
|
// Needed for placement new below which is fine
|
||
|
Node() {}
|
||
|
|
||
11 years ago
|
private:
|
||
10 years ago
|
std::atomic<Node*> next_;
|
||
|
|
||
10 years ago
|
// Prohibit copying due to the below
|
||
|
Node(const Node&) = delete;
|
||
|
Node& operator=(const Node&) = delete;
|
||
|
|
||
11 years ago
|
public:
|
||
10 years ago
|
char key[1];
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
// 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.
|
||
11 years ago
|
class HashLinkListRep : public MemTableRep {
|
||
|
public:
|
||
10 years ago
|
HashLinkListRep(const MemTableRep::KeyComparator& compare,
|
||
|
MemTableAllocator* 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,
|
||
11 years ago
|
bool if_log_bucket_dist_when_flash);
|
||
11 years ago
|
|
||
11 years ago
|
virtual KeyHandle Allocate(const size_t len, char** buf) override;
|
||
|
|
||
|
virtual void Insert(KeyHandle handle) override;
|
||
11 years ago
|
|
||
|
virtual bool Contains(const char* key) const override;
|
||
|
|
||
|
virtual size_t ApproximateMemoryUsage() override;
|
||
|
|
||
11 years ago
|
virtual void Get(const LookupKey& k, void* callback_args,
|
||
|
bool (*callback_func)(void* arg,
|
||
|
const char* entry)) override;
|
||
|
|
||
11 years ago
|
virtual ~HashLinkListRep();
|
||
|
|
||
11 years ago
|
virtual MemTableRep::Iterator* GetIterator(Arena* arena = nullptr) override;
|
||
11 years ago
|
|
||
11 years ago
|
virtual MemTableRep::Iterator* GetDynamicPrefixIterator(
|
||
10 years ago
|
Arena* arena = nullptr) override;
|
||
11 years ago
|
|
||
11 years ago
|
private:
|
||
|
friend class DynamicIterator;
|
||
|
|
||
|
size_t bucket_size_;
|
||
|
|
||
|
// Maps slices (which are transformed user keys) to buckets of keys sharing
|
||
|
// the same transform.
|
||
10 years ago
|
Pointer* buckets_;
|
||
11 years ago
|
|
||
11 years ago
|
const uint32_t threshold_use_skiplist_;
|
||
|
|
||
11 years ago
|
// The user-supplied transform whose domain is the user keys.
|
||
|
const SliceTransform* transform_;
|
||
|
|
||
11 years ago
|
const MemTableRep::KeyComparator& compare_;
|
||
11 years ago
|
|
||
11 years ago
|
Logger* logger_;
|
||
|
int bucket_entries_logging_threshold_;
|
||
|
bool if_log_bucket_dist_when_flash_;
|
||
|
|
||
11 years ago
|
bool LinkListContains(Node* head, const Slice& key) const;
|
||
|
|
||
|
SkipListBucketHeader* GetSkipListBucketHeader(Pointer* first_next_pointer)
|
||
|
const;
|
||
|
|
||
|
Node* GetLinkListFirstNode(Pointer* first_next_pointer) const;
|
||
11 years ago
|
|
||
|
Slice GetPrefix(const Slice& internal_key) const {
|
||
|
return transform_->Transform(ExtractUserKey(internal_key));
|
||
|
}
|
||
11 years ago
|
|
||
|
size_t GetHash(const Slice& slice) const {
|
||
10 years ago
|
return MurmurHash(slice.data(), static_cast<int>(slice.size()), 0) %
|
||
|
bucket_size_;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
Pointer* GetBucket(size_t i) const {
|
||
10 years ago
|
return static_cast<Pointer*>(buckets_[i].load(std::memory_order_acquire));
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
Pointer* GetBucket(const Slice& slice) const {
|
||
11 years ago
|
return GetBucket(GetHash(slice));
|
||
|
}
|
||
|
|
||
11 years ago
|
bool Equal(const Slice& a, const Key& b) const {
|
||
|
return (compare_(b, a) == 0);
|
||
|
}
|
||
|
|
||
11 years ago
|
bool Equal(const Key& a, const Key& b) const { return (compare_(a, b) == 0); }
|
||
|
|
||
11 years ago
|
bool KeyIsAfterNode(const Slice& internal_key, const Node* n) const {
|
||
|
// nullptr n is considered infinite
|
||
|
return (n != nullptr) && (compare_(n->key, internal_key) < 0);
|
||
|
}
|
||
|
|
||
11 years ago
|
bool KeyIsAfterNode(const Key& key, const Node* n) const {
|
||
|
// nullptr n is considered infinite
|
||
|
return (n != nullptr) && (compare_(n->key, key) < 0);
|
||
|
}
|
||
|
|
||
11 years ago
|
|
||
|
Node* FindGreaterOrEqualInBucket(Node* head, const Slice& key) const;
|
||
11 years ago
|
|
||
|
class FullListIterator : public MemTableRep::Iterator {
|
||
|
public:
|
||
10 years ago
|
explicit FullListIterator(MemtableSkipList* list, Allocator* allocator)
|
||
|
: iter_(list), full_list_(list), allocator_(allocator) {}
|
||
11 years ago
|
|
||
|
virtual ~FullListIterator() {
|
||
|
}
|
||
|
|
||
|
// Returns true iff the iterator is positioned at a valid node.
|
||
10 years ago
|
virtual bool Valid() const override { return iter_.Valid(); }
|
||
11 years ago
|
|
||
|
// Returns the key at the current position.
|
||
|
// REQUIRES: Valid()
|
||
10 years ago
|
virtual const char* key() const override {
|
||
11 years ago
|
assert(Valid());
|
||
|
return iter_.key();
|
||
|
}
|
||
|
|
||
|
// Advances to the next position.
|
||
|
// REQUIRES: Valid()
|
||
10 years ago
|
virtual void Next() override {
|
||
11 years ago
|
assert(Valid());
|
||
|
iter_.Next();
|
||
|
}
|
||
|
|
||
|
// Advances to the previous position.
|
||
|
// REQUIRES: Valid()
|
||
10 years ago
|
virtual void Prev() override {
|
||
11 years ago
|
assert(Valid());
|
||
|
iter_.Prev();
|
||
|
}
|
||
|
|
||
|
// Advance to the first entry with a key >= target
|
||
10 years ago
|
virtual void Seek(const Slice& internal_key,
|
||
|
const char* memtable_key) override {
|
||
11 years ago
|
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.
|
||
10 years ago
|
virtual void SeekToFirst() override { iter_.SeekToFirst(); }
|
||
11 years ago
|
|
||
|
// Position at the last entry in collection.
|
||
|
// Final state of iterator is Valid() iff collection is not empty.
|
||
10 years ago
|
virtual void SeekToLast() override { iter_.SeekToLast(); }
|
||
11 years ago
|
private:
|
||
11 years ago
|
MemtableSkipList::Iterator iter_;
|
||
11 years ago
|
// To destruct with the iterator.
|
||
11 years ago
|
std::unique_ptr<MemtableSkipList> full_list_;
|
||
10 years ago
|
std::unique_ptr<Allocator> allocator_;
|
||
11 years ago
|
std::string tmp_; // For passing to EncodeKey
|
||
|
};
|
||
|
|
||
11 years ago
|
class LinkListIterator : public MemTableRep::Iterator {
|
||
11 years ago
|
public:
|
||
11 years ago
|
explicit LinkListIterator(const HashLinkListRep* const hash_link_list_rep,
|
||
|
Node* head)
|
||
|
: hash_link_list_rep_(hash_link_list_rep),
|
||
|
head_(head),
|
||
|
node_(nullptr) {}
|
||
11 years ago
|
|
||
11 years ago
|
virtual ~LinkListIterator() {}
|
||
11 years ago
|
|
||
|
// Returns true iff the iterator is positioned at a valid node.
|
||
10 years ago
|
virtual bool Valid() const override { return node_ != nullptr; }
|
||
11 years ago
|
|
||
|
// Returns the key at the current position.
|
||
|
// REQUIRES: Valid()
|
||
10 years ago
|
virtual const char* key() const override {
|
||
11 years ago
|
assert(Valid());
|
||
|
return node_->key;
|
||
|
}
|
||
|
|
||
|
// Advances to the next position.
|
||
|
// REQUIRES: Valid()
|
||
10 years ago
|
virtual void Next() override {
|
||
11 years ago
|
assert(Valid());
|
||
|
node_ = node_->Next();
|
||
|
}
|
||
|
|
||
|
// Advances to the previous position.
|
||
|
// REQUIRES: Valid()
|
||
10 years ago
|
virtual void Prev() override {
|
||
11 years ago
|
// 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
|
||
10 years ago
|
virtual void Seek(const Slice& internal_key,
|
||
|
const char* memtable_key) override {
|
||
11 years ago
|
node_ = hash_link_list_rep_->FindGreaterOrEqualInBucket(head_,
|
||
11 years ago
|
internal_key);
|
||
11 years ago
|
}
|
||
|
|
||
|
// Position at the first entry in collection.
|
||
|
// Final state of iterator is Valid() iff collection is not empty.
|
||
10 years ago
|
virtual void SeekToFirst() override {
|
||
11 years ago
|
// 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.
|
||
10 years ago
|
virtual void SeekToLast() override {
|
||
11 years ago
|
// 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_;
|
||
|
}
|
||
|
};
|
||
|
|
||
11 years ago
|
class DynamicIterator : public HashLinkListRep::LinkListIterator {
|
||
11 years ago
|
public:
|
||
|
explicit DynamicIterator(HashLinkListRep& memtable_rep)
|
||
11 years ago
|
: HashLinkListRep::LinkListIterator(&memtable_rep, nullptr),
|
||
|
memtable_rep_(memtable_rep) {}
|
||
11 years ago
|
|
||
|
// Advance to the first entry with a key >= target
|
||
10 years ago
|
virtual void Seek(const Slice& k, const char* memtable_key) override {
|
||
11 years ago
|
auto transformed = memtable_rep_.GetPrefix(k);
|
||
11 years ago
|
auto* bucket = memtable_rep_.GetBucket(transformed);
|
||
|
|
||
|
SkipListBucketHeader* skip_list_header =
|
||
|
memtable_rep_.GetSkipListBucketHeader(bucket);
|
||
|
if (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.GetKey().data());
|
||
|
}
|
||
|
} else {
|
||
|
// The bucket is organized as a linked list
|
||
|
skip_list_iter_.reset();
|
||
|
Reset(memtable_rep_.GetLinkListFirstNode(bucket));
|
||
|
HashLinkListRep::LinkListIterator::Seek(k, memtable_key);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
virtual bool Valid() const override {
|
||
11 years ago
|
if (skip_list_iter_) {
|
||
|
return skip_list_iter_->Valid();
|
||
|
}
|
||
|
return HashLinkListRep::LinkListIterator::Valid();
|
||
|
}
|
||
|
|
||
10 years ago
|
virtual const char* key() const override {
|
||
11 years ago
|
if (skip_list_iter_) {
|
||
|
return skip_list_iter_->key();
|
||
|
}
|
||
|
return HashLinkListRep::LinkListIterator::key();
|
||
|
}
|
||
|
|
||
10 years ago
|
virtual void Next() override {
|
||
11 years ago
|
if (skip_list_iter_) {
|
||
|
skip_list_iter_->Next();
|
||
|
} else {
|
||
|
HashLinkListRep::LinkListIterator::Next();
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
private:
|
||
|
// the underlying memtable
|
||
|
const HashLinkListRep& memtable_rep_;
|
||
11 years ago
|
std::unique_ptr<MemtableSkipList::Iterator> skip_list_iter_;
|
||
11 years ago
|
};
|
||
|
|
||
|
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() { }
|
||
10 years ago
|
virtual bool Valid() const override { return false; }
|
||
|
virtual const char* key() const override {
|
||
11 years ago
|
assert(false);
|
||
|
return nullptr;
|
||
|
}
|
||
10 years ago
|
virtual void Next() override {}
|
||
|
virtual void Prev() override {}
|
||
|
virtual void Seek(const Slice& user_key,
|
||
|
const char* memtable_key) override {}
|
||
|
virtual void SeekToFirst() override {}
|
||
|
virtual void SeekToLast() override {}
|
||
|
|
||
11 years ago
|
private:
|
||
|
};
|
||
|
};
|
||
|
|
||
11 years ago
|
HashLinkListRep::HashLinkListRep(const MemTableRep::KeyComparator& compare,
|
||
10 years ago
|
MemTableAllocator* allocator,
|
||
|
const SliceTransform* transform,
|
||
11 years ago
|
size_t bucket_size,
|
||
|
uint32_t threshold_use_skiplist,
|
||
|
size_t huge_page_tlb_size, Logger* logger,
|
||
11 years ago
|
int bucket_entries_logging_threshold,
|
||
|
bool if_log_bucket_dist_when_flash)
|
||
10 years ago
|
: MemTableRep(allocator),
|
||
11 years ago
|
bucket_size_(bucket_size),
|
||
11 years ago
|
// 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)),
|
||
11 years ago
|
transform_(transform),
|
||
11 years ago
|
compare_(compare),
|
||
|
logger_(logger),
|
||
|
bucket_entries_logging_threshold_(bucket_entries_logging_threshold),
|
||
|
if_log_bucket_dist_when_flash_(if_log_bucket_dist_when_flash) {
|
||
10 years ago
|
char* mem = allocator_->AllocateAligned(sizeof(Pointer) * bucket_size,
|
||
11 years ago
|
huge_page_tlb_size, logger);
|
||
11 years ago
|
|
||
10 years ago
|
buckets_ = new (mem) Pointer[bucket_size];
|
||
11 years ago
|
|
||
|
for (size_t i = 0; i < bucket_size_; ++i) {
|
||
10 years ago
|
buckets_[i].store(nullptr, std::memory_order_relaxed);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
HashLinkListRep::~HashLinkListRep() {
|
||
|
}
|
||
|
|
||
11 years ago
|
KeyHandle HashLinkListRep::Allocate(const size_t len, char** buf) {
|
||
10 years ago
|
char* mem = allocator_->AllocateAligned(sizeof(Node) + len);
|
||
11 years ago
|
Node* x = new (mem) Node();
|
||
|
*buf = x->key;
|
||
|
return static_cast<void*>(x);
|
||
|
}
|
||
|
|
||
11 years ago
|
SkipListBucketHeader* HashLinkListRep::GetSkipListBucketHeader(
|
||
|
Pointer* first_next_pointer) const {
|
||
|
if (first_next_pointer == nullptr) {
|
||
|
return nullptr;
|
||
|
}
|
||
10 years ago
|
if (first_next_pointer->load(std::memory_order_relaxed) == nullptr) {
|
||
11 years ago
|
// Single entry bucket
|
||
|
return nullptr;
|
||
|
}
|
||
|
// Counting header
|
||
|
BucketHeader* header = reinterpret_cast<BucketHeader*>(first_next_pointer);
|
||
|
if (header->IsSkipListBucket()) {
|
||
10 years ago
|
assert(header->GetNumEntries() > threshold_use_skiplist_);
|
||
11 years ago
|
auto* skip_list_bucket_header =
|
||
|
reinterpret_cast<SkipListBucketHeader*>(header);
|
||
10 years ago
|
assert(skip_list_bucket_header->Counting_header.next.load(
|
||
|
std::memory_order_relaxed) == header);
|
||
11 years ago
|
return skip_list_bucket_header;
|
||
|
}
|
||
10 years ago
|
assert(header->GetNumEntries() <= threshold_use_skiplist_);
|
||
11 years ago
|
return nullptr;
|
||
|
}
|
||
|
|
||
|
Node* HashLinkListRep::GetLinkListFirstNode(Pointer* first_next_pointer) const {
|
||
|
if (first_next_pointer == nullptr) {
|
||
|
return nullptr;
|
||
|
}
|
||
10 years ago
|
if (first_next_pointer->load(std::memory_order_relaxed) == nullptr) {
|
||
11 years ago
|
// Single entry bucket
|
||
|
return reinterpret_cast<Node*>(first_next_pointer);
|
||
|
}
|
||
|
// Counting header
|
||
|
BucketHeader* header = reinterpret_cast<BucketHeader*>(first_next_pointer);
|
||
|
if (!header->IsSkipListBucket()) {
|
||
10 years ago
|
assert(header->GetNumEntries() <= threshold_use_skiplist_);
|
||
10 years ago
|
return reinterpret_cast<Node*>(
|
||
10 years ago
|
header->next.load(std::memory_order_acquire));
|
||
11 years ago
|
}
|
||
10 years ago
|
assert(header->GetNumEntries() > threshold_use_skiplist_);
|
||
11 years ago
|
return nullptr;
|
||
|
}
|
||
|
|
||
11 years ago
|
void HashLinkListRep::Insert(KeyHandle handle) {
|
||
|
Node* x = static_cast<Node*>(handle);
|
||
|
assert(!Contains(x->key));
|
||
|
Slice internal_key = GetLengthPrefixedSlice(x->key);
|
||
11 years ago
|
auto transformed = GetPrefix(internal_key);
|
||
11 years ago
|
auto& bucket = buckets_[GetHash(transformed)];
|
||
10 years ago
|
Pointer* first_next_pointer =
|
||
|
static_cast<Pointer*>(bucket.load(std::memory_order_relaxed));
|
||
11 years ago
|
|
||
11 years ago
|
if (first_next_pointer == nullptr) {
|
||
|
// Case 1. empty bucket
|
||
11 years ago
|
// NoBarrier_SetNext() suffices since we will add a barrier when
|
||
|
// we publish a pointer to "x" in prev[i].
|
||
|
x->NoBarrier_SetNext(nullptr);
|
||
10 years ago
|
bucket.store(x, std::memory_order_release);
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
BucketHeader* header = nullptr;
|
||
10 years ago
|
if (first_next_pointer->load(std::memory_order_relaxed) == nullptr) {
|
||
11 years ago
|
// 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.
|
||
10 years ago
|
auto* mem = allocator_->AllocateAligned(sizeof(BucketHeader));
|
||
11 years ago
|
header = new (mem) BucketHeader(first, 1);
|
||
10 years ago
|
bucket.store(header, std::memory_order_release);
|
||
11 years ago
|
} else {
|
||
|
header = reinterpret_cast<BucketHeader*>(first_next_pointer);
|
||
|
if (header->IsSkipListBucket()) {
|
||
|
// Case 4. Bucket is already a skip list
|
||
10 years ago
|
assert(header->GetNumEntries() > threshold_use_skiplist_);
|
||
11 years ago
|
auto* skip_list_bucket_header =
|
||
|
reinterpret_cast<SkipListBucketHeader*>(header);
|
||
10 years ago
|
// Only one thread can execute Insert() at one time. No need to do atomic
|
||
|
// incremental.
|
||
|
skip_list_bucket_header->Counting_header.IncNumEntries();
|
||
11 years ago
|
skip_list_bucket_header->skip_list.Insert(x->key);
|
||
|
return;
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
if (bucket_entries_logging_threshold_ > 0 &&
|
||
10 years ago
|
header->GetNumEntries() ==
|
||
11 years ago
|
static_cast<uint32_t>(bucket_entries_logging_threshold_)) {
|
||
10 years ago
|
Info(logger_, "HashLinkedList bucket %" ROCKSDB_PRIszt
|
||
|
" has more than %d "
|
||
|
"entries. Key to insert: %s",
|
||
10 years ago
|
GetHash(transformed), header->GetNumEntries(),
|
||
11 years ago
|
GetLengthPrefixedSlice(x->key).ToString(true).c_str());
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
if (header->GetNumEntries() == threshold_use_skiplist_) {
|
||
11 years ago
|
// Case 3. number of entries reaches the threshold so need to convert to
|
||
|
// skip list.
|
||
|
LinkListIterator bucket_iter(
|
||
10 years ago
|
this, reinterpret_cast<Node*>(
|
||
|
first_next_pointer->load(std::memory_order_relaxed)));
|
||
10 years ago
|
auto mem = allocator_->AllocateAligned(sizeof(SkipListBucketHeader));
|
||
11 years ago
|
SkipListBucketHeader* new_skip_list_header = new (mem)
|
||
10 years ago
|
SkipListBucketHeader(compare_, allocator_, header->GetNumEntries() + 1);
|
||
11 years ago
|
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());
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// insert the new entry
|
||
|
skip_list.Insert(x->key);
|
||
|
// Set the bucket
|
||
10 years ago
|
bucket.store(new_skip_list_header, std::memory_order_release);
|
||
11 years ago
|
} else {
|
||
11 years ago
|
// Case 5. Need to insert to the sorted linked list without changing the
|
||
|
// header.
|
||
10 years ago
|
Node* first =
|
||
|
reinterpret_cast<Node*>(header->next.load(std::memory_order_relaxed));
|
||
11 years ago
|
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.
|
||
10 years ago
|
header->IncNumEntries();
|
||
11 years ago
|
|
||
|
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 {
|
||
10 years ago
|
header->next.store(static_cast<void*>(x), std::memory_order_release);
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
bool HashLinkListRep::Contains(const char* key) const {
|
||
11 years ago
|
Slice internal_key = GetLengthPrefixedSlice(key);
|
||
|
|
||
|
auto transformed = GetPrefix(internal_key);
|
||
11 years ago
|
auto bucket = GetBucket(transformed);
|
||
|
if (bucket == nullptr) {
|
||
|
return false;
|
||
|
}
|
||
11 years ago
|
|
||
|
SkipListBucketHeader* skip_list_header = GetSkipListBucketHeader(bucket);
|
||
|
if (skip_list_header != nullptr) {
|
||
|
return skip_list_header->skip_list.Contains(key);
|
||
|
} else {
|
||
|
return LinkListContains(GetLinkListFirstNode(bucket), internal_key);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
size_t HashLinkListRep::ApproximateMemoryUsage() {
|
||
10 years ago
|
// Memory is always allocated from the allocator.
|
||
11 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
11 years ago
|
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);
|
||
11 years ago
|
|
||
|
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());
|
||
11 years ago
|
iter.Valid() && callback_func(callback_args, iter.key());
|
||
|
iter.Next()) {
|
||
|
}
|
||
11 years ago
|
} else {
|
||
|
auto* link_list_head = GetLinkListFirstNode(bucket);
|
||
|
if (link_list_head != nullptr) {
|
||
|
LinkListIterator iter(this, link_list_head);
|
||
|
for (iter.Seek(k.internal_key(), nullptr);
|
||
|
iter.Valid() && callback_func(callback_args, iter.key());
|
||
|
iter.Next()) {
|
||
|
}
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
MemTableRep::Iterator* HashLinkListRep::GetIterator(Arena* alloc_arena) {
|
||
11 years ago
|
// allocate a new arena of similar size to the one currently in use
|
||
10 years ago
|
Arena* new_arena = new Arena(allocator_->BlockSize());
|
||
11 years ago
|
auto list = new MemtableSkipList(compare_, new_arena);
|
||
11 years ago
|
HistogramImpl keys_per_bucket_hist;
|
||
|
|
||
11 years ago
|
for (size_t i = 0; i < bucket_size_; ++i) {
|
||
11 years ago
|
int count = 0;
|
||
11 years ago
|
auto* bucket = GetBucket(i);
|
||
11 years ago
|
if (bucket != nullptr) {
|
||
11 years ago
|
auto* skip_list_header = GetSkipListBucketHeader(bucket);
|
||
|
if (skip_list_header != nullptr) {
|
||
|
// Is a skip list
|
||
|
MemtableSkipList::Iterator itr(&skip_list_header->skip_list);
|
||
|
for (itr.SeekToFirst(); itr.Valid(); itr.Next()) {
|
||
|
list->Insert(itr.key());
|
||
|
count++;
|
||
|
}
|
||
|
} else {
|
||
|
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++;
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
if (if_log_bucket_dist_when_flash_) {
|
||
|
keys_per_bucket_hist.Add(count);
|
||
|
}
|
||
11 years ago
|
}
|
||
11 years ago
|
if (if_log_bucket_dist_when_flash_ && logger_ != nullptr) {
|
||
|
Info(logger_, "hashLinkedList Entry distribution among buckets: %s",
|
||
|
keys_per_bucket_hist.ToString().c_str());
|
||
|
}
|
||
|
|
||
11 years ago
|
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);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
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);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
bool HashLinkListRep::LinkListContains(Node* head,
|
||
|
const Slice& user_key) const {
|
||
11 years ago
|
Node* x = FindGreaterOrEqualInBucket(head, user_key);
|
||
|
return (x != nullptr && Equal(user_key, x->key));
|
||
11 years ago
|
}
|
||
|
|
||
|
Node* HashLinkListRep::FindGreaterOrEqualInBucket(Node* head,
|
||
11 years ago
|
const Slice& key) const {
|
||
11 years ago
|
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
|
||
|
|
||
11 years ago
|
MemTableRep* HashLinkListRepFactory::CreateMemTableRep(
|
||
10 years ago
|
const MemTableRep::KeyComparator& compare, MemTableAllocator* allocator,
|
||
11 years ago
|
const SliceTransform* transform, Logger* logger) {
|
||
10 years ago
|
return new HashLinkListRep(compare, allocator, transform, bucket_count_,
|
||
11 years ago
|
threshold_use_skiplist_, huge_page_tlb_size_,
|
||
|
logger, bucket_entries_logging_threshold_,
|
||
|
if_log_bucket_dist_when_flash_);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
MemTableRepFactory* NewHashLinkListRepFactory(
|
||
|
size_t bucket_count, size_t huge_page_tlb_size,
|
||
11 years ago
|
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);
|
||
11 years ago
|
}
|
||
|
|
||
|
} // namespace rocksdb
|
||
11 years ago
|
#endif // ROCKSDB_LITE
|