From ba9787994770dab80403ee21539bd5bbba912d47 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 19 Mar 2023 20:39:34 +0000 Subject: add #db operator for number of entries in db --- README.md | 3 +++ lmdb.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/README.md b/README.md index b63c4e3..e8187d3 100644 --- a/README.md +++ b/README.md @@ -57,3 +57,6 @@ write `value` as the value of `key` into the database. ### `pairs(db)` returns `lmdb.next, db, nil`. + +## `#db` +return the number of entries in the database. diff --git a/lmdb.c b/lmdb.c index 60ddca2..03c696f 100644 --- a/lmdb.c +++ b/lmdb.c @@ -288,6 +288,19 @@ static int db_put(lua_State *L) { return 0; } +static int db_len(lua_State *L) { + lua_settop(L, 1); + MDB_dbi dbi = *(MDB_dbi *) luaL_checkudata(L, 1, "lmdb.db"); + check_env(L, 1); + + get_parent(L, 1); + struct handle *tud = lua_touserdata(L, -1); + struct MDB_stat stat; + asserr(mdb_stat(tud->obj, dbi, &stat)); + lua_pushinteger(L, stat.ms_entries); + return 1; +} + static int db_next(lua_State *L) { lua_settop(L, 2); MDB_dbi dbi = *(MDB_dbi *) luaL_checkudata(L, 1, "lmdb.db"); @@ -377,6 +390,7 @@ static const struct luaL_Reg lmdb_db[] = { {"__index", db_get}, {"__newindex", db_put}, {"__pairs", db_pairs}, + {"__len", db_len}, {NULL, NULL}, }; -- cgit v1.2.3