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

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