changeset 254:aeb755b72a7d

try using cdb as C library
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Sat, 18 Jan 2025 21:25:17 +0000
parents 79701366f438
children 0c5b05d62eda
files lib/python/cc/lmh/cdb.pxd lib/python/cc/lmh/db.pyx lib/python/cc/lmh/setup.py
diffstat 3 files changed, 100 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/python/cc/lmh/cdb.pxd	Sat Jan 18 21:25:17 2025 +0000
@@ -0,0 +1,46 @@
+cdef extern from "cdb.h":
+
+    ctypedef unsigned int uint32;
+
+    ctypedef struct Cdb:
+        pass
+
+    Cdb* cdb_new ()
+    void cdb_free(Cdb* cdb)
+    void cdb_init(Cdb* cdb,int fd)
+
+    int cdb_read(Cdb* cdb,char *,unsigned int,uint32)
+
+    void cdb_findstart(Cdb* cdb)
+    int cdb_findnext(Cdb* cdb,char *,unsigned int)
+    int cdb_find(Cdb* cdb,char *,unsigned int)
+
+    int cdb_pos();
+    int cdb_len();
+
+    char *cdb_mmap();
+    int cdb_msize();
+
+cdef extern from "error.h":
+  int errno;
+
+  int error_intr;
+  int error_nomem;
+  int error_noent;
+  int error_txtbsy;
+  int error_io;
+  int error_exist;
+  int error_timeout;
+  int error_inprogress;
+  int error_wouldblock;
+  int error_again;
+  int error_pipe;
+  int error_perm;
+  int error_acces;
+  int error_nodevice;
+  int error_proto;
+
+  char *error_str(int);
+  int error_temp(int);
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/python/cc/lmh/db.pyx	Sat Jan 18 21:25:17 2025 +0000
@@ -0,0 +1,48 @@
+# distutils: sources = [cdb_min/cdb.c, cdb_min/error.c, cdb_min/open_read.c, cdb_min/seek_cur.c, cdb_min/open_trunc.c, cdb_min/seek_set.c, cdb_min/byte_copy.c, cdb_min/byte_diff.c, cdb_min/error_str.c, cdb_min/uint32_unpack.c, cdb_min/cdb_hash.c]
+# distutils: include_dirs = cdb_min
+
+res: int
+
+cimport cdb
+import sys, timeit
+
+cdef class Cdb:
+  cdef cdb.Cdb* _c_cdb
+
+  def __cinit__(self):
+    self._c_cdb = cdb.cdb_new()
+
+  cdef int find(self: Cdb, p: bytes):
+    return cdb.cdb_find(self._c_cdb, p, len(p))
+
+  cdef void init(self: Cdb, fno: int):
+    cdb.cdb_init(self._c_cdb,fno)
+    cdef char[::1] _mview = <char[:cdb.cdb_msize():1]>(cdb.cdb_mmap())
+
+probe: bytes = sys.argv[2].encode('utf8')
+
+cdef Cdb CC
+
+cdef Cdb testMe():
+  global probe
+  res: int
+  print('testing...')
+  cd = Cdb()
+  with open(sys.argv[1],'rb') as dbf:
+    cd.init(dbf.fileno())
+    cdb.cdb_findstart(cd._c_cdb)
+    if cd.find(probe) > 0:
+      print(cdb.cdb_pos(), cdb.cdb_len())
+    else:
+      print(0)
+  print('tested')
+  return cd
+
+def cfind(p: bytes) -> int:
+  global CC
+  return cdb.cdb_find(CC._c_cdb, p, len(p))
+
+CC = testMe()
+
+print(timeit.timeit('cfind(probe)',
+               number=int(sys.argv[3]), globals=globals()))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/python/cc/lmh/setup.py	Sat Jan 18 21:25:17 2025 +0000
@@ -0,0 +1,6 @@
+from setuptools import Extension, setup
+from Cython.Build import cythonize
+
+setup(
+    ext_modules = cythonize([Extension("db", ["db.pyx"])])
+)