db_iter.cc 54.2 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
5
//
J
jorlow@chromium.org 已提交
6 7 8 9 10
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "db/db_iter.h"
S
Stanislau Hlebik 已提交
11
#include <string>
12
#include <iostream>
S
Stanislau Hlebik 已提交
13
#include <limits>
J
jorlow@chromium.org 已提交
14 15

#include "db/dbformat.h"
16
#include "db/merge_context.h"
17
#include "db/merge_helper.h"
18
#include "db/pinned_iterators_manager.h"
19
#include "monitoring/perf_context_imp.h"
20 21 22
#include "rocksdb/env.h"
#include "rocksdb/iterator.h"
#include "rocksdb/merge_operator.h"
23
#include "rocksdb/options.h"
S
sdong 已提交
24
#include "table/internal_iterator.h"
25
#include "util/arena.h"
26
#include "util/filename.h"
J
jorlow@chromium.org 已提交
27 28
#include "util/logging.h"
#include "util/mutexlock.h"
29
#include "util/string_util.h"
30
#include "util/trace_replay.h"
J
jorlow@chromium.org 已提交
31

32
namespace rocksdb {
J
jorlow@chromium.org 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

#if 0
static void DumpInternalIter(Iterator* iter) {
  for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
    ParsedInternalKey k;
    if (!ParseInternalKey(iter->key(), &k)) {
      fprintf(stderr, "Corrupt '%s'\n", EscapeString(iter->key()).c_str());
    } else {
      fprintf(stderr, "@ '%s'\n", k.DebugString().c_str());
    }
  }
}
#endif

// Memtables and sstables that make the DB representation contain
// (userkey,seq,type) => uservalue entries.  DBIter
// combines multiple entries for the same userkey found in the DB
// representation into a single entry while accounting for sequence
// numbers, deletion markers, overwrites, etc.
S
Siying Dong 已提交
52
class DBIter final: public Iterator {
J
jorlow@chromium.org 已提交
53
 public:
54
  // The following is grossly complicated. TODO: clean it up
J
jorlow@chromium.org 已提交
55
  // Which direction is the iterator currently moving?
56 57 58 59 60 61 62
  // (1) When moving forward:
  //   (1a) if current_entry_is_merged_ = false, the internal iterator is
  //        positioned at the exact entry that yields this->key(), this->value()
  //   (1b) if current_entry_is_merged_ = true, the internal iterator is
  //        positioned immediately after the last entry that contributed to the
  //        current this->value(). That entry may or may not have key equal to
  //        this->key().
J
jorlow@chromium.org 已提交
63 64 65 66 67 68 69
  // (2) When moving backwards, the internal iterator is positioned
  //     just before all entries whose user key == this->key().
  enum Direction {
    kForward,
    kReverse
  };

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  // LocalStatistics contain Statistics counters that will be aggregated per
  // each iterator instance and then will be sent to the global statistics when
  // the iterator is destroyed.
  //
  // The purpose of this approach is to avoid perf regression happening
  // when multiple threads bump the atomic counters from a DBIter::Next().
  struct LocalStatistics {
    explicit LocalStatistics() { ResetCounters(); }

    void ResetCounters() {
      next_count_ = 0;
      next_found_count_ = 0;
      prev_count_ = 0;
      prev_found_count_ = 0;
      bytes_read_ = 0;
85
      skip_count_ = 0;
86 87 88 89 90 91 92 93
    }

    void BumpGlobalStatistics(Statistics* global_statistics) {
      RecordTick(global_statistics, NUMBER_DB_NEXT, next_count_);
      RecordTick(global_statistics, NUMBER_DB_NEXT_FOUND, next_found_count_);
      RecordTick(global_statistics, NUMBER_DB_PREV, prev_count_);
      RecordTick(global_statistics, NUMBER_DB_PREV_FOUND, prev_found_count_);
      RecordTick(global_statistics, ITER_BYTES_READ, bytes_read_);
94
      RecordTick(global_statistics, NUMBER_ITER_SKIP, skip_count_);
95
      PERF_COUNTER_ADD(iter_read_bytes, bytes_read_);
96 97 98 99 100 101 102 103 104 105 106 107 108
      ResetCounters();
    }

    // Map to Tickers::NUMBER_DB_NEXT
    uint64_t next_count_;
    // Map to Tickers::NUMBER_DB_NEXT_FOUND
    uint64_t next_found_count_;
    // Map to Tickers::NUMBER_DB_PREV
    uint64_t prev_count_;
    // Map to Tickers::NUMBER_DB_PREV_FOUND
    uint64_t prev_found_count_;
    // Map to Tickers::ITER_BYTES_READ
    uint64_t bytes_read_;
109 110
    // Map to Tickers::NUMBER_ITER_SKIP
    uint64_t skip_count_;
111 112
  };

S
Siying Dong 已提交
113
  DBIter(Env* _env, const ReadOptions& read_options,
114 115
         const ImmutableCFOptions& cf_options,
         const MutableCFOptions& mutable_cf_options, const Comparator* cmp,
S
sdong 已提交
116
         InternalIterator* iter, SequenceNumber s, bool arena_mode,
Y
Yi Wu 已提交
117
         uint64_t max_sequential_skip_in_iterations,
118 119
         ReadCallback* read_callback, DBImpl* db_impl, ColumnFamilyData* cfd,
         bool allow_blob)
120
      : arena_mode_(arena_mode),
S
Siying Dong 已提交
121
        env_(_env),
122
        logger_(cf_options.info_log),
J
jorlow@chromium.org 已提交
123
        user_comparator_(cmp),
124
        merge_operator_(cf_options.merge_operator),
J
jorlow@chromium.org 已提交
125 126
        iter_(iter),
        sequence_(s),
J
jorlow@chromium.org 已提交
127
        direction_(kForward),
128
        valid_(false),
129
        current_entry_is_merged_(false),
130
        statistics_(cf_options.statistics),
131
        num_internal_keys_skipped_(0),
132
        iterate_lower_bound_(read_options.iterate_lower_bound),
133 134 135 136 137
        iterate_upper_bound_(read_options.iterate_upper_bound),
        prefix_same_as_start_(read_options.prefix_same_as_start),
        pin_thru_lifetime_(read_options.pin_data),
        total_order_seek_(read_options.total_order_seek),
        range_del_agg_(cf_options.internal_comparator, s,
Y
Yi Wu 已提交
138
                       true /* collapse_deletions */),
Y
Yi Wu 已提交
139
        read_callback_(read_callback),
140 141
        db_impl_(db_impl),
        cfd_(cfd),
142
        allow_blob_(allow_blob),
143 144
        is_blob_(false),
        start_seqnum_(read_options.iter_start_seqnum) {
L
Lei Jin 已提交
145
    RecordTick(statistics_, NO_ITERATORS);
146
    prefix_extractor_ = mutable_cf_options.prefix_extractor.get();
147
    max_skip_ = max_sequential_skip_in_iterations;
148
    max_skippable_internal_keys_ = read_options.max_skippable_internal_keys;
149 150 151 152 153 154
    if (pin_thru_lifetime_) {
      pinned_iters_mgr_.StartPinning();
    }
    if (iter_) {
      iter_->SetPinnedItersMgr(&pinned_iters_mgr_);
    }
J
jorlow@chromium.org 已提交
155 156
  }
  virtual ~DBIter() {
157
    // Release pinned data if any
158 159 160
    if (pinned_iters_mgr_.PinningEnabled()) {
      pinned_iters_mgr_.ReleasePinnedData();
    }
161 162 163
    // Compiler warning issue filed:
    // https://github.com/facebook/rocksdb/issues/3013
    RecordTick(statistics_, NO_ITERATORS, uint64_t(-1));
164
    ResetInternalKeysSkippedCounter();
165
    local_stats_.BumpGlobalStatistics(statistics_);
166 167 168
    if (!arena_mode_) {
      delete iter_;
    } else {
S
sdong 已提交
169
      iter_->~InternalIterator();
170 171
    }
  }
S
sdong 已提交
172
  virtual void SetIter(InternalIterator* iter) {
173 174
    assert(iter_ == nullptr);
    iter_ = iter;
175
    iter_->SetPinnedItersMgr(&pinned_iters_mgr_);
J
jorlow@chromium.org 已提交
176
  }
A
Andrew Kryczka 已提交
177 178 179 180
  virtual RangeDelAggregator* GetRangeDelAggregator() {
    return &range_del_agg_;
  }

I
Igor Sugak 已提交
181 182
  virtual bool Valid() const override { return valid_; }
  virtual Slice key() const override {
J
jorlow@chromium.org 已提交
183
    assert(valid_);
184 185 186 187 188 189
    if(start_seqnum_ > 0) {
      return saved_key_.GetInternalKey();
    } else {
      return saved_key_.GetUserKey();
    }

J
jorlow@chromium.org 已提交
190
  }
I
Igor Sugak 已提交
191
  virtual Slice value() const override {
J
jorlow@chromium.org 已提交
192
    assert(valid_);
193
    if (current_entry_is_merged_) {
194 195 196
      // If pinned_value_ is set then the result of merge operator is one of
      // the merge operands and we should return it.
      return pinned_value_.data() ? pinned_value_ : saved_value_;
197 198 199 200 201
    } else if (direction_ == kReverse) {
      return pinned_value_;
    } else {
      return iter_->value();
    }
J
jorlow@chromium.org 已提交
202
  }
I
Igor Sugak 已提交
203
  virtual Status status() const override {
J
jorlow@chromium.org 已提交
204 205 206
    if (status_.ok()) {
      return iter_->status();
    } else {
207
      assert(!valid_);
J
jorlow@chromium.org 已提交
208 209 210
      return status_;
    }
  }
Y
Yi Wu 已提交
211 212 213 214
  bool IsBlob() const {
    assert(valid_ && (allow_blob_ || !is_blob_));
    return is_blob_;
  }
215 216 217 218 219 220

