From 5eecc854236f0b943aaa89e0c3a46f9fbd208ca9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 20 Jun 2011 18:40:33 +0930 Subject: tdb2: create tdb2 versions of various testing TDBs. Soon, TDB2 will handle tdb1 files, but until then, we substitute. Signed-off-by: Rusty Russell --- lib/tdb/python/tests/simple.py | 63 ++++++++++++++++++++++++++++-------------- lib/tdb2/pytdb.c | 14 +++++++++- 2 files changed, 55 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/tdb/python/tests/simple.py b/lib/tdb/python/tests/simple.py index 99f31d2fcd..2877092fe3 100644 --- a/lib/tdb/python/tests/simple.py +++ b/lib/tdb/python/tests/simple.py @@ -20,8 +20,13 @@ class OpenTdbTests(TestCase): class CloseTdbTests(TestCase): def test_double_close(self): - self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT, - os.O_CREAT|os.O_RDWR) + # No hash size in tdb2. + if tdb.__version__.startswith("2"): + self.tdb = tdb.Tdb(tempfile.mkstemp()[1], tdb.DEFAULT, + os.O_CREAT|os.O_RDWR) + else: + self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT, + os.O_CREAT|os.O_RDWR) self.assertNotEqual(None, self.tdb) # ensure that double close does not crash python @@ -42,8 +47,12 @@ class SimpleTdbTests(TestCase): def setUp(self): super(SimpleTdbTests, self).setUp() - self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT, - os.O_CREAT|os.O_RDWR) + if tdb.__version__.startswith("2"): + self.tdb = tdb.Tdb(tempfile.mkstemp()[1], tdb.DEFAULT, + os.O_CREAT|os.O_RDWR) + else: + self.tdb = tdb.Tdb(tempfile.mkstemp()[1], 0, tdb.DEFAULT, + os.O_CREAT|os.O_RDWR) self.assertNotEqual(None, self.tdb) def tearDown(self): @@ -56,7 +65,8 @@ class SimpleTdbTests(TestCase): self.tdb.lock_all() def test_max_dead(self): - self.tdb.max_dead = 20 + if not tdb.__version__.startswith("2"): + self.tdb.max_dead = 20 def test_unlockall(self): self.tdb.lock_all() @@ -67,7 +77,8 @@ class SimpleTdbTests(TestCase): self.tdb.read_unlock_all() def test_reopen(self): - self.tdb.reopen() + if not tdb.__version__.startswith("2"): + self.tdb.reopen() def test_store(self): self.tdb.store("bar", "bla") @@ -75,7 +86,8 @@ class SimpleTdbTests(TestCase): def test_getitem(self): self.tdb["bar"] = "foo" - self.tdb.reopen() + if not tdb.__version__.startswith("2"): + self.tdb.reopen() self.assertEquals("foo", self.tdb["bar"]) def test_delete(self): @@ -91,13 +103,16 @@ class SimpleTdbTests(TestCase): self.assertRaises(KeyError, lambda: self.tdb["bla"]) def test_hash_size(self): - self.tdb.hash_size + if not tdb.__version__.startswith("2"): + self.tdb.hash_size def test_map_size(self): - self.tdb.map_size + if not tdb.__version__.startswith("2"): + self.tdb.map_size def test_freelist_size(self): - self.tdb.freelist_size + if not tdb.__version__.startswith("2"): + self.tdb.freelist_size def test_name(self): self.tdb.filename @@ -145,17 +160,19 @@ class SimpleTdbTests(TestCase): self.assertEquals(0, len(list(self.tdb))) def test_repack(self): - self.tdb["foo"] = "abc" - self.tdb["bar"] = "def" - del self.tdb["foo"] - self.tdb.repack() + if not tdb.__version__.startswith("2"): + self.tdb["foo"] = "abc" + self.tdb["bar"] = "def" + del self.tdb["foo"] + self.tdb.repack() def test_seqnum(self): - self.tdb.enable_seqnum() - seq1 = self.tdb.seqnum - self.tdb.increment_seqnum_nonblock() - seq2 = self.tdb.seqnum - self.assertEquals(seq2-seq1, 1) + if not tdb.__version__.startswith("2"): + self.tdb.enable_seqnum() + seq1 = self.tdb.seqnum + self.tdb.increment_seqnum_nonblock() + seq2 = self.tdb.seqnum + self.assertEquals(seq2-seq1, 1) def test_len(self): self.assertEquals(0, len(list(self.tdb))) @@ -163,8 +180,12 @@ class SimpleTdbTests(TestCase): self.assertEquals(1, len(list(self.tdb))) def test_add_flags(self): - self.tdb.add_flags(tdb.NOMMAP) - self.tdb.remove_flags(tdb.NOMMAP) + if tdb.__version__.startswith("2"): + self.tdb.add_flag(tdb.NOMMAP) + self.tdb.remove_flag(tdb.NOMMAP) + else: + self.tdb.add_flags(tdb.NOMMAP) + self.tdb.remove_flags(tdb.NOMMAP) class VersionTests(TestCase): diff --git a/lib/tdb2/pytdb.c b/lib/tdb2/pytdb.c index 86f604874b..c760045508 100644 --- a/lib/tdb2/pytdb.c +++ b/lib/tdb2/pytdb.c @@ -72,12 +72,21 @@ static PyObject *PyString_FromTDB_DATA(TDB_DATA data) return NULL; \ } +static void stderr_log(struct tdb_context *tdb, + enum tdb_log_level level, + const char *message, + void *data) +{ + fprintf(stderr, "%s:%s\n", tdb_name(tdb), message); +} + static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwargs) { char *name = NULL; int tdb_flags = TDB_DEFAULT, flags = O_RDWR, mode = 0600; struct tdb_context *ctx; PyTdbObject *ret; + union tdb_attribute logattr; const char *kwnames[] = { "name", "tdb_flags", "flags", "mode", NULL }; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|siii", (char **)kwnames, &name, &tdb_flags, &flags, &mode)) @@ -87,7 +96,10 @@ static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwarg tdb_flags |= TDB_INTERNAL; } - ctx = tdb_open(name, tdb_flags, flags, mode, NULL); + logattr.log.base.attr = TDB_ATTRIBUTE_LOG; + logattr.log.base.next = NULL; + logattr.log.fn = stderr_log; + ctx = tdb_open(name, tdb_flags, flags, mode, &logattr); if (ctx == NULL) { PyErr_SetFromErrno(PyExc_IOError); return NULL; -- cgit