作者: 谢瑞阳 10225101483 徐翔宇 10225101535
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.

179 lines
5.3 KiB

  1. // Copyright (c) 2011 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. #include "db/version_set.h"
  5. #include "util/logging.h"
  6. #include "util/testharness.h"
  7. #include "util/testutil.h"
  8. namespace leveldb {
  9. class FindFileTest {
  10. public:
  11. std::vector<FileMetaData*> files_;
  12. bool disjoint_sorted_files_;
  13. FindFileTest() : disjoint_sorted_files_(true) { }
  14. ~FindFileTest() {
  15. for (int i = 0; i < files_.size(); i++) {
  16. delete files_[i];
  17. }
  18. }
  19. void Add(const char* smallest, const char* largest,
  20. SequenceNumber smallest_seq = 100,
  21. SequenceNumber largest_seq = 100) {
  22. FileMetaData* f = new FileMetaData;
  23. f->number = files_.size() + 1;
  24. f->smallest = InternalKey(smallest, smallest_seq, kTypeValue);
  25. f->largest = InternalKey(largest, largest_seq, kTypeValue);
  26. files_.push_back(f);
  27. }
  28. int Find(const char* key) {
  29. InternalKey target(key, 100, kTypeValue);
  30. InternalKeyComparator cmp(BytewiseComparator());
  31. return FindFile(cmp, files_, target.Encode());
  32. }
  33. bool Overlaps(const char* smallest, const char* largest) {
  34. InternalKeyComparator cmp(BytewiseComparator());
  35. Slice s(smallest != nullptr ? smallest : "");
  36. Slice l(largest != nullptr ? largest : "");
  37. return SomeFileOverlapsRange(cmp, disjoint_sorted_files_, files_,
  38. (smallest != nullptr ? &s : nullptr),
  39. (largest != nullptr ? &l : nullptr));
  40. }
  41. };
  42. TEST(FindFileTest, Empty) {
  43. ASSERT_EQ(0, Find("foo"));
  44. ASSERT_TRUE(! Overlaps("a", "z"));
  45. ASSERT_TRUE(! Overlaps(nullptr, "z"));
  46. ASSERT_TRUE(! Overlaps("a", nullptr));
  47. ASSERT_TRUE(! Overlaps(nullptr, nullptr));
  48. }
  49. TEST(FindFileTest, Single) {
  50. Add("p", "q");
  51. ASSERT_EQ(0, Find("a"));
  52. ASSERT_EQ(0, Find("p"));
  53. ASSERT_EQ(0, Find("p1"));
  54. ASSERT_EQ(0, Find("q"));
  55. ASSERT_EQ(1, Find("q1"));
  56. ASSERT_EQ(1, Find("z"));
  57. ASSERT_TRUE(! Overlaps("a", "b"));
  58. ASSERT_TRUE(! Overlaps("z1", "z2"));
  59. ASSERT_TRUE(Overlaps("a", "p"));
  60. ASSERT_TRUE(Overlaps("a", "q"));
  61. ASSERT_TRUE(Overlaps("a", "z"));
  62. ASSERT_TRUE(Overlaps("p", "p1"));
  63. ASSERT_TRUE(Overlaps("p", "q"));
  64. ASSERT_TRUE(Overlaps("p", "z"));
  65. ASSERT_TRUE(Overlaps("p1", "p2"));
  66. ASSERT_TRUE(Overlaps("p1", "z"));
  67. ASSERT_TRUE(Overlaps("q", "q"));
  68. ASSERT_TRUE(Overlaps("q", "q1"));
  69. ASSERT_TRUE(! Overlaps(nullptr, "j"));
  70. ASSERT_TRUE(! Overlaps("r", nullptr));
  71. ASSERT_TRUE(Overlaps(nullptr, "p"));
  72. ASSERT_TRUE(Overlaps(nullptr, "p1"));
  73. ASSERT_TRUE(Overlaps("q", nullptr));
  74. ASSERT_TRUE(Overlaps(nullptr, nullptr));
  75. }
  76. TEST(FindFileTest, Multiple) {
  77. Add("150", "200");
  78. Add("200", "250");
  79. Add("300", "350");
  80. Add("400", "450");
  81. ASSERT_EQ(0, Find("100"));
  82. ASSERT_EQ(0, Find("150"));
  83. ASSERT_EQ(0, Find("151"));
  84. ASSERT_EQ(0, Find("199"));
  85. ASSERT_EQ(0, Find("200"));
  86. ASSERT_EQ(1, Find("201"));
  87. ASSERT_EQ(1, Find("249"));
  88. ASSERT_EQ(1, Find("250"));
  89. ASSERT_EQ(2, Find("251"));
  90. ASSERT_EQ(2, Find("299"));
  91. ASSERT_EQ(2, Find("300"));
  92. ASSERT_EQ(2, Find("349"));
  93. ASSERT_EQ(2, Find("350"));
  94. ASSERT_EQ(3, Find("351"));
  95. ASSERT_EQ(3, Find("400"));
  96. ASSERT_EQ(3, Find("450"));
  97. ASSERT_EQ(4, Find("451"));
  98. ASSERT_TRUE(! Overlaps("100", "149"));
  99. ASSERT_TRUE(! Overlaps("251", "299"));
  100. ASSERT_TRUE(! Overlaps("451", "500"));
  101. ASSERT_TRUE(! Overlaps("351", "399"));
  102. ASSERT_TRUE(Overlaps("100", "150"));
  103. ASSERT_TRUE(Overlaps("100", "200"));
  104. ASSERT_TRUE(Overlaps("100", "300"));
  105. ASSERT_TRUE(Overlaps("100", "400"));
  106. ASSERT_TRUE(Overlaps("100", "500"));
  107. ASSERT_TRUE(Overlaps("375", "400"));
  108. ASSERT_TRUE(Overlaps("450", "450"));
  109. ASSERT_TRUE(Overlaps("450", "500"));
  110. }
  111. TEST(FindFileTest, MultipleNullBoundaries) {
  112. Add("150", "200");
  113. Add("200", "250");
  114. Add("300", "350");
  115. Add("400", "450");
  116. ASSERT_TRUE(! Overlaps(nullptr, "149"));
  117. ASSERT_TRUE(! Overlaps("451", nullptr));
  118. ASSERT_TRUE(Overlaps(nullptr, nullptr));
  119. ASSERT_TRUE(Overlaps(nullptr, "150"));
  120. ASSERT_TRUE(Overlaps(nullptr, "199"));
  121. ASSERT_TRUE(Overlaps(nullptr, "200"));
  122. ASSERT_TRUE(Overlaps(nullptr, "201"));
  123. ASSERT_TRUE(Overlaps(nullptr, "400"));
  124. ASSERT_TRUE(Overlaps(nullptr, "800"));
  125. ASSERT_TRUE(Overlaps("100", nullptr));
  126. ASSERT_TRUE(Overlaps("200", nullptr));
  127. ASSERT_TRUE(Overlaps("449", nullptr));
  128. ASSERT_TRUE(Overlaps("450", nullptr));
  129. }
  130. TEST(FindFileTest, OverlapSequenceChecks) {
  131. Add("200", "200", 5000, 3000);
  132. ASSERT_TRUE(! Overlaps("199", "199"));
  133. ASSERT_TRUE(! Overlaps("201", "300"));
  134. ASSERT_TRUE(Overlaps("200", "200"));
  135. ASSERT_TRUE(Overlaps("190", "200"));
  136. ASSERT_TRUE(Overlaps("200", "210"));
  137. }
  138. TEST(FindFileTest, OverlappingFiles) {
  139. Add("150", "600");
  140. Add("400", "500");
  141. disjoint_sorted_files_ = false;
  142. ASSERT_TRUE(! Overlaps("100", "149"));
  143. ASSERT_TRUE(! Overlaps("601", "700"));
  144. ASSERT_TRUE(Overlaps("100", "150"));
  145. ASSERT_TRUE(Overlaps("100", "200"));
  146. ASSERT_TRUE(Overlaps("100", "300"));
  147. ASSERT_TRUE(Overlaps("100", "400"));
  148. ASSERT_TRUE(Overlaps("100", "500"));
  149. ASSERT_TRUE(Overlaps("375", "400"));
  150. ASSERT_TRUE(Overlaps("450", "450"));
  151. ASSERT_TRUE(Overlaps("450", "500"));
  152. ASSERT_TRUE(Overlaps("450", "700"));
  153. ASSERT_TRUE(Overlaps("600", "700"));
  154. }
  155. } // namespace leveldb
  156. int main(int argc, char** argv) {
  157. return leveldb::test::RunAllTests();
  158. }