  virtual Status GetProperty(std::string prop_name,
                             std::string* prop) override {
    if (prop == nullptr) {
      return Status::InvalidArgument("prop is nullptr");
    }
221
    if (prop_name == "rocksdb.iterator.super-version-number") {
222
      // First try to pass the value returned from inner iterator.
S
Siying Dong 已提交
223
      return iter_->GetProperty(prop_name, prop);
224
    } else if (prop_name == "rocksdb.iterator.is-key-pinned") {
225
      if (valid_) {
226
        *prop = (pin_thru_lifetime_ && saved_key_.IsKeyPinned()) ? "1" : "0";
227 228 229 230
      } else {
        *prop = "Iterator is not valid.";
      }
      return Status::OK();
231 232 233
    } else if (prop_name == "rocksdb.iterator.internal-key") {
      *prop = saved_key_.GetUserKey().ToString();
      return Status::OK();
234 235
    }
    return Status::InvalidArgument("Undentified property.");
236
  }
J
jorlow@chromium.org 已提交
237

I
Igor Sugak 已提交
238 239 240
  virtual void Next() override;
  virtual void Prev() override;
  virtual void Seek(const Slice& target) override;
A
Aaron Gao 已提交
241
  virtual void SeekForPrev(const Slice& target) override;
I
Igor Sugak 已提交
242 243
  virtual void SeekToFirst() override;
  virtual void SeekToLast() override;
S
Siying Dong 已提交
244 245 246
  Env* env() { return env_; }
  void set_sequence(uint64_t s) { sequence_ = s; }
  void set_valid(bool v) { valid_ = v; }
J
jorlow@chromium.org 已提交
247

J
jorlow@chromium.org 已提交
248
 private:
249 250 251 252 253 254
  // For all methods in this block:
  // PRE: iter_->Valid() && status_.ok()
  // Return false if there was an error, and status() is non-ok, valid_ = false;
  // in this case callers would usually stop what they were doing and return.
  bool ReverseToForward();
  bool ReverseToBackward();
S
Stanislau Hlebik 已提交
255 256
  bool FindValueForCurrentKey();
  bool FindValueForCurrentKeyUsingSeek();
257 258 259
  bool FindUserKeyBeforeSavedKey();
  inline bool FindNextUserEntry(bool skipping, bool prefix_check);
  bool FindNextUserEntryInternal(bool skipping, bool prefix_check);
J
jorlow@chromium.org 已提交
260
  bool ParseKey(ParsedInternalKey* key);
261 262 263
  bool MergeValuesNewToOld();

  void PrevInternal();
264
  bool TooManyInternalKeysSkipped(bool increment = true);
Y
Yi Wu 已提交
265
  bool IsVisible(SequenceNumber sequence);
J
jorlow@chromium.org 已提交
266

267 268 269 270 271 272 273 274 275 276 277 278
  // CanReseekToSkip() returns whether the iterator can use the optimization
  // where it reseek by sequence number to get the next key when there are too
  // many versions. This is disabled for write unprepared because seeking to
  // sequence number does not guarantee that it is visible.
  inline bool CanReseekToSkip();

  // MaxVisibleSequenceNumber() returns the maximum visible sequence number
  // for this snapshot. This sequence number may be greater than snapshot
  // seqno because uncommitted data written to DB for write unprepared will
  // have a higher sequence number.
  inline SequenceNumber MaxVisibleSequenceNumber();

279 280 281 282 283 284 285 286 287 288
  // Temporarily pin the blocks that we encounter until ReleaseTempPinnedData()
  // is called
  void TempPinData() {
    if (!pin_thru_lifetime_) {
      pinned_iters_mgr_.StartPinning();
    }
  }

  // Release blocks pinned by TempPinData()
  void ReleaseTempPinnedData() {
289 290
    if (!pin_thru_lifetime_ && pinned_iters_mgr_.PinningEnabled()) {
      pinned_iters_mgr_.ReleasePinnedData();
291 292 293
    }
  }

J
jorlow@chromium.org 已提交
294 295 296 297 298 299 300 301 302
  inline void ClearSavedValue() {
    if (saved_value_.capacity() > 1048576) {
      std::string empty;
      swap(empty, saved_value_);
    } else {
      saved_value_.clear();
    }
  }

303
  inline void ResetInternalKeysSkippedCounter() {
304 305 306 307
    local_stats_.skip_count_ += num_internal_keys_skipped_;
    if (valid_) {
      local_stats_.skip_count_--;
    }
308 309 310
    num_internal_keys_skipped_ = 0;
  }

311
  const SliceTransform* prefix_extractor_;
312
  bool arena_mode_;
J
jorlow@chromium.org 已提交
313
  Env* const env_;
I
Igor Canadi 已提交
314
  Logger* logger_;
J
jorlow@chromium.org 已提交
315
  const Comparator* const user_comparator_;
316
  const MergeOperator* const merge_operator_;
S
sdong 已提交
317
  InternalIterator* iter_;
S
Siying Dong 已提交
318
  SequenceNumber sequence_;
J
jorlow@chromium.org 已提交
319

J
jorlow@chromium.org 已提交
320
  Status status_;
S
Stanislau Hlebik 已提交
321
  IterKey saved_key_;
322 323 324 325
  // Reusable internal key data structure. This is only used inside one function
  // and should not be used across functions. Reusing this object can reduce
  // overhead of calling construction of the function if creating it each time.
  ParsedInternalKey ikey_;
S
Stanislau Hlebik 已提交
326
  std::string saved_value_;
327
  Slice pinned_value_;
J
jorlow@chromium.org 已提交
328
  Direction direction_;
J
jorlow@chromium.org 已提交
329
  bool valid_;
330
  bool current_entry_is_merged_;
331
  // for prefix seek mode to support prev()
332
  Statistics* statistics_;
333
  uint64_t max_skip_;
334 335
  uint64_t max_skippable_internal_keys_;
  uint64_t num_internal_keys_skipped_;
336
  const Slice* iterate_lower_bound_;
337
  const Slice* iterate_upper_bound_;
338 339 340
  IterKey prefix_start_buf_;
  Slice prefix_start_key_;
  const bool prefix_same_as_start_;
341 342 343
  // Means that we will pin all data blocks we read as long the Iterator
  // is not deleted, will be true if ReadOptions::pin_data is true
  const bool pin_thru_lifetime_;
344
  const bool total_order_seek_;
345
  // List of operands for merge operator.
346
  MergeContext merge_context_;
A
Andrew Kryczka 已提交
347
  RangeDelAggregator range_del_agg_;
348
  LocalStatistics local_stats_;
349
  PinnedIteratorsManager pinned_iters_mgr_;
Y
Yi Wu 已提交
350
  ReadCallback* read_callback_;
351 352
  DBImpl* db_impl_;
  ColumnFamilyData* cfd_;
Y
Yi Wu 已提交
353 354
  bool allow_blob_;
  bool is_blob_;
355 356 357
  // for diff snapshots we want the lower bound on the seqnum;
  // if this value > 0 iterator will return internal keys
  SequenceNumber start_seqnum_;
J
jorlow@chromium.org 已提交
358 359 360 361 362 363 364 365 366

  // No copying allowed
  DBIter(const DBIter&);
  void operator=(const DBIter&);
};

inline bool DBIter::ParseKey(ParsedInternalKey* ikey) {
  if (!ParseInternalKey(iter_->key(), ikey)) {
    status_ = Status::Corruption("corrupted internal key in DBIter");
367
    valid_ = false;
368 369
    ROCKS_LOG_ERROR(logger_, "corrupted internal key in DBIter: %s",
                    iter_->key().ToString(true).c_str());
J
jorlow@chromium.org 已提交
370 371 372 373 374 375
    return false;
  } else {
    return true;
  }
}

J
jorlow@chromium.org 已提交
376 377
void DBIter::Next() {
  assert(valid_);
378
  assert(status_.ok());
J
jorlow@chromium.org 已提交
379

380 381
  // Release temporarily pinned blocks from last operation
  ReleaseTempPinnedData();
382
  ResetInternalKeysSkippedCounter();
383
  bool ok = true;
S
Stanislau Hlebik 已提交
384
  if (direction_ == kReverse) {
385 386 387
    if (!ReverseToForward()) {
      ok = false;
    }
388 389 390 391 392 393 394
  } else if (iter_->Valid() && !current_entry_is_merged_) {
    // If the current value is not a merge, the iter position is the
    // current key, which is already returned. We can safely issue a
    // Next() without checking the current key.
    // If the current key is a merge, very likely iter already points
    // to the next internal position.
    iter_->Next();
395
    PERF_COUNTER_ADD(internal_key_skipped_count, 1);
J
jorlow@chromium.org 已提交
396
  }
J
jorlow@chromium.org 已提交
397

398 399 400
  if (statistics_ != nullptr) {
    local_stats_.next_count_++;
  }
401 402 403 404
  if (ok && iter_->Valid()) {
    FindNextUserEntry(true /* skipping the current user key */,
                      prefix_same_as_start_);
  } else {
405 406
    valid_ = false;
  }
407 408 409 410
  if (statistics_ != nullptr && valid_) {
    local_stats_.next_found_count_++;
    local_stats_.bytes_read_ += (key().size() + value().size());
  }
J
jorlow@chromium.org 已提交
411 412
}

413 414 415 416 417 418 419
// PRE: saved_key_ has the current user key if skipping
// POST: saved_key_ should have the next user key if valid_,
//       if the current entry is a result of merge
//           current_entry_is_merged_ => true
//           saved_value_             => the merged value
//
// NOTE: In between, saved_key_ can point to a user key that has
420
//       a delete marker or a sequence number higher than sequence_
421
//       saved_key_ MUST have a proper user_key before calling this function
422 423 424 425 426
//
// The prefix_check parameter controls whether we check the iterated
// keys against the prefix of the seeked key. Set to false when
// performing a seek without a key (e.g. SeekToFirst). Set to
// prefix_same_as_start_ for other iterations.
427
inline bool DBIter::FindNextUserEntry(bool skipping, bool prefix_check) {
428
  PERF_TIMER_GUARD(find_next_user_entry_time);
429
  return FindNextUserEntryInternal(skipping, prefix_check);
430 431 432
}

// Actual implementation of DBIter::FindNextUserEntry()
433
bool DBIter::FindNextUserEntryInternal(bool skipping, bool prefix_check) {
J
jorlow@chromium.org 已提交
434 435
  // Loop until we hit an acceptable entry to yield
  assert(iter_->Valid());
436
  assert(status_.ok());
J
jorlow@chromium.org 已提交
437
  assert(direction_ == kForward);
438
  current_entry_is_merged_ = false;
439 440 441 442 443 444 445 446 447 448 449

  // How many times in a row we have skipped an entry with user key less than
  // or equal to saved_key_. We could skip these entries either because
  // sequence numbers were too high or because skipping = true.
  // What saved_key_ contains throughout this method:
  //  - if skipping        : saved_key_ contains the key that we need to skip,
  //                         and we haven't seen any keys greater than that,
  //  - if num_skipped > 0 : saved_key_ contains the key that we have skipped
  //                         num_skipped times, and we haven't seen any keys
  //                         greater than that,
  //  - none of the above  : saved_key_ can contain anything, it doesn't matter.
450
  uint64_t num_skipped = 0;
451

Y
Yi Wu 已提交
452 453
  is_blob_ = false;

J
jorlow@chromium.org 已提交
454
  do {
455
    if (!ParseKey(&ikey_)) {
456
      return false;
457
    }
458

459
    if (iterate_upper_bound_ != nullptr &&
460
        user_comparator_->Compare(ikey_.user_key, *iterate_upper_bound_) >= 0) {
461 462
      break;
    }
463

464
    if (prefix_extractor_ && prefix_check &&
465 466
        prefix_extractor_->Transform(ikey_.user_key)
                .compare(prefix_start_key_) != 0) {
467 468 469
      break;
    }

470
    if (TooManyInternalKeysSkipped()) {
471
      return false;
472 473
    }

Y
Yi Wu 已提交
474
    if (IsVisible(ikey_.sequence)) {
475 476
      if (skipping && user_comparator_->Compare(ikey_.user_key,
                                                saved_key_.GetUserKey()) <= 0) {
477 478 479 480
        num_skipped++;  // skip this entry
        PERF_COUNTER_ADD(internal_key_skipped_count, 1);
      } else {
        num_skipped = 0;
481
        switch (ikey_.type) {
482 483 484 485
          case kTypeDeletion:
          case kTypeSingleDeletion:
            // Arrange to skip all upcoming entries for this key since
            // they are hidden by this deletion.
486 487 488
            // if iterartor specified start_seqnum we
            // 1) return internal key, including the type
            // 2) return ikey only if ikey.seqnum >= start_seqnum_
489
            // note that if deletion seqnum is < start_seqnum_ we
490 491 492
            // just skip it like in normal iterator.
            if (start_seqnum_ > 0 && ikey_.sequence >= start_seqnum_)  {
              saved_key_.SetInternalKey(ikey_);
493 494
              valid_ = true;
              return true;
495 496
            } else {
              saved_key_.SetUserKey(
497 498
                ikey_.user_key,
                !pin_thru_lifetime_ || !iter_->IsKeyPinned() /* copy */);
499 500
              skipping = true;
              PERF_COUNTER_ADD(internal_delete_skipped_count, 1);
501
            }
502 503
            break;
          case kTypeValue:
Y
Yi Wu 已提交
504
          case kTypeBlobIndex:
505 506 507 508 509 510 511 512
            if (start_seqnum_ > 0) {
              // we are taking incremental snapshot here
              // incremental snapshots aren't supported on DB with range deletes
              assert(!(
                (ikey_.type == kTypeBlobIndex) && (start_seqnum_ > 0)
              ));
              if (ikey_.sequence >= start_seqnum_) {
                saved_key_.SetInternalKey(ikey_);
Y
Yi Wu 已提交
513
                valid_ = true;
514
                return true;
515 516 517 518 519 520
              } else {
                // this key and all previous versions shouldn't be included,
                // skipping
                saved_key_.SetUserKey(ikey_.user_key,
                  !pin_thru_lifetime_ || !iter_->IsKeyPinned() /* copy */);
                skipping = true;
Y
Yi Wu 已提交
521
              }
522
            } else {
523 524 525 526
              saved_key_.SetUserKey(
                  ikey_.user_key,
                  !pin_thru_lifetime_ || !iter_->IsKeyPinned() /* copy */);
              if (range_del_agg_.ShouldDelete(
527
                      ikey_, RangeDelPositioningMode::kForwardTraversal)) {
528 529 530 531 532 533 534 535 536 537 538 539
                // Arrange to skip all upcoming entries for this key since
                // they are hidden by this deletion.
                skipping = true;
                num_skipped = 0;
                PERF_COUNTER_ADD(internal_delete_skipped_count, 1);
              } else if (ikey_.type == kTypeBlobIndex) {
                if (!allow_blob_) {
                  ROCKS_LOG_ERROR(logger_, "Encounter unexpected blob index.");
                  status_ = Status::NotSupported(
                      "Encounter unexpected blob index. Please open DB with "
                      "rocksdb::blob_db::BlobDB instead.");
                  valid_ = false;
540
                  return false;
541
                }
542 543 544 545

                is_blob_ = true;
                valid_ = true;
                return true;
546 547
              } else {
                valid_ = true;
548
                return true;
549
              }
550 551 552
            }
            break;
          case kTypeMerge:
553
            saved_key_.SetUserKey(
554 555
                ikey_.user_key,
                !pin_thru_lifetime_ || !iter_->IsKeyPinned() /* copy */);
556
            if (range_del_agg_.ShouldDelete(
557
                    ikey_, RangeDelPositioningMode::kForwardTraversal)) {
558 559 560 561 562 563 564 565 566 567
              // Arrange to skip all upcoming entries for this key since
              // they are hidden by this deletion.
              skipping = true;
              num_skipped = 0;
              PERF_COUNTER_ADD(internal_delete_skipped_count, 1);
            } else {
              // By now, we are sure the current ikey is going to yield a
              // value
              current_entry_is_merged_ = true;
              valid_ = true;
568
              return MergeValuesNewToOld();  // Go to a different state machine
569 570 571 572 573
            }
            break;
          default:
            assert(false);
            break;
574
        }
J
jorlow@chromium.org 已提交
575
      }
576 577 578
    } else {
      PERF_COUNTER_ADD(internal_recent_skipped_count, 1);

579 580 581 582 583 584
      // This key was inserted after our snapshot was taken.
      // If this happens too many times in a row for the same user key, we want
      // to seek to the target sequence number.
      int cmp =
          user_comparator_->Compare(ikey_.user_key, saved_key_.GetUserKey());
      if (cmp == 0 || (skipping && cmp <= 0)) {
585 586
        num_skipped++;
      } else {
587
        saved_key_.SetUserKey(
588
            ikey_.user_key,
589
            !iter_->IsKeyPinned() || !pin_thru_lifetime_ /* copy */);
590 591 592
        skipping = false;
        num_skipped = 0;
      }
J
jorlow@chromium.org 已提交
593
    }
594 595 596

    // If we have sequentially iterated via numerous equal keys, then it's
    // better to seek so that we can avoid too many key comparisons.
597
    if (num_skipped > max_skip_ && CanReseekToSkip()) {
598 599
      num_skipped = 0;
      std::string last_key;
600 601 602 603
      if (skipping) {
        // We're looking for the next user-key but all we see are the same
        // user-key with decreasing sequence numbers. Fast forward to
        // sequence number 0 and type deletion (the smallest type).
604 605
        AppendInternalKey(&last_key, ParsedInternalKey(saved_key_.GetUserKey(),
                                                       0, kTypeDeletion));
606 607 608 609 610 611 612 613 614
        // Don't set skipping = false because we may still see more user-keys
        // equal to saved_key_.
      } else {
        // We saw multiple entries with this user key and sequence numbers
        // higher than sequence_. Fast forward to sequence_.
        // Note that this only covers a case when a higher key was overwritten
        // many times since our snapshot was taken, not the case when a lot of
        // different keys were inserted after our snapshot was taken.
        AppendInternalKey(&last_key,
615
                          ParsedInternalKey(saved_key_.GetUserKey(), sequence_,
616 617
                                            kValueTypeForSeek));
      }
618 619 620 621 622
      iter_->Seek(last_key);
      RecordTick(statistics_, NUMBER_OF_RESEEKS_IN_ITERATION);
    } else {
      iter_->Next();
    }
J
jorlow@chromium.org 已提交
623
  } while (iter_->Valid());
624

J
jorlow@chromium.org 已提交
625
  valid_ = false;
626
  return iter_->status().ok();
J
jorlow@chromium.org 已提交
627 628
}

629 630 631 632 633 634
// Merge values of the same user key starting from the current iter_ position
// Scan from the newer entries to older entries.
// PRE: iter_->key() points to the first merge type entry
//      saved_key_ stores the user key
// POST: saved_value_ has the merged value for the user key
//       iter_ points to the next entry (or invalid)
635
bool DBIter::MergeValuesNewToOld() {
636
  if (!merge_operator_) {
637
    ROCKS_LOG_ERROR(logger_, "Options::merge_operator is null.");
638
    status_ = Status::InvalidArgument("merge_operator_ must be set.");
639
    valid_ = false;
640
    return false;
D
Deon Nicholas 已提交
641
  }
642

643 644
  // Temporarily pin the blocks that hold merge operands
  TempPinData();
645
  merge_context_.Clear();
646
  // Start the merge process by pushing the first operand
647 648
  merge_context_.PushOperand(iter_->value(),
                             iter_->IsValuePinned() /* operand_pinned */);
649
  TEST_SYNC_POINT("DBIter::MergeValuesNewToOld:PushedFirstOperand");
650 651

  ParsedInternalKey ikey;
652
  Status s;
653
  for (iter_->Next(); iter_->Valid(); iter_->Next()) {
654
    TEST_SYNC_POINT("DBIter::MergeValuesNewToOld:SteppedToNextOperand");
655
    if (!ParseKey(&ikey)) {
656
      return false;
657 658
    }

659
    if (!user_comparator_->Equal(ikey.user_key, saved_key_.GetUserKey())) {
660 661
      // hit the next user key, stop right here
      break;
A
Andrew Kryczka 已提交
662
    } else if (kTypeDeletion == ikey.type || kTypeSingleDeletion == ikey.type ||
663
               range_del_agg_.ShouldDelete(
664
                   ikey, RangeDelPositioningMode::kForwardTraversal)) {
665 666 667 668
      // hit a delete with the same user key, stop right here
      // iter_ is positioned after delete
      iter_->Next();
      break;
A
Andres Noetzli 已提交
669
    } else if (kTypeValue == ikey.type) {
670 671
      // hit a put, merge the put value with operands and store the
      // final result in saved_value_. We are done!
I
Igor Canadi 已提交
672
      const Slice val = iter_->value();
673 674
      s = MergeHelper::TimedFullMerge(
          merge_operator_, ikey.user_key, &val, merge_context_.GetOperands(),
675
          &saved_value_, logger_, statistics_, env_, &pinned_value_, true);
676
      if (!s.ok()) {
Y
Yi Wu 已提交
677
        valid_ = false;
678
        status_ = s;
679
        return false;
680
      }
681 682
      // iter_ is positioned after put
      iter_->Next();
683 684 685 686 687
      if (!iter_->status().ok()) {
        valid_ = false;
        return false;
      }
      return true;
A
Andres Noetzli 已提交
688
    } else if (kTypeMerge == ikey.type) {
689 690
      // hit a merge, add the value as an operand and run associative merge.
      // when complete, add result to operands and continue.
691 692
      merge_context_.PushOperand(iter_->value(),
                                 iter_->IsValuePinned() /* operand_pinned */);
693
      PERF_COUNTER_ADD(internal_merge_count, 1);
Y
Yi Wu 已提交
694 695 696 697 698 699 700 701 702 703 704
    } else if (kTypeBlobIndex == ikey.type) {
      if (!allow_blob_) {
        ROCKS_LOG_ERROR(logger_, "Encounter unexpected blob index.");
        status_ = Status::NotSupported(
            "Encounter unexpected blob index. Please open DB with "
            "rocksdb::blob_db::BlobDB instead.");
      } else {
        status_ =
            Status::NotSupported("Blob DB does not support merge operator.");
      }
      valid_ = false;
705
      return false;
A
Andres Noetzli 已提交
706 707
    } else {
      assert(false);
708 709 710
    }
  }

711 712 713 714 715
  if (!iter_->status().ok()) {
    valid_ = false;
    return false;
  }

716 717 718 719
  // we either exhausted all internal keys under this user key, or hit
  // a deletion marker.
  // feed null as the existing value to the merge operator, such that
  // client can differentiate this scenario and do things accordingly.
720 721 722
  s = MergeHelper::TimedFullMerge(merge_operator_, saved_key_.GetUserKey(),
                                  nullptr, merge_context_.GetOperands(),
                                  &saved_value_, logger_, statistics_, env_,
723
                                  &pinned_value_, true);
724
  if (!s.ok()) {
Y
Yi Wu 已提交
725
    valid_ = false;
726
    status_ = s;
727
    return false;
728
  }
729 730 731

  assert(status_.ok());
  return true;
732 733
}

J
jorlow@chromium.org 已提交
734 735
void DBIter::Prev() {
  assert(valid_);
736
  assert(status_.ok());
737
  ReleaseTempPinnedData();
738
  ResetInternalKeysSkippedCounter();
739
  bool ok = true;
S
Stanislau Hlebik 已提交
740
  if (direction_ == kForward) {
741 742 743 744 745 746
    if (!ReverseToBackward()) {
      ok = false;
    }
  }
  if (ok) {
    PrevInternal();
S
Stanislau Hlebik 已提交
747
  }
M
Manuel Ung 已提交
748
  if (statistics_ != nullptr) {
749
    local_stats_.prev_count_++;
M
Manuel Ung 已提交
750
    if (valid_) {
751 752
      local_stats_.prev_found_count_++;
      local_stats_.bytes_read_ += (key().size() + value().size());
M
Manuel Ung 已提交
753 754
    }
  }
S
Stanislau Hlebik 已提交
755
}
J
jorlow@chromium.org 已提交
756

757 758 759 760 761 762 763
bool DBIter::ReverseToForward() {
  assert(iter_->status().ok());

  // When moving backwards, iter_ is positioned on _previous_ key, which may
  // not exist or may have different prefix than the current key().
  // If that's the case, seek iter_ to current key.
  if ((prefix_extractor_ != nullptr && !total_order_seek_) || !iter_->Valid()) {
764 765
    IterKey last_key;
    last_key.SetInternalKey(ParsedInternalKey(
766 767
        saved_key_.GetUserKey(), kMaxSequenceNumber, kValueTypeForSeek));
    iter_->Seek(last_key.GetInternalKey());
768
  }
769

770
  direction_ = kForward;
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
  // Skip keys less than the current key() (a.k.a. saved_key_).
  while (iter_->Valid()) {
    ParsedInternalKey ikey;
    if (!ParseKey(&ikey)) {
      return false;
    }
    if (user_comparator_->Compare(ikey.user_key, saved_key_.GetUserKey()) >=
        0) {
      return true;
    }
    iter_->Next();
  }

  if (!iter_->status().ok()) {
    valid_ = false;
    return false;
787
  }
788 789

  return true;
790 791
}

792 793 794 795 796 797 798 799 800 801
// Move iter_ to the key before saved_key_.
bool DBIter::ReverseToBackward() {
  assert(iter_->status().ok());

  // When current_entry_is_merged_ is true, iter_ may be positioned on the next
  // key, which may not exist or may have prefix different from current.
  // If that's the case, seek to saved_key_.
  if (current_entry_is_merged_ &&
      ((prefix_extractor_ != nullptr && !total_order_seek_) ||
       !iter_->Valid())) {
802
    IterKey last_key;
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
    // Using kMaxSequenceNumber and kValueTypeForSeek
    // (not kValueTypeForSeekForPrev) to seek to a key strictly smaller
    // than saved_key_.
    last_key.SetInternalKey(ParsedInternalKey(
        saved_key_.GetUserKey(), kMaxSequenceNumber, kValueTypeForSeek));
    if (prefix_extractor_ != nullptr && !total_order_seek_) {
      iter_->SeekForPrev(last_key.GetInternalKey());
    } else {
      // Some iterators may not support SeekForPrev(), so we avoid using it
      // when prefix seek mode is disabled. This is somewhat expensive
      // (an extra Prev(), as well as an extra change of direction of iter_),
      // so we may need to reconsider it later.
      iter_->Seek(last_key.GetInternalKey());
      if (!iter_->Valid() && iter_->status().ok()) {
        iter_->SeekToLast();
818
      }
819 820 821 822
    }
  }

  direction_ = kReverse;
823
  return FindUserKeyBeforeSavedKey();
824 825
}

