history_trimming_iterator.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  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. #pragma once
  6. #include <string>
  7. #include <vector>
  8. #include "db/dbformat.h"
  9. #include "rocksdb/iterator.h"
  10. #include "rocksdb/slice.h"
  11. #include "table/internal_iterator.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. class HistoryTrimmingIterator : public InternalIterator {
  14. public:
  15. explicit HistoryTrimmingIterator(InternalIterator* input,
  16. const Comparator* cmp, const std::string& ts)
  17. : input_(input), filter_ts_(ts), cmp_(cmp) {
  18. assert(cmp_->timestamp_size() > 0 && !ts.empty());
  19. }
  20. bool filter() const {
  21. if (!input_->Valid()) {
  22. return true;
  23. }
  24. Slice current_ts = ExtractTimestampFromKey(key(), cmp_->timestamp_size());
  25. return cmp_->CompareTimestamp(current_ts, Slice(filter_ts_)) <= 0;
  26. }
  27. bool Valid() const override { return input_->Valid(); }
  28. void SeekToFirst() override {
  29. input_->SeekToFirst();
  30. while (!filter()) {
  31. input_->Next();
  32. }
  33. }
  34. void SeekToLast() override {
  35. input_->SeekToLast();
  36. while (!filter()) {
  37. input_->Prev();
  38. }
  39. }
  40. void Seek(const Slice& target) override {
  41. input_->Seek(target);
  42. while (!filter()) {
  43. input_->Next();
  44. }
  45. }
  46. void SeekForPrev(const Slice& target) override {
  47. input_->SeekForPrev(target);
  48. while (!filter()) {
  49. input_->Prev();
  50. }
  51. }
  52. void Next() override {
  53. do {
  54. input_->Next();
  55. } while (!filter());
  56. }
  57. void Prev() override {
  58. do {
  59. input_->Prev();
  60. } while (!filter());
  61. }
  62. Slice key() const override { return input_->key(); }
  63. Slice value() const override { return input_->value(); }
  64. Status status() const override { return input_->status(); }
  65. bool IsKeyPinned() const override { return input_->IsKeyPinned(); }
  66. bool IsValuePinned() const override { return input_->IsValuePinned(); }
  67. bool IsDeleteRangeSentinelKey() const override {
  68. return input_->IsDeleteRangeSentinelKey();
  69. }
  70. private:
  71. InternalIterator* input_;
  72. const std::string filter_ts_;
  73. const Comparator* const cmp_;
  74. };
  75. } // namespace ROCKSDB_NAMESPACE