10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
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.

298 lines
7.8 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. SaveKey(ExtractUserKey(iter_->key()), &saved_key_);
  196. do {
  197. ParsedInternalKey ikey;
  198. if (ParseKey(&ikey) && ikey.sequence <= sequence_) {
  199. if ((value_type != kTypeDeletion) &&
  200. user_comparator_->Compare(ikey.user_key, saved_key_) < 0) {
  201. // We encountered a non-deleted value in entries for previous keys,
  202. break;
  203. }
  204. value_type = ikey.type;
  205. if (value_type == kTypeDeletion) {
  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. saved_value_.assign(raw_value.data(), raw_value.size());
  214. }
  215. }
  216. iter_->Prev();
  217. } while (iter_->Valid());
  218. }
  219. if (value_type == kTypeDeletion) {
  220. // End
  221. valid_ = false;
  222. saved_key_.clear();
  223. ClearSavedValue();
  224. direction_ = kForward;
  225. } else {
  226. valid_ = true;
  227. }
  228. }
  229. void DBIter::Seek(const Slice& target) {
  230. direction_ = kForward;
  231. ClearSavedValue();
  232. saved_key_.clear();
  233. AppendInternalKey(
  234. &saved_key_, ParsedInternalKey(target, sequence_, kValueTypeForSeek));
  235. iter_->Seek(saved_key_);
  236. if (iter_->Valid()) {
  237. FindNextUserEntry(false, &saved_key_ /* temporary storage */);
  238. } else {
  239. valid_ = false;
  240. }
  241. }
  242. void DBIter::SeekToFirst() {
  243. direction_ = kForward;
  244. ClearSavedValue();
  245. iter_->SeekToFirst();
  246. if (iter_->Valid()) {
  247. FindNextUserEntry(false, &saved_key_ /* temporary storage */);
  248. } else {
  249. valid_ = false;
  250. }
  251. }
  252. void DBIter::SeekToLast() {
  253. direction_ = kReverse;
  254. ClearSavedValue();
  255. iter_->SeekToLast();
  256. FindPrevUserEntry();
  257. }
  258. } // anonymous namespace
  259. Iterator* NewDBIterator(
  260. const std::string* dbname,
  261. Env* env,
  262. const Comparator* user_key_comparator,
  263. Iterator* internal_iter,
  264. const SequenceNumber& sequence) {
  265. return new DBIter(dbname, env, user_key_comparator, internal_iter, sequence);
  266. }
  267. }