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_TRANSACTION_H
9#define BALOO_TRANSACTION_H
10
11#include "databasedbis.h"
12#include "mtimedb.h"
13#include "postingdb.h"
14#include "writetransaction.h"
15#include "documenttimedb.h"
16#include <functional>
17#include <memory>
18
19#include <lmdb.h>
20
21namespace Baloo {
22
23class Database;
24class Document;
25class PostingIterator;
26class EngineQuery;
27class DatabaseSize;
28class DBState;
29
30class BALOO_ENGINE_EXPORT Transaction
31{
32public:
33 enum TransactionType {
34 ReadOnly,
35 ReadWrite,
36 };
37 Transaction(const Database& db, TransactionType type);
38 Transaction(Database* db, TransactionType type);
39 ~Transaction();
40
41 //
42 // Getters
43 //
44 bool hasDocument(quint64 id) const;
45 bool inPhaseOne(quint64 id) const;
46 bool hasFailed(quint64 id) const;
47 QVector<quint64> failedIds(quint64 limit) const;
48 QByteArray documentUrl(quint64 id) const;
49
50 /**
51 * This method is not cheap, and does not stat the filesystem in order to convert the path
52 * \p path into an id.
53 */
54 quint64 documentId(const QByteArray& path) const;
55 QByteArray documentData(quint64 id) const;
56
57 DocumentTimeDB::TimeInfo documentTimeInfo(quint64 id) const;
58
59 PostingIterator* postingIterator(const EngineQuery& query) const;
60 PostingIterator* postingCompIterator(const QByteArray& prefix, qlonglong value, PostingDB::Comparator com) const;
61 PostingIterator* postingCompIterator(const QByteArray& prefix, double value, PostingDB::Comparator com) const;
62 PostingIterator* postingCompIterator(const QByteArray& prefix, const QByteArray& value, PostingDB::Comparator com) const;
63 PostingIterator* mTimeRangeIter(quint32 beginTime, quint32 endTime) const;
64 PostingIterator* docUrlIter(quint64 id) const;
65
66 QVector<quint64> fetchPhaseOneIds(int size) const;
67 uint phaseOneSize() const;
68 uint size() const;
69
70 QVector<QByteArray> fetchTermsStartingWith(const QByteArray& term) const;
71
72 //
73 // Introspecing document data
74 //
75 QVector<QByteArray> documentTerms(quint64 docId) const;
76 QVector<QByteArray> documentFileNameTerms(quint64 docId) const;
77 QVector<QByteArray> documentXattrTerms(quint64 docId) const;
78
79 DatabaseSize dbSize();
80
81 //
82 // Transaction handling
83 //
84 bool commit();
85 void abort();
86 void reset(TransactionType type);
87
88 //
89 // Write Methods
90 //
91 void addDocument(const Document& doc);
92 void removeDocument(quint64 id);
93 void removeRecursively(quint64 parentId);
94 void addFailed(quint64 id);
95
96 bool removeRecursively(quint64 parentId, std::function<bool(quint64)> shouldDelete)
97 {
98 Q_ASSERT(m_txn);
99 Q_ASSERT(m_writeTrans);
100
101 return m_writeTrans->removeRecursively(parentId, shouldDelete);
102 }
103
104 void replaceDocument(const Document& doc, DocumentOperations operations);
105 void setPhaseOne(quint64 id);
106 void removePhaseOne(quint64 id);
107
108 // Debugging
109 void checkFsTree();
110 void checkTermsDbinPostingDb();
111 void checkPostingDbinTermsDb();
112
113private:
114 Transaction(const Transaction& rhs) = delete;
115 void init(TransactionType type);
116
117 const DatabaseDbis& m_dbis;
118 MDB_txn *m_txn = nullptr;
119 MDB_env *m_env = nullptr;
120 std::unique_ptr<WriteTransaction> m_writeTrans;
121
122 friend class DatabaseSanitizerImpl;
123 friend class DBState; // for testing
124};
125}
126
127#endif // BALOO_TRANSACTION_H
128

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