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
69 // BTree page number constants
75 // Number of levels to create in a new BTree
81 There are five lock types for each node in three independent sets:
82 1. (set 1) AccessIntent: Sharable. Going to Read the node. Incompatible with NodeDelete.
83 2. (set 1) NodeDelete: Exclusive. About to release the node. Incompatible with AccessIntent.
84 3. (set 2) ReadLock: Sharable. Read the node. Incompatible with WriteLock.
85 4. (set 2) WriteLock: Exclusive. Modify the node. Incompatible with ReadLock and other WriteLocks.
86 5. (set 3) ParentModification: Exclusive. Change the node's parent keys. Incompatible with another ParentModification.
97 // definition for latch implementation
99 // exclusive is set for write access
100 // share is count of read accessors
101 // grant write lock when share == 0
103 volatile typedef struct {
104 unsigned char mutex[1];
105 unsigned char exclusive:1;
106 unsigned char pending:1;
110 // Define the length of the page and key pointers
114 // Page key slot definition.
116 // If BT_maxbits is 15 or less, you can save 2 bytes
117 // for each key stored by making the first two uints
118 // into ushorts. You can also save 4 bytes by removing
119 // the tod field from the key.
121 // Keys are marked dead, but remain on the page until
122 // cleanup is called. The fence key (highest key) for
123 // the page is always present, even if dead.
127 uint tod; // time-stamp for key
129 ushort off:BT_maxbits; // page offset for key start
130 ushort dead:1; // set for deleted key
131 unsigned char id[BtId]; // id associated with key
134 // The key structure occupies space at the upper end of
135 // each page. It's a length byte followed by the value
140 unsigned char key[0];
143 // The first part of an index page.
144 // It is immediately followed
145 // by the BtSlot array of keys.
147 typedef struct BtPage_ {
148 uint cnt; // count of keys in page
149 uint act; // count of active keys
150 uint min; // next key offset
151 unsigned char bits:6; // page size in bits
152 unsigned char free:1; // page is on free list
153 unsigned char dirty:1; // page is dirty in cache
154 unsigned char lvl:6; // level of page
155 unsigned char kill:1; // page is being deleted
156 unsigned char clean:1; // page needs cleaning
157 unsigned char right[BtId]; // page number to right
161 struct BtPage_ alloc[2]; // next & free page_nos in right ptr
162 BtSpinLatch lock[1]; // allocation area lite latch
163 volatile uint latchdeployed;// highest number of latch entries deployed
164 volatile uint nlatchpage; // number of latch pages at BT_latch
165 volatile uint latchtotal; // number of page latch entries
166 volatile uint latchhash; // number of latch hash table slots
167 volatile uint latchvictim; // next latch hash entry to examine
168 volatile uint safelevel; // safe page level in cache
169 volatile uint cache[MAX_lvl];// cache census counts by btree level
172 // latch hash table entries
175 volatile uint slot; // Latch table entry at head of collision chain
176 BtSpinLatch latch[1]; // lock for the collision chain
179 // latch manager table structure
182 volatile uid page_no; // latch set page number on disk
183 BtSpinLatch readwr[1]; // read/write page lock
184 BtSpinLatch access[1]; // Access Intent/Page delete
185 BtSpinLatch parent[1]; // Posting of fence key in parent
186 volatile ushort pin; // number of pins/level/clock bits
187 volatile uint next; // next entry in hash table chain
188 volatile uint prev; // prev entry in hash table chain
191 #define CLOCK_mask 0xe000
192 #define CLOCK_unit 0x2000
193 #define PIN_mask 0x07ff
194 #define LVL_mask 0x1800
197 // The object structure for Btree access
199 typedef struct _BtDb {
200 uint page_size; // each page size
201 uint page_bits; // each page size in bits
202 uid page_no; // current page number
203 uid cursor_page; // current cursor page number
205 uint mode; // read-write mode
206 BtPage cursor; // cached frame for start/next (never mapped)
207 BtPage frame; // spare frame for the page split (never mapped)
208 BtPage page; // current mapped page in buffer pool
209 BtLatchSet *latch; // current page latch
210 BtLatchMgr *latchmgr; // mapped latch page from allocation page
211 BtLatchSet *latchsets; // mapped latch set from latch pages
212 unsigned char *pagepool; // cached page pool set
213 BtHashEntry *table; // the hash table
218 HANDLE halloc; // allocation and latch table handle
220 unsigned char *mem; // frame, cursor, memory buffers
221 uint found; // last deletekey found key
239 extern void bt_close (BtDb *bt);
240 extern BtDb *bt_open (char *name, uint mode, uint bits, uint cacheblk);
241 extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod);
242 extern BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl);
243 extern uid bt_findkey (BtDb *bt, unsigned char *key, uint len);
244 extern uint bt_startkey (BtDb *bt, unsigned char *key, uint len);
245 extern uint bt_nextkey (BtDb *bt, uint slot);
247 // internal functions
248 void bt_update (BtDb *bt, BtPage page);
249 BtPage bt_mappage (BtDb *bt, BtLatchSet *latch);
250 // Helper functions to return slot values
252 extern BtKey bt_key (BtDb *bt, uint slot);
253 extern uid bt_uid (BtDb *bt, uint slot);
255 extern uint bt_tod (BtDb *bt, uint slot);
258 // The page is allocated from low and hi ends.
259 // The key offsets and row-id's are allocated
260 // from the bottom, while the text of the key
261 // is allocated from the top. When the two
262 // areas meet, the page is split into two.
264 // A key consists of a length byte, two bytes of
265 // index number (0 - 65534), and up to 253 bytes
266 // of key value. Duplicate keys are discarded.
267 // Associated with each key is a 48 bit row-id.
269 // The b-tree root is always located at page 1.
270 // The first leaf page of level zero is always
271 // located on page 2.
273 // The b-tree pages are linked with right
274 // pointers to facilitate enumerators,
275 // and provide for concurrency.
277 // When to root page fills, it is split in two and
278 // the tree height is raised by a new root at page
279 // one with two keys.
281 // Deleted keys are marked with a dead bit until
282 // page cleanup The fence key for a node is always
283 // present, even after deletion and cleanup.
285 // Deleted leaf pages are reclaimed on a free list.
286 // The upper levels of the btree are fixed on creation.
288 // To achieve maximum concurrency one page is locked at a time
289 // as the tree is traversed to find leaf key in question. The right
290 // page numbers are used in cases where the page is being split,
293 // Page 0 (ALLOC page) is dedicated to lock for new page extensions,
294 // and chains empty leaf pages together for reuse.
296 // Parent locks are obtained to prevent resplitting or deleting a node
297 // before its fence is posted into its upper level.
299 // A special open mode of BT_fl is provided to safely access files on
300 // WIN32 networks. WIN32 network operations should not use memory mapping.
301 // This WIN32 mode sets FILE_FLAG_NOBUFFERING and FILE_FLAG_WRITETHROUGH
302 // to prevent local caching of network file contents.
304 // Access macros to address slot and key values from the page.
305 // Page slots use 1 based indexing.
307 #define slotptr(page, slot) (((BtSlot *)(page+1)) + (slot-1))
308 #define keyptr(page, slot) ((BtKey)((unsigned char*)(page) + slotptr(page, slot)->off))
310 void bt_putid(unsigned char *dest, uid id)
315 dest[i] = (unsigned char)id, id >>= 8;
318 uid bt_getid(unsigned char *src)
323 for( i = 0; i < BtId; i++ )
324 id <<= 8, id |= *src++;
329 BTERR bt_abort (BtDb *bt, BtPage page, uid page_no, BTERR err)
333 fprintf(stderr, "\n Btree2 abort, error %d on page %.8x\n", err, page_no);
334 fprintf(stderr, "level=%d kill=%d free=%d cnt=%x act=%x\n", page->lvl, page->kill, page->free, page->cnt, page->act);
335 ptr = keyptr(page, page->cnt);
336 fprintf(stderr, "fence='%.*s'\n", ptr->len, ptr->key);
337 fprintf(stderr, "right=%.8x\n", bt_getid(page->right));
338 return bt->err = err;
343 // wait until write lock mode is clear
344 // and add 1 to the share count
346 void bt_spinreadlock(BtSpinLatch *latch)
351 // obtain latch mutex
353 if( __sync_lock_test_and_set(latch->mutex, 1) )
356 if( _InterlockedExchange8(latch->mutex, 1) )
359 // see if exclusive request is granted or pending
361 if( prev = !(latch->exclusive | latch->pending) )
367 _InterlockedExchange8(latch->mutex, 0);
374 } while( sched_yield(), 1 );
376 } while( SwitchToThread(), 1 );
380 // wait for other read and write latches to relinquish
382 void bt_spinwritelock(BtSpinLatch *latch)
388 if( __sync_lock_test_and_set(latch->mutex, 1) )
391 if( _InterlockedExchange8(latch->mutex, 1) )
394 if( prev = !(latch->share | latch->exclusive) )
395 latch->exclusive = 1, latch->pending = 0;
401 _InterlockedExchange8(latch->mutex, 0);
406 } while( sched_yield(), 1 );
408 } while( SwitchToThread(), 1 );
412 // try to obtain write lock
414 // return 1 if obtained,
417 int bt_spinwritetry(BtSpinLatch *latch)
422 if( __sync_lock_test_and_set(latch->mutex, 1) )
425 if( _InterlockedExchange8(latch->mutex, 1) )
428 // take write access if all bits are clear
430 if( prev = !(latch->exclusive | latch->share) )
431 latch->exclusive = 1;
436 _InterlockedExchange8(latch->mutex, 0);
443 void bt_spinreleasewrite(BtSpinLatch *latch)
446 while( __sync_lock_test_and_set(latch->mutex, 1) )
449 while( _InterlockedExchange8(latch->mutex, 1) )
452 latch->exclusive = 0;
456 _InterlockedExchange8(latch->mutex, 0);
460 // decrement reader count
462 void bt_spinreleaseread(BtSpinLatch *latch)
465 while( __sync_lock_test_and_set(latch->mutex, 1) )
468 while( _InterlockedExchange8(latch->mutex, 1) )
475 _InterlockedExchange8(latch->mutex, 0);
479 // read page from permanent location in Btree file
481 BTERR bt_readpage (BtDb *bt, BtPage page, uid page_no)
483 off64_t off = page_no << bt->page_bits;
486 if( pread (bt->idx, page, bt->page_size, page_no << bt->page_bits) < bt->page_size ) {
487 fprintf (stderr, "Unable to read page %.8x errno = %d\n", page_no, errno);
488 return bt->err = BTERR_read;
494 memset (ovl, 0, sizeof(OVERLAPPED));
496 ovl->OffsetHigh = off >> 32;
498 if( !ReadFile(bt->idx, page, bt->page_size, amt, ovl)) {
499 fprintf (stderr, "Unable to read page %.8x GetLastError = %d\n", page_no, GetLastError());
500 return bt->err = BTERR_read;
502 if( *amt < bt->page_size ) {
503 fprintf (stderr, "Unable to read page %.8x GetLastError = %d\n", page_no, GetLastError());
504 return bt->err = BTERR_read;
510 // write page to permanent location in Btree file
511 // clear the dirty bit
513 BTERR bt_writepage (BtDb *bt, BtPage page, uid page_no)
515 off64_t off = page_no << bt->page_bits;
520 if( pwrite(bt->idx, page, bt->page_size, off) < bt->page_size )
521 return bt->err = BTERR_wrt;
526 memset (ovl, 0, sizeof(OVERLAPPED));
528 ovl->OffsetHigh = off >> 32;
531 if( !WriteFile(bt->idx, page, bt->page_size, amt, ovl) )
532 return bt->err = BTERR_wrt;
534 if( *amt < bt->page_size )
535 return bt->err = BTERR_wrt;
540 // link latch table entry into head of latch hash table
542 BTERR bt_latchlink (BtDb *bt, uint hashidx, uint slot, uid page_no)
544 BtPage page = (BtPage)((uid)slot * bt->page_size + bt->pagepool);
545 BtLatchSet *latch = bt->latchsets + slot;
548 if( latch->next = bt->table[hashidx].slot )
549 bt->latchsets[latch->next].prev = slot;
551 bt->table[hashidx].slot = slot;
552 latch->page_no = page_no;
556 if( bt_readpage (bt, page, page_no) )
559 lvl = page->lvl << LVL_shift;
562 latch->pin |= lvl; // store lvl
563 latch->pin |= lvl << 3; // initialize clock
566 __sync_fetch_and_add (&bt->latchmgr->cache[page->lvl], 1);
568 _InterlockedAdd(&bt->latchmgr->cache[page->lvl], 1);
575 void bt_unpinlatch (BtLatchSet *latch)
578 __sync_fetch_and_add(&latch->pin, -1);
580 _InterlockedDecrement16 (&latch->pin);
584 // find existing latchset or inspire new one
585 // return with latchset pinned
587 BtLatchSet *bt_pinlatch (BtDb *bt, uid page_no)
589 uint hashidx = page_no % bt->latchmgr->latchhash;
597 // try to find our entry
599 bt_spinwritelock(bt->table[hashidx].latch);
601 if( slot = bt->table[hashidx].slot ) do
603 latch = bt->latchsets + slot;
604 if( page_no == latch->page_no )
606 } while( slot = latch->next );
612 latch = bt->latchsets + slot;
613 lvl = (latch->pin & LVL_mask) >> LVL_shift;
614 lvl *= CLOCK_unit * 2;
617 __sync_fetch_and_add(&latch->pin, 1);
618 __sync_fetch_and_or(&latch->pin, lvl);
620 _InterlockedIncrement16 (&latch->pin);
621 _InterlockedOr16 (&latch->pin, lvl);
623 bt_spinreleasewrite(bt->table[hashidx].latch);
627 // see if there are any unused pool entries
629 slot = __sync_fetch_and_add (&bt->latchmgr->latchdeployed, 1) + 1;
631 slot = _InterlockedIncrement (&bt->latchmgr->latchdeployed);
634 if( slot < bt->latchmgr->latchtotal ) {
635 latch = bt->latchsets + slot;
636 if( bt_latchlink (bt, hashidx, slot, page_no) )
638 bt_spinreleasewrite (bt->table[hashidx].latch);
643 __sync_fetch_and_add (&bt->latchmgr->latchdeployed, -1);
645 _InterlockedDecrement (&bt->latchmgr->latchdeployed);
647 // find and reuse previous entry on victim
651 slot = __sync_fetch_and_add(&bt->latchmgr->latchvictim, 1);
653 slot = _InterlockedIncrement (&bt->latchmgr->latchvictim) - 1;
655 // try to get write lock on hash chain
656 // skip entry if not obtained
657 // or has outstanding pins
659 slot %= bt->latchmgr->latchtotal;
661 // on slot wraparound, check census
662 // count and increment safe level
664 cnt = bt->latchmgr->cache[bt->latchmgr->safelevel];
667 if( cnt < bt->latchmgr->latchtotal / 10 )
669 __sync_fetch_and_add(&bt->latchmgr->safelevel, 1);
671 _InterlockedIncrement (&bt->latchmgr->safelevel);
673 fprintf(stderr, "X");
677 latch = bt->latchsets + slot;
678 idx = latch->page_no % bt->latchmgr->latchhash;
679 lvl = (latch->pin & LVL_mask) >> LVL_shift;
681 // see if we are evicting this level yet
683 if( lvl > bt->latchmgr->safelevel )
686 if( !bt_spinwritetry (bt->table[idx].latch) )
689 if( latch->pin & ~LVL_mask ) {
690 if( latch->pin & CLOCK_mask )
692 __sync_fetch_and_add(&latch->pin, -CLOCK_unit);
694 _InterlockedExchangeAdd16 (&latch->pin, -CLOCK_unit);
696 bt_spinreleasewrite (bt->table[idx].latch);
700 // update permanent page area in btree
702 page = (BtPage)((uid)slot * bt->page_size + bt->pagepool);
704 posix_fadvise (bt->idx, page_no << bt->page_bits, bt->page_size, POSIX_FADV_WILLNEED);
706 fprintf(stderr, "%d", page->lvl);
707 __sync_fetch_and_add (&bt->latchmgr->cache[page->lvl], -1);
709 _InterlockedAdd(&bt->latchmgr->cache[page->lvl], -1);
712 if( bt_writepage (bt, page, latch->page_no) )
715 // unlink our available slot from its hash chain
718 bt->latchsets[latch->prev].next = latch->next;
720 bt->table[idx].slot = latch->next;
723 bt->latchsets[latch->next].prev = latch->prev;
725 bt_spinreleasewrite (bt->table[idx].latch);
727 if( bt_latchlink (bt, hashidx, slot, page_no) )
730 bt_spinreleasewrite (bt->table[hashidx].latch);
735 // close and release memory
737 void bt_close (BtDb *bt)
740 munmap (bt->table, bt->latchmgr->nlatchpage * bt->page_size);
741 munmap (bt->latchmgr, bt->page_size);
743 FlushViewOfFile(bt->latchmgr, 0);
744 UnmapViewOfFile(bt->latchmgr);
745 CloseHandle(bt->halloc);
754 VirtualFree (bt->mem, 0, MEM_RELEASE);
755 FlushFileBuffers(bt->idx);
756 CloseHandle(bt->idx);
760 // open/create new btree
762 // call with file_name, BT_openmode, bits in page size (e.g. 16),
763 // size of mapped page pool (e.g. 8192)
765 BtDb *bt_open (char *name, uint mode, uint bits, uint nodemax)
767 uint lvl, attr, last, slot, idx;
768 uint nlatchpage, latchhash;
769 BtLatchMgr *latchmgr;
779 struct flock lock[1];
782 // determine sanity of page size and buffer pool
784 if( bits > BT_maxbits )
786 else if( bits < BT_minbits )
789 if( mode == BT_ro ) {
790 fprintf(stderr, "ReadOnly mode not supported: %s\n", name);
794 bt = calloc (1, sizeof(BtDb));
796 bt->idx = open ((char*)name, O_RDWR | O_CREAT, 0666);
797 posix_fadvise( bt->idx, 0, 0, POSIX_FADV_RANDOM);
799 if( bt->idx == -1 ) {
800 fprintf(stderr, "unable to open %s\n", name);
801 return free(bt), NULL;
804 bt = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, sizeof(BtDb));
805 attr = FILE_ATTRIBUTE_NORMAL;
806 bt->idx = CreateFile(name, GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, attr, NULL);
808 if( bt->idx == INVALID_HANDLE_VALUE ) {
809 fprintf(stderr, "unable to open %s\n", name);
810 return GlobalFree(bt), NULL;
814 memset (lock, 0, sizeof(lock));
815 lock->l_len = sizeof(struct BtPage_);
816 lock->l_type = F_WRLCK;
818 if( fcntl (bt->idx, F_SETLKW, lock) < 0 ) {
819 fprintf(stderr, "unable to lock record zero %s\n", name);
820 return bt_close (bt), NULL;
823 memset (ovl, 0, sizeof(ovl));
825 // use large offsets to
826 // simulate advisory locking
828 ovl->OffsetHigh |= 0x80000000;
830 if( !LockFileEx (bt->idx, LOCKFILE_EXCLUSIVE_LOCK, 0, sizeof(struct BtPage_), 0L, ovl) ) {
831 fprintf(stderr, "unable to lock record zero %s, GetLastError = %d\n", name, GetLastError());
832 return bt_close (bt), NULL;
837 latchmgr = valloc (BT_maxpage);
840 // read minimum page size to get root info
842 if( size = lseek (bt->idx, 0L, 2) ) {
843 if( pread(bt->idx, latchmgr, BT_minpage, 0) == BT_minpage )
844 bits = latchmgr->alloc->bits;
846 fprintf(stderr, "Unable to read page zero\n");
847 return free(bt), free(latchmgr), NULL;
851 latchmgr = VirtualAlloc(NULL, BT_maxpage, MEM_COMMIT, PAGE_READWRITE);
852 size = GetFileSize(bt->idx, amt);
855 if( !ReadFile(bt->idx, (char *)latchmgr, BT_minpage, amt, NULL) ) {
856 fprintf(stderr, "Unable to read page zero\n");
857 return bt_close (bt), NULL;
859 bits = latchmgr->alloc->bits;
863 bt->page_size = 1 << bits;
864 bt->page_bits = bits;
869 nlatchpage = latchmgr->nlatchpage;
874 fprintf(stderr, "Buffer pool too small: %d\n", nodemax);
875 return bt_close(bt), NULL;
878 // initialize an empty b-tree with latch page, root page, page of leaves
879 // and page(s) of latches and page pool cache
881 memset (latchmgr, 0, 1 << bits);
882 latchmgr->alloc->bits = bt->page_bits;
884 // calculate number of latch hash table entries
886 nlatchpage = (nodemax/16 * sizeof(BtHashEntry) + bt->page_size - 1) / bt->page_size;
887 latchhash = nlatchpage * bt->page_size / sizeof(BtHashEntry);
889 nlatchpage += nodemax; // size of the buffer pool in pages
890 nlatchpage += (sizeof(BtLatchSet) * nodemax + bt->page_size - 1)/bt->page_size;
892 bt_putid(latchmgr->alloc->right, MIN_lvl+1+nlatchpage);
893 latchmgr->nlatchpage = nlatchpage;
894 latchmgr->latchtotal = nodemax;
895 latchmgr->latchhash = latchhash;
897 if( bt_writepage (bt, latchmgr->alloc, 0) ) {
898 fprintf (stderr, "Unable to create btree page zero\n");
899 return bt_close (bt), NULL;
902 memset (latchmgr, 0, 1 << bits);
903 latchmgr->alloc->bits = bt->page_bits;
905 for( lvl=MIN_lvl; lvl--; ) {
906 last = MIN_lvl - lvl; // page number
907 slotptr(latchmgr->alloc, 1)->off = bt->page_size - 3;
908 bt_putid(slotptr(latchmgr->alloc, 1)->id, lvl ? last + 1 : 0);
909 key = keyptr(latchmgr->alloc, 1);
910 key->len = 2; // create stopper key
914 latchmgr->alloc->min = bt->page_size - 3;
915 latchmgr->alloc->lvl = lvl;
916 latchmgr->alloc->cnt = 1;
917 latchmgr->alloc->act = 1;
919 if( bt_writepage (bt, latchmgr->alloc, last) ) {
920 fprintf (stderr, "Unable to create btree page %.8x\n", last);
921 return bt_close (bt), NULL;
925 // clear out buffer pool pages
927 memset(latchmgr, 0, bt->page_size);
928 last = MIN_lvl + nlatchpage;
930 if( bt_writepage (bt, latchmgr->alloc, last) ) {
931 fprintf (stderr, "Unable to write buffer pool page %.8x\n", last);
932 return bt_close (bt), NULL;
938 VirtualFree (latchmgr, 0, MEM_RELEASE);
943 lock->l_type = F_UNLCK;
944 if( fcntl (bt->idx, F_SETLK, lock) < 0 ) {
945 fprintf (stderr, "Unable to unlock page zero\n");
946 return bt_close (bt), NULL;
949 if( !UnlockFileEx (bt->idx, 0, sizeof(struct BtPage_), 0, ovl) ) {
950 fprintf (stderr, "Unable to unlock page zero, GetLastError = %d\n", GetLastError());
951 return bt_close (bt), NULL;
955 flag = PROT_READ | PROT_WRITE;
956 bt->latchmgr = mmap (0, bt->page_size, flag, MAP_SHARED, bt->idx, ALLOC_page * bt->page_size);
957 if( bt->latchmgr == MAP_FAILED ) {
958 fprintf (stderr, "Unable to mmap page zero, errno = %d", errno);
959 return bt_close (bt), NULL;
961 bt->table = (void *)mmap (0, (uid)nlatchpage * bt->page_size, flag, MAP_SHARED, bt->idx, LATCH_page * bt->page_size);
962 if( bt->table == MAP_FAILED ) {
963 fprintf (stderr, "Unable to mmap buffer pool, errno = %d", errno);
964 return bt_close (bt), NULL;
966 madvise (bt->table, (uid)nlatchpage << bt->page_bits, MADV_RANDOM | MADV_WILLNEED);
968 flag = PAGE_READWRITE;
969 bt->halloc = CreateFileMapping(bt->idx, NULL, flag, 0, ((uid)nlatchpage + LATCH_page) * bt->page_size, NULL);
971 fprintf (stderr, "Unable to create file mapping for buffer pool mgr, GetLastError = %d\n", GetLastError());
972 return bt_close (bt), NULL;
975 flag = FILE_MAP_WRITE;
976 bt->latchmgr = MapViewOfFile(bt->halloc, flag, 0, 0, ((uid)nlatchpage + LATCH_page) * bt->page_size);
977 if( !bt->latchmgr ) {
978 fprintf (stderr, "Unable to map buffer pool, GetLastError = %d\n", GetLastError());
979 return bt_close (bt), NULL;
982 bt->table = (void *)((char *)bt->latchmgr + LATCH_page * bt->page_size);
984 bt->pagepool = (unsigned char *)bt->table + (uid)(nlatchpage - bt->latchmgr->latchtotal) * bt->page_size;
985 bt->latchsets = (BtLatchSet *)(bt->pagepool - (uid)bt->latchmgr->latchtotal * sizeof(BtLatchSet));
988 bt->mem = valloc (2 * bt->page_size);
990 bt->mem = VirtualAlloc(NULL, 2 * bt->page_size, MEM_COMMIT, PAGE_READWRITE);
992 bt->frame = (BtPage)bt->mem;
993 bt->cursor = (BtPage)(bt->mem + bt->page_size);
997 // place write, read, or parent lock on requested page_no.
999 void bt_lockpage(BtLock mode, BtLatchSet *latch)
1003 bt_spinreadlock (latch->readwr);
1006 bt_spinwritelock (latch->readwr);
1009 bt_spinreadlock (latch->access);
1012 bt_spinwritelock (latch->access);
1015 bt_spinwritelock (latch->parent);
1020 // remove write, read, or parent lock on requested page
1022 void bt_unlockpage(BtLock mode, BtLatchSet *latch)
1026 bt_spinreleaseread (latch->readwr);
1029 bt_spinreleasewrite (latch->readwr);
1032 bt_spinreleaseread (latch->access);
1035 bt_spinreleasewrite (latch->access);
1038 bt_spinreleasewrite (latch->parent);
1043 // allocate a new page and write page into it
1045 uid bt_newpage(BtDb *bt, BtPage page)
1051 // lock allocation page
1053 bt_spinwritelock(bt->latchmgr->lock);
1055 // use empty chain first
1056 // else allocate empty page
1058 if( new_page = bt_getid(bt->latchmgr->alloc[1].right) ) {
1059 if( latch = bt_pinlatch (bt, new_page) )
1060 temp = bt_mappage (bt, latch);
1064 bt_putid(bt->latchmgr->alloc[1].right, bt_getid(temp->right));
1065 bt_spinreleasewrite(bt->latchmgr->lock);
1066 memcpy (temp, page, bt->page_size);
1068 bt_update (bt, temp);
1069 bt_unpinlatch (latch);
1072 new_page = bt_getid(bt->latchmgr->alloc->right);
1073 bt_putid(bt->latchmgr->alloc->right, new_page+1);
1074 bt_spinreleasewrite(bt->latchmgr->lock);
1076 if( bt_writepage (bt, page, new_page) )
1080 bt_update (bt, bt->latchmgr->alloc);
1084 // compare two keys, returning > 0, = 0, or < 0
1085 // as the comparison value
1087 int keycmp (BtKey key1, unsigned char *key2, uint len2)
1089 uint len1 = key1->len;
1092 if( ans = memcmp (key1->key, key2, len1 > len2 ? len2 : len1) )
1103 // Update current page of btree by
1104 // flushing mapped area to disk backing of cache pool.
1105 // mark page as dirty for rewrite to permanent location
1107 void bt_update (BtDb *bt, BtPage page)
1110 msync (page, bt->page_size, MS_ASYNC);
1112 // FlushViewOfFile (page, bt->page_size);
1117 // map the btree cached page onto current page
1119 BtPage bt_mappage (BtDb *bt, BtLatchSet *latch)
1121 return (BtPage)((uid)(latch - bt->latchsets) * bt->page_size + bt->pagepool);
1124 // deallocate a deleted page
1125 // place on free chain out of allocator page
1126 // call with page latched for Writing and Deleting
1128 BTERR bt_freepage(BtDb *bt, uid page_no, BtLatchSet *latch)
1130 BtPage page = bt_mappage (bt, latch);
1132 // lock allocation page
1134 bt_spinwritelock (bt->latchmgr->lock);
1136 // store chain in second right
1137 bt_putid(page->right, bt_getid(bt->latchmgr->alloc[1].right));
1138 bt_putid(bt->latchmgr->alloc[1].right, page_no);
1141 bt_update(bt, page);
1143 // unlock released page
1145 bt_unlockpage (BtLockDelete, latch);
1146 bt_unlockpage (BtLockWrite, latch);
1147 bt_unpinlatch (latch);
1149 // unlock allocation page
1151 bt_spinreleasewrite (bt->latchmgr->lock);
1152 bt_update (bt, bt->latchmgr->alloc);
1156 // find slot in page for given key at a given level
1158 int bt_findslot (BtDb *bt, unsigned char *key, uint len)
1160 uint diff, higher = bt->page->cnt, low = 1, slot;
1163 // make stopper key an infinite fence value
1165 if( bt_getid (bt->page->right) )
1170 // low is the lowest candidate, higher is already
1171 // tested as .ge. the given key, loop ends when they meet
1173 while( diff = higher - low ) {
1174 slot = low + ( diff >> 1 );
1175 if( keycmp (keyptr(bt->page, slot), key, len) < 0 )
1178 higher = slot, good++;
1181 // return zero if key is on right link page
1183 return good ? higher : 0;
1186 // find and load page at given level for given key
1187 // leave page rd or wr locked as requested
1189 int bt_loadpage (BtDb *bt, unsigned char *key, uint len, uint lvl, uint lock)
1191 uid page_no = ROOT_page, prevpage = 0;
1192 uint drill = 0xff, slot;
1193 BtLatchSet *prevlatch;
1194 uint mode, prevmode;
1196 // start at root of btree and drill down
1199 // determine lock mode of drill level
1200 mode = (lock == BtLockWrite) && (drill == lvl) ? BtLockWrite : BtLockRead;
1202 if( bt->latch = bt_pinlatch(bt, page_no) )
1203 bt->page_no = page_no;
1207 // obtain access lock using lock chaining
1209 if( page_no > ROOT_page )
1210 bt_lockpage(BtLockAccess, bt->latch);
1213 bt_unlockpage(prevmode, prevlatch);
1214 bt_unpinlatch(prevlatch);
1218 // obtain read lock using lock chaining
1220 bt_lockpage(mode, bt->latch);
1222 if( page_no > ROOT_page )
1223 bt_unlockpage(BtLockAccess, bt->latch);
1225 // map/obtain page contents
1227 bt->page = bt_mappage (bt, bt->latch);
1229 // re-read and re-lock root after determining actual level of root
1231 if( bt->page->lvl != drill) {
1232 if( bt->page_no != ROOT_page )
1233 return bt->err = BTERR_struct, 0;
1235 drill = bt->page->lvl;
1237 if( lock != BtLockRead && drill == lvl ) {
1238 bt_unlockpage(mode, bt->latch);
1239 bt_unpinlatch(bt->latch);
1244 prevpage = bt->page_no;
1245 prevlatch = bt->latch;
1248 // find key on page at this level
1249 // and descend to requested level
1251 if( !bt->page->kill )
1252 if( slot = bt_findslot (bt, key, len) ) {
1256 while( slotptr(bt->page, slot)->dead )
1257 if( slot++ < bt->page->cnt )
1262 page_no = bt_getid(slotptr(bt->page, slot)->id);
1267 // or slide right into next page
1270 page_no = bt_getid(bt->page->right);
1274 // return error on end of right chain
1276 bt->err = BTERR_eof;
1277 return 0; // return error
1280 // a fence key was deleted from a page
1281 // push new fence value upwards
1283 BTERR bt_fixfence (BtDb *bt, uid page_no, uint lvl)
1285 unsigned char leftkey[256], rightkey[256];
1286 BtLatchSet *latch = bt->latch;
1289 // remove deleted key, the old fence value
1291 ptr = keyptr(bt->page, bt->page->cnt);
1292 memcpy(rightkey, ptr, ptr->len + 1);
1294 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1295 bt->page->clean = 1;
1297 ptr = keyptr(bt->page, bt->page->cnt);
1298 memcpy(leftkey, ptr, ptr->len + 1);
1300 bt_update (bt, bt->page);
1301 bt_lockpage (BtLockParent, latch);
1302 bt_unlockpage (BtLockWrite, latch);
1304 // insert new (now smaller) fence key
1306 if( bt_insertkey (bt, leftkey+1, *leftkey, lvl + 1, page_no, time(NULL)) )
1309 // remove old (larger) fence key
1311 if( bt_deletekey (bt, rightkey+1, *rightkey, lvl + 1) )
1314 bt_unlockpage (BtLockParent, latch);
1315 bt_unpinlatch (latch);
1319 // root has a single child
1320 // collapse a level from the btree
1321 // call with root locked in bt->page
1323 BTERR bt_collapseroot (BtDb *bt, BtPage root)
1330 // find the child entry
1331 // and promote to new root
1334 for( idx = 0; idx++ < root->cnt; )
1335 if( !slotptr(root, idx)->dead )
1338 child = bt_getid (slotptr(root, idx)->id);
1339 if( latch = bt_pinlatch (bt, child) )
1340 temp = bt_mappage (bt, latch);
1344 bt_lockpage (BtLockDelete, latch);
1345 bt_lockpage (BtLockWrite, latch);
1346 memcpy (root, temp, bt->page_size);
1348 bt_update (bt, root);
1350 if( bt_freepage (bt, child, latch) )
1353 } while( root->lvl > 1 && root->act == 1 );
1355 bt_unlockpage (BtLockWrite, bt->latch);
1356 bt_unpinlatch (bt->latch);
1360 // find and delete key on page by marking delete flag bit
1361 // when page becomes empty, delete it
1363 BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl)
1365 unsigned char lowerkey[256], higherkey[256];
1366 uint slot, dirty = 0, idx, fence, found;
1367 BtLatchSet *latch, *rlatch;
1372 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1373 ptr = keyptr(bt->page, slot);
1377 // are we deleting a fence slot?
1379 fence = slot == bt->page->cnt;
1381 // if key is found delete it, otherwise ignore request
1383 if( found = !keycmp (ptr, key, len) )
1384 if( found = slotptr(bt->page, slot)->dead == 0 ) {
1385 dirty = slotptr(bt->page,slot)->dead = 1;
1386 bt->page->clean = 1;
1389 // collapse empty slots
1391 while( idx = bt->page->cnt - 1 )
1392 if( slotptr(bt->page, idx)->dead ) {
1393 *slotptr(bt->page, idx) = *slotptr(bt->page, idx + 1);
1394 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1399 right = bt_getid(bt->page->right);
1400 page_no = bt->page_no;
1405 return bt_abort (bt, bt->page, page_no, BTERR_notfound);
1406 bt_unlockpage(BtLockWrite, latch);
1407 bt_unpinlatch (latch);
1408 return bt->found = found, 0;
1411 // did we delete a fence key in an upper level?
1413 if( lvl && bt->page->act && fence )
1414 if( bt_fixfence (bt, page_no, lvl) )
1417 return bt->found = found, 0;
1419 // is this a collapsed root?
1421 if( lvl > 1 && page_no == ROOT_page && bt->page->act == 1 )
1422 if( bt_collapseroot (bt, bt->page) )
1425 return bt->found = found, 0;
1427 // return if page is not empty
1429 if( bt->page->act ) {
1430 bt_update(bt, bt->page);
1431 bt_unlockpage(BtLockWrite, latch);
1432 bt_unpinlatch (latch);
1433 return bt->found = found, 0;
1436 // cache copy of fence key
1437 // in order to find parent
1439 ptr = keyptr(bt->page, bt->page->cnt);
1440 memcpy(lowerkey, ptr, ptr->len + 1);
1442 // obtain lock on right page
1444 if( rlatch = bt_pinlatch (bt, right) )
1445 temp = bt_mappage (bt, rlatch);
1449 bt_lockpage(BtLockWrite, rlatch);
1452 bt_abort(bt, temp, right, 0);
1453 return bt_abort(bt, bt->page, bt->page_no, BTERR_kill);
1456 // pull contents of next page into current empty page
1458 memcpy (bt->page, temp, bt->page_size);
1460 // cache copy of key to update
1462 ptr = keyptr(temp, temp->cnt);
1463 memcpy(higherkey, ptr, ptr->len + 1);
1465 // Mark right page as deleted and point it to left page
1466 // until we can post updates at higher level.
1468 bt_putid(temp->right, page_no);
1471 bt_update(bt, bt->page);
1472 bt_update(bt, temp);
1474 bt_lockpage(BtLockParent, latch);
1475 bt_unlockpage(BtLockWrite, latch);
1477 bt_lockpage(BtLockParent, rlatch);
1478 bt_unlockpage(BtLockWrite, rlatch);
1480 // redirect higher key directly to consolidated node
1482 if( bt_insertkey (bt, higherkey+1, *higherkey, lvl+1, page_no, time(NULL)) )
1485 // delete old lower key to consolidated node
1487 if( bt_deletekey (bt, lowerkey + 1, *lowerkey, lvl + 1) )
1490 // obtain write & delete lock on deleted node
1491 // add right block to free chain
1493 bt_lockpage(BtLockDelete, rlatch);
1494 bt_lockpage(BtLockWrite, rlatch);
1495 bt_unlockpage(BtLockParent, rlatch);
1497 if( bt_freepage (bt, right, rlatch) )
1500 bt_unlockpage(BtLockParent, latch);
1501 bt_unpinlatch(latch);
1505 // find key in leaf level and return row-id
1507 uid bt_findkey (BtDb *bt, unsigned char *key, uint len)
1513 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1514 ptr = keyptr(bt->page, slot);
1518 // if key exists, return row-id
1519 // otherwise return 0
1521 if( ptr->len == len && !memcmp (ptr->key, key, len) )
1522 id = bt_getid(slotptr(bt->page,slot)->id);
1526 bt_unlockpage (BtLockRead, bt->latch);
1527 bt_unpinlatch (bt->latch);
1531 // check page for space available,
1532 // clean if necessary and return
1533 // 0 - page needs splitting
1534 // >0 - go ahead with new slot
1536 uint bt_cleanpage(BtDb *bt, uint amt, uint slot)
1538 uint nxt = bt->page_size;
1539 BtPage page = bt->page;
1540 uint cnt = 0, idx = 0;
1541 uint max = page->cnt;
1542 uint newslot = slot;
1546 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1549 // skip cleanup if nothing to reclaim
1554 memcpy (bt->frame, page, bt->page_size);
1556 // skip page info and set rest of page to zero
1558 memset (page+1, 0, bt->page_size - sizeof(*page));
1561 while( cnt++ < max ) {
1564 // always leave fence key in list
1565 if( cnt < max && slotptr(bt->frame,cnt)->dead )
1569 key = keyptr(bt->frame, cnt);
1570 nxt -= key->len + 1;
1571 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1574 memcpy(slotptr(page, ++idx)->id, slotptr(bt->frame, cnt)->id, BtId);
1575 if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
1578 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1580 slotptr(page, idx)->off = nxt;
1586 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1592 // split the root and raise the height of the btree
1594 BTERR bt_splitroot(BtDb *bt, unsigned char *leftkey, uid page_no2)
1596 uint nxt = bt->page_size;
1597 BtPage root = bt->page;
1600 // Obtain an empty page to use, and copy the current
1601 // root contents into it
1603 if( !(right = bt_newpage(bt, root)) )
1606 // preserve the page info at the bottom
1607 // and set rest to zero
1609 memset(root+1, 0, bt->page_size - sizeof(*root));
1611 // insert first key on newroot page
1613 nxt -= *leftkey + 1;
1614 memcpy ((unsigned char *)root + nxt, leftkey, *leftkey + 1);
1615 bt_putid(slotptr(root, 1)->id, right);
1616 slotptr(root, 1)->off = nxt;
1618 // insert second key on newroot page
1619 // and increase the root height
1622 ((unsigned char *)root)[nxt] = 2;
1623 ((unsigned char *)root)[nxt+1] = 0xff;
1624 ((unsigned char *)root)[nxt+2] = 0xff;
1625 bt_putid(slotptr(root, 2)->id, page_no2);
1626 slotptr(root, 2)->off = nxt;
1628 bt_putid(root->right, 0);
1629 root->min = nxt; // reset lowest used offset and key count
1634 // update and release root (bt->page)
1636 bt_update(bt, root);
1638 bt_unlockpage(BtLockWrite, bt->latch);
1639 bt_unpinlatch(bt->latch);
1643 // split already locked full node
1646 BTERR bt_splitpage (BtDb *bt)
1648 uint cnt = 0, idx = 0, max, nxt = bt->page_size;
1649 unsigned char fencekey[256], rightkey[256];
1650 uid page_no = bt->page_no, right;
1651 BtLatchSet *latch, *rlatch;
1652 BtPage page = bt->page;
1653 uint lvl = page->lvl;
1658 // split higher half of keys to bt->frame
1659 // the last key (fence key) might be dead
1661 memset (bt->frame, 0, bt->page_size);
1666 while( cnt++ < max ) {
1667 key = keyptr(page, cnt);
1668 nxt -= key->len + 1;
1669 memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
1670 memcpy(slotptr(bt->frame,++idx)->id, slotptr(page,cnt)->id, BtId);
1671 if( !(slotptr(bt->frame, idx)->dead = slotptr(page, cnt)->dead) )
1674 slotptr(bt->frame, idx)->tod = slotptr(page, cnt)->tod;
1676 slotptr(bt->frame, idx)->off = nxt;
1679 // remember fence key for new right page
1681 memcpy (rightkey, key, key->len + 1);
1683 bt->frame->bits = bt->page_bits;
1684 bt->frame->min = nxt;
1685 bt->frame->cnt = idx;
1686 bt->frame->lvl = lvl;
1690 if( page_no > ROOT_page )
1691 memcpy (bt->frame->right, page->right, BtId);
1693 // get new free page and write frame to it.
1695 if( !(right = bt_newpage(bt, bt->frame)) )
1698 // update lower keys to continue in old page
1700 memcpy (bt->frame, page, bt->page_size);
1701 memset (page+1, 0, bt->page_size - sizeof(*page));
1702 nxt = bt->page_size;
1708 // assemble page of smaller keys
1709 // (they're all active keys)
1711 while( cnt++ < max / 2 ) {
1712 key = keyptr(bt->frame, cnt);
1713 nxt -= key->len + 1;
1714 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1715 memcpy(slotptr(page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
1717 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1719 slotptr(page, idx)->off = nxt;
1723 // remember fence key for smaller page
1725 memcpy (fencekey, key, key->len + 1);
1727 bt_putid(page->right, right);
1731 // if current page is the root page, split it
1733 if( page_no == ROOT_page )
1734 return bt_splitroot (bt, fencekey, right);
1738 if( rlatch = bt_pinlatch (bt, right) )
1739 bt_lockpage (BtLockParent, rlatch);
1743 // update left (containing) node
1745 bt_update(bt, page);
1747 bt_lockpage (BtLockParent, latch);
1748 bt_unlockpage (BtLockWrite, latch);
1750 // insert new fence for reformulated left block
1752 if( bt_insertkey (bt, fencekey+1, *fencekey, lvl+1, page_no, time(NULL)) )
1755 // switch fence for right block of larger keys to new right page
1757 if( bt_insertkey (bt, rightkey+1, *rightkey, lvl+1, right, time(NULL)) )
1760 bt_unlockpage (BtLockParent, latch);
1761 bt_unlockpage (BtLockParent, rlatch);
1763 bt_unpinlatch (rlatch);
1764 bt_unpinlatch (latch);
1768 // Insert new key into the btree at requested level.
1769 // Pages are unlocked at exit.
1771 BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod)
1778 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1779 ptr = keyptr(bt->page, slot);
1783 bt->err = BTERR_ovflw;
1787 // if key already exists, update id and return
1791 if( !keycmp (ptr, key, len) ) {
1792 if( slotptr(page, slot)->dead )
1794 slotptr(page, slot)->dead = 0;
1796 slotptr(page, slot)->tod = tod;
1798 bt_putid(slotptr(page,slot)->id, id);
1799 bt_update(bt, bt->page);
1800 bt_unlockpage(BtLockWrite, bt->latch);
1801 bt_unpinlatch (bt->latch);
1805 // check if page has enough space
1807 if( slot = bt_cleanpage (bt, len, slot) )
1810 if( bt_splitpage (bt) )
1814 // calculate next available slot and copy key into page
1816 page->min -= len + 1; // reset lowest used offset
1817 ((unsigned char *)page)[page->min] = len;
1818 memcpy ((unsigned char *)page + page->min +1, key, len );
1820 for( idx = slot; idx < page->cnt; idx++ )
1821 if( slotptr(page, idx)->dead )
1824 // now insert key into array before slot
1825 // preserving the fence slot
1827 if( idx == page->cnt )
1833 *slotptr(page, idx) = *slotptr(page, idx -1), idx--;
1835 bt_putid(slotptr(page,slot)->id, id);
1836 slotptr(page, slot)->off = page->min;
1838 slotptr(page, slot)->tod = tod;
1840 slotptr(page, slot)->dead = 0;
1842 bt_update(bt, bt->page);
1844 bt_unlockpage(BtLockWrite, bt->latch);
1845 bt_unpinlatch(bt->latch);
1849 // cache page of keys into cursor and return starting slot for given key
1851 uint bt_startkey (BtDb *bt, unsigned char *key, uint len)
1855 // cache page for retrieval
1857 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1858 memcpy (bt->cursor, bt->page, bt->page_size);
1862 bt_unlockpage(BtLockRead, bt->latch);
1863 bt->cursor_page = bt->page_no;
1864 bt_unpinlatch (bt->latch);
1868 // return next slot for cursor page
1869 // or slide cursor right into next page
1871 uint bt_nextkey (BtDb *bt, uint slot)
1877 right = bt_getid(bt->cursor->right);
1879 while( slot++ < bt->cursor->cnt )
1880 if( slotptr(bt->cursor,slot)->dead )
1882 else if( right || (slot < bt->cursor->cnt))
1890 bt->cursor_page = right;
1892 if( latch = bt_pinlatch (bt, right) )
1893 bt_lockpage(BtLockRead, latch);
1897 bt->page = bt_mappage (bt, latch);
1898 memcpy (bt->cursor, bt->page, bt->page_size);
1899 bt_unlockpage(BtLockRead, latch);
1900 bt_unpinlatch (latch);
1907 BtKey bt_key(BtDb *bt, uint slot)
1909 return keyptr(bt->cursor, slot);
1912 uid bt_uid(BtDb *bt, uint slot)
1914 return bt_getid(slotptr(bt->cursor,slot)->id);
1918 uint bt_tod(BtDb *bt, uint slot)
1920 return slotptr(bt->cursor,slot)->tod;
1926 uint bt_audit (BtDb *bt)
1938 posix_fadvise( bt->idx, 0, 0, POSIX_FADV_SEQUENTIAL);
1940 if( *(ushort *)(bt->latchmgr->lock) )
1941 fprintf(stderr, "Alloc page locked\n");
1942 *(ushort *)(bt->latchmgr->lock) = 0;
1944 memset (blks, 0, sizeof(blks));
1946 for( idx = 1; idx <= bt->latchmgr->latchdeployed; idx++ ) {
1947 latch = bt->latchsets + idx;
1948 if( *(ushort *)latch->readwr )
1949 fprintf(stderr, "latchset %d rwlocked for page %.8x\n", idx, latch->page_no);
1950 *(ushort *)latch->readwr = 0;
1952 if( *(ushort *)latch->access )
1953 fprintf(stderr, "latchset %d accesslocked for page %.8x\n", idx, latch->page_no);
1954 *(ushort *)latch->access = 0;
1956 if( *(ushort *)latch->parent )
1957 fprintf(stderr, "latchset %d parentlocked for page %.8x\n", idx, latch->page_no);
1958 *(ushort *)latch->parent = 0;
1960 if( latch->pin & PIN_mask ) {
1961 fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
1964 page = (BtPage)((uid)idx * bt->page_size + bt->pagepool);
1968 if( bt_writepage (bt, page, latch->page_no) )
1969 fprintf(stderr, "Page %.8x Write Error\n", latch->page_no);
1972 for( idx = 0; blks[idx]; idx++ )
1973 fprintf(stderr, "cache: %d lvl %d blocks\n", blks[idx], idx);
1975 for( hashidx = 0; hashidx < bt->latchmgr->latchhash; hashidx++ ) {
1976 if( *(ushort *)(bt->table[hashidx].latch) )
1977 fprintf(stderr, "hash entry %d locked\n", hashidx);
1979 *(ushort *)(bt->table[hashidx].latch) = 0;
1982 memset (blks, 0, sizeof(blks));
1984 next = bt->latchmgr->nlatchpage + LATCH_page;
1985 page_no = LEAF_page;
1987 while( page_no < bt_getid(bt->latchmgr->alloc->right) ) {
1988 if( bt_readpage (bt, bt->frame, page_no) )
1989 fprintf(stderr, "page %.8x unreadable\n", page_no);
1990 if( !bt->frame->free ) {
1991 for( idx = 0; idx++ < bt->frame->cnt - 1; ) {
1992 ptr = keyptr(bt->frame, idx+1);
1993 if( keycmp (keyptr(bt->frame, idx), ptr->key, ptr->len) >= 0 )
1994 fprintf(stderr, "page %.8x idx %.2x out of order\n", page_no, idx);
1996 if( !bt->frame->lvl )
1997 cnt += bt->frame->act;
1998 blks[bt->frame->lvl]++;
2001 if( page_no > LEAF_page )
2006 for( idx = 0; blks[idx]; idx++ )
2007 fprintf(stderr, "btree: %d lvl %d blocks\n", blks[idx], idx);
2013 double getCpuTime(int type)
2016 FILETIME xittime[1];
2017 FILETIME systime[1];
2018 FILETIME usrtime[1];
2019 SYSTEMTIME timeconv[1];
2022 memset (timeconv, 0, sizeof(SYSTEMTIME));
2026 GetSystemTimeAsFileTime (xittime);
2027 FileTimeToSystemTime (xittime, timeconv);
2028 ans = (double)timeconv->wDayOfWeek * 3600 * 24;
2031 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
2032 FileTimeToSystemTime (usrtime, timeconv);
2035 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
2036 FileTimeToSystemTime (systime, timeconv);
2040 ans += (double)timeconv->wHour * 3600;
2041 ans += (double)timeconv->wMinute * 60;
2042 ans += (double)timeconv->wSecond;
2043 ans += (double)timeconv->wMilliseconds / 1000;
2048 #include <sys/resource.h>
2050 double getCpuTime(int type)
2052 struct rusage used[1];
2053 struct timeval tv[1];
2057 gettimeofday(tv, NULL);
2058 return (double)tv->tv_sec + (double)tv->tv_usec / 1000000;
2061 getrusage(RUSAGE_SELF, used);
2062 return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
2065 getrusage(RUSAGE_SELF, used);
2066 return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
2073 // standalone program to index file of keys
2074 // then list them onto std-out
2076 int main (int argc, char **argv)
2078 uint slot, line = 0, off = 0, found = 0;
2079 int ch, cnt = 0, bits = 12, idx;
2080 unsigned char key[256];
2095 _setmode (1, _O_BINARY);
2098 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]);
2099 fprintf (stderr, " page_bits: size of btree page in bits\n");
2100 fprintf (stderr, " mapped_pool_pages: number of pages in buffer pool\n");
2104 start = getCpuTime(0);
2108 bits = atoi(argv[4]);
2111 map = atoi(argv[5]);
2114 off = atoi(argv[6]);
2116 bt = bt_open ((argv[1]), BT_rw, bits, map);
2119 fprintf(stderr, "Index Open Error %s\n", argv[1]);
2123 switch(argv[3][0]| 0x20)
2125 case 'p': // display page
2126 if( latch = bt_pinlatch (bt, off) )
2127 page = bt_mappage (bt, latch);
2129 fprintf(stderr, "unable to read page %.8x\n", off);
2131 write (1, page, bt->page_size);
2134 case 'a': // buffer pool audit
2135 fprintf(stderr, "started audit for %s\n", argv[1]);
2136 cnt = bt_audit (bt);
2137 fprintf(stderr, "finished audit for %s, %d keys\n", argv[1], cnt);
2140 case 'w': // write keys
2141 fprintf(stderr, "started indexing for %s\n", argv[2]);
2142 if( argc > 2 && (in = fopen (argv[2], "rb")) ) {
2144 posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE);
2146 while( ch = getc(in), ch != EOF )
2150 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2152 if( bt_insertkey (bt, key, len, 0, ++line, *tod) )
2153 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2156 else if( len < 245 )
2159 fprintf(stderr, "finished adding keys for %s, %d \n", argv[2], line);
2162 case 'd': // delete keys
2163 fprintf(stderr, "started deleting keys for %s\n", argv[2]);
2164 if( argc > 2 && (in = fopen (argv[2], "rb")) ) {
2166 posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE);
2168 while( ch = getc(in), ch != EOF )
2172 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2174 if( bt_deletekey (bt, key, len, 0) )
2175 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2178 else if( len < 245 )
2181 fprintf(stderr, "finished deleting keys for %s, %d \n", argv[2], line);
2184 case 'f': // find keys
2185 fprintf(stderr, "started finding keys for %s\n", argv[2]);
2186 if( argc > 2 && (in = fopen (argv[2], "rb")) ) {
2188 posix_fadvise( fileno(in), 0, 0, POSIX_FADV_NOREUSE);
2190 while( ch = getc(in), ch != EOF )
2194 sprintf((char *)key+len, "%.9d", line + off), len += 9;
2196 if( bt_findkey (bt, key, len) )
2199 fprintf(stderr, "Error %d Syserr %d Line: %d\n", bt->err, errno, line), exit(0);
2202 else if( len < 245 )
2205 fprintf(stderr, "finished search of %d keys for %s, found %d\n", line, argv[2], found);
2208 case 's': // scan and print keys
2209 fprintf(stderr, "started scaning\n");
2210 cnt = len = key[0] = 0;
2212 if( slot = bt_startkey (bt, key, len) )
2215 fprintf(stderr, "Error %d in StartKey. Syserror: %d\n", bt->err, errno), exit(0);
2217 while( slot = bt_nextkey (bt, slot) ) {
2218 ptr = bt_key(bt, slot);
2219 fwrite (ptr->key, ptr->len, 1, stdout);
2220 fputc ('\n', stdout);
2224 fprintf(stderr, " Total keys read %d\n", cnt - 1);
2227 case 'c': // count keys
2228 fprintf(stderr, "started counting\n");
2231 next = bt->latchmgr->nlatchpage + LATCH_page;
2232 page_no = LEAF_page;
2234 while( page_no < bt_getid(bt->latchmgr->alloc->right) ) {
2235 if( latch = bt_pinlatch (bt, page_no) )
2236 page = bt_mappage (bt, latch);
2237 if( !page->free && !page->lvl )
2239 if( page_no > LEAF_page )
2242 for( idx = 0; idx++ < page->cnt; ) {
2243 if( slotptr(page, idx)->dead )
2245 ptr = keyptr(page, idx);
2246 if( idx != page->cnt && bt_getid (page->right) ) {
2247 fwrite (ptr->key, ptr->len, 1, stdout);
2248 fputc ('\n', stdout);
2251 bt_unpinlatch (latch);
2255 cnt--; // remove stopper key
2256 fprintf(stderr, " Total keys read %d\n", cnt);
2260 done = getCpuTime(0);
2261 elapsed = (float)(done - start);
2262 fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2263 elapsed = getCpuTime(1);
2264 fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2265 elapsed = getCpuTime(2);
2266 fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);