blob: 3689a4e387c148692bd8a080dd1f7cbfa2c3d9b2 [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
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000115PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000116{
117 PyObject* key = args;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000118 pysqlite_Node* node;
119 pysqlite_Node* ptr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000120 PyObject* data;
121
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000122 node = (pysqlite_Node*)PyDict_GetItem(self->mapping, key);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 if (node) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 /* an entry for this key already exists in the cache */
125
126 /* increase usage counter of the node found */
127 if (node->count < LONG_MAX) {
128 node->count++;
129 }
130
131 /* if necessary, reorder entries in the cache by swapping positions */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 if (node->prev && node->count > node->prev->count) {
133 ptr = node->prev;
134
135 while (ptr->prev && node->count > ptr->prev->count) {
136 ptr = ptr->prev;
137 }
138
139 if (node->next) {
140 node->next->prev = node->prev;
141 } else {
142 self->last = node->prev;
143 }
144 if (node->prev) {
145 node->prev->next = node->next;
146 }
147 if (ptr->prev) {
148 ptr->prev->next = node;
149 } else {
150 self->first = node;
151 }
152
153 node->next = ptr;
154 node->prev = ptr->prev;
155 if (!node->prev) {
156 self->first = node;
157 }
158 ptr->prev = node;
159 }
160 } else {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000161 /* There is no entry for this key in the cache, yet. We'll insert a new
162 * entry in the cache, and make space if necessary by throwing the
163 * least used item out of the cache. */
164
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000165 if (PyDict_Size(self->mapping) == self->size) {
166 if (self->last) {
167 node = self->last;
168
169 if (PyDict_DelItem(self->mapping, self->last->key) != 0) {
170 return NULL;
171 }
172
173 if (node->prev) {
174 node->prev->next = NULL;
175 }
176 self->last = node->prev;
177 node->prev = NULL;
178
179 Py_DECREF(node);
180 }
181 }
182
183 data = PyObject_CallFunction(self->factory, "O", key);
184
185 if (!data) {
186 return NULL;
187 }
188
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000189 node = pysqlite_new_node(key, data);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 if (!node) {
191 return NULL;
192 }
193 node->prev = self->last;
194
195 Py_DECREF(data);
196
197 if (PyDict_SetItem(self->mapping, key, (PyObject*)node) != 0) {
198 Py_DECREF(node);
199 return NULL;
200 }
201
202 if (self->last) {
203 self->last->next = node;
204 } else {
205 self->first = node;
206 }
207 self->last = node;
208 }
209
210 Py_INCREF(node->data);
211 return node->data;
212}
213
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000214PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000216 pysqlite_Node* ptr;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000217 PyObject* prevkey;
218 PyObject* nextkey;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000219 PyObject* display_str;
220
221 ptr = self->first;
222
223 while (ptr) {
224 if (ptr->prev) {
225 prevkey = ptr->prev->key;
226 } else {
227 prevkey = Py_None;
228 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229
230 if (ptr->next) {
231 nextkey = ptr->next->key;
232 } else {
233 nextkey = Py_None;
234 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000235
Amaury Forgeot d'Arc864741b2011-11-06 15:10:48 +0100236 display_str = PyUnicode_FromFormat("%S <- %S -> %S\n",
237 prevkey, ptr->key, nextkey);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000238 if (!display_str) {
239 return NULL;
240 }
241 PyObject_Print(display_str, stdout, Py_PRINT_RAW);
242 Py_DECREF(display_str);
243
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244 ptr = ptr->next;
245 }
246
247 Py_INCREF(Py_None);
248 return Py_None;
249}
250
251static PyMethodDef cache_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000252 {"get", (PyCFunction)pysqlite_cache_get, METH_O,
Thomas Wouters477c8d52006-05-27 19:21:47 +0000253 PyDoc_STR("Gets an entry from the cache or calls the factory function to produce one.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000254 {"display", (PyCFunction)pysqlite_cache_display, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255 PyDoc_STR("For debugging only.")},
256 {NULL, NULL}
257};
258
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000259PyTypeObject pysqlite_NodeType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000260 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000261 MODULE_NAME "Node", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000262 sizeof(pysqlite_Node), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000264 (destructor)pysqlite_node_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000265 0, /* tp_print */
266 0, /* tp_getattr */
267 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000268 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000269 0, /* tp_repr */
270 0, /* tp_as_number */
271 0, /* tp_as_sequence */
272 0, /* tp_as_mapping */
273 0, /* tp_hash */
274 0, /* tp_call */
275 0, /* tp_str */
276 0, /* tp_getattro */
277 0, /* tp_setattro */
278 0, /* tp_as_buffer */
279 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
280 0, /* tp_doc */
281 0, /* tp_traverse */
282 0, /* tp_clear */
283 0, /* tp_richcompare */
284 0, /* tp_weaklistoffset */
285 0, /* tp_iter */
286 0, /* tp_iternext */
287 0, /* tp_methods */
288 0, /* tp_members */
289 0, /* tp_getset */
290 0, /* tp_base */
291 0, /* tp_dict */
292 0, /* tp_descr_get */
293 0, /* tp_descr_set */
294 0, /* tp_dictoffset */
295 (initproc)0, /* tp_init */
296 0, /* tp_alloc */
297 0, /* tp_new */
298 0 /* tp_free */
299};
300
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000301PyTypeObject pysqlite_CacheType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000302 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000303 MODULE_NAME ".Cache", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000304 sizeof(pysqlite_Cache), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000305 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000306 (destructor)pysqlite_cache_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000307 0, /* tp_print */
308 0, /* tp_getattr */
309 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000310 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000311 0, /* tp_repr */
312 0, /* tp_as_number */
313 0, /* tp_as_sequence */
314 0, /* tp_as_mapping */
315 0, /* tp_hash */
316 0, /* tp_call */
317 0, /* tp_str */
318 0, /* tp_getattro */
319 0, /* tp_setattro */
320 0, /* tp_as_buffer */
321 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
322 0, /* tp_doc */
323 0, /* tp_traverse */
324 0, /* tp_clear */
325 0, /* tp_richcompare */
326 0, /* tp_weaklistoffset */
327 0, /* tp_iter */
328 0, /* tp_iternext */
329 cache_methods, /* tp_methods */
330 0, /* tp_members */
331 0, /* tp_getset */
332 0, /* tp_base */
333 0, /* tp_dict */
334 0, /* tp_descr_get */
335 0, /* tp_descr_set */
336 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000337 (initproc)pysqlite_cache_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 0, /* tp_alloc */
339 0, /* tp_new */
340 0 /* tp_free */
341};
342
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000343extern int pysqlite_cache_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000344{
345 int rc;
346
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000347 pysqlite_NodeType.tp_new = PyType_GenericNew;
348 pysqlite_CacheType.tp_new = PyType_GenericNew;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000349
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000350 rc = PyType_Ready(&pysqlite_NodeType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000351 if (rc < 0) {
352 return rc;
353 }
354
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000355 rc = PyType_Ready(&pysqlite_CacheType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000356 return rc;
357}