forward_iterator.cc 23.3 KB
Newer Older
L
Lei Jin 已提交
1 2 3 4 5 6 7 8
//  Copyright (c) 2013, Facebook, Inc.  All rights reserved.
//  This source code is licensed under the BSD-style license found in the
//  LICENSE file in the root directory of this source tree. An additional grant
//  of patent rights can be found in the PATENTS file in the same directory.

#ifndef ROCKSDB_LITE
#include "db/forward_iterator.h"

9
#include <limits>
L
Lei Jin 已提交
10 11
#include <string>
#include <utility>
12

I
Igor Canadi 已提交
13
#include "db/job_context.h"
L
Lei Jin 已提交
14 15 16 17 18 19 20 21
#include "db/db_impl.h"
#include "db/db_iter.h"
#include "db/column_family.h"
#include "rocksdb/env.h"
#include "rocksdb/slice.h"
#include "rocksdb/slice_transform.h"
#include "table/merger.h"
#include "db/dbformat.h"
22
#include "util/sync_point.h"
L
Lei Jin 已提交
23 24 25 26 27 28 29 30

namespace rocksdb {

// Usage:
//     LevelIterator iter;
//     iter.SetFileIndex(file_index);
//     iter.Seek(target);
//     iter.Next()
S
sdong 已提交
31
class LevelIterator : public InternalIterator {
L
Lei Jin 已提交
32 33 34 35 36 37 38 39 40 41 42
 public:
  LevelIterator(const ColumnFamilyData* const cfd,
      const ReadOptions& read_options,
      const std::vector<FileMetaData*>& files)
    : cfd_(cfd), read_options_(read_options), files_(files), valid_(false),
      file_index_(std::numeric_limits<uint32_t>::max()) {}

  void SetFileIndex(uint32_t file_index) {
    assert(file_index < files_.size());
    if (file_index != file_index_) {
      file_index_ = file_index;
43
      Reset();
L
Lei Jin 已提交
44 45 46
    }
    valid_ = false;
  }
47 48 49 50
  void Reset() {
    assert(file_index_ < files_.size());
    file_iter_.reset(cfd_->table_cache()->NewIterator(
        read_options_, *(cfd_->soptions()), cfd_->internal_comparator(),
51 52
        files_[file_index_]->fd, nullptr /* table_reader_ptr */, nullptr,
        false));
53
  }
L
Lei Jin 已提交
54 55 56 57
  void SeekToLast() override {
    status_ = Status::NotSupported("LevelIterator::SeekToLast()");
    valid_ = false;
  }
I
Igor Sugak 已提交
58
  void Prev() override {
L
Lei Jin 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    status_ = Status::NotSupported("LevelIterator::Prev()");
    valid_ = false;
  }
  bool Valid() const override {
    return valid_;
  }
  void SeekToFirst() override {
    SetFileIndex(0);
    file_iter_->SeekToFirst();
    valid_ = file_iter_->Valid();
  }
  void Seek(const Slice& internal_key) override {
    assert(file_iter_ != nullptr);
    file_iter_->Seek(internal_key);
    valid_ = file_iter_->Valid();
  }
  void Next() override {
    assert(valid_);
    file_iter_->Next();
78 79 80 81 82
    for (;;) {
      if (file_iter_->status().IsIncomplete() || file_iter_->Valid()) {
        valid_ = !file_iter_->status().IsIncomplete();
        return;
      }
L
Lei Jin 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
      if (file_index_ + 1 >= files_.size()) {
        valid_ = false;
        return;
      }
      SetFileIndex(file_index_ + 1);
      file_iter_->SeekToFirst();
    }
  }
  Slice key() const override {
    assert(valid_);
    return file_iter_->key();
  }
  Slice value() const override {
    assert(valid_);
    return file_iter_->value();
  }
  Status status() const override {
100 101 102 103 104 105
    if (!status_.ok()) {
      return status_;
    } else if (file_iter_ && !file_iter_->status().ok()) {
      return file_iter_->status();
    }
    return Status::OK();
L
Lei Jin 已提交
106 107 108 109 110 111 112 113 114 115
  }

