10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

266 lines
6.9 KiB

Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}. The "DeleteFile" method name causes pain for Windows developers, because <windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA. Current code uses workarounds, like #undefining DeleteFile everywhere an Env is declared, implemented, or used. This CL removes the need for workarounds by renaming Env::DeleteFile to Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to Env::RemoveDir. A few internal methods are also renamed for consistency. Software that supports Windows is expected to migrate any Env implementations and usage to Remove{File,Dir}, and never use the name Env::Delete{File,Dir} in its code. The renaming is done in a backwards-compatible way, at the risk of making it slightly more difficult to build a new correct Env implementation. The backwards compatibility is achieved using the following hacks: 1) Env::Remove{File,Dir} methods are added, with a default implementation that calls into Env::Delete{File,Dir}. This makes old Env implementations compatible with code that calls into the updated API. 2) The Env::Delete{File,Dir} methods are no longer pure virtuals. Instead, they gain a default implementation that calls into Env::Remove{File,Dir}. This makes updated Env implementations compatible with code that calls into the old API. The cost of this approach is that it's possible to write an Env without overriding either Rename{File,Dir} or Delete{File,Dir}, without getting a compiler warning. However, attempting to run the test suite will immediately fail with an infinite call stack ending in {Remove,Delete}{File,Dir}, making developers aware of the problem. PiperOrigin-RevId: 288710907
4 years ago
  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. PutVarint64(dst,f.smallest_deadtime);
  76. PutVarint64(dst,f.largest_deadtime);
  77. }
  78. }
  79. static bool GetInternalKey(Slice* input, InternalKey* dst) {
  80. Slice str;
  81. if (GetLengthPrefixedSlice(input, &str)) {
  82. return dst->DecodeFrom(str);
  83. } else {
  84. return false;
  85. }
  86. }
  87. static bool GetLevel(Slice* input, int* level) {
  88. uint32_t v;
  89. if (GetVarint32(input, &v) && v < config::kNumLevels) {
  90. *level = v;
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96. Status VersionEdit::DecodeFrom(const Slice& src) {
  97. Clear();
  98. Slice input = src;
  99. const char* msg = nullptr;
  100. uint32_t tag;
  101. // Temporary storage for parsing
  102. int level;
  103. uint64_t number;
  104. FileMetaData f;
  105. Slice str;
  106. InternalKey key;
  107. while (msg == nullptr && GetVarint32(&input, &tag)) {
  108. switch (tag) {
  109. case kComparator:
  110. if (GetLengthPrefixedSlice(&input, &str)) {
  111. comparator_ = str.ToString();
  112. has_comparator_ = true;
  113. } else {
  114. msg = "comparator name";
  115. }
  116. break;
  117. case kLogNumber:
  118. if (GetVarint64(&input, &log_number_)) {
  119. has_log_number_ = true;
  120. } else {
  121. msg = "log number";
  122. }
  123. break;
  124. case kPrevLogNumber:
  125. if (GetVarint64(&input, &prev_log_number_)) {
  126. has_prev_log_number_ = true;
  127. } else {
  128. msg = "previous log number";
  129. }
  130. break;
  131. case kNextFileNumber:
  132. if (GetVarint64(&input, &next_file_number_)) {
  133. has_next_file_number_ = true;
  134. } else {
  135. msg = "next file number";
  136. }
  137. break;
  138. case kLastSequence:
  139. if (GetVarint64(&input, &last_sequence_)) {
  140. has_last_sequence_ = true;
  141. } else {
  142. msg = "last sequence number";
  143. }
  144. break;
  145. case kCompactPointer:
  146. if (GetLevel(&input, &level) && GetInternalKey(&input, &key)) {
  147. compact_pointers_.push_back(std::make_pair(level, key));
  148. } else {
  149. msg = "compaction pointer";
  150. }
  151. break;
  152. case kDeletedFile:
  153. if (GetLevel(&input, &level) && GetVarint64(&input, &number)) {
  154. deleted_files_.insert(std::make_pair(level, number));
  155. } else {
  156. msg = "deleted file";
  157. }
  158. break;
  159. case kNewFile:
  160. if (GetLevel(&input, &level) && GetVarint64(&input, &f.number) &&
  161. GetVarint64(&input, &f.file_size) &&
  162. GetInternalKey(&input, &f.smallest) &&
  163. GetInternalKey(&input, &f.largest) &&
  164. GetVarint64(&input,&f.smallest_deadtime) &&
  165. GetVarint64(&input,&f.largest_deadtime)) {
  166. new_files_.push_back(std::make_pair(level, f));
  167. } else {
  168. msg = "new-file entry";
  169. }
  170. break;
  171. default:
  172. msg = "unknown tag";
  173. break;
  174. }
  175. }
  176. if (msg == nullptr && !input.empty()) {
  177. msg = "invalid tag";
  178. }
  179. Status result;
  180. if (msg != nullptr) {
  181. result = Status::Corruption("VersionEdit", msg);
  182. }
  183. return result;
  184. }
  185. std::string VersionEdit::DebugString() const {
  186. std::string r;
  187. r.append("VersionEdit {");
  188. if (has_comparator_) {
  189. r.append("\n Comparator: ");
  190. r.append(comparator_);
  191. }
  192. if (has_log_number_) {
  193. r.append("\n LogNumber: ");
  194. AppendNumberTo(&r, log_number_);
  195. }
  196. if (has_prev_log_number_) {
  197. r.append("\n PrevLogNumber: ");
  198. AppendNumberTo(&r, prev_log_number_);
  199. }
  200. if (has_next_file_number_) {
  201. r.append("\n NextFile: ");
  202. AppendNumberTo(&r, next_file_number_);
  203. }
  204. if (has_last_sequence_) {
  205. r.append("\n LastSeq: ");
  206. AppendNumberTo(&r, last_sequence_);
  207. }
  208. for (size_t i = 0; i < compact_pointers_.size(); i++) {
  209. r.append("\n CompactPointer: ");
  210. AppendNumberTo(&r, compact_pointers_[i].first);
  211. r.append(" ");
  212. r.append(compact_pointers_[i].second.DebugString());
  213. }
  214. for (const auto& deleted_files_kvp : deleted_files_) {
  215. r.append("\n RemoveFile: ");
  216. AppendNumberTo(&r, deleted_files_kvp.first);
  217. r.append(" ");
  218. AppendNumberTo(&r, deleted_files_kvp.second);
  219. }
  220. for (size_t i = 0; i < new_files_.size(); i++) {
  221. const FileMetaData& f = new_files_[i].second;
  222. r.append("\n AddFile: ");
  223. AppendNumberTo(&r, new_files_[i].first);
  224. r.append(" ");
  225. AppendNumberTo(&r, f.number);
  226. r.append(" ");
  227. AppendNumberTo(&r, f.file_size);
  228. r.append(" ");
  229. r.append(f.smallest.DebugString());
  230. r.append(" .. ");
  231. r.append(f.largest.DebugString());
  232. r.append(" ");
  233. AppendNumberTo(&r,f.smallest_deadtime);
  234. r.append(" .. ");
  235. AppendNumberTo(&r,f.largest_deadtime);
  236. }
  237. r.append("\n}\n");
  238. return r;
  239. }
  240. } // namespace leveldb