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