 private:
  const ColumnFamilyData* const cfd_;
  const ReadOptions& read_options_;
  const std::vector<FileMetaData*>& files_;

  bool valid_;
  uint32_t file_index_;
  Status status_;
S
sdong 已提交
116
  std::unique_ptr<InternalIterator> file_iter_;
L
Lei Jin 已提交
117 118
};

I
Igor Canadi 已提交
119
ForwardIterator::ForwardIterator(DBImpl* db, const ReadOptions& read_options,
120 121
                                 ColumnFamilyData* cfd,
                                 SuperVersion* current_sv)
L
Lei Jin 已提交
122 123 124
    : db_(db),
      read_options_(read_options),
      cfd_(cfd),
125
      prefix_extractor_(cfd->ioptions()->prefix_extractor),
L
Lei Jin 已提交
126 127
      user_comparator_(cfd->user_comparator()),
      immutable_min_heap_(MinIterComparator(&cfd_->internal_comparator())),
128
      sv_(current_sv),
L
Lei Jin 已提交
129 130
      mutable_iter_(nullptr),
      current_(nullptr),
131
      valid_(false),
132 133
      status_(Status::OK()),
      immutable_status_(Status::OK()),
134
      has_iter_trimmed_for_upper_bound_(false),
135
      current_over_upper_bound_(false),
136
      is_prev_set_(false),
137 138 139 140 141
      is_prev_inclusive_(false) {
  if (sv_) {
    RebuildIterators(false);
  }
}
L
Lei Jin 已提交
142 143

ForwardIterator::~ForwardIterator() {
144
  Cleanup(true);
L
Lei Jin 已提交
145 146
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
void ForwardIterator::SVCleanup() {
  if (sv_ != nullptr && sv_->Unref()) {
    // Job id == 0 means that this is not our background process, but rather
    // user thread
    JobContext job_context(0);
    db_->mutex_.Lock();
    sv_->Cleanup();
    db_->FindObsoleteFiles(&job_context, false, true);
    db_->mutex_.Unlock();
    delete sv_;
    if (job_context.HaveSomethingToDelete()) {
      db_->PurgeObsoleteFiles(job_context);
    }
    job_context.Clean();
  }
}

164
void ForwardIterator::Cleanup(bool release_sv) {
165
  if (mutable_iter_ != nullptr) {
S
sdong 已提交
166
    mutable_iter_->~InternalIterator();
167
  }
L
Lei Jin 已提交
168
  for (auto* m : imm_iters_) {
S
sdong 已提交
169
    m->~InternalIterator();
L
Lei Jin 已提交
170 171 172 173 174 175 176 177 178 179 180
  }
  imm_iters_.clear();
  for (auto* f : l0_iters_) {
    delete f;
  }
  l0_iters_.clear();
  for (auto* l : level_iters_) {
    delete l;
  }
  level_iters_.clear();

181
  if (release_sv) {
182
    SVCleanup();
L
Lei Jin 已提交
183 184 185 186
  }
}

bool ForwardIterator::Valid() const {
187 188
  // See UpdateCurrent().
  return valid_ ? !current_over_upper_bound_ : false;
L
Lei Jin 已提交
189 190 191
}

void ForwardIterator::SeekToFirst() {
192
  if (sv_ == nullptr) {
193
    RebuildIterators(true);
194 195
  } else if (sv_->version_number != cfd_->GetSuperVersionNumber()) {
    RenewIterators();
196
  } else if (immutable_status_.IsIncomplete()) {
197
    ResetIncompleteIterators();
L
Lei Jin 已提交
198 199 200 201
  }
  SeekInternal(Slice(), true);
}

202 203 204 205 206 207 208
bool ForwardIterator::IsOverUpperBound(const Slice& internal_key) const {
  return !(read_options_.iterate_upper_bound == nullptr ||
           cfd_->internal_comparator().user_comparator()->Compare(
               ExtractUserKey(internal_key),
               *read_options_.iterate_upper_bound) < 0);
}

L
Lei Jin 已提交
209
void ForwardIterator::Seek(const Slice& internal_key) {
210 211 212
  if (IsOverUpperBound(internal_key)) {
    valid_ = false;
  }
213
  if (sv_ == nullptr) {
214
    RebuildIterators(true);
215 216
  } else if (sv_->version_number != cfd_->GetSuperVersionNumber()) {
    RenewIterators();
217
  } else if (immutable_status_.IsIncomplete()) {
218
    ResetIncompleteIterators();
L
Lei Jin 已提交
219 220 221 222 223 224
  }
  SeekInternal(internal_key, false);
}

void ForwardIterator::SeekInternal(const Slice& internal_key,
                                   bool seek_to_first) {
225
  assert(mutable_iter_);
L
Lei Jin 已提交
226 227 228 229 230 231 232 233 234
  // mutable
  seek_to_first ? mutable_iter_->SeekToFirst() :
                  mutable_iter_->Seek(internal_key);

  // immutable
  // TODO(ljin): NeedToSeekImmutable has negative impact on performance
  // if it turns to need to seek immutable often. We probably want to have
  // an option to turn it off.
  if (seek_to_first || NeedToSeekImmutable(internal_key)) {
235
    immutable_status_ = Status::OK();
236 237 238
    if ((has_iter_trimmed_for_upper_bound_) &&
        (cfd_->internal_comparator().InternalKeyComparator::Compare(
             prev_key_.GetKey(), internal_key) > 0)) {
239 240 241 242 243 244
      // Some iterators are trimmed. Need to rebuild.
      RebuildIterators(true);
      // Already seeked mutable iter, so seek again
      seek_to_first ? mutable_iter_->SeekToFirst()
                    : mutable_iter_->Seek(internal_key);
    }
L
Lei Jin 已提交
245 246 247 248
    {
      auto tmp = MinIterHeap(MinIterComparator(&cfd_->internal_comparator()));
      immutable_min_heap_.swap(tmp);
    }
249 250
    for (size_t i = 0; i < imm_iters_.size(); i++) {
      auto* m = imm_iters_[i];
L
Lei Jin 已提交
251
      seek_to_first ? m->SeekToFirst() : m->Seek(internal_key);
252 253 254
      if (!m->status().ok()) {
        immutable_status_ = m->status();
      } else if (m->Valid()) {
255
        immutable_min_heap_.push(m);
L
Lei Jin 已提交
256 257 258
      }
    }

259 260 261 262
    Slice user_key;
    if (!seek_to_first) {
      user_key = ExtractUserKey(internal_key);
    }
S
sdong 已提交
263
    const VersionStorageInfo* vstorage = sv_->current->storage_info();
S
sdong 已提交
264
    const std::vector<FileMetaData*>& l0 = vstorage->LevelFiles(0);
L
Lei Jin 已提交
265
    for (uint32_t i = 0; i < l0.size(); ++i) {
266 267 268
      if (!l0_iters_[i]) {
        continue;
      }
L
Lei Jin 已提交
269 270 271 272 273
      if (seek_to_first) {
        l0_iters_[i]->SeekToFirst();
      } else {
        // If the target key passes over the larget key, we are sure Next()
        // won't go over this file.
274
        if (user_comparator_->Compare(user_key,
L
Lei Jin 已提交
275
              l0[i]->largest.user_key()) > 0) {
276 277 278 279 280
          if (read_options_.iterate_upper_bound != nullptr) {
            has_iter_trimmed_for_upper_bound_ = true;
            delete l0_iters_[i];
            l0_iters_[i] = nullptr;
          }
L
Lei Jin 已提交
281 282 283 284
          continue;
        }
        l0_iters_[i]->Seek(internal_key);
      }
285

286 287
      if (!l0_iters_[i]->status().ok()) {
        immutable_status_ = l0_iters_[i]->status();
288
      } else if (l0_iters_[i]->Valid()) {
289 290 291 292 293 294 295
        if (!IsOverUpperBound(l0_iters_[i]->key())) {
          immutable_min_heap_.push(l0_iters_[i]);
        } else {
          has_iter_trimmed_for_upper_bound_ = true;
          delete l0_iters_[i];
          l0_iters_[i] = nullptr;
        }
L
Lei Jin 已提交
296 297
      }
    }
298 299 300

    int32_t search_left_bound = 0;
    int32_t search_right_bound = FileIndexer::kLevelMaxIndex;
301
    for (int32_t level = 1; level < vstorage->num_levels(); ++level) {
L
Lei Jin 已提交
302
      const std::vector<FileMetaData*>& level_files =
S
sdong 已提交
303
          vstorage->LevelFiles(level);
L
Lei Jin 已提交
304
      if (level_files.empty()) {
305 306
        search_left_bound = 0;
        search_right_bound = FileIndexer::kLevelMaxIndex;
L
Lei Jin 已提交
307 308
        continue;
      }
309 310 311
      if (level_iters_[level - 1] == nullptr) {
        continue;
      }
L
Lei Jin 已提交
312
      uint32_t f_idx = 0;
313
      const auto& indexer = vstorage->file_indexer();
L
Lei Jin 已提交
314
      if (!seek_to_first) {
315 316 317
        if (search_left_bound == search_right_bound) {
          f_idx = search_left_bound;
        } else if (search_left_bound < search_right_bound) {
318 319 320 321 322
          f_idx =
              FindFileInRange(level_files, internal_key, search_left_bound,
                              search_right_bound == FileIndexer::kLevelMaxIndex
                                  ? static_cast<uint32_t>(level_files.size())
                                  : search_right_bound);
323 324 325 326 327
        } else {
          // search_left_bound > search_right_bound
          // There are only 2 cases this can happen:
          // (1) target key is smaller than left most file
          // (2) target key is larger than right most file
L
Lei Jin 已提交
328
          assert(search_left_bound == (int32_t)level_files.size() ||
329 330 331 332 333
                 search_right_bound == -1);
          if (search_right_bound == -1) {
            assert(search_left_bound == 0);
            f_idx = 0;
          } else {
L
Lei Jin 已提交
334 335
            indexer.GetNextLevelIndex(
                level, level_files.size() - 1,
336 337 338 339 340 341
                1, 1, &search_left_bound, &search_right_bound);
            continue;
          }
        }

        // Prepare hints for the next level
L
Lei Jin 已提交
342
        if (f_idx < level_files.size()) {
343
          int cmp_smallest = user_comparator_->Compare(
L
Lei Jin 已提交
344
              user_key, level_files[f_idx]->smallest.user_key());
345
          assert(user_comparator_->Compare(
346
                     user_key, level_files[f_idx]->largest.user_key()) <= 0);
347 348
          indexer.GetNextLevelIndex(level, f_idx, cmp_smallest, -1,
                                    &search_left_bound, &search_right_bound);
349
        } else {
L
Lei Jin 已提交
350 351
          indexer.GetNextLevelIndex(
              level, level_files.size() - 1,
352 353
              1, 1, &search_left_bound, &search_right_bound);
        }
L
Lei Jin 已提交
354
      }
355 356

      // Seek
L
Lei Jin 已提交
357
      if (f_idx < level_files.size()) {
L
Lei Jin 已提交
358 359 360
        level_iters_[level - 1]->SetFileIndex(f_idx);
        seek_to_first ? level_iters_[level - 1]->SeekToFirst() :
                        level_iters_[level - 1]->Seek(internal_key);
361

362 363
        if (!level_iters_[level - 1]->status().ok()) {
          immutable_status_ = level_iters_[level - 1]->status();
364
        } else if (level_iters_[level - 1]->Valid()) {
365 366 367 368 369 370 371 372
          if (!IsOverUpperBound(level_iters_[level - 1]->key())) {
            immutable_min_heap_.push(level_iters_[level - 1]);
          } else {
            // Nothing in this level is interesting. Remove.
            has_iter_trimmed_for_upper_bound_ = true;
            delete level_iters_[level - 1];
            level_iters_[level - 1] = nullptr;
          }
L
Lei Jin 已提交
373 374 375 376
        }
      }
    }

377
    if (seek_to_first) {
L
Lei Jin 已提交
378 379 380 381
      is_prev_set_ = false;
    } else {
      prev_key_.SetKey(internal_key);
      is_prev_set_ = true;
382
      is_prev_inclusive_ = true;
L
Lei Jin 已提交
383
    }
384 385

    TEST_SYNC_POINT_CALLBACK("ForwardIterator::SeekInternal:Immutable", this);
T
Tomislav Novak 已提交
386 387 388
  } else if (current_ && current_ != mutable_iter_) {
    // current_ is one of immutable iterators, push it back to the heap
    immutable_min_heap_.push(current_);
L
Lei Jin 已提交
389 390 391
  }

  UpdateCurrent();
392
  TEST_SYNC_POINT_CALLBACK("ForwardIterator::SeekInternal:Return", this);
L
Lei Jin 已提交
393 394 395 396
}

void ForwardIterator::Next() {
  assert(valid_);
397
  bool update_prev_key = false;
L
Lei Jin 已提交
398 399

  if (sv_ == nullptr ||
400
      sv_->version_number != cfd_->GetSuperVersionNumber()) {
L
Lei Jin 已提交
401 402 403
    std::string current_key = key().ToString();
    Slice old_key(current_key.data(), current_key.size());

404 405 406 407 408
    if (sv_ == nullptr) {
      RebuildIterators(true);
    } else {
      RenewIterators();
    }
L
Lei Jin 已提交
409 410 411 412 413 414
    SeekInternal(old_key, false);
    if (!valid_ || key().compare(old_key) != 0) {
      return;
    }
  } else if (current_ != mutable_iter_) {
    // It is going to advance immutable iterator
415 416 417 418 419 420

    if (is_prev_set_ && prefix_extractor_) {
      // advance prev_key_ to current_ only if they share the same prefix
      update_prev_key =
        prefix_extractor_->Transform(prev_key_.GetKey()).compare(
          prefix_extractor_->Transform(current_->key())) == 0;
421 422
    } else {
      update_prev_key = true;
423 424
    }

425

426 427 428 429 430
    if (update_prev_key) {
      prev_key_.SetKey(current_->key());
      is_prev_set_ = true;
      is_prev_inclusive_ = false;
    }
L
Lei Jin 已提交
431 432 433
  }

  current_->Next();
434
  if (current_ != mutable_iter_) {
435 436
    if (!current_->status().ok()) {
      immutable_status_ = current_->status();
437
    } else if ((current_->Valid()) && (!IsOverUpperBound(current_->key()))) {
438
      immutable_min_heap_.push(current_);
439 440 441 442 443 444
    } else {
      if ((current_->Valid()) && (IsOverUpperBound(current_->key()))) {
        // remove the current iterator
        DeleteCurrentIter();
        current_ = nullptr;
      }
445
      if (update_prev_key) {
446
        mutable_iter_->Seek(prev_key_.GetKey());
447
      }
448
    }
L
Lei Jin 已提交
449 450
  }
  UpdateCurrent();
451
  TEST_SYNC_POINT_CALLBACK("ForwardIterator::Next:Return", this);
L
Lei Jin 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
}

Slice ForwardIterator::key() const {
  assert(valid_);
  return current_->key();
}

Slice ForwardIterator::value() const {
  assert(valid_);
  return current_->value();
}

Status ForwardIterator::status() const {
  if (!status_.ok()) {
    return status_;
  } else if (!mutable_iter_->status().ok()) {
    return mutable_iter_->status();
  }
470

471
  return immutable_status_;
L
Lei Jin 已提交
472 473
}

474
void ForwardIterator::RebuildIterators(bool refresh_sv) {
L
Lei Jin 已提交
475
  // Clean up
476 477 478 479 480
  Cleanup(refresh_sv);
  if (refresh_sv) {
    // New
    sv_ = cfd_->GetReferencedSuperVersion(&(db_->mutex_));
  }
481 482
  mutable_iter_ = sv_->mem->NewIterator(read_options_, &arena_);
  sv_->imm->AddIterators(read_options_, &imm_iters_, &arena_);
483
  has_iter_trimmed_for_upper_bound_ = false;
S
sdong 已提交
484

S
sdong 已提交
485
  const auto* vstorage = sv_->current->storage_info();
S
sdong 已提交
486
  const auto& l0_files = vstorage->LevelFiles(0);
L
Lei Jin 已提交
487 488
  l0_iters_.reserve(l0_files.size());
  for (const auto* l0 : l0_files) {
489 490 491 492 493 494 495
    if ((read_options_.iterate_upper_bound != nullptr) &&
        cfd_->internal_comparator().user_comparator()->Compare(
            l0->smallest.user_key(), *read_options_.iterate_upper_bound) > 0) {
      has_iter_trimmed_for_upper_bound_ = true;
      l0_iters_.push_back(nullptr);
      continue;
    }
L
Lei Jin 已提交
496
    l0_iters_.push_back(cfd_->table_cache()->NewIterator(
497
        read_options_, *cfd_->soptions(), cfd_->internal_comparator(), l0->fd));
L
Lei Jin 已提交
498
  }
499
  level_iters_.reserve(vstorage->num_levels() - 1);
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
  BuildLevelIterators(vstorage);
  current_ = nullptr;
  is_prev_set_ = false;
}

void ForwardIterator::RenewIterators() {
  SuperVersion* svnew;
  assert(sv_);
  svnew = cfd_->GetReferencedSuperVersion(&(db_->mutex_));

  if (mutable_iter_ != nullptr) {
    mutable_iter_->~InternalIterator();
  }
  for (auto* m : imm_iters_) {
    m->~InternalIterator();
  }
  imm_iters_.clear();

  mutable_iter_ = svnew->mem->NewIterator(read_options_, &arena_);
  svnew->imm->AddIterators(read_options_, &imm_iters_, &arena_);

  const auto* vstorage = sv_->current->storage_info();
  const auto& l0_files = vstorage->LevelFiles(0);
  const auto* vstorage_new = svnew->current->storage_info();
  const auto& l0_files_new = vstorage_new->LevelFiles(0);
  uint32_t iold, inew;
  bool found;
  std::vector<InternalIterator*> l0_iters_new;
  l0_iters_new.reserve(l0_files_new.size());

  for (inew = 0; inew < l0_files_new.size(); inew++) {
    found = false;
    for (iold = 0; iold < l0_files.size(); iold++) {
      if (l0_files[iold] == l0_files_new[inew]) {
        found = true;
        break;
      }
    }
    if (found) {
      if (l0_iters_[iold] == nullptr) {
        l0_iters_new.push_back(nullptr);
        TEST_SYNC_POINT_CALLBACK("ForwardIterator::RenewIterators:Null", this);
      } else {
        l0_iters_new.push_back(l0_iters_[iold]);
        l0_iters_[iold] = nullptr;
        TEST_SYNC_POINT_CALLBACK("ForwardIterator::RenewIterators:Copy", this);
      }
      continue;
    }
    l0_iters_new.push_back(cfd_->table_cache()->NewIterator(
        read_options_, *cfd_->soptions(), cfd_->internal_comparator(),
        l0_files_new[inew]->fd));
  }

  for (auto* f : l0_iters_) {
    delete f;
  }
  l0_iters_.clear();
  l0_iters_ = l0_iters_new;

  for (auto* l : level_iters_) {
    delete l;
  }
  BuildLevelIterators(vstorage_new);
  current_ = nullptr;
  is_prev_set_ = false;
  SVCleanup();
  sv_ = svnew;
}

void ForwardIterator::BuildLevelIterators(const VersionStorageInfo* vstorage) {
571
  for (int32_t level = 1; level < vstorage->num_levels(); ++level) {
S
sdong 已提交
572
    const auto& level_files = vstorage->LevelFiles(level);
573 574 575 576 577
    if ((level_files.empty()) ||
        ((read_options_.iterate_upper_bound != nullptr) &&
         (user_comparator_->Compare(*read_options_.iterate_upper_bound,
                                    level_files[0]->smallest.user_key()) <
          0))) {
578
      level_iters_[level - 1] = nullptr;
579 580 581
      if (!level_files.empty()) {
        has_iter_trimmed_for_upper_bound_ = true;
      }
L
Lei Jin 已提交
582
    } else {
583 584
      level_iters_[level - 1] =
          new LevelIterator(cfd_, read_options_, level_files);
L
Lei Jin 已提交
585 586 587 588
    }
  }
}

589
void ForwardIterator::ResetIncompleteIterators() {
S
sdong 已提交
590
  const auto& l0_files = sv_->current->storage_info()->LevelFiles(0);
591 592
  for (uint32_t i = 0; i < l0_iters_.size(); ++i) {
    assert(i < l0_files.size());
593
    if (!l0_iters_[i] || !l0_iters_[i]->status().IsIncomplete()) {
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
      continue;
    }
    delete l0_iters_[i];
    l0_iters_[i] = cfd_->table_cache()->NewIterator(
        read_options_, *cfd_->soptions(), cfd_->internal_comparator(),
        l0_files[i]->fd);
  }

  for (auto* level_iter : level_iters_) {
    if (level_iter && level_iter->status().IsIncomplete()) {
      level_iter->Reset();
    }
  }

  current_ = nullptr;
  is_prev_set_ = false;
}

L
Lei Jin 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624
void ForwardIterator::UpdateCurrent() {
  if (immutable_min_heap_.empty() && !mutable_iter_->Valid()) {
    current_ = nullptr;
  } else if (immutable_min_heap_.empty()) {
    current_ = mutable_iter_;
  } else if (!mutable_iter_->Valid()) {
    current_ = immutable_min_heap_.top();
    immutable_min_heap_.pop();
  } else {
    current_ = immutable_min_heap_.top();
    assert(current_ != nullptr);
    assert(current_->Valid());
    int cmp = cfd_->internal_comparator().InternalKeyComparator::Compare(
L
Lei Jin 已提交
625
        mutable_iter_->key(), current_->key());
L
Lei Jin 已提交
626 627 628 629 630 631 632 633 634 635 636
    assert(cmp != 0);
    if (cmp > 0) {
      immutable_min_heap_.pop();
    } else {
      current_ = mutable_iter_;
    }
  }
  valid_ = (current_ != nullptr);
  if (!status_.ok()) {
    status_ = Status::OK();
  }
637 638 639 640 641 642 643

  // Upper bound doesn't apply to the memtable iterator. We want Valid() to
  // return false when all iterators are over iterate_upper_bound, but can't
  // just set valid_ to false, as that would effectively disable the tailing
  // optimization (Seek() would be called on all immutable iterators regardless
  // of whether the target key is greater than prev_key_).
  current_over_upper_bound_ = valid_ && IsOverUpperBound(current_->key());
L
Lei Jin 已提交
644 645 646
}

bool ForwardIterator::NeedToSeekImmutable(const Slice& target) {
647 648 649 650 651 652 653
  // We maintain the interval (prev_key_, immutable_min_heap_.top()->key())
  // such that there are no records with keys within that range in
  // immutable_min_heap_. Since immutable structures (SST files and immutable
  // memtables) can't change in this version, we don't need to do a seek if
  // 'target' belongs to that interval (immutable_min_heap_.top() is already
  // at the correct position).

654
  if (!valid_ || !current_ || !is_prev_set_ || !immutable_status_.ok()) {
L
Lei Jin 已提交
655 656 657 658 659 660 661 662
    return true;
  }
  Slice prev_key = prev_key_.GetKey();
  if (prefix_extractor_ && prefix_extractor_->Transform(target).compare(
    prefix_extractor_->Transform(prev_key)) != 0) {
    return true;
  }
  if (cfd_->internal_comparator().InternalKeyComparator::Compare(
663
        prev_key, target) >= (is_prev_inclusive_ ? 1 : 0)) {
L
Lei Jin 已提交
664 665
    return true;
  }
666 667 668 669 670 671 672 673

  if (immutable_min_heap_.empty() && current_ == mutable_iter_) {
    // Nothing to seek on.
    return false;
  }
  if (cfd_->internal_comparator().InternalKeyComparator::Compare(
        target, current_ == mutable_iter_ ? immutable_min_heap_.top()->key()
                                          : current_->key()) > 0) {
L
Lei Jin 已提交
674 675 676 677 678
    return true;
  }
  return false;
}

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
void ForwardIterator::DeleteCurrentIter() {
  const VersionStorageInfo* vstorage = sv_->current->storage_info();
  const std::vector<FileMetaData*>& l0 = vstorage->LevelFiles(0);
  for (uint32_t i = 0; i < l0.size(); ++i) {
    if (!l0_iters_[i]) {
      continue;
    }
    if (l0_iters_[i] == current_) {
      has_iter_trimmed_for_upper_bound_ = true;
      delete l0_iters_[i];
      l0_iters_[i] = nullptr;
      return;
    }
  }

  for (int32_t level = 1; level < vstorage->num_levels(); ++level) {
    if (level_iters_[level - 1] == nullptr) {
      continue;
    }
    if (level_iters_[level - 1] == current_) {
      has_iter_trimmed_for_upper_bound_ = true;
      delete level_iters_[level - 1];
      level_iters_[level - 1] = nullptr;
    }
  }
}

706 707 708 709 710
bool ForwardIterator::TEST_CheckDeletedIters(int* pdeleted_iters,
                                             int* pnum_iters) {
  bool retval = false;
  int deleted_iters = 0;
  int num_iters = 0;
711 712 713 714 715

  const VersionStorageInfo* vstorage = sv_->current->storage_info();
  const std::vector<FileMetaData*>& l0 = vstorage->LevelFiles(0);
  for (uint32_t i = 0; i < l0.size(); ++i) {
    if (!l0_iters_[i]) {
716 717 718 719
      retval = true;
      deleted_iters++;
    } else {
      num_iters++;
720 721 722 723 724 725
    }
  }

  for (int32_t level = 1; level < vstorage->num_levels(); ++level) {
    if ((level_iters_[level - 1] == nullptr) &&
        (!vstorage->LevelFiles(level).empty())) {
726 727 728 729
      retval = true;
      deleted_iters++;
    } else if (!vstorage->LevelFiles(level).empty()) {
      num_iters++;
730 731
    }
  }
732 733 734 735 736 737 738 739 740 741
  if ((!retval) && num_iters <= 1) {
    retval = true;
  }
  if (pdeleted_iters) {
    *pdeleted_iters = deleted_iters;
  }
  if (pnum_iters) {
    *pnum_iters = num_iters;
  }
  return retval;
742
}
743

L
Lei Jin 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
uint32_t ForwardIterator::FindFileInRange(
    const std::vector<FileMetaData*>& files, const Slice& internal_key,
    uint32_t left, uint32_t right) {
  while (left < right) {
    uint32_t mid = (left + right) / 2;
    const FileMetaData* f = files[mid];
    if (cfd_->internal_comparator().InternalKeyComparator::Compare(
          f->largest.Encode(), internal_key) < 0) {
      // Key at "mid.largest" is < "target".  Therefore all
      // files at or before "mid" are uninteresting.
      left = mid + 1;
    } else {
      // Key at "mid.largest" is >= "target".  Therefore all files
      // after "mid" are uninteresting.
      right = mid;
    }
  }
  return right;
}

}  // namespace rocksdb

#endif  // ROCKSDB_LITE