作者: 韩晨旭 10225101440 李畅 10225102463
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

179 строки
5.2 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 != NULL ? smallest : "");
  36. Slice l(largest != NULL ? largest : "");
  37. return SomeFileOverlapsRange(cmp, disjoint_sorted_files_, files_,
  38. (smallest != NULL ? &s : NULL),
  39. (largest != NULL ? &l : NULL));
  40. }
  41. };
  42. TEST(FindFileTest, Empty) {
  43. ASSERT_EQ(0, Find("foo"));
  44. ASSERT_TRUE(! Overlaps("a", "z"));
  45. ASSERT_TRUE(! Overlaps(NULL, "z"));
  46. ASSERT_TRUE(! Overlaps("a", NULL));
  47. ASSERT_TRUE(! Overlaps(NULL, NULL));
  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(NULL, "j"));
  70. ASSERT_TRUE(! Overlaps("r", NULL));
  71. ASSERT_TRUE(Overlaps(NULL, "p"));
  72. ASSERT_TRUE(Overlaps(NULL, "p1"));
  73. ASSERT_TRUE(Overlaps("q", NULL));
  74. ASSERT_TRUE(Overlaps(NULL, NULL));
  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(NULL, "149"));
  117. ASSERT_TRUE(! Overlaps("451", NULL));
  118. ASSERT_TRUE(Overlaps(NULL, NULL));
  119. ASSERT_TRUE(Overlaps(NULL, "150"));
  120. ASSERT_TRUE(Overlaps(NULL, "199"));
  121. ASSERT_TRUE(Overlaps(NULL, "200"));
  122. ASSERT_TRUE(Overlaps(NULL, "201"));
  123. ASSERT_TRUE(Overlaps(NULL, "400"));
  124. ASSERT_TRUE(Overlaps(NULL, "800"));
  125. ASSERT_TRUE(Overlaps("100", NULL));
  126. ASSERT_TRUE(Overlaps("200", NULL));
  127. ASSERT_TRUE(Overlaps("449", NULL));
  128. ASSERT_TRUE(Overlaps("450", NULL));
  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. }