github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/c-deps/libroach/iterator.cc (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  #include "iterator.h"
    12  #include "chunked_buffer.h"
    13  #include "encoding.h"
    14  #include "keys.h"
    15  
    16  using namespace cockroach;
    17  
    18  DBIterator::DBIterator(std::atomic<int64_t>* iters, DBIterOptions iter_options)
    19      : iters_count(iters) {
    20    read_opts.prefix_same_as_start = iter_options.prefix;
    21    read_opts.total_order_seek = !iter_options.prefix;
    22  
    23    SetLowerBound(iter_options.lower_bound);
    24    SetUpperBound(iter_options.upper_bound);
    25    read_opts.iterate_lower_bound = &lower_bound;
    26    read_opts.iterate_upper_bound = &upper_bound;
    27  
    28    if (!EmptyTimestamp(iter_options.min_timestamp_hint) ||
    29        !EmptyTimestamp(iter_options.max_timestamp_hint)) {
    30      assert(!EmptyTimestamp(iter_options.max_timestamp_hint));
    31      const std::string min = EncodeTimestamp(iter_options.min_timestamp_hint);
    32      const std::string max = EncodeTimestamp(iter_options.max_timestamp_hint);
    33      read_opts.table_filter = [min, max, this](const rocksdb::TableProperties& props) {
    34        auto userprops = props.user_collected_properties;
    35        auto tbl_min = userprops.find("crdb.ts.min");
    36        if (tbl_min == userprops.end() || tbl_min->second.empty()) {
    37          if (stats != nullptr) {
    38            ++stats->timebound_num_ssts;
    39          }
    40          return true;
    41        }
    42        auto tbl_max = userprops.find("crdb.ts.max");
    43        if (tbl_max == userprops.end() || tbl_max->second.empty()) {
    44          if (stats != nullptr) {
    45            ++stats->timebound_num_ssts;
    46          }
    47          return true;
    48        }
    49        // If the timestamp range of the table overlaps with the timestamp range we
    50        // want to iterate, the table might contain timestamps we care about.
    51        bool used = max.compare(tbl_min->second) >= 0 && min.compare(tbl_max->second) <= 0;
    52        if (used && stats != nullptr) {
    53          ++stats->timebound_num_ssts;
    54        }
    55        return used;
    56      };
    57    }
    58  
    59    if (iter_options.with_stats) {
    60      stats.reset(new IteratorStats());
    61    }
    62  
    63    ++(*iters_count);
    64  }
    65  
    66  DBIterator::~DBIterator() { --(*iters_count); }
    67  
    68  void DBIterator::SetLowerBound(DBKey key) {
    69    if (key.key.data == NULL) {
    70      lower_bound_str = kMinKey.data();
    71    } else {
    72      lower_bound_str = EncodeKey(key);
    73    }
    74    lower_bound = lower_bound_str;
    75  }
    76  
    77  void DBIterator::SetUpperBound(DBKey key) {
    78    if (key.key.data == NULL) {
    79      upper_bound_str = kMaxKey.data();
    80    } else {
    81      upper_bound_str = EncodeKey(key);
    82    }
    83    upper_bound = upper_bound_str;
    84  }