version_set_test.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. void AddBoundaryInputs(const InternalKeyComparator& icmp,
  156. const std::vector<FileMetaData*>& level_files,
  157. std::vector<FileMetaData*>* compaction_files);
  158. class AddBoundaryInputsTest {
  159. public:
  160. std::vector<FileMetaData*> level_files_;
  161. std::vector<FileMetaData*> compaction_files_;
  162. std::vector<FileMetaData*> all_files_;
  163. InternalKeyComparator icmp_;
  164. AddBoundaryInputsTest() : icmp_(BytewiseComparator()){};
  165. ~AddBoundaryInputsTest() {
  166. for (size_t i = 0; i < all_files_.size(); ++i) {
  167. delete all_files_[i];
  168. }
  169. all_files_.clear();
  170. };
  171. FileMetaData* CreateFileMetaData(uint64_t number, InternalKey smallest,
  172. InternalKey largest) {
  173. FileMetaData* f = new FileMetaData();
  174. f->number = number;
  175. f->smallest = smallest;
  176. f->largest = largest;
  177. all_files_.push_back(f);
  178. return f;
  179. }
  180. };
  181. TEST(AddBoundaryInputsTest, TestEmptyFileSets) {
  182. AddBoundaryInputs(icmp_, level_files_, &compaction_files_);
  183. ASSERT_TRUE(compaction_files_.empty());
  184. ASSERT_TRUE(level_files_.empty());
  185. }
  186. TEST(AddBoundaryInputsTest, TestEmptyLevelFiles) {
  187. FileMetaData* f1 =
  188. CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
  189. InternalKey(InternalKey("100", 1, kTypeValue)));
  190. compaction_files_.push_back(f1);
  191. AddBoundaryInputs(icmp_, level_files_, &compaction_files_);
  192. ASSERT_EQ(1, compaction_files_.size());
  193. ASSERT_EQ(f1, compaction_files_[0]);
  194. ASSERT_TRUE(level_files_.empty());
  195. }
  196. TEST(AddBoundaryInputsTest, TestEmptyCompactionFiles) {
  197. FileMetaData* f1 =
  198. CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
  199. InternalKey(InternalKey("100", 1, kTypeValue)));
  200. level_files_.push_back(f1);
  201. AddBoundaryInputs(icmp_, level_files_, &compaction_files_);
  202. ASSERT_TRUE(compaction_files_.empty());
  203. ASSERT_EQ(1, level_files_.size());
  204. ASSERT_EQ(f1, level_files_[0]);
  205. }
  206. TEST(AddBoundaryInputsTest, TestNoBoundaryFiles) {
  207. FileMetaData* f1 =
  208. CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
  209. InternalKey(InternalKey("100", 1, kTypeValue)));
  210. FileMetaData* f2 =
  211. CreateFileMetaData(1, InternalKey("200", 2, kTypeValue),
  212. InternalKey(InternalKey("200", 1, kTypeValue)));
  213. FileMetaData* f3 =
  214. CreateFileMetaData(1, InternalKey("300", 2, kTypeValue),
  215. InternalKey(InternalKey("300", 1, kTypeValue)));
  216. level_files_.push_back(f3);
  217. level_files_.push_back(f2);
  218. level_files_.push_back(f1);
  219. compaction_files_.push_back(f2);
  220. compaction_files_.push_back(f3);
  221. AddBoundaryInputs(icmp_, level_files_, &compaction_files_);
  222. ASSERT_EQ(2, compaction_files_.size());
  223. }
  224. TEST(AddBoundaryInputsTest, TestOneBoundaryFiles) {
  225. FileMetaData* f1 =
  226. CreateFileMetaData(1, InternalKey("100", 3, kTypeValue),
  227. InternalKey(InternalKey("100", 2, kTypeValue)));
  228. FileMetaData* f2 =
  229. CreateFileMetaData(1, InternalKey("100", 1, kTypeValue),
  230. InternalKey(InternalKey("200", 3, kTypeValue)));
  231. FileMetaData* f3 =
  232. CreateFileMetaData(1, InternalKey("300", 2, kTypeValue),
  233. InternalKey(InternalKey("300", 1, kTypeValue)));
  234. level_files_.push_back(f3);
  235. level_files_.push_back(f2);
  236. level_files_.push_back(f1);
  237. compaction_files_.push_back(f1);
  238. AddBoundaryInputs(icmp_, level_files_, &compaction_files_);
  239. ASSERT_EQ(2, compaction_files_.size());
  240. ASSERT_EQ(f1, compaction_files_[0]);
  241. ASSERT_EQ(f2, compaction_files_[1]);
  242. }
  243. TEST(AddBoundaryInputsTest, TestTwoBoundaryFiles) {
  244. FileMetaData* f1 =
  245. CreateFileMetaData(1, InternalKey("100", 6, kTypeValue),
  246. InternalKey(InternalKey("100", 5, kTypeValue)));
  247. FileMetaData* f2 =
  248. CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
  249. InternalKey(InternalKey("300", 1, kTypeValue)));
  250. FileMetaData* f3 =
  251. CreateFileMetaData(1, InternalKey("100", 4, kTypeValue),
  252. InternalKey(InternalKey("100", 3, kTypeValue)));
  253. level_files_.push_back(f2);
  254. level_files_.push_back(f3);
  255. level_files_.push_back(f1);
  256. compaction_files_.push_back(f1);
  257. AddBoundaryInputs(icmp_, level_files_, &compaction_files_);
  258. ASSERT_EQ(3, compaction_files_.size());
  259. ASSERT_EQ(f1, compaction_files_[0]);
  260. ASSERT_EQ(f3, compaction_files_[1]);
  261. ASSERT_EQ(f2, compaction_files_[2]);
  262. }
  263. TEST(AddBoundaryInputsTest, TestDisjoinFilePointers) {
  264. FileMetaData* f1 =
  265. CreateFileMetaData(1, InternalKey("100", 6, kTypeValue),
  266. InternalKey(InternalKey("100", 5, kTypeValue)));
  267. FileMetaData* f2 =
  268. CreateFileMetaData(1, InternalKey("100", 6, kTypeValue),
  269. InternalKey(InternalKey("100", 5, kTypeValue)));
  270. FileMetaData* f3 =
  271. CreateFileMetaData(1, InternalKey("100", 2, kTypeValue),
  272. InternalKey(InternalKey("300", 1, kTypeValue)));
  273. FileMetaData* f4 =
  274. CreateFileMetaData(1, InternalKey("100", 4, kTypeValue),
  275. InternalKey(InternalKey("100", 3, kTypeValue)));
  276. level_files_.push_back(f2);
  277. level_files_.push_back(f3);
  278. level_files_.push_back(f4);
  279. compaction_files_.push_back(f1);
  280. AddBoundaryInputs(icmp_, level_files_, &compaction_files_);
  281. ASSERT_EQ(3, compaction_files_.size());
  282. ASSERT_EQ(f1, compaction_files_[0]);
  283. ASSERT_EQ(f4, compaction_files_[1]);
  284. ASSERT_EQ(f3, compaction_files_[2]);
  285. }
  286. } // namespace leveldb
  287. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }