Akonadi

querycache.cpp
1 /*
2  Copyright (c) 2012 Volker Krause <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18  */
19 
20 #include "querycache.h"
21 #include "dbtype.h"
22 #include "datastore.h"
23 
24 #include <QSqlQuery>
25 #include <QThreadStorage>
26 #include <QHash>
27 #include <QTimer>
28 
29 #include <chrono>
30 #include <list>
31 
32 using namespace std::chrono_literals;
33 using namespace Akonadi;
34 using namespace Akonadi::Server;
35 
36 namespace {
37 
38 // After these seconds without activity the cache is cleaned
39 static constexpr auto CleanupTimeout = 60s;
40 static constexpr int MaxCacheSize = 50;
41 
44 class Cache
45 {
46 public:
47  Cache()
48  {
49  QObject::connect(&m_cleanupTimer, &QTimer::timeout, std::bind(&Cache::cleanup, this));
50  m_cleanupTimer.setSingleShot(true);
51  }
52 
53  std::optional<QSqlQuery> query(const QString &queryStatement)
54  {
55  m_cleanupTimer.start(CleanupTimeout);
56  auto it = m_keys.find(queryStatement);
57  if (it == m_keys.end()) {
58  return std::nullopt;
59  }
60 
61  auto node = **it;
62  m_queries.erase(*it);
63  m_queries.push_front(node);
64  *it = m_queries.begin();
65  return node.query;
66  }
67 
68  void insert(const QString &queryStatement, const QSqlQuery &query)
69  {
70  if (m_queries.size() >= MaxCacheSize) {
71  m_keys.remove(m_queries.back().queryStatement);
72  m_queries.pop_back();
73  }
74 
75  m_queries.emplace_front(Node{queryStatement, query});
76  m_keys.insert(queryStatement, m_queries.begin());
77  }
78 
79  void cleanup()
80  {
81  m_keys.clear();
82  m_queries.clear();
83  }
84 
85 public: // public, this is just a helper class
86  struct Node {
87  QString queryStatement;
88  QSqlQuery query;
89  };
90  std::list<Node> m_queries;
92  QTimer m_cleanupTimer;
93 };
94 
95 static QThreadStorage<Cache *> g_queryCache;
96 
97 static Cache *perThreadCache()
98 {
99  if (!g_queryCache.hasLocalData()) {
100  g_queryCache.setLocalData(new Cache());
101  }
102 
103  return g_queryCache.localData();
104 }
105 
106 }
107 
108 std::optional<QSqlQuery> QueryCache::query(const QString &queryStatement)
109 {
110  return perThreadCache()->query(queryStatement);
111 }
112 
113 void QueryCache::insert(const QString &queryStatement, const QSqlQuery &query)
114 {
115  if (DbType::type(DataStore::self()->database()) != DbType::Sqlite) {
116  perThreadCache()->insert(queryStatement, query);
117  }
118 }
119 
120 void QueryCache::clear()
121 {
122  if (!g_queryCache.hasLocalData()) {
123  return;
124  }
125 
126  g_queryCache.localData()->cleanup();
127 }
128 
std::optional< QSqlQuery > query(const QString &queryStatement)
Returns the cached (and prepared) query for queryStatement.
Definition: querycache.cpp:108
void setLocalData(T data)
void timeout()
bool hasLocalData() const const
KGuiItem insert()
Helper integration between Akonadi and Qt.
QString::iterator begin()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 24 2020 22:46:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.