S
Stanislau Hlebik 已提交
826 827
void DBIter::PrevInternal() {
  while (iter_->Valid()) {
828 829 830
    saved_key_.SetUserKey(
        ExtractUserKey(iter_->key()),
        !iter_->IsKeyPinned() || !pin_thru_lifetime_ /* copy */);
831

832 833 834 835 836 837 838 839
    if (prefix_extractor_ && prefix_same_as_start_ &&
        prefix_extractor_->Transform(saved_key_.GetUserKey())
                .compare(prefix_start_key_) != 0) {
      // Current key does not have the same prefix as start
      valid_ = false;
      return;
    }

840 841 842 843 844 845 846 847
    if (iterate_lower_bound_ != nullptr &&
        user_comparator_->Compare(saved_key_.GetUserKey(),
                                  *iterate_lower_bound_) < 0) {
      // We've iterated earlier than the user-specified lower bound.
      valid_ = false;
      return;
    }

848
    if (!FindValueForCurrentKey()) {  // assigns valid_
S
Stanislau Hlebik 已提交
849
      return;
J
jorlow@chromium.org 已提交
850
    }
851

852 853 854
    // Whether or not we found a value for current key, we need iter_ to end up
    // on a smaller key.
    if (!FindUserKeyBeforeSavedKey()) {
855 856 857
      return;
    }

858 859 860
    if (valid_) {
      // Found the value.
      return;
S
Stanislau Hlebik 已提交
861
    }
862 863 864

    if (TooManyInternalKeysSkipped(false)) {
      return;
S
Stanislau Hlebik 已提交
865 866
    }
  }
867

S
Stanislau Hlebik 已提交
868 869
  // We haven't found any key - iterator is not valid
  valid_ = false;
J
jorlow@chromium.org 已提交
870 871
}

872 873 874 875 876 877 878 879 880 881 882
// Used for backwards iteration.
// Looks at the entries with user key saved_key_ and finds the most up-to-date
// value for it, or executes a merge, or determines that the value was deleted.
// Sets valid_ to true if the value is found and is ready to be presented to
// the user through value().
// Sets valid_ to false if the value was deleted, and we should try another key.
// Returns false if an error occurred, and !status().ok() and !valid_.
//
// PRE: iter_ is positioned on the last entry with user key equal to saved_key_.
// POST: iter_ is positioned on one of the entries equal to saved_key_, or on
//       the entry just before them, or on the entry just after them.
S
Stanislau Hlebik 已提交
883 884
bool DBIter::FindValueForCurrentKey() {
  assert(iter_->Valid());
885
  merge_context_.Clear();
886
  current_entry_is_merged_ = false;
A
Andres Noetzli 已提交
887 888
  // last entry before merge (could be kTypeDeletion, kTypeSingleDeletion or
  // kTypeValue)
S
Stanislau Hlebik 已提交
889 890
  ValueType last_not_merge_type = kTypeDeletion;
  ValueType last_key_entry_type = kTypeDeletion;
J
jorlow@chromium.org 已提交
891

892 893 894
  // Temporarily pin blocks that hold (merge operands / the value)
  ReleaseTempPinnedData();
  TempPinData();
S
Stanislau Hlebik 已提交
895
  size_t num_skipped = 0;
896 897 898 899 900 901 902 903 904 905
  while (iter_->Valid()) {
    ParsedInternalKey ikey;
    if (!ParseKey(&ikey)) {
      return false;
    }

    if (!IsVisible(ikey.sequence) ||
        !user_comparator_->Equal(ikey.user_key, saved_key_.GetUserKey())) {
      break;
    }
906 907 908 909
    if (TooManyInternalKeysSkipped()) {
      return false;
    }

910 911 912
    // This user key has lots of entries.
    // We're going from old to new, and it's taking too long. Let's do a Seek()
    // and go from new to old. This helps when a key was overwritten many times.
913
    if (num_skipped >= max_skip_ && CanReseekToSkip()) {
S
Stanislau Hlebik 已提交
914 915 916 917 918 919
      return FindValueForCurrentKeyUsingSeek();
    }

    last_key_entry_type = ikey.type;
    switch (last_key_entry_type) {
      case kTypeValue:
Y
Yi Wu 已提交
920
      case kTypeBlobIndex:
921
        if (range_del_agg_.ShouldDelete(
922
                ikey, RangeDelPositioningMode::kBackwardTraversal)) {
A
Andrew Kryczka 已提交
923 924 925 926 927 928
          last_key_entry_type = kTypeRangeDeletion;
          PERF_COUNTER_ADD(internal_delete_skipped_count, 1);
        } else {
          assert(iter_->IsValuePinned());
          pinned_value_ = iter_->value();
        }
929
        merge_context_.Clear();
A
Andrew Kryczka 已提交
930
        last_not_merge_type = last_key_entry_type;
S
Stanislau Hlebik 已提交
931 932
        break;
      case kTypeDeletion:
A
Andres Noetzli 已提交
933
      case kTypeSingleDeletion:
934
        merge_context_.Clear();
A
Andres Noetzli 已提交
935
        last_not_merge_type = last_key_entry_type;
936
        PERF_COUNTER_ADD(internal_delete_skipped_count, 1);
S
Stanislau Hlebik 已提交
937 938
        break;
      case kTypeMerge:
939
        if (range_del_agg_.ShouldDelete(
940
                ikey, RangeDelPositioningMode::kBackwardTraversal)) {
A
Andrew Kryczka 已提交
941 942 943 944 945 946 947 948
          merge_context_.Clear();
          last_key_entry_type = kTypeRangeDeletion;
          last_not_merge_type = last_key_entry_type;
          PERF_COUNTER_ADD(internal_delete_skipped_count, 1);
        } else {
          assert(merge_operator_ != nullptr);
          merge_context_.PushOperandBack(
              iter_->value(), iter_->IsValuePinned() /* operand_pinned */);
949
          PERF_COUNTER_ADD(internal_merge_count, 1);
A
Andrew Kryczka 已提交
950
        }
S
Stanislau Hlebik 已提交
951 952 953 954 955
        break;
      default:
        assert(false);
    }

956
    PERF_COUNTER_ADD(internal_key_skipped_count, 1);
S
Stanislau Hlebik 已提交
957 958
    iter_->Prev();
    ++num_skipped;
959 960 961 962 963
  }

  if (!iter_->status().ok()) {
    valid_ = false;
    return false;
S
Stanislau Hlebik 已提交
964 965
  }

966
  Status s;
Y
Yi Wu 已提交
967
  is_blob_ = false;
S
Stanislau Hlebik 已提交
968 969
  switch (last_key_entry_type) {
    case kTypeDeletion:
A
Andres Noetzli 已提交
970
    case kTypeSingleDeletion:
A
Andrew Kryczka 已提交
971
    case kTypeRangeDeletion:
S
Stanislau Hlebik 已提交
972
      valid_ = false;
973
      return true;
S
Stanislau Hlebik 已提交
974
    case kTypeMerge:
975
      current_entry_is_merged_ = true;
A
Aaron Gao 已提交
976
      if (last_not_merge_type == kTypeDeletion ||
A
Andrew Kryczka 已提交
977 978
          last_not_merge_type == kTypeSingleDeletion ||
          last_not_merge_type == kTypeRangeDeletion) {
979 980 981
        s = MergeHelper::TimedFullMerge(
            merge_operator_, saved_key_.GetUserKey(), nullptr,
            merge_context_.GetOperands(), &saved_value_, logger_, statistics_,
982
            env_, &pinned_value_, true);
Y
Yi Wu 已提交
983 984 985 986 987 988 989 990 991 992 993
      } else if (last_not_merge_type == kTypeBlobIndex) {
        if (!allow_blob_) {
          ROCKS_LOG_ERROR(logger_, "Encounter unexpected blob index.");
          status_ = Status::NotSupported(
              "Encounter unexpected blob index. Please open DB with "
              "rocksdb::blob_db::BlobDB instead.");
        } else {
          status_ =
              Status::NotSupported("Blob DB does not support merge operator.");
        }
        valid_ = false;
994
        return false;
995
      } else {
S
Stanislau Hlebik 已提交
996
        assert(last_not_merge_type == kTypeValue);
997
        s = MergeHelper::TimedFullMerge(
998
            merge_operator_, saved_key_.GetUserKey(), &pinned_value_,
999
            merge_context_.GetOperands(), &saved_value_, logger_, statistics_,
1000
            env_, &pinned_value_, true);
1001
      }
S
Stanislau Hlebik 已提交
1002 1003
      break;
    case kTypeValue:
1004
      // do nothing - we've already has value in pinned_value_
S
Stanislau Hlebik 已提交
1005
      break;
Y
Yi Wu 已提交
1006 1007 1008 1009 1010 1011 1012
    case kTypeBlobIndex:
      if (!allow_blob_) {
        ROCKS_LOG_ERROR(logger_, "Encounter unexpected blob index.");
        status_ = Status::NotSupported(
            "Encounter unexpected blob index. Please open DB with "
            "rocksdb::blob_db::BlobDB instead.");
        valid_ = false;
1013
        return false;
Y
Yi Wu 已提交
1014 1015 1016
      }
      is_blob_ = true;
      break;
S
Stanislau Hlebik 已提交
1017 1018 1019
    default:
      assert(false);
      break;
J
jorlow@chromium.org 已提交
1020
  }
1021
  if (!s.ok()) {
Y
Yi Wu 已提交
1022
    valid_ = false;
1023
    status_ = s;
1024
    return false;
1025
  }
1026
  valid_ = true;
S
Stanislau Hlebik 已提交
1027 1028
  return true;
}
J
jorlow@chromium.org 已提交
1029

