File indexing completed on 2024-04-28 15:17:29

0001 /*
0002     This file is part of the KDE Baloo project.
0003     SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "andpostingiterator.h"
0009 
0010 using namespace Baloo;
0011 
0012 AndPostingIterator::AndPostingIterator(const QVector<PostingIterator*>& iterators)
0013     : m_iterators(iterators)
0014     , m_docId(0)
0015 {
0016     if (m_iterators.contains(nullptr)) {
0017         qDeleteAll(m_iterators);
0018         m_iterators.clear();
0019     }
0020 }
0021 
0022 AndPostingIterator::~AndPostingIterator()
0023 {
0024     qDeleteAll(m_iterators);
0025 }
0026 
0027 quint64 AndPostingIterator::docId() const
0028 {
0029     return m_docId;
0030 }
0031 
0032 quint64 AndPostingIterator::skipTo(quint64 id)
0033 {
0034     if (m_iterators.isEmpty()) {
0035         m_docId = 0;
0036         return 0;
0037     }
0038 
0039     while (true) {
0040         quint64 lower_bound = id;
0041         for (PostingIterator* iter : std::as_const(m_iterators)) {
0042             lower_bound = iter->skipTo(lower_bound);
0043 
0044             if (lower_bound == 0) {
0045                 m_docId = 0;
0046                 return 0;
0047             }
0048         }
0049 
0050         if (lower_bound == id) {
0051             m_docId = lower_bound;
0052             return lower_bound;
0053         }
0054         id = lower_bound;
0055     }
0056 }
0057 
0058 quint64 AndPostingIterator::next()
0059 {
0060     if (m_iterators.isEmpty()) {
0061         m_docId = 0;
0062         return 0;
0063     }
0064 
0065     m_docId = m_iterators[0]->next();
0066     m_docId = skipTo(m_docId);
0067 
0068     return m_docId;
0069 }