iterator_rocks.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #pragma once
  19. #include "rocksdb/iterator.h"
  20. #include "search_iterator.h"
  21. namespace wwsearch {
  22. /* Need only in using rocksdb.
  23. * A wrapper for rocksdb interator to return SearchStatus.
  24. */
  25. class IteratorRocks : public Iterator {
  26. private:
  27. rocksdb::Iterator* iterator_;
  28. public:
  29. IteratorRocks(rocksdb::Iterator* iterator) : iterator_(iterator) {}
  30. virtual ~IteratorRocks() {
  31. if (iterator_ != nullptr) {
  32. delete iterator_;
  33. iterator_ = nullptr;
  34. }
  35. }
  36. virtual bool Valid() const { return this->iterator_->Valid(); }
  37. virtual void SeekToFirst() { this->iterator_->SeekToFirst(); }
  38. virtual void SeekToLast() { this->iterator_->SeekToLast(); }
  39. virtual void Seek(const Slice& target) {
  40. rocksdb::Slice slice_target(target.data(), target.size());
  41. this->iterator_->Seek(slice_target);
  42. }
  43. virtual void SeekForPrev(const Slice& target) {
  44. rocksdb::Slice slice_target(target.data(), target.size());
  45. this->iterator_->SeekForPrev(slice_target);
  46. }
  47. virtual void Next() { this->iterator_->Next(); }
  48. virtual void Prev() { this->iterator_->Prev(); }
  49. virtual Slice key() const {
  50. auto rocks_slice = this->iterator_->key();
  51. return wwsearch::Slice(rocks_slice.data(), rocks_slice.size());
  52. }
  53. virtual Slice value() const {
  54. auto rocks_slice = this->iterator_->value();
  55. return wwsearch::Slice(rocks_slice.data(), rocks_slice.size());
  56. }
  57. virtual SearchStatus status() const {
  58. auto status = this->iterator_->status();
  59. wwsearch::SearchStatus ss;
  60. if (!status.ok()) {
  61. ss.SetStatus(kRocksDBErrorStatus, status.getState());
  62. }
  63. return ss;
  64. }
  65. private:
  66. };
  67. } // namespace wwsearch