Back to index

python3.2  3.2.2
Classes | Typedefs | Functions | Variables
cache.h File Reference
#include "Python.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _pysqlite_Node
struct  pysqlite_Cache

Typedefs

typedef struct _pysqlite_Node pysqlite_Node

Functions

int pysqlite_node_init (pysqlite_Node *self, PyObject *args, PyObject *kwargs)
void pysqlite_node_dealloc (pysqlite_Node *self)
int pysqlite_cache_init (pysqlite_Cache *self, PyObject *args, PyObject *kwargs)
void pysqlite_cache_dealloc (pysqlite_Cache *self)
PyObjectpysqlite_cache_get (pysqlite_Cache *self, PyObject *args)
int pysqlite_cache_setup_types (void)

Variables

PyTypeObject pysqlite_NodeType
PyTypeObject pysqlite_CacheType

Class Documentation

struct _pysqlite_Node

Definition at line 32 of file cache.h.

Collaboration diagram for _pysqlite_Node:
Class Members
long count
PyObject * data
PyObject_HEAD PyObject * key
struct _pysqlite_Node * next
struct _pysqlite_Node * prev
struct pysqlite_Cache

Definition at line 42 of file cache.h.

Collaboration diagram for pysqlite_Cache:
Class Members
int decref_factory
PyObject * factory
pysqlite_Node * first
pysqlite_Node * last
PyObject * mapping
PyObject_HEAD int size

Typedef Documentation

typedef struct _pysqlite_Node pysqlite_Node

Function Documentation

Definition at line 90 of file cache.c.

{
    pysqlite_Node* node;
    pysqlite_Node* delete_node;

    if (!self->factory) {
        /* constructor failed, just get out of here */
        return;
    }

    /* iterate over all nodes and deallocate them */
    node = self->first;
    while (node) {
        delete_node = node;
        node = node->next;
        Py_DECREF(delete_node);
    }

    if (self->decref_factory) {
        Py_DECREF(self->factory);
    }
    Py_DECREF(self->mapping);

    Py_TYPE(self)->tp_free((PyObject*)self);
}

Definition at line 116 of file cache.c.

{
    PyObject* key = args;
    pysqlite_Node* node;
    pysqlite_Node* ptr;
    PyObject* data;

    node = (pysqlite_Node*)PyDict_GetItem(self->mapping, key);
    if (node) {
        /* an entry for this key already exists in the cache */

        /* increase usage counter of the node found */
        if (node->count < LONG_MAX) {
            node->count++;
        }

        /* if necessary, reorder entries in the cache by swapping positions */
        if (node->prev && node->count > node->prev->count) {
            ptr = node->prev;

            while (ptr->prev && node->count > ptr->prev->count) {
                ptr = ptr->prev;
            }

            if (node->next) {
                node->next->prev = node->prev;
            } else {
                self->last = node->prev;
            }
            if (node->prev) {
                node->prev->next = node->next;
            }
            if (ptr->prev) {
                ptr->prev->next = node;
            } else {
                self->first = node;
            }

            node->next = ptr;
            node->prev = ptr->prev;
            if (!node->prev) {
                self->first = node;
            }
            ptr->prev = node;
        }
    } else {
        /* There is no entry for this key in the cache, yet. We'll insert a new
         * entry in the cache, and make space if necessary by throwing the
         * least used item out of the cache. */

        if (PyDict_Size(self->mapping) == self->size) {
            if (self->last) {
                node = self->last;

                if (PyDict_DelItem(self->mapping, self->last->key) != 0) {
                    return NULL;
                }

                if (node->prev) {
                    node->prev->next = NULL;
                }
                self->last = node->prev;
                node->prev = NULL;

                Py_DECREF(node);
            }
        }

        data = PyObject_CallFunction(self->factory, "O", key);

        if (!data) {
            return NULL;
        }

        node = pysqlite_new_node(key, data);
        if (!node) {
            return NULL;
        }
        node->prev = self->last;

        Py_DECREF(data);

        if (PyDict_SetItem(self->mapping, key, (PyObject*)node) != 0) {
            Py_DECREF(node);
            return NULL;
        }

        if (self->last) {
            self->last->next = node;
        } else {
            self->first = node;
        }
        self->last = node;
    }

    Py_INCREF(node->data);
    return node->data;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int pysqlite_cache_init ( pysqlite_Cache self,
PyObject args,
PyObject kwargs 
)

Definition at line 58 of file cache.c.

{
    PyObject* factory;
    int size = 10;

    self->factory = NULL;

    if (!PyArg_ParseTuple(args, "O|i", &factory, &size)) {
        return -1;
    }

    /* minimum cache size is 5 entries */
    if (size < 5) {
        size = 5;
    }
    self->size = size;
    self->first = NULL;
    self->last = NULL;

    self->mapping = PyDict_New();
    if (!self->mapping) {
        return -1;
    }

    Py_INCREF(factory);
    self->factory = factory;

    self->decref_factory = 1;

    return 0;
}

Here is the call graph for this function:

Definition at line 361 of file cache.c.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 50 of file cache.c.

{
    Py_DECREF(self->key);
    Py_DECREF(self->data);

    Py_TYPE(self)->tp_free((PyObject*)self);
}
int pysqlite_node_init ( pysqlite_Node self,
PyObject args,
PyObject kwargs 
)

Variable Documentation

Definition at line 319 of file cache.c.

Definition at line 277 of file cache.c.