10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

304 Zeilen
9.5 KiB

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