1 // foster btree version f2
4 // author: karl malbrain, malbrain@cal.berkeley.edu
7 This work, including the source code, documentation
8 and related data, is placed into the public domain.
10 The orginal author is Karl Malbrain.
12 THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
13 OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
14 MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
15 ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
16 RESULTING FROM THE USE, MODIFICATION, OR
17 REDISTRIBUTION OF THIS SOFTWARE.
20 // Please see the project home page for documentation
21 // code.google.com/p/high-concurrency-btree
23 #define _FILE_OFFSET_BITS 64
24 #define _LARGEFILE64_SOURCE
40 #define WIN32_LEAN_AND_MEAN
53 typedef unsigned long long uid;
56 typedef unsigned long long off64_t;
57 typedef unsigned short ushort;
58 typedef unsigned int uint;
61 #define BT_ro 0x6f72 // ro
62 #define BT_rw 0x7772 // rw
64 #define BT_latchtable 128 // number of latch manager slots
66 #define BT_maxbits 24 // maximum page size in bits
67 #define BT_minbits 9 // minimum page size in bits
68 #define BT_minpage (1 << BT_minbits) // minimum page size
69 #define BT_maxpage (1 << BT_maxbits) // maximum page size
72 There are five lock types for each node in three independent sets:
73 1. (set 1) AccessIntent: Sharable. Going to Read the node. Incompatible with NodeDelete.
74 2. (set 1) NodeDelete: Exclusive. About to release the node. Incompatible with AccessIntent.
75 3. (set 2) ReadLock: Sharable. Read the node. Incompatible with WriteLock.
76 4. (set 2) WriteLock: Exclusive. Modify the node. Incompatible with ReadLock and other WriteLocks.
77 5. (set 3) ParentLock: Exclusive. Have parent adopt/delete maximum foster child from the node.
88 // Define the length of the page and key pointers
92 // Page key slot definition.
94 // If BT_maxbits is 15 or less, you can save 4 bytes
95 // for each key stored by making the first two uints
96 // into ushorts. You can also save 4 bytes by removing
97 // the tod field from the key.
99 // Keys are marked dead, but remain on the page until
100 // it cleanup is called. The fence key (highest key) for
101 // the page is always present, even after cleanup.
104 uint off:BT_maxbits; // page offset for key start
105 uint dead:1; // set for deleted key
106 uint tod; // time-stamp for key
107 unsigned char id[BtId]; // id associated with key
110 // The key structure occupies space at the upper end of
111 // each page. It's a length byte followed by the value
116 unsigned char key[1];
119 // The first part of an index page.
120 // It is immediately followed
121 // by the BtSlot array of keys.
123 typedef struct Page {
124 volatile uint cnt; // count of keys in page
125 volatile uint act; // count of active keys
126 volatile uint min; // next key offset
127 volatile uint foster; // count of foster children
128 unsigned char bits; // page size in bits
129 unsigned char lvl:7; // level of page
130 unsigned char dirty:1; // page needs to be cleaned
131 unsigned char right[BtId]; // page number to right
134 // mode & definition for hash latch implementation
143 // mutex locks the other fields
144 // exclusive is set for write access
145 // share is count of read accessors
148 volatile ushort mutex:1;
149 volatile ushort exclusive:1;
150 volatile ushort pending:1;
151 volatile ushort share:13;
154 // hash table entries
157 BtSpinLatch latch[1];
158 volatile ushort slot; // Latch table entry at head of chain
161 // latch manager table structure
164 BtSpinLatch readwr[1]; // read/write page lock
165 BtSpinLatch access[1]; // Access Intent/Page delete
166 BtSpinLatch parent[1]; // adoption of foster children
167 BtSpinLatch busy[1]; // slot is being moved between chains
168 volatile ushort next; // next entry in hash table chain
169 volatile ushort prev; // prev entry in hash table chain
170 volatile ushort pin; // number of outstanding locks
171 volatile ushort hash; // hash slot entry is under
172 volatile uid page_no; // latch set page number
175 // The memory mapping pool table buffer manager entry
178 unsigned long long int lru; // number of times accessed
179 uid basepage; // mapped base page number
180 char *map; // mapped memory pointer
181 ushort pin; // mapped page pin counter
182 ushort slot; // slot index in this array
183 void *hashprev; // previous pool entry for the same hash idx
184 void *hashnext; // next pool entry for the same hash idx
186 HANDLE hmap; // Windows memory mapping handle
190 // structure for latch manager on ALLOC_page
193 struct Page alloc[2]; // next & free page_nos in right ptr
194 BtSpinLatch lock[1]; // allocation area lite latch
195 ushort latchdeployed; // highest number of latch entries deployed
196 ushort nlatchpage; // number of latch pages at BT_latch
197 ushort latchtotal; // number of page latch entries
198 ushort latchhash; // number of latch hash table slots
199 ushort latchvictim; // next latch entry to examine
200 BtHashEntry table[0]; // the hash table
203 // The object structure for Btree access
206 uint page_size; // page size
207 uint page_bits; // page size in bits
208 uint seg_bits; // seg size in pages in bits
209 uint mode; // read-write mode
212 char *pooladvise; // bit maps for pool page advisements
216 ushort poolcnt; // highest page pool node in use
217 ushort poolmax; // highest page pool node allocated
218 ushort poolmask; // total number of pages in mmap segment - 1
219 ushort hashsize; // size of Hash Table for pool entries
220 ushort evicted; // last evicted hash table slot
221 ushort *hash; // hash table of pool entries
222 BtPool *pool; // memory pool page segments
223 BtSpinLatch *latch; // latches for pool hash slots
224 BtLatchMgr *latchmgr; // mapped latch page from allocation page
225 BtLatchSet *latchsets; // mapped latch set from latch pages
227 HANDLE halloc; // allocation and latch table handle
232 BtMgr *mgr; // buffer manager for thread
233 BtPage cursor; // cached frame for start/next (never mapped)
234 BtPage frame; // spare frame for the page split (never mapped)
235 BtPage zero; // page frame for zeroes at end of file
236 BtPage page; // current page
237 uid page_no; // current page number
238 uid cursor_page; // current cursor page number
239 BtLatchSet *set; // current page latch set
240 BtPool *pool; // current page pool
241 unsigned char *mem; // frame, cursor, page memory buffer
242 int found; // last delete was found
243 int err; // last error
258 extern void bt_close (BtDb *bt);
259 extern BtDb *bt_open (BtMgr *mgr);
260 extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uid id, uint tod, uint lvl);
261 extern BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len);
262 extern uid bt_findkey (BtDb *bt, unsigned char *key, uint len);
263 extern uint bt_startkey (BtDb *bt, unsigned char *key, uint len);
264 extern uint bt_nextkey (BtDb *bt, uint slot);
267 extern BtMgr *bt_mgr (char *name, uint mode, uint bits, uint poolsize, uint segsize, uint hashsize);
268 void bt_mgrclose (BtMgr *mgr);
270 // Helper functions to return cursor slot values
272 extern BtKey bt_key (BtDb *bt, uint slot);
273 extern uid bt_uid (BtDb *bt, uint slot);
274 extern uint bt_tod (BtDb *bt, uint slot);
276 // BTree page number constants
277 #define ALLOC_page 0 // allocation & lock manager hash table
278 #define ROOT_page 1 // root of the btree
279 #define LEAF_page 2 // first page of leaves
280 #define LATCH_page 3 // pages for lock manager
282 // Number of levels to create in a new BTree
286 // The page is allocated from low and hi ends.
287 // The key offsets and row-id's are allocated
288 // from the bottom, while the text of the key
289 // is allocated from the top. When the two
290 // areas meet, the page is split into two.
292 // A key consists of a length byte, two bytes of
293 // index number (0 - 65534), and up to 253 bytes
294 // of key value. Duplicate keys are discarded.
295 // Associated with each key is a 48 bit row-id.
297 // The b-tree root is always located at page 1.
298 // The first leaf page of level zero is always
299 // located on page 2.
301 // When to root page fills, it is split in two and
302 // the tree height is raised by a new root at page
303 // one with two keys.
305 // Deleted keys are marked with a dead bit until
306 // page cleanup The fence key for a node is always
307 // present, even after deletion and cleanup.
309 // Groups of pages called segments from the btree are
310 // cached with memory mapping. A hash table is used to keep
311 // track of the cached segments. This behaviour is controlled
312 // by the cache block size parameter to bt_open.
314 // To achieve maximum concurrency one page is locked at a time
315 // as the tree is traversed to find leaf key in question.
317 // An adoption traversal leaves the parent node locked as the
318 // tree is traversed to the level in quesiton.
320 // Page 0 is dedicated to lock for new page extensions,
321 // and chains empty pages together for reuse.
323 // Empty pages are chained together through the ALLOC page and reused.
325 // Access macros to address slot and key values from the page
327 #define slotptr(page, slot) (((BtSlot *)(page+1)) + (slot-1))
328 #define keyptr(page, slot) ((BtKey)((unsigned char*)(page) + slotptr(page, slot)->off))
330 void bt_putid(unsigned char *dest, uid id)
335 dest[i] = (unsigned char)id, id >>= 8;
338 uid bt_getid(unsigned char *src)
343 for( i = 0; i < BtId; i++ )
344 id <<= 8, id |= *src++;
349 // wait until write lock mode is clear
350 // and add 1 to the share count
352 void bt_spinreadlock(BtSpinLatch *latch)
358 while( __sync_fetch_and_or((ushort *)latch, Mutex) & Mutex )
361 while( _InterlockedOr16((ushort *)latch, Mutex) & Mutex )
365 // see if exclusive request is granted or pending
367 if( prev = !(latch->exclusive | latch->pending) )
369 __sync_fetch_and_add((ushort *)latch, Share);
371 _InterlockedExchangeAdd16 ((ushort *)latch, Share);
375 __sync_fetch_and_and ((ushort *)latch, ~Mutex);
377 _InterlockedAnd16((ushort *)latch, ~Mutex);
382 } while( sched_yield(), 1 );
384 } while( SwitchToThread(), 1 );
388 // wait for other read and write latches to relinquish
390 void bt_spinwritelock(BtSpinLatch *latch)
394 while( __sync_fetch_and_or((ushort *)latch, Mutex | Pending) & Mutex )
397 while( _InterlockedOr16((ushort *)latch, Mutex | Pending) & Mutex )
400 if( !(latch->share | latch->exclusive) ) {
402 __sync_fetch_and_or((ushort *)latch, Write);
403 __sync_fetch_and_and ((ushort *)latch, ~(Mutex | Pending));
405 _InterlockedOr16((ushort *)latch, Write);
406 _InterlockedAnd16((ushort *)latch, ~(Mutex | Pending));
412 __sync_fetch_and_and ((ushort *)latch, ~Mutex);
415 _InterlockedAnd16((ushort *)latch, ~Mutex);
421 // try to obtain write lock
423 // return 1 if obtained,
426 int bt_spinwritetry(BtSpinLatch *latch)
431 if( prev = __sync_fetch_and_or((ushort *)latch, Mutex), prev & Mutex )
434 if( prev = _InterlockedOr16((ushort *)latch, Mutex), prev & Mutex )
437 // take write access if all bits are clear
441 __sync_fetch_and_or ((ushort *)latch, Write);
443 _InterlockedOr16((ushort *)latch, Write);
447 __sync_fetch_and_and ((ushort *)latch, ~Mutex);
449 _InterlockedAnd16((ushort *)latch, ~Mutex);
456 void bt_spinreleasewrite(BtSpinLatch *latch)
459 __sync_fetch_and_and ((ushort *)latch, ~Write);
461 _InterlockedAnd16((ushort *)latch, ~Write);
465 // decrement reader count
467 void bt_spinreleaseread(BtSpinLatch *latch)
470 __sync_fetch_and_add((ushort *)latch, -Share);
472 _InterlockedExchangeAdd16 ((ushort *)latch, -Share);
476 // link latch table entry into latch hash table
478 void bt_latchlink (BtDb *bt, ushort hashidx, ushort victim, uid page_no)
480 BtLatchSet *set = bt->mgr->latchsets + victim;
482 if( set->next = bt->mgr->latchmgr->table[hashidx].slot )
483 bt->mgr->latchsets[set->next].prev = victim;
485 bt->mgr->latchmgr->table[hashidx].slot = victim;
486 set->page_no = page_no;
493 void bt_unpinlatch (BtLatchSet *set)
496 __sync_fetch_and_add(&set->pin, -1);
498 _InterlockedDecrement16 (&set->pin);
502 // find existing latchset or inspire new one
503 // return with latchset pinned
505 BtLatchSet *bt_pinlatch (BtDb *bt, uid page_no)
507 ushort hashidx = page_no % bt->mgr->latchmgr->latchhash;
508 ushort slot, avail = 0, victim, idx;
511 // obtain read lock on hash table entry
513 bt_spinreadlock(bt->mgr->latchmgr->table[hashidx].latch);
515 if( slot = bt->mgr->latchmgr->table[hashidx].slot ) do
517 set = bt->mgr->latchsets + slot;
518 if( page_no == set->page_no )
520 } while( slot = set->next );
524 __sync_fetch_and_add(&set->pin, 1);
526 _InterlockedIncrement16 (&set->pin);
530 bt_spinreleaseread (bt->mgr->latchmgr->table[hashidx].latch);
535 // try again, this time with write lock
537 bt_spinwritelock(bt->mgr->latchmgr->table[hashidx].latch);
539 if( slot = bt->mgr->latchmgr->table[hashidx].slot ) do
541 set = bt->mgr->latchsets + slot;
542 if( page_no == set->page_no )
544 if( !set->pin && !avail )
546 } while( slot = set->next );
548 // found our entry, or take over an unpinned one
550 if( slot || (slot = avail) ) {
551 set = bt->mgr->latchsets + slot;
553 __sync_fetch_and_add(&set->pin, 1);
555 _InterlockedIncrement16 (&set->pin);
557 set->page_no = page_no;
558 bt_spinreleasewrite(bt->mgr->latchmgr->table[hashidx].latch);
562 // see if there are any unused entries
564 victim = __sync_fetch_and_add (&bt->mgr->latchmgr->latchdeployed, 1) + 1;
566 victim = _InterlockedIncrement16 (&bt->mgr->latchmgr->latchdeployed);
569 if( victim < bt->mgr->latchmgr->latchtotal ) {
570 set = bt->mgr->latchsets + victim;
572 __sync_fetch_and_add(&set->pin, 1);
574 _InterlockedIncrement16 (&set->pin);
576 bt_latchlink (bt, hashidx, victim, page_no);
577 bt_spinreleasewrite (bt->mgr->latchmgr->table[hashidx].latch);
582 victim = __sync_fetch_and_add (&bt->mgr->latchmgr->latchdeployed, -1);
584 victim = _InterlockedDecrement16 (&bt->mgr->latchmgr->latchdeployed);
586 // find and reuse previous lock entry
590 victim = __sync_fetch_and_add(&bt->mgr->latchmgr->latchvictim, 1);
592 victim = _InterlockedIncrement16 (&bt->mgr->latchmgr->latchvictim) - 1;
594 // we don't use slot zero
596 if( victim %= bt->mgr->latchmgr->latchtotal )
597 set = bt->mgr->latchsets + victim;
601 // take control of our slot
602 // from other threads
604 if( set->pin || !bt_spinwritetry (set->busy) )
609 // try to get write lock on hash chain
610 // skip entry if not obtained
611 // or has outstanding locks
613 if( !bt_spinwritetry (bt->mgr->latchmgr->table[idx].latch) ) {
614 bt_spinreleasewrite (set->busy);
619 bt_spinreleasewrite (set->busy);
620 bt_spinreleasewrite (bt->mgr->latchmgr->table[idx].latch);
624 // unlink our available victim from its hash chain
627 bt->mgr->latchsets[set->prev].next = set->next;
629 bt->mgr->latchmgr->table[idx].slot = set->next;
632 bt->mgr->latchsets[set->next].prev = set->prev;
634 bt_spinreleasewrite (bt->mgr->latchmgr->table[idx].latch);
636 __sync_fetch_and_add(&set->pin, 1);
638 _InterlockedIncrement16 (&set->pin);
640 bt_latchlink (bt, hashidx, victim, page_no);
641 bt_spinreleasewrite (bt->mgr->latchmgr->table[hashidx].latch);
642 bt_spinreleasewrite (set->busy);
647 void bt_mgrclose (BtMgr *mgr)
652 // release mapped pages
653 // note that slot zero is never used
655 for( slot = 1; slot < mgr->poolmax; slot++ ) {
656 pool = mgr->pool + slot;
659 munmap (pool->map, (mgr->poolmask+1) << mgr->page_bits);
662 FlushViewOfFile(pool->map, 0);
663 UnmapViewOfFile(pool->map);
664 CloseHandle(pool->hmap);
670 munmap (mgr->latchsets, mgr->latchmgr->nlatchpage * mgr->page_size);
671 munmap (mgr->latchmgr, mgr->page_size);
673 FlushViewOfFile(mgr->latchmgr, 0);
674 UnmapViewOfFile(mgr->latchmgr);
675 CloseHandle(mgr->halloc);
682 free (mgr->pooladvise);
685 FlushFileBuffers(mgr->idx);
686 CloseHandle(mgr->idx);
687 GlobalFree (mgr->pool);
688 GlobalFree (mgr->hash);
689 GlobalFree (mgr->latch);
694 // close and release memory
696 void bt_close (BtDb *bt)
703 VirtualFree (bt->mem, 0, MEM_RELEASE);
708 // open/create new btree buffer manager
710 // call with file_name, BT_openmode, bits in page size (e.g. 16),
711 // size of mapped page pool (e.g. 8192)
713 BtMgr *bt_mgr (char *name, uint mode, uint bits, uint poolmax, uint segsize, uint hashsize)
715 uint lvl, attr, cacheblk, last, slot, idx;
716 uint nlatchpage, latchhash;
717 BtLatchMgr *latchmgr;
725 SYSTEM_INFO sysinfo[1];
728 // determine sanity of page size and buffer pool
730 if( bits > BT_maxbits )
732 else if( bits < BT_minbits )
736 return NULL; // must have buffer pool
739 mgr = calloc (1, sizeof(BtMgr));
741 mgr->idx = open ((char*)name, O_RDWR | O_CREAT, 0666);
744 return free(mgr), NULL;
746 cacheblk = 4096; // minimum mmap segment size for unix
749 mgr = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, sizeof(BtMgr));
750 attr = FILE_ATTRIBUTE_NORMAL;
751 mgr->idx = CreateFile(name, GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, attr, NULL);
753 if( mgr->idx == INVALID_HANDLE_VALUE )
754 return GlobalFree(mgr), NULL;
756 // normalize cacheblk to multiple of sysinfo->dwAllocationGranularity
757 GetSystemInfo(sysinfo);
758 cacheblk = sysinfo->dwAllocationGranularity;
762 latchmgr = malloc (BT_maxpage);
765 // read minimum page size to get root info
767 if( size = lseek (mgr->idx, 0L, 2) ) {
768 if( pread(mgr->idx, latchmgr, BT_minpage, 0) == BT_minpage )
769 bits = latchmgr->alloc->bits;
771 return free(mgr), free(latchmgr), NULL;
772 } else if( mode == BT_ro )
773 return free(latchmgr), bt_mgrclose (mgr), NULL;
775 latchmgr = VirtualAlloc(NULL, BT_maxpage, MEM_COMMIT, PAGE_READWRITE);
776 size = GetFileSize(mgr->idx, amt);
779 if( !ReadFile(mgr->idx, (char *)latchmgr, BT_minpage, amt, NULL) )
780 return bt_mgrclose (mgr), NULL;
781 bits = latchmgr->alloc->bits;
782 } else if( mode == BT_ro )
783 return bt_mgrclose (mgr), NULL;
786 mgr->page_size = 1 << bits;
787 mgr->page_bits = bits;
789 mgr->poolmax = poolmax;
792 if( cacheblk < mgr->page_size )
793 cacheblk = mgr->page_size;
795 // mask for partial memmaps
797 mgr->poolmask = (cacheblk >> bits) - 1;
799 // see if requested size of pages per memmap is greater
801 if( (1 << segsize) > mgr->poolmask )
802 mgr->poolmask = (1 << segsize) - 1;
806 while( (1 << mgr->seg_bits) <= mgr->poolmask )
809 mgr->hashsize = hashsize;
812 mgr->pool = calloc (poolmax, sizeof(BtPool));
813 mgr->hash = calloc (hashsize, sizeof(ushort));
814 mgr->latch = calloc (hashsize, sizeof(BtSpinLatch));
815 mgr->pooladvise = calloc (poolmax, (mgr->poolmask + 8) / 8);
817 mgr->pool = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, poolmax * sizeof(BtPool));
818 mgr->hash = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, hashsize * sizeof(ushort));
819 mgr->latch = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, hashsize * sizeof(BtSpinLatch));
825 // initialize an empty b-tree with latch page, root page, page of leaves
826 // and page(s) of latches
828 memset (latchmgr, 0, 1 << bits);
829 nlatchpage = BT_latchtable / (mgr->page_size / sizeof(BtLatchSet)) + 1;
830 bt_putid(latchmgr->alloc->right, MIN_lvl+1+nlatchpage);
831 latchmgr->alloc->bits = mgr->page_bits;
833 latchmgr->nlatchpage = nlatchpage;
834 latchmgr->latchtotal = nlatchpage * (mgr->page_size / sizeof(BtLatchSet));
836 // initialize latch manager
838 latchhash = (mgr->page_size - sizeof(BtLatchMgr)) / sizeof(BtHashEntry);
840 // size of hash table = total number of latchsets
842 if( latchhash > latchmgr->latchtotal )
843 latchhash = latchmgr->latchtotal;
845 latchmgr->latchhash = latchhash;
848 if( write (mgr->idx, latchmgr, mgr->page_size) < mgr->page_size )
849 return bt_mgrclose (mgr), NULL;
851 if( !WriteFile (mgr->idx, (char *)latchmgr, mgr->page_size, amt, NULL) )
852 return bt_mgrclose (mgr), NULL;
854 if( *amt < mgr->page_size )
855 return bt_mgrclose (mgr), NULL;
858 memset (latchmgr, 0, 1 << bits);
859 latchmgr->alloc->bits = mgr->page_bits;
861 for( lvl=MIN_lvl; lvl--; ) {
862 slotptr(latchmgr->alloc, 1)->off = mgr->page_size - 3;
863 bt_putid(slotptr(latchmgr->alloc, 1)->id, lvl ? MIN_lvl - lvl + 1 : 0); // next(lower) page number
864 key = keyptr(latchmgr->alloc, 1);
865 key->len = 2; // create stopper key
868 latchmgr->alloc->min = mgr->page_size - 3;
869 latchmgr->alloc->lvl = lvl;
870 latchmgr->alloc->cnt = 1;
871 latchmgr->alloc->act = 1;
873 if( write (mgr->idx, latchmgr, mgr->page_size) < mgr->page_size )
874 return bt_mgrclose (mgr), NULL;
876 if( !WriteFile (mgr->idx, (char *)latchmgr, mgr->page_size, amt, NULL) )
877 return bt_mgrclose (mgr), NULL;
879 if( *amt < mgr->page_size )
880 return bt_mgrclose (mgr), NULL;
884 // clear out latch manager locks
885 // and rest of pages to round out segment
887 memset(latchmgr, 0, mgr->page_size);
890 while( last <= ((MIN_lvl + 1 + nlatchpage) | mgr->poolmask) ) {
892 pwrite(mgr->idx, latchmgr, mgr->page_size, last << mgr->page_bits);
894 SetFilePointer (mgr->idx, last << mgr->page_bits, NULL, FILE_BEGIN);
895 if( !WriteFile (mgr->idx, (char *)latchmgr, mgr->page_size, amt, NULL) )
896 return bt_mgrclose (mgr), NULL;
897 if( *amt < mgr->page_size )
898 return bt_mgrclose (mgr), NULL;
905 flag = PROT_READ | PROT_WRITE;
906 mgr->latchmgr = mmap (0, mgr->page_size, flag, MAP_SHARED, mgr->idx, ALLOC_page * mgr->page_size);
907 if( mgr->latchmgr == MAP_FAILED )
908 return bt_mgrclose (mgr), NULL;
909 mgr->latchsets = (BtLatchSet *)mmap (0, mgr->latchmgr->nlatchpage * mgr->page_size, flag, MAP_SHARED, mgr->idx, LATCH_page * mgr->page_size);
910 if( mgr->latchsets == MAP_FAILED )
911 return bt_mgrclose (mgr), NULL;
913 flag = PAGE_READWRITE;
914 mgr->halloc = CreateFileMapping(mgr->idx, NULL, flag, 0, (BT_latchtable / (mgr->page_size / sizeof(BtLatchSet)) + 1 + LATCH_page) * mgr->page_size, NULL);
916 return bt_mgrclose (mgr), NULL;
918 flag = FILE_MAP_WRITE;
919 mgr->latchmgr = MapViewOfFile(mgr->halloc, flag, 0, 0, (BT_latchtable / (mgr->page_size / sizeof(BtLatchSet)) + 1 + LATCH_page) * mgr->page_size);
921 return GetLastError(), bt_mgrclose (mgr), NULL;
923 mgr->latchsets = (void *)((char *)mgr->latchmgr + LATCH_page * mgr->page_size);
929 VirtualFree (latchmgr, 0, MEM_RELEASE);
934 // open BTree access method
935 // based on buffer manager
937 BtDb *bt_open (BtMgr *mgr)
939 BtDb *bt = malloc (sizeof(*bt));
941 memset (bt, 0, sizeof(*bt));
944 bt->mem = malloc (3 *mgr->page_size);
946 bt->mem = VirtualAlloc(NULL, 3 * mgr->page_size, MEM_COMMIT, PAGE_READWRITE);
948 bt->frame = (BtPage)bt->mem;
949 bt->zero = (BtPage)(bt->mem + 1 * mgr->page_size);
950 bt->cursor = (BtPage)(bt->mem + 2 * mgr->page_size);
952 memset(bt->zero, 0, mgr->page_size);
956 // compare two keys, returning > 0, = 0, or < 0
957 // as the comparison value
959 int keycmp (BtKey key1, unsigned char *key2, uint len2)
961 uint len1 = key1->len;
964 if( ans = memcmp (key1->key, key2, len1 > len2 ? len2 : len1) )
977 // find segment in pool
978 // must be called with hashslot idx locked
979 // return NULL if not there
980 // otherwise return node
982 BtPool *bt_findpool(BtDb *bt, uid page_no, uint idx)
987 // compute start of hash chain in pool
989 if( slot = bt->mgr->hash[idx] )
990 pool = bt->mgr->pool + slot;
994 page_no &= ~bt->mgr->poolmask;
996 while( pool->basepage != page_no )
997 if( pool = pool->hashnext )
1005 // add segment to hash table
1007 void bt_linkhash(BtDb *bt, BtPool *pool, uid page_no, int idx)
1012 pool->hashprev = pool->hashnext = NULL;
1013 pool->basepage = page_no & ~bt->mgr->poolmask;
1016 if( slot = bt->mgr->hash[idx] ) {
1017 node = bt->mgr->pool + slot;
1018 pool->hashnext = node;
1019 node->hashprev = pool;
1022 bt->mgr->hash[idx] = pool->slot;
1025 // find best segment to evict from buffer pool
1027 BtPool *bt_findlru (BtDb *bt, uint hashslot)
1029 unsigned long long int target = ~0LL;
1030 BtPool *pool = NULL, *node;
1035 node = bt->mgr->pool + hashslot;
1037 // scan pool entries under hash table slot
1042 if( node->lru > target )
1046 } while( node = node->hashnext );
1051 // map new buffer pool segment to virtual memory
1053 BTERR bt_mapsegment(BtDb *bt, BtPool *pool, uid page_no)
1055 off64_t off = (page_no & ~bt->mgr->poolmask) << bt->mgr->page_bits;
1056 off64_t limit = off + ((bt->mgr->poolmask+1) << bt->mgr->page_bits);
1060 flag = PROT_READ | ( bt->mgr->mode == BT_ro ? 0 : PROT_WRITE );
1061 pool->map = mmap (0, (bt->mgr->poolmask+1) << bt->mgr->page_bits, flag, MAP_SHARED, bt->mgr->idx, off);
1062 if( pool->map == MAP_FAILED )
1063 return bt->err = BTERR_map;
1064 // clear out madvise issued bits
1065 memset (bt->mgr->pooladvise + pool->slot * ((bt->mgr->poolmask + 8) / 8), 0, (bt->mgr->poolmask + 8)/8);
1067 flag = ( bt->mgr->mode == BT_ro ? PAGE_READONLY : PAGE_READWRITE );
1068 pool->hmap = CreateFileMapping(bt->mgr->idx, NULL, flag, (DWORD)(limit >> 32), (DWORD)limit, NULL);
1070 return bt->err = BTERR_map;
1072 flag = ( bt->mgr->mode == BT_ro ? FILE_MAP_READ : FILE_MAP_WRITE );
1073 pool->map = MapViewOfFile(pool->hmap, flag, (DWORD)(off >> 32), (DWORD)off, (bt->mgr->poolmask+1) << bt->mgr->page_bits);
1075 return bt->err = BTERR_map;
1080 // calculate page within pool
1082 BtPage bt_page (BtDb *bt, BtPool *pool, uid page_no)
1084 uint subpage = (uint)(page_no & bt->mgr->poolmask); // page within mapping
1087 page = (BtPage)(pool->map + (subpage << bt->mgr->page_bits));
1090 uint idx = subpage / 8;
1091 uint bit = subpage % 8;
1093 if( ~((bt->mgr->pooladvise + pool->slot * ((bt->mgr->poolmask + 8)/8))[idx] >> bit) & 1 ) {
1094 madvise (page, bt->mgr->page_size, MADV_WILLNEED);
1095 (bt->mgr->pooladvise + pool->slot * ((bt->mgr->poolmask + 8)/8))[idx] |= 1 << bit;
1104 void bt_unpinpool (BtPool *pool)
1107 __sync_fetch_and_add(&pool->pin, -1);
1109 _InterlockedDecrement16 (&pool->pin);
1113 // find or place requested page in segment-pool
1114 // return pool table entry, incrementing pin
1116 BtPool *bt_pinpool(BtDb *bt, uid page_no)
1118 BtPool *pool, *node, *next;
1119 uint slot, idx, victim;
1122 // lock hash table chain
1124 idx = (uint)(page_no >> bt->mgr->seg_bits) % bt->mgr->hashsize;
1125 bt_spinreadlock (&bt->mgr->latch[idx]);
1127 // look up in hash table
1129 if( pool = bt_findpool(bt, page_no, idx) ) {
1131 __sync_fetch_and_add(&pool->pin, 1);
1133 _InterlockedIncrement16 (&pool->pin);
1135 bt_spinreleaseread (&bt->mgr->latch[idx]);
1140 // upgrade to write lock
1142 bt_spinreleaseread (&bt->mgr->latch[idx]);
1143 bt_spinwritelock (&bt->mgr->latch[idx]);
1145 // try to find page in pool with write lock
1147 if( pool = bt_findpool(bt, page_no, idx) ) {
1149 __sync_fetch_and_add(&pool->pin, 1);
1151 _InterlockedIncrement16 (&pool->pin);
1153 bt_spinreleasewrite (&bt->mgr->latch[idx]);
1158 // allocate a new pool node
1159 // and add to hash table
1162 slot = __sync_fetch_and_add(&bt->mgr->poolcnt, 1);
1164 slot = _InterlockedIncrement16 (&bt->mgr->poolcnt) - 1;
1167 if( ++slot < bt->mgr->poolmax ) {
1168 pool = bt->mgr->pool + slot;
1171 if( bt_mapsegment(bt, pool, page_no) )
1174 bt_linkhash(bt, pool, page_no, idx);
1176 __sync_fetch_and_add(&pool->pin, 1);
1178 _InterlockedIncrement16 (&pool->pin);
1180 bt_spinreleasewrite (&bt->mgr->latch[idx]);
1184 // pool table is full
1185 // find best pool entry to evict
1188 __sync_fetch_and_add(&bt->mgr->poolcnt, -1);
1190 _InterlockedDecrement16 (&bt->mgr->poolcnt);
1195 victim = __sync_fetch_and_add(&bt->mgr->evicted, 1);
1197 victim = _InterlockedIncrement16 (&bt->mgr->evicted) - 1;
1199 victim %= bt->mgr->hashsize;
1201 // try to get write lock
1202 // skip entry if not obtained
1204 if( !bt_spinwritetry (&bt->mgr->latch[victim]) )
1207 // if cache entry is empty
1208 // or no slots are unpinned
1211 if( !(pool = bt_findlru(bt, bt->mgr->hash[victim])) ) {
1212 bt_spinreleasewrite (&bt->mgr->latch[victim]);
1216 // unlink victim pool node from hash table
1218 if( node = pool->hashprev )
1219 node->hashnext = pool->hashnext;
1220 else if( node = pool->hashnext )
1221 bt->mgr->hash[victim] = node->slot;
1223 bt->mgr->hash[victim] = 0;
1225 if( node = pool->hashnext )
1226 node->hashprev = pool->hashprev;
1228 bt_spinreleasewrite (&bt->mgr->latch[victim]);
1230 // remove old file mapping
1232 munmap (pool->map, (bt->mgr->poolmask+1) << bt->mgr->page_bits);
1234 FlushViewOfFile(pool->map, 0);
1235 UnmapViewOfFile(pool->map);
1236 CloseHandle(pool->hmap);
1240 // create new pool mapping
1241 // and link into hash table
1243 if( bt_mapsegment(bt, pool, page_no) )
1246 bt_linkhash(bt, pool, page_no, idx);
1248 __sync_fetch_and_add(&pool->pin, 1);
1250 _InterlockedIncrement16 (&pool->pin);
1252 bt_spinreleasewrite (&bt->mgr->latch[idx]);
1257 // place write, read, or parent lock on requested page_no.
1258 // pin to buffer pool and return latchset pointer
1260 void bt_lockpage(BtLock mode, BtLatchSet *set)
1264 bt_spinreadlock (set->readwr);
1267 bt_spinwritelock (set->readwr);
1270 bt_spinreadlock (set->access);
1273 bt_spinwritelock (set->access);
1276 bt_spinwritelock (set->parent);
1281 // remove write, read, or parent lock on requested page_no.
1283 void bt_unlockpage(BtLock mode, BtLatchSet *set)
1287 bt_spinreleaseread (set->readwr);
1290 bt_spinreleasewrite (set->readwr);
1293 bt_spinreleaseread (set->access);
1296 bt_spinreleasewrite (set->access);
1299 bt_spinreleasewrite (set->parent);
1304 // allocate a new page and write page into it
1306 uid bt_newpage(BtDb *bt, BtPage page)
1314 // lock allocation page
1316 bt_spinwritelock(bt->mgr->latchmgr->lock);
1318 // use empty chain first
1319 // else allocate empty page
1321 if( new_page = bt_getid(bt->mgr->latchmgr->alloc[1].right) ) {
1322 if( pool = bt_pinpool (bt, new_page) )
1323 pmap = bt_page (bt, pool, new_page);
1326 bt_putid(bt->mgr->latchmgr->alloc[1].right, bt_getid(pmap->right));
1327 bt_unpinpool (pool);
1330 new_page = bt_getid(bt->mgr->latchmgr->alloc->right);
1331 bt_putid(bt->mgr->latchmgr->alloc->right, new_page+1);
1335 // if writing first page of pool block, zero last page in the block
1337 if ( !reuse && bt->mgr->poolmask > 0 && (new_page & bt->mgr->poolmask) == 0 )
1339 // use zero buffer to write zeros
1340 if ( pwrite(bt->mgr->idx,bt->zero, bt->mgr->page_size, (new_page | bt->mgr->poolmask) << bt->mgr->page_bits) < bt->mgr->page_size )
1341 return bt->err = BTERR_wrt, 0;
1344 // unlock allocation latch
1346 bt_spinreleasewrite(bt->mgr->latchmgr->lock);
1348 if ( pwrite(bt->mgr->idx, page, bt->mgr->page_size, new_page << bt->mgr->page_bits) < bt->mgr->page_size )
1349 return bt->err = BTERR_wrt, 0;
1352 // unlock allocation latch
1354 bt_spinreleasewrite(bt->mgr->latchmgr->lock);
1356 // bring new page into pool and copy page.
1357 // this will extend the file into the new pages.
1358 // NB -- no latch required
1360 if( pool = bt_pinpool (bt, new_page) )
1361 pmap = bt_page (bt, pool, new_page);
1365 memcpy(pmap, page, bt->mgr->page_size);
1366 bt_unpinpool (pool);
1371 // find slot in page for given key at a given level
1373 int bt_findslot (BtDb *bt, unsigned char *key, uint len)
1375 uint diff, higher = bt->page->cnt, low = 1, slot;
1377 // low is the lowest candidate, higher is already
1378 // tested as .ge. the given key, loop ends when they meet
1380 while( diff = higher - low ) {
1381 slot = low + ( diff >> 1 );
1382 if( keycmp (keyptr(bt->page, slot), key, len) < 0 )
1391 // find and load page at given level for given key
1392 // leave page rd or wr locked as requested
1394 int bt_loadpage (BtDb *bt, unsigned char *key, uint len, uint lvl, BtLock lock)
1396 uid page_no = ROOT_page, prevpage = 0;
1397 BtLatchSet *set, *prevset;
1398 uint drill = 0xff, slot;
1399 uint mode, prevmode;
1402 // start at root of btree and drill down
1405 // determine lock mode of drill level
1406 mode = (lock == BtLockWrite) && (drill == lvl) ? BtLockWrite : BtLockRead;
1408 // obtain latch set for this page
1410 bt->set = bt_pinlatch (bt, page_no);
1411 bt->page_no = page_no;
1413 // pin page contents
1415 if( bt->pool = bt_pinpool (bt, page_no) )
1416 bt->page = bt_page (bt, bt->pool, page_no);
1420 // obtain access lock using lock chaining with Access mode
1422 if( page_no > ROOT_page )
1423 bt_lockpage(BtLockAccess, bt->set);
1425 // now unlock and unpin our (possibly foster) parent
1428 bt_unlockpage(prevmode, prevset);
1429 bt_unpinlatch (prevset);
1430 bt_unpinpool (prevpool);
1434 // obtain read lock using lock chaining
1436 bt_lockpage(mode, bt->set);
1438 if( page_no > ROOT_page )
1439 bt_unlockpage(BtLockAccess, bt->set);
1441 // re-read and re-lock root after determining actual level of root
1443 if( page_no == ROOT_page )
1444 if( bt->page->lvl != drill) {
1445 drill = bt->page->lvl;
1447 if( lock == BtLockWrite && drill == lvl ) {
1448 bt_unlockpage(mode, bt->set);
1449 bt_unpinlatch (bt->set);
1450 bt_unpinpool (bt->pool);
1455 prevpage = bt->page_no;
1456 prevpool = bt->pool;
1460 // find key on page at this level
1461 // and either descend to requested level
1462 // or return key slot
1464 slot = bt_findslot (bt, key, len);
1466 // is this slot < foster child area
1467 // on the requested level?
1469 // if so, return actual slot even if dead
1471 if( slot <= bt->page->cnt - bt->page->foster )
1475 // find next active slot
1477 // note: foster children are never dead
1478 // nor fence keys for interiour nodes
1480 while( slotptr(bt->page, slot)->dead )
1481 if( slot++ < bt->page->cnt )
1484 return bt->err = BTERR_struct, 0; // last key shouldn't be deleted
1486 // is this slot < foster child area
1487 // if so, drill to next level
1489 if( slot <= bt->page->cnt - bt->page->foster )
1492 // continue right onto foster child
1493 // or down to next level.
1495 page_no = bt_getid(slotptr(bt->page, slot)->id);
1499 // return error on end of chain
1501 bt->err = BTERR_struct;
1502 return 0; // return error
1505 // find and delete key on page by marking delete flag bit
1506 // when leaf page becomes empty, delete it from the btree
1508 BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len)
1510 unsigned char leftkey[256];
1511 BtLatchSet *rset, *set;
1512 BtPool *pool, *rpool;
1518 if( slot = bt_loadpage (bt, key, len, 0, BtLockWrite) )
1519 ptr = keyptr(bt->page, slot);
1523 // if key is found delete it, otherwise ignore request
1524 // note that fence keys of interiour nodes are not deleted.
1526 if( bt->found = !keycmp (ptr, key, len) )
1527 if( bt->found = slotptr(bt->page, slot)->dead == 0 ) {
1528 slotptr(bt->page,slot)->dead = 1;
1529 if( slot < bt->page->cnt )
1530 bt->page->dirty = 1;
1534 page_no = bt->page_no;
1539 // return if page is not empty or not found
1541 if( page->act || !bt->found ) {
1542 bt_unlockpage(BtLockWrite, set);
1543 bt_unpinlatch (set);
1544 bt_unpinpool (pool);
1548 // cache copy of fence key of empty node
1550 ptr = keyptr(page, page->cnt);
1551 memcpy(leftkey, ptr, ptr->len + 1);
1553 // release write lock on empty node
1554 // obtain Parent lock
1556 bt_unlockpage(BtLockWrite, set);
1557 bt_lockpage(BtLockParent, set);
1559 // load and lock parent to see
1560 // if delete of empty node is OK
1561 // ie, not a fence key of parent
1564 if( slot = bt_loadpage (bt, leftkey+1, *leftkey, 1, BtLockWrite) )
1565 ptr = keyptr(bt->page, slot);
1569 // does parent level contain our fence key yet?
1570 // and is it free of foster children?
1572 if( !bt->page->foster )
1573 if( !keycmp (ptr, leftkey+1, *leftkey) )
1576 bt_unlockpage(BtLockWrite, bt->set);
1577 bt_unpinlatch (bt->set);
1578 bt_unpinpool (bt->pool);
1586 // find our left fence key
1588 while( slotptr(bt->page, slot)->dead )
1589 if( slot++ < bt->page->cnt )
1592 return bt->err = BTERR_struct; // last key shouldn't be deleted
1594 // now we have both parent and child
1596 bt_lockpage(BtLockDelete, set);
1597 bt_lockpage(BtLockWrite, set);
1599 // return if page has no right sibling within parent
1600 // or if empty node is no longer empty
1602 if( page->act || slot == bt->page->cnt ) {
1604 bt_unlockpage(BtLockWrite, bt->set);
1605 bt_unpinlatch (bt->set);
1606 bt_unpinpool (bt->pool);
1608 bt_unlockpage(BtLockParent, set);
1609 bt_unlockpage(BtLockDelete, set);
1610 bt_unlockpage(BtLockWrite, set);
1611 bt_unpinlatch (set);
1612 bt_unpinpool (pool);
1616 // lock and map our right page
1617 // note that it cannot be our foster child
1618 // since the our node is empty
1620 right = bt_getid(page->right);
1622 if( rpool = bt_pinpool (bt, right) )
1623 rpage = bt_page (bt, rpool, right);
1627 rset = bt_pinlatch (bt, right);
1628 bt_lockpage(BtLockWrite, rset);
1629 bt_lockpage(BtLockDelete, rset);
1631 // pull contents of right page into empty page
1633 memcpy (page, rpage, bt->mgr->page_size);
1635 // delete left parent slot for old empty page
1636 // and redirect right parent slot to it
1639 bt->page->dirty = 1;
1640 slotptr(bt->page, slot)->dead = 1;
1642 while( slot++ < bt->page->cnt )
1643 if( !slotptr(bt->page, slot)->dead )
1646 bt_putid(slotptr(bt->page,slot)->id, page_no);
1648 // release parent level lock
1649 // and our empty node lock
1651 bt_unlockpage(BtLockWrite, set);
1652 bt_unlockpage(BtLockWrite, bt->set);
1653 bt_unpinlatch (bt->set);
1654 bt_unpinpool (bt->pool);
1656 // add killed right block to free chain
1659 bt_spinwritelock(bt->mgr->latchmgr->lock);
1661 // store free chain in allocation page second right
1662 bt_putid(rpage->right, bt_getid(bt->mgr->latchmgr->alloc[1].right));
1663 bt_putid(bt->mgr->latchmgr->alloc[1].right, right);
1665 // unlock latch mgr and right page
1667 bt_spinreleasewrite(bt->mgr->latchmgr->lock);
1669 bt_unlockpage(BtLockWrite, rset);
1670 bt_unlockpage(BtLockDelete, rset);
1671 bt_unpinlatch (rset);
1672 bt_unpinpool (rpool);
1674 // remove ParentModify lock
1676 bt_unlockpage(BtLockParent, set);
1677 bt_unlockpage(BtLockDelete, set);
1678 bt_unpinlatch (set);
1679 bt_unpinpool (pool);
1683 // find key in leaf level and return row-id
1685 uid bt_findkey (BtDb *bt, unsigned char *key, uint len)
1691 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1692 ptr = keyptr(bt->page, slot);
1696 // if key exists, return row-id
1697 // otherwise return 0
1699 if( slot <= bt->page->cnt && !keycmp (ptr, key, len) )
1700 id = bt_getid(slotptr(bt->page,slot)->id);
1704 bt_unlockpage (BtLockRead, bt->set);
1705 bt_unpinlatch (bt->set);
1706 bt_unpinpool (bt->pool);
1710 // check page for space available,
1711 // clean if necessary and return
1712 // 0 - page needs splitting
1713 // >0 new slot value
1715 uint bt_cleanpage(BtDb *bt, uint amt, uint slot)
1717 uint nxt = bt->mgr->page_size;
1718 BtPage page = bt->page;
1719 uint cnt = 0, idx = 0;
1720 uint max = page->cnt;
1724 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1727 // skip cleanup if nothing to reclaim
1732 memcpy (bt->frame, page, bt->mgr->page_size);
1734 // skip page info and set rest of page to zero
1736 memset (page+1, 0, bt->mgr->page_size - sizeof(*page));
1740 // try cleaning up page first
1742 // always leave fence key in the array
1743 // otherwise, remove deleted key
1745 // note: foster children are never dead
1746 // nor are fence keys for interiour nodes
1748 while( cnt++ < max ) {
1751 else if( cnt < max && slotptr(bt->frame,cnt)->dead )
1756 key = keyptr(bt->frame, cnt);
1757 nxt -= key->len + 1;
1758 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1761 memcpy(slotptr(page, ++idx)->id, slotptr(bt->frame, cnt)->id, BtId);
1762 if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
1764 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1765 slotptr(page, idx)->off = nxt;
1771 // see if page has enough space now, or does it need splitting?
1773 if( page->min >= (idx+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1779 // add key to current page
1780 // page must already be writelocked
1782 void bt_addkeytopage (BtDb *bt, uint slot, unsigned char *key, uint len, uid id, uint tod)
1784 BtPage page = bt->page;
1787 // find next available dead slot and copy key onto page
1788 // note that foster children on the page are never dead
1790 // look for next hole, but stay back from the fence key
1792 for( idx = slot; idx < page->cnt; idx++ )
1793 if( slotptr(page, idx)->dead )
1796 if( idx == page->cnt )
1801 // now insert key into array before slot
1804 *slotptr(page, idx) = *slotptr(page, idx -1), idx--;
1806 page->min -= len + 1;
1807 ((unsigned char *)page)[page->min] = len;
1808 memcpy ((unsigned char *)page + page->min +1, key, len );
1810 bt_putid(slotptr(page,slot)->id, id);
1811 slotptr(page, slot)->off = page->min;
1812 slotptr(page, slot)->tod = tod;
1813 slotptr(page, slot)->dead = 0;
1816 // split the root and raise the height of the btree
1817 // call with current page locked and page no of foster child
1818 // return with current page (root) unlocked
1820 BTERR bt_splitroot(BtDb *bt, uid right)
1822 uint nxt = bt->mgr->page_size;
1823 unsigned char fencekey[256];
1824 BtPage root = bt->page;
1828 // Obtain an empty page to use, and copy the left page
1829 // contents into it from the root. Strip foster child key.
1830 // (it's the stopper key)
1832 memset (slotptr(root, root->cnt), 0, sizeof(BtSlot));
1838 // Save left fence key.
1840 key = keyptr(root, root->cnt);
1841 memcpy (fencekey, key, key->len + 1);
1843 // copy the lower keys into a new left page
1845 if( !(new_page = bt_newpage(bt, root)) )
1848 // preserve the page info at the bottom
1849 // and set rest of the root to zero
1851 memset (root+1, 0, bt->mgr->page_size - sizeof(*root));
1853 // insert left fence key on empty newroot page
1855 nxt -= *fencekey + 1;
1856 memcpy ((unsigned char *)root + nxt, fencekey, *fencekey + 1);
1857 bt_putid(slotptr(root, 1)->id, new_page);
1858 slotptr(root, 1)->off = nxt;
1860 // insert stopper key on newroot page
1861 // and increase the root height
1867 memcpy ((unsigned char *)root + nxt, fencekey, *fencekey + 1);
1868 bt_putid(slotptr(root, 2)->id, right);
1869 slotptr(root, 2)->off = nxt;
1871 bt_putid(root->right, 0);
1872 root->min = nxt; // reset lowest used offset and key count
1877 // release and unpin root (bt->page)
1879 bt_unlockpage(BtLockWrite, bt->set);
1880 bt_unpinlatch (bt->set);
1881 bt_unpinpool (bt->pool);
1885 // split already locked full node
1886 // in current page variables
1887 // return unlocked and unpinned.
1889 BTERR bt_splitpage (BtDb *bt)
1891 uint slot, cnt, idx, max, nxt = bt->mgr->page_size;
1892 unsigned char fencekey[256];
1893 uid page_no = bt->page_no;
1894 BtLatchSet *set = bt->set;
1895 BtPool *pool = bt->pool;
1896 BtPage page = bt->page;
1897 uint tod = time(NULL);
1898 uint lvl = page->lvl;
1899 uid new_page, right;
1902 // initialize frame buffer for right node
1904 memset (bt->frame, 0, bt->mgr->page_size);
1905 max = page->cnt - page->foster;
1906 tod = (uint)time(NULL);
1910 // split higher half of keys to bt->frame
1911 // leaving old foster children in the left node,
1912 // and adding a new foster child there.
1914 while( cnt++ < max ) {
1915 key = keyptr(page, cnt);
1916 nxt -= key->len + 1;
1917 memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
1918 memcpy(slotptr(bt->frame,++idx)->id, slotptr(page,cnt)->id, BtId);
1919 if( !(slotptr(bt->frame, idx)->dead = slotptr(page, cnt)->dead) )
1921 slotptr(bt->frame, idx)->tod = slotptr(page, cnt)->tod;
1922 slotptr(bt->frame, idx)->off = nxt;
1925 // transfer right link node to new right node
1927 if( page_no > ROOT_page ) {
1928 right = bt_getid (page->right);
1929 bt_putid(bt->frame->right, right);
1932 bt->frame->bits = bt->mgr->page_bits;
1933 bt->frame->min = nxt;
1934 bt->frame->cnt = idx;
1935 bt->frame->lvl = lvl;
1937 // get new free page and write right frame to it.
1939 if( !(new_page = bt_newpage(bt, bt->frame)) )
1942 // remember fence key for new right page to add
1943 // as foster child to the left node
1945 key = keyptr(bt->frame, idx);
1946 memcpy (fencekey, key, key->len + 1);
1948 // update lower keys and foster children to continue in old page
1950 memcpy (bt->frame, page, bt->mgr->page_size);
1951 memset (page+1, 0, bt->mgr->page_size - sizeof(*page));
1952 nxt = bt->mgr->page_size;
1958 // assemble page of smaller keys
1959 // to remain in the old page
1961 while( cnt++ < max / 2 ) {
1962 key = keyptr(bt->frame, cnt);
1963 nxt -= key->len + 1;
1964 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1965 memcpy (slotptr(page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
1966 if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
1968 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1969 slotptr(page, idx)->off = nxt;
1972 // insert new foster child for right page in queue
1973 // before any of the current foster children
1975 nxt -= *fencekey + 1;
1976 memcpy ((unsigned char *)page + nxt, fencekey, *fencekey + 1);
1978 bt_putid (slotptr(page,++idx)->id, new_page);
1979 slotptr(page, idx)->tod = tod;
1980 slotptr(page, idx)->off = nxt;
1984 // continue with old foster child keys
1985 // note that none will be dead
1987 cnt = bt->frame->cnt - bt->frame->foster;
1989 while( cnt++ < bt->frame->cnt ) {
1990 key = keyptr(bt->frame, cnt);
1991 nxt -= key->len + 1;
1992 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1993 memcpy (slotptr(page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
1994 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1995 slotptr(page, idx)->off = nxt;
2002 // link new right page
2004 bt_putid (page->right, new_page);
2006 // if current page is the root page, split it
2008 if( page_no == ROOT_page )
2009 return bt_splitroot (bt, new_page);
2011 // release wr lock on our page
2013 bt_unlockpage (BtLockWrite, set);
2015 // obtain ParentModification lock for current page
2016 // to fix new fence key and oldest foster child on page
2018 bt_lockpage (BtLockParent, set);
2020 // get our new fence key to insert in parent node
2022 bt_lockpage (BtLockRead, set);
2024 key = keyptr(page, page->cnt-1);
2025 memcpy (fencekey, key, key->len+1);
2027 bt_unlockpage (BtLockRead, set);
2029 if( bt_insertkey (bt, fencekey + 1, *fencekey, page_no, tod, lvl + 1) )
2032 // lock our page for writing
2034 bt_lockpage (BtLockRead, set);
2036 // switch old parent key from us to our oldest foster child
2038 key = keyptr(page, page->cnt);
2039 memcpy (fencekey, key, key->len+1);
2041 new_page = bt_getid (slotptr(page, page->cnt)->id);
2042 bt_unlockpage (BtLockRead, set);
2044 if( bt_insertkey (bt, fencekey + 1, *fencekey, new_page, tod, lvl + 1) )
2047 // now that it has its own parent pointer,
2048 // remove oldest foster child from our page
2050 bt_lockpage (BtLockWrite, set);
2051 memset (slotptr(page, page->cnt), 0, sizeof(BtSlot));
2059 bt_unlockpage (BtLockWrite, set);
2060 bt_unlockpage (BtLockParent, set);
2061 bt_unpinlatch (set);
2062 bt_unpinpool (pool);
2066 // Insert new key into the btree at leaf level.
2068 BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uid id, uint tod, uint lvl)
2075 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
2076 ptr = keyptr(bt->page, slot);
2080 bt->err = BTERR_ovflw;
2084 // if key already exists, update id and return
2088 if( !keycmp (ptr, key, len) ) {
2089 if( slotptr(page, slot)->dead )
2091 slotptr(page, slot)->dead = 0;
2092 slotptr(page, slot)->tod = tod;
2093 bt_putid(slotptr(page,slot)->id, id);
2094 bt_unlockpage(BtLockWrite, bt->set);
2095 bt_unpinlatch (bt->set);
2096 bt_unpinpool (bt->pool);
2100 // check if page has enough space
2102 if( slot = bt_cleanpage (bt, len, slot) )
2105 if( bt_splitpage (bt) )
2109 bt_addkeytopage (bt, slot, key, len, id, tod);
2111 bt_unlockpage (BtLockWrite, bt->set);
2112 bt_unpinlatch (bt->set);
2113 bt_unpinpool (bt->pool);
2117 // cache page of keys into cursor and return starting slot for given key
2119 uint bt_startkey (BtDb *bt, unsigned char *key, uint len)
2123 // cache page for retrieval
2124 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
2125 memcpy (bt->cursor, bt->page, bt->mgr->page_size);
2127 bt->cursor_page = bt->page_no;
2129 bt_unlockpage(BtLockRead, bt->set);
2130 bt_unpinlatch (bt->set);
2131 bt_unpinpool (bt->pool);
2135 // return next slot for cursor page
2136 // or slide cursor right into next page
2138 uint bt_nextkey (BtDb *bt, uint slot)
2146 right = bt_getid(bt->cursor->right);
2147 while( slot++ < bt->cursor->cnt - bt->cursor->foster )
2148 if( slotptr(bt->cursor,slot)->dead )
2150 else if( right || (slot < bt->cursor->cnt - bt->cursor->foster) )
2158 bt->cursor_page = right;
2159 if( pool = bt_pinpool (bt, right) )
2160 page = bt_page (bt, pool, right);
2164 set = bt_pinlatch (bt, right);
2165 bt_lockpage(BtLockRead, set);
2167 memcpy (bt->cursor, page, bt->mgr->page_size);
2169 bt_unlockpage(BtLockRead, set);
2170 bt_unpinlatch (set);
2171 bt_unpinpool (pool);
2178 BtKey bt_key(BtDb *bt, uint slot)
2180 return keyptr(bt->cursor, slot);
2183 uid bt_uid(BtDb *bt, uint slot)
2185 return bt_getid(slotptr(bt->cursor,slot)->id);
2188 uint bt_tod(BtDb *bt, uint slot)
2190 return slotptr(bt->cursor,slot)->tod;
2196 void bt_latchaudit (BtDb *bt)
2198 ushort idx, hashidx;
2205 for( idx = 1; idx < bt->mgr->latchmgr->latchdeployed; idx++ ) {
2206 set = bt->mgr->latchsets + idx;
2207 if( *(ushort *)set->readwr || *(ushort *)set->access || *(ushort *)set->parent ) {
2208 fprintf(stderr, "latchset %d locked for page %6x\n", idx, set->page_no);
2209 *(ushort *)set->readwr = 0;
2210 *(ushort *)set->access = 0;
2211 *(ushort *)set->parent = 0;
2214 fprintf(stderr, "latchset %d pinned\n", idx);
2219 for( hashidx = 0; hashidx < bt->mgr->latchmgr->latchhash; hashidx++ ) {
2220 if( *(ushort *)bt->mgr->latchmgr->table[hashidx].latch )
2221 fprintf(stderr, "latchmgr locked\n");
2222 if( idx = bt->mgr->latchmgr->table[hashidx].slot ) do {
2223 set = bt->mgr->latchsets + idx;
2224 if( *(ushort *)set->readwr || *(ushort *)set->access || *(ushort *)set->parent )
2225 fprintf(stderr, "latchset %d locked\n", idx);
2226 if( set->hash != hashidx )
2227 fprintf(stderr, "latchset %d wrong hashidx\n", idx);
2229 fprintf(stderr, "latchset %d pinned\n", idx);
2230 } while( idx = set->next );
2232 page_no = bt_getid(bt->mgr->latchmgr->alloc[1].right);
2235 fprintf(stderr, "free: %.6x\n", (uint)page_no);
2236 pool = bt_pinpool (bt, page_no);
2237 page = bt_page (bt, pool, page_no);
2238 page_no = bt_getid(page->right);
2239 bt_unpinpool (pool);
2251 // standalone program to index file of keys
2252 // then list them onto std-out
2255 void *index_file (void *arg)
2257 uint __stdcall index_file (void *arg)
2260 int line = 0, found = 0, cnt = 0;
2261 uid next, page_no = LEAF_page; // start on first page of leaves
2262 unsigned char key[256];
2263 ThreadArg *args = arg;
2264 int ch, len = 0, slot;
2273 bt = bt_open (args->mgr);
2276 switch(args->type | 0x20)
2279 fprintf(stderr, "started latch mgr audit\n");
2281 fprintf(stderr, "finished latch mgr audit\n");
2284 fprintf(stderr, "started indexing for %s\n", args->infile);
2285 if( in = fopen (args->infile, "rb") )
2286 while( ch = getc(in), ch != EOF )
2291 if( args->num == 1 )
2292 sprintf((char *)key+len, "%.9d", 1000000000 - line), len += 9;
2294 else if( args->num )
2295 sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
2297 if( bt_insertkey (bt, key, len, line, *tod, 0) )
2298 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2301 else if( len < 255 )
2303 fprintf(stderr, "finished %s for %d keys\n", args->infile, line);
2307 fprintf(stderr, "started deleting keys for %s\n", args->infile);
2308 if( in = fopen (args->infile, "rb") )
2309 while( ch = getc(in), ch != EOF )
2313 if( args->num == 1 )
2314 sprintf((char *)key+len, "%.9d", 1000000000 - line), len += 9;
2316 else if( args->num )
2317 sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
2319 if( bt_deletekey (bt, key, len) )
2320 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2323 else if( len < 255 )
2325 fprintf(stderr, "finished %s for keys, %d \n", args->infile, line);
2329 fprintf(stderr, "started finding keys for %s\n", args->infile);
2330 if( in = fopen (args->infile, "rb") )
2331 while( ch = getc(in), ch != EOF )
2335 if( args->num == 1 )
2336 sprintf((char *)key+len, "%.9d", 1000000000 - line), len += 9;
2338 else if( args->num )
2339 sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
2341 if( bt_findkey (bt, key, len) )
2344 fprintf(stderr, "Error %d Syserr %d Line: %d\n", bt->err, errno, line), exit(0);
2347 else if( len < 255 )
2349 fprintf(stderr, "finished %s for %d keys, found %d\n", args->infile, line, found);
2355 fprintf(stderr, "started reading\n");
2357 if( slot = bt_startkey (bt, key, len) )
2360 fprintf(stderr, "Error %d in StartKey. Syserror: %d\n", bt->err, errno), exit(0);
2362 while( slot = bt_nextkey (bt, slot) ) {
2363 ptr = bt_key(bt, slot);
2364 fwrite (ptr->key, ptr->len, 1, stdout);
2365 fputc ('\n', stdout);
2371 fprintf(stderr, "started reading\n");
2374 if( pool = bt_pinpool (bt, page_no) )
2375 page = bt_page (bt, pool, page_no);
2378 set = bt_pinlatch (bt, page_no);
2379 bt_lockpage (BtLockRead, set);
2381 next = bt_getid (page->right);
2382 bt_unlockpage (BtLockRead, set);
2383 bt_unpinlatch (set);
2384 bt_unpinpool (pool);
2385 } while( page_no = next );
2387 cnt--; // remove stopper key
2388 fprintf(stderr, " Total keys read %d\n", cnt);
2400 typedef struct timeval timer;
2402 int main (int argc, char **argv)
2404 int idx, cnt, len, slot, err;
2405 int segsize, bits = 16;
2410 time_t start[1], stop[1];
2423 fprintf (stderr, "Usage: %s idx_file Read/Write/Scan/Delete/Find [page_bits mapped_segments seg_bits line_numbers src_file1 src_file2 ... ]\n", argv[0]);
2424 fprintf (stderr, " where page_bits is the page size in bits\n");
2425 fprintf (stderr, " mapped_segments is the number of mmap segments in buffer pool\n");
2426 fprintf (stderr, " seg_bits is the size of individual segments in buffer pool in pages in bits\n");
2427 fprintf (stderr, " line_numbers = 1 to append line numbers to keys\n");
2428 fprintf (stderr, " src_file1 thru src_filen are files of keys separated by newline\n");
2433 gettimeofday(&start, NULL);
2439 bits = atoi(argv[3]);
2442 poolsize = atoi(argv[4]);
2445 fprintf (stderr, "Warning: no mapped_pool\n");
2447 if( poolsize > 65535 )
2448 fprintf (stderr, "Warning: mapped_pool > 65535 segments\n");
2451 segsize = atoi(argv[5]);
2453 segsize = 4; // 16 pages per mmap segment
2456 num = atoi(argv[6]);
2460 threads = malloc (cnt * sizeof(pthread_t));
2462 threads = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, cnt * sizeof(HANDLE));
2464 args = malloc (cnt * sizeof(ThreadArg));
2466 mgr = bt_mgr ((argv[1]), BT_rw, bits, poolsize, segsize, poolsize / 8);
2469 fprintf(stderr, "Index Open Error %s\n", argv[1]);
2475 for( idx = 0; idx < cnt; idx++ ) {
2476 args[idx].infile = argv[idx + 7];
2477 args[idx].type = argv[2][0];
2478 args[idx].mgr = mgr;
2479 args[idx].num = num;
2480 args[idx].idx = idx;
2482 if( err = pthread_create (threads + idx, NULL, index_file, args + idx) )
2483 fprintf(stderr, "Error creating thread %d\n", err);
2485 threads[idx] = (HANDLE)_beginthreadex(NULL, 65536, index_file, args + idx, 0, NULL);
2489 // wait for termination
2492 for( idx = 0; idx < cnt; idx++ )
2493 pthread_join (threads[idx], NULL);
2494 gettimeofday(&stop, NULL);
2495 real_time = 1000.0 * ( stop.tv_sec - start.tv_sec ) + 0.001 * (stop.tv_usec - start.tv_usec );
2497 WaitForMultipleObjects (cnt, threads, TRUE, INFINITE);
2499 for( idx = 0; idx < cnt; idx++ )
2500 CloseHandle(threads[idx]);
2503 real_time = 1000 * (*stop - *start);
2505 fprintf(stderr, " Time to complete: %.2f seconds\n", real_time/1000);