作者: 谢瑞阳 10225101483 徐翔宇 10225101535
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.

127 lines
4.1 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 "gtest/gtest.h"
  6. #include "db/dbformat.h"
  7. #include "port/port.h"
  8. #include "util/logging.h"
  9. namespace leveldb {
  10. TEST(FileNameTest, Parse) {
  11. Slice db;
  12. FileType type;
  13. uint64_t number;
  14. // Successful parses
  15. static struct {
  16. const char* fname;
  17. uint64_t number;
  18. FileType type;
  19. } cases[] = {
  20. {"100.log", 100, kLogFile},
  21. {"0.log", 0, kLogFile},
  22. {"0.sst", 0, kTableFile},
  23. {"0.ldb", 0, kTableFile},
  24. {"CURRENT", 0, kCurrentFile},
  25. {"LOCK", 0, kDBLockFile},
  26. {"MANIFEST-2", 2, kDescriptorFile},
  27. {"MANIFEST-7", 7, kDescriptorFile},
  28. {"LOG", 0, kInfoLogFile},
  29. {"LOG.old", 0, kInfoLogFile},
  30. {"18446744073709551615.log", 18446744073709551615ull, kLogFile},
  31. };
  32. for (int i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
  33. std::string f = cases[i].fname;
  34. ASSERT_TRUE(ParseFileName(f, &number, &type)) << f;
  35. ASSERT_EQ(cases[i].type, type) << f;
  36. ASSERT_EQ(cases[i].number, number) << f;
  37. }
  38. // Errors
  39. static const char* errors[] = {"",
  40. "foo",
  41. "foo-dx-100.log",
  42. ".log",
  43. "",
  44. "manifest",
  45. "CURREN",
  46. "CURRENTX",
  47. "MANIFES",
  48. "MANIFEST",
  49. "MANIFEST-",
  50. "XMANIFEST-3",
  51. "MANIFEST-3x",
  52. "LOC",
  53. "LOCKx",
  54. "LO",
  55. "LOGx",
  56. "18446744073709551616.log",
  57. "184467440737095516150.log",
  58. "100",
  59. "100.",
  60. "100.lop"};
  61. for (int i = 0; i < sizeof(errors) / sizeof(errors[0]); i++) {
  62. std::string f = errors[i];
  63. ASSERT_TRUE(!ParseFileName(f, &number, &type)) << f;
  64. }
  65. }
  66. TEST(FileNameTest, Construction) {
  67. uint64_t number;
  68. FileType type;
  69. std::string fname;
  70. fname = CurrentFileName("foo");
  71. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  72. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  73. ASSERT_EQ(0, number);
  74. ASSERT_EQ(kCurrentFile, type);
  75. fname = LockFileName("foo");
  76. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  77. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  78. ASSERT_EQ(0, number);
  79. ASSERT_EQ(kDBLockFile, type);
  80. fname = LogFileName("foo", 192);
  81. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  82. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  83. ASSERT_EQ(192, number);
  84. ASSERT_EQ(kLogFile, type);
  85. fname = TableFileName("bar", 200);
  86. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  87. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  88. ASSERT_EQ(200, number);
  89. ASSERT_EQ(kTableFile, type);
  90. fname = DescriptorFileName("bar", 100);
  91. ASSERT_EQ("bar/", std::string(fname.data(), 4));
  92. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  93. ASSERT_EQ(100, number);
  94. ASSERT_EQ(kDescriptorFile, type);
  95. fname = TempFileName("tmp", 999);
  96. ASSERT_EQ("tmp/", std::string(fname.data(), 4));
  97. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  98. ASSERT_EQ(999, number);
  99. ASSERT_EQ(kTempFile, type);
  100. fname = InfoLogFileName("foo");
  101. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  102. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  103. ASSERT_EQ(0, number);
  104. ASSERT_EQ(kInfoLogFile, type);
  105. fname = OldInfoLogFileName("foo");
  106. ASSERT_EQ("foo/", std::string(fname.data(), 4));
  107. ASSERT_TRUE(ParseFileName(fname.c_str() + 4, &number, &type));
  108. ASSERT_EQ(0, number);
  109. ASSERT_EQ(kInfoLogFile, type);
  110. }
  111. } // namespace leveldb