Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

111 righe
3.8 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/dbformat.h"
  5. #include "util/logging.h"
  6. #include "util/testharness.h"
  7. namespace leveldb {
  8. static std::string IKey(const std::string& user_key, uint64_t seq,
  9. ValueType vt) {
  10. std::string encoded;
  11. AppendInternalKey(&encoded, ParsedInternalKey(user_key, seq, vt));
  12. return encoded;
  13. }
  14. static std::string Shorten(const std::string& s, const std::string& l) {
  15. std::string result = s;
  16. InternalKeyComparator(BytewiseComparator()).FindShortestSeparator(&result, l);
  17. return result;
  18. }
  19. static std::string ShortSuccessor(const std::string& s) {
  20. std::string result = s;
  21. InternalKeyComparator(BytewiseComparator()).FindShortSuccessor(&result);
  22. return result;
  23. }
  24. static void TestKey(const std::string& key, uint64_t seq, ValueType vt) {
  25. std::string encoded = IKey(key, seq, vt);
  26. Slice in(encoded);
  27. ParsedInternalKey decoded("", 0, kTypeValue);
  28. ASSERT_TRUE(ParseInternalKey(in, &decoded));
  29. ASSERT_EQ(key, decoded.user_key.ToString());
  30. ASSERT_EQ(seq, decoded.sequence);
  31. ASSERT_EQ(vt, decoded.type);
  32. ASSERT_TRUE(!ParseInternalKey(Slice("bar"), &decoded));
  33. }
  34. class FormatTest {};
  35. TEST(FormatTest, InternalKey_EncodeDecode) {
  36. const char* keys[] = {"", "k", "hello", "longggggggggggggggggggggg"};
  37. const uint64_t seq[] = {1,
  38. 2,
  39. 3,
  40. (1ull << 8) - 1,
  41. 1ull << 8,
  42. (1ull << 8) + 1,
  43. (1ull << 16) - 1,
  44. 1ull << 16,
  45. (1ull << 16) + 1,
  46. (1ull << 32) - 1,
  47. 1ull << 32,
  48. (1ull << 32) + 1};
  49. for (int k = 0; k < sizeof(keys) / sizeof(keys[0]); k++) {
  50. for (int s = 0; s < sizeof(seq) / sizeof(seq[0]); s++) {
  51. TestKey(keys[k], seq[s], kTypeValue);
  52. TestKey("hello", 1, kTypeDeletion);
  53. }
  54. }
  55. }
  56. TEST(FormatTest, InternalKeyShortSeparator) {
  57. // When user keys are same
  58. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  59. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 99, kTypeValue)));
  60. ASSERT_EQ(
  61. IKey("foo", 100, kTypeValue),
  62. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 101, kTypeValue)));
  63. ASSERT_EQ(
  64. IKey("foo", 100, kTypeValue),
  65. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 100, kTypeValue)));
  66. ASSERT_EQ(
  67. IKey("foo", 100, kTypeValue),
  68. Shorten(IKey("foo", 100, kTypeValue), IKey("foo", 100, kTypeDeletion)));
  69. // When user keys are misordered
  70. ASSERT_EQ(IKey("foo", 100, kTypeValue),
  71. Shorten(IKey("foo", 100, kTypeValue), IKey("bar", 99, kTypeValue)));
  72. // When user keys are different, but correctly ordered
  73. ASSERT_EQ(
  74. IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
  75. Shorten(IKey("foo", 100, kTypeValue), IKey("hello", 200, kTypeValue)));
  76. // When start user key is prefix of limit user key
  77. ASSERT_EQ(
  78. IKey("foo", 100, kTypeValue),
  79. Shorten(IKey("foo", 100, kTypeValue), IKey("foobar", 200, kTypeValue)));
  80. // When limit user key is prefix of start user key
  81. ASSERT_EQ(
  82. IKey("foobar", 100, kTypeValue),
  83. Shorten(IKey("foobar", 100, kTypeValue), IKey("foo", 200, kTypeValue)));
  84. }
  85. TEST(FormatTest, InternalKeyShortestSuccessor) {
  86. ASSERT_EQ(IKey("g", kMaxSequenceNumber, kValueTypeForSeek),
  87. ShortSuccessor(IKey("foo", 100, kTypeValue)));
  88. ASSERT_EQ(IKey("\xff\xff", 100, kTypeValue),
  89. ShortSuccessor(IKey("\xff\xff", 100, kTypeValue)));
  90. }
  91. } // namespace leveldb
  92. int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }