File indexing completed on 2024-04-21 03:51:43

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 #ifndef BALOO_WRITETRANSACTION_H
0009 #define BALOO_WRITETRANSACTION_H
0010 
0011 #include "positioninfo.h"
0012 #include "document.h"
0013 #include "documentoperations.h"
0014 #include "databasedbis.h"
0015 #include "documenturldb.h"
0016 #include <functional>
0017 
0018 namespace Baloo {
0019 
0020 class BALOO_ENGINE_EXPORT WriteTransaction
0021 {
0022 public:
0023     WriteTransaction(DatabaseDbis dbis, MDB_txn* txn)
0024         : m_txn(txn)
0025         , m_dbis(dbis)
0026     {}
0027 
0028     void addDocument(const Document& doc);
0029     void removeDocument(quint64 id);
0030 
0031     /**
0032      * Remove the document with id \p parentId and all its children.
0033      */
0034     void removeRecursively(quint64 parentId);
0035 
0036     /**
0037      * Goes through every document in the database, and remove the ones for which \p shouldDelete
0038      * returns false. It starts searching from \p parentId, which can be 0 to search
0039      * through everything.
0040      *
0041      * \arg shouldDelete takes a quint64 as a parameter
0042      * \ret true if the document (and all its children) has been removed
0043      *
0044      * This function should typically be called when there are no other ReadTransaction in process
0045      * as that would otherwise balloon the size of the database.
0046      */
0047     bool removeRecursively(quint64 parentId, const std::function<bool(quint64)> &shouldDelete);
0048 
0049     void replaceDocument(const Document& doc, DocumentOperations operations);
0050     void commit();
0051 
0052     enum OperationType {
0053         AddId,
0054         RemoveId,
0055     };
0056     struct Operation {
0057         OperationType type;
0058         PositionInfo data;
0059     };
0060 
0061 private:
0062     /*
0063      * Adds an 'addId' operation to the pending queue for each term.
0064      * Returns the list of all the terms.
0065      */
0066     BALOO_ENGINE_NO_EXPORT QVector<QByteArray> addTerms(quint64 id, const QMap<QByteArray, Document::TermData>& terms);
0067     BALOO_ENGINE_NO_EXPORT QVector<QByteArray> replaceTerms(quint64 id, const QVector<QByteArray>& prevTerms,
0068                                      const QMap<QByteArray, Document::TermData>& terms);
0069     BALOO_ENGINE_NO_EXPORT void removeTerms(quint64 id, const QVector<QByteArray>& terms);
0070 
0071     QHash<QByteArray, QVector<Operation> > m_pendingOperations;
0072 
0073     MDB_txn* m_txn;
0074     DatabaseDbis m_dbis;
0075 };
0076 }
0077 
0078 Q_DECLARE_TYPEINFO(Baloo::WriteTransaction::Operation, Q_RELOCATABLE_TYPE);
0079 
0080 #endif // BALOO_WRITETRANSACTION_H