aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-03-19 20:39:34 +0000
committerthe lemons <citrons@mondecitronne.com>2023-03-20 22:57:38 -0500
commitba9787994770dab80403ee21539bd5bbba912d47 (patch)
tree09520983785cb031dd1a899b03f205185666034a
parent5bbf9ed81eeed87b44edffa15e3310b17918a7a7 (diff)
add #db operator for number of entries in db
-rw-r--r--README.md3
-rw-r--r--lmdb.c14
2 files changed, 17 insertions, 0 deletions
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},
};