2013-10-16 20:50:50 +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.
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "db/db_impl.h"
|
2013-11-20 01:29:42 +01:00
|
|
|
#include "db/table_properties_collector.h"
|
|
|
|
#include "rocksdb/table_properties.h"
|
2013-10-29 01:54:09 +01:00
|
|
|
#include "rocksdb/table.h"
|
2013-12-06 01:51:26 +01:00
|
|
|
#include "rocksdb/plain_table_factory.h"
|
2013-11-20 07:00:48 +01:00
|
|
|
#include "table/block_based_table_factory.h"
|
2013-12-06 01:51:26 +01:00
|
|
|
#include "table/meta_blocks.h"
|
2013-10-16 20:50:50 +02:00
|
|
|
#include "util/coding.h"
|
|
|
|
#include "util/testharness.h"
|
|
|
|
#include "util/testutil.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
2013-11-20 01:29:42 +01:00
|
|
|
class TablePropertiesTest {
|
2013-10-16 20:50:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO(kailiu) the following classes should be moved to some more general
|
|
|
|
// places, so that other tests can also make use of them.
|
|
|
|
// `FakeWritableFile` and `FakeRandomeAccessFile` bypass the real file system
|
|
|
|
// and therefore enable us to quickly setup the tests.
|
|
|
|
class FakeWritableFile : public WritableFile {
|
|
|
|
public:
|
|
|
|
~FakeWritableFile() { }
|
|
|
|
|
|
|
|
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 FakeRandomeAccessFile : public RandomAccessFile {
|
|
|
|
public:
|
|
|
|
explicit FakeRandomeAccessFile(const Slice& contents)
|
|
|
|
: contents_(contents.data(), contents.size()) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~FakeRandomeAccessFile() { }
|
|
|
|
|
|
|
|
uint64_t Size() const { return contents_.size(); }
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
memcpy(scratch, &contents_[offset], n);
|
|
|
|
*result = Slice(scratch, n);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string contents_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class DumbLogger : public Logger {
|
|
|
|
public:
|
|
|
|
virtual void Logv(const char* format, va_list ap) { }
|
|
|
|
virtual size_t GetLogFileSize() const { return 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Utilities test functions
|
|
|
|
void MakeBuilder(
|
2013-11-20 07:00:48 +01:00
|
|
|
const Options& options,
|
2013-10-16 20:50:50 +02:00
|
|
|
std::unique_ptr<FakeWritableFile>* writable,
|
|
|
|
std::unique_ptr<TableBuilder>* builder) {
|
|
|
|
writable->reset(new FakeWritableFile);
|
|
|
|
builder->reset(
|
2013-10-30 18:52:33 +01:00
|
|
|
options.table_factory->GetTableBuilder(options, writable->get(),
|
|
|
|
options.compression));
|
2013-10-16 20:50:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Collects keys that starts with "A" in a table.
|
2013-11-20 01:29:42 +01:00
|
|
|
class RegularKeysStartWithA: public TablePropertiesCollector {
|
2013-10-16 20:50:50 +02:00
|
|
|
public:
|
|
|
|
const char* Name() const { return "RegularKeysStartWithA"; }
|
|
|
|
|
2013-12-05 22:09:13 +01:00
|
|
|
Status Finish(UserCollectedProperties* properties) {
|
2013-10-16 20:50:50 +02:00
|
|
|
std::string encoded;
|
|
|
|
PutVarint32(&encoded, count_);
|
2013-12-05 22:09:13 +01:00
|
|
|
*properties = UserCollectedProperties {
|
2013-11-20 01:29:42 +01:00
|
|
|
{ "TablePropertiesTest", "Rocksdb" },
|
2013-10-16 20:50:50 +02:00
|
|
|
{ "Count", encoded }
|
|
|
|
};
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Add(const Slice& user_key, const Slice& value) {
|
|
|
|
// simply asssume all user keys are not empty.
|
|
|
|
if (user_key.data()[0] == 'A') {
|
|
|
|
++count_;
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2013-12-05 22:09:13 +01:00
|
|
|
virtual UserCollectedProperties GetReadableProperties() const {
|
2013-11-20 01:29:42 +01:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-16 20:50:50 +02:00
|
|
|
private:
|
|
|
|
uint32_t count_ = 0;
|
|
|
|
};
|
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
extern uint64_t kBlockBasedTableMagicNumber;
|
|
|
|
extern uint64_t kPlainTableMagicNumber;
|
|
|
|
void TestCustomizedTablePropertiesCollector(
|
|
|
|
uint64_t magic_number,
|
|
|
|
bool encode_as_internal,
|
|
|
|
const Options& options) {
|
2013-10-16 20:50:50 +02:00
|
|
|
// make sure the entries will be inserted with order.
|
|
|
|
std::map<std::string, std::string> kvs = {
|
2013-12-06 01:51:26 +01:00
|
|
|
{"About ", "val5"}, // starts with 'A'
|
|
|
|
{"Abstract", "val2"}, // starts with 'A'
|
|
|
|
{"Around ", "val7"}, // starts with 'A'
|
|
|
|
{"Beyond ", "val3"},
|
|
|
|
{"Builder ", "val1"},
|
|
|
|
{"Cancel ", "val4"},
|
|
|
|
{"Find ", "val6"},
|
2013-10-16 20:50:50 +02:00
|
|
|
};
|
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
// -- Step 1: build table
|
|
|
|
std::unique_ptr<TableBuilder> builder;
|
|
|
|
std::unique_ptr<FakeWritableFile> writable;
|
|
|
|
MakeBuilder(options, &writable, &builder);
|
|
|
|
|
|
|
|
for (const auto& kv : kvs) {
|
|
|
|
if (encode_as_internal) {
|
|
|
|
InternalKey ikey(kv.first, 0, ValueType::kTypeValue);
|
|
|
|
builder->Add(ikey.Encode(), kv.second);
|
|
|
|
} else {
|
|
|
|
builder->Add(kv.first, kv.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT_OK(builder->Finish());
|
|
|
|
|
|
|
|
// -- Step 2: Read properties
|
|
|
|
FakeRandomeAccessFile readable(writable->contents());
|
|
|
|
TableProperties props;
|
|
|
|
Status s = ReadTableProperties(
|
|
|
|
&readable,
|
|
|
|
writable->contents().size(),
|
|
|
|
magic_number,
|
|
|
|
Env::Default(),
|
|
|
|
nullptr,
|
|
|
|
&props
|
|
|
|
);
|
|
|
|
ASSERT_OK(s);
|
|
|
|
|
|
|
|
auto user_collected = props.user_collected_properties;
|
|
|
|
|
|
|
|
ASSERT_EQ("Rocksdb", user_collected.at("TablePropertiesTest"));
|
|
|
|
|
|
|
|
uint32_t starts_with_A = 0;
|
|
|
|
Slice key(user_collected.at("Count"));
|
|
|
|
ASSERT_TRUE(GetVarint32(&key, &starts_with_A));
|
|
|
|
ASSERT_EQ(3u, starts_with_A);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TablePropertiesTest, CustomizedTablePropertiesCollector) {
|
2013-11-20 01:29:42 +01:00
|
|
|
// Test properties collectors with internal keys or regular keys
|
2013-12-06 01:51:26 +01:00
|
|
|
// for block based table
|
2013-10-16 20:50:50 +02:00
|
|
|
for (bool encode_as_internal : { true, false }) {
|
2013-12-06 01:51:26 +01:00
|
|
|
Options options;
|
2013-10-16 20:50:50 +02:00
|
|
|
auto collector = new RegularKeysStartWithA();
|
|
|
|
if (encode_as_internal) {
|
2013-11-20 01:29:42 +01:00
|
|
|
options.table_properties_collectors = {
|
|
|
|
std::make_shared<UserKeyTablePropertiesCollector>(collector)
|
2013-10-16 20:50:50 +02:00
|
|
|
};
|
|
|
|
} else {
|
2013-11-20 01:29:42 +01:00
|
|
|
options.table_properties_collectors.resize(1);
|
|
|
|
options.table_properties_collectors[0].reset(collector);
|
2013-10-16 20:50:50 +02:00
|
|
|
}
|
2013-12-06 01:51:26 +01:00
|
|
|
TestCustomizedTablePropertiesCollector(
|
|
|
|
kBlockBasedTableMagicNumber,
|
|
|
|
encode_as_internal,
|
|
|
|
options
|
|
|
|
);
|
2013-10-16 20:50:50 +02:00
|
|
|
}
|
2013-12-06 01:51:26 +01:00
|
|
|
|
|
|
|
// test plain table
|
|
|
|
Options options;
|
|
|
|
options.table_properties_collectors.push_back(
|
|
|
|
std::make_shared<RegularKeysStartWithA>()
|
|
|
|
);
|
|
|
|
options.table_factory = std::make_shared<PlainTableFactory>(8, 8, 0);
|
|
|
|
TestCustomizedTablePropertiesCollector(
|
|
|
|
kPlainTableMagicNumber, true, options
|
|
|
|
);
|
2013-10-16 20:50:50 +02:00
|
|
|
}
|
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
void TestInternalKeyPropertiesCollector(
|
|
|
|
uint64_t magic_number,
|
|
|
|
bool sanitized,
|
|
|
|
std::shared_ptr<TableFactory> table_factory) {
|
2013-10-16 20:50:50 +02:00
|
|
|
InternalKey keys[] = {
|
2013-12-06 01:51:26 +01:00
|
|
|
InternalKey("A ", 0, ValueType::kTypeValue),
|
|
|
|
InternalKey("B ", 0, ValueType::kTypeValue),
|
|
|
|
InternalKey("C ", 0, ValueType::kTypeValue),
|
|
|
|
InternalKey("W ", 0, ValueType::kTypeDeletion),
|
|
|
|
InternalKey("X ", 0, ValueType::kTypeDeletion),
|
|
|
|
InternalKey("Y ", 0, ValueType::kTypeDeletion),
|
|
|
|
InternalKey("Z ", 0, ValueType::kTypeDeletion),
|
2013-10-16 20:50:50 +02:00
|
|
|
};
|
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
std::unique_ptr<TableBuilder> builder;
|
|
|
|
std::unique_ptr<FakeWritableFile> writable;
|
|
|
|
Options options;
|
|
|
|
options.table_factory = table_factory;
|
|
|
|
if (sanitized) {
|
|
|
|
options.table_properties_collectors = {
|
|
|
|
std::make_shared<RegularKeysStartWithA>()
|
|
|
|
};
|
|
|
|
// with sanitization, even regular properties collector will be able to
|
|
|
|
// handle internal keys.
|
|
|
|
auto comparator = options.comparator;
|
|
|
|
// HACK: Set options.info_log to avoid writing log in
|
|
|
|
// SanitizeOptions().
|
|
|
|
options.info_log = std::make_shared<DumbLogger>();
|
|
|
|
options = SanitizeOptions(
|
|
|
|
"db", // just a place holder
|
|
|
|
nullptr, // with skip internal key comparator
|
|
|
|
nullptr, // don't care filter policy
|
|
|
|
options
|
|
|
|
);
|
|
|
|
options.comparator = comparator;
|
|
|
|
} else {
|
|
|
|
options.table_properties_collectors = {
|
|
|
|
std::make_shared<InternalKeyPropertiesCollector>()
|
|
|
|
};
|
|
|
|
}
|
2013-10-16 20:50:50 +02:00
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
MakeBuilder(options, &writable, &builder);
|
|
|
|
for (const auto& k : keys) {
|
|
|
|
builder->Add(k.Encode(), "val");
|
|
|
|
}
|
2013-10-16 20:50:50 +02:00
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
ASSERT_OK(builder->Finish());
|
|
|
|
|
|
|
|
FakeRandomeAccessFile readable(writable->contents());
|
|
|
|
TableProperties props;
|
|
|
|
Status s = ReadTableProperties(
|
|
|
|
&readable,
|
|
|
|
writable->contents().size(),
|
|
|
|
magic_number,
|
|
|
|
Env::Default(),
|
|
|
|
nullptr,
|
|
|
|
&props
|
|
|
|
);
|
|
|
|
ASSERT_OK(s);
|
2013-10-16 20:50:50 +02:00
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
auto user_collected = props.user_collected_properties;
|
|
|
|
uint64_t deleted = GetDeletedKeys(user_collected);
|
|
|
|
ASSERT_EQ(4u, deleted);
|
2013-10-16 20:50:50 +02:00
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
if (sanitized) {
|
|
|
|
uint32_t starts_with_A = 0;
|
|
|
|
Slice key(user_collected.at("Count"));
|
|
|
|
ASSERT_TRUE(GetVarint32(&key, &starts_with_A));
|
|
|
|
ASSERT_EQ(1u, starts_with_A);
|
2013-10-16 20:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 01:51:26 +01:00
|
|
|
TEST(TablePropertiesTest, InternalKeyPropertiesCollector) {
|
|
|
|
TestInternalKeyPropertiesCollector(
|
|
|
|
kBlockBasedTableMagicNumber,
|
|
|
|
true /* sanitize */,
|
|
|
|
std::make_shared<BlockBasedTableFactory>()
|
|
|
|
);
|
|
|
|
TestInternalKeyPropertiesCollector(
|
|
|
|
kBlockBasedTableMagicNumber,
|
|
|
|
true /* not sanitize */,
|
|
|
|
std::make_shared<BlockBasedTableFactory>()
|
|
|
|
);
|
|
|
|
TestInternalKeyPropertiesCollector(
|
|
|
|
kPlainTableMagicNumber,
|
|
|
|
false /* not sanitize */,
|
|
|
|
std::make_shared<PlainTableFactory>(8, 8, 0)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-16 20:50:50 +02:00
|
|
|
} // namespace rocksdb
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
return rocksdb::test::RunAllTests();
|
|
|
|
}
|