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.
156 lines
4.5 KiB
156 lines
4.5 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.
|
||
|
//
|
||
12 years ago
|
// Test for issue 178: a manual compaction causes deleted data to reappear.
|
||
|
#include <iostream>
|
||
|
#include <sstream>
|
||
|
#include <cstdlib>
|
||
|
|
||
11 years ago
|
#include "rocksdb/db.h"
|
||
11 years ago
|
#include "rocksdb/compaction_filter.h"
|
||
|
#include "rocksdb/slice.h"
|
||
11 years ago
|
#include "rocksdb/write_batch.h"
|
||
12 years ago
|
#include "util/testharness.h"
|
||
9 years ago
|
#include "port/port.h"
|
||
12 years ago
|
|
||
11 years ago
|
using namespace rocksdb;
|
||
|
|
||
12 years ago
|
namespace {
|
||
|
|
||
|
const int kNumKeys = 1100000;
|
||
|
|
||
|
std::string Key1(int i) {
|
||
|
char buf[100];
|
||
|
snprintf(buf, sizeof(buf), "my_key_%d", i);
|
||
|
return buf;
|
||
|
}
|
||
|
|
||
|
std::string Key2(int i) {
|
||
|
return Key1(i) + "_xxx";
|
||
|
}
|
||
|
|
||
10 years ago
|
class ManualCompactionTest : public testing::Test {
|
||
11 years ago
|
public:
|
||
|
ManualCompactionTest() {
|
||
|
// Get rid of any state from an old run.
|
||
|
dbname_ = rocksdb::test::TmpDir() + "/rocksdb_cbug_test";
|
||
|
DestroyDB(dbname_, rocksdb::Options());
|
||
|
}
|
||
|
|
||
|
std::string dbname_;
|
||
|
};
|
||
|
|
||
|
class DestroyAllCompactionFilter : public CompactionFilter {
|
||
|
public:
|
||
|
DestroyAllCompactionFilter() {}
|
||
|
|
||
10 years ago
|
virtual bool Filter(int level, const Slice& key, const Slice& existing_value,
|
||
11 years ago
|
std::string* new_value,
|
||
10 years ago
|
bool* value_changed) const override {
|
||
11 years ago
|
return existing_value.ToString() == "destroy";
|
||
|
}
|
||
|
|
||
10 years ago
|
virtual const char* Name() const override {
|
||
11 years ago
|
return "DestroyAllCompactionFilter";
|
||
|
}
|
||
|
};
|
||
|
|
||
10 years ago
|
TEST_F(ManualCompactionTest, CompactTouchesAllKeys) {
|
||
11 years ago
|
for (int iter = 0; iter < 2; ++iter) {
|
||
|
DB* db;
|
||
|
Options options;
|
||
|
if (iter == 0) { // level compaction
|
||
|
options.num_levels = 3;
|
||
|
options.compaction_style = kCompactionStyleLevel;
|
||
|
} else { // universal compaction
|
||
|
options.compaction_style = kCompactionStyleUniversal;
|
||
|
}
|
||
|
options.create_if_missing = true;
|
||
|
options.compression = rocksdb::kNoCompression;
|
||
|
options.compaction_filter = new DestroyAllCompactionFilter();
|
||
|
ASSERT_OK(DB::Open(options, dbname_, &db));
|
||
|
|
||
|
db->Put(WriteOptions(), Slice("key1"), Slice("destroy"));
|
||
|
db->Put(WriteOptions(), Slice("key2"), Slice("destroy"));
|
||
|
db->Put(WriteOptions(), Slice("key3"), Slice("value3"));
|
||
|
db->Put(WriteOptions(), Slice("key4"), Slice("destroy"));
|
||
|
|
||
|
Slice key4("key4");
|
||
10 years ago
|
db->CompactRange(CompactRangeOptions(), nullptr, &key4);
|
||
11 years ago
|
Iterator* itr = db->NewIterator(ReadOptions());
|
||
|
itr->SeekToFirst();
|
||
|
ASSERT_TRUE(itr->Valid());
|
||
|
ASSERT_EQ("key3", itr->key().ToString());
|
||
|
itr->Next();
|
||
|
ASSERT_TRUE(!itr->Valid());
|
||
|
delete itr;
|
||
|
|
||
|
delete options.compaction_filter;
|
||
|
delete db;
|
||
|
DestroyDB(dbname_, options);
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
TEST_F(ManualCompactionTest, Test) {
|
||
12 years ago
|
// Open database. Disable compression since it affects the creation
|
||
|
// of layers and the code below is trying to test against a very
|
||
|
// specific scenario.
|
||
11 years ago
|
rocksdb::DB* db;
|
||
|
rocksdb::Options db_options;
|
||
12 years ago
|
db_options.create_if_missing = true;
|
||
11 years ago
|
db_options.compression = rocksdb::kNoCompression;
|
||
11 years ago
|
ASSERT_OK(rocksdb::DB::Open(db_options, dbname_, &db));
|
||
12 years ago
|
|
||
|
// create first key range
|
||
11 years ago
|
rocksdb::WriteBatch batch;
|
||
12 years ago
|
for (int i = 0; i < kNumKeys; i++) {
|
||
|
batch.Put(Key1(i), "value for range 1 key");
|
||
|
}
|
||
11 years ago
|
ASSERT_OK(db->Write(rocksdb::WriteOptions(), &batch));
|
||
12 years ago
|
|
||
|
// create second key range
|
||
|
batch.Clear();
|
||
|
for (int i = 0; i < kNumKeys; i++) {
|
||
|
batch.Put(Key2(i), "value for range 2 key");
|
||
|
}
|
||
11 years ago
|
ASSERT_OK(db->Write(rocksdb::WriteOptions(), &batch));
|
||
12 years ago
|
|
||
|
// delete second key range
|
||
|
batch.Clear();
|
||
|
for (int i = 0; i < kNumKeys; i++) {
|
||
|
batch.Delete(Key2(i));
|
||
|
}
|
||
11 years ago
|
ASSERT_OK(db->Write(rocksdb::WriteOptions(), &batch));
|
||
12 years ago
|
|
||
|
// compact database
|
||
|
std::string start_key = Key1(0);
|
||
|
std::string end_key = Key1(kNumKeys - 1);
|
||
11 years ago
|
rocksdb::Slice least(start_key.data(), start_key.size());
|
||
|
rocksdb::Slice greatest(end_key.data(), end_key.size());
|
||
12 years ago
|
|
||
|
// commenting out the line below causes the example to work correctly
|
||
10 years ago
|
db->CompactRange(CompactRangeOptions(), &least, &greatest);
|
||
12 years ago
|
|
||
|
// count the keys
|
||
11 years ago
|
rocksdb::Iterator* iter = db->NewIterator(rocksdb::ReadOptions());
|
||
12 years ago
|
int num_keys = 0;
|
||
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
||
|
num_keys++;
|
||
|
}
|
||
|
delete iter;
|
||
|
ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys";
|
||
|
|
||
|
// close database
|
||
|
delete db;
|
||
11 years ago
|
DestroyDB(dbname_, rocksdb::Options());
|
||
12 years ago
|
}
|
||
|
|
||
|
} // anonymous namespace
|
||
|
|
||
|
int main(int argc, char** argv) {
|
||
10 years ago
|
::testing::InitGoogleTest(&argc, argv);
|
||
|
return RUN_ALL_TESTS();
|
||
12 years ago
|
}
|