File indexing completed on 2025-01-05 04:46:55

0001 /*
0002  * SPDX-FileCopyrightText: 2014 Daniel Vrátil <dvratil@redhat.com>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  *
0006  */
0007 
0008 #pragma once
0009 
0010 #include <QHash>
0011 #include <QMutex>
0012 
0013 namespace Akonadi
0014 {
0015 namespace Server
0016 {
0017 class QueryBuilder;
0018 class Collection;
0019 
0020 /**
0021  * Provides cache for collection statistics
0022  *
0023  * Collection statistics are requested very often, so to take some load from the
0024  * database we cache the results until the statistics are invalidated (see
0025  * NotificationCollector, which takes care for invalidating the statistics).
0026  *
0027  * The cache (together with optimization of the actual SQL query) seems to
0028  * massively improve initial folder listing on system start (when IO and CPU loads
0029  * are very high).
0030  */
0031 class CollectionStatistics
0032 {
0033 public:
0034     struct Statistics {
0035         qint64 count;
0036         qint64 size;
0037         qint64 read;
0038     };
0039 
0040     explicit CollectionStatistics(bool prefetch = true);
0041     virtual ~CollectionStatistics() = default;
0042 
0043     Statistics statistics(const Collection &col);
0044 
0045     void itemAdded(const Collection &col, qint64 size, bool seen);
0046     void itemsSeenChanged(const Collection &col, qint64 seenCount);
0047 
0048     void invalidateCollection(const Collection &col);
0049 
0050     void expireCache();
0051 
0052 protected:
0053     QueryBuilder prepareGenericQuery();
0054 
0055     virtual Statistics calculateCollectionStatistics(const Collection &col);
0056 
0057     QMutex mCacheLock;
0058     QHash<qint64, Statistics> mCache;
0059 };
0060 
0061 } // namespace Server
0062 } // namespace Akonadi