dbformat_test.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "table/block_based/index_builder.h"
  11. #include "test_util/testharness.h"
  12. #include "test_util/testutil.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. static std::string IKey(const std::string& user_key, 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 scratch;
  22. return ShortenedIndexBuilder::FindShortestInternalKeySeparator(
  23. *BytewiseComparator(), s, l, &scratch)
  24. .ToString();
  25. }
  26. static std::string ShortSuccessor(const std::string& s) {
  27. std::string scratch;
  28. return ShortenedIndexBuilder::FindShortInternalKeySuccessor(
  29. *BytewiseComparator(), s, &scratch)
  30. .ToString();
  31. }
  32. static void TestKey(const std::string& key, uint64_t seq, ValueType vt) {
  33. std::string encoded = IKey(key, seq, vt);
  34. Slice in(encoded);
  35. ParsedInternalKey decoded("", 0, kTypeValue);
  36. ASSERT_OK(ParseInternalKey(in, &decoded, true /* log_err_key */));
  37. ASSERT_EQ(key, decoded.user_key.ToString());
  38. ASSERT_EQ(seq, decoded.sequence);
  39. ASSERT_EQ(vt, decoded.type);
  40. ASSERT_NOK(ParseInternalKey(Slice("bar"), &decoded, true /* log_err_key */));
  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[] = {1,
  46. 2,
  47. 3,
  48. (1ull << 8) - 1,
  49. 1ull << 8,
  50. (1ull << 8) + 1,
  51. (1ull << 16) - 1,
  52. 1ull << 16,
  53. (1ull << 16) + 1,
  54. (1ull << 32) - 1,
  55. 1ull << 32,
  56. (1ull << 32) + 1};
  57. for (unsigned int k = 0; k < sizeof(keys) / sizeof(keys[0]); k++) {
  58. for (unsigned int s = 0; s < sizeof(seq) / sizeof(seq[0]); s++) {
  59. TestKey(keys[k], seq[s], kTypeValue);
  60. TestKey("hello", 1, kTypeDeletion);
  61. }
  62. }
  63. }
  64. TEST_F(FormatTest, InternalKeyShortSeparator) {
  65. // When user keys are same
  66. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  67. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 99, kTypeValue)));
  68. ASSERT_EQ(
  69. IKey("foo", 100, kTypeValue),
  70. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 101, kTypeValue)));
  71. ASSERT_EQ(
  72. IKey("foo", 100, kTypeValue),
  73. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 100, kTypeValue)));
  74. ASSERT_EQ(
  75. IKey("foo", 100, kTypeValue),
  76. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 100, kTypeDeletion)));
  77. // When user keys are misordered
  78. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  79. Shorten(IKey("foo", 100, kTypeValue), IKey("bar", 99, kTypeValue)));
  80. // When user keys are different, but correctly ordered
  81. ASSERT_EQ(
  82. IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
  83. Shorten(IKey("foo", 100, kTypeValue), IKey("hello", 200, kTypeValue)));
  84. ASSERT_EQ(IKey("ABC2", kMaxSequenceNumber, kValueTypeForSeek),
  85. Shorten(IKey("ABC1AAAAA", 100, kTypeValue),
  86. IKey("ABC2ABB", 200, kTypeValue)));
  87. ASSERT_EQ(IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
  88. Shorten(IKey("AAA1AAA", 100, kTypeValue),
  89. IKey("AAA2AA", 200, kTypeValue)));
  90. ASSERT_EQ(
  91. IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
  92. Shorten(IKey("AAA1AAA", 100, kTypeValue), IKey("AAA4", 200, kTypeValue)));
  93. ASSERT_EQ(
  94. IKey("AAA1B", kMaxSequenceNumber, kValueTypeForSeek),
  95. Shorten(IKey("AAA1AAA", 100, kTypeValue), IKey("AAA2", 200, kTypeValue)));
  96. ASSERT_EQ(IKey("AAA2", kMaxSequenceNumber, kValueTypeForSeek),
  97. Shorten(IKey("AAA1AAA", 100, kTypeValue),
  98. IKey("AAA2A", 200, kTypeValue)));
  99. ASSERT_EQ(
  100. IKey("AAA1", 100, kTypeValue),
  101. Shorten(IKey("AAA1", 100, kTypeValue), IKey("AAA2", 200, kTypeValue)));
  102. // When start user key is prefix of limit user key
  103. ASSERT_EQ(
  104. IKey("foo", 100, kTypeValue),
  105. Shorten(IKey("foo", 100, kTypeValue), IKey("foobar", 200, kTypeValue)));
  106. // When limit user key is prefix of start user key
  107. ASSERT_EQ(
  108. IKey("foobar", 100, kTypeValue),
  109. Shorten(IKey("foobar", 100, kTypeValue), IKey("foo", 200, kTypeValue)));
  110. }
  111. TEST_F(FormatTest, InternalKeyShortestSuccessor) {
  112. ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
  113. ShortSuccessor(IKey("foo", 100, kTypeValue)));
  114. ASSERT_EQ(IKey("\xff\xff", 100, kTypeValue),
  115. ShortSuccessor(IKey("\xff\xff", 100, kTypeValue)));
  116. }
  117. TEST_F(FormatTest, IterKeyOperation) {
  118. IterKey k;
  119. const char p[] = "abcdefghijklmnopqrstuvwxyz";
  120. const char q[] = "0123456789";
  121. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  122. std::string(""));
  123. k.TrimAppend(0, p, 3);
  124. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  125. std::string("abc"));
  126. k.TrimAppend(1, p, 3);
  127. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  128. std::string("aabc"));
  129. k.TrimAppend(0, p, 26);
  130. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  131. std::string("abcdefghijklmnopqrstuvwxyz"));
  132. k.TrimAppend(26, q, 10);
  133. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  134. std::string("abcdefghijklmnopqrstuvwxyz0123456789"));
  135. k.TrimAppend(36, q, 1);
  136. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  137. std::string("abcdefghijklmnopqrstuvwxyz01234567890"));
  138. k.TrimAppend(26, q, 1);
  139. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  140. std::string("abcdefghijklmnopqrstuvwxyz0"));
  141. // Size going up, memory allocation is triggered
  142. k.TrimAppend(27, p, 26);
  143. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  144. std::string("abcdefghijklmnopqrstuvwxyz0"
  145. "abcdefghijklmnopqrstuvwxyz"));
  146. }
  147. TEST_F(FormatTest, IterKeyWithTimestampOperation) {
  148. IterKey k;
  149. k.SetUserKey("");
  150. const char p[] = "abcdefghijklmnopqrstuvwxyz";
  151. const char q[] = "0123456789";
  152. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  153. std::string(""));
  154. size_t ts_sz = 8;
  155. std::string min_timestamp(ts_sz, static_cast<unsigned char>(0));
  156. k.TrimAppendWithTimestamp(0, p, 3, ts_sz);
  157. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  158. "abc" + min_timestamp);
  159. k.TrimAppendWithTimestamp(1, p, 3, ts_sz);
  160. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  161. "aabc" + min_timestamp);
  162. k.TrimAppendWithTimestamp(0, p, 26, ts_sz);
  163. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  164. "abcdefghijklmnopqrstuvwxyz" + min_timestamp);
  165. k.TrimAppendWithTimestamp(26, q, 10, ts_sz);
  166. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  167. "abcdefghijklmnopqrstuvwxyz0123456789" + min_timestamp);
  168. k.TrimAppendWithTimestamp(36, q, 1, ts_sz);
  169. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  170. "abcdefghijklmnopqrstuvwxyz01234567890" + min_timestamp);
  171. k.TrimAppendWithTimestamp(26, q, 1, ts_sz);
  172. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  173. "abcdefghijklmnopqrstuvwxyz0" + min_timestamp);
  174. k.TrimAppendWithTimestamp(27, p, 26, ts_sz);
  175. ASSERT_EQ(std::string(k.GetUserKey().data(), k.GetUserKey().size()),
  176. "abcdefghijklmnopqrstuvwxyz0"
  177. "abcdefghijklmnopqrstuvwxyz" +
  178. min_timestamp);
  179. // IterKey holds an internal key, the last 8 bytes hold the key footer, the
  180. // timestamp is expected to be added before the key footer.
  181. std::string key_without_ts = "keywithoutts";
  182. k.SetInternalKey(key_without_ts + min_timestamp + "internal");
  183. ASSERT_EQ(std::string(k.GetInternalKey().data(), k.GetInternalKey().size()),
  184. key_without_ts + min_timestamp + "internal");
  185. k.TrimAppendWithTimestamp(0, p, 10, ts_sz);
  186. ASSERT_EQ(std::string(k.GetInternalKey().data(), k.GetInternalKey().size()),
  187. "ab" + min_timestamp + "cdefghij");
  188. k.TrimAppendWithTimestamp(1, p, 8, ts_sz);
  189. ASSERT_EQ(std::string(k.GetInternalKey().data(), k.GetInternalKey().size()),
  190. "a" + min_timestamp + "abcdefgh");
  191. k.TrimAppendWithTimestamp(9, p, 3, ts_sz);
  192. ASSERT_EQ(std::string(k.GetInternalKey().data(), k.GetInternalKey().size()),
  193. "aabc" + min_timestamp + "defghabc");
  194. k.TrimAppendWithTimestamp(10, q, 10, ts_sz);
  195. ASSERT_EQ(std::string(k.GetInternalKey().data(), k.GetInternalKey().size()),
  196. "aabcdefgha01" + min_timestamp + "23456789");
  197. k.TrimAppendWithTimestamp(20, q, 1, ts_sz);
  198. ASSERT_EQ(std::string(k.GetInternalKey().data(), k.GetInternalKey().size()),
  199. "aabcdefgha012" + min_timestamp + "34567890");
  200. k.TrimAppendWithTimestamp(21, p, 26, ts_sz);
  201. ASSERT_EQ(
  202. std::string(k.GetInternalKey().data(), k.GetInternalKey().size()),
  203. "aabcdefgha01234567890abcdefghijklmnopqr" + min_timestamp + "stuvwxyz");
  204. }
  205. TEST_F(FormatTest, UpdateInternalKey) {
  206. std::string user_key("abcdefghijklmnopqrstuvwxyz");
  207. uint64_t new_seq = 0x123456;
  208. ValueType new_val_type = kTypeDeletion;
  209. std::string ikey;
  210. AppendInternalKey(&ikey, ParsedInternalKey(user_key, 100U, kTypeValue));
  211. size_t ikey_size = ikey.size();
  212. UpdateInternalKey(&ikey, new_seq, new_val_type);
  213. ASSERT_EQ(ikey_size, ikey.size());
  214. Slice in(ikey);
  215. ParsedInternalKey decoded;
  216. ASSERT_OK(ParseInternalKey(in, &decoded, true /* log_err_key */));
  217. ASSERT_EQ(user_key, decoded.user_key.ToString());
  218. ASSERT_EQ(new_seq, decoded.sequence);
  219. ASSERT_EQ(new_val_type, decoded.type);
  220. }
  221. TEST_F(FormatTest, RangeTombstoneSerializeEndKey) {
  222. RangeTombstone t("a", "b", 2);
  223. InternalKey k("b", 3, kTypeValue);
  224. const InternalKeyComparator cmp(BytewiseComparator());
  225. ASSERT_LT(cmp.Compare(t.SerializeEndKey(), k), 0);
  226. }
  227. TEST_F(FormatTest, PadInternalKeyWithMinTimestamp) {
  228. std::string orig_user_key = "foo";
  229. std::string orig_internal_key = IKey(orig_user_key, 100, kTypeValue);
  230. size_t ts_sz = 8;
  231. std::string key_buf;
  232. PadInternalKeyWithMinTimestamp(&key_buf, orig_internal_key, ts_sz);
  233. ParsedInternalKey key_with_timestamp;
  234. Slice in(key_buf);
  235. ASSERT_OK(ParseInternalKey(in, &key_with_timestamp, true /*log_err_key*/));
  236. std::string min_timestamp(ts_sz, static_cast<unsigned char>(0));
  237. ASSERT_EQ(orig_user_key + min_timestamp, key_with_timestamp.user_key);
  238. ASSERT_EQ(100, key_with_timestamp.sequence);
  239. ASSERT_EQ(kTypeValue, key_with_timestamp.type);
  240. }
  241. TEST_F(FormatTest, StripTimestampFromInternalKey) {
  242. std::string orig_user_key = "foo";
  243. size_t ts_sz = 8;
  244. std::string timestamp(ts_sz, static_cast<unsigned char>(0));
  245. orig_user_key.append(timestamp.data(), timestamp.size());
  246. std::string orig_internal_key = IKey(orig_user_key, 100, kTypeValue);
  247. std::string key_buf;
  248. StripTimestampFromInternalKey(&key_buf, orig_internal_key, ts_sz);
  249. ParsedInternalKey key_without_timestamp;
  250. Slice in(key_buf);
  251. ASSERT_OK(ParseInternalKey(in, &key_without_timestamp, true /*log_err_key*/));
  252. ASSERT_EQ("foo", key_without_timestamp.user_key);
  253. ASSERT_EQ(100, key_without_timestamp.sequence);
  254. ASSERT_EQ(kTypeValue, key_without_timestamp.type);
  255. }
  256. TEST_F(FormatTest, ReplaceInternalKeyWithMinTimestamp) {
  257. std::string orig_user_key = "foo";
  258. size_t ts_sz = 8;
  259. orig_user_key.append(ts_sz, static_cast<unsigned char>(1));
  260. std::string orig_internal_key = IKey(orig_user_key, 100, kTypeValue);
  261. std::string key_buf;
  262. ReplaceInternalKeyWithMinTimestamp(&key_buf, orig_internal_key, ts_sz);
  263. ParsedInternalKey new_key;
  264. Slice in(key_buf);
  265. ASSERT_OK(ParseInternalKey(in, &new_key, true /*log_err_key*/));
  266. std::string min_timestamp(ts_sz, static_cast<unsigned char>(0));
  267. size_t ukey_diff_offset = new_key.user_key.difference_offset(orig_user_key);
  268. ASSERT_EQ(min_timestamp,
  269. Slice(new_key.user_key.data() + ukey_diff_offset, ts_sz));
  270. ASSERT_EQ(orig_user_key.size(), new_key.user_key.size());
  271. ASSERT_EQ(100, new_key.sequence);
  272. ASSERT_EQ(kTypeValue, new_key.type);
  273. }
  274. TEST(RocksdbVersionTest, Version) {
  275. // Test preprocessor macros for versioning
  276. ASSERT_GT(ROCKSDB_MAJOR, 0);
  277. ASSERT_GE(ROCKSDB_MINOR, 0);
  278. ASSERT_GE(ROCKSDB_PATCH, 0);
  279. ASSERT_LT(ROCKSDB_MAJOR, 1000);
  280. ASSERT_LT(ROCKSDB_MINOR, 1000);
  281. ASSERT_LT(ROCKSDB_PATCH, 1000);
  282. ASSERT_EQ(ROCKSDB_MAKE_VERSION_INT(123, 456, 789), 123456789);
  283. ASSERT_GT(ROCKSDB_VERSION_INT, 9999999);
  284. ASSERT_LT(ROCKSDB_VERSION_INT, 99999999);
  285. static_assert(ROCKSDB_VERSION_GE(9, 8, 7));
  286. static_assert(
  287. ROCKSDB_VERSION_GE(ROCKSDB_MAJOR, ROCKSDB_MINOR, ROCKSDB_PATCH));
  288. static_assert(
  289. ROCKSDB_VERSION_GE(ROCKSDB_MAJOR, ROCKSDB_MINOR, ROCKSDB_PATCH - 1));
  290. static_assert(
  291. ROCKSDB_VERSION_GE(ROCKSDB_MAJOR, ROCKSDB_MINOR, ROCKSDB_PATCH - 100));
  292. static_assert(
  293. ROCKSDB_VERSION_GE(ROCKSDB_MAJOR, ROCKSDB_MINOR - 1, ROCKSDB_PATCH + 1));
  294. static_assert(ROCKSDB_VERSION_GE(ROCKSDB_MAJOR - 1, ROCKSDB_MINOR + 1,
  295. ROCKSDB_PATCH + 1));
  296. static_assert(
  297. !ROCKSDB_VERSION_GE(ROCKSDB_MAJOR, ROCKSDB_MINOR, ROCKSDB_PATCH + 1));
  298. static_assert(
  299. !ROCKSDB_VERSION_GE(ROCKSDB_MAJOR, ROCKSDB_MINOR, ROCKSDB_PATCH + 100));
  300. static_assert(
  301. !ROCKSDB_VERSION_GE(ROCKSDB_MAJOR, ROCKSDB_MINOR + 1, ROCKSDB_PATCH - 1));
  302. static_assert(!ROCKSDB_VERSION_GE(ROCKSDB_MAJOR + 1, ROCKSDB_MINOR - 1,
  303. ROCKSDB_PATCH - 1));
  304. // More typical usage (but with literal numbers based on relevant API
  305. // features)
  306. #if ROCKSDB_VERSION_GE(ROCKSDB_MAJOR, ROCKSDB_MINOR, ROCKSDB_PATCH)
  307. static_assert(true);
  308. #else
  309. static_assert(false);
  310. #endif
  311. #if !ROCKSDB_VERSION_GE(ROCKSDB_MAJOR, ROCKSDB_MINOR, ROCKSDB_PATCH + 1)
  312. static_assert(true);
  313. #else
  314. static_assert(false);
  315. #endif
  316. }
  317. } // namespace ROCKSDB_NAMESPACE
  318. int main(int argc, char** argv) {
  319. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  320. ::testing::InitGoogleTest(&argc, argv);
  321. RegisterCustomObjects(argc, argv);
  322. return RUN_ALL_TESTS();
  323. }