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

     1  // Copyright 2019 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  #pragma once
    12  
    13  #include <libroach.h>
    14  #include "db.h"
    15  #include "engine.h"
    16  #include "iterator.h"
    17  #include "protos/roachpb/data.pb.h"
    18  #include "status.h"
    19  
    20  // DBIncrementalIterator is the C++ implementation of MVCCIncrementalIterator.
    21  // This implementation should be kept in sync with MVCCIncrementalIterator,
    22  // which is used as an oracle to test DBIncrementalIterator. It may be useful to
    23  // revive in the future when Pebble becomes the main storage engine.
    24  // For general documentation around this iterator please refer to the comments
    25  // in mvcc_incremental_iterator.go.
    26  struct DBIncrementalIterator {
    27    DBIncrementalIterator(DBEngine* engine, DBIterOptions opts, DBKey start, DBKey end,
    28                          DBString* write_intent);
    29    ~DBIncrementalIterator();
    30    DBIterState seek(DBKey key);
    31    DBIterState next(bool skip_current_versions);
    32    const rocksdb::Slice key();
    33    const rocksdb::Slice value();
    34  
    35    std::unique_ptr<DBIterator> iter;
    36  
    37    // The time_bound_iter is used as a performance optimization to potentially
    38    // allow skipping over a large number of keys. The time bound iterator skips
    39    // sstables which do not contain keys modified in a certain interval.
    40    //
    41    // A time-bound iterator cannot be used by itself due to a bug in the time-
    42    // bound iterator (#28358). This was historically augmented with an iterator
    43    // without the time-bound optimization to act as a sanity iterator, but
    44    // issues remained (#43799), so now the iterator above is the main iterator
    45    // the timeBoundIter is used to check if any keys can be skipped by the main
    46    // iterator.
    47    //
    48    // Note regarding the correctness of the time-bound iterator optimization:
    49    //
    50    // When using (t_s, t_e], say there is a version (committed or provisional)
    51    // k@t where t is in that interval, that is visible to iter. All sstables
    52    // containing k@t will be included in timeBoundIter. Note that there may be
    53    // multiple sequence numbers for the key k@t at the storage layer, say k@t#n1,
    54    // k@t#n2, where n1 > n2, some of which may be deleted, but the latest
    55    // sequence number will be visible using iter (since not being visible would be
    56    // a contradiction of the initial assumption that k@t is visible to iter).
    57    // Since there is no delete across all sstables that deletes k@t#n1, there is
    58    // no delete in the subset of sstables used by timeBoundIter that deletes
    59    // k@t#n1, so the timeBoundIter will see k@t.
    60    std::unique_ptr<DBIterator> time_bound_iter = nullptr;
    61  
    62    DBEngine* engine;
    63    DBIterOptions opts;
    64    bool valid;
    65    DBStatus status;
    66    DBKey start, end;
    67    DBString* write_intent;
    68  
    69   private:
    70    rocksdb::Slice sanityCheckMetadataKey();
    71    bool legacyTimestampIsLess(const cockroach::util::hlc::LegacyTimestamp& t1,
    72                               const cockroach::util::hlc::LegacyTimestamp& t2);
    73    DBIterState getState();
    74    void advanceKey();
    75    void maybeSkipKeys();
    76    bool extractKey(rocksdb::Slice mvcc_key, rocksdb::Slice* key);
    77  
    78    cockroach::util::hlc::LegacyTimestamp start_time;
    79    cockroach::util::hlc::LegacyTimestamp end_time;
    80  };