2013-08-22 02:12:47 +08:00
|
|
|
// Copyright (c) 2013 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.
|
|
|
|
|
2019-11-26 01:29:06 +08:00
|
|
|
#include "gtest/gtest.h"
|
2013-08-22 02:12:47 +08:00
|
|
|
#include "db/db_impl.h"
|
|
|
|
#include "leveldb/cache.h"
|
2019-05-03 02:01:00 +08:00
|
|
|
#include "leveldb/db.h"
|
2013-08-22 02:12:47 +08:00
|
|
|
#include "util/testutil.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
class AutoCompactTest : public testing::Test {
|
2013-08-22 02:12:47 +08:00
|
|
|
public:
|
|
|
|
AutoCompactTest() {
|
2019-11-22 05:09:53 +08:00
|
|
|
dbname_ = testing::TempDir() + "autocompact_test";
|
2013-08-22 02:12:47 +08:00
|
|
|
tiny_cache_ = NewLRUCache(100);
|
|
|
|
options_.block_cache = tiny_cache_;
|
|
|
|
DestroyDB(dbname_, options_);
|
|
|
|
options_.create_if_missing = true;
|
|
|
|
options_.compression = kNoCompression;
|
2019-11-22 05:09:53 +08:00
|
|
|
EXPECT_LEVELDB_OK(DB::Open(options_, dbname_, &db_));
|
2013-08-22 02:12:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~AutoCompactTest() {
|
|
|
|
delete db_;
|
|
|
|
DestroyDB(dbname_, Options());
|
|
|
|
delete tiny_cache_;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Key(int i) {
|
|
|
|
char buf[100];
|
2020-04-30 06:31:41 +08:00
|
|
|
std::snprintf(buf, sizeof(buf), "key%06d", i);
|
2013-08-22 02:12:47 +08:00
|
|
|
return std::string(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t Size(const Slice& start, const Slice& limit) {
|
|
|
|
Range r(start, limit);
|
|
|
|
uint64_t size;
|
|
|
|
db_->GetApproximateSizes(&r, 1, &size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoReads(int n);
|
2019-05-04 00:31:18 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::string dbname_;
|
|
|
|
Cache* tiny_cache_;
|
|
|
|
Options options_;
|
|
|
|
DB* db_;
|
2013-08-22 02:12:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const int kValueSize = 200 * 1024;
|
|
|
|
static const int kTotalSize = 100 * 1024 * 1024;
|
|
|
|
static const int kCount = kTotalSize / kValueSize;
|
|
|
|
|
|
|
|
// Read through the first n keys repeatedly and check that they get
|
|
|
|
// compacted (verified by checking the size of the key space).
|
|
|
|
void AutoCompactTest::DoReads(int n) {
|
|
|
|
std::string value(kValueSize, 'x');
|
|
|
|
DBImpl* dbi = reinterpret_cast<DBImpl*>(db_);
|
|
|
|
|
|
|
|
// Fill database
|
|
|
|
for (int i = 0; i < kCount; i++) {
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(db_->Put(WriteOptions(), Key(i), value));
|
2013-08-22 02:12:47 +08:00
|
|
|
}
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(dbi->TEST_CompactMemTable());
|
2013-08-22 02:12:47 +08:00
|
|
|
|
|
|
|
// Delete everything
|
|
|
|
for (int i = 0; i < kCount; i++) {
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(db_->Delete(WriteOptions(), Key(i)));
|
2013-08-22 02:12:47 +08:00
|
|
|
}
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(dbi->TEST_CompactMemTable());
|
2013-08-22 02:12:47 +08:00
|
|
|
|
|
|
|
// Get initial measurement of the space we will be reading.
|
|
|
|
const int64_t initial_size = Size(Key(0), Key(n));
|
|
|
|
const int64_t initial_other_size = Size(Key(n), Key(kCount));
|
|
|
|
|
|
|
|
// Read until size drops significantly.
|
|
|
|
std::string limit_key = Key(n);
|
|
|
|
for (int read = 0; true; read++) {
|
|
|
|
ASSERT_LT(read, 100) << "Taking too long to compact";
|
|
|
|
Iterator* iter = db_->NewIterator(ReadOptions());
|
|
|
|
for (iter->SeekToFirst();
|
2019-05-03 02:01:00 +08:00
|
|
|
iter->Valid() && iter->key().ToString() < limit_key; iter->Next()) {
|
2013-08-22 02:12:47 +08:00
|
|
|
// Drop data
|
|
|
|
}
|
|
|
|
delete iter;
|
|
|
|
// Wait a little bit to allow any triggered compactions to complete.
|
|
|
|
Env::Default()->SleepForMicroseconds(1000000);
|
|
|
|
uint64_t size = Size(Key(0), Key(n));
|
2020-04-30 06:31:41 +08:00
|
|
|
std::fprintf(stderr, "iter %3d => %7.3f MB [other %7.3f MB]\n", read + 1,
|
|
|
|
size / 1048576.0, Size(Key(n), Key(kCount)) / 1048576.0);
|
2019-05-03 02:01:00 +08:00
|
|
|
if (size <= initial_size / 10) {
|
2013-08-22 02:12:47 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that the size of the key space not touched by the reads
|
|
|
|
// is pretty much unchanged.
|
|
|
|
const int64_t final_other_size = Size(Key(n), Key(kCount));
|
|
|
|
ASSERT_LE(final_other_size, initial_other_size + 1048576);
|
2019-05-03 02:01:00 +08:00
|
|
|
ASSERT_GE(final_other_size, initial_other_size / 5 - 1048576);
|
2013-08-22 02:12:47 +08:00
|
|
|
}
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(AutoCompactTest, ReadAll) { DoReads(kCount); }
|
2013-08-22 02:12:47 +08:00
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(AutoCompactTest, ReadHalf) { DoReads(kCount / 2); }
|
2013-08-22 02:12:47 +08:00
|
|
|
|
|
|
|
} // namespace leveldb
|