From 2770e193b6777ebc6d3a5b222d324e21bd6ad85a Mon Sep 17 00:00:00 2001 From: Leonid Yuriev Date: Sun, 4 Nov 2018 18:57:19 +0300 Subject: [PATCH] mdbx: take in account shrink/growing thresholds while copy-with-compactification. Change-Id: Id93e62089819dfcc8cbc83620e0bdd806d8c1950 --- src/mdbx.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/mdbx.c b/src/mdbx.c index dd5c53e1..143a4e57 100644 --- a/src/mdbx.c +++ b/src/mdbx.c @@ -11534,9 +11534,38 @@ static int __cold mdbx_env_compact(MDBX_env *env, MDBX_txn *read_txn, } } - /* update signature */ + /* Calculate filesize taking in account shrink/growing thresholds */ + if (meta->mp_meta.mm_geo.next > meta->mp_meta.mm_geo.now) { + const pgno_t aligned = + pgno_align2os_pgno(env, pgno_add(meta->mp_meta.mm_geo.next, + meta->mp_meta.mm_geo.grow - + meta->mp_meta.mm_geo.next % + meta->mp_meta.mm_geo.grow)); + meta->mp_meta.mm_geo.now = aligned; + } else if (meta->mp_meta.mm_geo.next < meta->mp_meta.mm_geo.now) { + meta->mp_meta.mm_geo.now = meta->mp_meta.mm_geo.next; + const pgno_t aligner = meta->mp_meta.mm_geo.grow + ? meta->mp_meta.mm_geo.grow + : meta->mp_meta.mm_geo.shrink; + const pgno_t aligned = + pgno_align2os_pgno(env, meta->mp_meta.mm_geo.next + aligner - + meta->mp_meta.mm_geo.next % aligner); + meta->mp_meta.mm_geo.now = aligned; + } + + if (meta->mp_meta.mm_geo.now < meta->mp_meta.mm_geo.lower) + meta->mp_meta.mm_geo.now = meta->mp_meta.mm_geo.lower; + if (meta->mp_meta.mm_geo.now > meta->mp_meta.mm_geo.upper) + meta->mp_meta.mm_geo.now = meta->mp_meta.mm_geo.upper; + + /* Update signature */ + assert(meta->mp_meta.mm_geo.now >= meta->mp_meta.mm_geo.next); meta->mp_meta.mm_datasync_sign = mdbx_meta_sign(&meta->mp_meta); - return MDBX_SUCCESS; + + /* Extend file if required */ + return (meta->mp_meta.mm_geo.now != meta->mp_meta.mm_geo.next) + ? mdbx_ftruncate(fd, pgno2bytes(env, meta->mp_meta.mm_geo.now)) + : MDBX_SUCCESS; } /* Copy environment as-is. */