# HG changeset patch # User Henry S. Thompson # Date 1738330262 0 # Node ID 7886d7de5eedb0f716f16a851c37c7081e29189e # Parent f457d7eb8cbed2c36a3ec21ac9538f4787d13284 use cdb library directly, sequestration of cdb handle complete and working, nndb counts two loops now, one with and one without counting successes diff -r f457d7eb8cbe -r 7886d7de5eed lib/python/cc/lmh/db.pxd --- a/lib/python/cc/lmh/db.pxd Fri Jan 31 13:28:09 2025 +0000 +++ b/lib/python/cc/lmh/db.pxd Fri Jan 31 13:31:02 2025 +0000 @@ -1,5 +1,8 @@ cimport cdb cdef class CCdb: cdef cdb.Cdb* _c_cdb + cdef void findstart(self) cdef int find(self, bytes p) cdef char[::1] init(self, int fno) + cdef cdb.uint32 pos(self) + cdef int len(self) diff -r f457d7eb8cbe -r 7886d7de5eed lib/python/cc/lmh/db.pyx --- a/lib/python/cc/lmh/db.pyx Fri Jan 31 13:28:09 2025 +0000 +++ b/lib/python/cc/lmh/db.pyx Fri Jan 31 13:31:02 2025 +0000 @@ -1,6 +1,5 @@ -# 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] -# extra_objects = cdb_min/libcdb.a -# distutils: include_dirs = cdb_min +# distutils: extra_objects = cdb/libcdb.a +# distutils: include_dirs = cdb cimport cdb @@ -8,6 +7,9 @@ def __cinit__(self): self._c_cdb = cdb.cdb_new() + cdef void findstart(self): + cdb.cdb_findstart(self._c_cdb) + cdef int find(self, bytes p): return cdb.cdb_find(self._c_cdb, p, len(p)) @@ -15,3 +17,9 @@ cdb.cdb_init(self._c_cdb,fno) cdef char[::1] _mview = (cdb.cdb_mmap()) return _mview + + cdef cdb.uint32 pos(self): + return cdb.cdb_pos() + + cdef int len(self): + return cdb.cdb_len() diff -r f457d7eb8cbe -r 7886d7de5eed lib/python/cc/lmh/nndb.pyx --- a/lib/python/cc/lmh/nndb.pyx Fri Jan 31 13:28:09 2025 +0000 +++ b/lib/python/cc/lmh/nndb.pyx Fri Jan 31 13:31:02 2025 +0000 @@ -1,8 +1,6 @@ -# distutils: extra_objects = cdb_min/libcdb.a -# 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 +# distutils: extra_objects = cdb/libcdb.a +# distutils: include_dirs = cdb -cimport cdb cimport db # Usage: ... cdb-path probe nreps @@ -16,30 +14,33 @@ cdef db.CCdb testMe(bytes probe): global CC res: int = -1 - cdef cdb.Cdb* c_cdb print('testing... %s %s x %s'%(sys.argv[1],probe,int(sys.argv[3]))) with open(sys.argv[1],'rb') as dbf: mv = CC.init(dbf.fileno()) - c_cdb = CC._c_cdb - print("mv %s cdb %s"%(len(mv),(c_cdb))) - cdb.cdb_findstart(c_cdb) + CC.findstart() res=CC.find(probe) if res == 1: - print(res,o:=cdb.cdb_pos(), l:=cdb.cdb_len()) + print(res,o:=CC.pos(), l:=CC.len()) sys.stdout.buffer.write(mv[o:o+l]) sys.stdout.buffer.write(b'\n') else: print('losing %s'%res) print('tested') - return CC + +testMe(sys.argv[2].encode('utf8')) + +probe = sys.argv[2].encode("utf8") def cfind(p: bytes) -> int: global CC return CC.find(p) -testMe(sys.argv[2].encode('utf8')) +print(timeit.timeit('cfind(probe)', + number=int(sys.argv[3]), globals = globals())) -probe = sys.argv[2].encode("utf8") - -print(timeit.timeit('cfind(probe)', - number=int(sys.argv[3]), globals=globals())) +X = 0 +print(timeit.timeit('(X:=X+1) if cfind(probe)==1 else None', + number=int(sys.argv[3]), + setup = 'global X', + globals = globals()), + X)