blob: 7552d1b145394b242aeb1fe5ededa4cf833ebf5b [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* cache .c - a LRU cache
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
5 * This file is part of pysqlite.
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "cache.h"
Thomas Wouters477c8d52006-05-27 19:21:47 +000025#include <limits.h>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000026
27/* only used internally */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000028pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000029{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000030 pysqlite_Node* node;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000031
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000032 node = (pysqlite_Node*) (pysqlite_NodeType.tp_alloc(&pysqlite_NodeType, 0));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033 if (!node) {
34 return NULL;
35 }
36
37 Py_INCREF(key);
38 node->key = key;
39
40 Py_INCREF(data);
41 node->data = data;
42
43 node->prev = NULL;
44 node->next = NULL;
45
46 return node;
47}
48
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000049void pysqlite_node_dealloc(pysqlite_Node* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050{
51 Py_DECREF(self->key);
52 Py_DECREF(self->data);
53
Christian Heimes90aa7642007-12-19 02:45:37 +000054 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055}
56
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000057int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058{
59 PyObject* factory;
60 int size = 10;
61
62 self->factory = NULL;
63
Thomas Wouters477c8d52006-05-27 19:21:47 +000064 if (!PyArg_ParseTuple(args, "O|i", &factory, &size)) {
65 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066 }
67
Thomas Wouters477c8d52006-05-27 19:21:47 +000068 /* minimum cache size is 5 entries */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069 if (size < 5) {
70 size = 5;
71 }
72 self->size = size;
73 self->first = NULL;
74 self->last = NULL;
75
76 self->mapping = PyDict_New();
77 if (!self->mapping) {
78 return -1;
79 }
80
81 Py_INCREF(factory);
82 self->factory = factory;
83
84 self->decref_factory = 1;
85
86 return 0;
87}
88
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000089void pysqlite_cache_dealloc(pysqlite_Cache* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000090{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000091 pysqlite_Node* node;
92 pysqlite_Node* delete_node;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093
94 if (!self->factory) {
95 /* constructor failed, just get out of here */
96 return;
97 }
98
Thomas Wouters477c8d52006-05-27 19:21:47 +000099 /* iterate over all nodes and deallocate them */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000100 node = self->first;
101 while (node) {
102 delete_node = node;
103 node = node->next;
104 Py_DECREF(delete_node);
105 }
106
107 if (self->decref_factory) {
108 Py_DECREF(self->factory);
109 }
110 Py_DECREF(self->mapping);
111
Christian Heimes90aa7642007-12-19 02:45:37 +0000112 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113}
114
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200115PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000117 pysqlite_Node* node;
118 pysqlite_Node* ptr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 PyObject* data;
120
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200121 node = (pysqlite_Node*)PyDict_GetItemWithError(self->mapping, key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000122 if (node) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000123 /* an entry for this key already exists in the cache */
124
125 /* increase usage counter of the node found */
126 if (node->count < LONG_MAX) {
127 node->count++;
128 }
129
130 /* if necessary, reorder entries in the cache by swapping positions */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131 if (node->prev && node->count > node->prev->count) {
132 ptr = node->prev;
133
134 while (ptr->prev && node->count > ptr->prev->count) {
135 ptr = ptr->prev;
136 }
137
138 if (node->next) {
139 node->next->prev = node->prev;
140 } else {
141 self->last = node->prev;
142 }
143 if (node->prev) {
144 node->prev->next = node->next;
145 }
146 if (ptr->prev) {
147 ptr->prev->next = node;
148 } else {
149 self->first = node;
150 }
151
152 node->next = ptr;
153 node->prev = ptr->prev;
154 if (!node->prev) {
155 self->first = node;
156 }
157 ptr->prev = node;
158 }
Serhiy Storchakafc662ac2018-12-10 16:06:08 +0200159 }
160 else if (PyErr_Occurred()) {
161 return NULL;
162 }
163 else {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000164 /* There is no entry for this key in the cache, yet. We'll insert a new
165 * entry in the cache, and make space if necessary by throwing the
166 * least used item out of the cache. */
167
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200168 if (PyDict_GET_SIZE(self->mapping) == self->size) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169 if (self->last) {
170 node = self->last;
171
172 if (PyDict_DelItem(self->mapping, self->last->key) != 0) {
173 return NULL;
174 }
175
176 if (node->prev) {
177 node->prev->next = NULL;
178 }
179 self->last = node->prev;
180 node->prev = NULL;
181
182 Py_DECREF(node);
183 }
184 }
185
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200186 /* We cannot replace this by _PyObject_CallOneArg() since
187 * PyObject_CallFunction() has a special case when using a
188 * single tuple as argument. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189 data = PyObject_CallFunction(self->factory, "O", key);
190
191 if (!data) {
192 return NULL;
193 }
194
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000195 node = pysqlite_new_node(key, data);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196 if (!node) {
197 return NULL;
198 }
199 node->prev = self->last;
200
201 Py_DECREF(data);
202
203 if (PyDict_SetItem(self->mapping, key, (PyObject*)node) != 0) {
204 Py_DECREF(node);
205 return NULL;
206 }
207
208 if (self->last) {
209 self->last->next = node;
210 } else {
211 self->first = node;
212 }
213 self->last = node;
214 }
215
216 Py_INCREF(node->data);
217 return node->data;
218}
219
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000220PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000222 pysqlite_Node* ptr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000223 PyObject* prevkey;
224 PyObject* nextkey;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225 PyObject* display_str;
226
227 ptr = self->first;
228
229 while (ptr) {
230 if (ptr->prev) {
231 prevkey = ptr->prev->key;
232 } else {
233 prevkey = Py_None;
234 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000235
236 if (ptr->next) {
237 nextkey = ptr->next->key;
238 } else {
239 nextkey = Py_None;
240 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000241
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +0100242 display_str = PyUnicode_FromFormat("%S <- %S -> %S\n",
243 prevkey, ptr->key, nextkey);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244 if (!display_str) {
245 return NULL;
246 }
247 PyObject_Print(display_str, stdout, Py_PRINT_RAW);
248 Py_DECREF(display_str);
249
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250 ptr = ptr->next;
251 }
252
Berker Peksagfe21de92016-04-09 07:34:39 +0300253 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254}
255
256static PyMethodDef cache_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000257 {"get", (PyCFunction)pysqlite_cache_get, METH_O,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000258 PyDoc_STR("Gets an entry from the cache or calls the factory function to produce one.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000259 {"display", (PyCFunction)pysqlite_cache_display, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260 PyDoc_STR("For debugging only.")},
261 {NULL, NULL}
262};
263
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000264PyTypeObject pysqlite_NodeType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000265 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266 MODULE_NAME "Node", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000267 sizeof(pysqlite_Node), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000268 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000269 (destructor)pysqlite_node_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200270 0, /* tp_vectorcall_offset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000271 0, /* tp_getattr */
272 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200273 0, /* tp_as_async */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000274 0, /* tp_repr */
275 0, /* tp_as_number */
276 0, /* tp_as_sequence */
277 0, /* tp_as_mapping */
278 0, /* tp_hash */
279 0, /* tp_call */
280 0, /* tp_str */
281 0, /* tp_getattro */
282 0, /* tp_setattro */
283 0, /* tp_as_buffer */
284 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
285 0, /* tp_doc */
286 0, /* tp_traverse */
287 0, /* tp_clear */
288 0, /* tp_richcompare */
289 0, /* tp_weaklistoffset */
290 0, /* tp_iter */
291 0, /* tp_iternext */
292 0, /* tp_methods */
293 0, /* tp_members */
294 0, /* tp_getset */
295 0, /* tp_base */
296 0, /* tp_dict */
297 0, /* tp_descr_get */
298 0, /* tp_descr_set */
299 0, /* tp_dictoffset */
300 (initproc)0, /* tp_init */
301 0, /* tp_alloc */
302 0, /* tp_new */
303 0 /* tp_free */
304};
305
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000306PyTypeObject pysqlite_CacheType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000307 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000308 MODULE_NAME ".Cache", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000309 sizeof(pysqlite_Cache), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000310 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000311 (destructor)pysqlite_cache_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200312 0, /* tp_vectorcall_offset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313 0, /* tp_getattr */
314 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200315 0, /* tp_as_async */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316 0, /* tp_repr */
317 0, /* tp_as_number */
318 0, /* tp_as_sequence */
319 0, /* tp_as_mapping */
320 0, /* tp_hash */
321 0, /* tp_call */
322 0, /* tp_str */
323 0, /* tp_getattro */
324 0, /* tp_setattro */
325 0, /* tp_as_buffer */
326 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
327 0, /* tp_doc */
328 0, /* tp_traverse */
329 0, /* tp_clear */
330 0, /* tp_richcompare */
331 0, /* tp_weaklistoffset */
332 0, /* tp_iter */
333 0, /* tp_iternext */
334 cache_methods, /* tp_methods */
335 0, /* tp_members */
336 0, /* tp_getset */
337 0, /* tp_base */
338 0, /* tp_dict */
339 0, /* tp_descr_get */
340 0, /* tp_descr_set */
341 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000342 (initproc)pysqlite_cache_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343 0, /* tp_alloc */
344 0, /* tp_new */
345 0 /* tp_free */
346};
347
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000348extern int pysqlite_cache_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000349{
350 int rc;
351
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000352 pysqlite_NodeType.tp_new = PyType_GenericNew;
353 pysqlite_CacheType.tp_new = PyType_GenericNew;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000354
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000355 rc = PyType_Ready(&pysqlite_NodeType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000356 if (rc < 0) {
357 return rc;
358 }
359
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000360 rc = PyType_Ready(&pysqlite_CacheType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000361 return rc;
362}