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.
293 lines
8.6 KiB
293 lines
8.6 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
|
||
11 years ago
|
#include "rocksdb/memtablerep.h"
|
||
12 years ago
|
|
||
|
#include <unordered_set>
|
||
|
#include <set>
|
||
|
#include <memory>
|
||
|
#include <algorithm>
|
||
|
#include <type_traits>
|
||
|
|
||
11 years ago
|
#include "util/arena.h"
|
||
11 years ago
|
#include "db/memtable.h"
|
||
9 years ago
|
#include "memtable/stl_wrappers.h"
|
||
12 years ago
|
#include "port/port.h"
|
||
|
#include "util/mutexlock.h"
|
||
|
|
||
11 years ago
|
namespace rocksdb {
|
||
12 years ago
|
namespace {
|
||
|
|
||
|
using namespace stl_wrappers;
|
||
|
|
||
|
class VectorRep : public MemTableRep {
|
||
|
public:
|
||
10 years ago
|
VectorRep(const KeyComparator& compare, MemTableAllocator* allocator,
|
||
|
size_t count);
|
||
12 years ago
|
|
||
|
// Insert key into the collection. (The caller will pack key and value into a
|
||
|
// single buffer and pass that in as the parameter to Insert)
|
||
|
// REQUIRES: nothing that compares equal to key is currently in the
|
||
|
// collection.
|
||
11 years ago
|
virtual void Insert(KeyHandle handle) override;
|
||
12 years ago
|
|
||
|
// Returns true iff an entry that compares equal to key is in the collection.
|
||
|
virtual bool Contains(const char* key) const override;
|
||
|
|
||
|
virtual void MarkReadOnly() 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;
|
||
|
|
||
12 years ago
|
virtual ~VectorRep() override { }
|
||
|
|
||
|
class Iterator : public MemTableRep::Iterator {
|
||
11 years ago
|
class VectorRep* vrep_;
|
||
12 years ago
|
std::shared_ptr<std::vector<const char*>> bucket_;
|
||
10 years ago
|
std::vector<const char*>::const_iterator mutable cit_;
|
||
12 years ago
|
const KeyComparator& compare_;
|
||
11 years ago
|
std::string tmp_; // For passing to EncodeKey
|
||
11 years ago
|
bool mutable sorted_;
|
||
|
void DoSort() const;
|
||
12 years ago
|
public:
|
||
11 years ago
|
explicit Iterator(class VectorRep* vrep,
|
||
|
std::shared_ptr<std::vector<const char*>> bucket,
|
||
12 years ago
|
const KeyComparator& compare);
|
||
|
|
||
|
// Initialize an iterator over the specified collection.
|
||
|
// The returned iterator is not valid.
|
||
|
// explicit Iterator(const MemTableRep* collection);
|
||
|
virtual ~Iterator() override { };
|
||
|
|
||
|
// Returns true iff the iterator is positioned at a valid node.
|
||
|
virtual bool Valid() const override;
|
||
|
|
||
|
// Returns the key at the current position.
|
||
|
// REQUIRES: Valid()
|
||
|
virtual const char* key() const override;
|
||
|
|
||
|
// Advances to the next position.
|
||
|
// REQUIRES: Valid()
|
||
|
virtual void Next() override;
|
||
|
|
||
|
// Advances to the previous position.
|
||
|
// REQUIRES: Valid()
|
||
|
virtual void Prev() override;
|
||
|
|
||
|
// Advance to the first entry with a key >= target
|
||
11 years ago
|
virtual void Seek(const Slice& user_key, const char* memtable_key) override;
|
||
12 years ago
|
|
||
|
// Position at the first entry in collection.
|
||
|
// Final state of iterator is Valid() iff collection is not empty.
|
||
|
virtual void SeekToFirst() override;
|
||
|
|
||
|
// Position at the last entry in collection.
|
||
|
// Final state of iterator is Valid() iff collection is not empty.
|
||
|
virtual void SeekToLast() override;
|
||
|
};
|
||
|
|
||
|
// Return an iterator over the keys in this representation.
|
||
11 years ago
|
virtual MemTableRep::Iterator* GetIterator(Arena* arena) override;
|
||
12 years ago
|
|
||
|
private:
|
||
11 years ago
|
friend class Iterator;
|
||
12 years ago
|
typedef std::vector<const char*> Bucket;
|
||
|
std::shared_ptr<Bucket> bucket_;
|
||
|
mutable port::RWMutex rwlock_;
|
||
11 years ago
|
bool immutable_;
|
||
|
bool sorted_;
|
||
12 years ago
|
const KeyComparator& compare_;
|
||
|
};
|
||
|
|
||
11 years ago
|
void VectorRep::Insert(KeyHandle handle) {
|
||
|
auto* key = static_cast<char*>(handle);
|
||
12 years ago
|
WriteLock l(&rwlock_);
|
||
|
assert(!immutable_);
|
||
|
bucket_->push_back(key);
|
||
|
}
|
||
|
|
||
|
// Returns true iff an entry that compares equal to key is in the collection.
|
||
|
bool VectorRep::Contains(const char* key) const {
|
||
|
ReadLock l(&rwlock_);
|
||
|
return std::find(bucket_->begin(), bucket_->end(), key) != bucket_->end();
|
||
|
}
|
||
|
|
||
|
void VectorRep::MarkReadOnly() {
|
||
|
WriteLock l(&rwlock_);
|
||
|
immutable_ = true;
|
||
|
}
|
||
|
|
||
|
size_t VectorRep::ApproximateMemoryUsage() {
|
||
|
return
|
||
|
sizeof(bucket_) + sizeof(*bucket_) +
|
||
|
bucket_->size() *
|
||
|
sizeof(
|
||
|
std::remove_reference<decltype(*bucket_)>::type::value_type
|
||
|
);
|
||
|
}
|
||
|
|
||
10 years ago
|
VectorRep::VectorRep(const KeyComparator& compare, MemTableAllocator* allocator,
|
||
|
size_t count)
|
||
|
: MemTableRep(allocator),
|
||
11 years ago
|
bucket_(new Bucket()),
|
||
11 years ago
|
immutable_(false),
|
||
|
sorted_(false),
|
||
11 years ago
|
compare_(compare) { bucket_.get()->reserve(count); }
|
||
12 years ago
|
|
||
11 years ago
|
VectorRep::Iterator::Iterator(class VectorRep* vrep,
|
||
|
std::shared_ptr<std::vector<const char*>> bucket,
|
||
12 years ago
|
const KeyComparator& compare)
|
||
11 years ago
|
: vrep_(vrep),
|
||
|
bucket_(bucket),
|
||
11 years ago
|
cit_(bucket_->end()),
|
||
11 years ago
|
compare_(compare),
|
||
|
sorted_(false) { }
|
||
|
|
||
|
void VectorRep::Iterator::DoSort() const {
|
||
|
// vrep is non-null means that we are working on an immutable memtable
|
||
|
if (!sorted_ && vrep_ != nullptr) {
|
||
|
WriteLock l(&vrep_->rwlock_);
|
||
|
if (!vrep_->sorted_) {
|
||
|
std::sort(bucket_->begin(), bucket_->end(), Compare(compare_));
|
||
|
cit_ = bucket_->begin();
|
||
|
vrep_->sorted_ = true;
|
||
|
}
|
||
|
sorted_ = true;
|
||
|
}
|
||
|
if (!sorted_) {
|
||
|
std::sort(bucket_->begin(), bucket_->end(), Compare(compare_));
|
||
|
cit_ = bucket_->begin();
|
||
|
sorted_ = true;
|
||
|
}
|
||
|
assert(sorted_);
|
||
|
assert(vrep_ == nullptr || vrep_->sorted_);
|
||
|
}
|
||
12 years ago
|
|
||
|
// Returns true iff the iterator is positioned at a valid node.
|
||
|
bool VectorRep::Iterator::Valid() const {
|
||
11 years ago
|
DoSort();
|
||
12 years ago
|
return cit_ != bucket_->end();
|
||
|
}
|
||
|
|
||
|
// Returns the key at the current position.
|
||
|
// REQUIRES: Valid()
|
||
|
const char* VectorRep::Iterator::key() const {
|
||
10 years ago
|
assert(sorted_);
|
||
12 years ago
|
return *cit_;
|
||
|
}
|
||
|
|
||
|
// Advances to the next position.
|
||
|
// REQUIRES: Valid()
|
||
|
void VectorRep::Iterator::Next() {
|
||
10 years ago
|
assert(sorted_);
|
||
12 years ago
|
if (cit_ == bucket_->end()) {
|
||
|
return;
|
||
|
}
|
||
|
++cit_;
|
||
|
}
|
||
|
|
||
|
// Advances to the previous position.
|
||
|
// REQUIRES: Valid()
|
||
|
void VectorRep::Iterator::Prev() {
|
||
10 years ago
|
assert(sorted_);
|
||
12 years ago
|
if (cit_ == bucket_->begin()) {
|
||
|
// If you try to go back from the first element, the iterator should be
|
||
|
// invalidated. So we set it to past-the-end. This means that you can
|
||
|
// treat the container circularly.
|
||
|
cit_ = bucket_->end();
|
||
|
} else {
|
||
|
--cit_;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Advance to the first entry with a key >= target
|
||
11 years ago
|
void VectorRep::Iterator::Seek(const Slice& user_key,
|
||
|
const char* memtable_key) {
|
||
11 years ago
|
DoSort();
|
||
12 years ago
|
// Do binary search to find first value not less than the target
|
||
11 years ago
|
const char* encoded_key =
|
||
|
(memtable_key != nullptr) ? memtable_key : EncodeKey(&tmp_, user_key);
|
||
12 years ago
|
cit_ = std::equal_range(bucket_->begin(),
|
||
|
bucket_->end(),
|
||
11 years ago
|
encoded_key,
|
||
12 years ago
|
[this] (const char* a, const char* b) {
|
||
|
return compare_(a, b) < 0;
|
||
|
}).first;
|
||
|
}
|
||
|
|
||
|
// Position at the first entry in collection.
|
||
|
// Final state of iterator is Valid() iff collection is not empty.
|
||
|
void VectorRep::Iterator::SeekToFirst() {
|
||
11 years ago
|
DoSort();
|
||
12 years ago
|
cit_ = bucket_->begin();
|
||
|
}
|
||
|
|
||
|
// Position at the last entry in collection.
|
||
|
// Final state of iterator is Valid() iff collection is not empty.
|
||
|
void VectorRep::Iterator::SeekToLast() {
|
||
11 years ago
|
DoSort();
|
||
12 years ago
|
cit_ = bucket_->end();
|
||
|
if (bucket_->size() != 0) {
|
||
|
--cit_;
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
void VectorRep::Get(const LookupKey& k, void* callback_args,
|
||
|
bool (*callback_func)(void* arg, const char* entry)) {
|
||
|
rwlock_.ReadLock();
|
||
|
VectorRep* vector_rep;
|
||
|
std::shared_ptr<Bucket> bucket;
|
||
|
if (immutable_) {
|
||
|
vector_rep = this;
|
||
|
} else {
|
||
|
vector_rep = nullptr;
|
||
|
bucket.reset(new Bucket(*bucket_)); // make a copy
|
||
|
}
|
||
|
VectorRep::Iterator iter(vector_rep, immutable_ ? bucket_ : bucket, compare_);
|
||
11 years ago
|
rwlock_.ReadUnlock();
|
||
11 years ago
|
|
||
|
for (iter.Seek(k.user_key(), k.memtable_key().data());
|
||
|
iter.Valid() && callback_func(callback_args, iter.key()); iter.Next()) {
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
MemTableRep::Iterator* VectorRep::GetIterator(Arena* arena) {
|
||
|
char* mem = nullptr;
|
||
|
if (arena != nullptr) {
|
||
|
mem = arena->AllocateAligned(sizeof(Iterator));
|
||
|
}
|
||
12 years ago
|
ReadLock l(&rwlock_);
|
||
11 years ago
|
// Do not sort here. The sorting would be done the first time
|
||
|
// a Seek is performed on the iterator.
|
||
12 years ago
|
if (immutable_) {
|
||
11 years ago
|
if (arena == nullptr) {
|
||
|
return new Iterator(this, bucket_, compare_);
|
||
|
} else {
|
||
|
return new (mem) Iterator(this, bucket_, compare_);
|
||
|
}
|
||
12 years ago
|
} else {
|
||
11 years ago
|
std::shared_ptr<Bucket> tmp;
|
||
12 years ago
|
tmp.reset(new Bucket(*bucket_)); // make a copy
|
||
11 years ago
|
if (arena == nullptr) {
|
||
|
return new Iterator(nullptr, tmp, compare_);
|
||
|
} else {
|
||
|
return new (mem) Iterator(nullptr, tmp, compare_);
|
||
|
}
|
||
12 years ago
|
}
|
||
|
}
|
||
|
} // anon namespace
|
||
|
|
||
11 years ago
|
MemTableRep* VectorRepFactory::CreateMemTableRep(
|
||
10 years ago
|
const MemTableRep::KeyComparator& compare, MemTableAllocator* allocator,
|
||
11 years ago
|
const SliceTransform*, Logger* logger) {
|
||
10 years ago
|
return new VectorRep(compare, allocator, count_);
|
||
12 years ago
|
}
|
||
11 years ago
|
} // namespace rocksdb
|
||
11 years ago
|
#endif // ROCKSDB_LITE
|