小组成员:陈予曈,朱陈媛
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.

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