1c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter/* cache.h - definitions for the LRU cache
2c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter *
33bbb67273a8c146a38de91080a37e716e2699622Gerhard Häring * Copyright (C) 2004-2010 Gerhard H�ring <gh@ghaering.de>
4c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter *
5c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * This file is part of pysqlite.
6c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter *
7c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * This software is provided 'as-is', without any express or implied
8c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * warranty.  In no event will the authors be held liable for any damages
9c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * arising from the use of this software.
10c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter *
11c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * Permission is granted to anyone to use this software for any purpose,
12c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * including commercial applications, and to alter it and redistribute it
13c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * freely, subject to the following restrictions:
14c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter *
15c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * 1. The origin of this software must not be misrepresented; you must not
16c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter *    claim that you wrote the original software. If you use this software
17c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter *    in a product, an acknowledgment in the product documentation would be
18c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter *    appreciated but is not required.
19c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * 2. Altered source versions must be plainly marked as such, and must not be
20c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter *    misrepresented as being the original software.
21c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter * 3. This notice may not be removed or altered from any source distribution.
22c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter */
23c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter
24c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter#ifndef PYSQLITE_CACHE_H
25c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter#define PYSQLITE_CACHE_H
26c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter#include "Python.h"
27c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter
283e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring/* The LRU cache is implemented as a combination of a doubly-linked with a
293e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring * dictionary. The list items are of type 'Node' and the dictionary has the
303e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring * nodes as values. */
313e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring
320741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häringtypedef struct _pysqlite_Node
33c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter{
34c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter    PyObject_HEAD
35c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter    PyObject* key;
36c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter    PyObject* data;
37c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter    long count;
380741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häring    struct _pysqlite_Node* prev;
390741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häring    struct _pysqlite_Node* next;
400741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häring} pysqlite_Node;
41c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter
42c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxtertypedef struct
43c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter{
44c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter    PyObject_HEAD
45c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter    int size;
463e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring
473e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring    /* a dictionary mapping keys to Node entries */
48c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter    PyObject* mapping;
493e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring
503e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring    /* the factory callable */
51c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter    PyObject* factory;
523e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring
530741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häring    pysqlite_Node* first;
540741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häring    pysqlite_Node* last;
553e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring
563e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring    /* if set, decrement the factory function when the Cache is deallocated.
573e99c0ad649de0393d9a8af17f34d9d1f55f4ab2Gerhard Häring     * this is almost always desirable, but not in the pysqlite context */
58c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter    int decref_factory;
590741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häring} pysqlite_Cache;
60c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter
610741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häringextern PyTypeObject pysqlite_NodeType;
620741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häringextern PyTypeObject pysqlite_CacheType;
63c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter
640741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häringint pysqlite_node_init(pysqlite_Node* self, PyObject* args, PyObject* kwargs);
650741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häringvoid pysqlite_node_dealloc(pysqlite_Node* self);
66c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter
670741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häringint pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs);
680741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häringvoid pysqlite_cache_dealloc(pysqlite_Cache* self);
690741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard HäringPyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* args);
70c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter
710741a60ca7b332b755d8a6b3328da414f963f7b4Gerhard Häringint pysqlite_cache_setup_types(void);
72c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter
73c51ee69b27a35bb45e501766dd33674eae7ddb30Anthony Baxter#endif
74