| 1 | /* |
| 2 | This file is part of the KDE Baloo project. |
| 3 | SPDX-FileCopyrightText: 2015 Vishesh Handa <vhanda@kde.org> |
| 4 | SPDX-FileCopyrightText: 2016 Christoph Cullmann <cullmann@kde.org> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 7 | */ |
| 8 | |
| 9 | #ifndef BALOO_DATABASE_H |
| 10 | #define BALOO_DATABASE_H |
| 11 | |
| 12 | #include <QMutex> |
| 13 | |
| 14 | #include "document.h" |
| 15 | #include "databasedbis.h" |
| 16 | |
| 17 | namespace Baloo { |
| 18 | |
| 19 | class DatabaseTest; |
| 20 | |
| 21 | class BALOO_ENGINE_EXPORT Database |
| 22 | { |
| 23 | public: |
| 24 | /** |
| 25 | * Init database for given DB path, will not open it. |
| 26 | * @param path db path |
| 27 | */ |
| 28 | explicit Database(const QString& path); |
| 29 | |
| 30 | /** |
| 31 | * Destruct db, might close it, if opened. |
| 32 | */ |
| 33 | ~Database(); |
| 34 | |
| 35 | /** |
| 36 | * Database open mode |
| 37 | */ |
| 38 | enum OpenMode { |
| 39 | /** |
| 40 | * Create + open read-write database. |
| 41 | */ |
| 42 | CreateDatabase, |
| 43 | |
| 44 | /** |
| 45 | * Read-Write Database, only works if database exists. |
| 46 | */ |
| 47 | ReadWriteDatabase, |
| 48 | |
| 49 | /** |
| 50 | * Read-Only Database, only works if database exists. |
| 51 | */ |
| 52 | ReadOnlyDatabase, |
| 53 | }; |
| 54 | |
| 55 | enum class OpenResult { |
| 56 | Success, |
| 57 | InvalidPath, ///< Database does not exist, or could not be created |
| 58 | InvalidDatabase, ///< Database structure does not match expectation |
| 59 | InternalError, ///< Internal error in the database engine |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * Open database in given mode. |
| 64 | * Nop after open was done (even if mode differs). |
| 65 | * There is no close as this would invalidate the database for all threads using it. |
| 66 | * @param mode create or open only? |
| 67 | */ |
| 68 | OpenResult open(OpenMode mode); |
| 69 | |
| 70 | private: |
| 71 | /** |
| 72 | * serialize access, as open might be called from multiple threads |
| 73 | */ |
| 74 | mutable QMutex m_mutex; |
| 75 | |
| 76 | /** |
| 77 | * database path |
| 78 | */ |
| 79 | const QString m_path; |
| 80 | |
| 81 | MDB_env* m_env; |
| 82 | DatabaseDbis m_dbis; |
| 83 | |
| 84 | friend class Transaction; |
| 85 | friend class DatabaseTest; |
| 86 | |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | #endif // BALOO_DATABASE_H |
| 91 | |