dbformat_test.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #include "db/dbformat.h"
  10. #include "logging/logging.h"
  11. #include "test_util/testharness.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. static std::string IKey(const std::string& user_key,
  14. uint64_t seq,
  15. ValueType vt) {
  16. std::string encoded;
  17. AppendInternalKey(&encoded, ParsedInternalKey(user_key, seq, vt));
  18. return encoded;
  19. }
  20. static std::string Shorten(const std::string& s, const std::string& l) {
  21. std::string result = s;
  22. InternalKeyComparator(BytewiseComparator()).FindShortestSeparator(&result, l);
  23. return result;
  24. }
  25. static std::string ShortSuccessor(const std::string& s) {
  26. std::string result = s;
  27. InternalKeyComparator(BytewiseComparator()).FindShortSuccessor(&result);
  28. return result;
  29. }
  30. static void TestKey(const std::string& key,
  31. uint64_t seq,
  32. ValueType vt) {
  33. std::string encoded = IKey(key, seq, vt);
  34. Slice in(encoded);
  35. ParsedInternalKey decoded("", 0, kTypeValue);
  36. ASSERT_TRUE(ParseInternalKey(in, &decoded));
  37. ASSERT_EQ(key, decoded.user_key.ToString());
  38. ASSERT_EQ(seq, decoded.sequence);
  39. ASSERT_EQ(vt, decoded.type);
  40. ASSERT_TRUE(!ParseInternalKey(Slice("bar"), &decoded));
  41. }
  42. class FormatTest : public testing::Test {};
  43. TEST_F(FormatTest, InternalKey_EncodeDecode) {
  44. const char* keys[] = { "", "k", "hello", "longggggggggggggggggggggg" };
  45. const uint64_t seq[] = {
  46. 1, 2, 3,
  47. (1ull << 8) - 1, 1ull << 8, (1ull << 8) + 1,
  48. (1ull << 16) - 1, 1ull << 16, (1ull << 16) + 1,
  49. (1ull << 32) - 1, 1ull << 32, (1ull << 32) + 1
  50. };
  51. for (unsigned int k = 0; k < sizeof(keys) / sizeof(keys[0]); k++) {
  52. for (unsigned int s = 0; s < sizeof(seq) / sizeof(seq[0]); s++) {
  53. TestKey(keys[k], seq[s], kTypeValue);
  54. TestKey("hello", 1, kTypeDeletion);
  55. }
  56. }
  57. }
  58. TEST_F(FormatTest, InternalKeyShortSeparator) {
  59. // When user keys are same
  60. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  61. Shorten(IKey("foo", 100, kTypeValue),
  62. IKey("foo", 99, kTypeValue)));
  63. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  64. Shorten(IKey("foo", 100, kTypeValue),
  65. IKey("foo", 101, kTypeValue)));
  66. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  67. Shorten(IKey("foo", 100, kTypeValue),
  68. IKey("foo", 100, kTypeValue)));
  69. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  70. Shorten(IKey("foo", 100, kTypeValue),
  71. IKey("foo", 100, kTypeDeletion)));
  72. // When user keys are misordered
  73. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  74. Shorten(IKey("foo", 100, kTypeValue),
  75. IKey("bar", 99, kTypeValue)));
  76. // When user keys are different, but correctly ordered
  77. ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
  78. Shorten(IKey("foo", 100, kTypeValue),
  79. IKey("hello", 200, kTypeValue)));
  80. ASSERT_EQ(IKey("ABC2", kMaxSequenceNumber, kValueTypeForSeek),
  81. Shorten(IKey("ABC1AAAAA", 100, kTypeValue),
  82. IKey("ABC2ABB", 200, kTypeValue)));
  83. ASSERT_EQ(IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
  84. Shorten(IKey("AAA1AAA", 100, kTypeValue),
  85. IKey("AAA2AA", 200, kTypeValue)));
  86. ASSERT_EQ(
  87. IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
  88. Shorten(IKey("AAA1AAA", 100, kTypeValue), IKey("AAA4", 200, kTypeValue)));
  89. ASSERT_EQ(
  90. IKey("AAA1B", kMaxSequenceNumber, kValueTypeForSeek),
  91. Shorten(IKey("AAA1AAA", 100, kTypeValue), IKey("AAA2", 200, kTypeValue)));
  92. ASSERT_EQ(IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
  93. Shorten(IKey("AAA1AAA", 100, kTypeValue),
  94. IKey("AAA2A", 200, kTypeValue)));
  95. ASSERT_EQ(
  96. IKey("AAA1", 100, kTypeValue),
  97. Shorten(IKey("AAA1", 100, kTypeValue), IKey("AAA2", 200, kTypeValue)));
  98. // When start user key is prefix of limit user key
  99. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  100. Shorten(IKey("foo", 100, kTypeValue),
  101. IKey("foobar", 200, kTypeValue)));
  102. // When limit user key is prefix of start user key
  103. ASSERT_EQ(IKey("foobar", 100, kTypeValue),
  104. Shorten(IKey("foobar", 100, kTypeValue),
  105. IKey("foo", 200, kTypeValue)));
  106. }
  107. TEST_F(FormatTest, InternalKeyShortestSuccessor) {
  108. ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
  109. ShortSuccessor(IKey("foo", 100, kTypeValue)));
  110. ASSERT_EQ(IKey("\xff\xff", 100, kTypeValue),
  111. ShortSuccessor(IKey("\xff\xff", 100, kTypeValue)));
  112. }
  113. TEST_F(FormatTest, IterKeyOperation) {
  114. IterKey k;
  115. const char p[] = "abcdefghijklmnopqrstuvwxyz";
  116. const char q[] = "0123456789";
  117. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  118. std::string(""));
  119. k.TrimAppend(0, p, 3);
  120. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  121. std::string("abc"));
  122. k.TrimAppend(1, p, 3);
  123. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  124. std::string("aabc"));
  125. k.TrimAppend(0, p, 26);
  126. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  127. std::string("abcdefghijklmnopqrstuvwxyz"));
  128. k.TrimAppend(26, q, 10);
  129. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  130. std::string("abcdefghijklmnopqrstuvwxyz0123456789"));
  131. k.TrimAppend(36, q, 1);
  132. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  133. std::string("abcdefghijklmnopqrstuvwxyz01234567890"));
  134. k.TrimAppend(26, q, 1);
  135. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  136. std::string("abcdefghijklmnopqrstuvwxyz0"));
  137. // Size going up, memory allocation is triggered
  138. k.TrimAppend(27, p, 26);
  139. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  140. std::string("abcdefghijklmnopqrstuvwxyz0"
  141. "abcdefghijklmnopqrstuvwxyz"));
  142. }
  143. TEST_F(FormatTest, UpdateInternalKey) {
  144. std::string user_key("abcdefghijklmnopqrstuvwxyz");
  145. uint64_t new_seq = 0x123456;
  146. ValueType new_val_type = kTypeDeletion;
  147. std::string ikey;
  148. AppendInternalKey(&ikey, ParsedInternalKey(user_key, 100U, kTypeValue));
  149. size_t ikey_size = ikey.size();
  150. UpdateInternalKey(&ikey, new_seq, new_val_type);
  151. ASSERT_EQ(ikey_size, ikey.size());
  152. Slice in(ikey);
  153. ParsedInternalKey decoded;
  154. ASSERT_TRUE(ParseInternalKey(in, &decoded));
  155. ASSERT_EQ(user_key, decoded.user_key.ToString());
  156. ASSERT_EQ(new_seq, decoded.sequence);
  157. ASSERT_EQ(new_val_type, decoded.type);
  158. }
  159. TEST_F(FormatTest, RangeTombstoneSerializeEndKey) {
  160. RangeTombstone t("a", "b", 2);
  161. InternalKey k("b", 3, kTypeValue);
  162. const InternalKeyComparator cmp(BytewiseComparator());
  163. ASSERT_LT(cmp.Compare(t.SerializeEndKey(), k), 0);
  164. }
  165. } // namespace ROCKSDB_NAMESPACE
  166. int main(int argc, char** argv) {
  167. ::testing::InitGoogleTest(&argc, argv);
  168. return RUN_ALL_TESTS();
  169. }