// btree version threads2j linux futex concurrency version
// with reworked bt_deletekey
-// 12 FEB 2014
+// 17 FEB 2014
// author: karl malbrain, malbrain@cal.berkeley.edu
BtLockDelete,
BtLockRead,
BtLockWrite,
- BtLockParent
+ BtLockParent,
+ BtLockParentWrt
} BtLock;
// mode & definition for latch implementation
-enum {
- Mutex = 1 << 0, // the mutex bit
- Write = 1 << 1, // the writers bit
- Share = 1 << 2, // reader count
- PendRd = 1 << 12, // reader contended count
- PendWr = 1 << 22 // writer contended count
-} LockMode;
-
enum {
QueRd = 1, // reader queue
QueWr = 2 // writer queue
// share is count of read accessors
// grant write lock when share == 0
-typedef struct {
- volatile uint mutex:1; // 1 = busy
- volatile uint write:1; // 1 = exclusive
- volatile uint share:10; // count of readers holding locks
- volatile uint readwait:10; // count of readers waiting
- volatile uint writewait:10; // count of writers waiting
+volatile typedef struct {
+ unsigned char mutex[1]; // 1 = busy
+ unsigned char write:1; // 1 = exclusive
+ unsigned char readwait:1; // readers are waiting
+ unsigned char writewait:1; // writers are waiting
+ unsigned char filler:5;
+ ushort share; // count of readers holding locks
+ ushort rcnt; // count of waiting readers
+ ushort wcnt; // count of waiting writers
} BtLatch;
// Define the length of the page and key pointers
} BtSlot;
// The key structure occupies space at the upper end of
-// each page. It's a length byte followed by the value
+// each page. It's a length byte followed by the key
// bytes.
typedef struct {
uint min; // next key offset
unsigned char bits:7; // page size in bits
unsigned char free:1; // page is on free list
- unsigned char lvl:4; // level of page
+ unsigned char lvl:6; // level of page
unsigned char kill:1; // page is being deleted
unsigned char dirty:1; // page has deleted keys
- unsigned char posted:1; // page fence has posted
- unsigned char goright:1; // page is being killed, continue to right
unsigned char right[BtId]; // page number to right
- unsigned char fence[256]; // page fence key
} *BtPage;
// hash table entries
// The memory mapping pool table buffer manager entry
typedef struct {
- unsigned long long int lru; // number of times accessed
uid basepage; // mapped base page number
char *map; // mapped memory pointer
ushort slot; // slot index in this array
#endif
} BtPool;
+#define CLOCK_bit 0x8000 // bit in pool->pin
+
// The loadpage interface object
typedef struct {
// B-Tree functions
extern void bt_close (BtDb *bt);
extern BtDb *bt_open (BtMgr *mgr);
-extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uid id, uint tod, uint lvl);
-extern BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len);
+extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod);
+extern BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl);
extern uid bt_findkey (BtDb *bt, unsigned char *key, uint len);
extern uint bt_startkey (BtDb *bt, unsigned char *key, uint len);
extern uint bt_nextkey (BtDb *bt, uint slot);
-// internal functions
-BTERR bt_removepage (BtDb *bt, BtPageSet *set, uint lvl, unsigned char *pagefence);
-
// manager functions
extern BtMgr *bt_mgr (char *name, uint mode, uint bits, uint poolsize, uint segsize, uint hashsize);
void bt_mgrclose (BtMgr *mgr);
// Helper functions to return slot values
-
extern BtKey bt_key (BtDb *bt, uint slot);
extern uid bt_uid (BtDb *bt, uint slot);
extern uint bt_tod (BtDb *bt, uint slot);
// A key consists of a length byte, two bytes of
// index number (0 - 65534), and up to 253 bytes
// of key value. Duplicate keys are discarded.
-// Associated with each key is a 48 bit row-id.
+// Associated with each key is a 48 bit row-id,
+// or any other value desired.
// The b-tree root is always located at page 1.
// The first leaf page of level zero is always
// Deleted keys are marked with a dead bit until
// page cleanup. The fence key for a node is
-// present in a special array.
+// always present.
// Groups of pages called segments from the btree are optionally
// cached with a memory mapped pool. A hash table is used to keep
void bt_spinreadlock(BtLatch *latch, int private)
{
+ushort decr = 0;
uint prev;
if( private )
while( 1 ) {
// obtain latch mutex
- if( __sync_fetch_and_or((uint *)latch, Mutex) & Mutex ) {
+ while( __sync_lock_test_and_set(latch->mutex, 1) )
sched_yield();
- continue;
- }
+
+ if( decr )
+ latch->rcnt--, decr = 0;
// wait for writers to clear
// increment read waiters and wait
if( latch->write || latch->writewait ) {
- __sync_fetch_and_add ((uint *)latch, PendRd);
- prev = __sync_fetch_and_and ((uint *)latch, ~Mutex) & ~Mutex;
+ latch->readwait = 1;
+ latch->rcnt++;
+ prev = *(uint *)latch & ~1;
+ __sync_lock_release (latch->mutex);
sys_futex( (uint *)latch, FUTEX_WAIT_BITSET | private, prev, NULL, NULL, QueRd );
- __sync_fetch_and_sub ((uint *)latch, PendRd);
+ decr = 1;
continue;
}
// increment reader lock count
// and release latch mutex
- __sync_fetch_and_add ((uint *)latch, Share);
- __sync_fetch_and_and ((uint *)latch, ~Mutex);
+ latch->readwait = 0;
+ latch->share++;
+ __sync_lock_release (latch->mutex);
return;
}
}
void bt_spinwritelock(BtLatch *latch, int private)
{
+ushort decr = 0;
uint prev;
if( private )
while( 1 ) {
// obtain latch mutex
- if( __sync_fetch_and_or((uint *)latch, Mutex) & Mutex ) {
+ while( __sync_lock_test_and_set(latch->mutex, 1) )
sched_yield();
- continue;
- }
+
+ if( decr )
+ latch->wcnt--, decr = 0;
// wait for write and reader count to clear
if( latch->write || latch->share ) {
- __sync_fetch_and_add ((uint *)latch, PendWr);
- prev = __sync_fetch_and_and ((uint *)latch, ~Mutex) & ~Mutex;
+ latch->writewait = 1;
+ latch->wcnt++;
+ prev = *(uint *)latch & ~1;
+ __sync_lock_release (latch->mutex);
sys_futex( (uint *)latch, FUTEX_WAIT_BITSET | private, prev, NULL, NULL, QueWr );
- __sync_fetch_and_sub ((uint *)latch, PendWr);
+ decr = 1;
continue;
}
// take write mutex
// release latch mutex
- __sync_fetch_and_or ((uint *)latch, Write);
- __sync_fetch_and_and ((uint *)latch, ~Mutex);
+ if( !latch->wcnt )
+ latch->writewait = 0;
+
+ latch->write = 1;
+ __sync_lock_release (latch->mutex);
return;
}
}
// try for mutex,
// abandon request if not taken
- if( __sync_fetch_and_or((uint *)latch, Mutex) & Mutex )
+ if( __sync_lock_test_and_set(latch->mutex, 1) )
return 0;
// see if write mode is available
- if( !latch->write && !latch->share ) {
- __sync_fetch_and_or ((uint *)latch, Write);
- ans = 1;
- } else
+ if( !latch->write && !latch->share )
+ ans = latch->write = 1;
+ else
ans = 0;
// release latch mutex
- __sync_fetch_and_and ((uint *)latch, ~Mutex);
+ __sync_lock_release (latch->mutex);
return ans;
}
// obtain latch mutex
- while( __sync_fetch_and_or((uint *)latch, Mutex) & Mutex )
+ while( __sync_lock_test_and_set(latch->mutex, 1) )
sched_yield();
- __sync_fetch_and_and ((uint *)latch, ~Write);
+ latch->write = 0;
// favor writers
- if( latch->writewait )
+ if( latch->wcnt )
if( sys_futex( (uint *)latch, FUTEX_WAKE_BITSET | private, 1, NULL, NULL, QueWr ) )
goto wakexit;
- if( latch->readwait )
+ if( latch->rcnt )
sys_futex( (uint *)latch, FUTEX_WAKE_BITSET | private, INT_MAX, NULL, NULL, QueRd );
// release latch mutex
wakexit:
- __sync_fetch_and_and ((uint *)latch, ~Mutex);
+ __sync_lock_release (latch->mutex);
}
// decrement reader count
// obtain latch mutex
- while( __sync_fetch_and_or((uint *)latch, Mutex) & Mutex )
+ while( __sync_lock_test_and_set(latch->mutex, 1) )
sched_yield();
- __sync_fetch_and_sub ((uint *)latch, Share);
+ latch->share--;
- // wake waiting writers
+ // wake one waiting writer
- if( !latch->share && latch->writewait )
+ if( !latch->share && latch->wcnt )
sys_futex( (uint *)latch, FUTEX_WAKE_BITSET | private, 1, NULL, NULL, QueWr );
// release latch mutex
- __sync_fetch_and_and ((uint *)latch, ~Mutex);
+ __sync_lock_release (latch->mutex);
}
// link latch table entry into latch hash table
close (mgr->idx);
free (mgr->pool);
free (mgr->hash);
- free (mgr->latch);
+ free ((void *)mgr->latch);
free (mgr);
#else
FlushFileBuffers(mgr->idx);
CloseHandle(mgr->idx);
GlobalFree (mgr->pool);
GlobalFree (mgr->hash);
- GlobalFree (mgr->latch);
+ GlobalFree ((void *)mgr->latch);
GlobalFree (mgr);
#endif
}
void bt_close (BtDb *bt)
{
#ifdef unix
- if ( bt->mem )
+ if( bt->mem )
free (bt->mem);
#else
- if ( bt->mem)
+ if( bt->mem)
VirtualFree (bt->mem, 0, MEM_RELEASE);
#endif
free (bt);
latchmgr->alloc->bits = mgr->page_bits;
for( lvl=MIN_lvl; lvl--; ) {
- slotptr(latchmgr->alloc, 1)->off = offsetof(struct BtPage_, fence);
+ slotptr(latchmgr->alloc, 1)->off = mgr->page_size - 3;
bt_putid(slotptr(latchmgr->alloc, 1)->id, lvl ? MIN_lvl - lvl + 1 : 0); // next(lower) page number
- latchmgr->alloc->fence[0] = 2; // create stopper key
- latchmgr->alloc->fence[1] = 0xff;
- latchmgr->alloc->fence[2] = 0xff;
- latchmgr->alloc->min = mgr->page_size;
+ key = keyptr(latchmgr->alloc, 1);
+ key->len = 2; // create stopper key
+ key->key[0] = 0xff;
+ key->key[1] = 0xff;
+ latchmgr->alloc->min = mgr->page_size - 3;
latchmgr->alloc->lvl = lvl;
latchmgr->alloc->cnt = 1;
latchmgr->alloc->act = 1;
pool->hashprev = pool->hashnext = NULL;
pool->basepage = page_no & ~bt->mgr->poolmask;
- pool->lru = 1;
+ pool->pin = CLOCK_bit + 1;
if( slot = bt->mgr->hash[idx] ) {
node = bt->mgr->pool + slot;
bt->mgr->hash[idx] = pool->slot;
}
-// find best segment to evict from buffer pool
-
-BtPool *bt_findlru (BtDb *bt, uint hashslot)
-{
-unsigned long long int target = ~0LL;
-BtPool *pool = NULL, *node;
-
- if( !hashslot )
- return NULL;
-
- node = bt->mgr->pool + hashslot;
-
- // scan pool entries under hash table slot
-
- do {
- if( node->pin )
- continue;
- if( node->lru > target )
- continue;
- target = node->lru;
- pool = node;
- } while( node = node->hashnext );
-
- return pool;
-}
-
// map new buffer pool segment to virtual memory
BTERR bt_mapsegment(BtDb *bt, BtPool *pool, uid page_no)
#ifdef unix
flag = PROT_READ | ( bt->mgr->mode == BT_ro ? 0 : PROT_WRITE );
- pool->map = mmap (0, (bt->mgr->poolmask+1) << bt->mgr->page_bits, flag, MAP_SHARED | MAP_POPULATE, bt->mgr->idx, off);
+ pool->map = mmap (0, (bt->mgr->poolmask+1) << bt->mgr->page_bits, flag, MAP_SHARED, bt->mgr->idx, off);
if( pool->map == MAP_FAILED )
return bt->err = BTERR_map;
BtPool *bt_pinpool(BtDb *bt, uid page_no)
{
+uint slot, hashidx, idx, victim;
BtPool *pool, *node, *next;
-uint slot, idx, victim;
// lock hash table chain
- idx = (uint)(page_no >> bt->mgr->seg_bits) % bt->mgr->hashsize;
- bt_spinreadlock (&bt->mgr->latch[idx], 1);
+ hashidx = (uint)(page_no >> bt->mgr->seg_bits) % bt->mgr->hashsize;
+ bt_spinreadlock (&bt->mgr->latch[hashidx], 1);
// look up in hash table
- if( pool = bt_findpool(bt, page_no, idx) ) {
+ if( pool = bt_findpool(bt, page_no, hashidx) ) {
#ifdef unix
+ __sync_fetch_and_or(&pool->pin, CLOCK_bit);
__sync_fetch_and_add(&pool->pin, 1);
#else
+ _InterlockedOr16 (&pool->pin, CLOCK_bit);
_InterlockedIncrement16 (&pool->pin);
#endif
- bt_spinreleaseread (&bt->mgr->latch[idx], 1);
- pool->lru++;
- return pool;
- }
-
- // upgrade to write lock
-
- bt_spinreleaseread (&bt->mgr->latch[idx], 1);
- bt_spinwritelock (&bt->mgr->latch[idx], 1);
-
- // try to find page in pool with write lock
-
- if( pool = bt_findpool(bt, page_no, idx) ) {
-#ifdef unix
- __sync_fetch_and_add(&pool->pin, 1);
-#else
- _InterlockedIncrement16 (&pool->pin);
-#endif
- bt_spinreleasewrite (&bt->mgr->latch[idx], 1);
- pool->lru++;
+ bt_spinreleaseread (&bt->mgr->latch[hashidx], 1);
return pool;
}
if( bt_mapsegment(bt, pool, page_no) )
return NULL;
- bt_linkhash(bt, pool, page_no, idx);
-#ifdef unix
- __sync_fetch_and_add(&pool->pin, 1);
-#else
- _InterlockedIncrement16 (&pool->pin);
-#endif
- bt_spinreleasewrite (&bt->mgr->latch[idx], 1);
+ bt_linkhash(bt, pool, page_no, hashidx);
+ bt_spinreleasewrite (&bt->mgr->latch[hashidx], 1);
return pool;
}
#else
victim = _InterlockedIncrement16 (&bt->mgr->evicted) - 1;
#endif
- victim %= bt->mgr->hashsize;
+ victim %= bt->mgr->poolmax;
+ pool = bt->mgr->pool + victim;
+ idx = (uint)(pool->basepage >> bt->mgr->seg_bits) % bt->mgr->hashsize;
+
+ if( !victim )
+ continue;
// try to get write lock
// skip entry if not obtained
- if( !bt_spinwritetry (&bt->mgr->latch[victim]) )
+ if( !bt_spinwritetry (&bt->mgr->latch[idx]) )
continue;
- // if pool entry is empty
- // or any pages are pinned
- // skip this entry
+ // skip this entry if
+ // page is pinned
+ // or clock bit is set
- if( !(pool = bt_findlru(bt, bt->mgr->hash[victim])) ) {
- bt_spinreleasewrite (&bt->mgr->latch[victim], 1);
+ if( pool->pin ) {
+#ifdef unix
+ __sync_fetch_and_and(&pool->pin, ~CLOCK_bit);
+#else
+ _InterlockedAnd16 (&pool->pin, ~CLOCK_bit);
+#endif
+ bt_spinreleasewrite (&bt->mgr->latch[idx], 1);
continue;
}
if( node = pool->hashprev )
node->hashnext = pool->hashnext;
else if( node = pool->hashnext )
- bt->mgr->hash[victim] = node->slot;
+ bt->mgr->hash[idx] = node->slot;
else
- bt->mgr->hash[victim] = 0;
+ bt->mgr->hash[idx] = 0;
if( node = pool->hashnext )
node->hashprev = pool->hashprev;
- bt_spinreleasewrite (&bt->mgr->latch[victim], 1);
+ bt_spinreleasewrite (&bt->mgr->latch[idx], 1);
// remove old file mapping
#ifdef unix
if( bt_mapsegment(bt, pool, page_no) )
return NULL;
- bt_linkhash(bt, pool, page_no, idx);
-#ifdef unix
- __sync_fetch_and_add(&pool->pin, 1);
-#else
- _InterlockedIncrement16 (&pool->pin);
-#endif
- bt_spinreleasewrite (&bt->mgr->latch[idx], 1);
+ bt_linkhash(bt, pool, page_no, hashidx);
+ bt_spinreleasewrite (&bt->mgr->latch[hashidx], 1);
return pool;
}
}
case BtLockParent:
bt_spinwritelock (set->parent, 0);
break;
+ case BtLockParentWrt:
+ bt_spinwritelock (set->parent, 0);
+ bt_spinwritelock (set->readwr, 0);
+ break;
}
}
case BtLockParent:
bt_spinreleasewrite (set->parent, 0);
break;
+ case BtLockParentWrt:
+ bt_spinreleasewrite (set->parent, 0);
+ bt_spinreleasewrite (set->readwr, 0);
+ break;
}
}
reuse = 0;
}
#ifdef unix
- if ( pwrite(bt->mgr->idx, page, bt->mgr->page_size, new_page << bt->mgr->page_bits) < bt->mgr->page_size )
+ if( pwrite(bt->mgr->idx, page, bt->mgr->page_size, new_page << bt->mgr->page_bits) < bt->mgr->page_size )
return bt->err = BTERR_wrt, 0;
// if writing first page of pool block, zero last page in the block
- if ( !reuse && bt->mgr->poolmask > 0 && (new_page & bt->mgr->poolmask) == 0 )
+ if( !reuse && bt->mgr->poolmask > 0 && (new_page & bt->mgr->poolmask) == 0 )
{
// use zero buffer to write zeros
- if ( pwrite(bt->mgr->idx,bt->zero, bt->mgr->page_size, (new_page | bt->mgr->poolmask) << bt->mgr->page_bits) < bt->mgr->page_size )
+ if( pwrite(bt->mgr->idx,bt->zero, bt->mgr->page_size, (new_page | bt->mgr->poolmask) << bt->mgr->page_bits) < bt->mgr->page_size )
return bt->err = BTERR_wrt, 0;
}
#else
int bt_findslot (BtPageSet *set, unsigned char *key, uint len)
{
uint diff, higher = set->page->cnt, low = 1, slot;
+uint good = 0;
// make stopper key an infinite fence value
if( bt_getid (set->page->right) )
higher++;
+ else
+ good++;
// low is the lowest candidate.
// loop ends when they meet
// higher is already
- // tested as .ge. the given key.
+ // tested as .ge. the passed key.
while( diff = higher - low ) {
slot = low + ( diff >> 1 );
if( keycmp (keyptr(set->page, slot), key, len) < 0 )
low = slot + 1;
else
- higher = slot;
+ higher = slot, good++;
}
- if( higher <= set->page->cnt )
- return higher;
-
- // if leaf page, compare against fence value
-
// return zero if key is on right link page
- // or return slot beyond last key
-
- if( set->page->lvl || keycmp ((BtKey)set->page->fence, key, len) < 0 )
- return 0;
- return higher;
+ return good ? higher : 0;
}
// find and load page at given level for given key
// leave page rd or wr locked as requested
-int bt_loadpage (BtDb *bt, BtPageSet *set, unsigned char *key, uint len, uint lvl, uint lock)
+int bt_loadpage (BtDb *bt, BtPageSet *set, unsigned char *key, uint len, uint lvl, BtLock lock)
{
uid page_no = ROOT_page, prevpage = 0;
uint drill = 0xff, slot;
do {
// determine lock mode of drill level
- mode = (lock == BtLockWrite) && (drill == lvl) ? BtLockWrite : BtLockRead;
+ mode = (drill == lvl) ? lock : BtLockRead;
set->latch = bt_pinlatch (bt, page_no);
set->page_no = page_no;
bt_lockpage(mode, set->latch);
+ if( set->page->free )
+ return bt->err = BTERR_struct, 0;
+
if( page_no > ROOT_page )
bt_unlockpage(BtLockAccess, set->latch);
// re-read and re-lock root after determining actual level of root
if( set->page->lvl != drill) {
- if ( set->page_no != ROOT_page )
+ if( set->page_no != ROOT_page )
return bt->err = BTERR_struct, 0;
drill = set->page->lvl;
- if( lock == BtLockWrite && drill == lvl ) {
+ if( lock != BtLockRead && drill == lvl ) {
bt_unlockpage(mode, set->latch);
bt_unpinlatch (set->latch);
bt_unpinpool (set->pool);
prevpool = set->pool;
prevmode = mode;
- // if page is being deleted and we should continue right
-
- if( set->page->kill && set->page->goright ) {
- page_no = bt_getid (set->page->right);
- continue;
- }
-
- // otherwise, wait for deleted node to clear
-
- if( set->page->kill ) {
- bt_unlockpage(mode, set->latch);
- bt_unpinlatch (set->latch);
- bt_unpinpool (set->pool);
- page_no = ROOT_page;
- prevpage = 0;
- drill = 0xff;
-#ifdef unix
- sched_yield();
-#else
- SwitchToThread();
-#endif
- continue;
- }
-
// find key on page at this level
// and descend to requested level
- if( slot = bt_findslot (set, key, len) ) {
+ if( !set->page->kill )
+ if( slot = bt_findslot (set, key, len) ) {
if( drill == lvl )
return slot;
- if( slot > set->page->cnt )
- return bt->err = BTERR_struct;
-
while( slotptr(set->page, slot)->dead )
if( slot++ < set->page->cnt )
continue;
else
- return bt->err = BTERR_struct, 0;
+ goto slideright;
page_no = bt_getid(slotptr(set->page, slot)->id);
drill--;
continue;
- }
+ }
// or slide right into next page
+slideright:
page_no = bt_getid(set->page->right);
} while( page_no );
return 0; // return error
}
-// drill down fixing fence values for left sibling tree
-
-// call with set write locked
-// return with set unlocked & unpinned.
-
-BTERR bt_fixfences (BtDb *bt, BtPageSet *set, unsigned char *newfence)
-{
-unsigned char oldfence[256];
-BtPageSet next[1];
-int chk;
-
- memcpy (oldfence, set->page->fence, 256);
-
- while( !set->page->kill && set->page->lvl ) {
- next->page_no = bt_getid(slotptr(set->page, set->page->cnt)->id);
- next->latch = bt_pinlatch (bt, next->page_no);
- bt_lockpage (BtLockParent, next->latch);
- bt_lockpage (BtLockAccess, next->latch);
- bt_lockpage (BtLockWrite, next->latch);
- bt_unlockpage (BtLockAccess, next->latch);
-
- if( next->pool = bt_pinpool (bt, next->page_no) )
- next->page = bt_page (bt, next->pool, next->page_no);
- else
- return bt->err;
-
- chk = keycmp ((BtKey)next->page->fence, oldfence + 1, *oldfence);
-
- if( chk < 0 ) {
- next->page_no = bt_getid (next->page->right);
- bt_unlockpage (BtLockWrite, next->latch);
- bt_unlockpage (BtLockParent, next->latch);
- bt_unpinlatch (next->latch);
- bt_unpinpool (next->pool);
- continue;
- }
-
- if( chk > 0 )
- return bt->err = BTERR_struct;
-
- if( bt_fixfences (bt, next, newfence) )
- return bt->err;
-
- break;
- }
-
- memcpy (set->page->fence, newfence, 256);
-
- bt_unlockpage (BtLockWrite, set->latch);
- bt_unlockpage (BtLockParent, set->latch);
- bt_unpinlatch (set->latch);
- bt_unpinpool (set->pool);
- return 0;
-}
-
// return page to free list
// page must be delete & write locked
bt_spinreleasewrite (bt->mgr->latchmgr->lock, 0);
}
-// remove the root level by promoting its only child
-// call with parent and child pages
+// a fence key was deleted from a page
+// push new fence value upwards
-BTERR bt_removeroot (BtDb *bt, BtPageSet *root, BtPageSet *child)
+BTERR bt_fixfence (BtDb *bt, BtPageSet *set, uint lvl)
{
-uid next = 0;
-
- do {
- if( next ) {
- child->latch = bt_pinlatch (bt, next);
- bt_lockpage (BtLockDelete, child->latch);
- bt_lockpage (BtLockWrite, child->latch);
-
- if( child->pool = bt_pinpool (bt, next) )
- child->page = bt_page (bt, child->pool, next);
- else
- return bt->err;
-
- child->page_no = next;
- }
-
- memcpy (root->page, child->page, bt->mgr->page_size);
- next = bt_getid (slotptr(child->page, child->page->cnt)->id);
- bt_freepage (bt, child);
- } while( root->page->lvl > 1 && root->page->cnt == 1 );
+unsigned char leftkey[256], rightkey[256];
+uid page_no;
+BtKey ptr;
+uint idx;
- bt_unlockpage (BtLockWrite, root->latch);
- bt_unpinlatch (root->latch);
- bt_unpinpool (root->pool);
- return 0;
-}
+ // remove the old fence value
-// pull right page over ourselves in simple merge
+ ptr = keyptr(set->page, set->page->cnt);
+ memcpy (rightkey, ptr, ptr->len + 1);
-BTERR bt_mergeright (BtDb *bt, BtPageSet *set, BtPageSet *parent, BtPageSet *right, uint slot, uint idx)
-{
- // install ourselves as child page
- // and delete ourselves from parent
+ memset (slotptr(set->page, set->page->cnt--), 0, sizeof(BtSlot));
+ set->page->dirty = 1;
- bt_putid (slotptr(parent->page, idx)->id, set->page_no);
- slotptr(parent->page, slot)->dead = 1;
- parent->page->act--;
+ ptr = keyptr(set->page, set->page->cnt);
+ memcpy (leftkey, ptr, ptr->len + 1);
+ page_no = set->page_no;
- // collapse any empty slots
+ bt_unlockpage (BtLockWrite, set->latch);
- while( idx = parent->page->cnt - 1 )
- if( slotptr(parent->page, idx)->dead ) {
- *slotptr(parent->page, idx) = *slotptr(parent->page, idx + 1);
- memset (slotptr(parent->page, parent->page->cnt--), 0, sizeof(BtSlot));
- } else
- break;
+ // insert new (now smaller) fence key
- memcpy (set->page, right->page, bt->mgr->page_size);
- bt_unlockpage (BtLockParent, right->latch);
+ if( bt_insertkey (bt, leftkey+1, *leftkey, lvl+1, page_no, time(NULL)) )
+ return bt->err;
- bt_freepage (bt, right);
-
- // do we need to remove a btree level?
- // (leave the first page of leaves alone)
+ // now delete old fence key
- if( parent->page_no == ROOT_page && parent->page->cnt == 1 )
- if( set->page->lvl )
- return bt_removeroot (bt, parent, set);
+ if( bt_deletekey (bt, rightkey+1, *rightkey, lvl+1) )
+ return bt->err;
- bt_unlockpage (BtLockWrite, parent->latch);
- bt_unlockpage (BtLockDelete, set->latch);
- bt_unlockpage (BtLockWrite, set->latch);
- bt_unpinlatch (parent->latch);
- bt_unpinpool (parent->pool);
- bt_unpinlatch (set->latch);
+ bt_unlockpage (BtLockParent, set->latch);
+ bt_unpinlatch(set->latch);
bt_unpinpool (set->pool);
return 0;
}
-// remove both child and parent from the btree
-// from the fence position in the parent
-// call with both pages locked for writing
+// root has a single child
+// collapse a level from the tree
-BTERR bt_removeparent (BtDb *bt, BtPageSet *child, BtPageSet *parent, BtPageSet *right, BtPageSet *rparent, uint lvl)
+BTERR bt_collapseroot (BtDb *bt, BtPageSet *root)
{
-unsigned char pagefence[256];
+BtPageSet child[1];
uint idx;
- // pull right sibling over ourselves and unlock
-
- memcpy (child->page, right->page, bt->mgr->page_size);
-
- bt_unlockpage (BtLockWrite, child->latch);
- bt_unpinlatch (child->latch);
- bt_unpinpool (child->pool);
-
- // install ourselves into right link of old right page
-
- bt_putid (right->page->right, child->page_no);
- right->page->goright = 1; // tell bt_loadpage to go right to us
- right->page->kill = 1;
-
- bt_unlockpage (BtLockWrite, right->latch);
-
- // remove our slot from our parent
- // signal to move right
+ // find the child entry and promote as new root contents
- parent->page->goright = 1; // tell bt_loadpage to go right to rparent
- parent->page->kill = 1;
- parent->page->act--;
-
- // redirect right page pointer in right parent to us
-
- for( idx = 0; idx++ < rparent->page->cnt; )
- if( !slotptr(rparent->page, idx)->dead )
+ do {
+ for( idx = 0; idx++ < root->page->cnt; )
+ if( !slotptr(root->page, idx)->dead )
break;
- if( bt_getid (slotptr(rparent->page, idx)->id) != right->page_no )
- return bt->err = BTERR_struct;
-
- bt_putid (slotptr(rparent->page, idx)->id, child->page_no);
- bt_unlockpage (BtLockWrite, rparent->latch);
- bt_unpinlatch (rparent->latch);
- bt_unpinpool (rparent->pool);
+ child->page_no = bt_getid (slotptr(root->page, idx)->id);
- // free the right page
+ child->latch = bt_pinlatch (bt, child->page_no);
+ bt_lockpage (BtLockDelete, child->latch);
+ bt_lockpage (BtLockWrite, child->latch);
- bt_lockpage (BtLockDelete, right->latch);
- bt_lockpage (BtLockWrite, right->latch);
- bt_freepage (bt, right);
+ if( child->pool = bt_pinpool (bt, child->page_no) )
+ child->page = bt_page (bt, child->pool, child->page_no);
+ else
+ return bt->err;
- // save parent page fence value
+ memcpy (root->page, child->page, bt->mgr->page_size);
+ bt_freepage (bt, child);
- memcpy (pagefence, parent->page->fence, 256);
- bt_unlockpage (BtLockWrite, parent->latch);
+ } while( root->page->lvl > 1 && root->page->act == 1 );
- return bt_removepage (bt, parent, lvl, pagefence);
+ bt_unlockpage (BtLockParentWrt, root->latch);
+ bt_unpinlatch (root->latch);
+ bt_unpinpool (root->pool);
+ return 0;
}
-// remove page from btree
-// call with page unlocked
-// returns with page on free list
+// find and delete key on page by marking delete flag bit
+// if page becomes empty, delete it from the btree
-BTERR bt_removepage (BtDb *bt, BtPageSet *set, uint lvl, unsigned char *pagefence)
+BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl)
{
-BtPageSet parent[1], sibling[1], rparent[1];
-unsigned char newfence[256];
-uint slot, idx;
+unsigned char lowerfence[256], higherfence[256];
+uint slot, idx, dirty = 0, fence, found;
+BtPageSet set[1], right[1];
BtKey ptr;
- // load and lock our parent
-
-retry:
- if( !(slot = bt_loadpage (bt, parent, pagefence+1, *pagefence, lvl+1, BtLockWrite)) )
+ if( slot = bt_loadpage (bt, set, key, len, lvl, BtLockParentWrt) )
+ ptr = keyptr(set->page, slot);
+ else
return bt->err;
- // can we do a simple merge entirely
- // between siblings on the parent page?
-
- if( slot < parent->page->cnt ) {
- // find our right neighbor
- // right must exist because the stopper prevents
- // the rightmost page from deleting
-
- for( idx = slot; idx++ < parent->page->cnt; )
- if( !slotptr(parent->page, idx)->dead )
- break;
-
- sibling->page_no = bt_getid (slotptr (parent->page, idx)->id);
-
- bt_lockpage (BtLockDelete, set->latch);
- bt_lockpage (BtLockWrite, set->latch);
-
- // merge right if sibling shows up in
- // our parent and is not being killed
-
- if( sibling->page_no == bt_getid (set->page->right) ) {
- sibling->latch = bt_pinlatch (bt, sibling->page_no);
- bt_lockpage (BtLockParent, sibling->latch);
- bt_lockpage (BtLockDelete, sibling->latch);
- bt_lockpage (BtLockWrite, sibling->latch);
-
- if( sibling->pool = bt_pinpool (bt, sibling->page_no) )
- sibling->page = bt_page (bt, sibling->pool, sibling->page_no);
- else
- return bt->err;
-
- if( !sibling->page->kill )
- return bt_mergeright(bt, set, parent, sibling, slot, idx);
-
- // try again later
-
- bt_unlockpage (BtLockWrite, sibling->latch);
- bt_unlockpage (BtLockParent, sibling->latch);
- bt_unlockpage (BtLockDelete, sibling->latch);
- bt_unpinlatch (sibling->latch);
- bt_unpinpool (sibling->pool);
- }
-
- bt_unlockpage (BtLockDelete, set->latch);
- bt_unlockpage (BtLockWrite, set->latch);
- bt_unlockpage (BtLockWrite, parent->latch);
- bt_unpinlatch (parent->latch);
- bt_unpinpool (parent->pool);
-#ifdef linux
- sched_yield();
-#else
- SwitchToThread();
-#endif
- goto retry;
- }
-
- // find our left neighbor in our parent page
-
- for( idx = slot; --idx; )
- if( !slotptr(parent->page, idx)->dead )
- break;
-
- // if no left neighbor, delete ourselves and our parent
-
- if( !idx ) {
- bt_lockpage (BtLockAccess, set->latch);
- bt_lockpage (BtLockWrite, set->latch);
- bt_unlockpage (BtLockAccess, set->latch);
-
- rparent->page_no = bt_getid (parent->page->right);
- rparent->latch = bt_pinlatch (bt, rparent->page_no);
-
- bt_lockpage (BtLockAccess, rparent->latch);
- bt_lockpage (BtLockWrite, rparent->latch);
- bt_unlockpage (BtLockAccess, rparent->latch);
-
- if( rparent->pool = bt_pinpool (bt, rparent->page_no) )
- rparent->page = bt_page (bt, rparent->pool, rparent->page_no);
- else
- return bt->err;
-
- if( !rparent->page->kill ) {
- sibling->page_no = bt_getid (set->page->right);
- sibling->latch = bt_pinlatch (bt, sibling->page_no);
-
- bt_lockpage (BtLockAccess, sibling->latch);
- bt_lockpage (BtLockWrite, sibling->latch);
- bt_unlockpage (BtLockAccess, sibling->latch);
-
- if( sibling->pool = bt_pinpool (bt, sibling->page_no) )
- sibling->page = bt_page (bt, sibling->pool, sibling->page_no);
- else
- return bt->err;
-
- if( !sibling->page->kill )
- return bt_removeparent (bt, set, parent, sibling, rparent, lvl+1);
+ // are we deleting a fence slot?
- // try again later
+ fence = slot == set->page->cnt;
- bt_unlockpage (BtLockWrite, sibling->latch);
- bt_unpinlatch (sibling->latch);
- bt_unpinpool (sibling->pool);
- }
+ // if key is found delete it, otherwise ignore request
- bt_unlockpage (BtLockWrite, set->latch);
- bt_unlockpage (BtLockWrite, rparent->latch);
- bt_unpinlatch (rparent->latch);
- bt_unpinpool (rparent->pool);
+ if( found = !keycmp (ptr, key, len) )
+ if( found = slotptr(set->page, slot)->dead == 0 ) {
+ dirty = slotptr(set->page, slot)->dead = 1;
+ set->page->dirty = 1;
+ set->page->act--;
- bt_unlockpage (BtLockWrite, parent->latch);
- bt_unpinlatch (parent->latch);
- bt_unpinpool (parent->pool);
-#ifdef linux
- sched_yield();
-#else
- SwitchToThread();
-#endif
- goto retry;
- }
+ // collapse empty slots
- // redirect parent to our left sibling
- // lock and map our left sibling's page
+ while( idx = set->page->cnt - 1 )
+ if( slotptr(set->page, idx)->dead ) {
+ *slotptr(set->page, idx) = *slotptr(set->page, idx + 1);
+ memset (slotptr(set->page, set->page->cnt--), 0, sizeof(BtSlot));
+ } else
+ break;
+ }
- sibling->page_no = bt_getid (slotptr(parent->page, idx)->id);
- sibling->latch = bt_pinlatch (bt, sibling->page_no);
+ // did we delete a fence key in an upper level?
- // wait our turn on fence key maintenance
+ if( dirty && lvl && set->page->act && fence )
+ if( bt_fixfence (bt, set, lvl) )
+ return bt->err;
+ else
+ return bt->found = found, 0;
- bt_lockpage(BtLockParent, sibling->latch);
- bt_lockpage(BtLockAccess, sibling->latch);
- bt_lockpage(BtLockWrite, sibling->latch);
- bt_unlockpage(BtLockAccess, sibling->latch);
+ // is this a collapsed root?
- if( sibling->pool = bt_pinpool (bt, sibling->page_no) )
- sibling->page = bt_page (bt, sibling->pool, sibling->page_no);
- else
+ if( lvl > 1 && set->page_no == ROOT_page && set->page->act == 1 )
+ if( bt_collapseroot (bt, set) )
return bt->err;
+ else
+ return bt->found = found, 0;
- // wait until left sibling is in our parent
+ // return if page is not empty
- if( bt_getid (sibling->page->right) != set->page_no ) {
- bt_unlockpage (BtLockWrite, parent->latch);
- bt_unlockpage (BtLockWrite, sibling->latch);
- bt_unlockpage (BtLockParent, sibling->latch);
- bt_unpinlatch (parent->latch);
- bt_unpinpool (parent->pool);
- bt_unpinlatch (sibling->latch);
- bt_unpinpool (sibling->pool);
-#ifdef linux
- sched_yield();
-#else
- SwitchToThread();
-#endif
- goto retry;
+ if( set->page->act ) {
+ bt_unlockpage(BtLockParentWrt, set->latch);
+ bt_unpinlatch (set->latch);
+ bt_unpinpool (set->pool);
+ return bt->found = found, 0;
}
- // delete our left sibling from parent
-
- slotptr(parent->page,idx)->dead = 1;
- parent->page->dirty = 1;
- parent->page->act--;
-
- // redirect our parent slot to our left sibling
+ // cache copy of fence key
+ // to post in parent
- bt_putid (slotptr(parent->page, slot)->id, sibling->page_no);
- memcpy (sibling->page->right, set->page->right, BtId);
+ ptr = keyptr(set->page, set->page->cnt);
+ memcpy (lowerfence, ptr, ptr->len + 1);
- // collapse dead slots from parent
+ // obtain lock on right page
- while( idx = parent->page->cnt - 1 )
- if( slotptr(parent->page, idx)->dead ) {
- *slotptr(parent->page, idx) = *slotptr(parent->page, parent->page->cnt);
- memset (slotptr(parent->page, parent->page->cnt--), 0, sizeof(BtSlot));
- } else
- break;
+ right->page_no = bt_getid(set->page->right);
+ right->latch = bt_pinlatch (bt, right->page_no);
+ bt_lockpage (BtLockParentWrt, right->latch);
- // free our original page
-
- bt_lockpage (BtLockDelete, set->latch);
- bt_lockpage (BtLockWrite, set->latch);
- bt_freepage (bt, set);
-
- // go down the left node's fence keys to the leaf level
- // and update the fence keys in each page
-
- memcpy (newfence, parent->page->fence, 256);
-
- if( bt_fixfences (bt, sibling, newfence) )
- return bt->err;
-
- // promote sibling as new root?
-
- if( parent->page_no == ROOT_page && parent->page->cnt == 1 )
- if( sibling->page->lvl ) {
- sibling->latch = bt_pinlatch (bt, sibling->page_no);
- bt_lockpage (BtLockDelete, sibling->latch);
- bt_lockpage (BtLockWrite, sibling->latch);
+ // pin page contents
- if( sibling->pool = bt_pinpool (bt, sibling->page_no) )
- sibling->page = bt_page (bt, sibling->pool, sibling->page_no);
- else
- return bt->err;
+ if( right->pool = bt_pinpool (bt, right->page_no) )
+ right->page = bt_page (bt, right->pool, right->page_no);
+ else
+ return 0;
- return bt_removeroot (bt, parent, sibling);
- }
+ if( right->page->kill )
+ return bt->err = BTERR_struct;
- bt_unlockpage (BtLockWrite, parent->latch);
- bt_unpinlatch (parent->latch);
- bt_unpinpool (parent->pool);
+ // pull contents of right peer into our empty page
- return 0;
-}
+ memcpy (set->page, right->page, bt->mgr->page_size);
-// find and delete key on page by marking delete flag bit
-// if page becomes empty, delete it from the btree
+ // cache copy of key to update
-BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len)
-{
-unsigned char pagefence[256];
-uint slot, idx, found;
-BtPageSet set[1];
-BtKey ptr;
+ ptr = keyptr(right->page, right->page->cnt);
+ memcpy (higherfence, ptr, ptr->len + 1);
- if( slot = bt_loadpage (bt, set, key, len, 0, BtLockWrite) )
- ptr = keyptr(set->page, slot);
- else
- return bt->err;
+ // mark right page deleted and point it to left page
+ // until we can post parent updates
- // if key is found delete it, otherwise ignore request
+ bt_putid (right->page->right, set->page_no);
+ right->page->kill = 1;
- if( found = slot <= set->page->cnt )
- if( found = !keycmp (ptr, key, len) )
- if( found = slotptr(set->page, slot)->dead == 0 ) {
- slotptr(set->page,slot)->dead = 1;
- set->page->dirty = 1;
- set->page->act--;
+ bt_unlockpage (BtLockWrite, right->latch);
+ bt_unlockpage (BtLockWrite, set->latch);
- // collapse empty slots
+ // redirect higher key directly to our new node contents
- while( idx = set->page->cnt - 1 )
- if( slotptr(set->page, idx)->dead ) {
- *slotptr(set->page, idx) = *slotptr(set->page, idx + 1);
- memset (slotptr(set->page, set->page->cnt--), 0, sizeof(BtSlot));
- } else
- break;
- }
+ if( bt_insertkey (bt, higherfence+1, *higherfence, lvl+1, set->page_no, time(NULL)) )
+ return bt->err;
- if( set->page->act ) {
- bt_unlockpage(BtLockWrite, set->latch);
- bt_unpinlatch (set->latch);
- bt_unpinpool (set->pool);
- return bt->found = found, 0;
- }
+ // delete old lower key to our node
- memcpy (pagefence, set->page->fence, 256);
- set->page->kill = 1;
+ if( bt_deletekey (bt, lowerfence+1, *lowerfence, lvl+1) )
+ return bt->err;
- bt_unlockpage (BtLockWrite, set->latch);
+ // obtain delete and write locks to right node
- if( bt_removepage (bt, set, 0, pagefence) )
- return bt->err;
+ bt_unlockpage (BtLockParent, right->latch);
+ bt_lockpage (BtLockDelete, right->latch);
+ bt_lockpage (BtLockWrite, right->latch);
+ bt_freepage (bt, right);
+ bt_unlockpage (BtLockParent, set->latch);
+ bt_unpinlatch (set->latch);
+ bt_unpinpool (set->pool);
bt->found = found;
return 0;
}
{
BtPageSet set[1];
uint slot;
+uid id = 0;
BtKey ptr;
-uid id;
if( slot = bt_loadpage (bt, set, key, len, 0, BtLockRead) )
ptr = keyptr(set->page, slot);
// if key exists, return row-id
// otherwise return 0
- if( !keycmp (ptr, key, len) )
+ if( slot <= set->page->cnt )
+ if( !keycmp (ptr, key, len) )
id = bt_getid(slotptr(set->page,slot)->id);
- else
- id = 0;
bt_unlockpage (BtLockRead, set->latch);
bt_unpinlatch (set->latch);
uint bt_cleanpage(BtDb *bt, BtPage page, uint amt, uint slot)
{
-uint nxt = bt->mgr->page_size, off;
+uint nxt = bt->mgr->page_size;
uint cnt = 0, idx = 0;
uint max = page->cnt;
uint newslot = max;
while( cnt++ < max ) {
if( cnt == slot )
newslot = idx + 1;
- if( slotptr(bt->frame,cnt)->dead )
+ if( cnt < max && slotptr(bt->frame,cnt)->dead )
continue;
- // if its not the fence key,
// copy the key across
- off = slotptr(bt->frame,cnt)->off;
-
- if( off >= sizeof(*page) ) {
- key = keyptr(bt->frame, cnt);
- off = nxt -= key->len + 1;
- memcpy ((unsigned char *)page + nxt, key, key->len + 1);
- }
+ key = keyptr(bt->frame, cnt);
+ nxt -= key->len + 1;
+ memcpy ((unsigned char *)page + nxt, key, key->len + 1);
// copy slot
memcpy(slotptr(page, ++idx)->id, slotptr(bt->frame, cnt)->id, BtId);
+ if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
+ page->act++;
slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
- slotptr(page, idx)->off = off;
- page->act++;
+ slotptr(page, idx)->off = nxt;
}
page->min = nxt;
// split the root and raise the height of the btree
-BTERR bt_splitroot(BtDb *bt, BtPageSet *root, uid page_no2)
+BTERR bt_splitroot(BtDb *bt, BtPageSet *root, unsigned char *leftkey, uid page_no2)
{
uint nxt = bt->mgr->page_size;
-unsigned char leftkey[256];
-uid new_page;
+uid left;
// Obtain an empty page to use, and copy the current
// root contents into it, e.g. lower keys
- memcpy (leftkey, root->page->fence, 256);
- root->page->posted = 1;
-
- if( !(new_page = bt_newpage(bt, root->page)) )
+ if( !(left = bt_newpage(bt, root->page)) )
return bt->err;
// preserve the page info at the bottom
// of higher keys and set rest to zero
memset(root->page+1, 0, bt->mgr->page_size - sizeof(*root->page));
- memset(root->page->fence, 0, 256);
- root->page->fence[0] = 2;
- root->page->fence[1] = 0xff;
- root->page->fence[2] = 0xff;
- // insert lower keys page fence key on newroot page
+ // insert lower keys page fence key on newroot page as first key
nxt -= *leftkey + 1;
memcpy ((unsigned char *)root->page + nxt, leftkey, *leftkey + 1);
- bt_putid(slotptr(root->page, 1)->id, new_page);
+ bt_putid(slotptr(root->page, 1)->id, left);
slotptr(root->page, 1)->off = nxt;
// insert stopper key on newroot page
// and increase the root height
+ nxt -= 3;
+ ((unsigned char *)root->page)[nxt] = 2;
+ ((unsigned char *)root->page)[nxt+1] = 0xff;
+ ((unsigned char *)root->page)[nxt+2] = 0xff;
bt_putid(slotptr(root->page, 2)->id, page_no2);
- slotptr(root->page, 2)->off = offsetof(struct BtPage_, fence);
+ slotptr(root->page, 2)->off = nxt;
bt_putid(root->page->right, 0);
root->page->min = nxt; // reset lowest used offset and key count
BTERR bt_splitpage (BtDb *bt, BtPageSet *set)
{
-uint cnt = 0, idx = 0, max, nxt = bt->mgr->page_size, off;
-unsigned char fencekey[256];
+uint cnt = 0, idx = 0, max, nxt = bt->mgr->page_size;
+unsigned char fencekey[256], rightkey[256];
uint lvl = set->page->lvl;
-uid right;
+BtPageSet right[1];
+uint prev;
BtKey key;
// split higher half of keys to bt->frame
idx = 0;
while( cnt++ < max ) {
- if( !lvl || cnt < max ) {
- key = keyptr(set->page, cnt);
- off = nxt -= key->len + 1;
- memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
- } else
- off = offsetof(struct BtPage_, fence);
+ key = keyptr(set->page, cnt);
+ nxt -= key->len + 1;
+ memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
memcpy(slotptr(bt->frame,++idx)->id, slotptr(set->page,cnt)->id, BtId);
+ if( !(slotptr(bt->frame, idx)->dead = slotptr(set->page, cnt)->dead) )
+ bt->frame->act++;
slotptr(bt->frame, idx)->tod = slotptr(set->page, cnt)->tod;
- slotptr(bt->frame, idx)->off = off;
- bt->frame->act++;
+ slotptr(bt->frame, idx)->off = nxt;
}
- if( set->page_no == ROOT_page )
- bt->frame->posted = 1;
+ // remember existing fence key for new page to the right
+
+ memcpy (rightkey, key, key->len + 1);
- memcpy (bt->frame->fence, set->page->fence, 256);
bt->frame->bits = bt->mgr->page_bits;
bt->frame->min = nxt;
bt->frame->cnt = idx;
// get new free page and write higher keys to it.
- if( !(right = bt_newpage(bt, bt->frame)) )
+ if( !(right->page_no = bt_newpage(bt, bt->frame)) )
return bt->err;
// update lower keys to continue in old page
memcpy (bt->frame, set->page, bt->mgr->page_size);
memset (set->page+1, 0, bt->mgr->page_size - sizeof(*set->page));
nxt = bt->mgr->page_size;
- set->page->posted = 0;
set->page->dirty = 0;
set->page->act = 0;
cnt = 0;
while( cnt++ < max / 2 ) {
key = keyptr(bt->frame, cnt);
-
- if( !lvl || cnt < max / 2 ) {
- off = nxt -= key->len + 1;
- memcpy ((unsigned char *)set->page + nxt, key, key->len + 1);
- } else
- off = offsetof(struct BtPage_, fence);
-
+ nxt -= key->len + 1;
+ memcpy ((unsigned char *)set->page + nxt, key, key->len + 1);
memcpy(slotptr(set->page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
slotptr(set->page, idx)->tod = slotptr(bt->frame, cnt)->tod;
- slotptr(set->page, idx)->off = off;
+ slotptr(set->page, idx)->off = nxt;
set->page->act++;
}
- // install fence key for smaller key page
+ // remember fence key for smaller page
- memset(set->page->fence, 0, 256);
- memcpy(set->page->fence, key, key->len + 1);
+ memcpy(fencekey, key, key->len + 1);
- bt_putid(set->page->right, right);
+ bt_putid(set->page->right, right->page_no);
set->page->min = nxt;
set->page->cnt = idx;
// if current page is the root page, split it
if( set->page_no == ROOT_page )
- return bt_splitroot (bt, set, right);
-
- bt_unlockpage (BtLockWrite, set->latch);
+ return bt_splitroot (bt, set, fencekey, right->page_no);
// insert new fences in their parent pages
- while( 1 ) {
- bt_lockpage (BtLockParent, set->latch);
- bt_lockpage (BtLockWrite, set->latch);
-
- memcpy (fencekey, set->page->fence, 256);
- right = bt_getid (set->page->right);
-
- if( set->page->posted ) {
- bt_unlockpage (BtLockParent, set->latch);
- bt_unlockpage (BtLockWrite, set->latch);
- bt_unpinlatch (set->latch);
- bt_unpinpool (set->pool);
- return 0;
- }
+ right->latch = bt_pinlatch (bt, right->page_no);
+ bt_lockpage (BtLockParent, right->latch);
- set->page->posted = 1;
- bt_unlockpage (BtLockWrite, set->latch);
+ bt_lockpage (BtLockParent, set->latch);
+ bt_unlockpage (BtLockWrite, set->latch);
- if( bt_insertkey (bt, fencekey+1, *fencekey, set->page_no, time(NULL), lvl+1) )
- return bt->err;
+ // insert new fence for reformulated left block of smaller keys
- bt_unlockpage (BtLockParent, set->latch);
- bt_unpinlatch (set->latch);
- bt_unpinpool (set->pool);
+ if( bt_insertkey (bt, fencekey+1, *fencekey, lvl+1, set->page_no, time(NULL)) )
+ return bt->err;
- if( !(set->page_no = right) )
- break;
+ // switch fence for right block of larger keys to new right page
- set->latch = bt_pinlatch (bt, right);
+ if( bt_insertkey (bt, rightkey+1, *rightkey, lvl+1, right->page_no, time(NULL)) )
+ return bt->err;
- if( set->pool = bt_pinpool (bt, right) )
- set->page = bt_page (bt, set->pool, right);
- else
- return bt->err;
- }
+ bt_unlockpage (BtLockParent, set->latch);
+ bt_unpinlatch (set->latch);
+ bt_unpinpool (set->pool);
+ bt_unlockpage (BtLockParent, right->latch);
+ bt_unpinlatch (right->latch);
return 0;
}
-
// Insert new key into the btree at given level.
-BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uid id, uint tod, uint lvl)
+BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod)
{
BtPageSet set[1];
uint slot, idx;
ptr = keyptr(set->page, slot);
else
{
- if ( !bt->err )
+ if( !bt->err )
bt->err = BTERR_ovflw;
return bt->err;
}
// if key already exists, update id and return
- if( slot <= set->page->cnt )
- if( !keycmp (ptr, key, len) ) {
+ if( !keycmp (ptr, key, len) ) {
if( slotptr(set->page, slot)->dead )
set->page->act++;
slotptr(set->page, slot)->dead = 0;
((unsigned char *)set->page)[set->page->min] = len;
memcpy ((unsigned char *)set->page + set->page->min +1, key, len );
- for( idx = slot; idx <= set->page->cnt; idx++ )
+ for( idx = slot; idx < set->page->cnt; idx++ )
if( slotptr(set->page, idx)->dead )
break;
// now insert key into array before slot
- if( idx > set->page->cnt )
- set->page->cnt++;
+ if( idx == set->page->cnt )
+ idx++, set->page->cnt++;
set->page->act++;
do {
right = bt_getid(bt->cursor->right);
+
while( slot++ < bt->cursor->cnt )
if( slotptr(bt->cursor,slot)->dead )
continue;
bt_unpinlatch (set->latch);
bt_unpinpool (set->pool);
slot = 0;
+
} while( 1 );
return bt->err = 0;
return slotptr(bt->cursor,slot)->tod;
}
-
#ifdef STANDALONE
+#ifndef unix
+double getCpuTime(int type)
+{
+FILETIME crtime[1];
+FILETIME xittime[1];
+FILETIME systime[1];
+FILETIME usrtime[1];
+SYSTEMTIME timeconv[1];
+double ans = 0;
+
+ memset (timeconv, 0, sizeof(SYSTEMTIME));
+
+ switch( type ) {
+ case 0:
+ GetSystemTimeAsFileTime (xittime);
+ FileTimeToSystemTime (xittime, timeconv);
+ ans = (double)timeconv->wDayOfWeek * 3600 * 24;
+ break;
+ case 1:
+ GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
+ FileTimeToSystemTime (usrtime, timeconv);
+ break;
+ case 2:
+ GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
+ FileTimeToSystemTime (systime, timeconv);
+ break;
+ }
+
+ ans += (double)timeconv->wHour * 3600;
+ ans += (double)timeconv->wMinute * 60;
+ ans += (double)timeconv->wSecond;
+ ans += (double)timeconv->wMilliseconds / 1000;
+ return ans;
+}
+#else
+#include <time.h>
+#include <sys/resource.h>
+
+double getCpuTime(int type)
+{
+struct rusage used[1];
+struct timeval tv[1];
+
+ switch( type ) {
+ case 0:
+ gettimeofday(tv, NULL);
+ return (double)tv->tv_sec + (double)tv->tv_usec / 1000000;
+
+ case 1:
+ getrusage(RUSAGE_SELF, used);
+ return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
+
+ case 2:
+ getrusage(RUSAGE_SELF, used);
+ return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
+ }
+
+ return 0;
+}
+#endif
+
void bt_latchaudit (BtDb *bt)
{
ushort idx, hashidx;
+uid next, page_no;
BtLatchSet *latch;
-BtPool *pool;
-BtPage page;
-uid page_no;
+BtKey ptr;
#ifdef unix
- for( idx = 1; idx < bt->mgr->latchmgr->latchdeployed; idx++ ) {
+ if( *(uint *)(bt->mgr->latchmgr->lock) )
+ fprintf(stderr, "Alloc page locked\n");
+ *(uint *)(bt->mgr->latchmgr->lock) = 0;
+
+ for( idx = 1; idx <= bt->mgr->latchmgr->latchdeployed; idx++ ) {
latch = bt->mgr->latchsets + idx;
- if( *(uint *)latch->readwr ) {
- fprintf(stderr, "latchset %d r/w locked for page %.8x\n", idx, latch->page_no);
- *(uint *)latch->readwr = 0;
- }
- if( *(uint *)latch->access ) {
- fprintf(stderr, "latchset %d access locked for page %.8x\n", idx, latch->page_no);
- *(uint *)latch->access = 0;
- }
- if( *(uint *)latch->parent ) {
- fprintf(stderr, "latchset %d parent locked for page %.8x\n", idx, latch->page_no);
- *(uint *)latch->parent = 0;
- }
- if( *(uint *)latch->busy ) {
- fprintf(stderr, "latchset %d busy locked for page %.8x\n", idx, latch->page_no);
- *(uint *)latch->parent = 0;
- }
+ if( *(uint *)latch->readwr )
+ fprintf(stderr, "latchset %d rwlocked for page %.8x\n", idx, latch->page_no);
+ *(uint *)latch->readwr = 0;
+
+ if( *(uint *)latch->access )
+ fprintf(stderr, "latchset %d accesslocked for page %.8x\n", idx, latch->page_no);
+ *(uint *)latch->access = 0;
+
+ if( *(uint *)latch->parent )
+ fprintf(stderr, "latchset %d parentlocked for page %.8x\n", idx, latch->page_no);
+ *(uint *)latch->parent = 0;
+
if( latch->pin ) {
fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
latch->pin = 0;
}
for( hashidx = 0; hashidx < bt->mgr->latchmgr->latchhash; hashidx++ ) {
+ if( *(uint *)(bt->mgr->latchmgr->table[hashidx].latch) )
+ fprintf(stderr, "hash entry %d locked\n", hashidx);
+
+ *(uint *)(bt->mgr->latchmgr->table[hashidx].latch) = 0;
+
if( idx = bt->mgr->latchmgr->table[hashidx].slot ) do {
latch = bt->mgr->latchsets + idx;
- if( latch->hash != hashidx ) {
+ if( *(uint *)latch->busy )
+ fprintf(stderr, "latchset %d busylocked for page %.8x\n", idx, latch->page_no);
+ *(uint *)latch->busy = 0;
+ if( latch->hash != hashidx )
fprintf(stderr, "latchset %d wrong hashidx\n", idx);
- latch->hash = hashidx;
- }
+ if( latch->pin )
+ fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
} while( idx = latch->next );
}
- page_no = bt_getid(bt->mgr->latchmgr->alloc[1].right);
-
- while( page_no ) {
- fprintf(stderr, "free: %.6x\n", (uint)page_no);
-
- if( pool = bt_pinpool (bt, page_no) )
- page = bt_page (bt, pool, page_no);
- else
- return;
-
- page_no = bt_getid(page->right);
- bt_unpinpool (pool);
+ next = bt->mgr->latchmgr->nlatchpage + LATCH_page;
+ page_no = LEAF_page;
+
+ while( page_no < bt_getid(bt->mgr->latchmgr->alloc->right) ) {
+ pread (bt->mgr->idx, bt->frame, bt->mgr->page_size, page_no << bt->mgr->page_bits);
+ if( !bt->frame->free )
+ for( idx = 0; idx++ < bt->frame->cnt - 1; ) {
+ ptr = keyptr(bt->frame, idx+1);
+ if( keycmp (keyptr(bt->frame, idx), ptr->key, ptr->len) >= 0 )
+ fprintf(stderr, "page %.8x idx %.2x out of order\n", page_no, idx);
+ }
+
+ if( page_no > LEAF_page )
+ next = page_no + 1;
+ page_no = next;
}
#endif
}
else if( args->num )
sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
- if( bt_insertkey (bt, key, len, line, *tod, 0) )
+ if( bt_insertkey (bt, key, len, 0, line, *tod) )
fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
len = 0;
}
else if( args->num )
sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
- if( bt_deletekey (bt, key, len) )
+ if( bt_deletekey (bt, key, len, 0) )
fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
len = 0;
}
case 'c':
fprintf(stderr, "started counting\n");
+ next = bt->mgr->latchmgr->nlatchpage + LATCH_page;
+ page_no = LEAF_page;
- do {
- if( set->pool = bt_pinpool (bt, page_no) )
- set->page = bt_page (bt, set->pool, page_no);
- else
- break;
- set->latch = bt_pinlatch (bt, page_no);
- bt_lockpage (BtLockRead, set->latch);
- cnt += set->page->act;
- next = bt_getid (set->page->right);
- bt_unlockpage (BtLockRead, set->latch);
- bt_unpinlatch (set->latch);
- bt_unpinpool (set->pool);
- } while( page_no = next );
+ while( page_no < bt_getid(bt->mgr->latchmgr->alloc->right) ) {
+ uid off = page_no << bt->mgr->page_bits;
+#ifdef unix
+ pread (bt->mgr->idx, bt->frame, bt->mgr->page_size, off);
+#else
+ DWORD amt[1];
+ SetFilePointer (bt->mgr->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
+
+ if( !ReadFile(bt->mgr->idx, bt->frame, bt->mgr->page_size, amt, NULL))
+ return bt->err = BTERR_map;
+
+ if( *amt < bt->mgr->page_size )
+ return bt->err = BTERR_map;
+#endif
+ if( !bt->frame->free && !bt->frame->lvl )
+ cnt += bt->frame->act;
+ if( page_no > LEAF_page )
+ next = page_no + 1;
+ page_no = next;
+ }
+
cnt--; // remove stopper key
fprintf(stderr, " Total keys read %d\n", cnt);
break;
{
int idx, cnt, len, slot, err;
int segsize, bits = 16;
+double start, stop;
#ifdef unix
pthread_t *threads;
-timer start, stop;
#else
-time_t start[1], stop[1];
HANDLE *threads;
#endif
-double real_time;
ThreadArg *args;
uint poolsize = 0;
+float elapsed;
int num = 0;
char key[1];
BtMgr *mgr;
exit(0);
}
-#ifdef unix
- gettimeofday(&start, NULL);
-#else
- time(start);
-#endif
+ start = getCpuTime(0);
if( argc > 3 )
bits = atoi(argv[3]);
#ifdef unix
for( idx = 0; idx < cnt; idx++ )
pthread_join (threads[idx], NULL);
- gettimeofday(&stop, NULL);
- real_time = 1000.0 * ( stop.tv_sec - start.tv_sec ) + 0.001 * (stop.tv_usec - start.tv_usec );
#else
WaitForMultipleObjects (cnt, threads, TRUE, INFINITE);
for( idx = 0; idx < cnt; idx++ )
CloseHandle(threads[idx]);
- time (stop);
- real_time = 1000 * (*stop - *start);
#endif
- fprintf(stderr, " Time to complete: %.2f seconds\n", real_time/1000);
+ elapsed = getCpuTime(0) - start;
+ fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
+ elapsed = getCpuTime(1);
+ fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
+ elapsed = getCpuTime(2);
+ fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
+
bt_mgrclose (mgr);
}