作者: 韩晨旭 10225101440 李畅 10225102463
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

88 rader
2.5 KiB

  1. // Copyright (c) 2013 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. // Test for issue 178: a manual compaction causes deleted data to reappear.
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <sstream>
  8. #include "leveldb/db.h"
  9. #include "leveldb/write_batch.h"
  10. #include "util/testharness.h"
  11. namespace {
  12. const int kNumKeys = 1100000;
  13. std::string Key1(int i) {
  14. char buf[100];
  15. snprintf(buf, sizeof(buf), "my_key_%d", i);
  16. return buf;
  17. }
  18. std::string Key2(int i) { return Key1(i) + "_xxx"; }
  19. class Issue178 {};
  20. TEST(Issue178, Test) {
  21. // Get rid of any state from an old run.
  22. std::string dbpath = leveldb::test::TmpDir() + "/leveldb_cbug_test";
  23. DestroyDB(dbpath, leveldb::Options());
  24. // Open database. Disable compression since it affects the creation
  25. // of layers and the code below is trying to test against a very
  26. // specific scenario.
  27. leveldb::DB* db;
  28. leveldb::Options db_options;
  29. db_options.create_if_missing = true;
  30. db_options.compression = leveldb::kNoCompression;
  31. ASSERT_OK(leveldb::DB::Open(db_options, dbpath, &db));
  32. // create first key range
  33. leveldb::WriteBatch batch;
  34. for (size_t i = 0; i < kNumKeys; i++) {
  35. batch.Put(Key1(i), "value for range 1 key");
  36. }
  37. ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));
  38. // create second key range
  39. batch.Clear();
  40. for (size_t i = 0; i < kNumKeys; i++) {
  41. batch.Put(Key2(i), "value for range 2 key");
  42. }
  43. ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));
  44. // delete second key range
  45. batch.Clear();
  46. for (size_t i = 0; i < kNumKeys; i++) {
  47. batch.Delete(Key2(i));
  48. }
  49. ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));
  50. // compact database
  51. std::string start_key = Key1(0);
  52. std::string end_key = Key1(kNumKeys - 1);
  53. leveldb::Slice least(start_key.data(), start_key.size());
  54. leveldb::Slice greatest(end_key.data(), end_key.size());
  55. // commenting out the line below causes the example to work correctly
  56. db->CompactRange(&least, &greatest);
  57. // count the keys
  58. leveldb::Iterator* iter = db->NewIterator(leveldb::ReadOptions());
  59. size_t num_keys = 0;
  60. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  61. num_keys++;
  62. }
  63. delete iter;
  64. ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys";
  65. // close database
  66. delete db;
  67. DestroyDB(dbpath, leveldb::Options());
  68. }
  69. } // anonymous namespace
  70. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }