iterator_wrapper_rocks.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  19. // Use of this source code is governed by a BSD-style license that can be
  20. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  21. #pragma once
  22. #include "iterator_rocks.h"
  23. #include "search_slice.h"
  24. #include "search_status.h"
  25. namespace wwsearch {
  26. /* Notice : A internal wrapper class with an interface similar to Iterator that
  27. * caches the valid() and key() results for an underlying iterator.
  28. * This can help avoid virtual function calls and also gives better
  29. * cache locality.
  30. */
  31. class IteratorWrapper {
  32. public:
  33. IteratorWrapper() : iter_(nullptr), valid_(false) {}
  34. explicit IteratorWrapper(Iterator* iter) : iter_(nullptr) { Set(iter); }
  35. ~IteratorWrapper() { delete iter_; }
  36. Iterator* iter() const { return iter_; }
  37. // Takes ownership of "iter" and will delete it when destroyed, or
  38. // when Set() is invoked again.
  39. void Set(Iterator* iter) {
  40. delete iter_;
  41. iter_ = iter;
  42. if (iter_ == nullptr) {
  43. valid_ = false;
  44. } else {
  45. Update();
  46. }
  47. }
  48. // Iterator interface methods
  49. bool Valid() const { return valid_; }
  50. Slice key() const {
  51. assert(Valid());
  52. return key_;
  53. }
  54. Slice value() const {
  55. assert(Valid());
  56. return iter_->value();
  57. }
  58. // Methods below require iter() != nullptr
  59. SearchStatus status() const {
  60. assert(iter_);
  61. return iter_->status();
  62. }
  63. void Next() {
  64. assert(iter_);
  65. iter_->Next();
  66. Update();
  67. }
  68. void Prev() {
  69. assert(iter_);
  70. iter_->Prev();
  71. Update();
  72. }
  73. void Seek(const Slice& k) {
  74. assert(iter_);
  75. iter_->Seek(k);
  76. Update();
  77. }
  78. void SeekForPrev(const Slice& k) {
  79. assert(iter_);
  80. iter_->SeekForPrev(k);
  81. Update();
  82. }
  83. void SeekToFirst() {
  84. assert(iter_);
  85. iter_->SeekToFirst();
  86. Update();
  87. }
  88. void SeekToLast() {
  89. assert(iter_);
  90. iter_->SeekToLast();
  91. Update();
  92. }
  93. private:
  94. void Update() {
  95. valid_ = iter_->Valid();
  96. if (valid_) {
  97. key_ = iter_->key();
  98. }
  99. }
  100. Iterator* iter_;
  101. bool valid_;
  102. Slice key_;
  103. };
  104. } // namespace wwsearch