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 rivejä
6.6 KiB

3 viikkoa sitten
  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. compact_pointers_.clear();
  33. deleted_files_.clear();
  34. new_files_.clear();
  35. }
  36. void VersionEdit::EncodeTo(std::string* dst) const {
  37. if (has_comparator_) {
  38. PutVarint32(dst, kComparator);
  39. PutLengthPrefixedSlice(dst, comparator_);
  40. }
  41. if (has_log_number_) {
  42. PutVarint32(dst, kLogNumber);
  43. PutVarint64(dst, log_number_);
  44. }
  45. if (has_prev_log_number_) {
  46. PutVarint32(dst, kPrevLogNumber);
  47. PutVarint64(dst, prev_log_number_);
  48. }
  49. if (has_next_file_number_) {
  50. PutVarint32(dst, kNextFileNumber);
  51. PutVarint64(dst, next_file_number_);
  52. }
  53. if (has_last_sequence_) {
  54. PutVarint32(dst, kLastSequence);
  55. PutVarint64(dst, last_sequence_);
  56. }
  57. for (size_t i = 0; i < compact_pointers_.size(); i++) {
  58. PutVarint32(dst, kCompactPointer);
  59. PutVarint32(dst, compact_pointers_[i].first); // level
  60. PutLengthPrefixedSlice(dst, compact_pointers_[i].second.Encode());
  61. }
  62. for (const auto& deleted_file_kvp : deleted_files_) {
  63. PutVarint32(dst, kDeletedFile);
  64. PutVarint32(dst, deleted_file_kvp.first); // level
  65. PutVarint64(dst, deleted_file_kvp.second); // file number
  66. }
  67. for (size_t i = 0; i < new_files_.size(); i++) {
  68. const FileMetaData& f = new_files_[i].second;
  69. PutVarint32(dst, kNewFile);
  70. PutVarint32(dst, new_files_[i].first); // level
  71. PutVarint64(dst, f.number);
  72. PutVarint64(dst, f.file_size);
  73. PutLengthPrefixedSlice(dst, f.smallest.Encode());
  74. PutLengthPrefixedSlice(dst, f.largest.Encode());
  75. }
  76. }
  77. static bool GetInternalKey(Slice* input, InternalKey* dst) {
  78. Slice str;
  79. if (GetLengthPrefixedSlice(input, &str)) {
  80. return dst->DecodeFrom(str);
  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 RemoveFile: ");
  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