From d27bb860ea5d3edb59b78413325102104a24ce1e Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 24 Mar 2023 22:04:17 +0000 Subject: add mapsize option --- README.md | 1 + lmdb.c | 11 +++++++++++ 2 files changed, 12 insertions(+) diff --git a/README.md b/README.md index b4db243..7f4aa46 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ create a new environment handle and open the database at the `path`. returns the `opt` is an optional table of options: * `maxdbs`: number: the maximum number of databases in the environment. must be set if multiple databases will be used. +* `mapsize`: number: the memory map size used for this environment. this is also the maximum size of the database. default: 1GiB (`1024^3`). * `rdonly`: boolean: open the environment in read-only mode. * `nosubdir`: boolean: the `path` is used for the database file itself and not its containing directory. * `nosync`: boolean: don't flush buffers to disk when committing. diff --git a/lmdb.c b/lmdb.c index a1c4e8e..6a6e7ba 100644 --- a/lmdb.c +++ b/lmdb.c @@ -70,6 +70,8 @@ static int env_open(lua_State *L) { int flags = 0; int maxdbs = 0; int maxdbs_supplied = 0; + int mapsize = 0; + int mapsize_supplied = 0; if (!lua_isnil(L, 2)) { for (int i = 0; env_flags[i].name != NULL; i++) { lua_getfield(L, 2, env_flags[i].name); @@ -78,11 +80,20 @@ static int env_open(lua_State *L) { } lua_getfield(L, 2, "maxdbs"); maxdbs = lua_tointegerx(L, -1, &maxdbs_supplied); + lua_getfield(L, 2, "mapsize"); + mapsize = lua_tointegerx(L, -1, &mapsize_supplied); } MDB_env *env; reterr(mdb_env_create(&env)); if (maxdbs_supplied) mdb_env_set_maxdbs(env, maxdbs); + if (mapsize_supplied) + mdb_env_set_mapsize(env, mapsize); + else + // lmdb default mapsize is 1MiB which is tiny + // so we use a default of 1GiB instead + mdb_env_set_mapsize(env, 1073741824); + int result = mdb_env_open(env, lua_tostring(L, 1), flags, 0664); if (result != 0) { mdb_env_close(env); -- cgit v1.2.3