|
| 1 | +// Use of this source code is governed by a BSD-style license that can be |
| 2 | +// found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 3 | +// Copyright (c) 2013, Facebook, Inc. All rights reserved. |
| 4 | +// This source code is licensed under the BSD-style license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. An additional grant |
| 6 | +// of patent rights can be found in the PATENTS file in the same directory. |
| 7 | +// |
| 8 | +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
| 9 | +// Use of this source code is governed by a BSD-style license that can be |
| 10 | +// found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 11 | +#include <algorithm> |
| 12 | +#include <set> |
| 13 | + |
| 14 | +#include "rocksdb/db.h" |
| 15 | +#include "rocksdb/filter_policy.h" |
| 16 | +#include "db/db_impl.h" |
| 17 | +#include "db/filename.h" |
| 18 | +#include "db/version_set.h" |
| 19 | +#include "db/write_batch_internal.h" |
| 20 | +#include "db/db_statistics.h" |
| 21 | +#include "rocksdb/cache.h" |
| 22 | +#include "rocksdb/compaction_filter.h" |
| 23 | +#include "rocksdb/env.h" |
| 24 | +#include "rocksdb/table.h" |
| 25 | +#include "rocksdb/plain_table_factory.h" |
| 26 | +#include "util/hash.h" |
| 27 | +#include "util/logging.h" |
| 28 | +#include "util/mutexlock.h" |
| 29 | +#include "util/testharness.h" |
| 30 | +#include "util/testutil.h" |
| 31 | +#include "utilities/merge_operators.h" |
| 32 | + |
| 33 | +using std::unique_ptr; |
| 34 | + |
| 35 | +namespace rocksdb { |
| 36 | + |
| 37 | +class PlainTableDBTest { |
| 38 | +protected: |
| 39 | +public: |
| 40 | + std::string dbname_; |
| 41 | + Env* env_; |
| 42 | + DB* db_; |
| 43 | + |
| 44 | + Options last_options_; |
| 45 | + |
| 46 | + PlainTableDBTest() : |
| 47 | + env_(Env::Default()) { |
| 48 | + dbname_ = test::TmpDir() + "/plain_table_db_test"; |
| 49 | + ASSERT_OK(DestroyDB(dbname_, Options())); |
| 50 | + db_ = nullptr; |
| 51 | + Reopen(); |
| 52 | + } |
| 53 | + |
| 54 | + ~PlainTableDBTest() { |
| 55 | + delete db_; |
| 56 | + ASSERT_OK(DestroyDB(dbname_, Options())); |
| 57 | + } |
| 58 | + |
| 59 | + // Return the current option configuration. |
| 60 | + Options CurrentOptions() { |
| 61 | + Options options; |
| 62 | + options.table_factory.reset(new PlainTableFactory(16, 8)); |
| 63 | + options.allow_mmap_reads = true; |
| 64 | + return options; |
| 65 | + } |
| 66 | + |
| 67 | + DBImpl* dbfull() { |
| 68 | + return reinterpret_cast<DBImpl*>(db_); |
| 69 | + } |
| 70 | + |
| 71 | + void Reopen(Options* options = nullptr) { |
| 72 | + ASSERT_OK(TryReopen(options)); |
| 73 | + } |
| 74 | + |
| 75 | + void Close() { |
| 76 | + delete db_; |
| 77 | + db_ = nullptr; |
| 78 | + } |
| 79 | + |
| 80 | + void DestroyAndReopen(Options* options = nullptr) { |
| 81 | + //Destroy using last options |
| 82 | + Destroy(&last_options_); |
| 83 | + ASSERT_OK(TryReopen(options)); |
| 84 | + } |
| 85 | + |
| 86 | + void Destroy(Options* options) { |
| 87 | + delete db_; |
| 88 | + db_ = nullptr; |
| 89 | + ASSERT_OK(DestroyDB(dbname_, *options)); |
| 90 | + } |
| 91 | + |
| 92 | + Status PureReopen(Options* options, DB** db) { |
| 93 | + return DB::Open(*options, dbname_, db); |
| 94 | + } |
| 95 | + |
| 96 | + Status TryReopen(Options* options = nullptr) { |
| 97 | + delete db_; |
| 98 | + db_ = nullptr; |
| 99 | + Options opts; |
| 100 | + if (options != nullptr) { |
| 101 | + opts = *options; |
| 102 | + } else { |
| 103 | + opts = CurrentOptions(); |
| 104 | + opts.create_if_missing = true; |
| 105 | + } |
| 106 | + last_options_ = opts; |
| 107 | + |
| 108 | + return DB::Open(opts, dbname_, &db_); |
| 109 | + } |
| 110 | + |
| 111 | + Status Put(const Slice& k, const Slice& v) { |
| 112 | + return db_->Put(WriteOptions(), k, v); |
| 113 | + } |
| 114 | + |
| 115 | + Status Delete(const std::string& k) { |
| 116 | + return db_->Delete(WriteOptions(), k); |
| 117 | + } |
| 118 | + |
| 119 | + std::string Get(const std::string& k, const Snapshot* snapshot = nullptr) { |
| 120 | + ReadOptions options; |
| 121 | + options.snapshot = snapshot; |
| 122 | + std::string result; |
| 123 | + Status s = db_->Get(options, k, &result); |
| 124 | + if (s.IsNotFound()) { |
| 125 | + result = "NOT_FOUND"; |
| 126 | + } else if (!s.ok()) { |
| 127 | + result = s.ToString(); |
| 128 | + } |
| 129 | + return result; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + int NumTableFilesAtLevel(int level) { |
| 134 | + std::string property; |
| 135 | + ASSERT_TRUE( |
| 136 | + db_->GetProperty("rocksdb.num-files-at-level" + NumberToString(level), |
| 137 | + &property)); |
| 138 | + return atoi(property.c_str()); |
| 139 | + } |
| 140 | + |
| 141 | + // Return spread of files per level |
| 142 | + std::string FilesPerLevel() { |
| 143 | + std::string result; |
| 144 | + int last_non_zero_offset = 0; |
| 145 | + for (int level = 0; level < db_->NumberLevels(); level++) { |
| 146 | + int f = NumTableFilesAtLevel(level); |
| 147 | + char buf[100]; |
| 148 | + snprintf(buf, sizeof(buf), "%s%d", (level ? "," : ""), f); |
| 149 | + result += buf; |
| 150 | + if (f > 0) { |
| 151 | + last_non_zero_offset = result.size(); |
| 152 | + } |
| 153 | + } |
| 154 | + result.resize(last_non_zero_offset); |
| 155 | + return result; |
| 156 | + } |
| 157 | + |
| 158 | + std::string IterStatus(Iterator* iter) { |
| 159 | + std::string result; |
| 160 | + if (iter->Valid()) { |
| 161 | + result = iter->key().ToString() + "->" + iter->value().ToString(); |
| 162 | + } else { |
| 163 | + result = "(invalid)"; |
| 164 | + } |
| 165 | + return result; |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +TEST(PlainTableDBTest, Empty) { |
| 170 | + ASSERT_TRUE(db_ != nullptr); |
| 171 | + ASSERT_EQ("NOT_FOUND", Get("0000000000000foo")); |
| 172 | +} |
| 173 | + |
| 174 | +TEST(PlainTableDBTest, ReadWrite) { |
| 175 | + ASSERT_OK(Put("1000000000000foo", "v1")); |
| 176 | + ASSERT_EQ("v1", Get("1000000000000foo")); |
| 177 | + ASSERT_OK(Put("0000000000000bar", "v2")); |
| 178 | + ASSERT_OK(Put("1000000000000foo", "v3")); |
| 179 | + ASSERT_EQ("v3", Get("1000000000000foo")); |
| 180 | + ASSERT_EQ("v2", Get("0000000000000bar")); |
| 181 | +} |
| 182 | + |
| 183 | +TEST(PlainTableDBTest, Flush) { |
| 184 | + ASSERT_OK(Put("1000000000000foo", "v1")); |
| 185 | + ASSERT_OK(Put("0000000000000bar", "v2")); |
| 186 | + ASSERT_OK(Put("1000000000000foo", "v3")); |
| 187 | + dbfull()->TEST_FlushMemTable(); |
| 188 | + ASSERT_EQ("v3", Get("1000000000000foo")); |
| 189 | + ASSERT_EQ("v2", Get("0000000000000bar")); |
| 190 | +} |
| 191 | + |
| 192 | +TEST(PlainTableDBTest, Iterator) { |
| 193 | + ASSERT_OK(Put("1000000000foo002", "v_2")); |
| 194 | + ASSERT_OK(Put("0000000000000bar", "random")); |
| 195 | + ASSERT_OK(Put("1000000000foo001", "v1")); |
| 196 | + ASSERT_OK(Put("3000000000000bar", "bar_v")); |
| 197 | + ASSERT_OK(Put("1000000000foo003", "v__3")); |
| 198 | + ASSERT_OK(Put("1000000000foo004", "v__4")); |
| 199 | + ASSERT_OK(Put("1000000000foo005", "v__5")); |
| 200 | + ASSERT_OK(Put("1000000000foo007", "v__7")); |
| 201 | + ASSERT_OK(Put("1000000000foo008", "v__8")); |
| 202 | + dbfull()->TEST_FlushMemTable(); |
| 203 | + ASSERT_EQ("v1", Get("1000000000foo001")); |
| 204 | + ASSERT_EQ("v__3", Get("1000000000foo003")); |
| 205 | + ReadOptions ro; |
| 206 | + Iterator* iter = dbfull()->NewIterator(ro); |
| 207 | + iter->Seek("1000000000foo001"); |
| 208 | + ASSERT_TRUE(iter->Valid()); |
| 209 | + ASSERT_EQ("1000000000foo001", iter->key().ToString()); |
| 210 | + ASSERT_EQ("v1", iter->value().ToString()); |
| 211 | + |
| 212 | + iter->Next(); |
| 213 | + ASSERT_TRUE(iter->Valid()); |
| 214 | + ASSERT_EQ("1000000000foo002", iter->key().ToString()); |
| 215 | + ASSERT_EQ("v_2", iter->value().ToString()); |
| 216 | + |
| 217 | + iter->Next(); |
| 218 | + ASSERT_TRUE(iter->Valid()); |
| 219 | + ASSERT_EQ("1000000000foo003", iter->key().ToString()); |
| 220 | + ASSERT_EQ("v__3", iter->value().ToString()); |
| 221 | + |
| 222 | + iter->Next(); |
| 223 | + ASSERT_TRUE(iter->Valid()); |
| 224 | + ASSERT_EQ("1000000000foo004", iter->key().ToString()); |
| 225 | + ASSERT_EQ("v__4", iter->value().ToString()); |
| 226 | + |
| 227 | + iter->Seek("3000000000000bar"); |
| 228 | + ASSERT_TRUE(iter->Valid()); |
| 229 | + ASSERT_EQ("3000000000000bar", iter->key().ToString()); |
| 230 | + ASSERT_EQ("bar_v", iter->value().ToString()); |
| 231 | + |
| 232 | + iter->Seek("1000000000foo000"); |
| 233 | + ASSERT_TRUE(iter->Valid()); |
| 234 | + ASSERT_EQ("1000000000foo001", iter->key().ToString()); |
| 235 | + ASSERT_EQ("v1", iter->value().ToString()); |
| 236 | + |
| 237 | + iter->Seek("1000000000foo005"); |
| 238 | + ASSERT_TRUE(iter->Valid()); |
| 239 | + ASSERT_EQ("1000000000foo005", iter->key().ToString()); |
| 240 | + ASSERT_EQ("v__5", iter->value().ToString()); |
| 241 | + |
| 242 | + iter->Seek("1000000000foo006"); |
| 243 | + ASSERT_TRUE(iter->Valid()); |
| 244 | + ASSERT_EQ("1000000000foo007", iter->key().ToString()); |
| 245 | + ASSERT_EQ("v__7", iter->value().ToString()); |
| 246 | + |
| 247 | + iter->Seek("1000000000foo008"); |
| 248 | + ASSERT_TRUE(iter->Valid()); |
| 249 | + ASSERT_EQ("1000000000foo008", iter->key().ToString()); |
| 250 | + ASSERT_EQ("v__8", iter->value().ToString()); |
| 251 | + |
| 252 | + iter->Seek("1000000000foo009"); |
| 253 | + ASSERT_TRUE(iter->Valid()); |
| 254 | + ASSERT_EQ("3000000000000bar", iter->key().ToString()); |
| 255 | + |
| 256 | + |
| 257 | + delete iter; |
| 258 | +} |
| 259 | + |
| 260 | +TEST(PlainTableDBTest, Flush2) { |
| 261 | + ASSERT_OK(Put("0000000000000bar", "b")); |
| 262 | + ASSERT_OK(Put("1000000000000foo", "v1")); |
| 263 | + dbfull()->TEST_FlushMemTable(); |
| 264 | + |
| 265 | + ASSERT_OK(Put("1000000000000foo", "v2")); |
| 266 | + dbfull()->TEST_FlushMemTable(); |
| 267 | + ASSERT_EQ("v2", Get("1000000000000foo")); |
| 268 | + |
| 269 | + ASSERT_OK(Put("0000000000000eee", "v3")); |
| 270 | + dbfull()->TEST_FlushMemTable(); |
| 271 | + ASSERT_EQ("v3", Get("0000000000000eee")); |
| 272 | + |
| 273 | + ASSERT_OK(Delete("0000000000000bar")); |
| 274 | + dbfull()->TEST_FlushMemTable(); |
| 275 | + ASSERT_EQ("NOT_FOUND", Get("0000000000000bar")); |
| 276 | + |
| 277 | + ASSERT_OK(Put("0000000000000eee", "v5")); |
| 278 | + dbfull()->TEST_FlushMemTable(); |
| 279 | + ASSERT_EQ("v5", Get("0000000000000eee")); |
| 280 | +} |
| 281 | + |
| 282 | +static std::string Key(int i) { |
| 283 | + char buf[100]; |
| 284 | + snprintf(buf, sizeof(buf), "key_______%06d", i); |
| 285 | + return std::string(buf); |
| 286 | +} |
| 287 | + |
| 288 | +static std::string RandomString(Random* rnd, int len) { |
| 289 | + std::string r; |
| 290 | + test::RandomString(rnd, len, &r); |
| 291 | + return r; |
| 292 | +} |
| 293 | + |
| 294 | +TEST(PlainTableDBTest, CompactionTrigger) { |
| 295 | + Options options = CurrentOptions(); |
| 296 | + options.write_buffer_size = 100 << 10; //100KB |
| 297 | + options.num_levels = 3; |
| 298 | + options.max_mem_compaction_level = 0; |
| 299 | + options.level0_file_num_compaction_trigger = 3; |
| 300 | + Reopen(&options); |
| 301 | + |
| 302 | + Random rnd(301); |
| 303 | + |
| 304 | + for (int num = 0; num < options.level0_file_num_compaction_trigger - 1; |
| 305 | + num++) { |
| 306 | + std::vector<std::string> values; |
| 307 | + // Write 120KB (12 values, each 10K) |
| 308 | + for (int i = 0; i < 12; i++) { |
| 309 | + values.push_back(RandomString(&rnd, 10000)); |
| 310 | + ASSERT_OK(Put(Key(i), values[i])); |
| 311 | + } |
| 312 | + dbfull()->TEST_WaitForFlushMemTable(); |
| 313 | + ASSERT_EQ(NumTableFilesAtLevel(0), num + 1); |
| 314 | + } |
| 315 | + |
| 316 | + //generate one more file in level-0, and should trigger level-0 compaction |
| 317 | + std::vector<std::string> values; |
| 318 | + for (int i = 0; i < 12; i++) { |
| 319 | + values.push_back(RandomString(&rnd, 10000)); |
| 320 | + ASSERT_OK(Put(Key(i), values[i])); |
| 321 | + } |
| 322 | + dbfull()->TEST_WaitForCompact(); |
| 323 | + |
| 324 | + ASSERT_EQ(NumTableFilesAtLevel(0), 0); |
| 325 | + ASSERT_EQ(NumTableFilesAtLevel(1), 1); |
| 326 | +} |
| 327 | + |
| 328 | +} // namespace rocksdb |
| 329 | + |
| 330 | +int main(int argc, char** argv) { |
| 331 | + return rocksdb::test::RunAllTests(); |
| 332 | +} |
0 commit comments