Akonadi

querycache.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "querycache.h"
8#include "datastore.h"
9#include "dbtype.h"
10
11#include <QHash>
12#include <QSqlDriver>
13#include <QSqlQuery>
14#include <QThreadStorage>
15#include <QTimer>
16
17#include <chrono>
18#include <list>
19
20using namespace std::chrono_literals;
21using namespace Akonadi;
22using namespace Akonadi::Server;
23
24namespace
25{
26// After these seconds without activity the cache is cleaned
27constexpr auto CleanupTimeout = 60s;
28constexpr int MaxCacheSize = 50;
29
30/// LRU cache with limited size and auto-cleanup after given
31/// period of time
32class Cache
33{
34public:
35 Cache()
36 {
37 QObject::connect(&m_cleanupTimer, &QTimer::timeout, &m_cleanupTimer, std::bind(&Cache::cleanup, this));
38 m_cleanupTimer.setSingleShot(true);
39 }
40
41 std::optional<QSqlQuery> query(const QString &queryStatement)
42 {
43 m_cleanupTimer.start(CleanupTimeout);
44 auto it = m_keys.find(queryStatement);
45 if (it == m_keys.end()) {
46 return {};
47 }
48
49 auto query = std::move(it->second->query);
50 m_queries.erase(it->second);
51 m_keys.erase(it);
52
53 return query;
54 }
55
56 void insert(const QString &queryStatement, QSqlQuery query)
57 {
58 if (m_queries.size() >= MaxCacheSize) {
59 // Get the last entry in m_queries
60 auto query_it = std::prev(m_queries.end());
61 // Find and erase corresponding entry in m_keys
62 std::erase_if(m_keys, [&](const auto &node) {
63 return node.second == query_it;
64 });
65 // Remove the last entry from m_queries, making room for a new one.
66 m_queries.pop_back();
67 }
68
69 m_queries.emplace_front(std::move(query));
70 m_keys.emplace(queryStatement, m_queries.begin());
71 }
72
73 void cleanup()
74 {
75 m_keys.clear();
76 m_queries.clear();
77 }
78
79public: // public, this is just a helper class
80 struct Node {
81 explicit Node(QSqlQuery query)
82 : query(std::move(query))
83 {
84 }
86 };
87 std::list<Node> m_queries;
88 std::unordered_map<QString, std::list<Node>::iterator> m_keys;
89 QTimer m_cleanupTimer;
90};
91
92QThreadStorage<Cache *> g_queryCache;
93
94Cache *perThreadCache()
95{
96 if (!g_queryCache.hasLocalData()) {
97 g_queryCache.setLocalData(new Cache());
98 }
99
100 return g_queryCache.localData();
101}
102
103} // namespace
104
105std::optional<QSqlQuery> QueryCache::query(const QString &queryStatement)
106{
107 return perThreadCache()->query(queryStatement);
108}
109
110void QueryCache::insert(const QSqlDatabase &db, const QString &queryStatement, QSqlQuery query)
111{
112 if (DbType::type(db) != DbType::Sqlite) {
113 perThreadCache()->insert(queryStatement, std::move(query));
114 }
115}
116
118{
119 if (!g_queryCache.hasLocalData()) {
120 return;
121 }
122
123 g_queryCache.localData()->cleanup();
124}
125
127{
128 return MaxCacheSize;
129}
Type type(const QSqlDatabase &db)
Returns the type of the given database object.
Definition dbtype.cpp:11
void insert(const QSqlDatabase &db, const QString &queryStatement, QSqlQuery query)
Insert query into the cache for queryStatement.
void clear()
Clears all queries from current thread.
size_t capacity()
Returns the per-thread capacityof the query cache.
std::optional< QSqlQuery > query(const QString &queryStatement)
Return a cached QSqlQuery for given queryStatement.
Helper integration between Akonadi and Qt.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
KIOCORE_EXPORT CopyJob * move(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
KGuiItem insert()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool hasLocalData() const const
void setLocalData(T data)
void timeout()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:01:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.