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
52 typedef unsigned long long uid;
55 typedef unsigned long long off64_t;
56 typedef unsigned short ushort;
57 typedef unsigned int uint;
60 #define BT_ro 0x6f72 // ro
61 #define BT_rw 0x7772 // rw
62 #define BT_fl 0x6c66 // fl
64 #define BT_maxbits 15 // maximum page size in bits
65 #define BT_minbits 12 // minimum page size in bits
66 #define BT_minpage (1 << BT_minbits) // minimum page size
67 #define BT_maxpage (1 << BT_maxbits) // maximum page size
70 There are five lock types for each node in three independent sets:
71 1. (set 1) AccessIntent: Sharable. Going to Read the node. Incompatible with NodeDelete.
72 2. (set 1) NodeDelete: Exclusive. About to release the node. Incompatible with AccessIntent.
73 3. (set 2) ReadLock: Sharable. Read the node. Incompatible with WriteLock.
74 4. (set 2) WriteLock: Exclusive. Modify the node. Incompatible with ReadLock and other WriteLocks.
75 5. (set 3) ParentModification: Exclusive. Change the node's parent keys. Incompatible with another ParentModification.
86 // definition for latch implementation
88 // exclusive is set for write access
89 // share is count of read accessors
90 // grant write lock when share == 0
92 volatile typedef struct {
93 unsigned char mutex[1];
94 unsigned char exclusive:1;
95 unsigned char pending:1;
99 // Define the length of the page and key pointers
103 // Page key slot definition.
105 // If BT_maxbits is 15 or less, you can save 2 bytes
106 // for each key stored by making the first two uints
107 // into ushorts. You can also save 4 bytes by removing
108 // the tod field from the key.
110 // Keys are marked dead, but remain on the page until
111 // cleanup is called. The fence key (highest key) for
112 // the page is always present, even if dead.
116 uint tod; // time-stamp for key
118 ushort off:BT_maxbits; // page offset for key start
119 ushort dead:1; // set for deleted key
120 unsigned char id[BtId]; // id associated with key
123 // The key structure occupies space at the upper end of
124 // each page. It's a length byte followed by the value
129 unsigned char key[0];
132 // The first part of an index page.
133 // It is immediately followed
134 // by the BtSlot array of keys.
136 typedef struct BtPage_ {
137 uint cnt; // count of keys in page
138 uint act; // count of active keys
139 uint min; // next key offset
140 unsigned char bits:6; // page size in bits
141 unsigned char free:1; // page is on free list
142 unsigned char dirty:1; // page is dirty in cache
143 unsigned char lvl:6; // level of page
144 unsigned char kill:1; // page is being deleted
145 unsigned char clean:1; // page needs cleaning
146 unsigned char right[BtId]; // page number to right
150 struct BtPage_ alloc[2]; // next & free page_nos in right ptr
151 BtSpinLatch lock[1]; // allocation area lite latch
152 uint latchdeployed; // highest number of latch entries deployed
153 uint nlatchpage; // number of latch pages at BT_latch
154 uint latchtotal; // number of page latch entries
155 uint latchhash; // number of latch hash table slots
156 uint latchvictim; // next latch hash entry to examine
159 // latch hash table entries
162 volatile uint slot; // Latch table entry at head of collision chain
163 BtSpinLatch latch[1]; // lock for the collision chain
166 // latch manager table structure
169 volatile uid page_no; // latch set page number on disk
170 BtSpinLatch readwr[1]; // read/write page lock
171 BtSpinLatch access[1]; // Access Intent/Page delete
172 BtSpinLatch parent[1]; // Posting of fence key in parent
173 volatile ushort clock; // accessed since last clock pass
174 volatile uint next; // next entry in hash table chain
175 volatile uint prev; // prev entry in hash table chain
176 volatile uint pin; // number of outstanding pins
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 cursor; // cached frame for start/next (never mapped)
189 BtPage frame; // spare frame for the page split (never mapped)
190 BtPage page; // current mapped page in buffer pool
191 BtLatchSet *latch; // current page latch
192 BtLatchMgr *latchmgr; // mapped latch page from allocation page
193 BtLatchSet *latchsets; // mapped latch set from latch pages
194 unsigned char *pagepool; // cached page pool set
195 BtHashEntry *table; // the hash table
200 HANDLE halloc; // allocation and latch table handle
202 unsigned char *mem; // frame, cursor, memory buffers
203 uint found; // last deletekey found key
221 extern void bt_close (BtDb *bt);
222 extern BtDb *bt_open (char *name, uint mode, uint bits, uint cacheblk);
223 extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod);
224 extern BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl);
225 extern uid bt_findkey (BtDb *bt, unsigned char *key, uint len);
226 extern uint bt_startkey (BtDb *bt, unsigned char *key, uint len);
227 extern uint bt_nextkey (BtDb *bt, uint slot);
229 // internal functions
230 void bt_update (BtDb *bt, BtPage page);
231 BtPage bt_mappage (BtDb *bt, BtLatchSet *latch);
232 // Helper functions to return slot values
234 extern BtKey bt_key (BtDb *bt, uint slot);
235 extern uid bt_uid (BtDb *bt, uint slot);
237 extern uint bt_tod (BtDb *bt, uint slot);
240 // BTree page number constants
246 // Number of levels to create in a new BTree
250 // The page is allocated from low and hi ends.
251 // The key offsets and row-id's are allocated
252 // from the bottom, while the text of the key
253 // is allocated from the top. When the two
254 // areas meet, the page is split into two.
256 // A key consists of a length byte, two bytes of
257 // index number (0 - 65534), and up to 253 bytes
258 // of key value. Duplicate keys are discarded.
259 // Associated with each key is a 48 bit row-id.
261 // The b-tree root is always located at page 1.
262 // The first leaf page of level zero is always
263 // located on page 2.
265 // The b-tree pages are linked with right
266 // pointers to facilitate enumerators,
267 // and provide for concurrency.
269 // When to root page fills, it is split in two and
270 // the tree height is raised by a new root at page
271 // one with two keys.
273 // Deleted keys are marked with a dead bit until
274 // page cleanup The fence key for a node is always
275 // present, even after deletion and cleanup.
277 // Deleted leaf pages are reclaimed on a free list.
278 // The upper levels of the btree are fixed on creation.
280 // To achieve maximum concurrency one page is locked at a time
281 // as the tree is traversed to find leaf key in question. The right
282 // page numbers are used in cases where the page is being split,
285 // Page 0 (ALLOC page) is dedicated to lock for new page extensions,
286 // and chains empty leaf pages together for reuse.
288 // Parent locks are obtained to prevent resplitting or deleting a node
289 // before its fence is posted into its upper level.
291 // A special open mode of BT_fl is provided to safely access files on
292 // WIN32 networks. WIN32 network operations should not use memory mapping.
293 // This WIN32 mode sets FILE_FLAG_NOBUFFERING and FILE_FLAG_WRITETHROUGH
294 // to prevent local caching of network file contents.
296 // Access macros to address slot and key values from the page.
297 // Page slots use 1 based indexing.
299 #define slotptr(page, slot) (((BtSlot *)(page+1)) + (slot-1))
300 #define keyptr(page, slot) ((BtKey)((unsigned char*)(page) + slotptr(page, slot)->off))
302 void bt_putid(unsigned char *dest, uid id)
307 dest[i] = (unsigned char)id, id >>= 8;
310 uid bt_getid(unsigned char *src)
315 for( i = 0; i < BtId; i++ )
316 id <<= 8, id |= *src++;
321 BTERR bt_abort (BtDb *bt, BtPage page, uid page_no, BTERR err)
325 fprintf(stderr, "\n Btree2 abort, error %d on page %.8x\n", err, page_no);
326 fprintf(stderr, "level=%d kill=%d free=%d cnt=%x act=%x\n", page->lvl, page->kill, page->free, page->cnt, page->act);
327 ptr = keyptr(page, page->cnt);
328 fprintf(stderr, "fence='%.*s'\n", ptr->len, ptr->key);
329 fprintf(stderr, "right=%.8x\n", bt_getid(page->right));
330 return bt->err = err;
335 // wait until write lock mode is clear
336 // and add 1 to the share count
338 void bt_spinreadlock(BtSpinLatch *latch)
343 // obtain latch mutex
345 if( __sync_lock_test_and_set(latch->mutex, 1) )
348 if( _InterlockedExchange8(latch->mutex, 1) )
351 // see if exclusive request is granted or pending
353 if( prev = !(latch->exclusive | latch->pending) )
359 _InterlockedExchange8(latch->mutex, 0);
366 } while( sched_yield(), 1 );
368 } while( SwitchToThread(), 1 );
372 // wait for other read and write latches to relinquish
374 void bt_spinwritelock(BtSpinLatch *latch)
380 if( __sync_lock_test_and_set(latch->mutex, 1) )
383 if( _InterlockedExchange8(latch->mutex, 1) )
386 if( prev = !(latch->share | latch->exclusive) )
387 latch->exclusive = 1, latch->pending = 0;
393 _InterlockedExchange8(latch->mutex, 0);
398 } while( sched_yield(), 1 );
400 } while( SwitchToThread(), 1 );
404 // try to obtain write lock
406 // return 1 if obtained,
409 int bt_spinwritetry(BtSpinLatch *latch)
414 if( __sync_lock_test_and_set(latch->mutex, 1) )
417 if( _InterlockedExchange8(latch->mutex, 1) )
420 // take write access if all bits are clear
422 if( prev = !(latch->exclusive | latch->share) )
423 latch->exclusive = 1;
428 _InterlockedExchange8(latch->mutex, 0);
435 void bt_spinreleasewrite(BtSpinLatch *latch)
438 while( __sync_lock_test_and_set(latch->mutex, 1) )
441 while( _InterlockedExchange8(latch->mutex, 1) )
444 latch->exclusive = 0;
448 _InterlockedExchange8(latch->mutex, 0);
452 // decrement reader count
454 void bt_spinreleaseread(BtSpinLatch *latch)
457 while( __sync_lock_test_and_set(latch->mutex, 1) )
460 while( _InterlockedExchange8(latch->mutex, 1) )
467 _InterlockedExchange8(latch->mutex, 0);
471 // read page from permanent location in Btree file
473 BTERR bt_readpage (BtDb *bt, BtPage page, uid page_no)
475 off64_t off = page_no << bt->page_bits;
478 if( pread (bt->idx, page, bt->page_size, page_no << bt->page_bits) < bt->page_size ) {
479 fprintf (stderr, "Unable to read page %.8x errno = %d\n", page_no, errno);
480 return bt->err = BTERR_read;
486 memset (ovl, 0, sizeof(OVERLAPPED));
488 ovl->OffsetHigh = off >> 32;
490 if( !ReadFile(bt->idx, page, bt->page_size, amt, ovl)) {
491 fprintf (stderr, "Unable to read page %.8x GetLastError = %d\n", page_no, GetLastError());
492 return bt->err = BTERR_read;
494 if( *amt < bt->page_size ) {
495 fprintf (stderr, "Unable to read page %.8x GetLastError = %d\n", page_no, GetLastError());
496 return bt->err = BTERR_read;
502 // write page to permanent location in Btree file
503 // clear the dirty bit
505 BTERR bt_writepage (BtDb *bt, BtPage page, uid page_no)
507 off64_t off = page_no << bt->page_bits;
512 if( pwrite(bt->idx, page, bt->page_size, off) < bt->page_size )
513 return bt->err = BTERR_wrt;
518 memset (ovl, 0, sizeof(OVERLAPPED));
520 ovl->OffsetHigh = off >> 32;
523 if( !WriteFile(bt->idx, page, bt->page_size, amt, ovl) )
524 return bt->err = BTERR_wrt;
526 if( *amt < bt->page_size )
527 return bt->err = BTERR_wrt;
532 // link latch table entry into head of latch hash table
534 BTERR bt_latchlink (BtDb *bt, uint hashidx, uint slot, uid page_no)
536 BtPage page = (BtPage)((uid)slot * bt->page_size + bt->pagepool);
537 BtLatchSet *latch = bt->latchsets + slot;
539 if( latch->next = bt->table[hashidx].slot )
540 bt->latchsets[latch->next].prev = slot;
542 bt->table[hashidx].slot = slot;
543 latch->page_no = page_no;
548 return bt_readpage (bt, page, page_no);
553 void bt_unpinlatch (BtLatchSet *latch)
556 __sync_fetch_and_add(&latch->pin, -1);
558 _InterlockedDecrement (&latch->pin);
562 // find existing latchset or inspire new one
563 // return with latchset pinned
565 BtLatchSet *bt_pinlatch (BtDb *bt, uid page_no)
567 uint hashidx = page_no % bt->latchmgr->latchhash;
574 // try to find our entry
576 bt_spinwritelock(bt->table[hashidx].latch);
578 if( slot = bt->table[hashidx].slot ) do
580 latch = bt->latchsets + slot;
581 if( page_no == latch->page_no )
583 } while( slot = latch->next );
588 latch = bt->latchsets + slot;
591 bt_spinreleasewrite(bt->table[hashidx].latch);
595 // see if there are any unused pool entries
597 slot = __sync_fetch_and_add (&bt->latchmgr->latchdeployed, 1) + 1;
599 slot = _InterlockedIncrement (&bt->latchmgr->latchdeployed);
602 if( slot < bt->latchmgr->latchtotal ) {
603 latch = bt->latchsets + slot;
604 if( bt_latchlink (bt, hashidx, slot, page_no) )
606 bt_spinreleasewrite (bt->table[hashidx].latch);
611 __sync_fetch_and_add (&bt->latchmgr->latchdeployed, -1);
613 _InterlockedDecrement (&bt->latchmgr->latchdeployed);
615 // find and reuse previous entry on victim
619 slot = __sync_fetch_and_add(&bt->latchmgr->latchvictim, 1);
621 slot = _InterlockedIncrement (&bt->latchmgr->latchvictim) - 1;
623 // try to get write lock on hash chain
624 // skip entry if not obtained
625 // or has outstanding pins
627 slot %= bt->latchmgr->latchtotal;
628 latch = bt->latchsets + slot;
630 idx = latch->page_no % bt->latchmgr->latchhash;
635 if( !bt_spinwritetry (bt->table[idx].latch) )
640 bt_spinreleasewrite (bt->table[idx].latch);
644 // update permanent page area in btree
646 page = (BtPage)((uid)slot * bt->page_size + bt->pagepool);
648 posix_fadvise (bt->idx, page_no << bt->page_bits, bt->page_size, POSIX_FADV_WILLNEED);
651 if( bt_writepage (bt, page, latch->page_no) )
654 // unlink our available slot from its hash chain
657 bt->latchsets[latch->prev].next = latch->next;
659 bt->table[idx].slot = latch->next;
662 bt->latchsets[latch->next].prev = latch->prev;
664 bt_spinreleasewrite (bt->table[idx].latch);
666 if( bt_latchlink (bt, hashidx, slot, page_no) )
669 bt_spinreleasewrite (bt->table[hashidx].latch);
674 // close and release memory
676 void bt_close (BtDb *bt)
679 munmap (bt->table, bt->latchmgr->nlatchpage * bt->page_size);
680 munmap (bt->latchmgr, bt->page_size);
682 FlushViewOfFile(bt->latchmgr, 0);
683 UnmapViewOfFile(bt->latchmgr);
684 CloseHandle(bt->halloc);
693 VirtualFree (bt->mem, 0, MEM_RELEASE);
694 FlushFileBuffers(bt->idx);
695 CloseHandle(bt->idx);
699 // open/create new btree
701 // call with file_name, BT_openmode, bits in page size (e.g. 16),
702 // size of mapped page pool (e.g. 8192)
704 BtDb *bt_open (char *name, uint mode, uint bits, uint nodemax)
706 uint lvl, attr, last, slot, idx;
707 uint nlatchpage, latchhash;
708 BtLatchMgr *latchmgr;
718 struct flock lock[1];
721 // determine sanity of page size and buffer pool
723 if( bits > BT_maxbits )
725 else if( bits < BT_minbits )
728 if( mode == BT_ro ) {
729 fprintf(stderr, "ReadOnly mode not supported: %s\n", name);
733 bt = calloc (1, sizeof(BtDb));
735 bt->idx = open ((char*)name, O_RDWR | O_CREAT, 0666);
736 posix_fadvise( bt->idx, 0, 0, POSIX_FADV_RANDOM);
738 if( bt->idx == -1 ) {
739 fprintf(stderr, "unable to open %s\n", name);
740 return free(bt), NULL;
743 bt = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, sizeof(BtDb));
744 attr = FILE_ATTRIBUTE_NORMAL;
745 bt->idx = CreateFile(name, GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, attr, NULL);
747 if( bt->idx == INVALID_HANDLE_VALUE ) {
748 fprintf(stderr, "unable to open %s\n", name);
749 return GlobalFree(bt), NULL;
753 memset (lock, 0, sizeof(lock));
754 lock->l_len = sizeof(struct BtPage_);
755 lock->l_type = F_WRLCK;
757 if( fcntl (bt->idx, F_SETLKW, lock) < 0 ) {
758 fprintf(stderr, "unable to lock record zero %s\n", name);
759 return bt_close (bt), NULL;
762 memset (ovl, 0, sizeof(ovl));
764 // use large offsets to
765 // simulate advisory locking
767 ovl->OffsetHigh |= 0x80000000;
769 if( !LockFileEx (bt->idx, LOCKFILE_EXCLUSIVE_LOCK, 0, sizeof(struct BtPage_), 0L, ovl) ) {
770 fprintf(stderr, "unable to lock record zero %s, GetLastError = %d\n", name, GetLastError());
771 return bt_close (bt), NULL;
776 latchmgr = valloc (BT_maxpage);
779 // read minimum page size to get root info
781 if( size = lseek (bt->idx, 0L, 2) ) {
782 if( pread(bt->idx, latchmgr, BT_minpage, 0) == BT_minpage )
783 bits = latchmgr->alloc->bits;
785 fprintf(stderr, "Unable to read page zero\n");
786 return free(bt), free(latchmgr), NULL;
790 latchmgr = VirtualAlloc(NULL, BT_maxpage, MEM_COMMIT, PAGE_READWRITE);
791 size = GetFileSize(bt->idx, amt);
794 if( !ReadFile(bt->idx, (char *)latchmgr, BT_minpage, amt, NULL) ) {
795 fprintf(stderr, "Unable to read page zero\n");
796 return bt_close (bt), NULL;
798 bits = latchmgr->alloc->bits;
802 bt->page_size = 1 << bits;
803 bt->page_bits = bits;
808 nlatchpage = latchmgr->nlatchpage;
813 fprintf(stderr, "Buffer pool too small: %d\n", nodemax);
814 return bt_close(bt), NULL;
817 // initialize an empty b-tree with latch page, root page, page of leaves
818 // and page(s) of latches and page pool cache
820 memset (latchmgr, 0, 1 << bits);
821 latchmgr->alloc->bits = bt->page_bits;
823 // calculate number of latch hash table entries
825 nlatchpage = (nodemax/16 * sizeof(BtHashEntry) + bt->page_size - 1) / bt->page_size;
826 latchhash = nlatchpage * bt->page_size / sizeof(BtHashEntry);
828 nlatchpage += nodemax; // size of the buffer pool in pages
829 nlatchpage += (sizeof(BtLatchSet) * nodemax + bt->page_size - 1)/bt->page_size;
831 bt_putid(latchmgr->alloc->right, MIN_lvl+1+nlatchpage);
832 latchmgr->nlatchpage = nlatchpage;
833 latchmgr->latchtotal = nodemax;
834 latchmgr->latchhash = latchhash;
836 if( bt_writepage (bt, latchmgr->alloc, 0) ) {
837 fprintf (stderr, "Unable to create btree page zero\n");
838 return bt_close (bt), NULL;
841 memset (latchmgr, 0, 1 << bits);
842 latchmgr->alloc->bits = bt->page_bits;
844 for( lvl=MIN_lvl; lvl--; ) {
845 last = MIN_lvl - lvl; // page number
846 slotptr(latchmgr->alloc, 1)->off = bt->page_size - 3;
847 bt_putid(slotptr(latchmgr->alloc, 1)->id, lvl ? last + 1 : 0);
848 key = keyptr(latchmgr->alloc, 1);
849 key->len = 2; // create stopper key
853 latchmgr->alloc->min = bt->page_size - 3;
854 latchmgr->alloc->lvl = lvl;
855 latchmgr->alloc->cnt = 1;
856 latchmgr->alloc->act = 1;
858 if( bt_writepage (bt, latchmgr->alloc, last) ) {
859 fprintf (stderr, "Unable to create btree page %.8x\n", last);
860 return bt_close (bt), NULL;
864 // clear out buffer pool pages
866 memset(latchmgr, 0, bt->page_size);
867 last = MIN_lvl + nlatchpage;
869 if( bt_writepage (bt, latchmgr->alloc, last) ) {
870 fprintf (stderr, "Unable to write buffer pool page %.8x\n", last);
871 return bt_close (bt), NULL;
877 VirtualFree (latchmgr, 0, MEM_RELEASE);
882 lock->l_type = F_UNLCK;
883 if( fcntl (bt->idx, F_SETLK, lock) < 0 ) {
884 fprintf (stderr, "Unable to unlock page zero\n");
885 return bt_close (bt), NULL;
888 if( !UnlockFileEx (bt->idx, 0, sizeof(struct BtPage_), 0, ovl) ) {
889 fprintf (stderr, "Unable to unlock page zero, GetLastError = %d\n", GetLastError());
890 return bt_close (bt), NULL;
894 flag = PROT_READ | PROT_WRITE;
895 bt->latchmgr = mmap (0, bt->page_size, flag, MAP_SHARED, bt->idx, ALLOC_page * bt->page_size);
896 if( bt->latchmgr == MAP_FAILED ) {
897 fprintf (stderr, "Unable to mmap page zero, errno = %d", errno);
898 return bt_close (bt), NULL;
900 bt->table = (void *)mmap (0, (uid)nlatchpage * bt->page_size, flag, MAP_SHARED, bt->idx, LATCH_page * bt->page_size);
901 if( bt->table == MAP_FAILED ) {
902 fprintf (stderr, "Unable to mmap buffer pool, errno = %d", errno);
903 return bt_close (bt), NULL;
905 madvise (bt->table, (uid)nlatchpage << bt->page_bits, MADV_RANDOM | MADV_WILLNEED);
907 flag = PAGE_READWRITE;
908 bt->halloc = CreateFileMapping(bt->idx, NULL, flag, 0, ((uid)nlatchpage + LATCH_page) * bt->page_size, NULL);
910 fprintf (stderr, "Unable to create file mapping for buffer pool mgr, GetLastError = %d\n", GetLastError());
911 return bt_close (bt), NULL;
914 flag = FILE_MAP_WRITE;
915 bt->latchmgr = MapViewOfFile(bt->halloc, flag, 0, 0, ((uid)nlatchpage + LATCH_page) * bt->page_size);
916 if( !bt->latchmgr ) {
917 fprintf (stderr, "Unable to map buffer pool, GetLastError = %d\n", GetLastError());
918 return bt_close (bt), NULL;
921 bt->table = (void *)((char *)bt->latchmgr + LATCH_page * bt->page_size);
923 bt->pagepool = (unsigned char *)bt->table + (uid)(nlatchpage - bt->latchmgr->latchtotal) * bt->page_size;
924 bt->latchsets = (BtLatchSet *)(bt->pagepool - (uid)bt->latchmgr->latchtotal * sizeof(BtLatchSet));
927 bt->mem = valloc (2 * bt->page_size);
929 bt->mem = VirtualAlloc(NULL, 2 * bt->page_size, MEM_COMMIT, PAGE_READWRITE);
931 bt->frame = (BtPage)bt->mem;
932 bt->cursor = (BtPage)(bt->mem + bt->page_size);
936 // place write, read, or parent lock on requested page_no.
938 void bt_lockpage(BtLock mode, BtLatchSet *latch)
942 bt_spinreadlock (latch->readwr);
945 bt_spinwritelock (latch->readwr);
948 bt_spinreadlock (latch->access);
951 bt_spinwritelock (latch->access);
954 bt_spinwritelock (latch->parent);
959 // remove write, read, or parent lock on requested page
961 void bt_unlockpage(BtLock mode, BtLatchSet *latch)
965 bt_spinreleaseread (latch->readwr);
968 bt_spinreleasewrite (latch->readwr);
971 bt_spinreleaseread (latch->access);
974 bt_spinreleasewrite (latch->access);
977 bt_spinreleasewrite (latch->parent);
982 // allocate a new page and write page into it
984 uid bt_newpage(BtDb *bt, BtPage page)
990 // lock allocation page
992 bt_spinwritelock(bt->latchmgr->lock);
994 // use empty chain first
995 // else allocate empty page
997 if( new_page = bt_getid(bt->latchmgr->alloc[1].right) ) {
998 if( latch = bt_pinlatch (bt, new_page) )
999 temp = bt_mappage (bt, latch);
1003 bt_putid(bt->latchmgr->alloc[1].right, bt_getid(temp->right));
1004 bt_spinreleasewrite(bt->latchmgr->lock);
1005 memcpy (temp, page, bt->page_size);
1007 bt_update (bt, temp);
1008 bt_unpinlatch (latch);
1011 new_page = bt_getid(bt->latchmgr->alloc->right);
1012 bt_putid(bt->latchmgr->alloc->right, new_page+1);
1013 bt_spinreleasewrite(bt->latchmgr->lock);
1015 if( bt_writepage (bt, page, new_page) )
1019 bt_update (bt, bt->latchmgr->alloc);
1023 // compare two keys, returning > 0, = 0, or < 0
1024 // as the comparison value
1026 int keycmp (BtKey key1, unsigned char *key2, uint len2)
1028 uint len1 = key1->len;
1031 if( ans = memcmp (key1->key, key2, len1 > len2 ? len2 : len1) )
1042 // Update current page of btree by
1043 // flushing mapped area to disk backing of cache pool.
1044 // mark page as dirty for rewrite to permanent location
1046 void bt_update (BtDb *bt, BtPage page)
1049 // msync (page, bt->page_size, MS_ASYNC);
1051 // FlushViewOfFile (page, bt->page_size);
1056 // map the btree cached page onto current page
1058 BtPage bt_mappage (BtDb *bt, BtLatchSet *latch)
1060 return (BtPage)((uid)(latch - bt->latchsets) * bt->page_size + bt->pagepool);
1063 // deallocate a deleted page
1064 // place on free chain out of allocator page
1065 // call with page latched for Writing and Deleting
1067 BTERR bt_freepage(BtDb *bt, uid page_no, BtLatchSet *latch)
1069 BtPage page = bt_mappage (bt, latch);
1071 // lock allocation page
1073 bt_spinwritelock (bt->latchmgr->lock);
1075 // store chain in second right
1076 bt_putid(page->right, bt_getid(bt->latchmgr->alloc[1].right));
1077 bt_putid(bt->latchmgr->alloc[1].right, page_no);
1080 bt_update(bt, page);
1082 // unlock released page
1084 bt_unlockpage (BtLockDelete, latch);
1085 bt_unlockpage (BtLockWrite, latch);
1086 bt_unpinlatch (latch);
1088 // unlock allocation page
1090 bt_spinreleasewrite (bt->latchmgr->lock);
1091 bt_update (bt, bt->latchmgr->alloc);
1095 // find slot in page for given key at a given level
1097 int bt_findslot (BtDb *bt, unsigned char *key, uint len)
1099 uint diff, higher = bt->page->cnt, low = 1, slot;
1102 // make stopper key an infinite fence value
1104 if( bt_getid (bt->page->right) )
1109 // low is the lowest candidate, higher is already
1110 // tested as .ge. the given key, loop ends when they meet
1112 while( diff = higher - low ) {
1113 slot = low + ( diff >> 1 );
1114 if( keycmp (keyptr(bt->page, slot), key, len) < 0 )
1117 higher = slot, good++;
1120 // return zero if key is on right link page
1122 return good ? higher : 0;
1125 // find and load page at given level for given key
1126 // leave page rd or wr locked as requested
1128 int bt_loadpage (BtDb *bt, unsigned char *key, uint len, uint lvl, uint lock)
1130 uid page_no = ROOT_page, prevpage = 0;
1131 uint drill = 0xff, slot;
1132 BtLatchSet *prevlatch;
1133 uint mode, prevmode;
1135 // start at root of btree and drill down
1138 // determine lock mode of drill level
1139 mode = (lock == BtLockWrite) && (drill == lvl) ? BtLockWrite : BtLockRead;
1141 if( bt->latch = bt_pinlatch(bt, page_no) )
1142 bt->page_no = page_no;
1146 // obtain access lock using lock chaining
1148 if( page_no > ROOT_page )
1149 bt_lockpage(BtLockAccess, bt->latch);
1152 bt_unlockpage(prevmode, prevlatch);
1153 bt_unpinlatch(prevlatch);
1157 // obtain read lock using lock chaining
1159 bt_lockpage(mode, bt->latch);
1161 if( page_no > ROOT_page )
1162 bt_unlockpage(BtLockAccess, bt->latch);
1164 // map/obtain page contents
1166 bt->page = bt_mappage (bt, bt->latch);
1168 // re-read and re-lock root after determining actual level of root
1170 if( bt->page->lvl != drill) {
1171 if( bt->page_no != ROOT_page )
1172 return bt->err = BTERR_struct, 0;
1174 drill = bt->page->lvl;
1176 if( lock != BtLockRead && drill == lvl ) {
1177 bt_unlockpage(mode, bt->latch);
1178 bt_unpinlatch(bt->latch);
1183 prevpage = bt->page_no;
1184 prevlatch = bt->latch;
1187 // find key on page at this level
1188 // and descend to requested level
1190 if( !bt->page->kill )
1191 if( slot = bt_findslot (bt, key, len) ) {
1195 while( slotptr(bt->page, slot)->dead )
1196 if( slot++ < bt->page->cnt )
1201 page_no = bt_getid(slotptr(bt->page, slot)->id);
1206 // or slide right into next page
1209 page_no = bt_getid(bt->page->right);
1213 // return error on end of right chain
1215 bt->err = BTERR_eof;
1216 return 0; // return error
1219 // a fence key was deleted from a page
1220 // push new fence value upwards
1222 BTERR bt_fixfence (BtDb *bt, uid page_no, uint lvl)
1224 unsigned char leftkey[256], rightkey[256];
1225 BtLatchSet *latch = bt->latch;
1228 // remove deleted key, the old fence value
1230 ptr = keyptr(bt->page, bt->page->cnt);
1231 memcpy(rightkey, ptr, ptr->len + 1);
1233 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1234 bt->page->clean = 1;
1236 ptr = keyptr(bt->page, bt->page->cnt);
1237 memcpy(leftkey, ptr, ptr->len + 1);
1239 bt_update (bt, bt->page);
1240 bt_lockpage (BtLockParent, latch);
1241 bt_unlockpage (BtLockWrite, latch);
1243 // insert new (now smaller) fence key
1245 if( bt_insertkey (bt, leftkey+1, *leftkey, lvl + 1, page_no, time(NULL)) )
1248 // remove old (larger) fence key
1250 if( bt_deletekey (bt, rightkey+1, *rightkey, lvl + 1) )
1253 bt_unlockpage (BtLockParent, latch);
1254 bt_unpinlatch (latch);
1258 // root has a single child
1259 // collapse a level from the btree
1260 // call with root locked in bt->page
1262 BTERR bt_collapseroot (BtDb *bt, BtPage root)
1269 // find the child entry
1270 // and promote to new root
1273 for( idx = 0; idx++ < root->cnt; )
1274 if( !slotptr(root, idx)->dead )
1277 child = bt_getid (slotptr(root, idx)->id);
1278 if( latch = bt_pinlatch (bt, child) )
1279 temp = bt_mappage (bt, latch);
1283 bt_lockpage (BtLockDelete, latch);
1284 bt_lockpage (BtLockWrite, latch);
1285 memcpy (root, temp, bt->page_size);
1287 bt_update (bt, root);
1289 if( bt_freepage (bt, child, latch) )
1292 } while( root->lvl > 1 && root->act == 1 );
1294 bt_unlockpage (BtLockWrite, bt->latch);
1295 bt_unpinlatch (bt->latch);
1299 // find and delete key on page by marking delete flag bit
1300 // when page becomes empty, delete it
1302 BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl)
1304 unsigned char lowerkey[256], higherkey[256];
1305 uint slot, dirty = 0, idx, fence, found;
1306 BtLatchSet *latch, *rlatch;
1311 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1312 ptr = keyptr(bt->page, slot);
1316 // are we deleting a fence slot?
1318 fence = slot == bt->page->cnt;
1320 // if key is found delete it, otherwise ignore request
1322 if( found = !keycmp (ptr, key, len) )
1323 if( found = slotptr(bt->page, slot)->dead == 0 ) {
1324 dirty = slotptr(bt->page,slot)->dead = 1;
1325 bt->page->clean = 1;
1328 // collapse empty slots
1330 while( idx = bt->page->cnt - 1 )
1331 if( slotptr(bt->page, idx)->dead ) {
1332 *slotptr(bt->page, idx) = *slotptr(bt->page, idx + 1);
1333 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1338 right = bt_getid(bt->page->right);
1339 page_no = bt->page_no;
1344 return bt_abort (bt, bt->page, page_no, BTERR_notfound);
1345 bt_unlockpage(BtLockWrite, latch);
1346 bt_unpinlatch (latch);
1347 return bt->found = found, 0;
1350 // did we delete a fence key in an upper level?
1352 if( lvl && bt->page->act && fence )
1353 if( bt_fixfence (bt, page_no, lvl) )
1356 return bt->found = found, 0;
1358 // is this a collapsed root?
1360 if( lvl > 1 && page_no == ROOT_page && bt->page->act == 1 )
1361 if( bt_collapseroot (bt, bt->page) )
1364 return bt->found = found, 0;
1366 // return if page is not empty
1368 if( bt->page->act ) {
1369 bt_update(bt, bt->page);
1370 bt_unlockpage(BtLockWrite, latch);
1371 bt_unpinlatch (latch);
1372 return bt->found = found, 0;
1375 // cache copy of fence key
1376 // in order to find parent
1378 ptr = keyptr(bt->page, bt->page->cnt);
1379 memcpy(lowerkey, ptr, ptr->len + 1);
1381 // obtain lock on right page
1383 if( rlatch = bt_pinlatch (bt, right) )
1384 temp = bt_mappage (bt, rlatch);
1388 bt_lockpage(BtLockWrite, rlatch);
1391 bt_abort(bt, temp, right, 0);
1392 return bt_abort(bt, bt->page, bt->page_no, BTERR_kill);
1395 // pull contents of next page into current empty page
1397 memcpy (bt->page, temp, bt->page_size);
1399 // cache copy of key to update
1401 ptr = keyptr(temp, temp->cnt);
1402 memcpy(higherkey, ptr, ptr->len + 1);
1404 // Mark right page as deleted and point it to left page
1405 // until we can post updates at higher level.
1407 bt_putid(temp->right, page_no);
1410 bt_update(bt, bt->page);
1411 bt_update(bt, temp);
1413 bt_lockpage(BtLockParent, latch);
1414 bt_unlockpage(BtLockWrite, latch);
1416 bt_lockpage(BtLockParent, rlatch);
1417 bt_unlockpage(BtLockWrite, rlatch);
1419 // redirect higher key directly to consolidated node
1421 if( bt_insertkey (bt, higherkey+1, *higherkey, lvl+1, page_no, time(NULL)) )
1424 // delete old lower key to consolidated node
1426 if( bt_deletekey (bt, lowerkey + 1, *lowerkey, lvl + 1) )
1429 // obtain write & delete lock on deleted node
1430 // add right block to free chain
1432 bt_lockpage(BtLockDelete, rlatch);
1433 bt_lockpage(BtLockWrite, rlatch);
1434 bt_unlockpage(BtLockParent, rlatch);
1436 if( bt_freepage (bt, right, rlatch) )
1439 bt_unlockpage(BtLockParent, latch);
1440 bt_unpinlatch(latch);
1444 // find key in leaf level and return row-id
1446 uid bt_findkey (BtDb *bt, unsigned char *key, uint len)
1452 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1453 ptr = keyptr(bt->page, slot);
1457 // if key exists, return row-id
1458 // otherwise return 0
1460 if( ptr->len == len && !memcmp (ptr->key, key, len) )
1461 id = bt_getid(slotptr(bt->page,slot)->id);
1465 bt_unlockpage (BtLockRead, bt->latch);
1466 bt_unpinlatch (bt->latch);
1470 // check page for space available,
1471 // clean if necessary and return
1472 // 0 - page needs splitting
1473 // >0 - go ahead with new slot
1475 uint bt_cleanpage(BtDb *bt, uint amt, uint slot)
1477 uint nxt = bt->page_size;
1478 BtPage page = bt->page;
1479 uint cnt = 0, idx = 0;
1480 uint max = page->cnt;
1481 uint newslot = slot;
1485 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1488 // skip cleanup if nothing to reclaim
1493 memcpy (bt->frame, page, bt->page_size);
1495 // skip page info and set rest of page to zero
1497 memset (page+1, 0, bt->page_size - sizeof(*page));
1500 while( cnt++ < max ) {
1503 // always leave fence key in list
1504 if( cnt < max && slotptr(bt->frame,cnt)->dead )
1508 key = keyptr(bt->frame, cnt);
1509 nxt -= key->len + 1;
1510 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1513 memcpy(slotptr(page, ++idx)->id, slotptr(bt->frame, cnt)->id, BtId);
1514 if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
1517 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1519 slotptr(page, idx)->off = nxt;
1525 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1531 // split the root and raise the height of the btree
1533 BTERR bt_splitroot(BtDb *bt, unsigned char *leftkey, uid page_no2)
1535 uint nxt = bt->page_size;
1536 BtPage root = bt->page;
1539 // Obtain an empty page to use, and copy the current
1540 // root contents into it
1542 if( !(right = bt_newpage(bt, root)) )
1545 // preserve the page info at the bottom
1546 // and set rest to zero
1548 memset(root+1, 0, bt->page_size - sizeof(*root));
1550 // insert first key on newroot page
1552 nxt -= *leftkey + 1;
1553 memcpy ((unsigned char *)root + nxt, leftkey, *leftkey + 1);
1554 bt_putid(slotptr(root, 1)->id, right);
1555 slotptr(root, 1)->off = nxt;
1557 // insert second key on newroot page
1558 // and increase the root height
1561 ((unsigned char *)root)[nxt] = 2;
1562 ((unsigned char *)root)[nxt+1] = 0xff;
1563 ((unsigned char *)root)[nxt+2] = 0xff;
1564 bt_putid(slotptr(root, 2)->id, page_no2);
1565 slotptr(root, 2)->off = nxt;
1567 bt_putid(root->right, 0);
1568 root->min = nxt; // reset lowest used offset and key count
1573 // update and release root (bt->page)
1575 bt_update(bt, root);
1577 bt_unlockpage(BtLockWrite, bt->latch);
1578 bt_unpinlatch(bt->latch);
1582 // split already locked full node
1585 BTERR bt_splitpage (BtDb *bt)
1587 uint cnt = 0, idx = 0, max, nxt = bt->page_size;
1588 unsigned char fencekey[256], rightkey[256];
1589 uid page_no = bt->page_no, right;
1590 BtLatchSet *latch, *rlatch;
1591 BtPage page = bt->page;
1592 uint lvl = page->lvl;
1597 // split higher half of keys to bt->frame
1598 // the last key (fence key) might be dead
1600 memset (bt->frame, 0, bt->page_size);
1605 while( cnt++ < max ) {
1606 key = keyptr(page, cnt);
1607 nxt -= key->len + 1;
1608 memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
1609 memcpy(slotptr(bt->frame,++idx)->id, slotptr(page,cnt)->id, BtId);
1610 if( !(slotptr(bt->frame, idx)->dead = slotptr(page, cnt)->dead) )
1613 slotptr(bt->frame, idx)->tod = slotptr(page, cnt)->tod;
1615 slotptr(bt->frame, idx)->off = nxt;
1618 // remember fence key for new right page
1620 memcpy (rightkey, key, key->len + 1);
1622 bt->frame->bits = bt->page_bits;
1623 bt->frame->min = nxt;
1624 bt->frame->cnt = idx;
1625 bt->frame->lvl = lvl;
1629 if( page_no > ROOT_page )
1630 memcpy (bt->frame->right, page->right, BtId);
1632 // get new free page and write frame to it.
1634 if( !(right = bt_newpage(bt, bt->frame)) )
1637 // update lower keys to continue in old page
1639 memcpy (bt->frame, page, bt->page_size);
1640 memset (page+1, 0, bt->page_size - sizeof(*page));
1641 nxt = bt->page_size;
1647 // assemble page of smaller keys
1648 // (they're all active keys)
1650 while( cnt++ < max / 2 ) {
1651 key = keyptr(bt->frame, cnt);
1652 nxt -= key->len + 1;
1653 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1654 memcpy(slotptr(page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
1656 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1658 slotptr(page, idx)->off = nxt;
1662 // remember fence key for smaller page
1664 memcpy (fencekey, key, key->len + 1);
1666 bt_putid(page->right, right);
1670 // if current page is the root page, split it
1672 if( page_no == ROOT_page )
1673 return bt_splitroot (bt, fencekey, right);
1677 if( rlatch = bt_pinlatch (bt, right) )
1678 bt_lockpage (BtLockParent, rlatch);
1682 // update left (containing) node
1684 bt_update(bt, page);
1686 bt_lockpage (BtLockParent, latch);
1687 bt_unlockpage (BtLockWrite, latch);
1689 // insert new fence for reformulated left block
1691 if( bt_insertkey (bt, fencekey+1, *fencekey, lvl+1, page_no, time(NULL)) )
1694 // switch fence for right block of larger keys to new right page
1696 if( bt_insertkey (bt, rightkey+1, *rightkey, lvl+1, right, time(NULL)) )
1699 bt_unlockpage (BtLockParent, latch);
1700 bt_unlockpage (BtLockParent, rlatch);
1702 bt_unpinlatch (rlatch);
1703 bt_unpinlatch (latch);
1707 // Insert new key into the btree at requested level.
1708 // Pages are unlocked at exit.
1710 BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod)
1717 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1718 ptr = keyptr(bt->page, slot);
1722 bt->err = BTERR_ovflw;
1726 // if key already exists, update id and return
1730 if( !keycmp (ptr, key, len) ) {
1731 if( slotptr(page, slot)->dead )
1733 slotptr(page, slot)->dead = 0;
1735 slotptr(page, slot)->tod = tod;
1737 bt_putid(slotptr(page,slot)->id, id);
1738 bt_update(bt, bt->page);
1739 bt_unlockpage(BtLockWrite, bt->latch);
1740 bt_unpinlatch (bt->latch);
1744 // check if page has enough space
1746 if( slot = bt_cleanpage (bt, len, slot) )
1749 if( bt_splitpage (bt) )
1753 // calculate next available slot and copy key into page
1755 page->min -= len + 1; // reset lowest used offset
1756 ((unsigned char *)page)[page->min] = len;
1757 memcpy ((unsigned char *)page + page->min +1, key, len );
1759 for( idx = slot; idx < page->cnt; idx++ )
1760 if( slotptr(page, idx)->dead )
1763 // now insert key into array before slot
1764 // preserving the fence slot
1766 if( idx == page->cnt )
1772 *slotptr(page, idx) = *slotptr(page, idx -1), idx--;
1774 bt_putid(slotptr(page,slot)->id, id);
1775 slotptr(page, slot)->off = page->min;
1777 slotptr(page, slot)->tod = tod;
1779 slotptr(page, slot)->dead = 0;
1781 bt_update(bt, bt->page);
1783 bt_unlockpage(BtLockWrite, bt->latch);
1784 bt_unpinlatch(bt->latch);
1788 // cache page of keys into cursor and return starting slot for given key
1790 uint bt_startkey (BtDb *bt, unsigned char *key, uint len)
1794 // cache page for retrieval
1796 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1797 memcpy (bt->cursor, bt->page, bt->page_size);
1801 bt_unlockpage(BtLockRead, bt->latch);
1802 bt->cursor_page = bt->page_no;
1803 bt_unpinlatch (bt->latch);
1807 // return next slot for cursor page
1808 // or slide cursor right into next page
1810 uint bt_nextkey (BtDb *bt, uint slot)
1816 right = bt_getid(bt->cursor->right);
1818 while( slot++ < bt->cursor->cnt )
1819 if( slotptr(bt->cursor,slot)->dead )
1821 else if( right || (slot < bt->cursor->cnt))
1829 bt->cursor_page = right;
1831 if( latch = bt_pinlatch (bt, right) )
1832 bt_lockpage(BtLockRead, latch);
1836 bt->page = bt_mappage (bt, latch);
1837 memcpy (bt->cursor, bt->page, bt->page_size);
1838 bt_unlockpage(BtLockRead, latch);
1839 bt_unpinlatch (latch);
1846 BtKey bt_key(BtDb *bt, uint slot)
1848 return keyptr(bt->cursor, slot);
1851 uid bt_uid(BtDb *bt, uint slot)
1853 return bt_getid(slotptr(bt->cursor,slot)->id);
1857 uint bt_tod(BtDb *bt, uint slot)
1859 return slotptr(bt->cursor,slot)->tod;
1865 uint bt_audit (BtDb *bt)
1876 memset (blks, 0, sizeof(blks));
1878 if( *(ushort *)(bt->latchmgr->lock) )
1879 fprintf(stderr, "Alloc page locked\n");
1880 *(ushort *)(bt->latchmgr->lock) = 0;
1882 for( idx = 1; idx <= bt->latchmgr->latchdeployed; idx++ ) {
1883 latch = bt->latchsets + idx;
1884 if( *(ushort *)latch->readwr )
1885 fprintf(stderr, "latchset %d rwlocked for page %.8x\n", idx, latch->page_no);
1886 *(ushort *)latch->readwr = 0;
1888 if( *(ushort *)latch->access )
1889 fprintf(stderr, "latchset %d accesslocked for page %.8x\n", idx, latch->page_no);
1890 *(ushort *)latch->access = 0;
1892 if( *(ushort *)latch->parent )
1893 fprintf(stderr, "latchset %d parentlocked for page %.8x\n", idx, latch->page_no);
1894 *(ushort *)latch->parent = 0;
1897 fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
1900 page = (BtPage)((uid)idx * bt->page_size + bt->pagepool);
1903 if( bt_writepage (bt, page, latch->page_no) )
1904 fprintf(stderr, "Page %.8x Write Error\n", latch->page_no);
1907 for( hashidx = 0; hashidx < bt->latchmgr->latchhash; hashidx++ ) {
1908 if( *(ushort *)(bt->table[hashidx].latch) )
1909 fprintf(stderr, "hash entry %d locked\n", hashidx);
1911 *(ushort *)(bt->table[hashidx].latch) = 0;
1913 if( idx = bt->table[hashidx].slot ) do {
1914 latch = bt->latchsets + idx;
1916 fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
1917 } while( idx = latch->next );
1920 next = bt->latchmgr->nlatchpage + LATCH_page;
1921 page_no = LEAF_page;
1923 while( page_no < bt_getid(bt->latchmgr->alloc->right) ) {
1924 if( bt_readpage (bt, bt->frame, page_no) )
1925 fprintf(stderr, "page %.8x unreadable\n", page_no);
1926 if( !bt->frame->free ) {
1927 for( idx = 0; idx++ < bt->frame->cnt - 1; ) {
1928 ptr = keyptr(bt->frame, idx+1);
1929 if( keycmp (keyptr(bt->frame, idx), ptr->key, ptr->len) >= 0 )
1930 fprintf(stderr, "page %.8x idx %.2x out of order\n", page_no, idx);
1932 if( !bt->frame->lvl )
1933 cnt += bt->frame->act;
1934 blks[bt->frame->lvl]++;
1937 if( page_no > LEAF_page )
1941 for( idx = 0; blks[idx]; idx++ )
1942 fprintf(stderr, "%d lvl %d blocks\n", blks[idx], idx);
1947 double getCpuTime(int type)
1950 FILETIME xittime[1];
1951 FILETIME systime[1];
1952 FILETIME usrtime[1];
1953 SYSTEMTIME timeconv[1];
1956 memset (timeconv, 0, sizeof(SYSTEMTIME));
1960 GetSystemTimeAsFileTime (xittime);
1961 FileTimeToSystemTime (xittime, timeconv);
1962 ans = (double)timeconv->wDayOfWeek * 3600 * 24;
1965 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
1966 FileTimeToSystemTime (usrtime, timeconv);
1969 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
1970 FileTimeToSystemTime (systime, timeconv);
1974 ans += (double)timeconv->wHour * 3600;
1975 ans += (double)timeconv->wMinute * 60;
1976 ans += (double)timeconv->wSecond;
1977 ans += (double)timeconv->wMilliseconds / 1000;
1982 #include <sys/resource.h>
1984 double getCpuTime(int type)
1986 struct rusage used[1];
1987 struct timeval tv[1];
1991 gettimeofday(tv, NULL);
1992 return (double)tv->tv_sec + (double)tv->tv_usec / 1000000;
1995 getrusage(RUSAGE_SELF, used);
1996 return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
1999 getrusage(RUSAGE_SELF, used);
2000 return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
2007 // standalone program to index file of keys
2008 // then list them onto std-out
2010 int main (int argc, char **argv)
2012 uint slot, line = 0, off = 0, found = 0;
2013 int ch, cnt = 0, bits = 12, idx;
2014 unsigned char key[256];
2029 _setmode (1, _O_BINARY);
2032 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]);
2033 fprintf (stderr, " page_bits: size of btree page in bits\n");
2034 fprintf (stderr, " mapped_pool_pages: number of pages in buffer pool\n");
2038 start = getCpuTime(0);
2042 bits = atoi(argv[4]);
2045 map = atoi(argv[5]);
2048 off = atoi(argv[6]);
2050 bt = bt_open ((argv[1]), BT_rw, bits, map);
2053 fprintf(stderr, "Index Open Error %s\n", argv[1]);
2057 switch(argv[3][0]| 0x20)
2059 case 'p': // display page
2060 if( latch = bt_pinlatch (bt, off) )
2061 page = bt_mappage (bt, latch);
2063 fprintf(stderr, "unable to read page %.8x\n", off);
2065 write (1, page, bt->page_size);
2068 case 'a': // buffer pool audit
2069 fprintf(stderr, "started audit for %s\n", argv[1]);
2070 cnt = bt_audit (bt);
2071 fprintf(stderr, "finished audit for %s, %d keys\n", argv[1], cnt);
2074 case 'w': // write keys
2075 fprintf(stderr, "started indexing for %s\n", argv[2]);
2076 if( argc > 2 && (in = fopen (argv[2], "rb")) ) {
2078 posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE);
2080 while( ch = getc(in), ch != EOF )
2084 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2086 if( bt_insertkey (bt, key, len, 0, ++line, *tod) )
2087 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2090 else if( len < 245 )
2093 fprintf(stderr, "finished adding keys for %s, %d \n", argv[2], line);
2096 case 'd': // delete keys
2097 fprintf(stderr, "started deleting keys for %s\n", argv[2]);
2098 if( argc > 2 && (in = fopen (argv[2], "rb")) ) {
2100 posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE);
2102 while( ch = getc(in), ch != EOF )
2106 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2108 if( bt_deletekey (bt, key, len, 0) )
2109 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2112 else if( len < 245 )
2115 fprintf(stderr, "finished deleting keys for %s, %d \n", argv[2], line);
2118 case 'f': // find keys
2119 fprintf(stderr, "started finding keys for %s\n", argv[2]);
2120 if( argc > 2 && (in = fopen (argv[2], "rb")) ) {
2122 posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE);
2124 while( ch = getc(in), ch != EOF )
2128 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2130 if( bt_findkey (bt, key, len) )
2133 fprintf(stderr, "Error %d Syserr %d Line: %d\n", bt->err, errno, line), exit(0);
2136 else if( len < 245 )
2139 fprintf(stderr, "finished search of %d keys for %s, found %d\n", line, argv[2], found);
2142 case 's': // scan and print keys
2143 fprintf(stderr, "started scaning\n");
2144 cnt = len = key[0] = 0;
2146 if( slot = bt_startkey (bt, key, len) )
2149 fprintf(stderr, "Error %d in StartKey. Syserror: %d\n", bt->err, errno), exit(0);
2151 while( slot = bt_nextkey (bt, slot) ) {
2152 ptr = bt_key(bt, slot);
2153 fwrite (ptr->key, ptr->len, 1, stdout);
2154 fputc ('\n', stdout);
2158 fprintf(stderr, " Total keys read %d\n", cnt - 1);
2161 case 'c': // count keys
2162 fprintf(stderr, "started counting\n");
2165 next = bt->latchmgr->nlatchpage + LATCH_page;
2166 page_no = LEAF_page;
2168 while( page_no < bt_getid(bt->latchmgr->alloc->right) ) {
2169 if( latch = bt_pinlatch (bt, page_no) )
2170 page = bt_mappage (bt, latch);
2171 if( !page->free && !page->lvl )
2173 if( page_no > LEAF_page )
2176 for( idx = 0; idx++ < page->cnt; ) {
2177 if( slotptr(page, idx)->dead )
2179 ptr = keyptr(page, idx);
2180 if( idx != page->cnt && bt_getid (page->right) ) {
2181 fwrite (ptr->key, ptr->len, 1, stdout);
2182 fputc ('\n', stdout);
2185 bt_unpinlatch (latch);
2189 cnt--; // remove stopper key
2190 fprintf(stderr, " Total keys read %d\n", cnt);
2194 done = getCpuTime(0);
2195 elapsed = (float)(done - start);
2196 fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2197 elapsed = getCpuTime(1);
2198 fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2199 elapsed = getCpuTime(2);
2200 fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);