S
Stanislau Hlebik 已提交
1030 1031
// This function is used in FindValueForCurrentKey.
// We use Seek() function instead of Prev() to find necessary value
1032 1033
// TODO: This is very similar to FindNextUserEntry() and MergeValuesNewToOld().
//       Would be nice to reuse some code.
S
Stanislau Hlebik 已提交
1034
bool DBIter::FindValueForCurrentKeyUsingSeek() {
1035 1036 1037
  // FindValueForCurrentKey will enable pinning before calling
  // FindValueForCurrentKeyUsingSeek()
  assert(pinned_iters_mgr_.PinningEnabled());
S
Stanislau Hlebik 已提交
1038
  std::string last_key;
1039 1040
  AppendInternalKey(&last_key, ParsedInternalKey(saved_key_.GetUserKey(),
                                                 sequence_, kValueTypeForSeek));
S
Stanislau Hlebik 已提交
1041 1042 1043
  iter_->Seek(last_key);
  RecordTick(statistics_, NUMBER_OF_RESEEKS_IN_ITERATION);

1044 1045
  // In case read_callback presents, the value we seek to may not be visible.
  // Find the next value that's visible.
S
Stanislau Hlebik 已提交
1046
  ParsedInternalKey ikey;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
  while (true) {
    if (!iter_->Valid()) {
      valid_ = false;
      return iter_->status().ok();
    }

    if (!ParseKey(&ikey)) {
      return false;
    }
    if (!user_comparator_->Equal(ikey.user_key, saved_key_.GetUserKey())) {
      // No visible values for this key, even though FindValueForCurrentKey()
      // has seen some. This is possible if we're using a tailing iterator, and
      // the entries were discarded in a compaction.
      valid_ = false;
      return true;
    }

    if (IsVisible(ikey.sequence)) {
      break;
    }
1067 1068 1069

    iter_->Next();
  }
S
Stanislau Hlebik 已提交
1070

A
Andrew Kryczka 已提交
1071
  if (ikey.type == kTypeDeletion || ikey.type == kTypeSingleDeletion ||
1072
      range_del_agg_.ShouldDelete(
1073
          ikey, RangeDelPositioningMode::kBackwardTraversal)) {
J
jorlow@chromium.org 已提交
1074
    valid_ = false;
1075
    return true;
S
Stanislau Hlebik 已提交
1076
  }
Y
Yi Wu 已提交
1077 1078 1079 1080 1081 1082
  if (ikey.type == kTypeBlobIndex && !allow_blob_) {
    ROCKS_LOG_ERROR(logger_, "Encounter unexpected blob index.");
    status_ = Status::NotSupported(
        "Encounter unexpected blob index. Please open DB with "
        "rocksdb::blob_db::BlobDB instead.");
    valid_ = false;
1083
    return false;
Y
Yi Wu 已提交
1084 1085
  }
  if (ikey.type == kTypeValue || ikey.type == kTypeBlobIndex) {
A
Andrew Kryczka 已提交
1086 1087 1088 1089 1090
    assert(iter_->IsValuePinned());
    pinned_value_ = iter_->value();
    valid_ = true;
    return true;
  }
S
Stanislau Hlebik 已提交
1091 1092 1093

  // kTypeMerge. We need to collect all kTypeMerge values and save them
  // in operands
1094
  assert(ikey.type == kTypeMerge);
1095
  current_entry_is_merged_ = true;
1096
  merge_context_.Clear();
1097 1098 1099
  merge_context_.PushOperand(iter_->value(),
                             iter_->IsValuePinned() /* operand_pinned */);
  while (true) {
S
Stanislau Hlebik 已提交
1100 1101
    iter_->Next();

1102 1103 1104 1105 1106 1107
    if (!iter_->Valid()) {
      if (!iter_->status().ok()) {
        valid_ = false;
        return false;
      }
      break;
S
Stanislau Hlebik 已提交
1108
    }
1109 1110 1111 1112 1113 1114 1115 1116 1117
    if (!ParseKey(&ikey)) {
      return false;
    }
    if (!user_comparator_->Equal(ikey.user_key, saved_key_.GetUserKey())) {
      break;
    }

    if (ikey.type == kTypeDeletion || ikey.type == kTypeSingleDeletion ||
        range_del_agg_.ShouldDelete(
1118
            ikey, RangeDelPositioningMode::kBackwardTraversal)) {
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
      break;
    } else if (ikey.type == kTypeValue) {
      const Slice val = iter_->value();
      Status s = MergeHelper::TimedFullMerge(
          merge_operator_, saved_key_.GetUserKey(), &val,
          merge_context_.GetOperands(), &saved_value_, logger_, statistics_,
          env_, &pinned_value_, true);
      if (!s.ok()) {
        valid_ = false;
        status_ = s;
        return false;
      }
Y
Yi Wu 已提交
1131
      valid_ = true;
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
      return true;
    } else if (ikey.type == kTypeMerge) {
      merge_context_.PushOperand(iter_->value(),
                                 iter_->IsValuePinned() /* operand_pinned */);
      PERF_COUNTER_ADD(internal_merge_count, 1);
    } else if (ikey.type == kTypeBlobIndex) {
      if (!allow_blob_) {
        ROCKS_LOG_ERROR(logger_, "Encounter unexpected blob index.");
        status_ = Status::NotSupported(
            "Encounter unexpected blob index. Please open DB with "
            "rocksdb::blob_db::BlobDB instead.");
      } else {
        status_ =
            Status::NotSupported("Blob DB does not support merge operator.");
      }
Y
Yi Wu 已提交
1147
      valid_ = false;
1148 1149 1150
      return false;
    } else {
      assert(false);
1151
    }
S
Stanislau Hlebik 已提交
1152 1153
  }

1154 1155 1156 1157 1158
  Status s = MergeHelper::TimedFullMerge(
      merge_operator_, saved_key_.GetUserKey(), nullptr,
      merge_context_.GetOperands(), &saved_value_, logger_, statistics_, env_,
      &pinned_value_, true);
  if (!s.ok()) {
Y
Yi Wu 已提交
1159
    valid_ = false;
1160
    status_ = s;
1161
    return false;
1162
  }
S
Stanislau Hlebik 已提交
1163

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
  // Make sure we leave iter_ in a good state. If it's valid and we don't care
  // about prefixes, that's already good enough. Otherwise it needs to be
  // seeked to the current key.
  if ((prefix_extractor_ != nullptr && !total_order_seek_) || !iter_->Valid()) {
    if (prefix_extractor_ != nullptr && !total_order_seek_) {
      iter_->SeekForPrev(last_key);
    } else {
      iter_->Seek(last_key);
      if (!iter_->Valid() && iter_->status().ok()) {
        iter_->SeekToLast();
      }
    }
    RecordTick(statistics_, NUMBER_OF_RESEEKS_IN_ITERATION);
S
Stanislau Hlebik 已提交
1177
  }
1178 1179 1180

  valid_ = true;
  return true;
S
Stanislau Hlebik 已提交
1181 1182
}

1183 1184 1185 1186
// Move backwards until the key smaller than saved_key_.
// Changes valid_ only if return value is false.
bool DBIter::FindUserKeyBeforeSavedKey() {
  assert(status_.ok());
S
Stanislau Hlebik 已提交
1187
  size_t num_skipped = 0;
1188 1189 1190 1191
  while (iter_->Valid()) {
    ParsedInternalKey ikey;
    if (!ParseKey(&ikey)) {
      return false;
1192 1193
    }

1194 1195 1196 1197 1198 1199
    if (user_comparator_->Compare(ikey.user_key, saved_key_.GetUserKey()) < 0) {
      return true;
    }

    if (TooManyInternalKeysSkipped()) {
      return false;
S
Stanislau Hlebik 已提交
1200
    }
1201

S
Siying Dong 已提交
1202
    assert(ikey.sequence != kMaxSequenceNumber);
Y
Yi Wu 已提交
1203
    if (!IsVisible(ikey.sequence)) {
1204 1205 1206 1207
      PERF_COUNTER_ADD(internal_recent_skipped_count, 1);
    } else {
      PERF_COUNTER_ADD(internal_key_skipped_count, 1);
    }
1208

1209
    if (num_skipped >= max_skip_ && CanReseekToSkip()) {
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
      num_skipped = 0;
      IterKey last_key;
      last_key.SetInternalKey(ParsedInternalKey(
          saved_key_.GetUserKey(), kMaxSequenceNumber, kValueTypeForSeek));
      // It would be more efficient to use SeekForPrev() here, but some
      // iterators may not support it.
      iter_->Seek(last_key.GetInternalKey());
      RecordTick(statistics_, NUMBER_OF_RESEEKS_IN_ITERATION);
      if (!iter_->Valid()) {
        break;
      }
    } else {
      ++num_skipped;
    }

S
Stanislau Hlebik 已提交
1225 1226
    iter_->Prev();
  }
1227 1228 1229 1230 1231 1232 1233

  if (!iter_->status().ok()) {
    valid_ = false;
    return false;
  }

  return true;
S
Stanislau Hlebik 已提交
1234 1235
}

1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
bool DBIter::TooManyInternalKeysSkipped(bool increment) {
  if ((max_skippable_internal_keys_ > 0) &&
      (num_internal_keys_skipped_ > max_skippable_internal_keys_)) {
    valid_ = false;
    status_ = Status::Incomplete("Too many internal keys skipped.");
    return true;
  } else if (increment) {
    num_internal_keys_skipped_++;
  }
  return false;
}

Y
Yi Wu 已提交
1248
bool DBIter::IsVisible(SequenceNumber sequence) {
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
  return sequence <= MaxVisibleSequenceNumber() &&
         (read_callback_ == nullptr || read_callback_->IsVisible(sequence));
}

