小组成员: 曹可心-10223903406 朴祉燕-10224602413
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.

92 lines
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 <iostream>
  6. #include <sstream>
  7. #include <cstdlib>
  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) {
  19. return Key1(i) + "_xxx";
  20. }
  21. class Issue178 { };
  22. TEST(Issue178, Test) {
  23. // Get rid of any state from an old run.
  24. std::string dbpath = leveldb::test::TmpDir() + "/leveldb_cbug_test";
  25. DestroyDB(dbpath, leveldb::Options());
  26. // Open database. Disable compression since it affects the creation
  27. // of layers and the code below is trying to test against a very
  28. // specific scenario.
  29. leveldb::DB* db;
  30. leveldb::Options db_options;
  31. db_options.create_if_missing = true;
  32. db_options.compression = leveldb::kNoCompression;
  33. ASSERT_OK(leveldb::DB::Open(db_options, dbpath, &db));
  34. // create first key range
  35. leveldb::WriteBatch batch;
  36. for (size_t i = 0; i < kNumKeys; i++) {
  37. batch.Put(Key1(i), "value for range 1 key");
  38. }
  39. ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));
  40. // create second key range
  41. batch.Clear();
  42. for (size_t i = 0; i < kNumKeys; i++) {
  43. batch.Put(Key2(i), "value for range 2 key");
  44. }
  45. ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));
  46. // delete second key range
  47. batch.Clear();
  48. for (size_t i = 0; i < kNumKeys; i++) {
  49. batch.Delete(Key2(i));
  50. }
  51. ASSERT_OK(db->Write(leveldb::WriteOptions(), &batch));
  52. // compact database
  53. std::string start_key = Key1(0);
  54. std::string end_key = Key1(kNumKeys - 1);
  55. leveldb::Slice least(start_key.data(), start_key.size());
  56. leveldb::Slice greatest(end_key.data(), end_key.size());
  57. // commenting out the line below causes the example to work correctly
  58. db->CompactRange(&least, &greatest);
  59. // count the keys
  60. leveldb::Iterator* iter = db->NewIterator(leveldb::ReadOptions());
  61. size_t num_keys = 0;
  62. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  63. num_keys++;
  64. }
  65. delete iter;
  66. ASSERT_EQ(kNumKeys, num_keys) << "Bad number of keys";
  67. // close database
  68. delete db;
  69. DestroyDB(dbpath, leveldb::Options());
  70. }
  71. } // anonymous namespace
  72. int main(int argc, char** argv) {
  73. return leveldb::test::RunAllTests();
  74. }