File indexing completed on 2024-05-05 04:38:10

0001 /*
0002     SPDX-FileCopyrightText: 2008 David Nolden <david.nolden.kdevelop@art-master.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef ITEMREPOSITORYREGISTRY_H
0008 #define ITEMREPOSITORYREGISTRY_H
0009 
0010 #include "serializationexport.h"
0011 
0012 #include <QScopedPointer>
0013 
0014 class QString;
0015 class QRecursiveMutex;
0016 class QAtomicInt;
0017 
0018 namespace KDevelop {
0019 class ISession;
0020 class AbstractItemRepository;
0021 class ItemRepositoryRegistryPrivate;
0022 
0023 /**
0024  * Manages a set of item-repositories and allows loading/storing them all at once from/to disk.
0025  * Does not automatically store contained repositories on destruction.
0026  * For the global registry, the storing is triggered from within duchain, so you don't need to care about it.
0027  */
0028 class KDEVPLATFORMSERIALIZATION_EXPORT ItemRepositoryRegistry
0029 {
0030 public:
0031     /// Closes all contained repositories.
0032     /// @warning The current state is not stored to disk.
0033     /// @note Currently the destructor is never invoked.
0034     ~ItemRepositoryRegistry();
0035 
0036     /**
0037      * Initialize the global item-repository registry for the given @p session.
0038      */
0039     static void initialize(const QString& repositoryPath);
0040 
0041     /// @returns The global item-repository registry.
0042     static ItemRepositoryRegistry* self();
0043 
0044     /// Deletes the item-repository of a specified session; or, if it is currently used, marks it for deletion at exit.
0045     static void deleteRepositoryFromDisk(const QString& repositoryPath);
0046 
0047     /// Add a new repository.
0048     /// It will automatically be opened with the current path, if one is set.
0049     /// @note A caller of this function, other than @p repository's constructor, must hold @p repository's mutex lock.
0050     void registerRepository(AbstractItemRepository* repository);
0051 
0052     /// Remove a repository.
0053     /// @note Unregistering does not close @p repository.
0054     void unRegisterRepository(AbstractItemRepository* repository);
0055 
0056     /// @returns The path to item-repositories.
0057     QString path() const;
0058 
0059     /// Stores all repositories to disk, eventually unloading unused data to save memory.
0060     /// @note Should be called on a regular basis.
0061     void store();
0062 
0063     /// Indicates that the application has been closed gracefully.
0064     /// @note Must be called somewhere at the end of the shutdown sequence.
0065     void shutdown();
0066 
0067     /// Does a big cleanup, removing all non-persistent items in the repositories.
0068     /// @returns Count of bytes of data that have been removed.
0069     int finalCleanup();
0070 
0071     /// Prints the statistics of all registered item-repositories to the command line using qDebug().
0072     void printAllStatistics() const;
0073 
0074     /// Marks the directory as inconsistent, so it will be discarded
0075     /// on next startup if the application crashes during the write process.
0076     void lockForWriting();
0077 
0078     /// Removes the inconsistency mark set by @ref lockForWriting().
0079     void unlockForWriting();
0080 
0081     /// Returns a custom counter persistently stored as part of item-repositories in the
0082     /// same directory, possibly creating it.
0083     /// @param identity     The string used to identify a counter.
0084     /// @param initialValue Value to initialize a previously inexistent counter with.
0085     QAtomicInt& customCounter(const QString& identity, int initialValue);
0086 
0087     /// @returns The global item-repository mutex.
0088     /// @note    Can be used to protect the initialization.
0089     QRecursiveMutex& mutex();
0090 
0091 private:
0092     explicit ItemRepositoryRegistry(const QString& repositoryPath);
0093 
0094     const QScopedPointer<class ItemRepositoryRegistryPrivate> d_ptr;
0095     Q_DECLARE_PRIVATE(ItemRepositoryRegistry)
0096 
0097     static ItemRepositoryRegistry* m_self;
0098 };
0099 
0100 /// @returns The global item-repository registry (now it is @ref ItemRepositoryRegistry::self()).
0101 KDEVPLATFORMSERIALIZATION_EXPORT ItemRepositoryRegistry& globalItemRepositoryRegistry();
0102 }
0103 
0104 #endif // ITEMREPOSITORYREGISTRY_H