mirror of
https://github.com/isar/libmdbx.git
synced 2025-01-20 05:18:21 +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_node *node;
|
||||||
MDB_page *sp, *xp;
|
MDB_page *sp, *xp;
|
||||||
char *base;
|
char *base;
|
||||||
int nsize, delta;
|
indx_t delta, nsize, len, ptr;
|
||||||
indx_t i, numkeys, ptr;
|
int i;
|
||||||
|
|
||||||
node = NODEPTR(mp, indx);
|
node = NODEPTR(mp, indx);
|
||||||
sp = (MDB_page *)NODEDATA(node);
|
sp = (MDB_page *)NODEDATA(node);
|
||||||
delta = SIZELEFT(sp);
|
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)) {
|
if (IS_LEAF2(sp)) {
|
||||||
nsize = NUMKEYS(sp) * sp->mp_ksize;
|
len = nsize;
|
||||||
if (nsize & 1)
|
if (nsize & 1)
|
||||||
return; /* do not make the node uneven-sized */
|
return; /* do not make the node uneven-sized */
|
||||||
memmove(PAGEDATA(xp), PAGEDATA(sp), nsize);
|
|
||||||
} else {
|
} else {
|
||||||
int i;
|
xp = (MDB_page *)((char *)sp + delta); /* destination subpage */
|
||||||
numkeys = NUMKEYS(sp);
|
for (i = NUMKEYS(sp); --i >= 0; )
|
||||||
for (i=numkeys-1; i>=0; i--)
|
|
||||||
xp->mp_ptrs[i] = sp->mp_ptrs[i] - delta;
|
xp->mp_ptrs[i] = sp->mp_ptrs[i] - delta;
|
||||||
|
len = PAGEHDRSZ;
|
||||||
}
|
}
|
||||||
xp->mp_upper = sp->mp_lower;
|
sp->mp_upper = sp->mp_lower;
|
||||||
xp->mp_lower = sp->mp_lower;
|
COPY_PGNO(sp->mp_pgno, mp->mp_pgno);
|
||||||
xp->mp_flags = sp->mp_flags;
|
|
||||||
xp->mp_ksize = sp->mp_ksize;
|
|
||||||
COPY_PGNO(xp->mp_pgno, mp->mp_pgno);
|
|
||||||
|
|
||||||
nsize = NODEDSZ(node) - delta;
|
|
||||||
SETDSZ(node, nsize);
|
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];
|
ptr = mp->mp_ptrs[indx];
|
||||||
numkeys = NUMKEYS(mp);
|
for (i = NUMKEYS(mp); --i >= 0; ) {
|
||||||
for (i = 0; i < numkeys; i++) {
|
|
||||||
if (mp->mp_ptrs[i] <= ptr)
|
if (mp->mp_ptrs[i] <= ptr)
|
||||||
mp->mp_ptrs[i] += delta;
|
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;
|
mp->mp_upper += delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user