提供基本的ttl测试用例
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.

260 lines
6.7 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 (DeletedFileSet::const_iterator iter = deleted_files_.begin();
  62. iter != deleted_files_.end(); ++iter) {
  63. PutVarint32(dst, kDeletedFile);
  64. PutVarint32(dst, iter->first); // level
  65. PutVarint64(dst, iter->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. dst->DecodeFrom(str);
  81. return true;
  82. } else {
  83. return false;
  84. }
  85. }
  86. static bool GetLevel(Slice* input, int* level) {
  87. uint32_t v;
  88. if (GetVarint32(input, &v) && v < config::kNumLevels) {
  89. *level = v;
  90. return true;
  91. } else {
  92. return false;
  93. }
  94. }
  95. Status VersionEdit::DecodeFrom(const Slice& src) {
  96. Clear();
  97. Slice input = src;
  98. const char* msg = nullptr;
  99. uint32_t tag;
  100. // Temporary storage for parsing
  101. int level;
  102. uint64_t number;
  103. FileMetaData f;
  104. Slice str;
  105. InternalKey key;
  106. while (msg == nullptr && GetVarint32(&input, &tag)) {
  107. switch (tag) {
  108. case kComparator:
  109. if (GetLengthPrefixedSlice(&input, &str)) {
  110. comparator_ = str.ToString();
  111. has_comparator_ = true;
  112. } else {
  113. msg = "comparator name";
  114. }
  115. break;
  116. case kLogNumber:
  117. if (GetVarint64(&input, &log_number_)) {
  118. has_log_number_ = true;
  119. } else {
  120. msg = "log number";
  121. }
  122. break;
  123. case kPrevLogNumber:
  124. if (GetVarint64(&input, &prev_log_number_)) {
  125. has_prev_log_number_ = true;
  126. } else {
  127. msg = "previous log number";
  128. }
  129. break;
  130. case kNextFileNumber:
  131. if (GetVarint64(&input, &next_file_number_)) {
  132. has_next_file_number_ = true;
  133. } else {
  134. msg = "next file number";
  135. }
  136. break;
  137. case kLastSequence:
  138. if (GetVarint64(&input, &last_sequence_)) {
  139. has_last_sequence_ = true;
  140. } else {
  141. msg = "last sequence number";
  142. }
  143. break;
  144. case kCompactPointer:
  145. if (GetLevel(&input, &level) && GetInternalKey(&input, &key)) {
  146. compact_pointers_.push_back(std::make_pair(level, key));
  147. } else {
  148. msg = "compaction pointer";
  149. }
  150. break;
  151. case kDeletedFile:
  152. if (GetLevel(&input, &level) && GetVarint64(&input, &number)) {
  153. deleted_files_.insert(std::make_pair(level, number));
  154. } else {
  155. msg = "deleted file";
  156. }
  157. break;
  158. case kNewFile:
  159. if (GetLevel(&input, &level) && GetVarint64(&input, &f.number) &&
  160. GetVarint64(&input, &f.file_size) &&
  161. GetInternalKey(&input, &f.smallest) &&
  162. GetInternalKey(&input, &f.largest)) {
  163. new_files_.push_back(std::make_pair(level, f));
  164. } else {
  165. msg = "new-file entry";
  166. }
  167. break;
  168. default:
  169. msg = "unknown tag";
  170. break;
  171. }
  172. }
  173. if (msg == nullptr && !input.empty()) {
  174. msg = "invalid tag";
  175. }
  176. Status result;
  177. if (msg != nullptr) {
  178. result = Status::Corruption("VersionEdit", msg);
  179. }
  180. return result;
  181. }
  182. std::string VersionEdit::DebugString() const {
  183. std::string r;
  184. r.append("VersionEdit {");
  185. if (has_comparator_) {
  186. r.append("\n Comparator: ");
  187. r.append(comparator_);
  188. }
  189. if (has_log_number_) {
  190. r.append("\n LogNumber: ");
  191. AppendNumberTo(&r, log_number_);
  192. }
  193. if (has_prev_log_number_) {
  194. r.append("\n PrevLogNumber: ");
  195. AppendNumberTo(&r, prev_log_number_);
  196. }
  197. if (has_next_file_number_) {
  198. r.append("\n NextFile: ");
  199. AppendNumberTo(&r, next_file_number_);
  200. }
  201. if (has_last_sequence_) {
  202. r.append("\n LastSeq: ");
  203. AppendNumberTo(&r, last_sequence_);
  204. }
  205. for (size_t i = 0; i < compact_pointers_.size(); i++) {
  206. r.append("\n CompactPointer: ");
  207. AppendNumberTo(&r, compact_pointers_[i].first);
  208. r.append(" ");
  209. r.append(compact_pointers_[i].second.DebugString());
  210. }
  211. for (DeletedFileSet::const_iterator iter = deleted_files_.begin();
  212. iter != deleted_files_.end(); ++iter) {
  213. r.append("\n DeleteFile: ");
  214. AppendNumberTo(&r, iter->first);
  215. r.append(" ");
  216. AppendNumberTo(&r, iter->second);
  217. }
  218. for (size_t i = 0; i < new_files_.size(); i++) {
  219. const FileMetaData& f = new_files_[i].second;
  220. r.append("\n AddFile: ");
  221. AppendNumberTo(&r, new_files_[i].first);
  222. r.append(" ");
  223. AppendNumberTo(&r, f.number);
  224. r.append(" ");
  225. AppendNumberTo(&r, f.file_size);
  226. r.append(" ");
  227. r.append(f.smallest.DebugString());
  228. r.append(" .. ");
  229. r.append(f.largest.DebugString());
  230. }
  231. r.append("\n}\n");
  232. return r;
  233. }
  234. } // namespace leveldb