10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲

225 linhas
6.2 KiB

Release 1.18 Changes are: * Update version number to 1.18 * Replace the basic fprintf call with a call to fwrite in order to work around the apparent compiler optimization/rewrite failure that we are seeing with the new toolchain/iOS SDKs provided with Xcode6 and iOS8. * Fix ALL the header guards. * Createed a README.md with the LevelDB project description. * A new CONTRIBUTING file. * Don't implicitly convert uint64_t to size_t or int. Either preserve it as uint64_t, or explicitly cast. This fixes MSVC warnings about possible value truncation when compiling this code in Chromium. * Added a DumpFile() library function that encapsulates the guts of the "leveldbutil dump" command. This will allow clients to dump data to their log files instead of stdout. It will also allow clients to supply their own environment. * leveldb: Remove unused function 'ConsumeChar'. * leveldbutil: Remove unused member variables from WriteBatchItemPrinter. * OpenBSD, NetBSD and DragonflyBSD have _LITTLE_ENDIAN, so define PLATFORM_IS_LITTLE_ENDIAN like on FreeBSD. This fixes: * issue #143 * issue #198 * issue #249 * Switch from <cstdatomic> to <atomic>. The former never made it into the standard and doesn't exist in modern gcc versions at all. The later contains everything that leveldb was using from the former. This problem was noticed when porting to Portable Native Client where no memory barrier is defined. The fact that <cstdatomic> is missing normally goes unnoticed since memory barriers are defined for most architectures. * Make Hash() treat its input as unsigned. Before this change LevelDB files from platforms with different signedness of char were not compatible. This change fixes: issue #243 * Verify checksums of index/meta/filter blocks when paranoid_checks set. * Invoke all tools for iOS with xcrun. (This was causing problems with the new XCode 5.1.1 image on pulse.) * include <sys/stat.h> only once, and fix the following linter warning: "Found C system header after C++ system header" * When encountering a corrupted table file, return Status::Corruption instead of Status::InvalidArgument. * Support cygwin as build platform, patch is from https://code.google.com/p/leveldb/issues/detail?id=188 * Fix typo, merge patch from https://code.google.com/p/leveldb/issues/detail?id=159 * Fix typos and comments, and address the following two issues: * issue #166 * issue #241 * Add missing db synchronize after "fillseq" in the benchmark. * Removed unused variable in SeekRandom: value (issue #201)
há 10 anos
  1. // Copyright (c) 2012 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 <stdio.h>
  5. #include "db/dbformat.h"
  6. #include "db/filename.h"
  7. #include "db/log_reader.h"
  8. #include "db/version_edit.h"
  9. #include "db/write_batch_internal.h"
  10. #include "leveldb/env.h"
  11. #include "leveldb/iterator.h"
  12. #include "leveldb/options.h"
  13. #include "leveldb/status.h"
  14. #include "leveldb/table.h"
  15. #include "leveldb/write_batch.h"
  16. #include "util/logging.h"
  17. namespace leveldb {
  18. namespace {
  19. bool GuessType(const std::string& fname, FileType* type) {
  20. size_t pos = fname.rfind('/');
  21. std::string basename;
  22. if (pos == std::string::npos) {
  23. basename = fname;
  24. } else {
  25. basename = std::string(fname.data() + pos + 1, fname.size() - pos - 1);
  26. }
  27. uint64_t ignored;
  28. return ParseFileName(basename, &ignored, type);
  29. }
  30. // Notified when log reader encounters corruption.
  31. class CorruptionReporter : public log::Reader::Reporter {
  32. public:
  33. WritableFile* dst_;
  34. virtual void Corruption(size_t bytes, const Status& status) {
  35. std::string r = "corruption: ";
  36. AppendNumberTo(&r, bytes);
  37. r += " bytes; ";
  38. r += status.ToString();
  39. r.push_back('\n');
  40. dst_->Append(r);
  41. }
  42. };
  43. // Print contents of a log file. (*func)() is called on every record.
  44. Status PrintLogContents(Env* env, const std::string& fname,
  45. void (*func)(uint64_t, Slice, WritableFile*),
  46. WritableFile* dst) {
  47. SequentialFile* file;
  48. Status s = env->NewSequentialFile(fname, &file);
  49. if (!s.ok()) {
  50. return s;
  51. }
  52. CorruptionReporter reporter;
  53. reporter.dst_ = dst;
  54. log::Reader reader(file, &reporter, true, 0);
  55. Slice record;
  56. std::string scratch;
  57. while (reader.ReadRecord(&record, &scratch)) {
  58. (*func)(reader.LastRecordOffset(), record, dst);
  59. }
  60. delete file;
  61. return Status::OK();
  62. }
  63. // Called on every item found in a WriteBatch.
  64. class WriteBatchItemPrinter : public WriteBatch::Handler {
  65. public:
  66. WritableFile* dst_;
  67. virtual void Put(const Slice& key, const Slice& value) {
  68. std::string r = " put '";
  69. AppendEscapedStringTo(&r, key);
  70. r += "' '";
  71. AppendEscapedStringTo(&r, value);
  72. r += "'\n";
  73. dst_->Append(r);
  74. }
  75. virtual void Delete(const Slice& key) {
  76. std::string r = " del '";
  77. AppendEscapedStringTo(&r, key);
  78. r += "'\n";
  79. dst_->Append(r);
  80. }
  81. };
  82. // Called on every log record (each one of which is a WriteBatch)
  83. // found in a kLogFile.
  84. static void WriteBatchPrinter(uint64_t pos, Slice record, WritableFile* dst) {
  85. std::string r = "--- offset ";
  86. AppendNumberTo(&r, pos);
  87. r += "; ";
  88. if (record.size() < 12) {
  89. r += "log record length ";
  90. AppendNumberTo(&r, record.size());
  91. r += " is too small\n";
  92. dst->Append(r);
  93. return;
  94. }
  95. WriteBatch batch;
  96. WriteBatchInternal::SetContents(&batch, record);
  97. r += "sequence ";
  98. AppendNumberTo(&r, WriteBatchInternal::Sequence(&batch));
  99. r.push_back('\n');
  100. dst->Append(r);
  101. WriteBatchItemPrinter batch_item_printer;
  102. batch_item_printer.dst_ = dst;
  103. Status s = batch.Iterate(&batch_item_printer);
  104. if (!s.ok()) {
  105. dst->Append(" error: " + s.ToString() + "\n");
  106. }
  107. }
  108. Status DumpLog(Env* env, const std::string& fname, WritableFile* dst) {
  109. return PrintLogContents(env, fname, WriteBatchPrinter, dst);
  110. }
  111. // Called on every log record (each one of which is a WriteBatch)
  112. // found in a kDescriptorFile.
  113. static void VersionEditPrinter(uint64_t pos, Slice record, WritableFile* dst) {
  114. std::string r = "--- offset ";
  115. AppendNumberTo(&r, pos);
  116. r += "; ";
  117. VersionEdit edit;
  118. Status s = edit.DecodeFrom(record);
  119. if (!s.ok()) {
  120. r += s.ToString();
  121. r.push_back('\n');
  122. } else {
  123. r += edit.DebugString();
  124. }
  125. dst->Append(r);
  126. }
  127. Status DumpDescriptor(Env* env, const std::string& fname, WritableFile* dst) {
  128. return PrintLogContents(env, fname, VersionEditPrinter, dst);
  129. }
  130. Status DumpTable(Env* env, const std::string& fname, WritableFile* dst) {
  131. uint64_t file_size;
  132. RandomAccessFile* file = NULL;
  133. Table* table = NULL;
  134. Status s = env->GetFileSize(fname, &file_size);
  135. if (s.ok()) {
  136. s = env->NewRandomAccessFile(fname, &file);
  137. }
  138. if (s.ok()) {
  139. // We use the default comparator, which may or may not match the
  140. // comparator used in this database. However this should not cause
  141. // problems since we only use Table operations that do not require
  142. // any comparisons. In particular, we do not call Seek or Prev.
  143. s = Table::Open(Options(), file, file_size, &table);
  144. }
  145. if (!s.ok()) {
  146. delete table;
  147. delete file;
  148. return s;
  149. }
  150. ReadOptions ro;
  151. ro.fill_cache = false;
  152. Iterator* iter = table->NewIterator(ro);
  153. std::string r;
  154. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  155. r.clear();
  156. ParsedInternalKey key;
  157. if (!ParseInternalKey(iter->key(), &key)) {
  158. r = "badkey '";
  159. AppendEscapedStringTo(&r, iter->key());
  160. r += "' => '";
  161. AppendEscapedStringTo(&r, iter->value());
  162. r += "'\n";
  163. dst->Append(r);
  164. } else {
  165. r = "'";
  166. AppendEscapedStringTo(&r, key.user_key);
  167. r += "' @ ";
  168. AppendNumberTo(&r, key.sequence);
  169. r += " : ";
  170. if (key.type == kTypeDeletion) {
  171. r += "del";
  172. } else if (key.type == kTypeValue) {
  173. r += "val";
  174. } else {
  175. AppendNumberTo(&r, key.type);
  176. }
  177. r += " => '";
  178. AppendEscapedStringTo(&r, iter->value());
  179. r += "'\n";
  180. dst->Append(r);
  181. }
  182. }
  183. s = iter->status();
  184. if (!s.ok()) {
  185. dst->Append("iterator error: " + s.ToString() + "\n");
  186. }
  187. delete iter;
  188. delete table;
  189. delete file;
  190. return Status::OK();
  191. }
  192. } // namespace
  193. Status DumpFile(Env* env, const std::string& fname, WritableFile* dst) {
  194. FileType ftype;
  195. if (!GuessType(fname, &ftype)) {
  196. return Status::InvalidArgument(fname + ": unknown file type");
  197. }
  198. switch (ftype) {
  199. case kLogFile: return DumpLog(env, fname, dst);
  200. case kDescriptorFile: return DumpDescriptor(env, fname, dst);
  201. case kTableFile: return DumpTable(env, fname, dst);
  202. default:
  203. break;
  204. }
  205. return Status::InvalidArgument(fname + ": not a dump-able file type");
  206. }
  207. } // namespace leveldb