File indexing completed on 2024-05-12 05:11:21

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  *
0006  */
0007 
0008 #pragma once
0009 
0010 #include "search_xapian_export.h"
0011 #include <xapian.h>
0012 
0013 #include <QList>
0014 #include <QPair>
0015 #include <QString>
0016 
0017 namespace Akonadi
0018 {
0019 namespace Search
0020 {
0021 class XapianDocument;
0022 
0023 /** Xapian database. */
0024 class AKONADI_SEARCH_XAPIAN_EXPORT XapianDatabase
0025 {
0026 public:
0027     /**
0028      * Create the Xapian db at path \p path. The parameter \p
0029      * writeOnly locks the database as long as this object is
0030      * valid
0031      */
0032     explicit XapianDatabase(const QString &path, bool writeOnly = false);
0033     ~XapianDatabase();
0034 
0035     void replaceDocument(uint id, const Xapian::Document &doc);
0036     void replaceDocument(uint id, const XapianDocument &doc);
0037     void deleteDocument(uint id);
0038 
0039     /**
0040      * Commit all the pending changes. This may not commit
0041      * at this instance as the db might be locked by another process
0042      * It emits the committed signal on completion
0043      */
0044     void commit();
0045 
0046     XapianDocument document(uint id);
0047 
0048     /**
0049      * A pointer to the actual db. Only use this when doing queries
0050      */
0051     Xapian::Database *db()
0052     {
0053         if (m_db) {
0054             m_db->reopen();
0055             return m_db;
0056         }
0057         return &m_wDb;
0058     }
0059 
0060     /**
0061      * Returns true if the XapianDatabase has changes which need to
0062      * be committed
0063      */
0064     [[nodiscard]] bool haveChanges() const;
0065 
0066 private:
0067     Xapian::Database *m_db = nullptr;
0068     Xapian::WritableDatabase m_wDb;
0069 
0070     using DocIdPair = QPair<Xapian::docid, Xapian::Document>;
0071     QList<DocIdPair> m_docsToAdd;
0072     QList<uint> m_docsToRemove;
0073 
0074     std::string m_path;
0075     const bool m_writeOnly = false;
0076 
0077     Xapian::WritableDatabase createWritableDb();
0078 };
0079 }
0080 }