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 {
82 };
83 std::list<Node> m_queries;
84 std::unordered_map<QString, std::list<Node>::iterator> m_keys;
85 QTimer m_cleanupTimer;
86};
87
88QThreadStorage<Cache *> g_queryCache;
89
90Cache *perThreadCache()
91{
92 if (!g_queryCache.hasLocalData()) {
93 g_queryCache.setLocalData(new Cache());
94 }
95
96 return g_queryCache.localData();
97}
98
99} // namespace
100
101std::optional<QSqlQuery> QueryCache::query(const QString &queryStatement)
102{
103 return perThreadCache()->query(queryStatement);
104}
105
106void QueryCache::insert(const QSqlDatabase &db, const QString &queryStatement, QSqlQuery query)
107{
108 if (DbType::type(db) != DbType::Sqlite) {
109 perThreadCache()->insert(queryStatement, std::move(query));
110 }
111}
112
114{
115 if (!g_queryCache.hasLocalData()) {
116 return;
117 }
118
119 g_queryCache.localData()->cleanup();
120}
121
123{
124 return MaxCacheSize;
125}
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)
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 Fri Oct 11 2024 12:11:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.