小组成员: 曹可心-10223903406 朴祉燕-10224602413
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.

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