2 // with combined latch & pool manager
5 // author: karl malbrain, malbrain@cal.berkeley.edu
8 This work, including the source code, documentation
9 and related data, is placed into the public domain.
11 The orginal author is Karl Malbrain.
13 THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
14 OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
15 MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
16 ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
17 RESULTING FROM THE USE, MODIFICATION, OR
18 REDISTRIBUTION OF THIS SOFTWARE.
21 // Please see the project home page for documentation
22 // code.google.com/p/high-concurrency-btree
24 #define _FILE_OFFSET_BITS 64
25 #define _LARGEFILE64_SOURCE
40 #define WIN32_LEAN_AND_MEAN
51 typedef unsigned long long uid;
54 typedef unsigned long long off64_t;
55 typedef unsigned short ushort;
56 typedef unsigned int uint;
59 #define BT_ro 0x6f72 // ro
60 #define BT_rw 0x7772 // rw
61 #define BT_fl 0x6c66 // fl
63 #define BT_maxbits 15 // maximum page size in bits
64 #define BT_minbits 12 // minimum page size in bits
65 #define BT_minpage (1 << BT_minbits) // minimum page size
66 #define BT_maxpage (1 << BT_maxbits) // maximum page size
69 There are five lock types for each node in three independent sets:
70 1. (set 1) AccessIntent: Sharable. Going to Read the node. Incompatible with NodeDelete.
71 2. (set 1) NodeDelete: Exclusive. About to release the node. Incompatible with AccessIntent.
72 3. (set 2) ReadLock: Sharable. Read the node. Incompatible with WriteLock.
73 4. (set 2) WriteLock: Exclusive. Modify the node. Incompatible with ReadLock and other WriteLocks.
74 5. (set 3) ParentModification: Exclusive. Change the node's parent keys. Incompatible with another ParentModification.
85 // definition for latch implementation
87 // exclusive is set for write access
88 // share is count of read accessors
89 // grant write lock when share == 0
91 volatile typedef struct {
92 unsigned char mutex[1];
93 unsigned char exclusive:1;
94 unsigned char pending:1;
98 // Define the length of the page and key pointers
102 // Page key slot definition.
104 // If BT_maxbits is 15 or less, you can save 2 bytes
105 // for each key stored by making the first two uints
106 // into ushorts. You can also save 4 bytes by removing
107 // the tod field from the key.
109 // Keys are marked dead, but remain on the page until
110 // cleanup is called. The fence key (highest key) for
111 // the page is always present, even if dead.
115 uint tod; // time-stamp for key
117 ushort off:BT_maxbits; // page offset for key start
118 ushort dead:1; // set for deleted key
119 unsigned char id[BtId]; // id associated with key
122 // The key structure occupies space at the upper end of
123 // each page. It's a length byte followed by the value
128 unsigned char key[0];
131 // The first part of an index page.
132 // It is immediately followed
133 // by the BtSlot array of keys.
135 typedef struct BtPage_ {
136 uint cnt; // count of keys in page
137 uint act; // count of active keys
138 uint min; // next key offset
139 unsigned char bits:7; // page size in bits
140 unsigned char free:1; // page is on free list
141 unsigned char lvl:6; // level of page
142 unsigned char kill:1; // page is being deleted
143 unsigned char dirty:1; // page is dirty
144 unsigned char right[BtId]; // page number to right
148 struct BtPage_ alloc[2]; // next & free page_nos in right ptr
149 BtSpinLatch lock[1]; // allocation area lite latch
150 uint latchdeployed; // highest number of latch entries deployed
151 uint nlatchpage; // number of latch pages at BT_latch
152 uint latchtotal; // number of page latch entries
153 uint latchhash; // number of latch hash table slots
154 uint latchvictim; // next latch entry to examine
157 // latch hash table entries
160 volatile uint slot; // Latch table entry at head of collision chain
161 BtSpinLatch latch[1]; // lock for the collision chain
164 // latch manager table structure
167 BtSpinLatch readwr[1]; // read/write page lock
168 BtSpinLatch access[1]; // Access Intent/Page delete
169 BtSpinLatch parent[1]; // Posting of fence key in parent
170 BtSpinLatch busy[1]; // slot is being moved between chains
171 volatile uint next; // next entry in hash table chain
172 volatile uint prev; // prev entry in hash table chain
173 volatile uint hash; // hash slot entry is under
174 volatile ushort dirty; // page is dirty in cache
175 volatile ushort pin; // number of outstanding pins
176 volatile uid page_no; // latch set page number on disk
179 // The object structure for Btree access
181 typedef struct _BtDb {
182 uint page_size; // each page size
183 uint page_bits; // each page size in bits
184 uid page_no; // current page number
185 uid cursor_page; // current cursor page number
187 uint mode; // read-write mode
188 BtPage alloc; // frame buffer for alloc page ( page 0 )
189 BtPage cursor; // cached frame for start/next (never mapped)
190 BtPage frame; // spare frame for the page split (never mapped)
191 BtPage zero; // zeroes frame buffer (never mapped)
192 BtPage page; // current page
193 BtLatchSet *latch; // current page latch
194 BtLatchMgr *latchmgr; // mapped latch page from allocation page
195 BtLatchSet *latchsets; // mapped latch set from latch pages
196 unsigned char *latchpool; // cached page pool set
197 BtHashEntry *table; // the hash table
202 HANDLE halloc; // allocation and latch table handle
204 unsigned char *mem; // frame, cursor, page memory buffer
205 uint found; // last deletekey found key
223 extern void bt_close (BtDb *bt);
224 extern BtDb *bt_open (char *name, uint mode, uint bits, uint cacheblk);
225 extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod);
226 extern BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl);
227 extern uid bt_findkey (BtDb *bt, unsigned char *key, uint len);
228 extern uint bt_startkey (BtDb *bt, unsigned char *key, uint len);
229 extern uint bt_nextkey (BtDb *bt, uint slot);
231 // internal functions
232 BTERR bt_update (BtDb *bt, BtPage page, BtLatchSet *latch);
233 BtPage bt_mappage (BtDb *bt, BtLatchSet *latch);
234 // Helper functions to return slot values
236 extern BtKey bt_key (BtDb *bt, uint slot);
237 extern uid bt_uid (BtDb *bt, uint slot);
239 extern uint bt_tod (BtDb *bt, uint slot);
242 // BTree page number constants
248 // Number of levels to create in a new BTree
252 // The page is allocated from low and hi ends.
253 // The key offsets and row-id's are allocated
254 // from the bottom, while the text of the key
255 // is allocated from the top. When the two
256 // areas meet, the page is split into two.
258 // A key consists of a length byte, two bytes of
259 // index number (0 - 65534), and up to 253 bytes
260 // of key value. Duplicate keys are discarded.
261 // Associated with each key is a 48 bit row-id.
263 // The b-tree root is always located at page 1.
264 // The first leaf page of level zero is always
265 // located on page 2.
267 // The b-tree pages are linked with right
268 // pointers to facilitate enumerators,
269 // and provide for concurrency.
271 // When to root page fills, it is split in two and
272 // the tree height is raised by a new root at page
273 // one with two keys.
275 // Deleted keys are marked with a dead bit until
276 // page cleanup The fence key for a node is always
277 // present, even after deletion and cleanup.
279 // Deleted leaf pages are reclaimed on a free list.
280 // The upper levels of the btree are fixed on creation.
282 // To achieve maximum concurrency one page is locked at a time
283 // as the tree is traversed to find leaf key in question. The right
284 // page numbers are used in cases where the page is being split,
287 // Page 0 (ALLOC page) is dedicated to lock for new page extensions,
288 // and chains empty leaf pages together for reuse.
290 // Parent locks are obtained to prevent resplitting or deleting a node
291 // before its fence is posted into its upper level.
293 // A special open mode of BT_fl is provided to safely access files on
294 // WIN32 networks. WIN32 network operations should not use memory mapping.
295 // This WIN32 mode sets FILE_FLAG_NOBUFFERING and FILE_FLAG_WRITETHROUGH
296 // to prevent local caching of network file contents.
298 // Access macros to address slot and key values from the page.
299 // Page slots use 1 based indexing.
301 #define slotptr(page, slot) (((BtSlot *)(page+1)) + (slot-1))
302 #define keyptr(page, slot) ((BtKey)((unsigned char*)(page) + slotptr(page, slot)->off))
304 void bt_putid(unsigned char *dest, uid id)
309 dest[i] = (unsigned char)id, id >>= 8;
312 uid bt_getid(unsigned char *src)
317 for( i = 0; i < BtId; i++ )
318 id <<= 8, id |= *src++;
323 BTERR bt_abort (BtDb *bt, BtPage page, uid page_no, BTERR err)
327 fprintf(stderr, "\n Btree2 abort, error %d on page %.8x\n", err, page_no);
328 fprintf(stderr, "level=%d kill=%d free=%d cnt=%x act=%x\n", page->lvl, page->kill, page->free, page->cnt, page->act);
329 ptr = keyptr(page, page->cnt);
330 fprintf(stderr, "fence='%.*s'\n", ptr->len, ptr->key);
331 fprintf(stderr, "right=%.8x\n", bt_getid(page->right));
332 return bt->err = err;
337 // wait until write lock mode is clear
338 // and add 1 to the share count
340 void bt_spinreadlock(BtSpinLatch *latch)
345 // obtain latch mutex
347 if( __sync_lock_test_and_set(latch->mutex, 1) )
350 if( _InterlockedExchange8(latch->mutex, 1) )
353 // see if exclusive request is granted or pending
355 if( prev = !(latch->exclusive | latch->pending) )
361 _InterlockedExchange8(latch->mutex, 0);
368 } while( sched_yield(), 1 );
370 } while( SwitchToThread(), 1 );
374 // wait for other read and write latches to relinquish
376 void bt_spinwritelock(BtSpinLatch *latch)
382 if( __sync_lock_test_and_set(latch->mutex, 1) )
385 if( _InterlockedExchange8(latch->mutex, 1) )
388 if( prev = !(latch->share | latch->exclusive) )
389 latch->exclusive = 1, latch->pending = 0;
395 _InterlockedExchange8(latch->mutex, 0);
400 } while( sched_yield(), 1 );
402 } while( SwitchToThread(), 1 );
406 // try to obtain write lock
408 // return 1 if obtained,
411 int bt_spinwritetry(BtSpinLatch *latch)
416 if( __sync_lock_test_and_set(latch->mutex, 1) )
419 if( _InterlockedExchange8(latch->mutex, 1) )
422 // take write access if all bits are clear
424 if( prev = !(latch->exclusive | latch->share) )
425 latch->exclusive = 1;
430 _InterlockedExchange8(latch->mutex, 0);
437 void bt_spinreleasewrite(BtSpinLatch *latch)
440 while( __sync_lock_test_and_set(latch->mutex, 1) )
443 while( _InterlockedExchange8(latch->mutex, 1) )
446 latch->exclusive = 0;
450 _InterlockedExchange8(latch->mutex, 0);
454 // decrement reader count
456 void bt_spinreleaseread(BtSpinLatch *latch)
459 while( __sync_lock_test_and_set(latch->mutex, 1) )
462 while( _InterlockedExchange8(latch->mutex, 1) )
469 _InterlockedExchange8(latch->mutex, 0);
473 // link latch table entry into head of latch hash table
475 BTERR bt_latchlink (BtDb *bt, uint hashidx, uint victim, uid page_no)
477 BtPage page = (BtPage)(victim * bt->page_size + bt->latchpool);
478 BtLatchSet *latch = bt->latchsets + victim;
479 off64_t off = page_no << bt->page_bits;
482 if( latch->next = bt->table[hashidx].slot )
483 bt->latchsets[latch->next].prev = victim;
485 bt->table[hashidx].slot = victim;
486 latch->page_no = page_no;
487 latch->hash = hashidx;
491 if( pread (bt->idx, page, bt->page_size, page_no << bt->page_bits) )
492 return bt->err = BTERR_read;
494 SetFilePointer (bt->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
495 if( !ReadFile(bt->idx, page, bt->page_size, amt, NULL))
496 return bt->err = BTERR_read;
497 if( *amt < bt->page_size )
498 return bt->err = BTERR_read;
505 void bt_unpinlatch (BtLatchSet *latch)
508 __sync_fetch_and_add(&latch->pin, -1);
510 _InterlockedDecrement16 (&latch->pin);
514 // find existing latchset or inspire new one
515 // return with latchset pinned
517 BtLatchSet *bt_pinlatch (BtDb *bt, uid page_no)
519 uint hashidx = page_no % bt->latchmgr->latchhash;
520 uint slot, victim, idx;
526 // try to find unpinned entry
528 bt_spinwritelock(bt->table[hashidx].latch);
530 if( slot = bt->table[hashidx].slot ) do
532 latch = bt->latchsets + slot;
533 if( page_no == latch->page_no )
535 } while( slot = latch->next );
540 latch = bt->latchsets + slot;
542 __sync_fetch_and_add(&latch->pin, 1);
544 _InterlockedIncrement16 (&latch->pin);
546 bt_spinreleasewrite(bt->table[hashidx].latch);
550 // see if there are any unused entries
552 victim = __sync_fetch_and_add (&bt->latchmgr->latchdeployed, 1) + 1;
554 victim = _InterlockedIncrement (&bt->latchmgr->latchdeployed);
557 if( victim < bt->latchmgr->latchtotal ) {
558 latch = bt->latchsets + victim;
560 __sync_fetch_and_add(&latch->pin, 1);
562 _InterlockedIncrement16 (&latch->pin);
564 bt_latchlink (bt, hashidx, victim, page_no);
565 bt_spinreleasewrite (bt->table[hashidx].latch);
570 victim = __sync_fetch_and_add (&bt->latchmgr->latchdeployed, -1);
572 victim = _InterlockedDecrement (&bt->latchmgr->latchdeployed);
574 // find and reuse previous lock entry
578 victim = __sync_fetch_and_add(&bt->latchmgr->latchvictim, 1);
580 victim = _InterlockedIncrement (&bt->latchmgr->latchvictim) - 1;
582 // we don't use slot zero
584 if( victim %= bt->latchmgr->latchtotal )
585 latch = bt->latchsets + victim;
589 // take control of our slot
590 // from other threads
592 if( latch->pin || !bt_spinwritetry (latch->busy) )
597 // try to get write lock on hash chain
598 // skip entry if not obtained
599 // or has outstanding locks
601 if( !bt_spinwritetry (bt->table[idx].latch) ) {
602 bt_spinreleasewrite (latch->busy);
607 bt_spinreleasewrite (latch->busy);
608 bt_spinreleasewrite (bt->table[idx].latch);
612 // update permanent page area in btree
614 page = (BtPage)(victim * bt->page_size + bt->latchpool);
615 off = latch->page_no << bt->page_bits;
618 if( pwrite(bt->idx, page, bt->page_size, off) < bt->page_size )
619 return bt->err = BTERR_wrt, NULL;
622 SetFilePointer (bt->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
624 if( !WriteFile(bt->idx, page, bt->page_size, amt, NULL) )
625 return bt->err = BTERR_wrt, NULL;
627 if( *amt < bt->page_size )
628 return bt->err = BTERR_wrt, NULL;
631 // unlink our available victim from its hash chain
634 bt->latchsets[latch->prev].next = latch->next;
636 bt->table[idx].slot = latch->next;
639 bt->latchsets[latch->next].prev = latch->prev;
641 bt_spinreleasewrite (bt->table[idx].latch);
643 __sync_fetch_and_add(&latch->pin, 1);
645 _InterlockedIncrement16 (&latch->pin);
647 bt_latchlink (bt, hashidx, victim, page_no);
648 bt_spinreleasewrite (bt->table[hashidx].latch);
649 bt_spinreleasewrite (latch->busy);
654 // close and release memory
656 void bt_close (BtDb *bt)
659 munmap (bt->table, bt->latchmgr->nlatchpage * bt->page_size);
660 munmap (bt->latchmgr, bt->page_size);
662 FlushViewOfFile(bt->latchmgr, 0);
663 UnmapViewOfFile(bt->latchmgr);
664 CloseHandle(bt->halloc);
673 VirtualFree (bt->mem, 0, MEM_RELEASE);
674 FlushFileBuffers(bt->idx);
675 CloseHandle(bt->idx);
679 // open/create new btree
681 // call with file_name, BT_openmode, bits in page size (e.g. 16),
682 // size of mapped page pool (e.g. 8192)
684 BtDb *bt_open (char *name, uint mode, uint bits, uint nodemax)
686 uint lvl, attr, last, slot, idx;
687 uint nlatchpage, latchhash;
688 BtLatchMgr *latchmgr;
698 struct flock lock[1];
701 // determine sanity of page size and buffer pool
703 if( bits > BT_maxbits )
705 else if( bits < BT_minbits )
709 bt = calloc (1, sizeof(BtDb));
711 bt->idx = open ((char*)name, O_RDWR | O_CREAT, 0666);
714 return free(bt), NULL;
716 bt = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, sizeof(BtDb));
717 attr = FILE_ATTRIBUTE_NORMAL;
718 bt->idx = CreateFile(name, GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, attr, NULL);
720 if( bt->idx == INVALID_HANDLE_VALUE )
721 return GlobalFree(bt), NULL;
724 memset (lock, 0, sizeof(lock));
726 lock->l_type = F_WRLCK;
727 lock->l_len = sizeof(struct BtPage_);
730 if( fcntl (bt->idx, F_SETLKW, lock) < 0 )
731 return bt_close (bt), NULL;
733 memset (ovl, 0, sizeof(ovl));
734 len = sizeof(struct BtPage_);
736 // use large offsets to
737 // simulate advisory locking
739 ovl->OffsetHigh |= 0x80000000;
741 if( LockFileEx (bt->idx, LOCKFILE_EXCLUSIVE_LOCK, 0, len, 0L, ovl) )
742 return bt_close (bt), NULL;
746 latchmgr = malloc (BT_maxpage);
749 // read minimum page size to get root info
751 if( size = lseek (bt->idx, 0L, 2) ) {
752 if( pread(bt->idx, latchmgr, BT_minpage, 0) == BT_minpage )
753 bits = latchmgr->alloc->bits;
755 return free(bt), free(latchmgr), NULL;
756 } else if( mode == BT_ro )
757 return free(latchmgr), bt_close (bt), NULL;
759 latchmgr = VirtualAlloc(NULL, BT_maxpage, MEM_COMMIT, PAGE_READWRITE);
760 size = GetFileSize(bt->idx, amt);
763 if( !ReadFile(bt->idx, (char *)latchmgr, BT_minpage, amt, NULL) )
764 return bt_close (bt), NULL;
765 bits = latchmgr->alloc->bits;
766 } else if( mode == BT_ro )
767 return bt_close (bt), NULL;
770 bt->page_size = 1 << bits;
771 bt->page_bits = bits;
778 // initialize an empty b-tree with latch page, root page, page of leaves
779 // and page(s) of latches and page pool cache
781 memset (latchmgr, 0, 1 << bits);
782 latchmgr->alloc->bits = bt->page_bits;
784 // calculate number of latch hash table entries
786 nlatchpage = (nodemax/8 * sizeof(BtHashEntry) + bt->page_size - 1) / bt->page_size;
787 latchhash = nlatchpage * bt->page_size / sizeof(BtHashEntry);
789 nlatchpage += nodemax; // size of the buffer pool in pages
790 nlatchpage += (sizeof(BtLatchSet) * nodemax + bt->page_size - 1)/bt->page_size;
792 bt_putid(latchmgr->alloc->right, MIN_lvl+1+nlatchpage);
793 latchmgr->nlatchpage = nlatchpage;
794 latchmgr->latchtotal = nodemax;
795 latchmgr->latchhash = latchhash;
797 if( write (bt->idx, latchmgr, bt->page_size) < bt->page_size )
798 return bt_close (bt), NULL;
800 if( !WriteFile (bt->idx, (char *)latchmgr, bt->page_size, amt, NULL) )
801 return bt_close (bt), NULL;
803 if( *amt < bt->page_size )
804 return bt_close (bt), NULL;
806 memset (latchmgr, 0, 1 << bits);
807 latchmgr->alloc->bits = bt->page_bits;
809 for( lvl=MIN_lvl; lvl--; ) {
810 slotptr(latchmgr->alloc, 1)->off = bt->page_size - 3;
811 bt_putid(slotptr(latchmgr->alloc, 1)->id, lvl ? MIN_lvl - lvl + 1 : 0); // next(lower) page number
812 key = keyptr(latchmgr->alloc, 1);
813 key->len = 2; // create stopper key
816 latchmgr->alloc->min = bt->page_size - 3;
817 latchmgr->alloc->lvl = lvl;
818 latchmgr->alloc->cnt = 1;
819 latchmgr->alloc->act = 1;
821 if( write (bt->idx, latchmgr, bt->page_size) < bt->page_size )
822 return bt_close (bt), NULL;
824 if( !WriteFile (bt->idx, (char *)latchmgr, bt->page_size, amt, NULL) )
825 return bt_close (bt), NULL;
827 if( *amt < bt->page_size )
828 return bt_close (bt), NULL;
832 // clear out latch manager pages
834 memset(latchmgr, 0, bt->page_size);
837 while( last < ((MIN_lvl + 1 + nlatchpage) ) ) {
838 off = (uid)last << bt->page_bits;
840 pwrite(bt->idx, latchmgr, bt->page_size, off);
842 SetFilePointer (bt->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
843 if( !WriteFile (bt->idx, (char *)latchmgr, bt->page_size, amt, NULL) )
844 return bt_close (bt), NULL;
845 if( *amt < bt->page_size )
846 return bt_close (bt), NULL;
853 lock->l_type = F_UNLCK;
854 if( fcntl (bt->idx, F_SETLK, lock) < 0 )
855 return bt_close (bt), NULL;
857 if( !UnlockFileEx (bt->idx, 0, sizeof(struct BtPage_), 0, ovl) )
858 return bt_close (bt), NULL;
861 flag = PROT_READ | PROT_WRITE;
862 bt->latchmgr = mmap (0, bt->page_size, flag, MAP_SHARED, bt->idx, ALLOC_page * bt->page_size);
863 if( bt->latchmgr == MAP_FAILED )
864 return bt_close (bt), NULL;
865 bt->table = (void *)mmap (0, bt->latchmgr->nlatchpage * bt->page_size, flag, MAP_SHARED, bt->idx, LATCH_page * bt->page_size);
866 if( bt->table == MAP_FAILED )
867 return bt_close (bt), NULL;
869 flag = PAGE_READWRITE;
870 bt->halloc = CreateFileMapping(bt->idx, NULL, flag, 0, (bt->latchmgr->nlatchpage + LATCH_page) * bt->page_size, NULL);
872 return bt_close (bt), NULL;
874 flag = FILE_MAP_WRITE;
875 bt->latchmgr = MapViewOfFile(bt->halloc, flag, 0, 0, (bt->latchmgr->nlatchpage + LATCH_page) * bt->page_size);
877 return GetLastError(), bt_close (bt), NULL;
879 bt->table = (void *)((char *)bt->latchmgr + LATCH_page * bt->page_size);
881 bt->latchpool = (unsigned char *)bt->table + (bt->latchmgr->nlatchpage - bt->latchmgr->latchtotal) * bt->page_size;
882 bt->latchsets = (BtLatchSet *)(bt->latchpool - bt->latchmgr->latchtotal * sizeof(BtLatchSet));
887 VirtualFree (latchmgr, 0, MEM_RELEASE);
891 bt->mem = malloc (3 * bt->page_size);
893 bt->mem = VirtualAlloc(NULL, 3 * bt->page_size, MEM_COMMIT, PAGE_READWRITE);
895 bt->frame = (BtPage)bt->mem;
896 bt->cursor = (BtPage)(bt->mem + bt->page_size);
897 bt->zero = (BtPage)(bt->mem + 2 * bt->page_size);
899 memset (bt->zero, 0, bt->page_size);
903 // place write, read, or parent lock on requested page_no.
905 void bt_lockpage(BtLock mode, BtLatchSet *latch)
909 bt_spinreadlock (latch->readwr);
912 bt_spinwritelock (latch->readwr);
915 bt_spinreadlock (latch->access);
918 bt_spinwritelock (latch->access);
921 bt_spinwritelock (latch->parent);
926 // remove write, read, or parent lock on requested page
928 void bt_unlockpage(BtLock mode, BtLatchSet *latch)
932 bt_spinreleaseread (latch->readwr);
935 bt_spinreleasewrite (latch->readwr);
938 bt_spinreleaseread (latch->access);
941 bt_spinreleasewrite (latch->access);
944 bt_spinreleasewrite (latch->parent);
949 // allocate a new page and write page into it
951 uid bt_newpage(BtDb *bt, BtPage page)
960 // lock allocation page
962 bt_spinwritelock(bt->latchmgr->lock);
964 // use empty chain first
965 // else allocate empty page
967 if( new_page = bt_getid(bt->latchmgr->alloc[1].right) ) {
968 latch = bt_pinlatch (bt, new_page);
969 temp = bt_mappage (bt, latch);
971 bt_putid(bt->latchmgr->alloc[1].right, bt_getid(temp->right));
972 bt_spinreleasewrite(bt->latchmgr->lock);
973 memcpy (temp, page, bt->page_size);
975 if( bt_update (bt, temp, latch) )
978 bt_unpinlatch (latch);
980 new_page = bt_getid(bt->latchmgr->alloc->right);
981 bt_putid(bt->latchmgr->alloc->right, new_page+1);
982 bt_spinreleasewrite(bt->latchmgr->lock);
983 off = new_page << bt->page_bits;
985 if( pwrite(bt->idx, page, bt->page_size, off) < bt->page_size )
986 return bt->err = BTERR_wrt, 0;
988 SetFilePointer (bt->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
990 if( !WriteFile(bt->idx, page, bt->page_size, amt, NULL) )
991 return bt->err = BTERR_wrt, 0;
993 if( *amt < bt->page_size )
994 return bt->err = BTERR_wrt, 0;
1001 // compare two keys, returning > 0, = 0, or < 0
1002 // as the comparison value
1004 int keycmp (BtKey key1, unsigned char *key2, uint len2)
1006 uint len1 = key1->len;
1009 if( ans = memcmp (key1->key, key2, len1 > len2 ? len2 : len1) )
1020 // Update current page of btree by
1021 // flushing mapped area to disk backing of cache pool.
1023 BTERR bt_update (BtDb *bt, BtPage page, BtLatchSet *latch)
1026 msync (page, bt->page_size, MS_ASYNC);
1028 FlushViewOfFile (page, bt->page_size);
1034 // map the btree cached page onto current page
1036 BtPage bt_mappage (BtDb *bt, BtLatchSet *latch)
1038 return (BtPage)((latch - bt->latchsets) * bt->page_size + bt->latchpool);
1041 // deallocate a deleted page
1042 // place on free chain out of allocator page
1043 // call with page latched for Writing and Deleting
1045 BTERR bt_freepage(BtDb *bt, uid page_no, BtLatchSet *latch)
1047 BtPage page = bt_mappage (bt, latch);
1049 // lock allocation page
1051 bt_spinwritelock (bt->latchmgr->lock);
1053 // store chain in second right
1054 bt_putid(page->right, bt_getid(bt->latchmgr->alloc[1].right));
1055 bt_putid(bt->latchmgr->alloc[1].right, page_no);
1058 if( bt_update(bt, page, latch) )
1061 // unlock released page
1063 bt_unlockpage (BtLockDelete, latch);
1064 bt_unlockpage (BtLockWrite, latch);
1065 bt_unpinlatch (latch);
1067 // unlock allocation page
1069 bt_spinreleasewrite (bt->latchmgr->lock);
1073 // find slot in page for given key at a given level
1075 int bt_findslot (BtDb *bt, unsigned char *key, uint len)
1077 uint diff, higher = bt->page->cnt, low = 1, slot;
1080 // make stopper key an infinite fence value
1082 if( bt_getid (bt->page->right) )
1087 // low is the lowest candidate, higher is already
1088 // tested as .ge. the given key, loop ends when they meet
1090 while( diff = higher - low ) {
1091 slot = low + ( diff >> 1 );
1092 if( keycmp (keyptr(bt->page, slot), key, len) < 0 )
1095 higher = slot, good++;
1098 // return zero if key is on right link page
1100 return good ? higher : 0;
1103 // find and load page at given level for given key
1104 // leave page rd or wr locked as requested
1106 int bt_loadpage (BtDb *bt, unsigned char *key, uint len, uint lvl, uint lock)
1108 uid page_no = ROOT_page, prevpage = 0;
1109 uint drill = 0xff, slot;
1110 BtLatchSet *prevlatch;
1111 uint mode, prevmode;
1113 // start at root of btree and drill down
1116 // determine lock mode of drill level
1117 mode = (lock == BtLockWrite) && (drill == lvl) ? BtLockWrite : BtLockRead;
1119 bt->latch = bt_pinlatch(bt, page_no);
1120 bt->page_no = page_no;
1122 // obtain access lock using lock chaining
1124 if( page_no > ROOT_page )
1125 bt_lockpage(BtLockAccess, bt->latch);
1128 bt_unlockpage(prevmode, prevlatch);
1129 bt_unpinlatch(prevlatch);
1133 // obtain read lock using lock chaining
1135 bt_lockpage(mode, bt->latch);
1137 if( page_no > ROOT_page )
1138 bt_unlockpage(BtLockAccess, bt->latch);
1140 // map/obtain page contents
1142 bt->page = bt_mappage (bt, bt->latch);
1144 // re-read and re-lock root after determining actual level of root
1146 if( bt->page->lvl != drill) {
1147 if( bt->page_no != ROOT_page )
1148 return bt->err = BTERR_struct, 0;
1150 drill = bt->page->lvl;
1152 if( lock != BtLockRead && drill == lvl ) {
1153 bt_unlockpage(mode, bt->latch);
1154 bt_unpinlatch(bt->latch);
1159 prevpage = bt->page_no;
1160 prevlatch = bt->latch;
1163 // find key on page at this level
1164 // and descend to requested level
1166 if( !bt->page->kill )
1167 if( slot = bt_findslot (bt, key, len) ) {
1171 while( slotptr(bt->page, slot)->dead )
1172 if( slot++ < bt->page->cnt )
1177 page_no = bt_getid(slotptr(bt->page, slot)->id);
1182 // or slide right into next page
1185 page_no = bt_getid(bt->page->right);
1189 // return error on end of right chain
1191 bt->err = BTERR_eof;
1192 return 0; // return error
1195 // a fence key was deleted from a page
1196 // push new fence value upwards
1198 BTERR bt_fixfence (BtDb *bt, uid page_no, uint lvl)
1200 unsigned char leftkey[256], rightkey[256];
1201 BtLatchSet *latch = bt->latch;
1204 // remove deleted key, the old fence value
1206 ptr = keyptr(bt->page, bt->page->cnt);
1207 memcpy(rightkey, ptr, ptr->len + 1);
1209 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1210 bt->page->dirty = 1;
1212 ptr = keyptr(bt->page, bt->page->cnt);
1213 memcpy(leftkey, ptr, ptr->len + 1);
1215 if( bt_update (bt, bt->page, latch) )
1218 bt_lockpage (BtLockParent, latch);
1219 bt_unlockpage (BtLockWrite, latch);
1221 // insert new (now smaller) fence key
1223 if( bt_insertkey (bt, leftkey+1, *leftkey, lvl + 1, page_no, time(NULL)) )
1226 // remove old (larger) fence key
1228 if( bt_deletekey (bt, rightkey+1, *rightkey, lvl + 1) )
1231 bt_unlockpage (BtLockParent, latch);
1232 bt_unpinlatch (latch);
1236 // root has a single child
1237 // collapse a level from the btree
1238 // call with root locked in bt->page
1240 BTERR bt_collapseroot (BtDb *bt, BtPage root)
1247 // find the child entry
1248 // and promote to new root
1251 for( idx = 0; idx++ < root->cnt; )
1252 if( !slotptr(root, idx)->dead )
1255 child = bt_getid (slotptr(root, idx)->id);
1256 latch = bt_pinlatch (bt, child);
1258 bt_lockpage (BtLockDelete, latch);
1259 bt_lockpage (BtLockWrite, latch);
1261 temp = bt_mappage (bt, latch);
1262 memcpy (root, temp, bt->page_size);
1264 if( bt_update (bt, root, bt->latch) )
1267 if( bt_freepage (bt, child, latch) )
1270 } while( root->lvl > 1 && root->act == 1 );
1272 bt_unlockpage (BtLockWrite, bt->latch);
1273 bt_unpinlatch (bt->latch);
1277 // find and delete key on page by marking delete flag bit
1278 // when page becomes empty, delete it
1280 BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl)
1282 unsigned char lowerkey[256], higherkey[256];
1283 uint slot, dirty = 0, idx, fence, found;
1284 BtLatchSet *latch, *rlatch;
1289 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1290 ptr = keyptr(bt->page, slot);
1294 // are we deleting a fence slot?
1296 fence = slot == bt->page->cnt;
1298 // if key is found delete it, otherwise ignore request
1300 if( found = !keycmp (ptr, key, len) )
1301 if( found = slotptr(bt->page, slot)->dead == 0 ) {
1302 dirty = slotptr(bt->page,slot)->dead = 1;
1303 bt->page->dirty = 1;
1306 // collapse empty slots
1308 while( idx = bt->page->cnt - 1 )
1309 if( slotptr(bt->page, idx)->dead ) {
1310 *slotptr(bt->page, idx) = *slotptr(bt->page, idx + 1);
1311 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1316 right = bt_getid(bt->page->right);
1317 page_no = bt->page_no;
1322 return bt_abort (bt, bt->page, page_no, BTERR_notfound);
1323 bt_unlockpage(BtLockWrite, latch);
1324 bt_unpinlatch (latch);
1325 return bt->found = found, 0;
1328 // did we delete a fence key in an upper level?
1330 if( lvl && bt->page->act && fence )
1331 if( bt_fixfence (bt, page_no, lvl) )
1334 return bt->found = found, 0;
1336 // is this a collapsed root?
1338 if( lvl > 1 && page_no == ROOT_page && bt->page->act == 1 )
1339 if( bt_collapseroot (bt, bt->page) )
1342 return bt->found = found, 0;
1344 // return if page is not empty
1346 if( bt->page->act ) {
1347 if( bt_update(bt, bt->page, latch) )
1349 bt_unlockpage(BtLockWrite, latch);
1350 bt_unpinlatch (latch);
1351 return bt->found = found, 0;
1354 // cache copy of fence key
1355 // in order to find parent
1357 ptr = keyptr(bt->page, bt->page->cnt);
1358 memcpy(lowerkey, ptr, ptr->len + 1);
1360 // obtain lock on right page
1362 rlatch = bt_pinlatch (bt, right);
1363 bt_lockpage(BtLockWrite, rlatch);
1365 temp = bt_mappage (bt, rlatch);
1368 bt_abort(bt, temp, right, 0);
1369 return bt_abort(bt, bt->page, bt->page_no, BTERR_kill);
1372 // pull contents of next page into current empty page
1374 memcpy (bt->page, temp, bt->page_size);
1376 // cache copy of key to update
1378 ptr = keyptr(temp, temp->cnt);
1379 memcpy(higherkey, ptr, ptr->len + 1);
1381 // Mark right page as deleted and point it to left page
1382 // until we can post updates at higher level.
1384 bt_putid(temp->right, page_no);
1387 if( bt_update(bt, bt->page, latch) )
1390 if( bt_update(bt, temp, rlatch) )
1393 bt_lockpage(BtLockParent, latch);
1394 bt_unlockpage(BtLockWrite, latch);
1396 bt_lockpage(BtLockParent, rlatch);
1397 bt_unlockpage(BtLockWrite, rlatch);
1399 // redirect higher key directly to consolidated node
1401 if( bt_insertkey (bt, higherkey+1, *higherkey, lvl+1, page_no, time(NULL)) )
1404 // delete old lower key to consolidated node
1406 if( bt_deletekey (bt, lowerkey + 1, *lowerkey, lvl + 1) )
1409 // obtain write & delete lock on deleted node
1410 // add right block to free chain
1412 bt_lockpage(BtLockDelete, rlatch);
1413 bt_lockpage(BtLockWrite, rlatch);
1414 bt_unlockpage(BtLockParent, rlatch);
1416 if( bt_freepage (bt, right, rlatch) )
1419 bt_unlockpage(BtLockParent, latch);
1420 bt_unpinlatch(latch);
1424 // find key in leaf level and return row-id
1426 uid bt_findkey (BtDb *bt, unsigned char *key, uint len)
1432 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1433 ptr = keyptr(bt->page, slot);
1437 // if key exists, return row-id
1438 // otherwise return 0
1440 if( ptr->len == len && !memcmp (ptr->key, key, len) )
1441 id = bt_getid(slotptr(bt->page,slot)->id);
1445 bt_unlockpage (BtLockRead, bt->latch);
1446 bt_unpinlatch (bt->latch);
1450 // check page for space available,
1451 // clean if necessary and return
1452 // 0 - page needs splitting
1453 // >0 - go ahead with new slot
1455 uint bt_cleanpage(BtDb *bt, uint amt, uint slot)
1457 uint nxt = bt->page_size;
1458 BtPage page = bt->page;
1459 uint cnt = 0, idx = 0;
1460 uint max = page->cnt;
1461 uint newslot = slot;
1465 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1468 // skip cleanup if nothing to reclaim
1473 memcpy (bt->frame, page, bt->page_size);
1475 // skip page info and set rest of page to zero
1477 memset (page+1, 0, bt->page_size - sizeof(*page));
1480 while( cnt++ < max ) {
1483 // always leave fence key in list
1484 if( cnt < max && slotptr(bt->frame,cnt)->dead )
1488 key = keyptr(bt->frame, cnt);
1489 nxt -= key->len + 1;
1490 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1493 memcpy(slotptr(page, ++idx)->id, slotptr(bt->frame, cnt)->id, BtId);
1494 if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
1497 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1499 slotptr(page, idx)->off = nxt;
1505 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1511 // split the root and raise the height of the btree
1513 BTERR bt_splitroot(BtDb *bt, unsigned char *leftkey, uid page_no2)
1515 uint nxt = bt->page_size;
1516 BtPage root = bt->page;
1519 // Obtain an empty page to use, and copy the current
1520 // root contents into it
1522 if( !(right = bt_newpage(bt, root)) )
1525 // preserve the page info at the bottom
1526 // and set rest to zero
1528 memset(root+1, 0, bt->page_size - sizeof(*root));
1530 // insert first key on newroot page
1532 nxt -= *leftkey + 1;
1533 memcpy ((unsigned char *)root + nxt, leftkey, *leftkey + 1);
1534 bt_putid(slotptr(root, 1)->id, right);
1535 slotptr(root, 1)->off = nxt;
1537 // insert second key on newroot page
1538 // and increase the root height
1541 ((unsigned char *)root)[nxt] = 2;
1542 ((unsigned char *)root)[nxt+1] = 0xff;
1543 ((unsigned char *)root)[nxt+2] = 0xff;
1544 bt_putid(slotptr(root, 2)->id, page_no2);
1545 slotptr(root, 2)->off = nxt;
1547 bt_putid(root->right, 0);
1548 root->min = nxt; // reset lowest used offset and key count
1553 // update and release root (bt->page)
1555 if( bt_update(bt, root, bt->latch) )
1558 bt_unlockpage(BtLockWrite, bt->latch);
1559 bt_unpinlatch(bt->latch);
1563 // split already locked full node
1566 BTERR bt_splitpage (BtDb *bt)
1568 uint cnt = 0, idx = 0, max, nxt = bt->page_size;
1569 unsigned char fencekey[256], rightkey[256];
1570 uid page_no = bt->page_no, right;
1571 BtLatchSet *latch, *rlatch;
1572 BtPage page = bt->page;
1573 uint lvl = page->lvl;
1578 // split higher half of keys to bt->frame
1579 // the last key (fence key) might be dead
1581 memset (bt->frame, 0, bt->page_size);
1586 while( cnt++ < max ) {
1587 key = keyptr(page, cnt);
1588 nxt -= key->len + 1;
1589 memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
1590 memcpy(slotptr(bt->frame,++idx)->id, slotptr(page,cnt)->id, BtId);
1591 if( !(slotptr(bt->frame, idx)->dead = slotptr(page, cnt)->dead) )
1594 slotptr(bt->frame, idx)->tod = slotptr(page, cnt)->tod;
1596 slotptr(bt->frame, idx)->off = nxt;
1599 // remember fence key for new right page
1601 memcpy (rightkey, key, key->len + 1);
1603 bt->frame->bits = bt->page_bits;
1604 bt->frame->min = nxt;
1605 bt->frame->cnt = idx;
1606 bt->frame->lvl = lvl;
1610 if( page_no > ROOT_page )
1611 memcpy (bt->frame->right, page->right, BtId);
1613 // get new free page and write frame to it.
1615 if( !(right = bt_newpage(bt, bt->frame)) )
1618 // update lower keys to continue in old page
1620 memcpy (bt->frame, page, bt->page_size);
1621 memset (page+1, 0, bt->page_size - sizeof(*page));
1622 nxt = bt->page_size;
1628 // assemble page of smaller keys
1629 // (they're all active keys)
1631 while( cnt++ < max / 2 ) {
1632 key = keyptr(bt->frame, cnt);
1633 nxt -= key->len + 1;
1634 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1635 memcpy(slotptr(page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
1637 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1639 slotptr(page, idx)->off = nxt;
1643 // remember fence key for smaller page
1645 memcpy (fencekey, key, key->len + 1);
1647 bt_putid(page->right, right);
1651 // if current page is the root page, split it
1653 if( page_no == ROOT_page )
1654 return bt_splitroot (bt, fencekey, right);
1658 rlatch = bt_pinlatch (bt, right);
1659 bt_lockpage (BtLockParent, rlatch);
1661 // update left (containing) node
1663 if( bt_update(bt, page, latch) )
1666 bt_lockpage (BtLockParent, latch);
1667 bt_unlockpage (BtLockWrite, latch);
1669 // insert new fence for reformulated left block
1671 if( bt_insertkey (bt, fencekey+1, *fencekey, lvl+1, page_no, time(NULL)) )
1674 // switch fence for right block of larger keys to new right page
1676 if( bt_insertkey (bt, rightkey+1, *rightkey, lvl+1, right, time(NULL)) )
1679 bt_unlockpage (BtLockParent, latch);
1680 bt_unlockpage (BtLockParent, rlatch);
1682 bt_unpinlatch (rlatch);
1683 bt_unpinlatch (latch);
1687 // Insert new key into the btree at requested level.
1688 // Pages are unlocked at exit.
1690 BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod)
1697 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1698 ptr = keyptr(bt->page, slot);
1702 bt->err = BTERR_ovflw;
1706 // if key already exists, update id and return
1710 if( !keycmp (ptr, key, len) ) {
1711 if( slotptr(page, slot)->dead )
1713 slotptr(page, slot)->dead = 0;
1715 slotptr(page, slot)->tod = tod;
1717 bt_putid(slotptr(page,slot)->id, id);
1718 if( bt_update(bt, bt->page, bt->latch) )
1720 bt_unlockpage(BtLockWrite, bt->latch);
1721 bt_unpinlatch (bt->latch);
1725 // check if page has enough space
1727 if( slot = bt_cleanpage (bt, len, slot) )
1730 if( bt_splitpage (bt) )
1734 // calculate next available slot and copy key into page
1736 page->min -= len + 1; // reset lowest used offset
1737 ((unsigned char *)page)[page->min] = len;
1738 memcpy ((unsigned char *)page + page->min +1, key, len );
1740 for( idx = slot; idx < page->cnt; idx++ )
1741 if( slotptr(page, idx)->dead )
1744 // now insert key into array before slot
1745 // preserving the fence slot
1747 if( idx == page->cnt )
1753 *slotptr(page, idx) = *slotptr(page, idx -1), idx--;
1755 bt_putid(slotptr(page,slot)->id, id);
1756 slotptr(page, slot)->off = page->min;
1758 slotptr(page, slot)->tod = tod;
1760 slotptr(page, slot)->dead = 0;
1762 if( bt_update(bt, bt->page, bt->latch) )
1765 bt_unlockpage(BtLockWrite, bt->latch);
1766 bt_unpinlatch(bt->latch);
1770 // cache page of keys into cursor and return starting slot for given key
1772 uint bt_startkey (BtDb *bt, unsigned char *key, uint len)
1776 // cache page for retrieval
1778 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1779 memcpy (bt->cursor, bt->page, bt->page_size);
1783 bt_unlockpage(BtLockRead, bt->latch);
1784 bt->cursor_page = bt->page_no;
1785 bt_unpinlatch (bt->latch);
1789 // return next slot for cursor page
1790 // or slide cursor right into next page
1792 uint bt_nextkey (BtDb *bt, uint slot)
1798 right = bt_getid(bt->cursor->right);
1800 while( slot++ < bt->cursor->cnt )
1801 if( slotptr(bt->cursor,slot)->dead )
1803 else if( right || (slot < bt->cursor->cnt))
1811 bt->cursor_page = right;
1812 latch = bt_pinlatch (bt, right);
1813 bt_lockpage(BtLockRead, latch);
1815 bt->page = bt_mappage (bt, latch);
1816 memcpy (bt->cursor, bt->page, bt->page_size);
1817 bt_unlockpage(BtLockRead, latch);
1818 bt_unpinlatch (latch);
1825 BtKey bt_key(BtDb *bt, uint slot)
1827 return keyptr(bt->cursor, slot);
1830 uid bt_uid(BtDb *bt, uint slot)
1832 return bt_getid(slotptr(bt->cursor,slot)->id);
1836 uint bt_tod(BtDb *bt, uint slot)
1838 return slotptr(bt->cursor,slot)->tod;
1844 uint bt_audit (BtDb *bt)
1856 if( *(ushort *)(bt->latchmgr->lock) )
1857 fprintf(stderr, "Alloc page locked\n");
1858 *(ushort *)(bt->latchmgr->lock) = 0;
1860 for( idx = 1; idx <= bt->latchmgr->latchdeployed; idx++ ) {
1861 latch = bt->latchsets + idx;
1862 if( *(ushort *)latch->readwr )
1863 fprintf(stderr, "latchset %d rwlocked for page %.8x\n", idx, latch->page_no);
1864 *(ushort *)latch->readwr = 0;
1866 if( *(ushort *)latch->access )
1867 fprintf(stderr, "latchset %d accesslocked for page %.8x\n", idx, latch->page_no);
1868 *(ushort *)latch->access = 0;
1870 if( *(ushort *)latch->parent )
1871 fprintf(stderr, "latchset %d parentlocked for page %.8x\n", idx, latch->page_no);
1872 *(ushort *)latch->parent = 0;
1875 fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
1878 page = (BtPage)(idx * bt->page_size + bt->latchpool);
1879 off = latch->page_no << bt->page_bits;
1882 if( pwrite(bt->idx, page, bt->page_size, off) < bt->page_size )
1883 fprintf(stderr, "Page %.8x Write Error\n", latch->page_no);
1885 if( latch->dirty ) {
1886 SetFilePointer (bt->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
1888 if( !WriteFile(bt->idx, page, bt->page_size, amt, NULL) )
1889 fprintf(stderr, "Page %.8x Write Error\n", latch->page_no);
1891 if( *amt < bt->page_size )
1892 fprintf(stderr, "Page %.8x Write Error\n", latch->page_no);
1898 for( hashidx = 0; hashidx < bt->latchmgr->latchhash; hashidx++ ) {
1899 if( *(ushort *)(bt->table[hashidx].latch) )
1900 fprintf(stderr, "hash entry %d locked\n", hashidx);
1902 *(ushort *)(bt->table[hashidx].latch) = 0;
1904 if( idx = bt->table[hashidx].slot ) do {
1905 latch = bt->latchsets + idx;
1906 if( *(ushort *)latch->busy )
1907 fprintf(stderr, "latchset %d busylocked for page %.8x\n", idx, latch->page_no);
1908 *(ushort *)latch->busy = 0;
1909 if( latch->hash != hashidx )
1910 fprintf(stderr, "latchset %d wrong hashidx\n", idx);
1912 fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
1913 } while( idx = latch->next );
1916 next = bt->latchmgr->nlatchpage + LATCH_page;
1917 page_no = LEAF_page;
1919 while( page_no < bt_getid(bt->latchmgr->alloc->right) ) {
1920 pread (bt->idx, bt->frame, bt->page_size, page_no << bt->page_bits);
1921 if( !bt->frame->free ) {
1922 for( idx = 0; idx++ < bt->frame->cnt - 1; ) {
1923 ptr = keyptr(bt->frame, idx+1);
1924 if( keycmp (keyptr(bt->frame, idx), ptr->key, ptr->len) >= 0 )
1925 fprintf(stderr, "page %.8x idx %.2x out of order\n", page_no, idx);
1927 if( !bt->frame->lvl )
1928 cnt += bt->frame->act;
1931 if( page_no > LEAF_page )
1942 double getCpuTime(int type)
1945 FILETIME xittime[1];
1946 FILETIME systime[1];
1947 FILETIME usrtime[1];
1948 SYSTEMTIME timeconv[1];
1951 memset (timeconv, 0, sizeof(SYSTEMTIME));
1955 GetSystemTimeAsFileTime (xittime);
1956 FileTimeToSystemTime (xittime, timeconv);
1957 ans = (double)timeconv->wDayOfWeek * 3600 * 24;
1960 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
1961 FileTimeToSystemTime (usrtime, timeconv);
1964 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
1965 FileTimeToSystemTime (systime, timeconv);
1969 ans += (double)timeconv->wHour * 3600;
1970 ans += (double)timeconv->wMinute * 60;
1971 ans += (double)timeconv->wSecond;
1972 ans += (double)timeconv->wMilliseconds / 1000;
1977 #include <sys/resource.h>
1979 double getCpuTime(int type)
1981 struct rusage used[1];
1982 struct timeval tv[1];
1986 gettimeofday(tv, NULL);
1987 return (double)tv->tv_sec + (double)tv->tv_usec / 1000000;
1990 getrusage(RUSAGE_SELF, used);
1991 return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
1994 getrusage(RUSAGE_SELF, used);
1995 return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
2002 // standalone program to index file of keys
2003 // then list them onto std-out
2005 int main (int argc, char **argv)
2007 uint slot, line = 0, off = 0, found = 0;
2008 int ch, cnt = 0, bits = 12, idx;
2009 unsigned char key[256];
2022 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]);
2023 fprintf (stderr, " page_bits: size of btree page in bits\n");
2024 fprintf (stderr, " mapped_pool_pages: number of pages in buffer pool\n");
2028 start = getCpuTime(0);
2032 bits = atoi(argv[4]);
2035 map = atoi(argv[5]);
2038 off = atoi(argv[6]);
2040 bt = bt_open ((argv[1]), BT_rw, bits, map);
2043 fprintf(stderr, "Index Open Error %s\n", argv[1]);
2047 switch(argv[3][0]| 0x20)
2050 fprintf(stderr, "started audit for %s\n", argv[2]);
2051 cnt = bt_audit (bt);
2052 fprintf(stderr, "finished audit for %s, %d keys\n", argv[2], cnt);
2056 fprintf(stderr, "started indexing for %s\n", argv[2]);
2057 if( argc > 2 && (in = fopen (argv[2], "rb")) )
2058 while( ch = getc(in), ch != EOF )
2062 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2064 if( bt_insertkey (bt, key, len, 0, ++line, *tod) )
2065 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2068 else if( len < 245 )
2070 fprintf(stderr, "finished adding keys for %s, %d \n", argv[2], line);
2074 fprintf(stderr, "started deleting keys for %s\n", argv[2]);
2075 if( argc > 2 && (in = fopen (argv[2], "rb")) )
2076 while( ch = getc(in), ch != EOF )
2080 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2082 if( bt_deletekey (bt, key, len, 0) )
2083 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2086 else if( len < 245 )
2088 fprintf(stderr, "finished deleting keys for %s, %d \n", argv[2], line);
2092 fprintf(stderr, "started finding keys for %s\n", argv[2]);
2093 if( argc > 2 && (in = fopen (argv[2], "rb")) )
2094 while( ch = getc(in), ch != EOF )
2098 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2100 if( bt_findkey (bt, key, len) )
2103 fprintf(stderr, "Error %d Syserr %d Line: %d\n", bt->err, errno, line), exit(0);
2106 else if( len < 245 )
2108 fprintf(stderr, "finished search of %d keys for %s, found %d\n", line, argv[2], found);
2115 fprintf(stderr, "started counting\n");
2118 next = bt->latchmgr->nlatchpage + LATCH_page;
2119 page_no = LEAF_page;
2121 while( page_no < bt_getid(bt->latchmgr->alloc->right) ) {
2122 uid off = page_no << bt->page_bits;
2124 pread (bt->idx, bt->frame, bt->page_size, off);
2128 SetFilePointer (bt->idx, (long)off, NULL, FILE_BEGIN);
2130 if( !ReadFile(bt->idx, bt->frame, bt->page_size, amt, NULL))
2131 fprintf (stderr, "unable to read page %.8x", page_no);
2133 if( *amt < bt->page_size )
2134 fprintf (stderr, "unable to read page %.8x", page_no);
2136 if( !bt->frame->free && !bt->frame->lvl )
2137 cnt += bt->frame->act;
2138 if( page_no > LEAF_page )
2143 cnt--; // remove stopper key
2144 fprintf(stderr, " Total keys read %d\n", cnt);
2148 done = getCpuTime(0);
2149 elapsed = (float)(done - start);
2150 fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2151 elapsed = getCpuTime(1);
2152 fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2153 elapsed = getCpuTime(2);
2154 fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);