git » nmdb » commit ac406c9

Make nmdb_*get() return -1 on miss, instead of 0. Breaks the API.

author Alberto Bertogli
2007-06-28 22:51:09 UTC
committer Alberto Bertogli
2007-06-29 05:21:33 UTC
parent 21d53a7a17513f0b75afef9d6d77d794c9998089

Make nmdb_*get() return -1 on miss, instead of 0. Breaks the API.

Without this patch, it's impossible to tell between a miss and a key
with a value of "" (the empty string).

This fixes that by returning -1 (instead of 0) when there's a miss, and -2
(instead of -1) when there's an error.

It updates all bindings and tests.

Too bad it missed the 0.20 release.

Signed-off-by: Alberto Bertogli <albertito@gmail.com>

bindings/d/nmdb.d +2 -2
bindings/newlisp/nmdb.lsp +1 -1
bindings/python/nmdb.py +3 -1
bindings/python/nmdb_ll.c +8 -6
libnmdb/libnmdb.3 +2 -2
libnmdb/libnmdb.c +4 -4
tests/c/1.c +2 -2
tests/c/2.c +2 -2
tests/c/3.c +2 -2
tests/c/get.c +2 -2

diff --git a/bindings/d/nmdb.d b/bindings/d/nmdb.d
index e746d86..d2775cb 100644
--- a/bindings/d/nmdb.d
+++ b/bindings/d/nmdb.d
@@ -83,9 +83,9 @@ class DB
 			throw new Exception("Invalid mode");
 		}
 
-		if (size == 0) {
+		if (size == -1) {
 			throw new KeyNotFound("Key not found: " ~ key);
-		} else if (size < 0) {
+		} else if (size <= -2) {
 			throw new Exception("Can't get value");
 		}
 
diff --git a/bindings/newlisp/nmdb.lsp b/bindings/newlisp/nmdb.lsp
index 187e1dc..2bb1eb3 100644
--- a/bindings/newlisp/nmdb.lsp
+++ b/bindings/newlisp/nmdb.lsp
@@ -90,7 +90,7 @@
 	  (val (dup "\000" vallen))
 	)
     (set 'rv (func NMDB key keylen val vallen))
-    (if (> rv 0)
+    (if (>= rv 0)
       (slice val 0 rv)
       -1) ) )
 
diff --git a/bindings/python/nmdb.py b/bindings/python/nmdb.py
index 4d0dccc..35e280f 100644
--- a/bindings/python/nmdb.py
+++ b/bindings/python/nmdb.py
@@ -86,7 +86,9 @@ class _nmdbDict (object):
 			r = self._get(key)
 		except:
 			raise NetworkError
-		if not r:
+		if r == -1:
+			# For key errors, get returns -1 instead of a string
+			# so we know it's a miss.
 			raise KeyError
 		if self.autopickle:
 			r = cPickle.loads(r)
diff --git a/bindings/python/nmdb_ll.c b/bindings/python/nmdb_ll.c
index e37b22a..0e917f9 100644
--- a/bindings/python/nmdb_ll.c
+++ b/bindings/python/nmdb_ll.c
@@ -129,11 +129,12 @@ static PyObject *db_cache_get(nmdbobject *db, PyObject *args)
 	rv = nmdb_cache_get(db->db, key, ksize, val, vsize);
 	Py_END_ALLOW_THREADS
 
