2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2014-03-06 20:36:39 +01:00
|
|
|
|
|
|
|
#include <cstdio>
|
2014-05-15 06:31:03 +02:00
|
|
|
#include <cstdlib>
|
2014-03-06 20:36:39 +01:00
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
|
2014-09-09 03:57:40 +02:00
|
|
|
#include "rocksdb/db.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/status.h"
|
|
|
|
#include "rocksdb/comparator.h"
|
|
|
|
#include "rocksdb/table.h"
|
|
|
|
#include "rocksdb/slice_transform.h"
|
|
|
|
#include "rocksdb/filter_policy.h"
|
2014-11-25 05:44:49 +01:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "util/string_util.h"
|
2014-03-06 20:36:39 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-03-06 20:36:39 +01:00
|
|
|
|
|
|
|
class SanityTest {
|
|
|
|
public:
|
|
|
|
explicit SanityTest(const std::string& path)
|
|
|
|
: env_(Env::Default()), path_(path) {
|
|
|
|
env_->CreateDirIfMissing(path);
|
|
|
|
}
|
|
|
|
virtual ~SanityTest() {}
|
|
|
|
|
|
|
|
virtual std::string Name() const = 0;
|
|
|
|
virtual Options GetOptions() const = 0;
|
|
|
|
|
|
|
|
Status Create() {
|
|
|
|
Options options = GetOptions();
|
|
|
|
options.create_if_missing = true;
|
|
|
|
std::string dbname = path_ + Name();
|
|
|
|
DestroyDB(dbname, options);
|
2015-07-22 03:04:28 +02:00
|
|
|
DB* db = nullptr;
|
2014-03-06 20:36:39 +01:00
|
|
|
Status s = DB::Open(options, dbname, &db);
|
|
|
|
std::unique_ptr<DB> db_guard(db);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 1000000; ++i) {
|
2022-05-06 22:03:58 +02:00
|
|
|
std::string k = "key" + std::to_string(i);
|
|
|
|
std::string v = "value" + std::to_string(i);
|
2014-03-06 20:36:39 +01:00
|
|
|
s = db->Put(WriteOptions(), Slice(k), Slice(v));
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
2014-09-09 03:57:40 +02:00
|
|
|
return db->Flush(FlushOptions());
|
2014-03-06 20:36:39 +01:00
|
|
|
}
|
|
|
|
Status Verify() {
|
2015-07-22 03:04:28 +02:00
|
|
|
DB* db = nullptr;
|
2014-03-06 20:36:39 +01:00
|
|
|
std::string dbname = path_ + Name();
|
|
|
|
Status s = DB::Open(GetOptions(), dbname, &db);
|
|
|
|
std::unique_ptr<DB> db_guard(db);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 1000000; ++i) {
|
2022-05-06 22:03:58 +02:00
|
|
|
std::string k = "key" + std::to_string(i);
|
|
|
|
std::string v = "value" + std::to_string(i);
|
2014-03-06 20:36:39 +01:00
|
|
|
std::string result;
|
|
|
|
s = db->Get(ReadOptions(), Slice(k), &result);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
if (result != v) {
|
|
|
|
return Status::Corruption("Unexpected value for key " + k);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Env* env_;
|
|
|
|
std::string const path_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SanityTestBasic : public SanityTest {
|
|
|
|
public:
|
|
|
|
explicit SanityTestBasic(const std::string& path) : SanityTest(path) {}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Options GetOptions() const override {
|
2014-03-06 20:36:39 +01:00
|
|
|
Options options;
|
|
|
|
options.create_if_missing = true;
|
|
|
|
return options;
|
|
|
|
}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual std::string Name() const override { return "Basic"; }
|
2014-03-06 20:36:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class SanityTestSpecialComparator : public SanityTest {
|
|
|
|
public:
|
|
|
|
explicit SanityTestSpecialComparator(const std::string& path)
|
|
|
|
: SanityTest(path) {
|
|
|
|
options_.comparator = new NewComparator();
|
|
|
|
}
|
|
|
|
~SanityTestSpecialComparator() { delete options_.comparator; }
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Options GetOptions() const override { return options_; }
|
|
|
|
virtual std::string Name() const override { return "SpecialComparator"; }
|
2014-03-06 20:36:39 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
class NewComparator : public Comparator {
|
|
|
|
public:
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual const char* Name() const override {
|
|
|
|
return "rocksdb.NewComparator";
|
|
|
|
}
|
|
|
|
virtual int Compare(const Slice& a, const Slice& b) const override {
|
2014-03-06 20:36:39 +01:00
|
|
|
return BytewiseComparator()->Compare(a, b);
|
|
|
|
}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual void FindShortestSeparator(std::string* s,
|
|
|
|
const Slice& l) const override {
|
2014-03-06 20:36:39 +01:00
|
|
|
BytewiseComparator()->FindShortestSeparator(s, l);
|
|
|
|
}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual void FindShortSuccessor(std::string* key) const override {
|
2014-03-06 20:36:39 +01:00
|
|
|
BytewiseComparator()->FindShortSuccessor(key);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Options options_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SanityTestZlibCompression : public SanityTest {
|
|
|
|
public:
|
|
|
|
explicit SanityTestZlibCompression(const std::string& path)
|
|
|
|
: SanityTest(path) {
|
|
|
|
options_.compression = kZlibCompression;
|
|
|
|
}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Options GetOptions() const override { return options_; }
|
|
|
|
virtual std::string Name() const override { return "ZlibCompression"; }
|
2014-03-06 20:36:39 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Options options_;
|
|
|
|
};
|
|
|
|
|
2015-01-15 01:24:24 +01:00
|
|
|
class SanityTestZlibCompressionVersion2 : public SanityTest {
|
|
|
|
public:
|
|
|
|
explicit SanityTestZlibCompressionVersion2(const std::string& path)
|
|
|
|
: SanityTest(path) {
|
|
|
|
options_.compression = kZlibCompression;
|
|
|
|
BlockBasedTableOptions table_options;
|
2015-03-27 19:32:49 +01:00
|
|
|
#if ROCKSDB_MAJOR > 3 || (ROCKSDB_MAJOR == 3 && ROCKSDB_MINOR >= 10)
|
2015-01-15 01:24:24 +01:00
|
|
|
table_options.format_version = 2;
|
2015-03-27 19:32:49 +01:00
|
|
|
#endif
|
2015-01-15 01:24:24 +01:00
|
|
|
options_.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
|
|
|
}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Options GetOptions() const override { return options_; }
|
|
|
|
virtual std::string Name() const override {
|
|
|
|
return "ZlibCompressionVersion2";
|
|
|
|
}
|
2015-01-15 01:24:24 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Options options_;
|
|
|
|
};
|
|
|
|
|
2015-01-14 18:45:29 +01:00
|
|
|
class SanityTestLZ4Compression : public SanityTest {
|
|
|
|
public:
|
|
|
|
explicit SanityTestLZ4Compression(const std::string& path)
|
|
|
|
: SanityTest(path) {
|
|
|
|
options_.compression = kLZ4Compression;
|
|
|
|
}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Options GetOptions() const override { return options_; }
|
|
|
|
virtual std::string Name() const override { return "LZ4Compression"; }
|
2015-01-14 18:45:29 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Options options_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SanityTestLZ4HCCompression : public SanityTest {
|
|
|
|
public:
|
|
|
|
explicit SanityTestLZ4HCCompression(const std::string& path)
|
|
|
|
: SanityTest(path) {
|
|
|
|
options_.compression = kLZ4HCCompression;
|
|
|
|
}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Options GetOptions() const override { return options_; }
|
|
|
|
virtual std::string Name() const override { return "LZ4HCCompression"; }
|
2015-01-14 18:45:29 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Options options_;
|
|
|
|
};
|
|
|
|
|
2015-08-28 00:40:42 +02:00
|
|
|
class SanityTestZSTDCompression : public SanityTest {
|
|
|
|
public:
|
|
|
|
explicit SanityTestZSTDCompression(const std::string& path)
|
|
|
|
: SanityTest(path) {
|
2016-09-02 00:28:40 +02:00
|
|
|
options_.compression = kZSTD;
|
2015-08-28 00:40:42 +02:00
|
|
|
}
|
|
|
|
virtual Options GetOptions() const override { return options_; }
|
|
|
|
virtual std::string Name() const override { return "ZSTDCompression"; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Options options_;
|
|
|
|
};
|
|
|
|
|
2014-11-12 22:05:12 +01:00
|
|
|
#ifndef ROCKSDB_LITE
|
2014-03-06 20:36:39 +01:00
|
|
|
class SanityTestPlainTableFactory : public SanityTest {
|
|
|
|
public:
|
|
|
|
explicit SanityTestPlainTableFactory(const std::string& path)
|
|
|
|
: SanityTest(path) {
|
|
|
|
options_.table_factory.reset(NewPlainTableFactory());
|
2014-04-01 02:06:53 +02:00
|
|
|
options_.prefix_extractor.reset(NewFixedPrefixTransform(2));
|
2014-03-06 20:36:39 +01:00
|
|
|
options_.allow_mmap_reads = true;
|
|
|
|
}
|
2014-04-01 02:06:53 +02:00
|
|
|
~SanityTestPlainTableFactory() {}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Options GetOptions() const override { return options_; }
|
|
|
|
virtual std::string Name() const override { return "PlainTable"; }
|
2014-03-06 20:36:39 +01:00
|
|
|
|
|
|
|
private:
|
|
|
|
Options options_;
|
|
|
|
};
|
2014-11-12 22:05:12 +01:00
|
|
|
#endif // ROCKSDB_LITE
|
2014-03-06 20:36:39 +01:00
|
|
|
|
2014-09-08 19:37:05 +02:00
|
|
|
class SanityTestBloomFilter : public SanityTest {
|
|
|
|
public:
|
2014-09-09 03:57:40 +02:00
|
|
|
explicit SanityTestBloomFilter(const std::string& path) : SanityTest(path) {
|
|
|
|
BlockBasedTableOptions table_options;
|
|
|
|
table_options.filter_policy.reset(NewBloomFilterPolicy(10));
|
|
|
|
options_.table_factory.reset(NewBlockBasedTableFactory(table_options));
|
2014-09-08 19:37:05 +02:00
|
|
|
}
|
|
|
|
~SanityTestBloomFilter() {}
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual Options GetOptions() const override { return options_; }
|
|
|
|
virtual std::string Name() const override { return "BloomFilter"; }
|
2014-09-08 19:37:05 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
Options options_;
|
|
|
|
};
|
|
|
|
|
2014-04-10 06:17:14 +02:00
|
|
|
namespace {
|
2014-03-06 20:36:39 +01:00
|
|
|
bool RunSanityTests(const std::string& command, const std::string& path) {
|
2016-07-09 02:50:51 +02:00
|
|
|
bool result = true;
|
|
|
|
// Suppress false positive clang static anaylzer warnings.
|
|
|
|
#ifndef __clang_analyzer__
|
2014-03-06 20:36:39 +01:00
|
|
|
std::vector<SanityTest*> sanity_tests = {
|
2015-08-28 00:40:42 +02:00
|
|
|
new SanityTestBasic(path),
|
|
|
|
new SanityTestSpecialComparator(path),
|
2014-03-06 20:36:39 +01:00
|
|
|
new SanityTestZlibCompression(path),
|
2015-01-15 01:24:24 +01:00
|
|
|
new SanityTestZlibCompressionVersion2(path),
|
2015-01-14 18:45:29 +01:00
|
|
|
new SanityTestLZ4Compression(path),
|
|
|
|
new SanityTestLZ4HCCompression(path),
|
2015-08-28 00:40:42 +02:00
|
|
|
new SanityTestZSTDCompression(path),
|
2014-11-12 22:05:12 +01:00
|
|
|
#ifndef ROCKSDB_LITE
|
2014-09-08 19:37:05 +02:00
|
|
|
new SanityTestPlainTableFactory(path),
|
2014-11-12 22:05:12 +01:00
|
|
|
#endif // ROCKSDB_LITE
|
2014-09-08 19:37:05 +02:00
|
|
|
new SanityTestBloomFilter(path)};
|
2014-03-06 20:36:39 +01:00
|
|
|
|
|
|
|
if (command == "create") {
|
|
|
|
fprintf(stderr, "Creating...\n");
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Verifying...\n");
|
|
|
|
}
|
|
|
|
for (auto sanity_test : sanity_tests) {
|
|
|
|
Status s;
|
|
|
|
fprintf(stderr, "%s -- ", sanity_test->Name().c_str());
|
|
|
|
if (command == "create") {
|
|
|
|
s = sanity_test->Create();
|
|
|
|
} else {
|
|
|
|
assert(command == "verify");
|
|
|
|
s = sanity_test->Verify();
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s\n", s.ToString().c_str());
|
|
|
|
if (!s.ok()) {
|
|
|
|
fprintf(stderr, "FAIL\n");
|
2015-01-15 01:24:24 +01:00
|
|
|
result = false;
|
2014-03-06 20:36:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
delete sanity_test;
|
|
|
|
}
|
2016-07-09 02:50:51 +02:00
|
|
|
#endif // __clang_analyzer__
|
2015-01-15 01:24:24 +01:00
|
|
|
return result;
|
2014-03-06 20:36:39 +01:00
|
|
|
}
|
2014-04-10 06:17:14 +02:00
|
|
|
} // namespace
|
2014-03-06 20:36:39 +01:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-03-06 20:36:39 +01:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
std::string path, command;
|
|
|
|
bool ok = (argc == 3);
|
|
|
|
if (ok) {
|
|
|
|
path = std::string(argv[1]);
|
|
|
|
command = std::string(argv[2]);
|
|
|
|
ok = (command == "create" || command == "verify");
|
|
|
|
}
|
|
|
|
if (!ok) {
|
|
|
|
fprintf(stderr, "Usage: %s <path> [create|verify] \n", argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (path.back() != '/') {
|
|
|
|
path += "/";
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
bool sanity_ok = ROCKSDB_NAMESPACE::RunSanityTests(command, path);
|
2014-03-06 20:36:39 +01:00
|
|
|
|
|
|
|
return sanity_ok ? 0 : 1;
|
|
|
|
}
|