作者: 谢瑞阳 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.

258 lines
6.6 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/version_edit.h"
  5. #include "db/version_set.h"
  6. #include "util/coding.h"
  7. namespace leveldb {
  8. // Tag numbers for serialized VersionEdit. These numbers are written to
  9. // disk and should not be changed.
  10. enum Tag {
  11. kComparator = 1,
  12. kLogNumber = 2,
  13. kNextFileNumber = 3,
  14. kLastSequence = 4,
  15. kCompactPointer = 5,
  16. kDeletedFile = 6,
  17. kNewFile = 7,
  18. // 8 was used for large value refs
  19. kPrevLogNumber = 9
  20. };
  21. void VersionEdit::Clear() {
  22. comparator_.clear();
  23. log_number_ = 0;
  24. prev_log_number_ = 0;
  25. last_sequence_ = 0;
  26. next_file_number_ = 0;
  27. has_comparator_ = false;
  28. has_log_number_ = false;
  29. has_prev_log_number_ = false;
  30. has_next_file_number_ = false;
  31. has_last_sequence_ = false;
  32. deleted_files_.clear();
  33. new_files_.clear();
  34. }
  35. void VersionEdit::EncodeTo(std::string* dst) const {
  36. if (has_comparator_) {
  37. PutVarint32(dst, kComparator);
  38. PutLengthPrefixedSlice(dst, comparator_);
  39. }
  40. if (has_log_number_) {
  41. PutVarint32(dst, kLogNumber);
  42. PutVarint64(dst, log_number_);
  43. }
  44. if (has_prev_log_number_) {
  45. PutVarint32(dst, kPrevLogNumber);
  46. PutVarint64(dst, prev_log_number_);
  47. }
  48. if (has_next_file_number_) {
  49. PutVarint32(dst, kNextFileNumber);
  50. PutVarint64(dst, next_file_number_);
  51. }
  52. if (has_last_sequence_) {
  53. PutVarint32(dst, kLastSequence);
  54. PutVarint64(dst, last_sequence_);
  55. }
  56. for (size_t i = 0; i < compact_pointers_.size(); i++) {
  57. PutVarint32(dst, kCompactPointer);
  58. PutVarint32(dst, compact_pointers_[i].first); // level
  59. PutLengthPrefixedSlice(dst, compact_pointers_[i].second.Encode());
  60. }
  61. for (const auto& deleted_file_kvp : deleted_files_) {
  62. PutVarint32(dst, kDeletedFile);
  63. PutVarint32(dst, deleted_file_kvp.first); // level
  64. PutVarint64(dst, deleted_file_kvp.second); // file number
  65. }
  66. for (size_t i = 0; i < new_files_.size(); i++) {
  67. const FileMetaData& f = new_files_[i].second;
  68. PutVarint32(dst, kNewFile);
  69. PutVarint32(dst, new_files_[i].first); // level
  70. PutVarint64(dst, f.number);
  71. PutVarint64(dst, f.file_size);
  72. PutLengthPrefixedSlice(dst, f.smallest.Encode());
  73. PutLengthPrefixedSlice(dst, f.largest.Encode());
  74. }
  75. }
  76. static bool GetInternalKey(Slice* input, InternalKey* dst) {
  77. Slice str;
  78. if (GetLengthPrefixedSlice(input, &str)) {
  79. dst->DecodeFrom(str);
  80. return true;
  81. } else {
  82. return false;
  83. }
  84. }
  85. static bool GetLevel(Slice* input, int* level) {
  86. uint32_t v;
  87. if (GetVarint32(input, &v) && v < config::kNumLevels) {
  88. *level = v;
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. }
  94. Status VersionEdit::DecodeFrom(const Slice& src) {
  95. Clear();
  96. Slice input = src;
  97. const char* msg = nullptr;
  98. uint32_t tag;
  99. // Temporary storage for parsing
  100. int level;
  101. uint64_t number;
  102. FileMetaData f;
  103. Slice str;
  104. InternalKey key;
  105. while (msg == nullptr && GetVarint32(&input, &tag)) {
  106. switch (tag) {
  107. case kComparator:
  108. if (GetLengthPrefixedSlice(&input, &str)) {
  109. comparator_ = str.ToString();
  110. has_comparator_ = true;
  111. } else {
  112. msg = "comparator name";
  113. }
  114. break;
  115. case kLogNumber:
  116. if (GetVarint64(&input, &log_number_)) {
  117. has_log_number_ = true;
  118. } else {
  119. msg = "log number";
  120. }
  121. break;
  122. case kPrevLogNumber:
  123. if (GetVarint64(&input, &prev_log_number_)) {
  124. has_prev_log_number_ = true;
  125. } else {
  126. msg = "previous log number";
  127. }
  128. break;
  129. case kNextFileNumber:
  130. if (GetVarint64(&input, &next_file_number_)) {
  131. has_next_file_number_ = true;
  132. } else {
  133. msg = "next file number";
  134. }
  135. break;
  136. case kLastSequence:
  137. if (GetVarint64(&input, &last_sequence_)) {
  138. has_last_sequence_ = true;
  139. } else {
  140. msg = "last sequence number";
  141. }
  142. break;
  143. case kCompactPointer:
  144. if (GetLevel(&input, &level) && GetInternalKey(&input, &key)) {
  145. compact_pointers_.push_back(std::make_pair(level, key));
  146. } else {
  147. msg = "compaction pointer";
  148. }
  149. break;
  150. case kDeletedFile:
  151. if (GetLevel(&input, &level) && GetVarint64(&input, &number)) {
  152. deleted_files_.insert(std::make_pair(level, number));
  153. } else {
  154. msg = "deleted file";
  155. }
  156. break;
  157. case kNewFile:
  158. if (GetLevel(&input, &level) && GetVarint64(&input, &f.number) &&
  159. GetVarint64(&input, &f.file_size) &&
  160. GetInternalKey(&input, &f.smallest) &&
  161. GetInternalKey(&input, &f.largest)) {
  162. new_files_.push_back(std::make_pair(level, f));
  163. } else {
  164. msg = "new-file entry";
  165. }
  166. break;
  167. default:
  168. msg = "unknown tag";
  169. break;
  170. }
  171. }
  172. if (msg == nullptr && !input.empty()) {
  173. msg = "invalid tag";
  174. }
  175. Status result;
  176. if (msg != nullptr) {
  177. result = Status::Corruption("VersionEdit", msg);
  178. }
  179. return result;
  180. }
  181. std::string VersionEdit::DebugString() const {
  182. std::string r;
  183. r.append("VersionEdit {");
  184. if (has_comparator_) {
  185. r.append("\n Comparator: ");
  186. r.append(comparator_);
  187. }
  188. if (has_log_number_) {
  189. r.append("\n LogNumber: ");
  190. AppendNumberTo(&r, log_number_);
  191. }
  192. if (has_prev_log_number_) {
  193. r.append("\n PrevLogNumber: ");
  194. AppendNumberTo(&r, prev_log_number_);
  195. }
  196. if (has_next_file_number_) {
  197. r.append("\n NextFile: ");
  198. AppendNumberTo(&r, next_file_number_);
  199. }
  200. if (has_last_sequence_) {
  201. r.append("\n LastSeq: ");
  202. AppendNumberTo(&r, last_sequence_);
  203. }
  204. for (size_t i = 0; i < compact_pointers_.size(); i++) {
  205. r.append("\n CompactPointer: ");
  206. AppendNumberTo(&r, compact_pointers_[i].first);
  207. r.append(" ");
  208. r.append(compact_pointers_[i].second.DebugString());
  209. }
  210. for (const auto& deleted_files_kvp : deleted_files_) {
  211. r.append("\n DeleteFile: ");
  212. AppendNumberTo(&r, deleted_files_kvp.first);
  213. r.append(" ");
  214. AppendNumberTo(&r, deleted_files_kvp.second);
  215. }
  216. for (size_t i = 0; i < new_files_.size(); i++) {
  217. const FileMetaData& f = new_files_[i].second;
  218. r.append("\n AddFile: ");
  219. AppendNumberTo(&r, new_files_[i].first);
  220. r.append(" ");
  221. AppendNumberTo(&r, f.number);
  222. r.append(" ");
  223. AppendNumberTo(&r, f.file_size);
  224. r.append(" ");
  225. r.append(f.smallest.DebugString());
  226. r.append(" .. ");
  227. r.append(f.largest.DebugString());
  228. }
  229. r.append("\n}\n");
  230. return r;
  231. }
  232. } // namespace leveldb