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

266 lines
7.8 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)
преди 10 години
  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. //
  5. // Decodes the blocks generated by block_builder.cc.
  6. #include "table/block.h"
  7. #include <algorithm>
  8. #include <vector>
  9. #include "leveldb/comparator.h"
  10. #include "table/format.h"
  11. #include "util/coding.h"
  12. #include "util/logging.h"
  13. namespace leveldb {
  14. inline uint32_t Block::NumRestarts() const {
  15. assert(size_ >= sizeof(uint32_t));
  16. return DecodeFixed32(data_ + size_ - sizeof(uint32_t));
  17. }
  18. Block::Block(const BlockContents& contents)
  19. : data_(contents.data.data()),
  20. size_(contents.data.size()),
  21. owned_(contents.heap_allocated) {
  22. if (size_ < sizeof(uint32_t)) {
  23. size_ = 0; // Error marker
  24. } else {
  25. size_t max_restarts_allowed = (size_ - sizeof(uint32_t)) / sizeof(uint32_t);
  26. if (NumRestarts() > max_restarts_allowed) {
  27. // The size is too small for NumRestarts()
  28. size_ = 0;
  29. } else {
  30. restart_offset_ = size_ - (1 + NumRestarts()) * sizeof(uint32_t);
  31. }
  32. }
  33. }
  34. Block::~Block() {
  35. if (owned_) {
  36. delete[] data_;
  37. }
  38. }
  39. // Helper routine: decode the next block entry starting at "p",
  40. // storing the number of shared key bytes, non_shared key bytes,
  41. // and the length of the value in "*shared", "*non_shared", and
  42. // "*value_length", respectively. Will not dereference past "limit".
  43. //
  44. // If any errors are detected, returns nullptr. Otherwise, returns a
  45. // pointer to the key delta (just past the three decoded values).
  46. static inline const char* DecodeEntry(const char* p, const char* limit,
  47. uint32_t* shared, uint32_t* non_shared,
  48. uint32_t* value_length) {
  49. if (limit - p < 3) return nullptr;
  50. *shared = reinterpret_cast<const unsigned char*>(p)[0];
  51. *non_shared = reinterpret_cast<const unsigned char*>(p)[1];
  52. *value_length = reinterpret_cast<const unsigned char*>(p)[2];
  53. if ((*shared | *non_shared | *value_length) < 128) {
  54. // Fast path: all three values are encoded in one byte each
  55. p += 3;
  56. } else {
  57. if ((p = GetVarint32Ptr(p, limit, shared)) == nullptr) return nullptr;
  58. if ((p = GetVarint32Ptr(p, limit, non_shared)) == nullptr) return nullptr;
  59. if ((p = GetVarint32Ptr(p, limit, value_length)) == nullptr) return nullptr;
  60. }
  61. if (static_cast<uint32_t>(limit - p) < (*non_shared + *value_length)) {
  62. return nullptr;
  63. }
  64. return p;
  65. }
  66. class Block::Iter : public Iterator {
  67. private:
  68. const Comparator* const comparator_;
  69. const char* const data_; // underlying block contents
  70. uint32_t const restarts_; // Offset of restart array (list of fixed32)
  71. uint32_t const num_restarts_; // Number of uint32_t entries in restart array
  72. // current_ is offset in data_ of current entry. >= restarts_ if !Valid
  73. uint32_t current_;
  74. uint32_t restart_index_; // Index of restart block in which current_ falls
  75. std::string key_;
  76. Slice value_;
  77. Status status_;
  78. inline int Compare(const Slice& a, const Slice& b) const {
  79. return comparator_->Compare(a, b);
  80. }
  81. // Return the offset in data_ just past the end of the current entry.
  82. inline uint32_t NextEntryOffset() const {
  83. return (value_.data() + value_.size()) - data_;
  84. }
  85. uint32_t GetRestartPoint(uint32_t index) {
  86. assert(index < num_restarts_);
  87. return DecodeFixed32(data_ + restarts_ + index * sizeof(uint32_t));
  88. }
  89. void SeekToRestartPoint(uint32_t index) {
  90. key_.clear();
  91. restart_index_ = index;
  92. // current_ will be fixed by ParseNextKey();
  93. // ParseNextKey() starts at the end of value_, so set value_ accordingly
  94. uint32_t offset = GetRestartPoint(index);
  95. value_ = Slice(data_ + offset, 0);
  96. }
  97. public:
  98. Iter(const Comparator* comparator, const char* data, uint32_t restarts,
  99. uint32_t num_restarts)
  100. : comparator_(comparator),
  101. data_(data),
  102. restarts_(restarts),
  103. num_restarts_(num_restarts),
  104. current_(restarts_),
  105. restart_index_(num_restarts_) {
  106. assert(num_restarts_ > 0);
  107. }
  108. virtual bool Valid() const { return current_ < restarts_; }
  109. virtual Status status() const { return status_; }
  110. virtual Slice key() const {
  111. assert(Valid());
  112. return key_;
  113. }
  114. virtual Slice value() const {
  115. assert(Valid());
  116. return value_;
  117. }
  118. virtual void Next() {
  119. assert(Valid());
  120. ParseNextKey();
  121. }
  122. virtual void Prev() {
  123. assert(Valid());
  124. // Scan backwards to a restart point before current_
  125. const uint32_t original = current_;
  126. while (GetRestartPoint(restart_index_) >= original) {
  127. if (restart_index_ == 0) {
  128. // No more entries
  129. current_ = restarts_;
  130. restart_index_ = num_restarts_;
  131. return;
  132. }
  133. restart_index_--;
  134. }
  135. SeekToRestartPoint(restart_index_);
  136. do {
  137. // Loop until end of current entry hits the start of original entry
  138. } while (ParseNextKey() && NextEntryOffset() < original);
  139. }
  140. virtual void Seek(const Slice& target) {
  141. // Binary search in restart array to find the last restart point
  142. // with a key < target
  143. uint32_t left = 0;
  144. uint32_t right = num_restarts_ - 1;
  145. while (left < right) {
  146. uint32_t mid = (left + right + 1) / 2;
  147. uint32_t region_offset = GetRestartPoint(mid);
  148. uint32_t shared, non_shared, value_length;
  149. const char* key_ptr =
  150. DecodeEntry(data_ + region_offset, data_ + restarts_, &shared,
  151. &non_shared, &value_length);
  152. if (key_ptr == nullptr || (shared != 0)) {
  153. CorruptionError();
  154. return;
  155. }
  156. Slice mid_key(key_ptr, non_shared);
  157. if (Compare(mid_key, target) < 0) {
  158. // Key at "mid" is smaller than "target". Therefore all
  159. // blocks before "mid" are uninteresting.
  160. left = mid;
  161. } else {
  162. // Key at "mid" is >= "target". Therefore all blocks at or
  163. // after "mid" are uninteresting.
  164. right = mid - 1;
  165. }
  166. }
  167. // Linear search (within restart block) for first key >= target
  168. SeekToRestartPoint(left);
  169. while (true) {
  170. if (!ParseNextKey()) {
  171. return;
  172. }
  173. if (Compare(key_, target) >= 0) {
  174. return;
  175. }
  176. }
  177. }
  178. virtual void SeekToFirst() {
  179. SeekToRestartPoint(0);
  180. ParseNextKey();
  181. }
  182. virtual void SeekToLast() {
  183. SeekToRestartPoint(num_restarts_ - 1);
  184. while (ParseNextKey() && NextEntryOffset() < restarts_) {
  185. // Keep skipping
  186. }
  187. }
  188. private:
  189. void CorruptionError() {
  190. current_ = restarts_;
  191. restart_index_ = num_restarts_;
  192. status_ = Status::Corruption("bad entry in block");
  193. key_.clear();
  194. value_.clear();
  195. }
  196. bool ParseNextKey() {
  197. current_ = NextEntryOffset();
  198. const char* p = data_ + current_;
  199. const char* limit = data_ + restarts_; // Restarts come right after data
  200. if (p >= limit) {
  201. // No more entries to return. Mark as invalid.
  202. current_ = restarts_;
  203. restart_index_ = num_restarts_;
  204. return false;
  205. }
  206. // Decode next entry
  207. uint32_t shared, non_shared, value_length;
  208. p = DecodeEntry(p, limit, &shared, &non_shared, &value_length);
  209. if (p == nullptr || key_.size() < shared) {
  210. CorruptionError();
  211. return false;
  212. } else {
  213. key_.resize(shared);
  214. key_.append(p, non_shared);
  215. value_ = Slice(p + non_shared, value_length);
  216. while (restart_index_ + 1 < num_restarts_ &&
  217. GetRestartPoint(restart_index_ + 1) < current_) {
  218. ++restart_index_;
  219. }
  220. return true;
  221. }
  222. }
  223. };
  224. Iterator* Block::NewIterator(const Comparator* comparator) {
  225. if (size_ < sizeof(uint32_t)) {
  226. return NewErrorIterator(Status::Corruption("bad block contents"));
  227. }
  228. const uint32_t num_restarts = NumRestarts();
  229. if (num_restarts == 0) {
  230. return NewEmptyIterator();
  231. } else {
  232. return new Iter(comparator, data_, restart_offset_, num_restarts);
  233. }
  234. }
  235. } // namespace leveldb