secondary_index_iterator.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/utilities/secondary_index.h"
  6. #include "utilities/secondary_index/secondary_index_helper.h"
  7. namespace ROCKSDB_NAMESPACE {
  8. SecondaryIndexIterator::SecondaryIndexIterator(
  9. const SecondaryIndex* index, std::unique_ptr<Iterator>&& underlying_it)
  10. : index_(index), underlying_it_(std::move(underlying_it)) {
  11. assert(index_);
  12. assert(underlying_it_);
  13. }
  14. bool SecondaryIndexIterator::Valid() const {
  15. return status_.ok() && underlying_it_->Valid() &&
  16. underlying_it_->key().starts_with(prefix_);
  17. }
  18. Status SecondaryIndexIterator::status() const {
  19. if (!status_.ok()) {
  20. return status_;
  21. }
  22. return underlying_it_->status();
  23. }
  24. void SecondaryIndexIterator::Seek(const Slice& target) {
  25. status_ = Status::OK();
  26. std::variant<Slice, std::string> prefix = target;
  27. const Status s = index_->FinalizeSecondaryKeyPrefix(&prefix);
  28. if (!s.ok()) {
  29. status_ = s;
  30. return;
  31. }
  32. prefix_ = SecondaryIndexHelper::AsString(prefix);
  33. // FIXME: this works for BytewiseComparator but not for all comparators in
  34. // general
  35. underlying_it_->Seek(prefix_);
  36. }
  37. void SecondaryIndexIterator::Next() {
  38. assert(Valid());
  39. underlying_it_->Next();
  40. }
  41. void SecondaryIndexIterator::Prev() {
  42. assert(Valid());
  43. underlying_it_->Prev();
  44. }
  45. bool SecondaryIndexIterator::PrepareValue() {
  46. assert(Valid());
  47. return underlying_it_->PrepareValue();
  48. }
  49. Slice SecondaryIndexIterator::key() const {
  50. assert(Valid());
  51. Slice key = underlying_it_->key();
  52. key.remove_prefix(prefix_.size());
  53. return key;
  54. }
  55. Slice SecondaryIndexIterator::value() const {
  56. assert(Valid());
  57. return underlying_it_->value();
  58. }
  59. const WideColumns& SecondaryIndexIterator::columns() const {
  60. assert(Valid());
  61. return underlying_it_->columns();
  62. }
  63. Slice SecondaryIndexIterator::timestamp() const {
  64. assert(Valid());
  65. return underlying_it_->timestamp();
  66. }
  67. Status SecondaryIndexIterator::GetProperty(std::string prop_name,
  68. std::string* prop) const {
  69. return underlying_it_->GetProperty(std::move(prop_name), prop);
  70. }
  71. } // namespace ROCKSDB_NAMESPACE