merge_iterator.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "doc_iterator.h"
  20. namespace wwsearch {
  21. /* Notice : Get intersection doc list from vector<DocIdSetIterator*>
  22. */
  23. class MergeIterator : public DocIdSetIterator {
  24. private:
  25. std::vector<DocIdSetIterator*> sub_iterator_;
  26. DocumentID curr_;
  27. int field_id_;
  28. public:
  29. MergeIterator() : curr_(NO_MORE_DOCS), field_id_(-1) {}
  30. virtual ~MergeIterator() {}
  31. virtual DocumentID DocID() override;
  32. virtual DocumentID NextDoc() override;
  33. virtual DocumentID Advance(DocumentID target) override;
  34. virtual CostType Cost() override;
  35. virtual int FieldId() override { return field_id_; }
  36. void AddSubIterator(DocIdSetIterator* iterator) {
  37. this->sub_iterator_.push_back(iterator);
  38. }
  39. // must call after AddSubIterator to reach init state.
  40. inline void FinishAddIterator() { curr_ = Advance(MAX_DOCID); }
  41. private:
  42. DocumentID InnderNextDoc(bool use_advance = false,
  43. DocumentID target = NO_MORE_DOCS);
  44. };
  45. } // namespace wwsearch