bool DBIter::CanReseekToSkip() {
  return read_callback_ == nullptr ||
         read_callback_->MaxUnpreparedSequenceNumber() == 0;
}

SequenceNumber DBIter::MaxVisibleSequenceNumber() {
  if (read_callback_ == nullptr) {
    return sequence_;
  }

  return std::max(sequence_, read_callback_->MaxUnpreparedSequenceNumber());
Y
Yi Wu 已提交
1264 1265
}

J
jorlow@chromium.org 已提交
1266
void DBIter::Seek(const Slice& target) {
L
Lei Jin 已提交
1267
  StopWatch sw(env_, statistics_, DB_SEEK);
1268
  status_ = Status::OK();
1269
  ReleaseTempPinnedData();
1270
  ResetInternalKeysSkippedCounter();
1271 1272

  SequenceNumber seq = MaxVisibleSequenceNumber();
1273
  saved_key_.Clear();
1274
  saved_key_.SetInternalKey(target, seq);
1275

1276 1277 1278 1279 1280 1281
#ifndef ROCKSDB_LITE
  if (db_impl_ != nullptr && cfd_ != nullptr) {
    db_impl_->TraceIteratorSeek(cfd_->GetID(), target);
  }
#endif  // ROCKSDB_LITE

Z
zhangjinpeng1987 已提交
1282 1283 1284 1285
  if (iterate_lower_bound_ != nullptr &&
      user_comparator_->Compare(saved_key_.GetUserKey(),
                                *iterate_lower_bound_) < 0) {
    saved_key_.Clear();
1286
    saved_key_.SetInternalKey(*iterate_lower_bound_, seq);
Z
zhangjinpeng1987 已提交
1287 1288
  }

1289 1290
  {
    PERF_TIMER_GUARD(seek_internal_seek_time);
1291
    iter_->Seek(saved_key_.GetInternalKey());
1292
    range_del_agg_.InvalidateRangeDelMapPositions();
1293
  }
M
Manuel Ung 已提交
1294
  RecordTick(statistics_, NUMBER_DB_SEEK);
J
jorlow@chromium.org 已提交
1295
  if (iter_->Valid()) {
1296 1297 1298
    if (prefix_extractor_ && prefix_same_as_start_) {
      prefix_start_key_ = prefix_extractor_->Transform(target);
    }
1299 1300
    direction_ = kForward;
    ClearSavedValue();
1301 1302 1303 1304
    FindNextUserEntry(false /* not skipping */, prefix_same_as_start_);
    if (!valid_) {
      prefix_start_key_.clear();
    }
M
Manuel Ung 已提交
1305 1306
    if (statistics_ != nullptr) {
      if (valid_) {
1307
        // Decrement since we don't want to count this key as skipped
M
Manuel Ung 已提交
1308 1309
        RecordTick(statistics_, NUMBER_DB_SEEK_FOUND);
        RecordTick(statistics_, ITER_BYTES_READ, key().size() + value().size());
1310
        PERF_COUNTER_ADD(iter_read_bytes, key().size() + value().size());
M
Manuel Ung 已提交
1311 1312
      }
    }
J
jorlow@chromium.org 已提交
1313 1314 1315
  } else {
    valid_ = false;
  }
1316

1317
  if (valid_ && prefix_extractor_ && prefix_same_as_start_) {
1318 1319
    prefix_start_buf_.SetUserKey(prefix_start_key_);
    prefix_start_key_ = prefix_start_buf_.GetUserKey();
1320
  }
J
jorlow@chromium.org 已提交
1321
}
J
jorlow@chromium.org 已提交
1322

A
Aaron Gao 已提交
1323 1324
void DBIter::SeekForPrev(const Slice& target) {
  StopWatch sw(env_, statistics_, DB_SEEK);
1325
  status_ = Status::OK();
A
Aaron Gao 已提交
1326
  ReleaseTempPinnedData();
1327
  ResetInternalKeysSkippedCounter();
A
Aaron Gao 已提交
1328 1329 1330 1331 1332
  saved_key_.Clear();
  // now saved_key is used to store internal key.
  saved_key_.SetInternalKey(target, 0 /* sequence_number */,
                            kValueTypeForSeekForPrev);

Z
zhangjinpeng1987 已提交
1333 1334 1335 1336 1337 1338 1339
  if (iterate_upper_bound_ != nullptr &&
      user_comparator_->Compare(saved_key_.GetUserKey(),
                                *iterate_upper_bound_) >= 0) {
    saved_key_.Clear();
    saved_key_.SetInternalKey(*iterate_upper_bound_, kMaxSequenceNumber);
  }

A
Aaron Gao 已提交
1340 1341
  {
    PERF_TIMER_GUARD(seek_internal_seek_time);
1342
    iter_->SeekForPrev(saved_key_.GetInternalKey());
1343
    range_del_agg_.InvalidateRangeDelMapPositions();
A
Aaron Gao 已提交
1344 1345
  }

1346 1347 1348 1349 1350 1351
#ifndef ROCKSDB_LITE
  if (db_impl_ != nullptr && cfd_ != nullptr) {
    db_impl_->TraceIteratorSeekForPrev(cfd_->GetID(), target);
  }
#endif  // ROCKSDB_LITE

A
Aaron Gao 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
  RecordTick(statistics_, NUMBER_DB_SEEK);
  if (iter_->Valid()) {
    if (prefix_extractor_ && prefix_same_as_start_) {
      prefix_start_key_ = prefix_extractor_->Transform(target);
    }
    direction_ = kReverse;
    ClearSavedValue();
    PrevInternal();
    if (!valid_) {
      prefix_start_key_.clear();
    }
    if (statistics_ != nullptr) {
      if (valid_) {
        RecordTick(statistics_, NUMBER_DB_SEEK_FOUND);
        RecordTick(statistics_, ITER_BYTES_READ, key().size() + value().size());
1367
        PERF_COUNTER_ADD(iter_read_bytes, key().size() + value().size());
A
Aaron Gao 已提交
1368 1369 1370 1371 1372 1373
      }
    }
  } else {
    valid_ = false;
  }
  if (valid_ && prefix_extractor_ && prefix_same_as_start_) {
1374 1375
    prefix_start_buf_.SetUserKey(prefix_start_key_);
    prefix_start_key_ = prefix_start_buf_.GetUserKey();
A
Aaron Gao 已提交
1376 1377 1378
  }
}

J
jorlow@chromium.org 已提交
1379
void DBIter::SeekToFirst() {
1380 1381 1382 1383
  if (iterate_lower_bound_ != nullptr) {
    Seek(*iterate_lower_bound_);
    return;
  }
1384 1385 1386 1387 1388 1389
  // Don't use iter_::Seek() if we set a prefix extractor
  // because prefix seek will be used.
  if (prefix_extractor_ != nullptr && !total_order_seek_) {
    max_skip_ = std::numeric_limits<uint64_t>::max();
  }
  status_ = Status::OK();
J
jorlow@chromium.org 已提交
1390
  direction_ = kForward;
1391
  ReleaseTempPinnedData();
1392
  ResetInternalKeysSkippedCounter();
J
jorlow@chromium.org 已提交
1393
  ClearSavedValue();
1394 1395 1396 1397

  {
    PERF_TIMER_GUARD(seek_internal_seek_time);
    iter_->SeekToFirst();
1398
    range_del_agg_.InvalidateRangeDelMapPositions();
1399 1400
  }

M
Manuel Ung 已提交
1401
  RecordTick(statistics_, NUMBER_DB_SEEK);
J
jorlow@chromium.org 已提交
1402
  if (iter_->Valid()) {
1403 1404 1405
    saved_key_.SetUserKey(
        ExtractUserKey(iter_->key()),
        !iter_->IsKeyPinned() || !pin_thru_lifetime_ /* copy */);
1406
    FindNextUserEntry(false /* not skipping */, false /* no prefix check */);
M
Manuel Ung 已提交
1407 1408 1409 1410
    if (statistics_ != nullptr) {
      if (valid_) {
        RecordTick(statistics_, NUMBER_DB_SEEK_FOUND);
        RecordTick(statistics_, ITER_BYTES_READ, key().size() + value().size());
1411
        PERF_COUNTER_ADD(iter_read_bytes, key().size() + value().size());
M
Manuel Ung 已提交
1412 1413
      }
    }
J
jorlow@chromium.org 已提交
1414 1415
  } else {
    valid_ = false;
J
jorlow@chromium.org 已提交
1416
  }
1417
  if (valid_ && prefix_extractor_ && prefix_same_as_start_) {
1418 1419 1420
    prefix_start_buf_.SetUserKey(
        prefix_extractor_->Transform(saved_key_.GetUserKey()));
    prefix_start_key_ = prefix_start_buf_.GetUserKey();
1421
  }
J
jorlow@chromium.org 已提交
1422 1423
}

J
jorlow@chromium.org 已提交
1424
void DBIter::SeekToLast() {
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434
  if (iterate_upper_bound_ != nullptr) {
    // Seek to last key strictly less than ReadOptions.iterate_upper_bound.
    SeekForPrev(*iterate_upper_bound_);
    if (Valid() && user_comparator_->Equal(*iterate_upper_bound_, key())) {
      ReleaseTempPinnedData();
      PrevInternal();
    }
    return;
  }

S
Stanislau Hlebik 已提交
1435
  // Don't use iter_::Seek() if we set a prefix extractor
1436
  // because prefix seek will be used.
1437
  if (prefix_extractor_ != nullptr && !total_order_seek_) {
S
Stanislau Hlebik 已提交
1438 1439
    max_skip_ = std::numeric_limits<uint64_t>::max();
  }
1440
  status_ = Status::OK();
J
jorlow@chromium.org 已提交
1441
  direction_ = kReverse;
1442
  ReleaseTempPinnedData();
1443
  ResetInternalKeysSkippedCounter();
J
jorlow@chromium.org 已提交
1444
  ClearSavedValue();
1445 1446 1447 1448

  {
    PERF_TIMER_GUARD(seek_internal_seek_time);
    iter_->SeekToLast();
1449
    range_del_agg_.InvalidateRangeDelMapPositions();
1450
  }
1451
  PrevInternal();
M
Manuel Ung 已提交
1452 1453 1454 1455 1456
  if (statistics_ != nullptr) {
    RecordTick(statistics_, NUMBER_DB_SEEK);
    if (valid_) {
      RecordTick(statistics_, NUMBER_DB_SEEK_FOUND);
      RecordTick(statistics_, ITER_BYTES_READ, key().size() + value().size());
1457
      PERF_COUNTER_ADD(iter_read_bytes, key().size() + value().size());
M
Manuel Ung 已提交
1458 1459
    }
  }
1460
  if (valid_ && prefix_extractor_ && prefix_same_as_start_) {
1461 1462 1463
    prefix_start_buf_.SetUserKey(
        prefix_extractor_->Transform(saved_key_.GetUserKey()));
    prefix_start_key_ = prefix_start_buf_.GetUserKey();
1464
  }
J
jorlow@chromium.org 已提交
1465 1466
}

1467 1468
Iterator* NewDBIterator(Env* env, const ReadOptions& read_options,
                        const ImmutableCFOptions& cf_options,
1469
                        const MutableCFOptions& mutable_cf_options,
1470 1471 1472
                        const Comparator* user_key_comparator,
                        InternalIterator* internal_iter,
                        const SequenceNumber& sequence,
Y
Yi Wu 已提交
1473
                        uint64_t max_sequential_skip_in_iterations,
1474 1475 1476 1477 1478 1479
                        ReadCallback* read_callback, DBImpl* db_impl,
                        ColumnFamilyData* cfd, bool allow_blob) {
  DBIter* db_iter = new DBIter(
      env, read_options, cf_options, mutable_cf_options, user_key_comparator,
      internal_iter, sequence, false, max_sequential_skip_in_iterations,
      read_callback, db_impl, cfd, allow_blob);
1480
  return db_iter;
1481 1482
}

I
Igor Canadi 已提交
1483
ArenaWrappedDBIter::~ArenaWrappedDBIter() { db_iter_->~DBIter(); }
1484

A
Andrew Kryczka 已提交
1485 1486 1487 1488
RangeDelAggregator* ArenaWrappedDBIter::GetRangeDelAggregator() {
  return db_iter_->GetRangeDelAggregator();
}

S
sdong 已提交
1489
void ArenaWrappedDBIter::SetIterUnderDBIter(InternalIterator* iter) {
1490 1491 1492 1493 1494 1495 1496 1497 1498
  static_cast<DBIter*>(db_iter_)->SetIter(iter);
}

inline bool ArenaWrappedDBIter::Valid() const { return db_iter_->Valid(); }
inline void ArenaWrappedDBIter::SeekToFirst() { db_iter_->SeekToFirst(); }
inline void ArenaWrappedDBIter::SeekToLast() { db_iter_->SeekToLast(); }
inline void ArenaWrappedDBIter::Seek(const Slice& target) {
  db_iter_->Seek(target);
}
A
Aaron Gao 已提交
1499 1500 1501
inline void ArenaWrappedDBIter::SeekForPrev(const Slice& target) {
  db_iter_->SeekForPrev(target);
}
1502 1503 1504 1505 1506
inline void ArenaWrappedDBIter::Next() { db_iter_->Next(); }
inline void ArenaWrappedDBIter::Prev() { db_iter_->Prev(); }
inline Slice ArenaWrappedDBIter::key() const { return db_iter_->key(); }
inline Slice ArenaWrappedDBIter::value() const { return db_iter_->value(); }
inline Status ArenaWrappedDBIter::status() const { return db_iter_->status(); }
Y
Yi Wu 已提交
1507
bool ArenaWrappedDBIter::IsBlob() const { return db_iter_->IsBlob(); }
1508 1509
inline Status ArenaWrappedDBIter::GetProperty(std::string prop_name,
                                              std::string* prop) {
S
Siying Dong 已提交
1510 1511 1512 1513 1514 1515 1516
  if (prop_name == "rocksdb.iterator.super-version-number") {
    // First try to pass the value returned from inner iterator.
    if (!db_iter_->GetProperty(prop_name, prop).ok()) {
      *prop = ToString(sv_number_);
    }
    return Status::OK();
  }
1517 1518
  return db_iter_->GetProperty(prop_name, prop);
}
S
Siying Dong 已提交
1519 1520 1521

void ArenaWrappedDBIter::Init(Env* env, const ReadOptions& read_options,
                              const ImmutableCFOptions& cf_options,
1522
                              const MutableCFOptions& mutable_cf_options,
S
Siying Dong 已提交
1523 1524
                              const SequenceNumber& sequence,
                              uint64_t max_sequential_skip_in_iteration,
Y
Yi Wu 已提交
1525
                              uint64_t version_number,
1526 1527
                              ReadCallback* read_callback, DBImpl* db_impl,
                              ColumnFamilyData* cfd, bool allow_blob,
1528
                              bool allow_refresh) {
S
Siying Dong 已提交
1529
  auto mem = arena_.AllocateAligned(sizeof(DBIter));
1530 1531 1532 1533
  db_iter_ = new (mem) DBIter(env, read_options, cf_options, mutable_cf_options,
                              cf_options.user_comparator, nullptr, sequence,
                              true, max_sequential_skip_in_iteration,
                              read_callback, db_impl, cfd, allow_blob);
S
Siying Dong 已提交
1534
  sv_number_ = version_number;
1535
  allow_refresh_ = allow_refresh;
S
Siying Dong 已提交
1536 1537 1538
}

Status ArenaWrappedDBIter::Refresh() {
1539
  if (cfd_ == nullptr || db_impl_ == nullptr || !allow_refresh_) {
S
Siying Dong 已提交
1540 1541 1542
    return Status::NotSupported("Creating renew iterator is not allowed.");
  }
  assert(db_iter_ != nullptr);
1543 1544 1545
  // TODO(yiwu): For last_seq_same_as_publish_seq_==false, this is not the
  // correct behavior. Will be corrected automatically when we take a snapshot
  // here for the case of WritePreparedTxnDB.
S
Siying Dong 已提交
1546 1547 1548 1549 1550 1551 1552 1553 1554
  SequenceNumber latest_seq = db_impl_->GetLatestSequenceNumber();
  uint64_t cur_sv_number = cfd_->GetSuperVersionNumber();
  if (sv_number_ != cur_sv_number) {
    Env* env = db_iter_->env();
    db_iter_->~DBIter();
    arena_.~Arena();
    new (&arena_) Arena();

    SuperVersion* sv = cfd_->GetReferencedSuperVersion(db_impl_->mutex());
1555 1556
    Init(env, read_options_, *(cfd_->ioptions()), sv->mutable_cf_options,
         latest_seq, sv->mutable_cf_options.max_sequential_skip_in_iterations,
1557 1558
         cur_sv_number, read_callback_, db_impl_, cfd_, allow_blob_,
         allow_refresh_);
S
Siying Dong 已提交
1559 1560 1561 1562 1563 1564 1565 1566 1567

    InternalIterator* internal_iter = db_impl_->NewInternalIterator(
        read_options_, cfd_, sv, &arena_, db_iter_->GetRangeDelAggregator());
    SetIterUnderDBIter(internal_iter);
  } else {
    db_iter_->set_sequence(latest_seq);
    db_iter_->set_valid(false);
  }
  return Status::OK();
1568
}
J
jorlow@chromium.org 已提交
1569

1570
ArenaWrappedDBIter* NewArenaWrappedDbIterator(
1571
    Env* env, const ReadOptions& read_options,
1572 1573
    const ImmutableCFOptions& cf_options,
    const MutableCFOptions& mutable_cf_options, const SequenceNumber& sequence,
S
Siying Dong 已提交
1574
    uint64_t max_sequential_skip_in_iterations, uint64_t version_number,
Y
Yi Wu 已提交
1575
    ReadCallback* read_callback, DBImpl* db_impl, ColumnFamilyData* cfd,
1576
    bool allow_blob, bool allow_refresh) {
1577
  ArenaWrappedDBIter* iter = new ArenaWrappedDBIter();
1578
  iter->Init(env, read_options, cf_options, mutable_cf_options, sequence,
Y
Yi Wu 已提交
1579
             max_sequential_skip_in_iterations, version_number, read_callback,
1580
             db_impl, cfd, allow_blob, allow_refresh);
1581
  if (db_impl != nullptr && cfd != nullptr && allow_refresh) {
Y
Yi Wu 已提交
1582 1583
    iter->StoreRefreshInfo(read_options, db_impl, cfd, read_callback,
                           allow_blob);
S
Siying Dong 已提交
1584
  }
1585

1586
  return iter;
J
jorlow@chromium.org 已提交
1587 1588
}

1589
}  // namespace rocksdb