1 // btree version 2v linux futex contention
2 // with combined latch & pool manager
3 // and phase-fair reader writer lock
6 // author: karl malbrain, malbrain@cal.berkeley.edu
9 This work, including the source code, documentation
10 and related data, is placed into the public domain.
12 The orginal author is Karl Malbrain.
14 THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
15 OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
16 MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
17 ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
18 RESULTING FROM THE USE, MODIFICATION, OR
19 REDISTRIBUTION OF THIS SOFTWARE.
22 // Please see the project home page for documentation
23 // code.google.com/p/high-concurrency-btree
25 #define _FILE_OFFSET_BITS 64
26 #define _LARGEFILE64_SOURCE
30 #include <linux/futex.h>
44 #define WIN32_LEAN_AND_MEAN
56 typedef unsigned long long uid;
59 typedef unsigned long long off64_t;
60 typedef unsigned short ushort;
61 typedef unsigned int uint;
64 #define BT_ro 0x6f72 // ro
65 #define BT_rw 0x7772 // rw
66 #define BT_fl 0x6c66 // fl
68 #define BT_maxbits 15 // maximum page size in bits
69 #define BT_minbits 12 // minimum page size in bits
70 #define BT_minpage (1 << BT_minbits) // minimum page size
71 #define BT_maxpage (1 << BT_maxbits) // maximum page size
73 // BTree page number constants
79 // Number of levels to create in a new BTree
85 There are five lock types for each node in three independent sets:
86 1. (set 1) AccessIntent: Sharable. Going to Read the node. Incompatible with NodeDelete.
87 2. (set 1) NodeDelete: Exclusive. About to release the node. Incompatible with AccessIntent.
88 3. (set 2) ReadLock: Sharable. Read the node. Incompatible with WriteLock.
89 4. (set 2) WriteLock: Exclusive. Modify the node. Incompatible with ReadLock and other WriteLocks.
90 5. (set 3) ParentModification: Exclusive. Change the node's parent keys. Incompatible with another ParentModification.
102 QueRd = 1, // reader queue
103 QueWr = 2 // writer queue
106 volatile typedef struct {
107 ushort rin[1]; // readers in count
108 ushort rout[1]; // readers out count
109 ushort ticket[1]; // writers in count
110 ushort serving[1]; // writers out count
113 // define bits at bottom of rin
115 #define PHID 0x1 // writer phase (0/1)
116 #define PRES 0x2 // writer present
117 #define MASK 0x3 // both write bits
118 #define RINC 0x4 // reader increment
130 // Define the length of the page and key pointers
134 // Page key slot definition.
136 // If BT_maxbits is 15 or less, you can save 2 bytes
137 // for each key stored by making the first two uints
138 // into ushorts. You can also save 4 bytes by removing
139 // the tod field from the key.
141 // Keys are marked dead, but remain on the page until
142 // cleanup is called. The fence key (highest key) for
143 // the page is always present, even if dead.
147 uint tod; // time-stamp for key
149 ushort off:BT_maxbits; // page offset for key start
150 ushort dead:1; // set for deleted key
151 unsigned char id[BtId]; // id associated with key
154 // The key structure occupies space at the upper end of
155 // each page. It's a length byte followed by the value
160 unsigned char key[0];
163 // The first part of an index page.
164 // It is immediately followed
165 // by the BtSlot array of keys.
167 typedef struct BtPage_ {
168 uint cnt; // count of keys in page
169 uint act; // count of active keys
170 uint min; // next key offset
171 unsigned char bits:6; // page size in bits
172 unsigned char free:1; // page is on free list
173 unsigned char dirty:1; // page is dirty in cache
174 unsigned char lvl:6; // level of page
175 unsigned char kill:1; // page is being deleted
176 unsigned char clean:1; // page needs cleaning
177 unsigned char right[BtId]; // page number to right
181 struct BtPage_ alloc[2]; // next & free page_nos in right ptr
182 BtMutex lock[1]; // allocation area lite latch
183 volatile uint latchdeployed;// highest number of latch entries deployed
184 volatile uint nlatchpage; // number of latch pages at BT_latch
185 volatile uint latchtotal; // number of page latch entries
186 volatile uint latchhash; // number of latch hash table slots
187 volatile uint latchvictim; // next latch hash entry to examine
188 volatile uint safelevel; // safe page level in cache
189 volatile uint cache[MAX_lvl];// cache census counts by btree level
192 // latch hash table entries
195 unsigned char busy[1]; // Latch table entry is busy being reallocated
196 uint slot:24; // Latch table entry at head of collision chain
199 // latch manager table structure
202 volatile uid page_no; // latch set page number on disk
203 RWLock readwr[1]; // read/write page lock
204 RWLock access[1]; // Access Intent/Page delete
205 RWLock parent[1]; // Posting of fence key in parent
206 volatile ushort pin; // number of pins/level/clock bits
207 volatile uint next; // next entry in hash table chain
208 volatile uint prev; // prev entry in hash table chain
211 #define CLOCK_mask 0xe000
212 #define CLOCK_unit 0x2000
213 #define PIN_mask 0x07ff
214 #define LVL_mask 0x1800
217 // The object structure for Btree access
219 typedef struct _BtDb {
220 uint page_size; // each page size
221 uint page_bits; // each page size in bits
222 uid page_no; // current page number
223 uid cursor_page; // current cursor page number
225 uint mode; // read-write mode
226 BtPage cursor; // cached frame for start/next (never mapped)
227 BtPage frame; // spare frame for the page split (never mapped)
228 BtPage page; // current mapped page in buffer pool
229 BtLatchSet *latch; // current page latch
230 BtLatchMgr *latchmgr; // mapped latch page from allocation page
231 BtLatchSet *latchsets; // mapped latch set from latch pages
232 unsigned char *pagepool; // cached page pool set
233 BtHashEntry *table; // the hash table
238 HANDLE halloc; // allocation and latch table handle
240 unsigned char *mem; // frame, cursor, memory buffers
241 uint found; // last deletekey found key
259 extern void bt_close (BtDb *bt);
260 extern BtDb *bt_open (char *name, uint mode, uint bits, uint cacheblk);
261 extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod);
262 extern BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl);
263 extern uid bt_findkey (BtDb *bt, unsigned char *key, uint len);
264 extern uint bt_startkey (BtDb *bt, unsigned char *key, uint len);
265 extern uint bt_nextkey (BtDb *bt, uint slot);
267 // internal functions
268 void bt_update (BtDb *bt, BtPage page);
269 BtPage bt_mappage (BtDb *bt, BtLatchSet *latch);
270 // Helper functions to return slot values
272 extern BtKey bt_key (BtDb *bt, uint slot);
273 extern uid bt_uid (BtDb *bt, uint slot);
275 extern uint bt_tod (BtDb *bt, uint slot);
278 // The page is allocated from low and hi ends.
279 // The key offsets and row-id's are allocated
280 // from the bottom, while the text of the key
281 // is allocated from the top. When the two
282 // areas meet, the page is split into two.
284 // A key consists of a length byte, two bytes of
285 // index number (0 - 65534), and up to 253 bytes
286 // of key value. Duplicate keys are discarded.
287 // Associated with each key is a 48 bit row-id.
289 // The b-tree root is always located at page 1.
290 // The first leaf page of level zero is always
291 // located on page 2.
293 // The b-tree pages are linked with right
294 // pointers to facilitate enumerators,
295 // and provide for concurrency.
297 // When to root page fills, it is split in two and
298 // the tree height is raised by a new root at page
299 // one with two keys.
301 // Deleted keys are marked with a dead bit until
302 // page cleanup The fence key for a node is always
303 // present, even after deletion and cleanup.
305 // Deleted leaf pages are reclaimed on a free list.
306 // The upper levels of the btree are fixed on creation.
308 // To achieve maximum concurrency one page is locked at a time
309 // as the tree is traversed to find leaf key in question. The right
310 // page numbers are used in cases where the page is being split,
313 // Page 0 (ALLOC page) is dedicated to lock for new page extensions,
314 // and chains empty leaf pages together for reuse.
316 // Parent locks are obtained to prevent resplitting or deleting a node
317 // before its fence is posted into its upper level.
319 // A special open mode of BT_fl is provided to safely access files on
320 // WIN32 networks. WIN32 network operations should not use memory mapping.
321 // This WIN32 mode sets FILE_FLAG_NOBUFFERING and FILE_FLAG_WRITETHROUGH
322 // to prevent local caching of network file contents.
324 // Access macros to address slot and key values from the page.
325 // Page slots use 1 based indexing.
327 #define slotptr(page, slot) (((BtSlot *)(page+1)) + (slot-1))
328 #define keyptr(page, slot) ((BtKey)((unsigned char*)(page) + slotptr(page, slot)->off))
330 void bt_putid(unsigned char *dest, uid id)
335 dest[i] = (unsigned char)id, id >>= 8;
338 uid bt_getid(unsigned char *src)
343 for( i = 0; i < BtId; i++ )
344 id <<= 8, id |= *src++;
349 BTERR bt_abort (BtDb *bt, BtPage page, uid page_no, BTERR err)
353 fprintf(stderr, "\n Btree2 abort, error %d on page %.8x\n", err, page_no);
354 fprintf(stderr, "level=%d kill=%d free=%d cnt=%x act=%x\n", page->lvl, page->kill, page->free, page->cnt, page->act);
355 ptr = keyptr(page, page->cnt);
356 fprintf(stderr, "fence='%.*s'\n", ptr->len, ptr->key);
357 fprintf(stderr, "right=%.8x\n", bt_getid(page->right));
358 return bt->err = err;
361 // Phase-Fair reader/writer lock implementation
362 // with futex calls on contention
364 int sys_futex(void *addr1, int op, int val1, struct timespec *timeout, void *addr2, int val3)
366 return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
369 // a phase fair reader/writer lock implementation
371 void WriteLock (RWLock *lock)
376 tix = __sync_fetch_and_add (lock->ticket, 1);
378 // wait for our ticket to come up
381 prev = *lock->ticket | *lock->serving << 16;
382 if( tix == prev >> 16 )
384 sys_futex( (uint *)lock->ticket, FUTEX_WAIT_BITSET, prev, NULL, NULL, QueWr );
387 w = PRES | (tix & PHID);
388 r = __sync_fetch_and_add (lock->rin, w);
391 prev = *lock->rin | *lock->rout << 16;
392 if( r == prev >> 16 )
394 sys_futex( (uint *)lock->rin, FUTEX_WAIT_BITSET, prev, NULL, NULL, QueWr );
398 void WriteRelease (RWLock *lock)
400 __sync_fetch_and_and (lock->rin, ~MASK);
403 if( (*lock->rin & ~MASK) != (*lock->rout & ~MASK) )
404 if( sys_futex( (uint *)lock->rin, FUTEX_WAKE_BITSET, INT_MAX, NULL, NULL, QueRd ) )
407 if( *lock->ticket != *lock->serving )
408 sys_futex( (uint *)lock->ticket, FUTEX_WAKE_BITSET, INT_MAX, NULL, NULL, QueWr );
411 void ReadLock (RWLock *lock)
416 w = __sync_fetch_and_add (lock->rin, RINC) & MASK;
420 prev = *lock->rin | *lock->rout << 16;
421 if( w != (prev & MASK) )
423 sys_futex( (uint *)lock->rin, FUTEX_WAIT_BITSET, prev, NULL, NULL, QueRd );
427 void ReadRelease (RWLock *lock)
429 __sync_fetch_and_add (lock->rout, RINC);
431 if( *lock->ticket == *lock->serving )
434 if( *lock->rin & PRES )
435 if( sys_futex( (uint *)lock->rin, FUTEX_WAKE_BITSET, 1, NULL, NULL, QueWr ) )
438 sys_futex( (uint *)lock->ticket, FUTEX_WAKE_BITSET, INT_MAX, NULL, NULL, QueWr );
441 // lite weight FIFO mutex Manager
443 void bt_getmutex (BtMutex *mutex)
447 ours = __sync_fetch_and_add (&mutex->bits->next, 1);
450 prev = mutex->value[0];
451 if( ours == mutex->bits->serving )
453 sys_futex( mutex->value, FUTEX_WAIT_BITSET, prev, NULL, NULL, 0 );
457 void bt_relmutex (BtMutex *mutex)
459 ushort serving = ++mutex->bits->serving;
461 if( mutex->bits->next != serving )
462 sys_futex( mutex->value, FUTEX_WAKE_BITSET, 1, NULL, NULL, 0 );
465 // read page from permanent location in Btree file
467 BTERR bt_readpage (BtDb *bt, BtPage page, uid page_no)
469 off64_t off = page_no << bt->page_bits;
472 if( pread (bt->idx, page, bt->page_size, page_no << bt->page_bits) < bt->page_size ) {
473 fprintf (stderr, "Unable to read page %.8x errno = %d\n", page_no, errno);
474 return bt->err = BTERR_read;
480 memset (ovl, 0, sizeof(OVERLAPPED));
482 ovl->OffsetHigh = off >> 32;
484 if( !ReadFile(bt->idx, page, bt->page_size, amt, ovl)) {
485 fprintf (stderr, "Unable to read page %.8x GetLastError = %d\n", page_no, GetLastError());
486 return bt->err = BTERR_read;
488 if( *amt < bt->page_size ) {
489 fprintf (stderr, "Unable to read page %.8x GetLastError = %d\n", page_no, GetLastError());
490 return bt->err = BTERR_read;
496 // write page to permanent location in Btree file
497 // clear the dirty bit
499 BTERR bt_writepage (BtDb *bt, BtPage page, uid page_no)
501 off64_t off = page_no << bt->page_bits;
506 if( pwrite(bt->idx, page, bt->page_size, off) < bt->page_size )
507 return bt->err = BTERR_wrt;
512 memset (ovl, 0, sizeof(OVERLAPPED));
514 ovl->OffsetHigh = off >> 32;
517 if( !WriteFile(bt->idx, page, bt->page_size, amt, ovl) )
518 return bt->err = BTERR_wrt;
520 if( *amt < bt->page_size )
521 return bt->err = BTERR_wrt;
526 // link latch table entry into head of latch hash table
528 BTERR bt_latchlink (BtDb *bt, uint hashidx, uint slot, uid page_no)
530 BtPage page = (BtPage)((uid)slot * bt->page_size + bt->pagepool);
531 BtLatchSet *latch = bt->latchsets + slot;
534 if( latch->next = bt->table[hashidx].slot )
535 bt->latchsets[latch->next].prev = slot;
537 bt->table[hashidx].slot = slot;
538 latch->page_no = page_no;
542 if( bt_readpage (bt, page, page_no) )
545 lvl = page->lvl << LVL_shift;
548 latch->pin |= lvl; // store lvl
549 latch->pin |= lvl << 3; // initialize clock
552 __sync_fetch_and_add (&bt->latchmgr->cache[page->lvl], 1);
554 _InterlockedAdd(&bt->latchmgr->cache[page->lvl], 1);
561 void bt_unpinlatch (BtLatchSet *latch)
564 __sync_fetch_and_add(&latch->pin, -1);
566 _InterlockedDecrement16 (&latch->pin);
570 // find existing latchset or inspire new one
571 // return with latchset pinned
573 BtLatchSet *bt_pinlatch (BtDb *bt, uid page_no)
575 uint hashidx = page_no % bt->latchmgr->latchhash;
583 // try to find our entry
585 while( __sync_fetch_and_or (bt->table[hashidx].busy, 1) == 1 )
588 if( slot = bt->table[hashidx].slot ) do
590 latch = bt->latchsets + slot;
591 if( page_no == latch->page_no )
593 } while( slot = latch->next );
599 latch = bt->latchsets + slot;
600 lvl = (latch->pin & LVL_mask) >> LVL_shift;
601 lvl *= CLOCK_unit * 2;
604 __sync_fetch_and_add(&latch->pin, 1);
605 __sync_fetch_and_or(&latch->pin, lvl);
607 _InterlockedIncrement16 (&latch->pin);
608 _InterlockedOr16 (&latch->pin, lvl);
610 bt->table[hashidx].busy[0] = 0;
614 // see if there are any unused pool entries
616 slot = __sync_fetch_and_add (&bt->latchmgr->latchdeployed, 1) + 1;
618 slot = _InterlockedIncrement (&bt->latchmgr->latchdeployed);
621 if( slot < bt->latchmgr->latchtotal ) {
622 latch = bt->latchsets + slot;
623 if( bt_latchlink (bt, hashidx, slot, page_no) )
625 bt->table[hashidx].busy[0] = 0;
630 __sync_fetch_and_add (&bt->latchmgr->latchdeployed, -1);
632 _InterlockedDecrement (&bt->latchmgr->latchdeployed);
634 // find and reuse previous entry on victim
638 slot = __sync_fetch_and_add(&bt->latchmgr->latchvictim, 1);
640 slot = _InterlockedIncrement (&bt->latchmgr->latchvictim) - 1;
642 // try to get write lock on hash chain
643 // skip entry if not obtained
644 // or has outstanding pins
646 slot %= bt->latchmgr->latchtotal;
648 // on slot wraparound, check census
649 // count and increment safe level
651 cnt = bt->latchmgr->cache[bt->latchmgr->safelevel];
654 if( cnt < bt->latchmgr->latchtotal / 10 )
656 __sync_fetch_and_add(&bt->latchmgr->safelevel, 1);
658 _InterlockedIncrement (&bt->latchmgr->safelevel);
663 latch = bt->latchsets + slot;
664 idx = latch->page_no % bt->latchmgr->latchhash;
665 lvl = (latch->pin & LVL_mask) >> LVL_shift;
667 // see if we are evicting this level yet
668 // or if we are on same chain as hashidx
670 if( idx == hashidx || lvl > bt->latchmgr->safelevel )
673 if( __sync_fetch_and_or (bt->table[idx].busy, 1) == 1 )
676 if( latch->pin & ~LVL_mask ) {
677 if( latch->pin & CLOCK_mask )
679 __sync_fetch_and_add(&latch->pin, -CLOCK_unit);
681 _InterlockedExchangeAdd16 (&latch->pin, -CLOCK_unit);
683 bt->table[idx].busy[0] = 0;
687 // update permanent page area in btree
689 page = (BtPage)((uid)slot * bt->page_size + bt->pagepool);
691 posix_fadvise (bt->idx, page_no << bt->page_bits, bt->page_size, POSIX_FADV_WILLNEED);
692 __sync_fetch_and_add (&bt->latchmgr->cache[page->lvl], -1);
694 _InterlockedAdd(&bt->latchmgr->cache[page->lvl], -1);
697 if( bt_writepage (bt, page, latch->page_no) )
700 // unlink our available slot from its hash chain
703 bt->latchsets[latch->prev].next = latch->next;
705 bt->table[idx].slot = latch->next;
708 bt->latchsets[latch->next].prev = latch->prev;
710 bt->table[idx].busy[0] = 0;
712 if( bt_latchlink (bt, hashidx, slot, page_no) )
715 bt->table[hashidx].busy[0] = 0;
720 // close and release memory
722 void bt_close (BtDb *bt)
725 munmap (bt->table, bt->latchmgr->nlatchpage * bt->page_size);
726 munmap (bt->latchmgr, bt->page_size);
728 FlushViewOfFile(bt->latchmgr, 0);
729 UnmapViewOfFile(bt->latchmgr);
730 CloseHandle(bt->halloc);
739 VirtualFree (bt->mem, 0, MEM_RELEASE);
740 FlushFileBuffers(bt->idx);
741 CloseHandle(bt->idx);
745 // open/create new btree
747 // call with file_name, BT_openmode, bits in page size (e.g. 16),
748 // size of mapped page pool (e.g. 8192)
750 BtDb *bt_open (char *name, uint mode, uint bits, uint nodemax)
752 uint lvl, attr, last, slot, idx;
753 uint nlatchpage, latchhash;
754 BtLatchMgr *latchmgr;
764 struct flock lock[1];
767 // determine sanity of page size and buffer pool
769 if( bits > BT_maxbits )
771 else if( bits < BT_minbits )
774 if( mode == BT_ro ) {
775 fprintf(stderr, "ReadOnly mode not supported: %s\n", name);
779 bt = calloc (1, sizeof(BtDb));
781 bt->idx = open ((char*)name, O_RDWR | O_CREAT, 0666);
782 posix_fadvise( bt->idx, 0, 0, POSIX_FADV_RANDOM);
784 if( bt->idx == -1 ) {
785 fprintf(stderr, "unable to open %s\n", name);
786 return free(bt), NULL;
789 bt = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, sizeof(BtDb));
790 attr = FILE_ATTRIBUTE_NORMAL;
791 bt->idx = CreateFile(name, GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, attr, NULL);
793 if( bt->idx == INVALID_HANDLE_VALUE ) {
794 fprintf(stderr, "unable to open %s\n", name);
795 return GlobalFree(bt), NULL;
799 memset (lock, 0, sizeof(lock));
800 lock->l_len = sizeof(struct BtPage_);
801 lock->l_type = F_WRLCK;
803 if( fcntl (bt->idx, F_SETLKW, lock) < 0 ) {
804 fprintf(stderr, "unable to lock record zero %s\n", name);
805 return bt_close (bt), NULL;
808 memset (ovl, 0, sizeof(ovl));
810 // use large offsets to
811 // simulate advisory locking
813 ovl->OffsetHigh |= 0x80000000;
815 if( !LockFileEx (bt->idx, LOCKFILE_EXCLUSIVE_LOCK, 0, sizeof(struct BtPage_), 0L, ovl) ) {
816 fprintf(stderr, "unable to lock record zero %s, GetLastError = %d\n", name, GetLastError());
817 return bt_close (bt), NULL;
822 latchmgr = valloc (BT_maxpage);
825 // read minimum page size to get root info
827 if( size = lseek (bt->idx, 0L, 2) ) {
828 if( pread(bt->idx, latchmgr, BT_minpage, 0) == BT_minpage )
829 bits = latchmgr->alloc->bits;
831 fprintf(stderr, "Unable to read page zero\n");
832 return free(bt), free(latchmgr), NULL;
836 latchmgr = VirtualAlloc(NULL, BT_maxpage, MEM_COMMIT, PAGE_READWRITE);
837 size = GetFileSize(bt->idx, amt);
840 if( !ReadFile(bt->idx, (char *)latchmgr, BT_minpage, amt, NULL) ) {
841 fprintf(stderr, "Unable to read page zero\n");
842 return bt_close (bt), NULL;
844 bits = latchmgr->alloc->bits;
848 bt->page_size = 1 << bits;
849 bt->page_bits = bits;
854 nlatchpage = latchmgr->nlatchpage;
859 fprintf(stderr, "Buffer pool too small: %d\n", nodemax);
860 return bt_close(bt), NULL;
863 // initialize an empty b-tree with latch page, root page, page of leaves
864 // and page(s) of latches and page pool cache
866 memset (latchmgr, 0, 1 << bits);
867 latchmgr->alloc->bits = bt->page_bits;
869 // calculate number of latch hash table entries
871 nlatchpage = (nodemax/16 * sizeof(BtHashEntry) + bt->page_size - 1) / bt->page_size;
872 latchhash = nlatchpage * bt->page_size / sizeof(BtHashEntry);
874 nlatchpage += nodemax; // size of the buffer pool in pages
875 nlatchpage += (sizeof(BtLatchSet) * nodemax + bt->page_size - 1)/bt->page_size;
877 bt_putid(latchmgr->alloc->right, MIN_lvl+1+nlatchpage);
878 latchmgr->nlatchpage = nlatchpage;
879 latchmgr->latchtotal = nodemax;
880 latchmgr->latchhash = latchhash;
882 if( bt_writepage (bt, latchmgr->alloc, 0) ) {
883 fprintf (stderr, "Unable to create btree page zero\n");
884 return bt_close (bt), NULL;
887 memset (latchmgr, 0, 1 << bits);
888 latchmgr->alloc->bits = bt->page_bits;
890 for( lvl=MIN_lvl; lvl--; ) {
891 last = MIN_lvl - lvl; // page number
892 slotptr(latchmgr->alloc, 1)->off = bt->page_size - 3;
893 bt_putid(slotptr(latchmgr->alloc, 1)->id, lvl ? last + 1 : 0);
894 key = keyptr(latchmgr->alloc, 1);
895 key->len = 2; // create stopper key
899 latchmgr->alloc->min = bt->page_size - 3;
900 latchmgr->alloc->lvl = lvl;
901 latchmgr->alloc->cnt = 1;
902 latchmgr->alloc->act = 1;
904 if( bt_writepage (bt, latchmgr->alloc, last) ) {
905 fprintf (stderr, "Unable to create btree page %.8x\n", last);
906 return bt_close (bt), NULL;
910 // clear out buffer pool pages
912 memset(latchmgr, 0, bt->page_size);
913 last = MIN_lvl + nlatchpage;
915 if( bt_writepage (bt, latchmgr->alloc, last) ) {
916 fprintf (stderr, "Unable to write buffer pool page %.8x\n", last);
917 return bt_close (bt), NULL;
923 VirtualFree (latchmgr, 0, MEM_RELEASE);
928 lock->l_type = F_UNLCK;
929 if( fcntl (bt->idx, F_SETLK, lock) < 0 ) {
930 fprintf (stderr, "Unable to unlock page zero\n");
931 return bt_close (bt), NULL;
934 if( !UnlockFileEx (bt->idx, 0, sizeof(struct BtPage_), 0, ovl) ) {
935 fprintf (stderr, "Unable to unlock page zero, GetLastError = %d\n", GetLastError());
936 return bt_close (bt), NULL;
940 flag = PROT_READ | PROT_WRITE;
941 bt->latchmgr = mmap (0, bt->page_size, flag, MAP_SHARED, bt->idx, ALLOC_page * bt->page_size);
942 if( bt->latchmgr == MAP_FAILED ) {
943 fprintf (stderr, "Unable to mmap page zero, errno = %d", errno);
944 return bt_close (bt), NULL;
946 bt->table = (void *)mmap (0, (uid)nlatchpage * bt->page_size, flag, MAP_SHARED, bt->idx, LATCH_page * bt->page_size);
947 if( bt->table == MAP_FAILED ) {
948 fprintf (stderr, "Unable to mmap buffer pool, errno = %d", errno);
949 return bt_close (bt), NULL;
951 madvise (bt->table, (uid)nlatchpage << bt->page_bits, MADV_RANDOM | MADV_WILLNEED);
953 flag = PAGE_READWRITE;
954 bt->halloc = CreateFileMapping(bt->idx, NULL, flag, 0, ((uid)nlatchpage + LATCH_page) * bt->page_size, NULL);
956 fprintf (stderr, "Unable to create file mapping for buffer pool mgr, GetLastError = %d\n", GetLastError());
957 return bt_close (bt), NULL;
960 flag = FILE_MAP_WRITE;
961 bt->latchmgr = MapViewOfFile(bt->halloc, flag, 0, 0, ((uid)nlatchpage + LATCH_page) * bt->page_size);
962 if( !bt->latchmgr ) {
963 fprintf (stderr, "Unable to map buffer pool, GetLastError = %d\n", GetLastError());
964 return bt_close (bt), NULL;
967 bt->table = (void *)((char *)bt->latchmgr + LATCH_page * bt->page_size);
969 bt->pagepool = (unsigned char *)bt->table + (uid)(nlatchpage - bt->latchmgr->latchtotal) * bt->page_size;
970 bt->latchsets = (BtLatchSet *)(bt->pagepool - (uid)bt->latchmgr->latchtotal * sizeof(BtLatchSet));
973 bt->mem = valloc (2 * bt->page_size);
975 bt->mem = VirtualAlloc(NULL, 2 * bt->page_size, MEM_COMMIT, PAGE_READWRITE);
977 bt->frame = (BtPage)bt->mem;
978 bt->cursor = (BtPage)(bt->mem + bt->page_size);
982 // place write, read, or parent lock on requested page_no.
984 void bt_lockpage(BtLock mode, BtLatchSet *latch)
988 ReadLock (latch->readwr);
991 WriteLock (latch->readwr);
994 ReadLock (latch->access);
997 WriteLock (latch->access);
1000 WriteLock (latch->parent);
1005 // remove write, read, or parent lock on requested page
1007 void bt_unlockpage(BtLock mode, BtLatchSet *latch)
1011 ReadRelease (latch->readwr);
1014 WriteRelease (latch->readwr);
1017 ReadRelease (latch->access);
1020 WriteRelease (latch->access);
1023 WriteRelease (latch->parent);
1028 // allocate a new page and write page into it
1030 uid bt_newpage(BtDb *bt, BtPage page)
1036 // lock allocation page
1038 bt_getmutex(bt->latchmgr->lock);
1040 // use empty chain first
1041 // else allocate empty page
1043 if( new_page = bt_getid(bt->latchmgr->alloc[1].right) ) {
1044 if( latch = bt_pinlatch (bt, new_page) )
1045 temp = bt_mappage (bt, latch);
1049 bt_putid(bt->latchmgr->alloc[1].right, bt_getid(temp->right));
1050 bt_relmutex(bt->latchmgr->lock);
1051 memcpy (temp, page, bt->page_size);
1053 bt_update (bt, temp);
1054 bt_unpinlatch (latch);
1057 new_page = bt_getid(bt->latchmgr->alloc->right);
1058 bt_putid(bt->latchmgr->alloc->right, new_page+1);
1059 bt_relmutex(bt->latchmgr->lock);
1061 if( bt_writepage (bt, page, new_page) )
1065 bt_update (bt, bt->latchmgr->alloc);
1069 // compare two keys, returning > 0, = 0, or < 0
1070 // as the comparison value
1072 int keycmp (BtKey key1, unsigned char *key2, uint len2)
1074 uint len1 = key1->len;
1077 if( ans = memcmp (key1->key, key2, len1 > len2 ? len2 : len1) )
1088 // Update current page of btree by
1089 // flushing mapped area to disk backing of cache pool.
1090 // mark page as dirty for rewrite to permanent location
1092 void bt_update (BtDb *bt, BtPage page)
1095 msync (page, bt->page_size, MS_ASYNC);
1097 // FlushViewOfFile (page, bt->page_size);
1102 // map the btree cached page onto current page
1104 BtPage bt_mappage (BtDb *bt, BtLatchSet *latch)
1106 return (BtPage)((uid)(latch - bt->latchsets) * bt->page_size + bt->pagepool);
1109 // deallocate a deleted page
1110 // place on free chain out of allocator page
1111 // call with page latched for Writing and Deleting
1113 BTERR bt_freepage(BtDb *bt, uid page_no, BtLatchSet *latch)
1115 BtPage page = bt_mappage (bt, latch);
1117 // lock allocation page
1119 bt_getmutex (bt->latchmgr->lock);
1121 // store chain in second right
1122 bt_putid(page->right, bt_getid(bt->latchmgr->alloc[1].right));
1123 bt_putid(bt->latchmgr->alloc[1].right, page_no);
1126 bt_update(bt, page);
1128 // unlock released page
1130 bt_unlockpage (BtLockDelete, latch);
1131 bt_unlockpage (BtLockWrite, latch);
1132 bt_unpinlatch (latch);
1134 // unlock allocation page
1136 bt_relmutex (bt->latchmgr->lock);
1137 bt_update (bt, bt->latchmgr->alloc);
1141 // find slot in page for given key at a given level
1143 int bt_findslot (BtDb *bt, unsigned char *key, uint len)
1145 uint diff, higher = bt->page->cnt, low = 1, slot;
1148 // make stopper key an infinite fence value
1150 if( bt_getid (bt->page->right) )
1155 // low is the lowest candidate, higher is already
1156 // tested as .ge. the given key, loop ends when they meet
1158 while( diff = higher - low ) {
1159 slot = low + ( diff >> 1 );
1160 if( keycmp (keyptr(bt->page, slot), key, len) < 0 )
1163 higher = slot, good++;
1166 // return zero if key is on right link page
1168 return good ? higher : 0;
1171 // find and load page at given level for given key
1172 // leave page rd or wr locked as requested
1174 int bt_loadpage (BtDb *bt, unsigned char *key, uint len, uint lvl, uint lock)
1176 uid page_no = ROOT_page, prevpage = 0;
1177 uint drill = 0xff, slot;
1178 BtLatchSet *prevlatch;
1179 uint mode, prevmode;
1181 // start at root of btree and drill down
1184 // determine lock mode of drill level
1185 mode = (lock == BtLockWrite) && (drill == lvl) ? BtLockWrite : BtLockRead;
1187 if( bt->latch = bt_pinlatch(bt, page_no) )
1188 bt->page_no = page_no;
1192 // obtain access lock using lock chaining
1194 if( page_no > ROOT_page )
1195 bt_lockpage(BtLockAccess, bt->latch);
1198 bt_unlockpage(prevmode, prevlatch);
1199 bt_unpinlatch(prevlatch);
1203 // obtain read lock using lock chaining
1205 bt_lockpage(mode, bt->latch);
1207 if( page_no > ROOT_page )
1208 bt_unlockpage(BtLockAccess, bt->latch);
1210 // map/obtain page contents
1212 bt->page = bt_mappage (bt, bt->latch);
1214 // re-read and re-lock root after determining actual level of root
1216 if( bt->page->lvl != drill) {
1217 if( bt->page_no != ROOT_page )
1218 return bt->err = BTERR_struct, 0;
1220 drill = bt->page->lvl;
1222 if( lock != BtLockRead && drill == lvl ) {
1223 bt_unlockpage(mode, bt->latch);
1224 bt_unpinlatch(bt->latch);
1229 prevpage = bt->page_no;
1230 prevlatch = bt->latch;
1233 // find key on page at this level
1234 // and descend to requested level
1236 if( !bt->page->kill )
1237 if( slot = bt_findslot (bt, key, len) ) {
1241 while( slotptr(bt->page, slot)->dead )
1242 if( slot++ < bt->page->cnt )
1247 page_no = bt_getid(slotptr(bt->page, slot)->id);
1252 // or slide right into next page
1255 page_no = bt_getid(bt->page->right);
1259 // return error on end of right chain
1261 bt->err = BTERR_eof;
1262 return 0; // return error
1265 // a fence key was deleted from a page
1266 // push new fence value upwards
1268 BTERR bt_fixfence (BtDb *bt, uid page_no, uint lvl)
1270 unsigned char leftkey[256], rightkey[256];
1271 BtLatchSet *latch = bt->latch;
1274 // remove deleted key, the old fence value
1276 ptr = keyptr(bt->page, bt->page->cnt);
1277 memcpy(rightkey, ptr, ptr->len + 1);
1279 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1280 bt->page->clean = 1;
1282 ptr = keyptr(bt->page, bt->page->cnt);
1283 memcpy(leftkey, ptr, ptr->len + 1);
1285 bt_update (bt, bt->page);
1286 bt_lockpage (BtLockParent, latch);
1287 bt_unlockpage (BtLockWrite, latch);
1289 // insert new (now smaller) fence key
1291 if( bt_insertkey (bt, leftkey+1, *leftkey, lvl + 1, page_no, time(NULL)) )
1294 // remove old (larger) fence key
1296 if( bt_deletekey (bt, rightkey+1, *rightkey, lvl + 1) )
1299 bt_unlockpage (BtLockParent, latch);
1300 bt_unpinlatch (latch);
1304 // root has a single child
1305 // collapse a level from the btree
1306 // call with root locked in bt->page
1308 BTERR bt_collapseroot (BtDb *bt, BtPage root)
1315 // find the child entry
1316 // and promote to new root
1319 for( idx = 0; idx++ < root->cnt; )
1320 if( !slotptr(root, idx)->dead )
1323 child = bt_getid (slotptr(root, idx)->id);
1324 if( latch = bt_pinlatch (bt, child) )
1325 temp = bt_mappage (bt, latch);
1329 bt_lockpage (BtLockDelete, latch);
1330 bt_lockpage (BtLockWrite, latch);
1331 memcpy (root, temp, bt->page_size);
1333 bt_update (bt, root);
1335 if( bt_freepage (bt, child, latch) )
1338 } while( root->lvl > 1 && root->act == 1 );
1340 bt_unlockpage (BtLockWrite, bt->latch);
1341 bt_unpinlatch (bt->latch);
1345 // find and delete key on page by marking delete flag bit
1346 // when page becomes empty, delete it
1348 BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl)
1350 unsigned char lowerkey[256], higherkey[256];
1351 uint slot, dirty = 0, idx, fence, found;
1352 BtLatchSet *latch, *rlatch;
1357 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1358 ptr = keyptr(bt->page, slot);
1362 // are we deleting a fence slot?
1364 fence = slot == bt->page->cnt;
1366 // if key is found delete it, otherwise ignore request
1368 if( found = !keycmp (ptr, key, len) )
1369 if( found = slotptr(bt->page, slot)->dead == 0 ) {
1370 dirty = slotptr(bt->page,slot)->dead = 1;
1371 bt->page->clean = 1;
1374 // collapse empty slots
1376 while( idx = bt->page->cnt - 1 )
1377 if( slotptr(bt->page, idx)->dead ) {
1378 *slotptr(bt->page, idx) = *slotptr(bt->page, idx + 1);
1379 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1384 right = bt_getid(bt->page->right);
1385 page_no = bt->page_no;
1390 return bt_abort (bt, bt->page, page_no, BTERR_notfound);
1391 bt_unlockpage(BtLockWrite, latch);
1392 bt_unpinlatch (latch);
1393 return bt->found = found, 0;
1396 // did we delete a fence key in an upper level?
1398 if( lvl && bt->page->act && fence )
1399 if( bt_fixfence (bt, page_no, lvl) )
1402 return bt->found = found, 0;
1404 // is this a collapsed root?
1406 if( lvl > 1 && page_no == ROOT_page && bt->page->act == 1 )
1407 if( bt_collapseroot (bt, bt->page) )
1410 return bt->found = found, 0;
1412 // return if page is not empty
1414 if( bt->page->act ) {
1415 bt_update(bt, bt->page);
1416 bt_unlockpage(BtLockWrite, latch);
1417 bt_unpinlatch (latch);
1418 return bt->found = found, 0;
1421 // cache copy of fence key
1422 // in order to find parent
1424 ptr = keyptr(bt->page, bt->page->cnt);
1425 memcpy(lowerkey, ptr, ptr->len + 1);
1427 // obtain lock on right page
1429 if( rlatch = bt_pinlatch (bt, right) )
1430 temp = bt_mappage (bt, rlatch);
1434 bt_lockpage(BtLockWrite, rlatch);
1437 bt_abort(bt, temp, right, 0);
1438 return bt_abort(bt, bt->page, bt->page_no, BTERR_kill);
1441 // pull contents of next page into current empty page
1443 memcpy (bt->page, temp, bt->page_size);
1445 // cache copy of key to update
1447 ptr = keyptr(temp, temp->cnt);
1448 memcpy(higherkey, ptr, ptr->len + 1);
1450 // Mark right page as deleted and point it to left page
1451 // until we can post updates at higher level.
1453 bt_putid(temp->right, page_no);
1456 bt_update(bt, bt->page);
1457 bt_update(bt, temp);
1459 bt_lockpage(BtLockParent, latch);
1460 bt_unlockpage(BtLockWrite, latch);
1462 bt_lockpage(BtLockParent, rlatch);
1463 bt_unlockpage(BtLockWrite, rlatch);
1465 // redirect higher key directly to consolidated node
1467 if( bt_insertkey (bt, higherkey+1, *higherkey, lvl+1, page_no, time(NULL)) )
1470 // delete old lower key to consolidated node
1472 if( bt_deletekey (bt, lowerkey + 1, *lowerkey, lvl + 1) )
1475 // obtain write & delete lock on deleted node
1476 // add right block to free chain
1478 bt_lockpage(BtLockDelete, rlatch);
1479 bt_lockpage(BtLockWrite, rlatch);
1480 bt_unlockpage(BtLockParent, rlatch);
1482 if( bt_freepage (bt, right, rlatch) )
1485 bt_unlockpage(BtLockParent, latch);
1486 bt_unpinlatch(latch);
1490 // find key in leaf level and return row-id
1492 uid bt_findkey (BtDb *bt, unsigned char *key, uint len)
1498 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1499 ptr = keyptr(bt->page, slot);
1503 // if key exists, return row-id
1504 // otherwise return 0
1506 if( ptr->len == len && !memcmp (ptr->key, key, len) )
1507 id = bt_getid(slotptr(bt->page,slot)->id);
1511 bt_unlockpage (BtLockRead, bt->latch);
1512 bt_unpinlatch (bt->latch);
1516 // check page for space available,
1517 // clean if necessary and return
1518 // 0 - page needs splitting
1519 // >0 - go ahead with new slot
1521 uint bt_cleanpage(BtDb *bt, uint amt, uint slot)
1523 uint nxt = bt->page_size;
1524 BtPage page = bt->page;
1525 uint cnt = 0, idx = 0;
1526 uint max = page->cnt;
1527 uint newslot = slot;
1531 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1534 // skip cleanup if nothing to reclaim
1539 memcpy (bt->frame, page, bt->page_size);
1541 // skip page info and set rest of page to zero
1543 memset (page+1, 0, bt->page_size - sizeof(*page));
1546 while( cnt++ < max ) {
1549 // always leave fence key in list
1550 if( cnt < max && slotptr(bt->frame,cnt)->dead )
1554 key = keyptr(bt->frame, cnt);
1555 nxt -= key->len + 1;
1556 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1559 memcpy(slotptr(page, ++idx)->id, slotptr(bt->frame, cnt)->id, BtId);
1560 if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
1563 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1565 slotptr(page, idx)->off = nxt;
1571 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1577 // split the root and raise the height of the btree
1579 BTERR bt_splitroot(BtDb *bt, unsigned char *leftkey, uid page_no2)
1581 uint nxt = bt->page_size;
1582 BtPage root = bt->page;
1585 // Obtain an empty page to use, and copy the current
1586 // root contents into it
1588 if( !(right = bt_newpage(bt, root)) )
1591 // preserve the page info at the bottom
1592 // and set rest to zero
1594 memset(root+1, 0, bt->page_size - sizeof(*root));
1596 // insert first key on newroot page
1598 nxt -= *leftkey + 1;
1599 memcpy ((unsigned char *)root + nxt, leftkey, *leftkey + 1);
1600 bt_putid(slotptr(root, 1)->id, right);
1601 slotptr(root, 1)->off = nxt;
1603 // insert second key on newroot page
1604 // and increase the root height
1607 ((unsigned char *)root)[nxt] = 2;
1608 ((unsigned char *)root)[nxt+1] = 0xff;
1609 ((unsigned char *)root)[nxt+2] = 0xff;
1610 bt_putid(slotptr(root, 2)->id, page_no2);
1611 slotptr(root, 2)->off = nxt;
1613 bt_putid(root->right, 0);
1614 root->min = nxt; // reset lowest used offset and key count
1619 // update and release root (bt->page)
1621 bt_update(bt, root);
1623 bt_unlockpage(BtLockWrite, bt->latch);
1624 bt_unpinlatch(bt->latch);
1628 // split already locked full node
1631 BTERR bt_splitpage (BtDb *bt)
1633 uint cnt = 0, idx = 0, max, nxt = bt->page_size;
1634 unsigned char fencekey[256], rightkey[256];
1635 uid page_no = bt->page_no, right;
1636 BtLatchSet *latch, *rlatch;
1637 BtPage page = bt->page;
1638 uint lvl = page->lvl;
1643 // split higher half of keys to bt->frame
1644 // the last key (fence key) might be dead
1646 memset (bt->frame, 0, bt->page_size);
1651 while( cnt++ < max ) {
1652 key = keyptr(page, cnt);
1653 nxt -= key->len + 1;
1654 memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
1655 memcpy(slotptr(bt->frame,++idx)->id, slotptr(page,cnt)->id, BtId);
1656 if( !(slotptr(bt->frame, idx)->dead = slotptr(page, cnt)->dead) )
1659 slotptr(bt->frame, idx)->tod = slotptr(page, cnt)->tod;
1661 slotptr(bt->frame, idx)->off = nxt;
1664 // remember fence key for new right page
1666 memcpy (rightkey, key, key->len + 1);
1668 bt->frame->bits = bt->page_bits;
1669 bt->frame->min = nxt;
1670 bt->frame->cnt = idx;
1671 bt->frame->lvl = lvl;
1675 if( page_no > ROOT_page )
1676 memcpy (bt->frame->right, page->right, BtId);
1678 // get new free page and write frame to it.
1680 if( !(right = bt_newpage(bt, bt->frame)) )
1683 // update lower keys to continue in old page
1685 memcpy (bt->frame, page, bt->page_size);
1686 memset (page+1, 0, bt->page_size - sizeof(*page));
1687 nxt = bt->page_size;
1693 // assemble page of smaller keys
1694 // (they're all active keys)
1696 while( cnt++ < max / 2 ) {
1697 key = keyptr(bt->frame, cnt);
1698 nxt -= key->len + 1;
1699 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1700 memcpy(slotptr(page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
1702 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1704 slotptr(page, idx)->off = nxt;
1708 // remember fence key for smaller page
1710 memcpy (fencekey, key, key->len + 1);
1712 bt_putid(page->right, right);
1716 // if current page is the root page, split it
1718 if( page_no == ROOT_page )
1719 return bt_splitroot (bt, fencekey, right);
1723 if( rlatch = bt_pinlatch (bt, right) )
1724 bt_lockpage (BtLockParent, rlatch);
1728 // update left (containing) node
1730 bt_update(bt, page);
1732 bt_lockpage (BtLockParent, latch);
1733 bt_unlockpage (BtLockWrite, latch);
1735 // insert new fence for reformulated left block
1737 if( bt_insertkey (bt, fencekey+1, *fencekey, lvl+1, page_no, time(NULL)) )
1740 // switch fence for right block of larger keys to new right page
1742 if( bt_insertkey (bt, rightkey+1, *rightkey, lvl+1, right, time(NULL)) )
1745 bt_unlockpage (BtLockParent, latch);
1746 bt_unlockpage (BtLockParent, rlatch);
1748 bt_unpinlatch (rlatch);
1749 bt_unpinlatch (latch);
1753 // Insert new key into the btree at requested level.
1754 // Pages are unlocked at exit.
1756 BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod)
1763 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1764 ptr = keyptr(bt->page, slot);
1768 bt->err = BTERR_ovflw;
1772 // if key already exists, update id and return
1776 if( !keycmp (ptr, key, len) ) {
1777 if( slotptr(page, slot)->dead )
1779 slotptr(page, slot)->dead = 0;
1781 slotptr(page, slot)->tod = tod;
1783 bt_putid(slotptr(page,slot)->id, id);
1784 bt_update(bt, bt->page);
1785 bt_unlockpage(BtLockWrite, bt->latch);
1786 bt_unpinlatch (bt->latch);
1790 // check if page has enough space
1792 if( slot = bt_cleanpage (bt, len, slot) )
1795 if( bt_splitpage (bt) )
1799 // calculate next available slot and copy key into page
1801 page->min -= len + 1; // reset lowest used offset
1802 ((unsigned char *)page)[page->min] = len;
1803 memcpy ((unsigned char *)page + page->min +1, key, len );
1805 for( idx = slot; idx < page->cnt; idx++ )
1806 if( slotptr(page, idx)->dead )
1809 // now insert key into array before slot
1810 // preserving the fence slot
1812 if( idx == page->cnt )
1818 *slotptr(page, idx) = *slotptr(page, idx -1), idx--;
1820 bt_putid(slotptr(page,slot)->id, id);
1821 slotptr(page, slot)->off = page->min;
1823 slotptr(page, slot)->tod = tod;
1825 slotptr(page, slot)->dead = 0;
1827 bt_update(bt, bt->page);
1829 bt_unlockpage(BtLockWrite, bt->latch);
1830 bt_unpinlatch(bt->latch);
1834 // cache page of keys into cursor and return starting slot for given key
1836 uint bt_startkey (BtDb *bt, unsigned char *key, uint len)
1840 // cache page for retrieval
1842 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1843 memcpy (bt->cursor, bt->page, bt->page_size);
1847 bt_unlockpage(BtLockRead, bt->latch);
1848 bt->cursor_page = bt->page_no;
1849 bt_unpinlatch (bt->latch);
1853 // return next slot for cursor page
1854 // or slide cursor right into next page
1856 uint bt_nextkey (BtDb *bt, uint slot)
1862 right = bt_getid(bt->cursor->right);
1864 while( slot++ < bt->cursor->cnt )
1865 if( slotptr(bt->cursor,slot)->dead )
1867 else if( right || (slot < bt->cursor->cnt))
1875 bt->cursor_page = right;
1877 if( latch = bt_pinlatch (bt, right) )
1878 bt_lockpage(BtLockRead, latch);
1882 bt->page = bt_mappage (bt, latch);
1883 memcpy (bt->cursor, bt->page, bt->page_size);
1884 bt_unlockpage(BtLockRead, latch);
1885 bt_unpinlatch (latch);
1892 BtKey bt_key(BtDb *bt, uint slot)
1894 return keyptr(bt->cursor, slot);
1897 uid bt_uid(BtDb *bt, uint slot)
1899 return bt_getid(slotptr(bt->cursor,slot)->id);
1903 uint bt_tod(BtDb *bt, uint slot)
1905 return slotptr(bt->cursor,slot)->tod;
1911 uint bt_audit (BtDb *bt)
1923 posix_fadvise( bt->idx, 0, 0, POSIX_FADV_SEQUENTIAL);
1925 if( *(ushort *)(bt->latchmgr->lock) )
1926 fprintf(stderr, "Alloc page locked\n");
1927 *(ushort *)(bt->latchmgr->lock) = 0;
1929 memset (blks, 0, sizeof(blks));
1931 for( idx = 1; idx <= bt->latchmgr->latchdeployed; idx++ ) {
1932 latch = bt->latchsets + idx;
1933 if( *(ushort *)latch->readwr )
1934 fprintf(stderr, "latchset %d rwlocked for page %.8x\n", idx, latch->page_no);
1935 *(ushort *)latch->readwr = 0;
1937 if( *(ushort *)latch->access )
1938 fprintf(stderr, "latchset %d accesslocked for page %.8x\n", idx, latch->page_no);
1939 *(ushort *)latch->access = 0;
1941 if( *(ushort *)latch->parent )
1942 fprintf(stderr, "latchset %d parentlocked for page %.8x\n", idx, latch->page_no);
1943 *(ushort *)latch->parent = 0;
1945 if( latch->pin & PIN_mask ) {
1946 fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
1949 page = (BtPage)((uid)idx * bt->page_size + bt->pagepool);
1953 if( bt_writepage (bt, page, latch->page_no) )
1954 fprintf(stderr, "Page %.8x Write Error\n", latch->page_no);
1957 for( idx = 0; blks[idx]; idx++ )
1958 fprintf(stderr, "cache: %d lvl %d blocks\n", blks[idx], idx);
1960 for( hashidx = 0; hashidx < bt->latchmgr->latchhash; hashidx++ ) {
1961 if( bt->table[hashidx].busy[0] )
1962 fprintf(stderr, "hash entry %d locked\n", hashidx);
1964 bt->table[hashidx].busy[0] = 0;
1967 memset (blks, 0, sizeof(blks));
1969 next = bt->latchmgr->nlatchpage + LATCH_page;
1970 page_no = LEAF_page;
1972 while( page_no < bt_getid(bt->latchmgr->alloc->right) ) {
1973 if( bt_readpage (bt, bt->frame, page_no) )
1974 fprintf(stderr, "page %.8x unreadable\n", page_no);
1975 if( !bt->frame->free ) {
1976 for( idx = 0; idx++ < bt->frame->cnt - 1; ) {
1977 ptr = keyptr(bt->frame, idx+1);
1978 if( keycmp (keyptr(bt->frame, idx), ptr->key, ptr->len) >= 0 )
1979 fprintf(stderr, "page %.8x idx %.2x out of order\n", page_no, idx);
1981 if( !bt->frame->lvl )
1982 cnt += bt->frame->act;
1983 blks[bt->frame->lvl]++;
1986 if( page_no > LEAF_page )
1991 for( idx = 0; blks[idx]; idx++ )
1992 fprintf(stderr, "btree: %d lvl %d blocks\n", blks[idx], idx);
1998 double getCpuTime(int type)
2001 FILETIME xittime[1];
2002 FILETIME systime[1];
2003 FILETIME usrtime[1];
2004 SYSTEMTIME timeconv[1];
2007 memset (timeconv, 0, sizeof(SYSTEMTIME));
2011 GetSystemTimeAsFileTime (xittime);
2012 FileTimeToSystemTime (xittime, timeconv);
2013 ans = (double)timeconv->wDayOfWeek * 3600 * 24;
2016 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
2017 FileTimeToSystemTime (usrtime, timeconv);
2020 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
2021 FileTimeToSystemTime (systime, timeconv);
2025 ans += (double)timeconv->wHour * 3600;
2026 ans += (double)timeconv->wMinute * 60;
2027 ans += (double)timeconv->wSecond;
2028 ans += (double)timeconv->wMilliseconds / 1000;
2033 #include <sys/resource.h>
2035 double getCpuTime(int type)
2037 struct rusage used[1];
2038 struct timeval tv[1];
2042 gettimeofday(tv, NULL);
2043 return (double)tv->tv_sec + (double)tv->tv_usec / 1000000;
2046 getrusage(RUSAGE_SELF, used);
2047 return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
2050 getrusage(RUSAGE_SELF, used);
2051 return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
2058 // standalone program to index file of keys
2059 // then list them onto std-out
2061 int main (int argc, char **argv)
2063 uint slot, line = 0, off = 0, found = 0;
2064 int ch, cnt = 0, bits = 12, idx;
2065 unsigned char key[256];
2080 _setmode (1, _O_BINARY);
2083 fprintf (stderr, "Usage: %s idx_file src_file Read/Write/Scan/Delete/Find/Count [page_bits mapped_pool_pages start_line_number]\n", argv[0]);
2084 fprintf (stderr, " page_bits: size of btree page in bits\n");
2085 fprintf (stderr, " mapped_pool_pages: number of pages in buffer pool\n");
2089 start = getCpuTime(0);
2093 bits = atoi(argv[4]);
2096 map = atoi(argv[5]);
2099 off = atoi(argv[6]);
2101 bt = bt_open ((argv[1]), BT_rw, bits, map);
2104 fprintf(stderr, "Index Open Error %s\n", argv[1]);
2108 switch(argv[3][0]| 0x20)
2110 case 'p': // display page
2111 if( latch = bt_pinlatch (bt, off) )
2112 page = bt_mappage (bt, latch);
2114 fprintf(stderr, "unable to read page %.8x\n", off);
2116 write (1, page, bt->page_size);
2119 case 'a': // buffer pool audit
2120 fprintf(stderr, "started audit for %s\n", argv[1]);
2121 cnt = bt_audit (bt);
2122 fprintf(stderr, "finished audit for %s, %d keys\n", argv[1], cnt);
2125 case 'w': // write keys
2126 fprintf(stderr, "started indexing for %s\n", argv[2]);
2127 if( argc > 2 && (in = fopen (argv[2], "rb")) ) {
2129 posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE);
2131 while( ch = getc(in), ch != EOF )
2135 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2137 if( bt_insertkey (bt, key, len, 0, ++line, *tod) )
2138 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2141 else if( len < 245 )
2144 fprintf(stderr, "finished adding keys for %s, %d \n", argv[2], line);
2147 case 'd': // delete keys
2148 fprintf(stderr, "started deleting keys for %s\n", argv[2]);
2149 if( argc > 2 && (in = fopen (argv[2], "rb")) ) {
2151 posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE);
2153 while( ch = getc(in), ch != EOF )
2157 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2159 if( bt_deletekey (bt, key, len, 0) )
2160 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2163 else if( len < 245 )
2166 fprintf(stderr, "finished deleting keys for %s, %d \n", argv[2], line);
2169 case 'f': // find keys
2170 fprintf(stderr, "started finding keys for %s\n", argv[2]);
2171 if( argc > 2 && (in = fopen (argv[2], "rb")) ) {
2173 posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE);
2175 while( ch = getc(in), ch != EOF )
2179 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2181 if( bt_findkey (bt, key, len) )
2184 fprintf(stderr, "Error %d Syserr %d Line: %d\n", bt->err, errno, line), exit(0);
2187 else if( len < 245 )
2190 fprintf(stderr, "finished search of %d keys for %s, found %d\n", line, argv[2], found);
2193 case 's': // scan and print keys
2194 fprintf(stderr, "started scaning\n");
2195 cnt = len = key[0] = 0;
2197 if( slot = bt_startkey (bt, key, len) )
2200 fprintf(stderr, "Error %d in StartKey. Syserror: %d\n", bt->err, errno), exit(0);
2202 while( slot = bt_nextkey (bt, slot) ) {
2203 ptr = bt_key(bt, slot);
2204 fwrite (ptr->key, ptr->len, 1, stdout);
2205 fputc ('\n', stdout);
2209 fprintf(stderr, " Total keys read %d\n", cnt - 1);
2212 case 'c': // count keys
2213 fprintf(stderr, "started counting\n");
2216 next = bt->latchmgr->nlatchpage + LATCH_page;
2217 page_no = LEAF_page;
2219 while( page_no < bt_getid(bt->latchmgr->alloc->right) ) {
2220 if( latch = bt_pinlatch (bt, page_no) )
2221 page = bt_mappage (bt, latch);
2222 if( !page->free && !page->lvl )
2224 if( page_no > LEAF_page )
2227 for( idx = 0; idx++ < page->cnt; ) {
2228 if( slotptr(page, idx)->dead )
2230 ptr = keyptr(page, idx);
2231 if( idx != page->cnt && bt_getid (page->right) ) {
2232 fwrite (ptr->key, ptr->len, 1, stdout);
2233 fputc ('\n', stdout);
2236 bt_unpinlatch (latch);
2240 cnt--; // remove stopper key
2241 fprintf(stderr, " Total keys read %d\n", cnt);
2245 done = getCpuTime(0);
2246 elapsed = (float)(done - start);
2247 fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2248 elapsed = getCpuTime(1);
2249 fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2250 elapsed = getCpuTime(2);
2251 fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);