File indexing completed on 2024-11-10 04:40:53

0001 /*
0002     SPDX-FileCopyrightText: 2013, 2014 Daniel Vrátil <dvratil@redhat.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akthread.h"
0010 
0011 #include "agentmanagerinterface.h"
0012 #include "exception.h"
0013 #include <QList>
0014 #include <QMap>
0015 #include <QMutex>
0016 #include <QSet>
0017 #include <QStringList>
0018 #include <QWaitCondition>
0019 
0020 namespace Akonadi
0021 {
0022 namespace Server
0023 {
0024 class Connection;
0025 class AgentSearchInstance;
0026 
0027 class SearchTask
0028 {
0029 public:
0030     QByteArray id;
0031     QString query;
0032     QStringList mimeTypes;
0033     QList<qint64> collections;
0034     bool complete;
0035 
0036     QMutex sharedLock;
0037     QWaitCondition notifier;
0038 
0039     QList<QPair<QString /* resource */, qint64 /* collection */>> queries;
0040     QSet<qint64> pendingResults;
0041 };
0042 
0043 class SearchTaskManager : public AkThread
0044 {
0045     Q_OBJECT
0046 
0047 protected:
0048     /**
0049      * Use AkThread::create() to create and start a new SearchTaskManager thread.
0050      */
0051     explicit SearchTaskManager();
0052 
0053 public:
0054     ~SearchTaskManager() override;
0055 
0056     void registerInstance(const QString &id);
0057     void unregisterInstance(const QString &id);
0058 
0059     void addTask(SearchTask *task);
0060 
0061     void pushResults(const QByteArray &searchId, const QSet<qint64> &ids, Connection *connection);
0062 
0063 private Q_SLOTS:
0064     void searchLoop();
0065 
0066 private:
0067     class ResourceTask
0068     {
0069     public:
0070         QString resourceId;
0071         qint64 collectionId;
0072         SearchTask *parentTask;
0073         QSet<qint64> results;
0074 
0075         qint64 timestamp;
0076     };
0077 
0078     using TasksMap = QMap<QString, ResourceTask *>;
0079 
0080     bool mShouldStop;
0081 
0082     TasksMap::Iterator cancelRunningTask(TasksMap::Iterator &iter);
0083     bool allResourceTasksCompleted(SearchTask *agentSearchTask) const;
0084 
0085     QMap<QString, AgentSearchInstance *> mInstances;
0086     QMutex mInstancesLock;
0087 
0088     QWaitCondition mWait;
0089     QMutex mLock;
0090 
0091     QList<SearchTask *> mTasklist;
0092 
0093     QMap<QString /* resource */, ResourceTask *> mRunningTasks;
0094     QList<ResourceTask *> mPendingResults;
0095 };
0096 
0097 AKONADI_EXCEPTION_MAKE_INSTANCE(SearchException);
0098 
0099 } // namespace Server
0100 } // namespace Akonadi