access/qnetworkaccesscache.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6enum ExpiryTimeEnum { -
7 ExpiryTime = 120 -
8}; -
9 -
10namespace { -
11 struct Receiver -
12 { -
13 QPointer<QObject> object; -
14 const char *member; -
15 }; -
16} -
17 -
18 -
19struct QNetworkAccessCache::Node -
20{ -
21 QDateTime timestamp; -
22 QQueue<Receiver> receiverQueue; -
23 QByteArray key; -
24 -
25 Node *older, *newer; -
26 CacheableObject *object; -
27 -
28 int useCount; -
29 -
30 Node() -
31 : older(0), newer(0), object(0), useCount(0) -
32 { }
executed: }
Execution Count:848
848
33}; -
34 -
35QNetworkAccessCache::CacheableObject::CacheableObject() -
36{ -
37 -
38 -
39} -
40 -
41QNetworkAccessCache::CacheableObject::~CacheableObject() -
42{ -
43 -
44 -
45 -
46 -
47 -
48} -
49 -
50void QNetworkAccessCache::CacheableObject::setExpires(bool enable) -
51{ -
52 expires = enable; -
53}
executed: }
Execution Count:857
857
54 -
55void QNetworkAccessCache::CacheableObject::setShareable(bool enable) -
56{ -
57 shareable = enable; -
58}
executed: }
Execution Count:857
857
59 -
60QNetworkAccessCache::QNetworkAccessCache() -
61 : oldest(0), newest(0) -
62{ -
63}
executed: }
Execution Count:675
675
64 -
65QNetworkAccessCache::~QNetworkAccessCache() -
66{ -
67 clear(); -
68}
executed: }
Execution Count:675
675
69 -
70void QNetworkAccessCache::clear() -
71{ -
72 NodeHash hashCopy = hash; -
73 hash.clear(); -
74 -
75 -
76 NodeHash::Iterator it = hashCopy.begin(); -
77 NodeHash::Iterator end = hashCopy.end(); -
78 for ( ; it != end; ++it) {
evaluated: it != end
TRUEFALSE
yes
Evaluation Count:846
yes
Evaluation Count:3027
846-3027
79 it->object->key.clear(); -
80 it->object->dispose(); -
81 }
executed: }
Execution Count:846
846
82 -
83 -
84 hashCopy.clear(); -
85 -
86 timer.stop(); -
87 -
88 oldest = newest = 0; -
89}
executed: }
Execution Count:3027
3027
90 -
91 -
92 -
93 -
94 -
95void QNetworkAccessCache::linkEntry(const QByteArray &key) -
96{ -
97 NodeHash::Iterator it = hash.find(key); -
98 if (it == hash.end())
partially evaluated: it == hash.end()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:631
0-631
99 return;
never executed: return;
0
100 -
101 Node *const node = &it.value(); -
102 qt_noop(); -
103 qt_noop(); -
104 qt_noop(); -
105 -
106 if (newest) {
evaluated: newest
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:628
3-628
107 qt_noop(); -
108 newest->newer = node; -
109 node->older = newest; -
110 }
executed: }
Execution Count:3
3
111 if (!oldest) {
evaluated: !oldest
TRUEFALSE
yes
Evaluation Count:628
yes
Evaluation Count:3
3-628
112 -
113 oldest = node; -
114 }
executed: }
Execution Count:628
628
115 -
116 node->timestamp = QDateTime::currentDateTime().addSecs(ExpiryTime); -
117 newest = node; -
118}
executed: }
Execution Count:631
631
119 -
120 -
121 -
122 -
123 -
124bool QNetworkAccessCache::unlinkEntry(const QByteArray &key) -
125{ -
126 NodeHash::Iterator it = hash.find(key); -
127 if (it == hash.end())
evaluated: it == hash.end()
TRUEFALSE
yes
Evaluation Count:848
yes
Evaluation Count:100
100-848
128 return false;
executed: return false;
Execution Count:848
848
129 -
130 Node *const node = &it.value(); -
131 -
132 bool wasOldest = false; -
133 if (node == oldest) {
evaluated: node == oldest
TRUEFALSE
yes
Evaluation Count:89
yes
Evaluation Count:11
11-89
134 oldest = node->newer; -
135 wasOldest = true; -
136 }
executed: }
Execution Count:89
89
137 if (node == newest)
evaluated: node == newest
TRUEFALSE
yes
Evaluation Count:88
yes
Evaluation Count:12
12-88
138 newest = node->older;
executed: newest = node->older;
Execution Count:88
88
139 if (node->older)
partially evaluated: node->older
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:100
0-100
140 node->older->newer = node->newer;
never executed: node->older->newer = node->newer;
0
141 if (node->newer)
evaluated: node->newer
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:99
1-99
142 node->newer->older = node->older;
executed: node->newer->older = node->older;
Execution Count:1
1
143 -
144 node->newer = node->older = 0; -
145 return wasOldest;
executed: return wasOldest;
Execution Count:100
100
146} -
147 -
148void QNetworkAccessCache::updateTimer() -
149{ -
150 timer.stop(); -
151 -
152 if (!oldest)
evaluated: !oldest
TRUEFALSE
yes
Evaluation Count:88
yes
Evaluation Count:629
88-629
153 return;
executed: return;
Execution Count:88
88
154 -
155 int interval = QDateTime::currentDateTime().secsTo(oldest->timestamp); -
156 if (interval <= 0) {
partially evaluated: interval <= 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:629
0-629
157 interval = 0; -
158 } else {
never executed: }
0
159 -
160 interval = (interval + 15) & ~16; -
161 }
executed: }
Execution Count:629
629
162 -
163 timer.start(interval * 1000, this); -
164}
executed: }
Execution Count:629
629
165 -
166bool QNetworkAccessCache::emitEntryReady(Node *node, QObject *target, const char *member) -
167{ -
168 if (!connect(this, "2""entryReady(QNetworkAccessCache::CacheableObject*)", 0-1
169 target, member, Qt::QueuedConnection))
partially evaluated: !connect(this, "2""entryReady(QNetworkAccessCache::CacheableObject*)", target, member, Qt::QueuedConnection)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
170 return false;
never executed: return false;
0
171 -
172 entryReady(node->object); -
173 disconnect("2""entryReady(QNetworkAccessCache::CacheableObject*)"); -
174 -
175 return true;
executed: return true;
Execution Count:1
1
176} -
177 -
178void QNetworkAccessCache::timerEvent(QTimerEvent *) -
179{ -
180 -
181 QDateTime now = QDateTime::currentDateTime(); -
182 -
183 while (oldest && oldest->timestamp < now) {
never evaluated: oldest
never evaluated: oldest->timestamp < now
0
184 Node *next = oldest->newer; -
185 oldest->object->dispose(); -
186 -
187 hash.remove(oldest->key); -
188 oldest = next; -
189 }
never executed: }
0
190 -
191 -
192 if (oldest)
never evaluated: oldest
0
193 oldest->older = 0;
never executed: oldest->older = 0;
0
194 else -
195 newest = 0;
never executed: newest = 0;
0
196 -
197 updateTimer(); -
198}
never executed: }
0
199 -
200void QNetworkAccessCache::addEntry(const QByteArray &key, CacheableObject *entry) -
201{ -
202 qt_noop(); -
203 -
204 if (unlinkEntry(key))
partially evaluated: unlinkEntry(key)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:857
0-857
205 updateTimer();
never executed: updateTimer();
0
206 -
207 Node &node = hash[key]; -
208 if (node.useCount)
evaluated: node.useCount
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:848
9-848
209 QMessageLogger("access/qnetworkaccesscache.cpp", 255, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::addEntry: overriding active cache entry '%s'", 9
210 key.constData());
executed: QMessageLogger("access/qnetworkaccesscache.cpp", 255, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::addEntry: overriding active cache entry '%s'", key.constData());
Execution Count:9
9
211 if (node.object)
evaluated: node.object
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:848
9-848
212 node.object->dispose();
executed: node.object->dispose();
Execution Count:9
9
213 node.object = entry; -
214 node.object->key = key; -
215 node.key = key; -
216 node.useCount = 1; -
217}
executed: }
Execution Count:857
857
218 -
219bool QNetworkAccessCache::hasEntry(const QByteArray &key) const -
220{ -
221 return hash.contains(key);
executed: return hash.contains(key);
Execution Count:662
662
222} -
223 -
224bool QNetworkAccessCache::requestEntry(const QByteArray &key, QObject *target, const char *member) -
225{ -
226 NodeHash::Iterator it = hash.find(key); -
227 if (it == hash.end())
evaluated: it == hash.end()
TRUEFALSE
yes
Evaluation Count:37
yes
Evaluation Count:1
1-37
228 return false;
executed: return false;
Execution Count:37
37
229 -
230 Node *node = &it.value(); -
231 -
232 if (node->useCount > 0 && !node->object->shareable) {
partially evaluated: node->useCount > 0
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
partially evaluated: !node->object->shareable
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
233 -
234 -
235 qt_noop(); -
236 Receiver receiver; -
237 receiver.object = target; -
238 receiver.member = member; -
239 node->receiverQueue.enqueue(receiver); -
240 -
241 -
242 return true;
executed: return true;
Execution Count:1
1
243 } else { -
244 -
245 if (unlinkEntry(key))
never evaluated: unlinkEntry(key)
0
246 updateTimer();
never executed: updateTimer();
0
247 -
248 ++node->useCount; -
249 return emitEntryReady(node, target, member);
never executed: return emitEntryReady(node, target, member);
0
250 } -
251} -
252 -
253QNetworkAccessCache::CacheableObject *QNetworkAccessCache::requestEntryNow(const QByteArray &key) -
254{ -
255 NodeHash::Iterator it = hash.find(key); -
256 if (it == hash.end())
evaluated: it == hash.end()
TRUEFALSE
yes
Evaluation Count:507
yes
Evaluation Count:562
507-562
257 return 0;
executed: return 0;
Execution Count:507
507
258 if (it->useCount > 0) {
evaluated: it->useCount > 0
TRUEFALSE
yes
Evaluation Count:473
yes
Evaluation Count:89
89-473
259 if (it->object->shareable) {
partially evaluated: it->object->shareable
TRUEFALSE
yes
Evaluation Count:473
no
Evaluation Count:0
0-473
260 ++it->useCount; -
261 return it->object;
executed: return it->object;
Execution Count:473
473
262 } -
263 -
264 -
265 return 0;
never executed: return 0;
0
266 } -
267 -
268 -
269 bool wasOldest = unlinkEntry(key); -
270 ++it->useCount; -
271 -
272 if (wasOldest)
partially evaluated: wasOldest
TRUEFALSE
yes
Evaluation Count:89
no
Evaluation Count:0
0-89
273 updateTimer();
executed: updateTimer();
Execution Count:89
89
274 return it->object;
executed: return it->object;
Execution Count:89
89
275} -
276 -
277void QNetworkAccessCache::releaseEntry(const QByteArray &key) -
278{ -
279 NodeHash::Iterator it = hash.find(key); -
280 if (it == hash.end()) {
partially evaluated: it == hash.end()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1105
0-1105
281 QMessageLogger("access/qnetworkaccesscache.cpp", 327, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::releaseEntry: trying to release key '%s' that is not in cache", -
282 key.constData()); -
283 return;
never executed: return;
0
284 } -
285 -
286 Node *node = &it.value(); -
287 qt_noop(); -
288 -
289 -
290 if (!node->receiverQueue.isEmpty()) {
evaluated: !node->receiverQueue.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1104
1-1104
291 -
292 Receiver receiver; -
293 do { -
294 receiver = node->receiverQueue.dequeue(); -
295 } while (receiver.object.isNull() && !node->receiverQueue.isEmpty());
executed: }
Execution Count:1
partially evaluated: receiver.object.isNull()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
never evaluated: !node->receiverQueue.isEmpty()
0-1
296 -
297 if (!receiver.object.isNull()) {
partially evaluated: !receiver.object.isNull()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
298 emitEntryReady(node, receiver.object, receiver.member); -
299 return;
executed: return;
Execution Count:1
1
300 } -
301 }
never executed: }
0
302 -
303 if (!--node->useCount) {
evaluated: !--node->useCount
TRUEFALSE
yes
Evaluation Count:631
yes
Evaluation Count:473
473-631
304 -
305 if (node->object->expires)
partially evaluated: node->object->expires
TRUEFALSE
yes
Evaluation Count:631
no
Evaluation Count:0
0-631
306 linkEntry(key);
executed: linkEntry(key);
Execution Count:631
631
307 -
308 if (oldest == node)
evaluated: oldest == node
TRUEFALSE
yes
Evaluation Count:628
yes
Evaluation Count:3
3-628
309 updateTimer();
executed: updateTimer();
Execution Count:628
628
310 }
executed: }
Execution Count:631
631
311}
executed: }
Execution Count:1104
1104
312 -
313void QNetworkAccessCache::removeEntry(const QByteArray &key) -
314{ -
315 NodeHash::Iterator it = hash.find(key); -
316 if (it == hash.end()) {
partially evaluated: it == hash.end()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
317 QMessageLogger("access/qnetworkaccesscache.cpp", 363, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::removeEntry: trying to remove key '%s' that is not in cache", -
318 key.constData()); -
319 return;
never executed: return;
0
320 } -
321 -
322 Node *node = &it.value(); -
323 if (unlinkEntry(key))
partially evaluated: unlinkEntry(key)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
324 updateTimer();
never executed: updateTimer();
0
325 if (node->useCount > 1)
partially evaluated: node->useCount > 1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
0-2
326 QMessageLogger("access/qnetworkaccesscache.cpp", 372, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::removeEntry: removing active cache entry '%s'", 0
327 key.constData());
never executed: QMessageLogger("access/qnetworkaccesscache.cpp", 372, __PRETTY_FUNCTION__).warning("QNetworkAccessCache::removeEntry: removing active cache entry '%s'", key.constData());
0
328 -
329 node->object->key.clear(); -
330 hash.remove(node->key); -
331}
executed: }
Execution Count:2
2
332 -
333 -
334 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial