diff options
author | ubq323 <ubq323@ubq323.website> | 2023-03-19 20:39:34 +0000 |
---|---|---|
committer | the lemons <citrons@mondecitronne.com> | 2023-03-20 22:57:38 -0500 |
commit | ba9787994770dab80403ee21539bd5bbba912d47 (patch) | |
tree | 09520983785cb031dd1a899b03f205185666034a | |
parent | 5bbf9ed81eeed87b44edffa15e3310b17918a7a7 (diff) |
add #db operator for number of entries in db
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | lmdb.c | 14 |
2 files changed, 17 insertions, 0 deletions
@@ -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. @@ -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}, }; |