vector_iterator.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. #pragma once
  3. #include <algorithm>
  4. #include <string>
  5. #include <vector>
  6. #include "db/dbformat.h"
  7. #include "rocksdb/iterator.h"
  8. #include "rocksdb/slice.h"
  9. #include "table/internal_iterator.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. // Iterator over a vector of keys/values
  12. class VectorIterator : public InternalIterator {
  13. public:
  14. VectorIterator(std::vector<std::string> keys, std::vector<std::string> values,
  15. const InternalKeyComparator* icmp)
  16. : keys_(std::move(keys)),
  17. values_(std::move(values)),
  18. indexed_cmp_(icmp, &keys_),
  19. current_(0) {
  20. assert(keys_.size() == values_.size());
  21. indices_.reserve(keys_.size());
  22. for (size_t i = 0; i < keys_.size(); i++) {
  23. indices_.push_back(i);
  24. }
  25. std::sort(indices_.begin(), indices_.end(), indexed_cmp_);
  26. }
  27. virtual bool Valid() const override {
  28. return !indices_.empty() && current_ < indices_.size();
  29. }
  30. virtual void SeekToFirst() override { current_ = 0; }
  31. virtual void SeekToLast() override { current_ = indices_.size() - 1; }
  32. virtual void Seek(const Slice& target) override {
  33. current_ = std::lower_bound(indices_.begin(), indices_.end(), target,
  34. indexed_cmp_) -
  35. indices_.begin();
  36. }
  37. virtual void SeekForPrev(const Slice& target) override {
  38. current_ = std::lower_bound(indices_.begin(), indices_.end(), target,
  39. indexed_cmp_) -
  40. indices_.begin();
  41. if (!Valid()) {
  42. SeekToLast();
  43. } else {
  44. Prev();
  45. }
  46. }
  47. virtual void Next() override { current_++; }
  48. virtual void Prev() override { current_--; }
  49. virtual Slice key() const override {
  50. return Slice(keys_[indices_[current_]]);
  51. }
  52. virtual Slice value() const override {
  53. return Slice(values_[indices_[current_]]);
  54. }
  55. virtual Status status() const override { return Status::OK(); }
  56. virtual bool IsKeyPinned() const override { return true; }
  57. virtual bool IsValuePinned() const override { return true; }
  58. private:
  59. struct IndexedKeyComparator {
  60. IndexedKeyComparator(const InternalKeyComparator* c,
  61. const std::vector<std::string>* ks)
  62. : cmp(c), keys(ks) {}
  63. bool operator()(size_t a, size_t b) const {
  64. return cmp->Compare((*keys)[a], (*keys)[b]) < 0;
  65. }
  66. bool operator()(size_t a, const Slice& b) const {
  67. return cmp->Compare((*keys)[a], b) < 0;
  68. }
  69. bool operator()(const Slice& a, size_t b) const {
  70. return cmp->Compare(a, (*keys)[b]) < 0;
  71. }
  72. const InternalKeyComparator* cmp;
  73. const std::vector<std::string>* keys;
  74. };
  75. std::vector<std::string> keys_;
  76. std::vector<std::string> values_;
  77. IndexedKeyComparator indexed_cmp_;
  78. std::vector<size_t> indices_;
  79. size_t current_;
  80. };
  81. } // namespace ROCKSDB_NAMESPACE