2013-10-16 21:59:46 +00:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
2014-01-24 19:09:04 +00:00
|
|
|
#include <algorithm>
|
2011-03-18 22:37:00 +00:00
|
|
|
#include <map>
|
2011-07-19 23:36:47 +00:00
|
|
|
#include <string>
|
2013-07-23 21:42:27 +00:00
|
|
|
#include <memory>
|
2013-10-10 18:43:24 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "db/dbformat.h"
|
2014-01-17 20:46:06 +00:00
|
|
|
#include "rocksdb/statistics.h"
|
|
|
|
#include "util/statistics.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "db/write_batch_internal.h"
|
2013-12-06 00:51:26 +00:00
|
|
|
|
2013-11-13 06:46:51 +00:00
|
|
|
#include "rocksdb/cache.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
2014-01-31 01:18:17 +00:00
|
|
|
#include "rocksdb/slice_transform.h"
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/memtablerep.h"
|
2014-01-28 05:58:46 +00:00
|
|
|
#include "table/block.h"
|
2013-11-13 06:46:51 +00:00
|
|
|
#include "table/block_based_table_builder.h"
|
2013-11-20 06:00:48 +00:00
|
|
|
#include "table/block_based_table_factory.h"
|
2013-11-13 06:46:51 +00:00
|
|
|
#include "table/block_based_table_reader.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "table/block_builder.h"
|
|
|
|
#include "table/format.h"
|
2014-01-28 05:58:46 +00:00
|
|
|
#include "table/meta_blocks.h"
|
|
|
|
#include "table/plain_table_factory.h"
|
2013-12-06 00:51:26 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/random.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-11-08 05:27:21 +00:00
|
|
|
namespace {
|
2014-01-24 19:09:04 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
// Return reverse of "key".
|
|
|
|
// Used to test non-lexicographic comparators.
|
2014-01-24 19:09:04 +00:00
|
|
|
std::string Reverse(const Slice& key) {
|
|
|
|
auto rev = key.ToString();
|
|
|
|
std::reverse(rev.begin(), rev.end());
|
2011-03-18 22:37:00 +00:00
|
|
|
return rev;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ReverseKeyComparator : public Comparator {
|
|
|
|
public:
|
|
|
|
virtual const char* Name() const {
|
2013-10-05 05:32:05 +00:00
|
|
|
return "rocksdb.ReverseBytewiseComparator";
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual int Compare(const Slice& a, const Slice& b) const {
|
|
|
|
return BytewiseComparator()->Compare(Reverse(a), Reverse(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void FindShortestSeparator(
|
|
|
|
std::string* start,
|
|
|
|
const Slice& limit) const {
|
|
|
|
std::string s = Reverse(*start);
|
|
|
|
std::string l = Reverse(limit);
|
|
|
|
BytewiseComparator()->FindShortestSeparator(&s, l);
|
|
|
|
*start = Reverse(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void FindShortSuccessor(std::string* key) const {
|
|
|
|
std::string s = Reverse(*key);
|
|
|
|
BytewiseComparator()->FindShortSuccessor(&s);
|
|
|
|
*key = Reverse(s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-24 19:09:04 +00:00
|
|
|
ReverseKeyComparator reverse_key_comparator;
|
|
|
|
|
|
|
|
void Increment(const Comparator* cmp, std::string* key) {
|
2011-03-18 22:37:00 +00:00
|
|
|
if (cmp == BytewiseComparator()) {
|
|
|
|
key->push_back('\0');
|
|
|
|
} else {
|
|
|
|
assert(cmp == &reverse_key_comparator);
|
|
|
|
std::string rev = Reverse(*key);
|
|
|
|
rev.push_back('\0');
|
|
|
|
*key = Reverse(rev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// An STL comparator that uses a Comparator
|
|
|
|
struct STLLessThan {
|
|
|
|
const Comparator* cmp;
|
|
|
|
|
|
|
|
STLLessThan() : cmp(BytewiseComparator()) { }
|
2013-03-01 02:04:58 +00:00
|
|
|
explicit STLLessThan(const Comparator* c) : cmp(c) { }
|
2011-03-18 22:37:00 +00:00
|
|
|
bool operator()(const std::string& a, const std::string& b) const {
|
|
|
|
return cmp->Compare(Slice(a), Slice(b)) < 0;
|
|
|
|
}
|
|
|
|
};
|
2014-01-24 19:09:04 +00:00
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
class StringSink: public WritableFile {
|
|
|
|
public:
|
|
|
|
~StringSink() { }
|
|
|
|
|
|
|
|
const std::string& contents() const { return contents_; }
|
|
|
|
|
|
|
|
virtual Status Close() { return Status::OK(); }
|
|
|
|
virtual Status Flush() { return Status::OK(); }
|
|
|
|
virtual Status Sync() { return Status::OK(); }
|
|
|
|
|
|
|
|
virtual Status Append(const Slice& data) {
|
|
|
|
contents_.append(data.data(), data.size());
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string contents_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class StringSource: public RandomAccessFile {
|
|
|
|
public:
|
2013-12-20 17:35:24 +00:00
|
|
|
StringSource(const Slice& contents, uint64_t uniq_id, bool mmap)
|
|
|
|
: contents_(contents.data(), contents.size()), uniq_id_(uniq_id),
|
|
|
|
mmap_(mmap) {
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~StringSource() { }
|
|
|
|
|
2011-03-28 20:43:44 +00:00
|
|
|
uint64_t Size() const { return contents_.size(); }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
virtual Status Read(uint64_t offset, size_t n, Slice* result,
|
|
|
|
char* scratch) const {
|
|
|
|
if (offset > contents_.size()) {
|
|
|
|
return Status::InvalidArgument("invalid Read offset");
|
|
|
|
}
|
|
|
|
if (offset + n > contents_.size()) {
|
|
|
|
n = contents_.size() - offset;
|
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
if (!mmap_) {
|
|
|
|
memcpy(scratch, &contents_[offset], n);
|
|
|
|
*result = Slice(scratch, n);
|
|
|
|
} else {
|
|
|
|
*result = Slice(&contents_[offset], n);
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2013-01-31 23:20:24 +00:00
|
|
|
virtual size_t GetUniqueId(char* id, size_t max_size) const {
|
|
|
|
if (max_size < 20) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* rid = id;
|
|
|
|
rid = EncodeVarint64(rid, uniq_id_);
|
|
|
|
rid = EncodeVarint64(rid, 0);
|
|
|
|
return static_cast<size_t>(rid-id);
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
|
|
|
std::string contents_;
|
2013-01-31 23:20:24 +00:00
|
|
|
uint64_t uniq_id_;
|
2013-12-20 17:35:24 +00:00
|
|
|
bool mmap_;
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2014-01-24 19:09:04 +00:00
|
|
|
typedef std::map<std::string, std::string, STLLessThan> KVMap;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Helper class for tests to unify the interface between
|
|
|
|
// BlockBuilder/TableBuilder and Block/Table.
|
|
|
|
class Constructor {
|
|
|
|
public:
|
2014-01-24 19:09:04 +00:00
|
|
|
explicit Constructor(const Comparator* cmp) : data_(STLLessThan(cmp)) {}
|
2011-03-18 22:37:00 +00:00
|
|
|
virtual ~Constructor() { }
|
|
|
|
|
|
|
|
void Add(const std::string& key, const Slice& value) {
|
|
|
|
data_[key] = value.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish constructing the data structure with all the keys that have
|
|
|
|
// been added so far. Returns the keys in sorted order in "*keys"
|
|
|
|
// and stores the key/value pairs in "*kvmap"
|
|
|
|
void Finish(const Options& options,
|
|
|
|
std::vector<std::string>* keys,
|
|
|
|
KVMap* kvmap) {
|
|
|
|
*kvmap = data_;
|
|
|
|
keys->clear();
|
|
|
|
for (KVMap::const_iterator it = data_.begin();
|
|
|
|
it != data_.end();
|
|
|
|
++it) {
|
|
|
|
keys->push_back(it->first);
|
|
|
|
}
|
|
|
|
data_.clear();
|
|
|
|
Status s = FinishImpl(options, *kvmap);
|
|
|
|
ASSERT_TRUE(s.ok()) << s.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct the data structure from the data in "data"
|
|
|
|
virtual Status FinishImpl(const Options& options, const KVMap& data) = 0;
|
|
|
|
|
|
|
|
virtual Iterator* NewIterator() const = 0;
|
|
|
|
|
|
|
|
virtual const KVMap& data() { return data_; }
|
|
|
|
|
2013-03-01 02:04:58 +00:00
|
|
|
virtual DB* db() const { return nullptr; } // Overridden in DBConstructor
|
2011-03-21 19:40:57 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
|
|
|
KVMap data_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BlockConstructor: public Constructor {
|
|
|
|
public:
|
|
|
|
explicit BlockConstructor(const Comparator* cmp)
|
|
|
|
: Constructor(cmp),
|
|
|
|
comparator_(cmp),
|
2013-03-01 02:04:58 +00:00
|
|
|
block_(nullptr) { }
|
2011-03-18 22:37:00 +00:00
|
|
|
~BlockConstructor() {
|
|
|
|
delete block_;
|
|
|
|
}
|
|
|
|
virtual Status FinishImpl(const Options& options, const KVMap& data) {
|
|
|
|
delete block_;
|
2013-03-01 02:04:58 +00:00
|
|
|
block_ = nullptr;
|
2013-11-17 07:21:15 +00:00
|
|
|
BlockBuilder builder(options);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
for (KVMap::const_iterator it = data.begin();
|
|
|
|
it != data.end();
|
|
|
|
++it) {
|
|
|
|
builder.Add(it->first, it->second);
|
|
|
|
}
|
|
|
|
// Open the block
|
2012-04-17 15:36:46 +00:00
|
|
|
data_ = builder.Finish().ToString();
|
|
|
|
BlockContents contents;
|
|
|
|
contents.data = data_;
|
|
|
|
contents.cachable = false;
|
|
|
|
contents.heap_allocated = false;
|
|
|
|
block_ = new Block(contents);
|
2011-03-18 22:37:00 +00:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
virtual Iterator* NewIterator() const {
|
|
|
|
return block_->NewIterator(comparator_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Comparator* comparator_;
|
2012-04-17 15:36:46 +00:00
|
|
|
std::string data_;
|
2011-03-18 22:37:00 +00:00
|
|
|
Block* block_;
|
|
|
|
|
|
|
|
BlockConstructor();
|
|
|
|
};
|
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
// A helper class that converts internal format keys into user keys
|
|
|
|
class KeyConvertingIterator: public Iterator {
|
2011-03-18 22:37:00 +00:00
|
|
|
public:
|
2013-12-20 17:35:24 +00:00
|
|
|
explicit KeyConvertingIterator(Iterator* iter) : iter_(iter) { }
|
|
|
|
virtual ~KeyConvertingIterator() { delete iter_; }
|
|
|
|
virtual bool Valid() const { return iter_->Valid(); }
|
|
|
|
virtual void Seek(const Slice& target) {
|
|
|
|
ParsedInternalKey ikey(target, kMaxSequenceNumber, kTypeValue);
|
|
|
|
std::string encoded;
|
|
|
|
AppendInternalKey(&encoded, ikey);
|
|
|
|
iter_->Seek(encoded);
|
|
|
|
}
|
|
|
|
virtual void SeekToFirst() { iter_->SeekToFirst(); }
|
|
|
|
virtual void SeekToLast() { iter_->SeekToLast(); }
|
|
|
|
virtual void Next() { iter_->Next(); }
|
|
|
|
virtual void Prev() { iter_->Prev(); }
|
|
|
|
|
|
|
|
virtual Slice key() const {
|
|
|
|
assert(Valid());
|
|
|
|
ParsedInternalKey key;
|
|
|
|
if (!ParseInternalKey(iter_->key(), &key)) {
|
|
|
|
status_ = Status::Corruption("malformed internal key");
|
|
|
|
return Slice("corrupted key");
|
|
|
|
}
|
|
|
|
return key.user_key;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
|
|
|
|
virtual Slice value() const { return iter_->value(); }
|
|
|
|
virtual Status status() const {
|
|
|
|
return status_.ok() ? iter_->status() : status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
mutable Status status_;
|
|
|
|
Iterator* iter_;
|
|
|
|
|
|
|
|
// No copying allowed
|
|
|
|
KeyConvertingIterator(const KeyConvertingIterator&);
|
|
|
|
void operator=(const KeyConvertingIterator&);
|
|
|
|
};
|
|
|
|
|
|
|
|
class TableConstructor: public Constructor {
|
|
|
|
public:
|
2014-01-28 18:35:48 +00:00
|
|
|
explicit TableConstructor(const Comparator* cmp,
|
|
|
|
bool convert_to_internal_key = false)
|
|
|
|
: Constructor(cmp), convert_to_internal_key_(convert_to_internal_key) {}
|
|
|
|
~TableConstructor() { Reset(); }
|
2014-01-24 18:57:15 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
virtual Status FinishImpl(const Options& options, const KVMap& data) {
|
|
|
|
Reset();
|
2013-01-31 23:20:24 +00:00
|
|
|
sink_.reset(new StringSink());
|
2013-12-20 17:35:24 +00:00
|
|
|
unique_ptr<TableBuilder> builder;
|
2014-01-28 05:58:46 +00:00
|
|
|
builder.reset(options.table_factory->NewTableBuilder(options, sink_.get(),
|
|
|
|
options.compression));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
for (KVMap::const_iterator it = data.begin();
|
|
|
|
it != data.end();
|
|
|
|
++it) {
|
2013-12-20 17:35:24 +00:00
|
|
|
if (convert_to_internal_key_) {
|
|
|
|
ParsedInternalKey ikey(it->first, kMaxSequenceNumber, kTypeValue);
|
|
|
|
std::string encoded;
|
|
|
|
AppendInternalKey(&encoded, ikey);
|
|
|
|
builder->Add(encoded, it->second);
|
|
|
|
} else {
|
|
|
|
builder->Add(it->first, it->second);
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(builder->status().ok());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
Status s = builder->Finish();
|
2011-03-18 22:37:00 +00:00
|
|
|
ASSERT_TRUE(s.ok()) << s.ToString();
|
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
ASSERT_EQ(sink_->contents().size(), builder->FileSize());
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
// Open the table
|
2013-01-31 23:20:24 +00:00
|
|
|
uniq_id_ = cur_uniq_id_++;
|
2014-01-28 18:35:48 +00:00
|
|
|
source_.reset(new StringSource(sink_->contents(), uniq_id_,
|
|
|
|
options.allow_mmap_reads));
|
2014-01-28 05:58:46 +00:00
|
|
|
return options.table_factory->NewTableReader(
|
2014-01-28 18:35:48 +00:00
|
|
|
options, soptions, std::move(source_), sink_->contents().size(),
|
|
|
|
&table_reader_);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual Iterator* NewIterator() const {
|
2013-12-20 17:35:24 +00:00
|
|
|
Iterator* iter = table_reader_->NewIterator(ReadOptions());
|
|
|
|
if (convert_to_internal_key_) {
|
|
|
|
return new KeyConvertingIterator(iter);
|
|
|
|
} else {
|
|
|
|
return iter;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ApproximateOffsetOf(const Slice& key) const {
|
2013-10-30 17:52:33 +00:00
|
|
|
return table_reader_->ApproximateOffsetOf(key);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2013-01-31 23:20:24 +00:00
|
|
|
virtual Status Reopen(const Options& options) {
|
2013-12-20 17:35:24 +00:00
|
|
|
source_.reset(
|
|
|
|
new StringSource(sink_->contents(), uniq_id_,
|
|
|
|
options.allow_mmap_reads));
|
2014-01-28 05:58:46 +00:00
|
|
|
return options.table_factory->NewTableReader(
|
|
|
|
options, soptions, std::move(source_), sink_->contents().size(),
|
|
|
|
&table_reader_);
|
2013-01-31 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2013-10-30 17:52:33 +00:00
|
|
|
virtual TableReader* table_reader() {
|
|
|
|
return table_reader_.get();
|
2013-01-31 23:20:24 +00:00
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
|
|
|
void Reset() {
|
2013-01-31 23:20:24 +00:00
|
|
|
uniq_id_ = 0;
|
2013-10-30 17:52:33 +00:00
|
|
|
table_reader_.reset();
|
2013-01-31 23:20:24 +00:00
|
|
|
sink_.reset();
|
2013-01-20 10:07:13 +00:00
|
|
|
source_.reset();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
bool convert_to_internal_key_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-01-31 23:20:24 +00:00
|
|
|
uint64_t uniq_id_;
|
|
|
|
unique_ptr<StringSink> sink_;
|
2013-01-20 10:07:13 +00:00
|
|
|
unique_ptr<StringSource> source_;
|
2013-10-30 17:52:33 +00:00
|
|
|
unique_ptr<TableReader> table_reader_;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
TableConstructor();
|
2013-01-31 23:20:24 +00:00
|
|
|
|
|
|
|
static uint64_t cur_uniq_id_;
|
2013-06-07 22:35:17 +00:00
|
|
|
const EnvOptions soptions;
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
2013-12-20 17:35:24 +00:00
|
|
|
uint64_t TableConstructor::cur_uniq_id_ = 1;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
class MemTableConstructor: public Constructor {
|
|
|
|
public:
|
|
|
|
explicit MemTableConstructor(const Comparator* cmp)
|
|
|
|
: Constructor(cmp),
|
2013-07-23 21:42:27 +00:00
|
|
|
internal_comparator_(cmp),
|
|
|
|
table_factory_(new SkipListFactory) {
|
2014-01-14 23:32:37 +00:00
|
|
|
Options options;
|
|
|
|
options.memtable_factory = table_factory_;
|
|
|
|
memtable_ = new MemTable(internal_comparator_, options);
|
2011-05-21 02:17:43 +00:00
|
|
|
memtable_->Ref();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
~MemTableConstructor() {
|
2013-12-02 05:23:44 +00:00
|
|
|
delete memtable_->Unref();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
virtual Status FinishImpl(const Options& options, const KVMap& data) {
|
2013-12-02 05:23:44 +00:00
|
|
|
delete memtable_->Unref();
|
2014-01-14 23:32:37 +00:00
|
|
|
Options memtable_options;
|
|
|
|
memtable_options.memtable_factory = table_factory_;
|
|
|
|
memtable_ = new MemTable(internal_comparator_, memtable_options);
|
2011-05-21 02:17:43 +00:00
|
|
|
memtable_->Ref();
|
2011-03-18 22:37:00 +00:00
|
|
|
int seq = 1;
|
|
|
|
for (KVMap::const_iterator it = data.begin();
|
|
|
|
it != data.end();
|
|
|
|
++it) {
|
|
|
|
memtable_->Add(seq, kTypeValue, it->first, it->second);
|
|
|
|
seq++;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
virtual Iterator* NewIterator() const {
|
|
|
|
return new KeyConvertingIterator(memtable_->NewIterator());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
InternalKeyComparator internal_comparator_;
|
|
|
|
MemTable* memtable_;
|
2013-07-23 21:42:27 +00:00
|
|
|
std::shared_ptr<SkipListFactory> table_factory_;
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class DBConstructor: public Constructor {
|
|
|
|
public:
|
|
|
|
explicit DBConstructor(const Comparator* cmp)
|
|
|
|
: Constructor(cmp),
|
|
|
|
comparator_(cmp) {
|
2013-03-01 02:04:58 +00:00
|
|
|
db_ = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
NewDB();
|
|
|
|
}
|
|
|
|
~DBConstructor() {
|
|
|
|
delete db_;
|
|
|
|
}
|
|
|
|
virtual Status FinishImpl(const Options& options, const KVMap& data) {
|
|
|
|
delete db_;
|
2013-03-01 02:04:58 +00:00
|
|
|
db_ = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
NewDB();
|
|
|
|
for (KVMap::const_iterator it = data.begin();
|
|
|
|
it != data.end();
|
|
|
|
++it) {
|
|
|
|
WriteBatch batch;
|
|
|
|
batch.Put(it->first, it->second);
|
|
|
|
ASSERT_TRUE(db_->Write(WriteOptions(), &batch).ok());
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
virtual Iterator* NewIterator() const {
|
|
|
|
return db_->NewIterator(ReadOptions());
|
|
|
|
}
|
|
|
|
|
2011-03-21 19:40:57 +00:00
|
|
|
virtual DB* db() const { return db_; }
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
|
|
|
void NewDB() {
|
|
|
|
std::string name = test::TmpDir() + "/table_testdb";
|
|
|
|
|
2013-11-20 06:00:48 +00:00
|
|
|
Options options;
|
2011-03-18 22:37:00 +00:00
|
|
|
options.comparator = comparator_;
|
|
|
|
Status status = DestroyDB(name, options);
|
|
|
|
ASSERT_TRUE(status.ok()) << status.ToString();
|
|
|
|
|
|
|
|
options.create_if_missing = true;
|
|
|
|
options.error_if_exists = true;
|
2011-03-21 19:40:57 +00:00
|
|
|
options.write_buffer_size = 10000; // Something small to force merging
|
2011-03-18 22:37:00 +00:00
|
|
|
status = DB::Open(options, name, &db_);
|
|
|
|
ASSERT_TRUE(status.ok()) << status.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
const Comparator* comparator_;
|
|
|
|
DB* db_;
|
|
|
|
};
|
|
|
|
|
2012-06-28 06:41:33 +00:00
|
|
|
static bool SnappyCompressionSupported() {
|
|
|
|
std::string out;
|
|
|
|
Slice in = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
2013-11-20 06:00:48 +00:00
|
|
|
return port::Snappy_Compress(Options().compression_opts,
|
2013-11-08 05:27:21 +00:00
|
|
|
in.data(), in.size(),
|
2012-11-01 17:50:08 +00:00
|
|
|
&out);
|
2012-06-28 06:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ZlibCompressionSupported() {
|
|
|
|
std::string out;
|
|
|
|
Slice in = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
2013-11-20 06:00:48 +00:00
|
|
|
return port::Zlib_Compress(Options().compression_opts,
|
2013-11-08 05:27:21 +00:00
|
|
|
in.data(), in.size(),
|
2012-11-01 17:50:08 +00:00
|
|
|
&out);
|
2012-06-28 06:41:33 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 02:37:01 +00:00
|
|
|
#ifdef BZIP2
|
2012-06-29 02:26:43 +00:00
|
|
|
static bool BZip2CompressionSupported() {
|
|
|
|
std::string out;
|
|
|
|
Slice in = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
2013-11-20 06:00:48 +00:00
|
|
|
return port::BZip2_Compress(Options().compression_opts,
|
2013-11-08 05:27:21 +00:00
|
|
|
in.data(), in.size(),
|
2012-11-01 17:50:08 +00:00
|
|
|
&out);
|
2012-06-29 02:26:43 +00:00
|
|
|
}
|
2013-01-15 02:37:01 +00:00
|
|
|
#endif
|
2012-06-29 02:26:43 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
enum TestType {
|
2013-12-20 17:35:24 +00:00
|
|
|
BLOCK_BASED_TABLE_TEST,
|
|
|
|
PLAIN_TABLE_SEMI_FIXED_PREFIX,
|
|
|
|
PLAIN_TABLE_FULL_STR_PREFIX,
|
2011-03-18 22:37:00 +00:00
|
|
|
BLOCK_TEST,
|
|
|
|
MEMTABLE_TEST,
|
2011-07-19 23:36:47 +00:00
|
|
|
DB_TEST
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TestArgs {
|
|
|
|
TestType type;
|
|
|
|
bool reverse_compare;
|
|
|
|
int restart_interval;
|
2012-06-28 06:41:33 +00:00
|
|
|
CompressionType compression;
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2013-11-20 06:00:48 +00:00
|
|
|
static std::vector<TestArgs> GenerateArgList() {
|
2014-01-24 19:09:04 +00:00
|
|
|
std::vector<TestArgs> test_args;
|
|
|
|
std::vector<TestType> test_types = {
|
|
|
|
BLOCK_BASED_TABLE_TEST, PLAIN_TABLE_SEMI_FIXED_PREFIX,
|
|
|
|
PLAIN_TABLE_FULL_STR_PREFIX, BLOCK_TEST,
|
|
|
|
MEMTABLE_TEST, DB_TEST};
|
|
|
|
std::vector<bool> reverse_compare_types = {false, true};
|
|
|
|
std::vector<int> restart_intervals = {16, 1, 1024};
|
2012-06-28 06:41:33 +00:00
|
|
|
|
|
|
|
// Only add compression if it is supported
|
2014-01-24 19:09:04 +00:00
|
|
|
std::vector<CompressionType> compression_types = {kNoCompression};
|
2012-06-28 06:41:33 +00:00
|
|
|
#ifdef SNAPPY
|
2014-01-24 19:09:04 +00:00
|
|
|
if (SnappyCompressionSupported()) {
|
2012-06-28 06:41:33 +00:00
|
|
|
compression_types.push_back(kSnappyCompression);
|
2014-01-24 19:09:04 +00:00
|
|
|
}
|
2012-06-28 06:41:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ZLIB
|
2014-01-24 19:09:04 +00:00
|
|
|
if (ZlibCompressionSupported()) {
|
2012-06-28 06:41:33 +00:00
|
|
|
compression_types.push_back(kZlibCompression);
|
2014-01-24 19:09:04 +00:00
|
|
|
}
|
2012-06-28 06:41:33 +00:00
|
|
|
#endif
|
|
|
|
|
2012-06-29 02:26:43 +00:00
|
|
|
#ifdef BZIP2
|
2014-01-24 19:09:04 +00:00
|
|
|
if (BZip2CompressionSupported()) {
|
2012-06-29 02:26:43 +00:00
|
|
|
compression_types.push_back(kBZip2Compression);
|
2014-01-24 19:09:04 +00:00
|
|
|
}
|
2012-06-29 02:26:43 +00:00
|
|
|
#endif
|
|
|
|
|
2014-01-24 19:09:04 +00:00
|
|
|
for (auto test_type : test_types) {
|
|
|
|
for (auto reverse_compare : reverse_compare_types) {
|
|
|
|
if (test_type == PLAIN_TABLE_SEMI_FIXED_PREFIX ||
|
|
|
|
test_type == PLAIN_TABLE_FULL_STR_PREFIX) {
|
2013-12-20 17:35:24 +00:00
|
|
|
// Plain table doesn't use restart index or compression.
|
|
|
|
TestArgs one_arg;
|
2014-01-24 19:09:04 +00:00
|
|
|
one_arg.type = test_type;
|
|
|
|
one_arg.reverse_compare = reverse_compare;
|
|
|
|
one_arg.restart_interval = restart_intervals[0];
|
2013-12-20 17:35:24 +00:00
|
|
|
one_arg.compression = compression_types[0];
|
2014-01-24 19:09:04 +00:00
|
|
|
test_args.push_back(one_arg);
|
2013-12-20 17:35:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-06-28 06:41:33 +00:00
|
|
|
|
2014-01-24 19:09:04 +00:00
|
|
|
for (auto restart_interval : restart_intervals) {
|
|
|
|
for (auto compression_type : compression_types) {
|
2013-12-20 17:35:24 +00:00
|
|
|
TestArgs one_arg;
|
2014-01-24 19:09:04 +00:00
|
|
|
one_arg.type = test_type;
|
|
|
|
one_arg.reverse_compare = reverse_compare;
|
|
|
|
one_arg.restart_interval = restart_interval;
|
|
|
|
one_arg.compression = compression_type;
|
|
|
|
test_args.push_back(one_arg);
|
2013-12-20 17:35:24 +00:00
|
|
|
}
|
2014-01-24 19:09:04 +00:00
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
}
|
2014-01-24 19:09:04 +00:00
|
|
|
}
|
|
|
|
return test_args;
|
2012-06-28 06:41:33 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
// In order to make all tests run for plain table format, including
|
|
|
|
// those operating on empty keys, create a new prefix transformer which
|
|
|
|
// return fixed prefix if the slice is not shorter than the prefix length,
|
|
|
|
// and the full slice if it is shorter.
|
|
|
|
class FixedOrLessPrefixTransform : public SliceTransform {
|
|
|
|
private:
|
|
|
|
const size_t prefix_len_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit FixedOrLessPrefixTransform(size_t prefix_len) :
|
|
|
|
prefix_len_(prefix_len) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* Name() const {
|
|
|
|
return "rocksdb.FixedPrefix";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Slice Transform(const Slice& src) const {
|
|
|
|
assert(InDomain(src));
|
|
|
|
if (src.size() < prefix_len_) {
|
|
|
|
return src;
|
|
|
|
}
|
|
|
|
return Slice(src.data(), prefix_len_);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool InDomain(const Slice& src) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool InRange(const Slice& dst) const {
|
|
|
|
return (dst.size() <= prefix_len_);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
class Harness {
|
|
|
|
public:
|
2013-03-01 02:04:58 +00:00
|
|
|
Harness() : constructor_(nullptr) { }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
void Init(const TestArgs& args) {
|
|
|
|
delete constructor_;
|
2013-03-01 02:04:58 +00:00
|
|
|
constructor_ = nullptr;
|
2013-11-20 06:00:48 +00:00
|
|
|
options_ = Options();
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
options_.block_restart_interval = args.restart_interval;
|
2012-06-28 06:41:33 +00:00
|
|
|
options_.compression = args.compression;
|
2011-03-18 22:37:00 +00:00
|
|
|
// Use shorter block size for tests to exercise block boundary
|
|
|
|
// conditions more.
|
|
|
|
options_.block_size = 256;
|
|
|
|
if (args.reverse_compare) {
|
|
|
|
options_.comparator = &reverse_key_comparator;
|
|
|
|
}
|
2013-12-20 17:35:24 +00:00
|
|
|
internal_comparator_.reset(new InternalKeyComparator(options_.comparator));
|
|
|
|
support_prev_ = true;
|
|
|
|
only_support_prefix_seek_ = false;
|
2014-01-28 18:35:48 +00:00
|
|
|
BlockBasedTableOptions table_options;
|
2011-03-18 22:37:00 +00:00
|
|
|
switch (args.type) {
|
2013-12-20 17:35:24 +00:00
|
|
|
case BLOCK_BASED_TABLE_TEST:
|
|
|
|
table_options.flush_block_policy_factory.reset(
|
|
|
|
new FlushBlockBySizePolicyFactory(options_.block_size,
|
|
|
|
options_.block_size_deviation));
|
|
|
|
options_.table_factory.reset(new BlockBasedTableFactory(table_options));
|
|
|
|
constructor_ = new TableConstructor(options_.comparator);
|
|
|
|
break;
|
|
|
|
case PLAIN_TABLE_SEMI_FIXED_PREFIX:
|
|
|
|
support_prev_ = false;
|
|
|
|
only_support_prefix_seek_ = true;
|
2014-01-04 02:27:33 +00:00
|
|
|
options_.prefix_extractor = prefix_transform.get();
|
2013-12-20 17:35:24 +00:00
|
|
|
options_.allow_mmap_reads = true;
|
|
|
|
options_.table_factory.reset(new PlainTableFactory());
|
|
|
|
constructor_ = new TableConstructor(options_.comparator, true);
|
|
|
|
options_.comparator = internal_comparator_.get();
|
|
|
|
break;
|
|
|
|
case PLAIN_TABLE_FULL_STR_PREFIX:
|
|
|
|
support_prev_ = false;
|
|
|
|
only_support_prefix_seek_ = true;
|
2014-01-03 18:53:21 +00:00
|
|
|
options_.prefix_extractor = noop_transform.get();
|
2013-12-20 17:35:24 +00:00
|
|
|
options_.allow_mmap_reads = true;
|
|
|
|
options_.table_factory.reset(new PlainTableFactory());
|
|
|
|
constructor_ = new TableConstructor(options_.comparator, true);
|
|
|
|
options_.comparator = internal_comparator_.get();
|
2011-03-18 22:37:00 +00:00
|
|
|
break;
|
|
|
|
case BLOCK_TEST:
|
|
|
|
constructor_ = new BlockConstructor(options_.comparator);
|
|
|
|
break;
|
|
|
|
case MEMTABLE_TEST:
|
|
|
|
constructor_ = new MemTableConstructor(options_.comparator);
|
|
|
|
break;
|
|
|
|
case DB_TEST:
|
|
|
|
constructor_ = new DBConstructor(options_.comparator);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~Harness() {
|
|
|
|
delete constructor_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Add(const std::string& key, const std::string& value) {
|
|
|
|
constructor_->Add(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Test(Random* rnd) {
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
KVMap data;
|
|
|
|
constructor_->Finish(options_, &keys, &data);
|
|
|
|
|
|
|
|
TestForwardScan(keys, data);
|
2013-12-20 17:35:24 +00:00
|
|
|
if (support_prev_) {
|
|
|
|
TestBackwardScan(keys, data);
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
TestRandomAccess(rnd, keys, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestForwardScan(const std::vector<std::string>& keys,
|
|
|
|
const KVMap& data) {
|
|
|
|
Iterator* iter = constructor_->NewIterator();
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
iter->SeekToFirst();
|
|
|
|
for (KVMap::const_iterator model_iter = data.begin();
|
|
|
|
model_iter != data.end();
|
|
|
|
++model_iter) {
|
|
|
|
ASSERT_EQ(ToString(data, model_iter), ToString(iter));
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestBackwardScan(const std::vector<std::string>& keys,
|
|
|
|
const KVMap& data) {
|
|
|
|
Iterator* iter = constructor_->NewIterator();
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
iter->SeekToLast();
|
|
|
|
for (KVMap::const_reverse_iterator model_iter = data.rbegin();
|
|
|
|
model_iter != data.rend();
|
|
|
|
++model_iter) {
|
|
|
|
ASSERT_EQ(ToString(data, model_iter), ToString(iter));
|
|
|
|
iter->Prev();
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestRandomAccess(Random* rnd,
|
|
|
|
const std::vector<std::string>& keys,
|
|
|
|
const KVMap& data) {
|
|
|
|
static const bool kVerbose = false;
|
|
|
|
Iterator* iter = constructor_->NewIterator();
|
|
|
|
ASSERT_TRUE(!iter->Valid());
|
|
|
|
KVMap::const_iterator model_iter = data.begin();
|
|
|
|
if (kVerbose) fprintf(stderr, "---\n");
|
|
|
|
for (int i = 0; i < 200; i++) {
|
2013-12-20 17:35:24 +00:00
|
|
|
const int toss = rnd->Uniform(support_prev_ ? 5 : 3);
|
2011-03-18 22:37:00 +00:00
|
|
|
switch (toss) {
|
|
|
|
case 0: {
|
|
|
|
if (iter->Valid()) {
|
|
|
|
if (kVerbose) fprintf(stderr, "Next\n");
|
|
|
|
iter->Next();
|
|
|
|
++model_iter;
|
|
|
|
ASSERT_EQ(ToString(data, model_iter), ToString(iter));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1: {
|
|
|
|
if (kVerbose) fprintf(stderr, "SeekToFirst\n");
|
|
|
|
iter->SeekToFirst();
|
|
|
|
model_iter = data.begin();
|
|
|
|
ASSERT_EQ(ToString(data, model_iter), ToString(iter));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 2: {
|
|
|
|
std::string key = PickRandomKey(rnd, keys);
|
|
|
|
model_iter = data.lower_bound(key);
|
|
|
|
if (kVerbose) fprintf(stderr, "Seek '%s'\n",
|
|
|
|
EscapeString(key).c_str());
|
|
|
|
iter->Seek(Slice(key));
|
|
|
|
ASSERT_EQ(ToString(data, model_iter), ToString(iter));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 3: {
|
|
|
|
if (iter->Valid()) {
|
|
|
|
if (kVerbose) fprintf(stderr, "Prev\n");
|
|
|
|
iter->Prev();
|
|
|
|
if (model_iter == data.begin()) {
|
|
|
|
model_iter = data.end(); // Wrap around to invalid value
|
|
|
|
} else {
|
|
|
|
--model_iter;
|
|
|
|
}
|
|
|
|
ASSERT_EQ(ToString(data, model_iter), ToString(iter));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 4: {
|
|
|
|
if (kVerbose) fprintf(stderr, "SeekToLast\n");
|
|
|
|
iter->SeekToLast();
|
|
|
|
if (keys.empty()) {
|
|
|
|
model_iter = data.end();
|
|
|
|
} else {
|
|
|
|
std::string last = data.rbegin()->first;
|
|
|
|
model_iter = data.lower_bound(last);
|
|
|
|
}
|
|
|
|
ASSERT_EQ(ToString(data, model_iter), ToString(iter));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString(const KVMap& data, const KVMap::const_iterator& it) {
|
|
|
|
if (it == data.end()) {
|
|
|
|
return "END";
|
|
|
|
} else {
|
|
|
|
return "'" + it->first + "->" + it->second + "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString(const KVMap& data,
|
|
|
|
const KVMap::const_reverse_iterator& it) {
|
|
|
|
if (it == data.rend()) {
|
|
|
|
return "END";
|
|
|
|
} else {
|
|
|
|
return "'" + it->first + "->" + it->second + "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString(const Iterator* it) {
|
|
|
|
if (!it->Valid()) {
|
|
|
|
return "END";
|
|
|
|
} else {
|
|
|
|
return "'" + it->key().ToString() + "->" + it->value().ToString() + "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PickRandomKey(Random* rnd, const std::vector<std::string>& keys) {
|
|
|
|
if (keys.empty()) {
|
|
|
|
return "foo";
|
|
|
|
} else {
|
|
|
|
const int index = rnd->Uniform(keys.size());
|
|
|
|
std::string result = keys[index];
|
2013-12-20 17:35:24 +00:00
|
|
|
switch (rnd->Uniform(support_prev_ ? 3 : 1)) {
|
2011-03-18 22:37:00 +00:00
|
|
|
case 0:
|
|
|
|
// Return an existing key
|
|
|
|
break;
|
|
|
|
case 1: {
|
|
|
|
// Attempt to return something smaller than an existing key
|
2013-12-20 17:35:24 +00:00
|
|
|
if (result.size() > 0 && result[result.size() - 1] > '\0'
|
|
|
|
&& (!only_support_prefix_seek_
|
|
|
|
|| options_.prefix_extractor->Transform(result).size()
|
|
|
|
< result.size())) {
|
|
|
|
result[result.size() - 1]--;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-12-20 17:35:24 +00:00
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
case 2: {
|
|
|
|
// Return something larger than an existing key
|
|
|
|
Increment(options_.comparator, &result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 02:04:58 +00:00
|
|
|
// Returns nullptr if not running against a DB
|
2011-03-21 19:40:57 +00:00
|
|
|
DB* db() const { return constructor_->db(); }
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
2013-11-20 06:00:48 +00:00
|
|
|
Options options_ = Options();
|
2011-03-18 22:37:00 +00:00
|
|
|
Constructor* constructor_;
|
2013-12-20 17:35:24 +00:00
|
|
|
bool support_prev_;
|
|
|
|
bool only_support_prefix_seek_;
|
|
|
|
shared_ptr<Comparator> internal_comparator_;
|
2014-01-03 18:53:21 +00:00
|
|
|
static std::unique_ptr<const SliceTransform> noop_transform;
|
2014-01-04 02:27:33 +00:00
|
|
|
static std::unique_ptr<const SliceTransform> prefix_transform;
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
2014-01-03 18:53:21 +00:00
|
|
|
std::unique_ptr<const SliceTransform> Harness::noop_transform(
|
|
|
|
NewNoopTransform());
|
2014-01-04 02:27:33 +00:00
|
|
|
std::unique_ptr<const SliceTransform> Harness::prefix_transform(
|
|
|
|
new FixedOrLessPrefixTransform(2));
|
2014-01-03 18:53:21 +00:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
static bool Between(uint64_t val, uint64_t low, uint64_t high) {
|
|
|
|
bool result = (val >= low) && (val <= high);
|
|
|
|
if (!result) {
|
|
|
|
fprintf(stderr, "Value %llu is not in range [%llu, %llu]\n",
|
|
|
|
(unsigned long long)(val),
|
|
|
|
(unsigned long long)(low),
|
|
|
|
(unsigned long long)(high));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-01-24 20:14:08 +00:00
|
|
|
// Tests against all kinds of tables
|
2014-01-25 05:10:19 +00:00
|
|
|
class GeneralTableTest {};
|
|
|
|
class BlockBasedTableTest {};
|
|
|
|
class PlainTableTest {};
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-10-10 18:43:24 +00:00
|
|
|
// This test include all the basic checks except those for index size and block
|
|
|
|
// size, which will be conducted in separated unit tests.
|
2014-01-24 20:14:08 +00:00
|
|
|
TEST(BlockBasedTableTest, BasicBlockBasedTableProperties) {
|
2013-12-20 17:35:24 +00:00
|
|
|
TableConstructor c(BytewiseComparator());
|
2013-10-10 18:43:24 +00:00
|
|
|
|
|
|
|
c.Add("a1", "val1");
|
|
|
|
c.Add("b2", "val2");
|
|
|
|
c.Add("c3", "val3");
|
|
|
|
c.Add("d4", "val4");
|
|
|
|
c.Add("e5", "val5");
|
|
|
|
c.Add("f6", "val6");
|
|
|
|
c.Add("g7", "val7");
|
|
|
|
c.Add("h8", "val8");
|
|
|
|
c.Add("j9", "val9");
|
|
|
|
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
KVMap kvmap;
|
2013-11-20 06:00:48 +00:00
|
|
|
Options options;
|
2013-10-10 18:43:24 +00:00
|
|
|
options.compression = kNoCompression;
|
|
|
|
options.block_restart_interval = 1;
|
|
|
|
|
|
|
|
c.Finish(options, &keys, &kvmap);
|
|
|
|
|
2013-11-20 00:29:42 +00:00
|
|
|
auto& props = c.table_reader()->GetTableProperties();
|
|
|
|
ASSERT_EQ(kvmap.size(), props.num_entries);
|
2013-10-10 18:43:24 +00:00
|
|
|
|
|
|
|
auto raw_key_size = kvmap.size() * 2ul;
|
|
|
|
auto raw_value_size = kvmap.size() * 4ul;
|
|
|
|
|
2013-11-20 00:29:42 +00:00
|
|
|
ASSERT_EQ(raw_key_size, props.raw_key_size);
|
|
|
|
ASSERT_EQ(raw_value_size, props.raw_value_size);
|
|
|
|
ASSERT_EQ(1ul, props.num_data_blocks);
|
|
|
|
ASSERT_EQ("", props.filter_policy_name); // no filter policy is used
|
2013-10-10 18:43:24 +00:00
|
|
|
|
|
|
|
// Verify data size.
|
2013-11-17 07:21:15 +00:00
|
|
|
BlockBuilder block_builder(options);
|
2013-10-10 18:43:24 +00:00
|
|
|
for (const auto& item : kvmap) {
|
|
|
|
block_builder.Add(item.first, item.second);
|
|
|
|
}
|
|
|
|
Slice content = block_builder.Finish();
|
|
|
|
ASSERT_EQ(
|
|
|
|
content.size() + kBlockTrailerSize,
|
2013-11-20 00:29:42 +00:00
|
|
|
props.data_size
|
2013-10-10 18:43:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-01-24 20:14:08 +00:00
|
|
|
TEST(BlockBasedTableTest, FilterPolicyNameProperties) {
|
2013-12-20 17:35:24 +00:00
|
|
|
TableConstructor c(BytewiseComparator());
|
2013-10-16 23:57:20 +00:00
|
|
|
c.Add("a1", "val1");
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
KVMap kvmap;
|
2013-11-20 06:00:48 +00:00
|
|
|
Options options;
|
2013-10-21 05:02:05 +00:00
|
|
|
std::unique_ptr<const FilterPolicy> filter_policy(
|
|
|
|
NewBloomFilterPolicy(10)
|
|
|
|
);
|
|
|
|
options.filter_policy = filter_policy.get();
|
2013-10-16 23:57:20 +00:00
|
|
|
|
|
|
|
c.Finish(options, &keys, &kvmap);
|
2013-11-20 00:29:42 +00:00
|
|
|
auto& props = c.table_reader()->GetTableProperties();
|
|
|
|
ASSERT_EQ("rocksdb.BuiltinBloomFilter", props.filter_policy_name);
|
2013-10-16 23:57:20 +00:00
|
|
|
}
|
|
|
|
|
2013-10-10 18:43:24 +00:00
|
|
|
static std::string RandomString(Random* rnd, int len) {
|
|
|
|
std::string r;
|
|
|
|
test::RandomString(rnd, len, &r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
// It's very hard to figure out the index block size of a block accurately.
|
|
|
|
// To make sure we get the index size, we just make sure as key number
|
|
|
|
// grows, the filter block size also grows.
|
2014-01-24 20:14:08 +00:00
|
|
|
TEST(BlockBasedTableTest, IndexSizeStat) {
|
2013-10-10 18:43:24 +00:00
|
|
|
uint64_t last_index_size = 0;
|
|
|
|
|
|
|
|
// we need to use random keys since the pure human readable texts
|
|
|
|
// may be well compressed, resulting insignifcant change of index
|
|
|
|
// block size.
|
|
|
|
Random rnd(test::RandomSeed());
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
keys.push_back(RandomString(&rnd, 10000));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each time we load one more key to the table. the table index block
|
|
|
|
// size is expected to be larger than last time's.
|
|
|
|
for (size_t i = 1; i < keys.size(); ++i) {
|
2013-12-20 17:35:24 +00:00
|
|
|
TableConstructor c(BytewiseComparator());
|
2013-10-10 18:43:24 +00:00
|
|
|
for (size_t j = 0; j < i; ++j) {
|
|
|
|
c.Add(keys[j], "val");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ks;
|
|
|
|
KVMap kvmap;
|
2013-11-20 06:00:48 +00:00
|
|
|
Options options;
|
2013-10-10 18:43:24 +00:00
|
|
|
options.compression = kNoCompression;
|
|
|
|
options.block_restart_interval = 1;
|
|
|
|
|
|
|
|
c.Finish(options, &ks, &kvmap);
|
|
|
|
auto index_size =
|
2013-11-20 00:29:42 +00:00
|
|
|
c.table_reader()->GetTableProperties().index_size;
|
2013-10-10 18:43:24 +00:00
|
|
|
ASSERT_GT(index_size, last_index_size);
|
|
|
|
last_index_size = index_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 20:14:08 +00:00
|
|
|
TEST(BlockBasedTableTest, NumBlockStat) {
|
2013-10-10 18:43:24 +00:00
|
|
|
Random rnd(test::RandomSeed());
|
2013-12-20 17:35:24 +00:00
|
|
|
TableConstructor c(BytewiseComparator());
|
2013-10-10 18:43:24 +00:00
|
|
|
Options options;
|
|
|
|
options.compression = kNoCompression;
|
|
|
|
options.block_restart_interval = 1;
|
|
|
|
options.block_size = 1000;
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
// the key/val are slightly smaller than block size, so that each block
|
|
|
|
// holds roughly one key/value pair.
|
|
|
|
c.Add(RandomString(&rnd, 900), "val");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> ks;
|
|
|
|
KVMap kvmap;
|
|
|
|
c.Finish(options, &ks, &kvmap);
|
|
|
|
ASSERT_EQ(
|
|
|
|
kvmap.size(),
|
2013-11-20 00:29:42 +00:00
|
|
|
c.table_reader()->GetTableProperties().num_data_blocks
|
2013-10-10 18:43:24 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-11-20 00:29:42 +00:00
|
|
|
class BlockCacheProperties {
|
2013-11-13 06:46:51 +00:00
|
|
|
public:
|
2013-12-03 19:17:58 +00:00
|
|
|
explicit BlockCacheProperties(Statistics* statistics) {
|
2014-01-17 20:46:06 +00:00
|
|
|
block_cache_miss = statistics->getTickerCount(BLOCK_CACHE_MISS);
|
|
|
|
block_cache_hit = statistics->getTickerCount(BLOCK_CACHE_HIT);
|
|
|
|
index_block_cache_miss = statistics->getTickerCount(BLOCK_CACHE_INDEX_MISS);
|
|
|
|
index_block_cache_hit = statistics->getTickerCount(BLOCK_CACHE_INDEX_HIT);
|
|
|
|
data_block_cache_miss = statistics->getTickerCount(BLOCK_CACHE_DATA_MISS);
|
|
|
|
data_block_cache_hit = statistics->getTickerCount(BLOCK_CACHE_DATA_HIT);
|
2013-11-13 06:46:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 00:29:42 +00:00
|
|
|
// Check if the fetched props matches the expected ones.
|
2013-11-13 06:46:51 +00:00
|
|
|
void AssertEqual(
|
|
|
|
long index_block_cache_miss,
|
|
|
|
long index_block_cache_hit,
|
|
|
|
long data_block_cache_miss,
|
|
|
|
long data_block_cache_hit) const {
|
|
|
|
ASSERT_EQ(index_block_cache_miss, this->index_block_cache_miss);
|
|
|
|
ASSERT_EQ(index_block_cache_hit, this->index_block_cache_hit);
|
|
|
|
ASSERT_EQ(data_block_cache_miss, this->data_block_cache_miss);
|
|
|
|
ASSERT_EQ(data_block_cache_hit, this->data_block_cache_hit);
|
|
|
|
ASSERT_EQ(
|
|
|
|
index_block_cache_miss + data_block_cache_miss,
|
|
|
|
this->block_cache_miss
|
|
|
|
);
|
|
|
|
ASSERT_EQ(
|
|
|
|
index_block_cache_hit + data_block_cache_hit,
|
|
|
|
this->block_cache_hit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
long block_cache_miss = 0;
|
|
|
|
long block_cache_hit = 0;
|
|
|
|
long index_block_cache_miss = 0;
|
|
|
|
long index_block_cache_hit = 0;
|
|
|
|
long data_block_cache_miss = 0;
|
|
|
|
long data_block_cache_hit = 0;
|
|
|
|
};
|
|
|
|
|
2014-01-24 20:14:08 +00:00
|
|
|
TEST(BlockBasedTableTest, BlockCacheTest) {
|
2013-11-13 06:46:51 +00:00
|
|
|
// -- Table construction
|
2013-11-20 06:00:48 +00:00
|
|
|
Options options;
|
2013-11-13 06:46:51 +00:00
|
|
|
options.create_if_missing = true;
|
|
|
|
options.statistics = CreateDBStatistics();
|
|
|
|
options.block_cache = NewLRUCache(1024);
|
2014-01-24 18:57:15 +00:00
|
|
|
|
|
|
|
// Enable the cache for index/filter blocks
|
|
|
|
BlockBasedTableOptions table_options;
|
|
|
|
table_options.cache_index_and_filter_blocks = true;
|
|
|
|
options.table_factory.reset(new BlockBasedTableFactory(table_options));
|
2013-11-13 06:46:51 +00:00
|
|
|
std::vector<std::string> keys;
|
|
|
|
KVMap kvmap;
|
|
|
|
|
2013-12-20 17:35:24 +00:00
|
|
|
TableConstructor c(BytewiseComparator());
|
2013-11-13 06:46:51 +00:00
|
|
|
c.Add("key", "value");
|
|
|
|
c.Finish(options, &keys, &kvmap);
|
|
|
|
|
|
|
|
// -- PART 1: Open with regular block cache.
|
|
|
|
// Since block_cache is disabled, no cache activities will be involved.
|
|
|
|
unique_ptr<Iterator> iter;
|
|
|
|
|
|
|
|
// At first, no block will be accessed.
|
|
|
|
{
|
2013-12-03 19:17:58 +00:00
|
|
|
BlockCacheProperties props(options.statistics.get());
|
2013-11-13 06:46:51 +00:00
|
|
|
// index will be added to block cache.
|
2013-11-20 00:29:42 +00:00
|
|
|
props.AssertEqual(
|
2013-11-13 06:46:51 +00:00
|
|
|
1, // index block miss
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only index block will be accessed
|
|
|
|
{
|
|
|
|
iter.reset(c.NewIterator());
|
2013-12-03 19:17:58 +00:00
|
|
|
BlockCacheProperties props(options.statistics.get());
|
2013-11-13 06:46:51 +00:00
|
|
|
// NOTE: to help better highlight the "detla" of each ticker, I use
|
|
|
|
// <last_value> + <added_value> to indicate the increment of changed
|
|
|
|
// value; other numbers remain the same.
|
2013-11-20 00:29:42 +00:00
|
|
|
props.AssertEqual(
|
2013-11-13 06:46:51 +00:00
|
|
|
1,
|
|
|
|
0 + 1, // index block hit
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only data block will be accessed
|
|
|
|
{
|
|
|
|
iter->SeekToFirst();
|
2013-12-03 19:17:58 +00:00
|
|
|
BlockCacheProperties props(options.statistics.get());
|
2013-11-20 00:29:42 +00:00
|
|
|
props.AssertEqual(
|
2013-11-13 06:46:51 +00:00
|
|
|
1,
|
|
|
|
1,
|
|
|
|
0 + 1, // data block miss
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data block will be in cache
|
|
|
|
{
|
|
|
|
iter.reset(c.NewIterator());
|
|
|
|
iter->SeekToFirst();
|
2013-12-03 19:17:58 +00:00
|
|
|
BlockCacheProperties props(options.statistics.get());
|
2013-11-20 00:29:42 +00:00
|
|
|
props.AssertEqual(
|
2013-11-13 06:46:51 +00:00
|
|
|
1,
|
|
|
|
1 + 1, // index block hit
|
|
|
|
1,
|
|
|
|
0 + 1 // data block hit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// release the iterator so that the block cache can reset correctly.
|
|
|
|
iter.reset();
|
|
|
|
|
|
|
|
// -- PART 2: Open without block cache
|
|
|
|
options.block_cache.reset();
|
2013-12-06 06:29:03 +00:00
|
|
|
options.statistics = CreateDBStatistics(); // reset the stats
|
2013-11-13 06:46:51 +00:00
|
|
|
c.Reopen(options);
|
|
|
|
|
|
|
|
{
|
|
|
|
iter.reset(c.NewIterator());
|
|
|
|
iter->SeekToFirst();
|
|
|
|
ASSERT_EQ("key", iter->key().ToString());
|
2013-12-03 19:17:58 +00:00
|
|
|
BlockCacheProperties props(options.statistics.get());
|
2013-11-13 06:46:51 +00:00
|
|
|
// Nothing is affected at all
|
2013-11-20 00:29:42 +00:00
|
|
|
props.AssertEqual(0, 0, 0, 0);
|
2013-11-13 06:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// -- PART 3: Open with very small block cache
|
|
|
|
// In this test, no block will ever get hit since the block cache is
|
|
|
|
// too small to fit even one entry.
|
|
|
|
options.block_cache = NewLRUCache(1);
|
|
|
|
c.Reopen(options);
|
|
|
|
{
|
2013-12-03 19:17:58 +00:00
|
|
|
BlockCacheProperties props(options.statistics.get());
|
2013-11-20 00:29:42 +00:00
|
|
|
props.AssertEqual(
|
2013-11-13 06:46:51 +00:00
|
|
|
1, // index block miss
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
// Both index and data block get accessed.
|
|
|
|
// It first cache index block then data block. But since the cache size
|
|
|
|
// is only 1, index block will be purged after data block is inserted.
|
|
|
|
iter.reset(c.NewIterator());
|
2013-12-03 19:17:58 +00:00
|
|
|
BlockCacheProperties props(options.statistics.get());
|
2013-11-20 00:29:42 +00:00
|
|
|
props.AssertEqual(
|
2013-11-13 06:46:51 +00:00
|
|
|
1 + 1, // index block miss
|
|
|
|
0,
|
|
|
|
0, // data block miss
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// SeekToFirst() accesses data block. With similar reason, we expect data
|
|
|
|
// block's cache miss.
|
|
|
|
iter->SeekToFirst();
|
2013-12-03 19:17:58 +00:00
|
|
|
BlockCacheProperties props(options.statistics.get());
|
2013-11-20 00:29:42 +00:00
|
|
|
props.AssertEqual(
|
2013-11-13 06:46:51 +00:00
|
|
|
2,
|
|
|
|
0,
|
|
|
|
0 + 1, // data block miss
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 20:14:08 +00:00
|
|
|
TEST(BlockBasedTableTest, BlockCacheLeak) {
|
|
|
|
// Check that when we reopen a table we don't lose access to blocks already
|
|
|
|
// in the cache. This test checks whether the Table actually makes use of the
|
|
|
|
// unique ID from the file.
|
|
|
|
|
|
|
|
Options opt;
|
|
|
|
opt.block_size = 1024;
|
|
|
|
opt.compression = kNoCompression;
|
2014-01-25 05:10:19 +00:00
|
|
|
opt.block_cache =
|
|
|
|
NewLRUCache(16 * 1024 * 1024); // big enough so we don't ever
|
|
|
|
// lose cached values.
|
2014-01-24 20:14:08 +00:00
|
|
|
|
|
|
|
TableConstructor c(BytewiseComparator());
|
|
|
|
c.Add("k01", "hello");
|
|
|
|
c.Add("k02", "hello2");
|
|
|
|
c.Add("k03", std::string(10000, 'x'));
|
|
|
|
c.Add("k04", std::string(200000, 'x'));
|
|
|
|
c.Add("k05", std::string(300000, 'x'));
|
|
|
|
c.Add("k06", "hello3");
|
|
|
|
c.Add("k07", std::string(100000, 'x'));
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
KVMap kvmap;
|
|
|
|
c.Finish(opt, &keys, &kvmap);
|
|
|
|
|
|
|
|
unique_ptr<Iterator> iter(c.NewIterator());
|
|
|
|
iter->SeekToFirst();
|
|
|
|
while (iter->Valid()) {
|
|
|
|
iter->key();
|
|
|
|
iter->value();
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
ASSERT_OK(iter->status());
|
|
|
|
|
|
|
|
ASSERT_OK(c.Reopen(opt));
|
2014-01-28 05:58:46 +00:00
|
|
|
auto table_reader = dynamic_cast<BlockBasedTable*>(c.table_reader());
|
2014-01-25 05:10:19 +00:00
|
|
|
for (const std::string& key : keys) {
|
2014-01-28 05:58:46 +00:00
|
|
|
ASSERT_TRUE(table_reader->TEST_KeyInCache(ReadOptions(), key));
|
2014-01-24 20:14:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern const uint64_t kPlainTableMagicNumber;
|
|
|
|
TEST(PlainTableTest, BasicPlainTableProperties) {
|
|
|
|
PlainTableFactory factory(8, 8, 0);
|
|
|
|
StringSink sink;
|
2014-01-25 05:10:19 +00:00
|
|
|
std::unique_ptr<TableBuilder> builder(
|
2014-01-28 05:58:46 +00:00
|
|
|
factory.NewTableBuilder(Options(), &sink, kNoCompression));
|
2014-01-24 20:14:08 +00:00
|
|
|
|
|
|
|
for (char c = 'a'; c <= 'z'; ++c) {
|
|
|
|
std::string key(16, c);
|
|
|
|
std::string value(28, c + 42);
|
|
|
|
builder->Add(key, value);
|
|
|
|
}
|
|
|
|
ASSERT_OK(builder->Finish());
|
|
|
|
|
|
|
|
StringSource source(sink.contents(), 72242, true);
|
|
|
|
|
|
|
|
TableProperties props;
|
2014-01-25 05:10:19 +00:00
|
|
|
auto s = ReadTableProperties(&source, sink.contents().size(),
|
|
|
|
kPlainTableMagicNumber, Env::Default(), nullptr,
|
|
|
|
&props);
|
2014-01-24 20:14:08 +00:00
|
|
|
ASSERT_OK(s);
|
|
|
|
|
|
|
|
ASSERT_EQ(0ul, props.index_size);
|
|
|
|
ASSERT_EQ(0ul, props.filter_size);
|
|
|
|
ASSERT_EQ(16ul * 26, props.raw_key_size);
|
|
|
|
ASSERT_EQ(28ul * 26, props.raw_value_size);
|
|
|
|
ASSERT_EQ(26ul, props.num_entries);
|
|
|
|
ASSERT_EQ(1ul, props.num_data_blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GeneralTableTest, ApproximateOffsetOfPlain) {
|
2013-12-20 17:35:24 +00:00
|
|
|
TableConstructor c(BytewiseComparator());
|
2011-03-18 22:37:00 +00:00
|
|
|
c.Add("k01", "hello");
|
|
|
|
c.Add("k02", "hello2");
|
|
|
|
c.Add("k03", std::string(10000, 'x'));
|
|
|
|
c.Add("k04", std::string(200000, 'x'));
|
|
|
|
c.Add("k05", std::string(300000, 'x'));
|
|
|
|
c.Add("k06", "hello3");
|
|
|
|
c.Add("k07", std::string(100000, 'x'));
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
KVMap kvmap;
|
2013-11-20 06:00:48 +00:00
|
|
|
Options options;
|
2011-03-18 22:37:00 +00:00
|
|
|
options.block_size = 1024;
|
|
|
|
options.compression = kNoCompression;
|
|
|
|
c.Finish(options, &keys, &kvmap);
|
|
|
|
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("abc"), 0, 0));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k01"), 0, 0));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k01a"), 0, 0));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k02"), 0, 0));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k03"), 0, 0));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k04"), 10000, 11000));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k04a"), 210000, 211000));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k05"), 210000, 211000));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k06"), 510000, 511000));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k07"), 510000, 511000));
|
2012-04-17 15:36:46 +00:00
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("xyz"), 610000, 612000));
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-24 19:09:04 +00:00
|
|
|
static void DoCompressionTest(CompressionType comp) {
|
2011-03-18 22:37:00 +00:00
|
|
|
Random rnd(301);
|
2013-12-20 17:35:24 +00:00
|
|
|
TableConstructor c(BytewiseComparator());
|
2011-03-18 22:37:00 +00:00
|
|
|
std::string tmp;
|
|
|
|
c.Add("k01", "hello");
|
|
|
|
c.Add("k02", test::CompressibleString(&rnd, 0.25, 10000, &tmp));
|
|
|
|
c.Add("k03", "hello3");
|
|
|
|
c.Add("k04", test::CompressibleString(&rnd, 0.25, 10000, &tmp));
|
|
|
|
std::vector<std::string> keys;
|
|
|
|
KVMap kvmap;
|
2013-11-20 06:00:48 +00:00
|
|
|
Options options;
|
2011-03-18 22:37:00 +00:00
|
|
|
options.block_size = 1024;
|
2012-06-28 06:41:33 +00:00
|
|
|
options.compression = comp;
|
2011-03-18 22:37:00 +00:00
|
|
|
c.Finish(options, &keys, &kvmap);
|
|
|
|
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("abc"), 0, 0));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k01"), 0, 0));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k02"), 0, 0));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k03"), 2000, 3000));
|
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("k04"), 2000, 3000));
|
2013-12-20 17:35:24 +00:00
|
|
|
ASSERT_TRUE(Between(c.ApproximateOffsetOf("xyz"), 4000, 6100));
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 20:14:08 +00:00
|
|
|
TEST(GeneralTableTest, ApproximateOffsetOfCompressed) {
|
2012-06-28 06:41:33 +00:00
|
|
|
CompressionType compression_state[2];
|
|
|
|
int valid = 0;
|
|
|
|
if (!SnappyCompressionSupported()) {
|
|
|
|
fprintf(stderr, "skipping snappy compression tests\n");
|
|
|
|
} else {
|
|
|
|
compression_state[valid] = kSnappyCompression;
|
|
|
|
valid++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ZlibCompressionSupported()) {
|
|
|
|
fprintf(stderr, "skipping zlib compression tests\n");
|
|
|
|
} else {
|
|
|
|
compression_state[valid] = kZlibCompression;
|
|
|
|
valid++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i =0; i < valid; i++)
|
|
|
|
{
|
2014-01-24 19:09:04 +00:00
|
|
|
DoCompressionTest(compression_state[i]);
|
2012-06-28 06:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-10 09:17:32 +00:00
|
|
|
TEST(Harness, Randomized) {
|
2013-11-20 06:00:48 +00:00
|
|
|
std::vector<TestArgs> args = GenerateArgList();
|
2013-11-10 09:17:32 +00:00
|
|
|
for (unsigned int i = 0; i < args.size(); i++) {
|
|
|
|
Init(args[i]);
|
|
|
|
Random rnd(test::RandomSeed() + 5);
|
|
|
|
for (int num_entries = 0; num_entries < 2000;
|
|
|
|
num_entries += (num_entries < 50 ? 1 : 200)) {
|
|
|
|
if ((num_entries % 10) == 0) {
|
|
|
|
fprintf(stderr, "case %d of %d: num_entries = %d\n",
|
|
|
|
(i + 1), int(args.size()), num_entries);
|
|
|
|
}
|
|
|
|
for (int e = 0; e < num_entries; e++) {
|
|
|
|
std::string v;
|
|
|
|
Add(test::RandomKey(&rnd, rnd.Skewed(4)),
|
|
|
|
test::RandomString(&rnd, rnd.Skewed(5), &v).ToString());
|
|
|
|
}
|
|
|
|
Test(&rnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Harness, RandomizedLongDB) {
|
|
|
|
Random rnd(test::RandomSeed());
|
|
|
|
TestArgs args = { DB_TEST, false, 16, kNoCompression };
|
|
|
|
Init(args);
|
|
|
|
int num_entries = 100000;
|
|
|
|
for (int e = 0; e < num_entries; e++) {
|
|
|
|
std::string v;
|
|
|
|
Add(test::RandomKey(&rnd, rnd.Skewed(4)),
|
|
|
|
test::RandomString(&rnd, rnd.Skewed(5), &v).ToString());
|
|
|
|
}
|
|
|
|
Test(&rnd);
|
|
|
|
|
|
|
|
// We must have created enough data to force merging
|
|
|
|
int files = 0;
|
|
|
|
for (int level = 0; level < db()->NumberLevels(); level++) {
|
|
|
|
std::string value;
|
|
|
|
char name[100];
|
|
|
|
snprintf(name, sizeof(name), "rocksdb.num-files-at-level%d", level);
|
|
|
|
ASSERT_TRUE(db()->GetProperty(name, &value));
|
|
|
|
files += atoi(value.c_str());
|
|
|
|
}
|
|
|
|
ASSERT_GT(files, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
class MemTableTest { };
|
|
|
|
|
|
|
|
TEST(MemTableTest, Simple) {
|
|
|
|
InternalKeyComparator cmp(BytewiseComparator());
|
|
|
|
auto table_factory = std::make_shared<SkipListFactory>();
|
2014-01-14 23:32:37 +00:00
|
|
|
Options options;
|
|
|
|
options.memtable_factory = table_factory;
|
|
|
|
MemTable* memtable = new MemTable(cmp, options);
|
2013-11-10 09:17:32 +00:00
|
|
|
memtable->Ref();
|
|
|
|
WriteBatch batch;
|
|
|
|
WriteBatchInternal::SetSequence(&batch, 100);
|
|
|
|
batch.Put(std::string("k1"), std::string("v1"));
|
|
|
|
batch.Put(std::string("k2"), std::string("v2"));
|
|
|
|
batch.Put(std::string("k3"), std::string("v3"));
|
|
|
|
batch.Put(std::string("largekey"), std::string("vlarge"));
|
|
|
|
ASSERT_TRUE(WriteBatchInternal::InsertInto(&batch, memtable, &options).ok());
|
|
|
|
|
|
|
|
Iterator* iter = memtable->NewIterator();
|
|
|
|
iter->SeekToFirst();
|
|
|
|
while (iter->Valid()) {
|
|
|
|
fprintf(stderr, "key: '%s' -> '%s'\n",
|
|
|
|
iter->key().ToString().c_str(),
|
|
|
|
iter->value().ToString().c_str());
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete iter;
|
2013-12-02 05:23:44 +00:00
|
|
|
delete memtable->Unref();
|
2013-11-10 09:17:32 +00:00
|
|
|
}
|
|
|
|
|
2013-12-06 00:51:26 +00:00
|
|
|
// Test the empty key
|
|
|
|
TEST(Harness, SimpleEmptyKey) {
|
2014-01-24 19:09:04 +00:00
|
|
|
auto args = GenerateArgList();
|
|
|
|
for (const auto& arg : args) {
|
|
|
|
Init(arg);
|
2013-12-06 00:51:26 +00:00
|
|
|
Random rnd(test::RandomSeed() + 1);
|
|
|
|
Add("", "v");
|
|
|
|
Test(&rnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Harness, SimpleSingle) {
|
2014-01-24 19:09:04 +00:00
|
|
|
auto args = GenerateArgList();
|
|
|
|
for (const auto& arg : args) {
|
|
|
|
Init(arg);
|
2013-12-06 00:51:26 +00:00
|
|
|
Random rnd(test::RandomSeed() + 2);
|
|
|
|
Add("abc", "v");
|
|
|
|
Test(&rnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Harness, SimpleMulti) {
|
2014-01-24 19:09:04 +00:00
|
|
|
auto args = GenerateArgList();
|
|
|
|
for (const auto& arg : args) {
|
|
|
|
Init(arg);
|
2013-12-06 00:51:26 +00:00
|
|
|
Random rnd(test::RandomSeed() + 3);
|
|
|
|
Add("abc", "v");
|
|
|
|
Add("abcd", "v");
|
|
|
|
Add("ac", "v2");
|
|
|
|
Test(&rnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Harness, SimpleSpecialKey) {
|
2014-01-24 19:09:04 +00:00
|
|
|
auto args = GenerateArgList();
|
|
|
|
for (const auto& arg : args) {
|
|
|
|
Init(arg);
|
2013-12-06 00:51:26 +00:00
|
|
|
Random rnd(test::RandomSeed() + 4);
|
|
|
|
Add("\xff\xff", "v3");
|
|
|
|
Test(&rnd);
|
|
|
|
}
|
|
|
|
}
|
2013-11-10 09:17:32 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2013-10-04 04:49:15 +00:00
|
|
|
return rocksdb::test::RunAllTests();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|