author | Alberto Bertogli
<albertito@gmail.com> 2007-08-28 07:16:57 UTC |
committer | Alberto Bertogli
<albertito@gmail.com> 2007-08-28 07:16:57 UTC |
parent | ea9d44cc184626baade9695235c2cb9c0521bf0c |
bindings/python/nmdb.py | +32 | -2 |
bindings/python/nmdb_ll.c | +41 | -0 |
diff --git a/bindings/python/nmdb.py b/bindings/python/nmdb.py index 77c76b6..38f7366 100644 --- a/bindings/python/nmdb.py +++ b/bindings/python/nmdb.py @@ -167,9 +167,36 @@ class GenericDB (object): oldval, newval) + def generic_incr(self, incrf, key, increment): + """Atomically increment the value associated with the given + key by the given increment.""" + if self.autopickle: + key = str(hash(key)) + r = incrf(key, increment) + if r == 2: + # success + return 2 + elif r == 1: + # no match, because the value didn't have the right + # format + raise TypeError, \ + "The value must be a NULL-terminated string" + elif r == 0: + # not in + raise KeyError + else: + raise NetworkError + + def cache_incr(self, key, increment = 1): + return self.generic_incr(self._db.cache_incr, key, increment) + + def normal_incr(self, key, increment = 1): + return self.generic_incr(self._db.incr, key, increment) + + # The following functions will assume the existance of self.set, - # self.get, self.delete and self.cas, which are supposed to be set - # by our subclasses. + # self.get, and self.delete, which are supposed to be set by our + # subclasses. def __getitem__(self, key): return self.get(key) @@ -201,17 +228,20 @@ class Cache (GenericDB): set = GenericDB.cache_set delete = GenericDB.cache_delete cas = GenericDB.cache_cas + incr = GenericDB.cache_incr class DB (GenericDB): get = GenericDB.normal_get set = GenericDB.normal_set delete = GenericDB.normal_delete cas = GenericDB.normal_cas + incr = GenericDB.normal_incr class SyncDB (GenericDB): get = GenericDB.normal_get set = GenericDB.set_sync delete = GenericDB.delete_sync cas = GenericDB.normal_cas + incr = GenericDB.normal_incr diff --git a/bindings/python/nmdb_ll.c b/bindings/python/nmdb_ll.c index 7aa90a2..2374118 100644 --- a/bindings/python/nmdb_ll.c +++ b/bindings/python/nmdb_ll.c @@ -182,6 +182,26 @@ static PyObject *db_cache_cas(nmdbobject *db, PyObject *args) return PyLong_FromLong(rv); } +/* cache increment */ +static PyObject *db_cache_incr(nmdbobject *db, PyObject *args) +{ + unsigned char *key; + int ksize; + int rv; + long long int increment; + + if (!PyArg_ParseTuple(args, "s#L:cache_incr", &key, &ksize, + &increment)) { + return NULL; + } + + Py_BEGIN_ALLOW_THREADS + rv = nmdb_cache_incr(db->db, key, ksize, increment); + Py_END_ALLOW_THREADS + + return PyLong_FromLong(rv); +} + /* db set */ static PyObject *db_set(nmdbobject *db, PyObject *args) @@ -276,6 +296,25 @@ static PyObject *db_cas(nmdbobject *db, PyObject *args) return PyLong_FromLong(rv); } +/* db increment */ +static PyObject *db_incr(nmdbobject *db, PyObject *args) +{ + unsigned char *key; + int ksize; + int rv; + long long int increment; + + if (!PyArg_ParseTuple(args, "s#L:incr", &key, &ksize, &increment)) { + return NULL; + } + + Py_BEGIN_ALLOW_THREADS + rv = nmdb_incr(db->db, key, ksize, increment); + Py_END_ALLOW_THREADS + + return PyLong_FromLong(rv); +} + /* db set sync */ static PyObject *db_set_sync(nmdbobject *db, PyObject *args) @@ -329,10 +368,12 @@ static PyMethodDef nmdb_methods[] = { { "cache_get", (PyCFunction) db_cache_get, METH_VARARGS, NULL }, { "cache_delete", (PyCFunction) db_cache_delete, METH_VARARGS, NULL }, { "cache_cas", (PyCFunction) db_cache_cas, METH_VARARGS, NULL }, + { "cache_incr", (PyCFunction) db_cache_incr, METH_VARARGS, NULL }, { "set", (PyCFunction) db_set, METH_VARARGS, NULL }, { "get", (PyCFunction) db_get, METH_VARARGS, NULL }, { "delete", (PyCFunction) db_delete, METH_VARARGS, NULL }, { "cas", (PyCFunction) db_cas, METH_VARARGS, NULL }, + { "incr", (PyCFunction) db_incr, METH_VARARGS, NULL }, { "set_sync", (PyCFunction) db_set_sync, METH_VARARGS, NULL }, { "delete_sync", (PyCFunction) db_delete_sync, METH_VARARGS, NULL },