作者: 韩晨旭 10225101440 李畅 10225102463
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.

317 regels
8.3 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. #include "db/db_iter.h"
  5. #include "db/filename.h"
  6. #include "db/db_impl.h"
  7. #include "db/dbformat.h"
  8. #include "leveldb/env.h"
  9. #include "leveldb/iterator.h"
  10. #include "port/port.h"
  11. #include "util/logging.h"
  12. #include "util/mutexlock.h"
  13. #include "util/random.h"
  14. namespace leveldb {
  15. #if 0
  16. static void DumpInternalIter(Iterator* iter) {
  17. for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
  18. ParsedInternalKey k;
  19. if (!ParseInternalKey(iter->key(), &k)) {
  20. fprintf(stderr, "Corrupt '%s'\n", EscapeString(iter->key()).c_str());
  21. } else {
  22. fprintf(stderr, "@ '%s'\n", k.DebugString().c_str());
  23. }
  24. }
  25. }
  26. #endif
  27. namespace {
  28. // Memtables and sstables that make the DB representation contain
  29. // (userkey,seq,type) => uservalue entries. DBIter
  30. // combines multiple entries for the same userkey found in the DB
  31. // representation into a single entry while accounting for sequence
  32. // numbers, deletion markers, overwrites, etc.
  33. class DBIter: public Iterator {
  34. public:
  35. // Which direction is the iterator currently moving?
  36. // (1) When moving forward, the internal iterator is positioned at
  37. // the exact entry that yields this->key(), this->value()
  38. // (2) When moving backwards, the internal iterator is positioned
  39. // just before all entries whose user key == this->key().
  40. enum Direction {
  41. kForward,
  42. kReverse
  43. };
  44. DBIter(DBImpl* db, const Comparator* cmp, Iterator* iter, SequenceNumber s,
  45. uint32_t seed)
  46. : db_(db),
  47. user_comparator_(cmp),
  48. iter_(iter),
  49. sequence_(s),
  50. direction_(kForward),
  51. valid_(false),
  52. rnd_(seed),
  53. bytes_counter_(RandomPeriod()) {
  54. }
  55. virtual ~DBIter() {
  56. delete iter_;
  57. }
  58. virtual bool Valid() const { return valid_; }
  59. virtual Slice key() const {
  60. assert(valid_);
  61. return (direction_ == kForward) ? ExtractUserKey(iter_->key()) : saved_key_;
  62. }
  63. virtual Slice value() const {
  64. assert(valid_);
  65. return (direction_ == kForward) ? iter_->value() : saved_value_;
  66. }
  67. virtual Status status() const {
  68. if (status_.ok()) {
  69. return iter_->status();
  70. } else {
  71. return status_;
  72. }
  73. }
  74. virtual void Next();
  75. virtual void Prev();
  76. virtual void Seek(const Slice& target);
  77. virtual void SeekToFirst();
  78. virtual void SeekToLast();
  79. private:
  80. void FindNextUserEntry(bool skipping, std::string* skip);
  81. void FindPrevUserEntry();
  82. bool ParseKey(ParsedInternalKey* key);
  83. inline void SaveKey(const Slice& k, std::string* dst) {
  84. dst->assign(k.data(), k.size());
  85. }
  86. inline void ClearSavedValue() {
  87. if (saved_value_.capacity() > 1048576) {
  88. std::string empty;
  89. swap(empty, saved_value_);
  90. } else {
  91. saved_value_.clear();
  92. }
  93. }
  94. // Pick next gap with average value of config::kReadBytesPeriod.
  95. ssize_t RandomPeriod() {
  96. return rnd_.Uniform(2*config::kReadBytesPeriod);
  97. }
  98. DBImpl* db_;
  99. const Comparator* const user_comparator_;
  100. Iterator* const iter_;
  101. SequenceNumber const sequence_;
  102. Status status_;
  103. std::string saved_key_; // == current key when direction_==kReverse
  104. std::string saved_value_; // == current raw value when direction_==kReverse
  105. Direction direction_;
  106. bool valid_;
  107. Random rnd_;
  108. ssize_t bytes_counter_;
  109. // No copying allowed
  110. DBIter(const DBIter&);
  111. void operator=(const DBIter&);
  112. };
  113. inline bool DBIter::ParseKey(ParsedInternalKey* ikey) {
  114. Slice k = iter_->key();
  115. ssize_t n = k.size() + iter_->value().size();
  116. bytes_counter_ -= n;
  117. while (bytes_counter_ < 0) {
  118. bytes_counter_ += RandomPeriod();
  119. db_->RecordReadSample(k);
  120. }
  121. if (!ParseInternalKey(k, ikey)) {
  122. status_ = Status::Corruption("corrupted internal key in DBIter");
  123. return false;
  124. } else {
  125. return true;
  126. }
  127. }
  128. void DBIter::Next() {
  129. assert(valid_);
  130. if (direction_ == kReverse) { // Switch directions?
  131. direction_ = kForward;
  132. // iter_ is pointing just before the entries for this->key(),
  133. // so advance into the range of entries for this->key() and then
  134. // use the normal skipping code below.
  135. if (!iter_->Valid()) {
  136. iter_->SeekToFirst();
  137. } else {
  138. iter_->Next();
  139. }
  140. if (!iter_->Valid()) {
  141. valid_ = false;
  142. saved_key_.clear();
  143. return;
  144. }
  145. // saved_key_ already contains the key to skip past.
  146. } else {
  147. // Store in saved_key_ the current key so we skip it below.
  148. SaveKey(ExtractUserKey(iter_->key()), &saved_key_);
  149. }
  150. FindNextUserEntry(true, &saved_key_);
  151. }
  152. void DBIter::FindNextUserEntry(bool skipping, std::string* skip) {
  153. // Loop until we hit an acceptable entry to yield
  154. assert(iter_->Valid());
  155. assert(direction_ == kForward);
  156. do {
  157. ParsedInternalKey ikey;
  158. if (ParseKey(&ikey) && ikey.sequence <= sequence_) {
  159. switch (ikey.type) {
  160. case kTypeDeletion:
  161. // Arrange to skip all upcoming entries for this key since
  162. // they are hidden by this deletion.
  163. SaveKey(ikey.user_key, skip);
  164. skipping = true;
  165. break;
  166. case kTypeValue:
  167. if (skipping &&
  168. user_comparator_->Compare(ikey.user_key, *skip) <= 0) {
  169. // Entry hidden
  170. } else {
  171. valid_ = true;
  172. saved_key_.clear();
  173. return;
  174. }
  175. break;
  176. }
  177. }
  178. iter_->Next();
  179. } while (iter_->Valid());
  180. saved_key_.clear();
  181. valid_ = false;
  182. }
  183. void DBIter::Prev() {
  184. assert(valid_);
  185. if (direction_ == kForward) { // Switch directions?
  186. // iter_ is pointing at the current entry. Scan backwards until
  187. // the key changes so we can use the normal reverse scanning code.
  188. assert(iter_->Valid()); // Otherwise valid_ would have been false
  189. SaveKey(ExtractUserKey(iter_->key()), &saved_key_);
  190. while (true) {
  191. iter_->Prev();
  192. if (!iter_->Valid()) {
  193. valid_ = false;
  194. saved_key_.clear();
  195. ClearSavedValue();
  196. return;
  197. }
  198. if (user_comparator_->Compare(ExtractUserKey(iter_->key()),
  199. saved_key_) < 0) {
  200. break;
  201. }
  202. }
  203. direction_ = kReverse;
  204. }
  205. FindPrevUserEntry();
  206. }
  207. void DBIter::FindPrevUserEntry() {
  208. assert(direction_ == kReverse);
  209. ValueType value_type = kTypeDeletion;
  210. if (iter_->Valid()) {
  211. do {
  212. ParsedInternalKey ikey;
  213. if (ParseKey(&ikey) && ikey.sequence <= sequence_) {
  214. if ((value_type != kTypeDeletion) &&
  215. user_comparator_->Compare(ikey.user_key, saved_key_) < 0) {
  216. // We encountered a non-deleted value in entries for previous keys,
  217. break;
  218. }
  219. value_type = ikey.type;
  220. if (value_type == kTypeDeletion) {
  221. saved_key_.clear();
  222. ClearSavedValue();
  223. } else {
  224. Slice raw_value = iter_->value();
  225. if (saved_value_.capacity() > raw_value.size() + 1048576) {
  226. std::string empty;
  227. swap(empty, saved_value_);
  228. }
  229. SaveKey(ExtractUserKey(iter_->key()), &saved_key_);
  230. saved_value_.assign(raw_value.data(), raw_value.size());
  231. }
  232. }
  233. iter_->Prev();
  234. } while (iter_->Valid());
  235. }
  236. if (value_type == kTypeDeletion) {
  237. // End
  238. valid_ = false;
  239. saved_key_.clear();
  240. ClearSavedValue();
  241. direction_ = kForward;
  242. } else {
  243. valid_ = true;
  244. }
  245. }
  246. void DBIter::Seek(const Slice& target) {
  247. direction_ = kForward;
  248. ClearSavedValue();
  249. saved_key_.clear();
  250. AppendInternalKey(
  251. &saved_key_, ParsedInternalKey(target, sequence_, kValueTypeForSeek));
  252. iter_->Seek(saved_key_);
  253. if (iter_->Valid()) {
  254. FindNextUserEntry(false, &saved_key_ /* temporary storage */);
  255. } else {
  256. valid_ = false;
  257. }
  258. }
  259. void DBIter::SeekToFirst() {
  260. direction_ = kForward;
  261. ClearSavedValue();
  262. iter_->SeekToFirst();
  263. if (iter_->Valid()) {
  264. FindNextUserEntry(false, &saved_key_ /* temporary storage */);
  265. } else {
  266. valid_ = false;
  267. }
  268. }
  269. void DBIter::SeekToLast() {
  270. direction_ = kReverse;
  271. ClearSavedValue();
  272. iter_->SeekToLast();
  273. FindPrevUserEntry();
  274. }
  275. } // anonymous namespace
  276. Iterator* NewDBIterator(
  277. DBImpl* db,
  278. const Comparator* user_key_comparator,
  279. Iterator* internal_iter,
  280. SequenceNumber sequence,
  281. uint32_t seed) {
  282. return new DBIter(db, user_key_comparator, internal_iter, sequence, seed);
  283. }
  284. } // namespace leveldb