blob: d102e97875054923c4843bebca5627be703430d5 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* cache .c - a LRU cache
2 *
3 * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de>
4 *
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"
25
26/* only used internally */
27Node* new_node(PyObject* key, PyObject* data)
28{
29 Node* node;
30
31 node = (Node*) (NodeType.tp_alloc(&NodeType, 0));
32 if (!node) {
33 return NULL;
34 }
35
36 Py_INCREF(key);
37 node->key = key;
38
39 Py_INCREF(data);
40 node->data = data;
41
42 node->prev = NULL;
43 node->next = NULL;
44
45 return node;
46}
47
48void node_dealloc(Node* self)
49{
50 Py_DECREF(self->key);
51 Py_DECREF(self->data);
52
53 self->ob_type->tp_free((PyObject*)self);
54}
55
56int cache_init(Cache* self, PyObject* args, PyObject* kwargs)
57{
58 PyObject* factory;
59 int size = 10;
60
61 self->factory = NULL;
62
63 if (!PyArg_ParseTuple(args, "O|i", &factory, &size))
64 {
65 return -1;
66 }
67
68 if (size < 5) {
69 size = 5;
70 }
71 self->size = size;
72 self->first = NULL;
73 self->last = NULL;
74
75 self->mapping = PyDict_New();
76 if (!self->mapping) {
77 return -1;
78 }
79
80 Py_INCREF(factory);
81 self->factory = factory;
82
83 self->decref_factory = 1;
84
85 return 0;
86}
87
88void cache_dealloc(Cache* self)
89{
90 Node* node;
91 Node* delete_node;
92
93 if (!self->factory) {
94 /* constructor failed, just get out of here */
95 return;
96 }
97
98 node = self->first;
99 while (node) {
100 delete_node = node;
101 node = node->next;
102 Py_DECREF(delete_node);
103 }
104
105 if (self->decref_factory) {
106 Py_DECREF(self->factory);
107 }
108 Py_DECREF(self->mapping);
109
110 self->ob_type->tp_free((PyObject*)self);
111}
112
113PyObject* cache_get(Cache* self, PyObject* args)
114{
115 PyObject* key = args;
116 Node* node;
117 Node* ptr;
118 PyObject* data;
119
120 node = (Node*)PyDict_GetItem(self->mapping, key);
121 if (node) {
122 node->count++;
123 if (node->prev && node->count > node->prev->count) {
124 ptr = node->prev;
125
126 while (ptr->prev && node->count > ptr->prev->count) {
127 ptr = ptr->prev;
128 }
129
130 if (node->next) {
131 node->next->prev = node->prev;
132 } else {
133 self->last = node->prev;
134 }
135 if (node->prev) {
136 node->prev->next = node->next;
137 }
138 if (ptr->prev) {
139 ptr->prev->next = node;
140 } else {
141 self->first = node;
142 }
143
144 node->next = ptr;
145 node->prev = ptr->prev;
146 if (!node->prev) {
147 self->first = node;
148 }
149 ptr->prev = node;
150 }
151 } else {
152 if (PyDict_Size(self->mapping) == self->size) {
153 if (self->last) {
154 node = self->last;
155
156 if (PyDict_DelItem(self->mapping, self->last->key) != 0) {
157 return NULL;
158 }
159
160 if (node->prev) {
161 node->prev->next = NULL;
162 }
163 self->last = node->prev;
164 node->prev = NULL;
165
166 Py_DECREF(node);
167 }
168 }
169
170 data = PyObject_CallFunction(self->factory, "O", key);
171
172 if (!data) {
173 return NULL;
174 }
175
176 node = new_node(key, data);
177 if (!node) {
178 return NULL;
179 }
180 node->prev = self->last;
181
182 Py_DECREF(data);
183
184 if (PyDict_SetItem(self->mapping, key, (PyObject*)node) != 0) {
185 Py_DECREF(node);
186 return NULL;
187 }
188
189 if (self->last) {
190 self->last->next = node;
191 } else {
192 self->first = node;
193 }
194 self->last = node;
195 }
196
197 Py_INCREF(node->data);
198 return node->data;
199}
200
201PyObject* cache_display(Cache* self, PyObject* args)
202{
203 Node* ptr;
204 PyObject* prevkey;
205 PyObject* nextkey;
206 PyObject* fmt_args;
207 PyObject* template;
208 PyObject* display_str;
209
210 ptr = self->first;
211
212 while (ptr) {
213 if (ptr->prev) {
214 prevkey = ptr->prev->key;
215 } else {
216 prevkey = Py_None;
217 }
218 Py_INCREF(prevkey);
219
220 if (ptr->next) {
221 nextkey = ptr->next->key;
222 } else {
223 nextkey = Py_None;
224 }
225 Py_INCREF(nextkey);
226
227 fmt_args = Py_BuildValue("OOO", prevkey, ptr->key, nextkey);
228 if (!fmt_args) {
229 return NULL;
230 }
231 template = PyString_FromString("%s <- %s ->%s\n");
232 if (!template) {
233 return NULL;
234 }
235 display_str = PyString_Format(template, fmt_args);
236 Py_DECREF(template);
237 Py_DECREF(fmt_args);
238 if (!display_str) {
239 return NULL;
240 }
241 PyObject_Print(display_str, stdout, Py_PRINT_RAW);
242 Py_DECREF(display_str);
243
244 Py_DECREF(prevkey);
245 Py_DECREF(nextkey);
246
247 ptr = ptr->next;
248 }
249
250 Py_INCREF(Py_None);
251 return Py_None;
252}
253
254static PyMethodDef cache_methods[] = {
255 {"get", (PyCFunction)cache_get, METH_O,
256 PyDoc_STR("Gets an entry from the cache.")},
257 {"display", (PyCFunction)cache_display, METH_NOARGS,
258 PyDoc_STR("For debugging only.")},
259 {NULL, NULL}
260};
261
262PyTypeObject NodeType = {
263 PyObject_HEAD_INIT(NULL)
264 0, /* ob_size */
265 MODULE_NAME "Node", /* tp_name */
266 sizeof(Node), /* tp_basicsize */
267 0, /* tp_itemsize */
268 (destructor)node_dealloc, /* tp_dealloc */
269 0, /* tp_print */
270 0, /* tp_getattr */
271 0, /* tp_setattr */
272 0, /* tp_compare */
273 0, /* tp_repr */
274 0, /* tp_as_number */
275 0, /* tp_as_sequence */
276 0, /* tp_as_mapping */
277 0, /* tp_hash */
278 0, /* tp_call */
279 0, /* tp_str */
280 0, /* tp_getattro */
281 0, /* tp_setattro */
282 0, /* tp_as_buffer */
283 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
284 0, /* tp_doc */
285 0, /* tp_traverse */
286 0, /* tp_clear */
287 0, /* tp_richcompare */
288 0, /* tp_weaklistoffset */
289 0, /* tp_iter */
290 0, /* tp_iternext */
291 0, /* tp_methods */
292 0, /* tp_members */
293 0, /* tp_getset */
294 0, /* tp_base */
295 0, /* tp_dict */
296 0, /* tp_descr_get */
297 0, /* tp_descr_set */
298 0, /* tp_dictoffset */
299 (initproc)0, /* tp_init */
300 0, /* tp_alloc */
301 0, /* tp_new */
302 0 /* tp_free */
303};
304
305PyTypeObject CacheType = {
306 PyObject_HEAD_INIT(NULL)
307 0, /* ob_size */
308 MODULE_NAME ".Cache", /* tp_name */
309 sizeof(Cache), /* tp_basicsize */
310 0, /* tp_itemsize */
311 (destructor)cache_dealloc, /* tp_dealloc */
312 0, /* tp_print */
313 0, /* tp_getattr */
314 0, /* tp_setattr */
315 0, /* tp_compare */
316 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 */
342 (initproc)cache_init, /* tp_init */
343 0, /* tp_alloc */
344 0, /* tp_new */
345 0 /* tp_free */
346};
347
348extern int cache_setup_types(void)
349{
350 int rc;
351
352 NodeType.tp_new = PyType_GenericNew;
353 CacheType.tp_new = PyType_GenericNew;
354
355 rc = PyType_Ready(&NodeType);
356 if (rc < 0) {
357 return rc;
358 }
359
360 rc = PyType_Ready(&CacheType);
361 return rc;
362}