multi_scan.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "rocksdb/db.h"
  6. namespace ROCKSDB_NAMESPACE {
  7. using MultiScanIterator = MultiScan::MultiScanIterator;
  8. MultiScan::MultiScan(const ReadOptions& read_options,
  9. const MultiScanArgs& scan_opts, DB* db,
  10. ColumnFamilyHandle* cfh)
  11. : read_options_(read_options), scan_opts_(scan_opts), db_(db), cfh_(cfh) {
  12. bool slow_path = false;
  13. // Setup read_options with iterate_uuper_bound based on the first scan.
  14. // Subsequent scans will update and allocate a new DB iterator as necessary
  15. if (scan_opts.GetScanRanges()[0].range.limit) {
  16. upper_bound_ = *scan_opts.GetScanRanges()[0].range.limit;
  17. read_options_.iterate_upper_bound = &upper_bound_;
  18. } else {
  19. read_options_.iterate_upper_bound = nullptr;
  20. }
  21. for (const auto& opts : scan_opts.GetScanRanges()) {
  22. // Check that all the ScanOptions either specify an upper bound or not. If
  23. // its mixed we take the slow path which avoids calling Prepare: we have to
  24. // reallocate the Iterator with updated read_options everytime we switch
  25. // between upper bound or no upper bound, which complicates Prepare.
  26. if (opts.range.limit.has_value() !=
  27. scan_opts.GetScanRanges()[0].range.limit.has_value()) {
  28. slow_path = true;
  29. break;
  30. }
  31. }
  32. db_iter_.reset(db->NewIterator(read_options_, cfh));
  33. if (!slow_path) {
  34. db_iter_->Prepare(scan_opts);
  35. }
  36. }
  37. MultiScanIterator& MultiScanIterator::operator++() {
  38. status_ = db_iter_->status();
  39. if (!status_.ok()) {
  40. throw MultiScanException(status_);
  41. }
  42. if (idx_ >= scan_opts_.size()) {
  43. throw std::logic_error("Index out of range");
  44. }
  45. idx_++;
  46. if (idx_ < scan_opts_.size()) {
  47. // Check if we need to update read_options_
  48. if (scan_opts_[idx_].range.limit.has_value() !=
  49. (read_options_.iterate_upper_bound != nullptr)) {
  50. if (scan_opts_[idx_].range.limit) {
  51. *upper_bound_ = *scan_opts_[idx_].range.limit;
  52. read_options_.iterate_upper_bound = upper_bound_;
  53. } else {
  54. read_options_.iterate_upper_bound = nullptr;
  55. }
  56. db_iter_.reset(db_->NewIterator(read_options_, cfh_));
  57. scan_.Reset(db_iter_.get());
  58. } else if (scan_opts_[idx_].range.limit) {
  59. *upper_bound_ = *scan_opts_[idx_].range.limit;
  60. }
  61. db_iter_->Seek(*scan_opts_[idx_].range.start);
  62. status_ = db_iter_->status();
  63. if (!status_.ok()) {
  64. throw MultiScanException(status_);
  65. }
  66. }
  67. return *this;
  68. }
  69. } // namespace ROCKSDB_NAMESPACE