-	if (rv < 0) {
+	if (rv <= -2) {
 		/* FIXME: define a better exception */
 		r = PyErr_SetFromErrno(PyExc_IOError);
-	} else if (rv == 0) {
-		r = PyString_FromStringAndSize("", 0);
+	} else if (rv == -1) {
+		/* Miss, handled in the high-level module. */
+		r = PyLong_FromLong(-1);
 	} else {
 		r = PyString_FromStringAndSize(val, rv);
 	}
@@ -223,11 +224,12 @@ static PyObject *db_get(nmdbobject *db, PyObject *args)
 	rv = nmdb_get(db->db, key, ksize, val, vsize);
 	Py_END_ALLOW_THREADS
 
-	if (rv < 0) {
+	if (rv <= -2) {
 		/* FIXME: define a better exception */
 		r = PyErr_SetFromErrno(PyExc_IOError);
-	} else if (rv == 0) {
-		r = PyString_FromStringAndSize("", 0);
+	} else if (rv == -1) {
+		/* Miss, handled in the high-level module. */
+		r = PyLong_FromLong(-1);
 	} else {
 		r = PyString_FromStringAndSize(val, rv);
 	}
diff --git a/libnmdb/libnmdb.3 b/libnmdb/libnmdb.3
index 0a4b765..33dce97 100644
--- a/libnmdb/libnmdb.3
+++ b/libnmdb/libnmdb.3
@@ -125,8 +125,8 @@ greater than 64kb in size to make room for the largest possible value. It will
 return the size of the retrieved key (which will be put in the buffer pointed
 at by
 .IR val ),
-0 if the requested key was not in the database (or cache, if the cache variant
-is used), or < 0 on failure.
+-1 if the requested key was not in the database (or cache, if the cache
+variant is used), or -2 on failure.
 
 .BR nmdb_del ()
 is used to remove a given key (and it's associated value). The normal variant
diff --git a/libnmdb/libnmdb.c b/libnmdb/libnmdb.c
index f43dd10..a4a9fa8 100644
--- a/libnmdb/libnmdb.c
+++ b/libnmdb/libnmdb.c
@@ -278,21 +278,21 @@ static ssize_t do_get(nmdb_t *db,
 
 	t = srv_send(srv, buf, moff + reqsize);
 	if (t <= 0) {
-		rv = -1;
+		rv = -2;
 		goto exit;
 	}
 
 	reply = get_rep(srv, buf, bsize, &p, &psize);
 
 	if (reply == REP_CACHE_MISS || reply == REP_NOTIN) {
-		rv = 0;
+		rv = -1;
 		goto exit;
 	} else if (reply == REP_ERR) {
-		rv = -1;
+		rv = -2;
 		goto exit;
 	} else if (reply != REP_OK && reply != REP_CACHE_HIT) {
 		/* invalid response */
-		rv = -1;
+		rv = -2;
 		goto exit;
 	}
 
diff --git a/tests/c/1.c b/tests/c/1.c
index 518313f..6e1658e 100644
--- a/tests/c/1.c
+++ b/tests/c/1.c
@@ -54,10 +54,10 @@ int main(int argc, char **argv)
 	timer_start();
 	for (i = 0; i < times; i++) {
 		r = NGET(db, key, ksize, gval, vsize);
-		if (r < 0) {
+		if (r <= -2) {
 			perror("Get");
 			return 1;
-		} else if (r == 0) {
+		} else if (r == -1) {
 			misses++;
 		}
 	}
diff --git a/tests/c/2.c b/tests/c/2.c
index 79a0de1..5df1d85 100644
--- a/tests/c/2.c
+++ b/tests/c/2.c
@@ -72,10 +72,10 @@ int main(int argc, char **argv)
 	for (i = 0; i < times; i++) {
 		* (int *) key = i;
 		r = NGET(db, key, ksize, val, vsize);
-		if (r < 0) {
+		if (r <= -2) {
 			perror("Get");
 			return 1;
-		} else if (r == 0) {
+		} else if (r == -1) {
 			misses++;
 		}
 	}
diff --git a/tests/c/3.c b/tests/c/3.c
index b629254..955fbd5 100644
--- a/tests/c/3.c
+++ b/tests/c/3.c
@@ -66,10 +66,10 @@ int main(int argc, char **argv)
 
 		* (int *) key = i;
 		r = NGET(db, key, ksize, val, bsize);
-		if (r < 0) {
+		if (r <= -2) {
 			perror("Get");
 			return 1;
-		} else if (r == 0) {
+		} else if (r == -1) {
 			misses++;
 		}
 
diff --git a/tests/c/get.c b/tests/c/get.c
index 829e0d3..c57efd6 100644
--- a/tests/c/get.c
+++ b/tests/c/get.c
@@ -57,10 +57,10 @@ int main(int argc, char **argv)
 	for (i = 0; i < times; i++) {
 		* (int *) key = i;
 		r = NGET(db, key, ksize, val, vsize);
-		if (r < 0) {
+		if (r <= -2) {
 			perror("Get");
 			return 1;
-		} else if (r == 0) {
+		} else if (r == -1) {
 			misses++;
 		}
 	}