/* implement a new lseek using lseek that *writeszero'swhenextendingafile *beyondtheend.
*/ long
new_lseek(int fd, long offset, int origin)
{ long cur_pos = 0; long end_pos = 0; long seek_pos = 0;
if (origin == SEEK_CUR) { if (offset < 1) return (lseek(fd, offset, SEEK_CUR));
/* the seek position desired is before the *endofthefile.Wedon'tneed *todoanythingspecialexcepttheseek.
*/ if (seek_pos <= end_pos) return (lseek(fd, seek_pos, SEEK_SET));
/* the seek position is beyond the end of the *file.Writezero'stotheend. * *wearealreadyattheendofthefileso *wejustneedto"write()"zerosforthe *differencebetweenseek_pos-end_posand *thenseektothepositiontofinish *thecall
*/
{ char buffer[1024]; long len = seek_pos - end_pos;
memset(buffer, 0, 1024); while (len > 0) { if (write(fd, buffer, (size_t)(1024 > len ? len : 1024)) < 0) return (-1);
len -= 1024;
} return (lseek(fd, seek_pos, SEEK_SET));
}
}
/* if the pair doesn't fit something is horribly *wrong.LJM
*/
tmp_uint16_array = (uint16 *)np; if (!PAIRFITS(tmp_uint16_array, &key, &val)) return (DATABASE_CORRUPTED_ERROR);
putpair(np, &key, &val);
moved += 2;
}
off = ino[n + 1];
}
/* Now clean up the page */
ino[0] -= moved;
FREESPACE(ino) = copyto - sizeof(uint16) * (ino[0] + 3);
OFFSET(ino) = copyto;
/* the maximum number of loops we will allow UGLY split to chew *onbeforeweassumethedatabaseiscorruptedandthrowit *away.
*/ #define MAX_UGLY_SPLIT_LOOPS 10000
staticint
ugly_split(HTAB *hashp, uint32 obucket, BUFHEAD *old_bufp,
BUFHEAD *new_bufp, /* Same as __split_page. */ int copyto, int moved) /* int copyto; First byte on page which contains key/data values. */ /* int moved; Number of pairs moved to new page. */
{ register BUFHEAD *bufp; /* Buffer header for ino */ register uint16 *ino; /* Page keys come off of */ register uint16 *np; /* New page */ register uint16 *op; /* Page keys go on to if they aren't moving */
uint32 loop_detection = 0;
BUFHEAD *last_bfp; /* Last buf header OVFL needing to be freed */
DBT key, val;
SPLIT_RETURN ret;
uint16 n, off, ov_addr, scopyto; char *cino; /* Character value of ino */ int status;
if (ino[0] < 1) { return DATABASE_CORRUPTED_ERROR;
}
n = ino[0] - 1; while (n < ino[0]) {
/* this function goes nuts sometimes and never returns. *IhaventfoundtheproblemyetbutIneedasolution *soifwelooptoooftenweassumeadatabasecurruptionerror *:LJM
*/
loop_detection++;
if (loop_detection > MAX_UGLY_SPLIT_LOOPS) return DATABASE_CORRUPTED_ERROR;
bp = (uint16 *)bufp->page;
do_expand = 0; while (bp[0] && (bp[2] < REAL_KEY || bp[bp[0]] < REAL_KEY)) /* Exception case */ if (bp[2] == FULL_KEY_DATA && bp[0] == 2) /* This is the last page of a big key/data pair
and we need to add another page */ break; elseif (bp[2] < REAL_KEY && bp[bp[0]] != OVFLPAGE) {
bufp = dbm_get_buf(hashp, bp[bp[0] - 1], bufp, 0); if (!bufp) { #ifdef DEBUG
assert(0); #endif return (-1);
}
bp = (uint16 *)bufp->page;
} else /* Try to squeeze key on this page */ if (FREESPACE(bp) > PAIRSIZE(key, val)) {
{
squeeze_key(bp, key, val);
/* LJM: I added this because I think it was *leftoutonaccident. *ifthisisn'tincrementednkeyswillnot *betheactualnumberofkeysinthedb.
*/
hashp->NKEYS++; return (0);
}
} else {
bufp = dbm_get_buf(hashp, bp[bp[0] - 1], bufp, 0); if (!bufp) { #ifdef DEBUG
assert(0); #endif return (-1);
}
bp = (uint16 *)bufp->page;
}
bp = (uint16 *)p; if (!rsize)
bp[0] = 0; /* We hit the EOF, so initialize a new page */ elseif ((unsigned)rsize != size) {
errno = EFTYPE; return (-1);
}
if (!is_bitmap && !bp[0]) {
PAGE_INIT(p);
} else {
if (hashp->LORDER != BYTE_ORDER) { registerint i, max;
if (is_bitmap) {
max = hashp->BSIZE >> 2; /* divide by 4 */ for (i = 0; i < max; i++)
M_32_SWAP(((int *)p)[i]);
} else {
M_16_SWAP(bp[0]);
max = bp[0] + 2;
/* bound the size of max by *themaximumnumberofentries *inthearray
*/ if ((unsigned)max > (size / sizeof(uint16))) return (DATABASE_CORRUPTED_ERROR);
/* do the byte order swap
*/ for (i = 1; i <= max; i++)
M_16_SWAP(bp[i]);
}
}
/* check the validity of the page here *(afterdoingbyteorderswapingifnecessary)
*/ if (!is_bitmap && bp[0] != 0) {
uint16 num_keys = bp[0];
uint16 offset;
uint16 i;
/* bp[0] is supposed to be the number of *entriescurrentlyinthepage.If *bp[0]istoolarge(largerthanthewhole *page)thenthepageiscorrupted
*/ if (bp[0] > (size / sizeof(uint16))) return (DATABASE_CORRUPTED_ERROR);
/* bound free space */ if (FREESPACE(bp) > size) return (DATABASE_CORRUPTED_ERROR);
/* check each key and data offset to make *suretheyareallwithinboundsthey *shouldallbelessthantheprevious *offsetaswell.
*/
offset = size; for (i = 1; i <= num_keys; i += 2) { /* ignore overflow pages etc. */ if (bp[i + 1] >= REAL_KEY) {
/* Look through all the free maps to find the first free block */
first_page = hashp->LAST_FREED >> (hashp->BSHIFT + BYTE_SHIFT); for (i = first_page; i <= (unsigned)free_page; i++) { if (!(freep = (uint32 *)hashp->mapp[i]) &&
!(freep = fetch_bitmap(hashp, i))) return (0); if (i == (unsigned)free_page)
in_use_bits = free_bit; else
in_use_bits = (hashp->BSIZE << BYTE_SHIFT) - 1;
if (i == (unsigned)first_page) {
bit = hashp->LAST_FREED &
((hashp->BSIZE << BYTE_SHIFT) - 1);
j = bit / BITS_PER_MAP;
bit = bit & ~(BITS_PER_MAP - 1);
} else {
bit = 0;
j = 0;
} for (; bit <= in_use_bits; j++, bit += BITS_PER_MAP) if (freep[j] != ALL_SET) goto found;
}
#define OVMSG "HASH: Out of overflow pages. Increase page size\n" if (offset > SPLITMASK) { if (++splitnum >= NCACHED) { #ifndef macintosh
(void)fwrite(OVMSG, 1, sizeof(OVMSG) - 1, stderr); #endif return (0);
}
hashp->OVFL_POINT = splitnum;
hashp->SPARES[splitnum] = hashp->SPARES[splitnum - 1];
hashp->SPARES[splitnum - 1]--;
offset = 1;
}
/* Check if we need to allocate a new bitmap page */ if (free_bit == (hashp->BSIZE << BYTE_SHIFT) - 1) {
free_page++; if (free_page >= NCACHED) { #ifndef macintosh
(void)fwrite(OVMSG, 1, sizeof(OVMSG) - 1, stderr); #endif return (0);
} /* *Thisistricky.The1indicatesthatyouwantthenewpage *allocatedwith1clearbit.Actually,youaregoingto *allocate2pagesfromthismap.Thefirstisgoingtobe *themappage,thesecondistheoverflowpagewewere *lookingfor.Theinit_bitmaproutineautomatically,sets *thefirstbitofitselftoindicatethatthebitmapitself *isinuse.Wewouldexplicitlysetthesecondbit,but *don'thavetoifwetellinit_bitmapnottoleaveitclear *inthefirstplace.
*/ if (dbm_ibitmap(hashp,
(int)OADDR_OF(splitnum, offset), 1, free_page)) return (0);
hashp->SPARES[splitnum]++; #ifdef DEBUG2
free_bit = 2; #endif
offset++;
if (offset > SPLITMASK) {
if (++splitnum >= NCACHED) {
#ifndef macintosh
(void)fwrite(OVMSG, 1, sizeof(OVMSG) - 1, stderr);
#endif
return (0);
}
hashp->OVFL_POINT = splitnum;
hashp->SPARES[splitnum] = hashp->SPARES[splitnum - 1];
hashp->SPARES[splitnum - 1]--;
offset = 0;
}
} else {
/*
* Free_bit addresses the last used bit. Bump it to address
* the first available bit.
*/
free_bit++;
SETBIT(freep, free_bit);
}
/* Calculate address of the new overflow page */
addr = OADDR_OF(splitnum, offset);
#ifdef DEBUG2
(void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
addr, free_bit, free_page);
#endif
return (addr);
found:
bit = bit + first_free(freep[j]);
SETBIT(freep, bit);
#ifdef DEBUG2
tmp1 = bit;
tmp2 = i;
#endif
/*
* Bits are addressed starting with 0, but overflow pages are addressed
* beginning at 1. Bit is a bit addressnumber, so we need to increment
* it to convert it to a page number.
*/
bit = 1 + bit + (i * (hashp->BSIZE << BYTE_SHIFT));
if (bit >= hashp->LAST_FREED)
hashp->LAST_FREED = bit - 1;
/* Calculate the split number for this page */
for (i = 0; (i < (unsigned)splitnum) && (bit > hashp->SPARES[i]); i++) {
}
offset = (i ? bit - hashp->SPARES[i - 1] : bit);
if (offset >= SPLITMASK)
return (0); /* Out of overflow pages */
addr = OADDR_OF(i, offset);
#ifdef DEBUG2
(void)fprintf(stderr, "OVERFLOW_PAGE: ADDR: %d BIT: %d PAGE %d\n",
addr, tmp1, tmp2);
#endif
/* Allocate and return the overflow page */
return (addr);
}
/*
* Mark this overflow page as free.
*/
extern void
dbm_free_ovflpage(HTAB *hashp, BUFHEAD *obufp)
{
uint16 addr;
uint32 *freep;
uint32 bit_address, free_page, free_bit;
uint16 ndx;
if (!(freep = hashp->mapp[free_page]))
freep = fetch_bitmap(hashp, free_page);
#ifdef DEBUG
/*
* This had better never happen. It means we tried to read a bitmap
* that has already had overflow pages allocated off it, and we
* failed to read it from the file.
*/
if (!freep) {
assert(0);
return;
}
#endif
CLRBIT(freep, free_bit);
#ifdef DEBUG2
(void)fprintf(stderr, "FREE_OVFLPAGE: ADDR: %d BIT: %d PAGE %d\n",
obufp->addr, free_bit, free_page);
#endif
dbm_reclaim_buf(hashp, obufp);
}
/*
* We have to know that the key will fit, but the last entry on the page is
* an overflow pair, so we need to shift things.
*/
static void
squeeze_key(uint16 *sp, const DBT *key, const DBT *val)
{
register char *p;
uint16 free_space, n, off, pageno;
p = (char *)sp;
n = sp[0];
free_space = FREESPACE(sp);
off = OFFSET(sp);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.