1/*
2 This file is part of the KDE Baloo project.
3 SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#ifndef BALOO_WRITETRANSACTION_H
9#define BALOO_WRITETRANSACTION_H
10
11#include "positioninfo.h"
12#include "document.h"
13#include "documentoperations.h"
14#include "databasedbis.h"
15#include "documenturldb.h"
16#include <functional>
17
18namespace Baloo {
19
20class BALOO_ENGINE_EXPORT WriteTransaction
21{
22public:
23 WriteTransaction(DatabaseDbis dbis, MDB_txn* txn)
24 : m_txn(txn)
25 , m_dbis(dbis)
26 {}
27
28 void addDocument(const Document& doc);
29 void removeDocument(quint64 id);
30
31 /**
32 * Remove the document with id \p parentId and all its children.
33 */
34 void removeRecursively(quint64 parentId);
35
36 /**
37 * Goes through every document in the database, and remove the ones for which \p shouldDelete
38 * returns false. It starts searching from \p parentId, which can be 0 to search
39 * through everything.
40 *
41 * \arg shouldDelete takes a quint64 as a parameter
42 * \ret true if the document (and all its children) has been removed
43 *
44 * This function should typically be called when there are no other ReadTransaction in process
45 * as that would otherwise balloon the size of the database.
46 */
47 bool removeRecursively(quint64 parentId, const std::function<bool(quint64)> &shouldDelete);
48
49 void replaceDocument(const Document& doc, DocumentOperations operations);
50 void commit();
51
52 enum OperationType {
53 AddId,
54 RemoveId,
55 };
56 struct Operation {
57 OperationType type;
58 PositionInfo data;
59 };
60
61private:
62 /*
63 * Adds an 'addId' operation to the pending queue for each term.
64 * Returns the list of all the terms.
65 */
66 BALOO_ENGINE_NO_EXPORT QVector<QByteArray> addTerms(quint64 id, const QMap<QByteArray, Document::TermData>& terms);
67 BALOO_ENGINE_NO_EXPORT QVector<QByteArray> replaceTerms(quint64 id, const QVector<QByteArray>& prevTerms,
68 const QMap<QByteArray, Document::TermData>& terms);
69 BALOO_ENGINE_NO_EXPORT void removeTerms(quint64 id, const QVector<QByteArray>& terms);
70
71 QHash<QByteArray, QVector<Operation> > m_pendingOperations;
72
73 MDB_txn* m_txn;
74 DatabaseDbis m_dbis;
75};
76}
77
78Q_DECLARE_TYPEINFO(Baloo::WriteTransaction::Operation, Q_RELOCATABLE_TYPE);
79
80#endif // BALOO_WRITETRANSACTION_H
81

source code of baloo/src/engine/writetransaction.h