You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 line
4.9 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/filename.h"
  5. #include "db/dbformat.h"
  6. #include "port/port.h"
  7. #include "util/logging.h"
  8. #include "util/testharness.h"
  9. namespace leveldb {
  10. class FileNameTest { };
  11. TEST(FileNameTest, Parse) {
  12. Slice db;
  13. FileType type;
  14. uint64_t number;
  15. LargeValueRef large_ref;
  16. // Successful parses
  17. static struct {
  18. const char* fname;
  19. uint64_t number;
  20. const char* large_ref;
  21. FileType type;
  22. } cases[] = {
  23. { "100.log", 100, "", kLogFile },
  24. { "0.log", 0, "", kLogFile },
  25. { "0.sst", 0, "", kTableFile },
  26. { "CURRENT", 0, "", kCurrentFile },
  27. { "LOCK", 0, "", kDBLockFile },
  28. { "MANIFEST-2", 2, "", kDescriptorFile },
  29. { "MANIFEST-7", 7, "", kDescriptorFile },
  30. { "LOG", 0, "", kInfoLogFile },
  31. { "LOG.old", 0, "", kInfoLogFile },
  32. { "18446744073709551615.log", 18446744073709551615ull, "",
  33. kLogFile },
  34. { "2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2323-1234-0.val", 0,
  35. "2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2323-1234-0", kLargeValueFile },
  36. { "2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2323-10000000000-0.val", 0,
  37. "2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2323-10000000000-0",
  38. kLargeValueFile },
  39. };
  40. for (int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
  41. std::string f = cases[i].fname;
  42. ASSERT_TRUE(ParseFileName(f, &number, &large_ref, &type)) << f;
  43. ASSERT_EQ(cases[i].type, type) << f;
  44. if (type == kLargeValueFile) {
  45. ASSERT_EQ(cases[i].large_ref, LargeValueRefToFilenameString(large_ref))
  46. << f;
  47. } else {
  48. ASSERT_EQ(cases[i].number, number) << f;
  49. }
  50. }
  51. // Errors
  52. static const char* errors[] = {
  53. "",
  54. "foo",
  55. "foo-dx-100.log",
  56. ".log",
  57. "",
  58. "manifest",
  59. "CURREN",
  60. "CURRENTX",
  61. "MANIFES",
  62. "MANIFEST",
  63. "MANIFEST-",
  64. "XMANIFEST-3",
  65. "MANIFEST-3x",
  66. "LOC",
  67. "LOCKx",
  68. "LO",
  69. "LOGx",
  70. "18446744073709551616.log",
  71. "184467440737095516150.log",
  72. "100",
  73. "100.",
  74. "100.lop",
  75. "100.val",
  76. ".val",
  77. "123456789012345678901234567890123456789-12340.val",
  78. "1234567890123456789012345678901234567-123-0.val",
  79. "12345678901234567890123456789012345678902-100-1-.val",
  80. // Overflow on value size
  81. "2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2323-100000000000000000000-1.val",
  82. // '03.val' is a bad compression type
  83. "2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2e2323-100000-3.val" };
  84. for (int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
  85. std::string f = errors[i];
  86. ASSERT_TRUE(!ParseFileName(f, &number, &large_ref, &type)) << f;
  87. };
  88. }
  89. TEST(FileNameTest, Construction) {
  90. uint64_t number;
  91. FileType type;
  92. LargeValueRef large_ref;
  93. std::string fname;
  94. fname = CurrentFileName("foo");
  95. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  96. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &large_ref, &type));
  97. ASSERT_EQ(0, number);
  98. ASSERT_EQ(kCurrentFile, type);
  99. fname = LockFileName("foo");
  100. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  101. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &large_ref, &type));
  102. ASSERT_EQ(0, number);
  103. ASSERT_EQ(kDBLockFile, type);
  104. fname = LogFileName("foo", 192);
  105. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  106. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &large_ref, &type));
  107. ASSERT_EQ(192, number);
  108. ASSERT_EQ(kLogFile, type);
  109. fname = TableFileName("bar", 200);
  110. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  111. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &large_ref, &type));
  112. ASSERT_EQ(200, number);
  113. ASSERT_EQ(kTableFile, type);
  114. fname = DescriptorFileName("bar", 100);
  115. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  116. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &large_ref, &type));
  117. ASSERT_EQ(100, number);
  118. ASSERT_EQ(kDescriptorFile, type);
  119. fname = TempFileName("tmp", 999);
  120. ASSERT_EQ("tmp/", std::string(fname.data(), 4));
  121. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &large_ref, &type));
  122. ASSERT_EQ(999, number);
  123. ASSERT_EQ(kTempFile, type);
  124. for (int i = 0; i <= kSnappyCompression; i++) {
  125. CompressionType ctype = static_cast<CompressionType>(i);
  126. std::string value = "abcdef";
  127. LargeValueRef real_large_ref = LargeValueRef::Make(Slice(value), ctype);
  128. fname = LargeValueFileName("tmp", real_large_ref);
  129. ASSERT_EQ("tmp/", std::string(fname.data(), 4));
  130. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &large_ref, &type));
  131. ASSERT_TRUE(real_large_ref == large_ref);
  132. ASSERT_EQ(kLargeValueFile, type);
  133. ASSERT_EQ(large_ref.compression_type(), ctype);
  134. }
  135. }
  136. }
  137. int main(int argc, char** argv) {
  138. return leveldb::test::RunAllTests();
  139. }