2013-10-16 23:59:46 +02: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.
|
|
|
|
//
|
2012-12-20 20:05:41 +01:00
|
|
|
#include <string>
|
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "db/memtable.h"
|
|
|
|
#include "db/write_batch_internal.h"
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/iterator.h"
|
2013-10-29 01:54:09 +01:00
|
|
|
#include "rocksdb/table.h"
|
2012-12-20 20:05:41 +01:00
|
|
|
#include "table/block.h"
|
|
|
|
#include "table/block_builder.h"
|
|
|
|
#include "table/format.h"
|
|
|
|
#include "util/random.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2012-12-20 20:05:41 +01:00
|
|
|
|
|
|
|
static std::string RandomString(Random* rnd, int len) {
|
|
|
|
std::string r;
|
|
|
|
test::RandomString(rnd, len, &r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
class BlockTest {};
|
|
|
|
|
|
|
|
// block test
|
|
|
|
TEST(BlockTest, SimpleTest) {
|
|
|
|
Random rnd(301);
|
|
|
|
Options options = Options();
|
2014-01-27 22:53:22 +01:00
|
|
|
std::unique_ptr<InternalKeyComparator> ic;
|
|
|
|
ic.reset(new test::PlainInternalKeyComparator(options.comparator));
|
|
|
|
|
2012-12-20 20:05:41 +01:00
|
|
|
std::vector<std::string> keys;
|
|
|
|
std::vector<std::string> values;
|
2014-01-27 22:53:22 +01:00
|
|
|
BlockBuilder builder(options, ic.get());
|
2012-12-20 20:05:41 +01:00
|
|
|
int num_records = 100000;
|
|
|
|
char buf[10];
|
|
|
|
char* p = &buf[0];
|
|
|
|
|
|
|
|
// add a bunch of records to a block
|
|
|
|
for (int i = 0; i < num_records; i++) {
|
|
|
|
// generate random kvs
|
|
|
|
sprintf(p, "%6d", i);
|
|
|
|
std::string k(p);
|
|
|
|
std::string v = RandomString(&rnd, 100); // 100 byte values
|
|
|
|
|
|
|
|
// write kvs to the block
|
|
|
|
Slice key(k);
|
|
|
|
Slice value(v);
|
|
|
|
builder.Add(key, value);
|
|
|
|
|
|
|
|
// remember kvs in a lookaside array
|
|
|
|
keys.push_back(k);
|
|
|
|
values.push_back(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read serialized contents of the block
|
|
|
|
Slice rawblock = builder.Finish();
|
|
|
|
|
2013-04-23 00:20:20 +02:00
|
|
|
// create block reader
|
2012-12-20 20:05:41 +01:00
|
|
|
BlockContents contents;
|
|
|
|
contents.data = rawblock;
|
|
|
|
contents.cachable = false;
|
|
|
|
contents.heap_allocated = false;
|
|
|
|
Block reader(contents);
|
|
|
|
|
|
|
|
// read contents of block sequentially
|
|
|
|
int count = 0;
|
|
|
|
Iterator* iter = reader.NewIterator(options.comparator);
|
|
|
|
for (iter->SeekToFirst();iter->Valid(); count++, iter->Next()) {
|
2013-04-23 00:20:20 +02:00
|
|
|
|
2012-12-20 20:05:41 +01:00
|
|
|
// read kv from block
|
|
|
|
Slice k = iter->key();
|
|
|
|
Slice v = iter->value();
|
|
|
|
|
|
|
|
// compare with lookaside array
|
|
|
|
ASSERT_EQ(k.ToString().compare(keys[count]), 0);
|
|
|
|
ASSERT_EQ(v.ToString().compare(values[count]), 0);
|
|
|
|
}
|
|
|
|
delete iter;
|
2013-04-23 00:20:20 +02:00
|
|
|
|
|
|
|
// read block contents randomly
|
2012-12-20 20:05:41 +01:00
|
|
|
iter = reader.NewIterator(options.comparator);
|
|
|
|
for (int i = 0; i < num_records; i++) {
|
|
|
|
|
|
|
|
// find a random key in the lookaside array
|
|
|
|
int index = rnd.Uniform(num_records);
|
|
|
|
Slice k(keys[index]);
|
|
|
|
|
|
|
|
// search in block for this key
|
|
|
|
iter->Seek(k);
|
|
|
|
ASSERT_TRUE(iter->Valid());
|
|
|
|
Slice v = iter->value();
|
|
|
|
ASSERT_EQ(v.ToString().compare(values[index]), 0);
|
|
|
|
}
|
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|
2012-12-20 20:05:41 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2013-10-04 06:49:15 +02:00
|
|
|
return rocksdb::test::RunAllTests();
|
2012-12-20 20:05:41 +01:00
|
|
|
}
|