LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
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.

85 lines
2.5 KiB

1 month ago
  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 "gtest/gtest.h"
  9. #include "leveldb/db.h"
  10. #include "leveldb/write_batch.h"
  11. #include "util/testutil.h"
  12. namespace {
  13. const int kNumKeys = 1100000;
  14. std::string Key1(int i) {
  15. char buf[100];
  16. std::snprintf(buf, sizeof(buf), "my_key_%d", i);
  17. return buf;
  18. }
  19. std::string Key2(int i) { return Key1(i) + "_xxx"; }
  20. TEST(Issue178, Test) {
  21. // Get rid of any state from an old run.
  22. std::string dbpath = testing::TempDir() + "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_LEVELDB_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_LEVELDB_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_LEVELDB_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_LEVELDB_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