mirror of
https://github.com/isar/libmdbx.git
synced 2025-01-02 00:04:12 +08:00
lmdb: Simpler mdb_node_shrink().
Change-Id: If0e4153e69c4f858ee45471c4888ec2024253b50
This commit is contained in:
parent
a6fa632a54
commit
6524dbf3a1
37
mdb.c
37
mdb.c
@ -7012,45 +7012,38 @@ mdb_node_shrink(MDB_page *mp, indx_t indx)
|
||||
MDB_node *node;
|
||||
MDB_page *sp, *xp;
|
||||
char *base;
|
||||
int nsize, delta;
|
||||
indx_t i, numkeys, ptr;
|
||||
indx_t delta, nsize, len, ptr;
|
||||
int i;
|
||||
|
||||
node = NODEPTR(mp, indx);
|
||||
sp = (MDB_page *)NODEDATA(node);
|
||||
delta = SIZELEFT(sp);
|
||||
xp = (MDB_page *)((char *)sp + delta);
|
||||
nsize = NODEDSZ(node) - delta;
|
||||
|
||||
/* shift subpage upward */
|
||||
/* Prepare to shift upward, set len = length(subpage part to shift) */
|
||||
if (IS_LEAF2(sp)) {
|
||||
nsize = NUMKEYS(sp) * sp->mp_ksize;
|
||||
len = nsize;
|
||||
if (nsize & 1)
|
||||
return; /* do not make the node uneven-sized */
|
||||
memmove(PAGEDATA(xp), PAGEDATA(sp), nsize);
|
||||
} else {
|
||||
int i;
|
||||
numkeys = NUMKEYS(sp);
|
||||
for (i=numkeys-1; i>=0; i--)
|
||||
xp = (MDB_page *)((char *)sp + delta); /* destination subpage */
|
||||
for (i = NUMKEYS(sp); --i >= 0; )
|
||||
xp->mp_ptrs[i] = sp->mp_ptrs[i] - delta;
|
||||
len = PAGEHDRSZ;
|
||||
}
|
||||
xp->mp_upper = sp->mp_lower;
|
||||
xp->mp_lower = sp->mp_lower;
|
||||
xp->mp_flags = sp->mp_flags;
|
||||
xp->mp_ksize = sp->mp_ksize;
|
||||
COPY_PGNO(xp->mp_pgno, mp->mp_pgno);
|
||||
|
||||
nsize = NODEDSZ(node) - delta;
|
||||
sp->mp_upper = sp->mp_lower;
|
||||
COPY_PGNO(sp->mp_pgno, mp->mp_pgno);
|
||||
SETDSZ(node, nsize);
|
||||
|
||||
/* shift lower nodes upward */
|
||||
/* Shift <lower nodes...initial part of subpage> upward */
|
||||
base = (char *)mp + mp->mp_upper + PAGEBASE;
|
||||
memmove(base + delta, base, (char *)sp + len - base);
|
||||
|
||||
ptr = mp->mp_ptrs[indx];
|
||||
numkeys = NUMKEYS(mp);
|
||||
for (i = 0; i < numkeys; i++) {
|
||||
for (i = NUMKEYS(mp); --i >= 0; ) {
|
||||
if (mp->mp_ptrs[i] <= ptr)
|
||||
mp->mp_ptrs[i] += delta;
|
||||
}
|
||||
|
||||
base = (char *)mp + mp->mp_upper + PAGEBASE;
|
||||
memmove(base + delta, base, ptr - mp->mp_upper + NODESIZE + NODEKSZ(node));
|
||||
mp->mp_upper += delta;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user