1 // btree version threadskv1 sched_yield version
2 // with reworked bt_deletekey code
3 // and phase-fair reader writer lock
6 // author: karl malbrain, malbrain@cal.berkeley.edu
9 This work, including the source code, documentation
10 and related data, is placed into the public domain.
12 The orginal author is Karl Malbrain.
14 THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
15 OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
16 MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
17 ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
18 RESULTING FROM THE USE, MODIFICATION, OR
19 REDISTRIBUTION OF THIS SOFTWARE.
22 // Please see the project home page for documentation
23 // code.google.com/p/high-concurrency-btree
25 #define _FILE_OFFSET_BITS 64
26 #define _LARGEFILE64_SOURCE
42 #define WIN32_LEAN_AND_MEAN
56 typedef unsigned long long uid;
59 typedef unsigned long long off64_t;
60 typedef unsigned short ushort;
61 typedef unsigned int uint;
64 #define BT_latchtable 128 // number of latch manager slots
66 #define BT_ro 0x6f72 // ro
67 #define BT_rw 0x7772 // rw
69 #define BT_maxbits 24 // maximum page size in bits
70 #define BT_minbits 9 // minimum page size in bits
71 #define BT_minpage (1 << BT_minbits) // minimum page size
72 #define BT_maxpage (1 << BT_maxbits) // maximum page size
75 There are five lock types for each node in three independent sets:
76 1. (set 1) AccessIntent: Sharable. Going to Read the node. Incompatible with NodeDelete.
77 2. (set 1) NodeDelete: Exclusive. About to release the node. Incompatible with AccessIntent.
78 3. (set 2) ReadLock: Sharable. Read the node. Incompatible with WriteLock.
79 4. (set 2) WriteLock: Exclusive. Modify the node. Incompatible with ReadLock and other WriteLocks.
80 5. (set 3) ParentModification: Exclusive. Change the node's parent keys. Incompatible with another ParentModification.
91 // definition for phase-fair reader/writer lock implementation
105 // definition for spin latch implementation
107 // exclusive is set for write access
108 // share is count of read accessors
109 // grant write lock when share == 0
111 volatile typedef struct {
122 // hash table entries
125 BtSpinLatch latch[1];
126 volatile ushort slot; // Latch table entry at head of chain
129 // latch manager table structure
132 RWLock readwr[1]; // read/write page lock
133 RWLock access[1]; // Access Intent/Page delete
134 RWLock parent[1]; // Posting of fence key in parent
135 BtSpinLatch busy[1]; // slot is being moved between chains
136 volatile ushort next; // next entry in hash table chain
137 volatile ushort prev; // prev entry in hash table chain
138 volatile ushort pin; // number of outstanding locks
139 volatile ushort hash; // hash slot entry is under
140 volatile uid page_no; // latch set page number
143 // Define the length of the page and key pointers
147 // Page key slot definition.
149 // If BT_maxbits is 15 or less, you can save 2 bytes
150 // for each key stored by making the two uints
153 // Keys are marked dead, but remain on the page until
154 // it cleanup is called. The fence key (highest key) for
155 // the page is always present, even after cleanup.
158 uint off:BT_maxbits; // page offset for key start
159 uint dead:1; // set for deleted key
162 // The key structure occupies space at the upper end of
163 // each page. It's a length byte followed by the key
168 unsigned char key[1];
171 // the value structure also occupies space at the upper
176 unsigned char value[1];
179 // The first part of an index page.
180 // It is immediately followed
181 // by the BtSlot array of keys.
183 typedef struct BtPage_ {
184 uint cnt; // count of keys in page
185 uint act; // count of active keys
186 uint min; // next key offset
187 unsigned char bits:7; // page size in bits
188 unsigned char free:1; // page is on free chain
189 unsigned char lvl:6; // level of page
190 unsigned char kill:1; // page is being deleted
191 unsigned char dirty:1; // page has deleted keys
192 unsigned char right[BtId]; // page number to right
195 // The memory mapping pool table buffer manager entry
198 uid basepage; // mapped base page number
199 char *map; // mapped memory pointer
200 ushort slot; // slot index in this array
201 ushort pin; // mapped page pin counter
202 void *hashprev; // previous pool entry for the same hash idx
203 void *hashnext; // next pool entry for the same hash idx
205 HANDLE hmap; // Windows memory mapping handle
209 #define CLOCK_bit 0x8000 // bit in pool->pin
211 // The loadpage interface object
214 uid page_no; // current page number
215 BtPage page; // current page pointer
216 BtPool *pool; // current page pool
217 BtLatchSet *latch; // current page latch set
220 // structure for latch manager on ALLOC_page
223 struct BtPage_ alloc[1]; // next page_no in right ptr
224 unsigned char chain[BtId]; // head of free page_nos chain
225 BtSpinLatch lock[1]; // allocation area lite latch
226 ushort latchdeployed; // highest number of latch entries deployed
227 ushort nlatchpage; // number of latch pages at BT_latch
228 ushort latchtotal; // number of page latch entries
229 ushort latchhash; // number of latch hash table slots
230 ushort latchvictim; // next latch entry to examine
231 BtHashEntry table[0]; // the hash table
234 // The object structure for Btree access
237 uint page_size; // page size
238 uint page_bits; // page size in bits
239 uint seg_bits; // seg size in pages in bits
240 uint mode; // read-write mode
246 ushort poolcnt; // highest page pool node in use
247 ushort poolmax; // highest page pool node allocated
248 ushort poolmask; // total number of pages in mmap segment - 1
249 ushort hashsize; // size of Hash Table for pool entries
250 volatile uint evicted; // last evicted hash table slot
251 ushort *hash; // pool index for hash entries
252 BtSpinLatch *latch; // latches for hash table slots
253 BtLatchMgr *latchmgr; // mapped latch page from allocation page
254 BtLatchSet *latchsets; // mapped latch set from latch pages
255 BtPool *pool; // memory pool page segments
257 HANDLE halloc; // allocation and latch table handle
262 BtMgr *mgr; // buffer manager for thread
263 BtPage cursor; // cached frame for start/next (never mapped)
264 BtPage frame; // spare frame for the page split (never mapped)
265 uid cursor_page; // current cursor page number
266 unsigned char *mem; // frame, cursor, page memory buffer
267 int found; // last delete or insert was found
268 int err; // last error
282 extern void bt_close (BtDb *bt);
283 extern BtDb *bt_open (BtMgr *mgr);
284 extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, unsigned char *value, uint vallen);
285 extern BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl);
286 extern int bt_findkey (BtDb *bt, unsigned char *key, uint keylen, unsigned char *value, uint vallen);
287 extern uint bt_startkey (BtDb *bt, unsigned char *key, uint len);
288 extern uint bt_nextkey (BtDb *bt, uint slot);
291 extern BtMgr *bt_mgr (char *name, uint mode, uint bits, uint poolsize, uint segsize, uint hashsize);
292 void bt_mgrclose (BtMgr *mgr);
294 // Helper functions to return slot values
296 extern BtKey bt_key (BtDb *bt, uint slot);
297 extern BtVal bt_val (BtDb *bt, uint slot);
299 // BTree page number constants
300 #define ALLOC_page 0 // allocation & lock manager hash table
301 #define ROOT_page 1 // root of the btree
302 #define LEAF_page 2 // first page of leaves
303 #define LATCH_page 3 // pages for lock manager
305 // Number of levels to create in a new BTree
309 // The page is allocated from low and hi ends.
310 // The key slots are allocated from the bottom,
311 // while the text and value of the key
312 // is allocated from the top. When the two
313 // areas meet, the page is split into two.
315 // A key consists of a length byte, two bytes of
316 // index number (0 - 65534), and up to 253 bytes
317 // of key value. Duplicate keys are discarded.
318 // Associated with each key is a value byte string
319 // containing any value desired.
321 // The b-tree root is always located at page 1.
322 // The first leaf page of level zero is always
323 // located on page 2.
325 // The b-tree pages are linked with next
326 // pointers to facilitate enumerators,
327 // and provide for concurrency.
329 // When to root page fills, it is split in two and
330 // the tree height is raised by a new root at page
331 // one with two keys.
333 // Deleted keys are marked with a dead bit until
334 // page cleanup. The fence key for a leaf node is
337 // Groups of pages called segments from the btree are optionally
338 // cached with a memory mapped pool. A hash table is used to keep
339 // track of the cached segments. This behaviour is controlled
340 // by the cache block size parameter to bt_open.
342 // To achieve maximum concurrency one page is locked at a time
343 // as the tree is traversed to find leaf key in question. The right
344 // page numbers are used in cases where the page is being split,
347 // Page 0 is dedicated to lock for new page extensions,
348 // and chains empty pages together for reuse. It also
349 // contains the latch manager hash table.
351 // The ParentModification lock on a node is obtained to serialize posting
352 // or changing the fence key for a node.
354 // Empty pages are chained together through the ALLOC page and reused.
356 // Access macros to address slot and key values from the page
357 // Page slots use 1 based indexing.
359 #define slotptr(page, slot) (((BtSlot *)(page+1)) + (slot-1))
360 #define keyptr(page, slot) ((BtKey)((unsigned char*)(page) + slotptr(page, slot)->off))
361 #define valptr(page, slot) ((BtVal)(keyptr(page,slot)->key + keyptr(page,slot)->len))
363 void bt_putid(unsigned char *dest, uid id)
368 dest[i] = (unsigned char)id, id >>= 8;
371 uid bt_getid(unsigned char *src)
376 for( i = 0; i < BtId; i++ )
377 id <<= 8, id |= *src++;
382 // Phase-Fair reader/writer lock implementation
384 void WriteLock (RWLock *lock)
389 tix = __sync_fetch_and_add (lock->ticket, 1);
391 tix = _InterlockedExchangeAdd16 (lock->ticket, 1);
393 // wait for our ticket to come up
395 while( tix != lock->serving[0] )
402 w = PRES | (tix & PHID);
404 r = __sync_fetch_and_add (lock->rin, w);
406 r = _InterlockedExchangeAdd16 (lock->rin, w);
408 while( r != *lock->rout )
416 void WriteRelease (RWLock *lock)
419 __sync_fetch_and_and (lock->rin, ~MASK);
421 _InterlockedAnd16 (lock->rin, ~MASK);
426 void ReadLock (RWLock *lock)
430 w = __sync_fetch_and_add (lock->rin, RINC) & MASK;
432 w = _InterlockedExchangeAdd16 (lock->rin, RINC) & MASK;
435 while( w == (*lock->rin & MASK) )
443 void ReadRelease (RWLock *lock)
446 __sync_fetch_and_add (lock->rout, RINC);
448 _InterlockedExchangeAdd16 (lock->rout, RINC);
452 // Spin Latch Manager
454 // wait until write lock mode is clear
455 // and add 1 to the share count
457 void bt_spinreadlock(BtSpinLatch *latch)
463 prev = __sync_fetch_and_add ((ushort *)latch, SHARE);
465 prev = _InterlockedExchangeAdd16((ushort *)latch, SHARE);
467 // see if exclusive request is granted or pending
472 prev = __sync_fetch_and_add ((ushort *)latch, -SHARE);
474 prev = _InterlockedExchangeAdd16((ushort *)latch, -SHARE);
477 } while( sched_yield(), 1 );
479 } while( SwitchToThread(), 1 );
483 // wait for other read and write latches to relinquish
485 void bt_spinwritelock(BtSpinLatch *latch)
491 prev = __sync_fetch_and_or((ushort *)latch, PEND | XCL);
493 prev = _InterlockedOr16((ushort *)latch, PEND | XCL);
496 if( !(prev & ~BOTH) )
500 __sync_fetch_and_and ((ushort *)latch, ~XCL);
502 _InterlockedAnd16((ushort *)latch, ~XCL);
505 } while( sched_yield(), 1 );
507 } while( SwitchToThread(), 1 );
511 // try to obtain write lock
513 // return 1 if obtained,
516 int bt_spinwritetry(BtSpinLatch *latch)
521 prev = __sync_fetch_and_or((ushort *)latch, XCL);
523 prev = _InterlockedOr16((ushort *)latch, XCL);
525 // take write access if all bits are clear
528 if( !(prev & ~BOTH) )
532 __sync_fetch_and_and ((ushort *)latch, ~XCL);
534 _InterlockedAnd16((ushort *)latch, ~XCL);
541 void bt_spinreleasewrite(BtSpinLatch *latch)
544 __sync_fetch_and_and((ushort *)latch, ~BOTH);
546 _InterlockedAnd16((ushort *)latch, ~BOTH);
550 // decrement reader count
552 void bt_spinreleaseread(BtSpinLatch *latch)
555 __sync_fetch_and_add((ushort *)latch, -SHARE);
557 _InterlockedExchangeAdd16((ushort *)latch, -SHARE);
561 // link latch table entry into latch hash table
563 void bt_latchlink (BtDb *bt, ushort hashidx, ushort victim, uid page_no)
565 BtLatchSet *set = bt->mgr->latchsets + victim;
567 if( set->next = bt->mgr->latchmgr->table[hashidx].slot )
568 bt->mgr->latchsets[set->next].prev = victim;
570 bt->mgr->latchmgr->table[hashidx].slot = victim;
571 set->page_no = page_no;
578 void bt_unpinlatch (BtLatchSet *set)
581 __sync_fetch_and_add(&set->pin, -1);
583 _InterlockedDecrement16 (&set->pin);
587 // find existing latchset or inspire new one
588 // return with latchset pinned
590 BtLatchSet *bt_pinlatch (BtDb *bt, uid page_no)
592 ushort hashidx = page_no % bt->mgr->latchmgr->latchhash;
593 ushort slot, avail = 0, victim, idx;
596 // obtain read lock on hash table entry
598 bt_spinreadlock(bt->mgr->latchmgr->table[hashidx].latch);
600 if( slot = bt->mgr->latchmgr->table[hashidx].slot ) do
602 set = bt->mgr->latchsets + slot;
603 if( page_no == set->page_no )
605 } while( slot = set->next );
609 __sync_fetch_and_add(&set->pin, 1);
611 _InterlockedIncrement16 (&set->pin);
615 bt_spinreleaseread (bt->mgr->latchmgr->table[hashidx].latch);
620 // try again, this time with write lock
622 bt_spinwritelock(bt->mgr->latchmgr->table[hashidx].latch);
624 if( slot = bt->mgr->latchmgr->table[hashidx].slot ) do
626 set = bt->mgr->latchsets + slot;
627 if( page_no == set->page_no )
629 if( !set->pin && !avail )
631 } while( slot = set->next );
633 // found our entry, or take over an unpinned one
635 if( slot || (slot = avail) ) {
636 set = bt->mgr->latchsets + slot;
638 __sync_fetch_and_add(&set->pin, 1);
640 _InterlockedIncrement16 (&set->pin);
642 set->page_no = page_no;
643 bt_spinreleasewrite(bt->mgr->latchmgr->table[hashidx].latch);
647 // see if there are any unused entries
649 victim = __sync_fetch_and_add (&bt->mgr->latchmgr->latchdeployed, 1) + 1;
651 victim = _InterlockedIncrement16 (&bt->mgr->latchmgr->latchdeployed);
654 if( victim < bt->mgr->latchmgr->latchtotal ) {
655 set = bt->mgr->latchsets + victim;
657 __sync_fetch_and_add(&set->pin, 1);
659 _InterlockedIncrement16 (&set->pin);
661 bt_latchlink (bt, hashidx, victim, page_no);
662 bt_spinreleasewrite (bt->mgr->latchmgr->table[hashidx].latch);
667 victim = __sync_fetch_and_add (&bt->mgr->latchmgr->latchdeployed, -1);
669 victim = _InterlockedDecrement16 (&bt->mgr->latchmgr->latchdeployed);
671 // find and reuse previous lock entry
675 victim = __sync_fetch_and_add(&bt->mgr->latchmgr->latchvictim, 1);
677 victim = _InterlockedIncrement16 (&bt->mgr->latchmgr->latchvictim) - 1;
679 // we don't use slot zero
681 if( victim %= bt->mgr->latchmgr->latchtotal )
682 set = bt->mgr->latchsets + victim;
686 // take control of our slot
687 // from other threads
689 if( set->pin || !bt_spinwritetry (set->busy) )
694 // try to get write lock on hash chain
695 // skip entry if not obtained
696 // or has outstanding locks
698 if( !bt_spinwritetry (bt->mgr->latchmgr->table[idx].latch) ) {
699 bt_spinreleasewrite (set->busy);
704 bt_spinreleasewrite (set->busy);
705 bt_spinreleasewrite (bt->mgr->latchmgr->table[idx].latch);
709 // unlink our available victim from its hash chain
712 bt->mgr->latchsets[set->prev].next = set->next;
714 bt->mgr->latchmgr->table[idx].slot = set->next;
717 bt->mgr->latchsets[set->next].prev = set->prev;
719 bt_spinreleasewrite (bt->mgr->latchmgr->table[idx].latch);
721 __sync_fetch_and_add(&set->pin, 1);
723 _InterlockedIncrement16 (&set->pin);
725 bt_latchlink (bt, hashidx, victim, page_no);
726 bt_spinreleasewrite (bt->mgr->latchmgr->table[hashidx].latch);
727 bt_spinreleasewrite (set->busy);
732 void bt_mgrclose (BtMgr *mgr)
737 // release mapped pages
738 // note that slot zero is never used
740 for( slot = 1; slot < mgr->poolmax; slot++ ) {
741 pool = mgr->pool + slot;
744 munmap (pool->map, (mgr->poolmask+1) << mgr->page_bits);
747 FlushViewOfFile(pool->map, 0);
748 UnmapViewOfFile(pool->map);
749 CloseHandle(pool->hmap);
755 munmap (mgr->latchsets, mgr->latchmgr->nlatchpage * mgr->page_size);
756 munmap (mgr->latchmgr, mgr->page_size);
758 FlushViewOfFile(mgr->latchmgr, 0);
759 UnmapViewOfFile(mgr->latchmgr);
760 CloseHandle(mgr->halloc);
766 free ((void *)mgr->latch);
769 FlushFileBuffers(mgr->idx);
770 CloseHandle(mgr->idx);
771 GlobalFree (mgr->pool);
772 GlobalFree (mgr->hash);
773 GlobalFree ((void *)mgr->latch);
778 // close and release memory
780 void bt_close (BtDb *bt)
787 VirtualFree (bt->mem, 0, MEM_RELEASE);
792 // open/create new btree buffer manager
794 // call with file_name, BT_openmode, bits in page size (e.g. 16),
795 // size of mapped page pool (e.g. 8192)
797 BtMgr *bt_mgr (char *name, uint mode, uint bits, uint poolmax, uint segsize, uint hashsize)
799 uint lvl, attr, cacheblk, last, slot, idx;
800 uint nlatchpage, latchhash;
801 unsigned char value[BtId];
802 BtLatchMgr *latchmgr;
811 SYSTEM_INFO sysinfo[1];
814 // determine sanity of page size and buffer pool
816 if( bits > BT_maxbits )
818 else if( bits < BT_minbits )
822 return NULL; // must have buffer pool
825 mgr = calloc (1, sizeof(BtMgr));
827 mgr->idx = open ((char*)name, O_RDWR | O_CREAT, 0666);
830 return free(mgr), NULL;
832 cacheblk = 4096; // minimum mmap segment size for unix
835 mgr = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, sizeof(BtMgr));
836 attr = FILE_ATTRIBUTE_NORMAL;
837 mgr->idx = CreateFile(name, GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, attr, NULL);
839 if( mgr->idx == INVALID_HANDLE_VALUE )
840 return GlobalFree(mgr), NULL;
842 // normalize cacheblk to multiple of sysinfo->dwAllocationGranularity
843 GetSystemInfo(sysinfo);
844 cacheblk = sysinfo->dwAllocationGranularity;
848 latchmgr = malloc (BT_maxpage);
851 // read minimum page size to get root info
853 if( size = lseek (mgr->idx, 0L, 2) ) {
854 if( pread(mgr->idx, latchmgr, BT_minpage, 0) == BT_minpage )
855 bits = latchmgr->alloc->bits;
857 return free(mgr), free(latchmgr), NULL;
858 } else if( mode == BT_ro )
859 return free(latchmgr), bt_mgrclose (mgr), NULL;
861 latchmgr = VirtualAlloc(NULL, BT_maxpage, MEM_COMMIT, PAGE_READWRITE);
862 size = GetFileSize(mgr->idx, amt);
865 if( !ReadFile(mgr->idx, (char *)latchmgr, BT_minpage, amt, NULL) )
866 return bt_mgrclose (mgr), NULL;
867 bits = latchmgr->alloc->bits;
868 } else if( mode == BT_ro )
869 return bt_mgrclose (mgr), NULL;
872 mgr->page_size = 1 << bits;
873 mgr->page_bits = bits;
875 mgr->poolmax = poolmax;
878 if( cacheblk < mgr->page_size )
879 cacheblk = mgr->page_size;
881 // mask for partial memmaps
883 mgr->poolmask = (cacheblk >> bits) - 1;
885 // see if requested size of pages per memmap is greater
887 if( (1 << segsize) > mgr->poolmask )
888 mgr->poolmask = (1 << segsize) - 1;
892 while( (1 << mgr->seg_bits) <= mgr->poolmask )
895 mgr->hashsize = hashsize;
898 mgr->pool = calloc (poolmax, sizeof(BtPool));
899 mgr->hash = calloc (hashsize, sizeof(ushort));
900 mgr->latch = calloc (hashsize, sizeof(BtSpinLatch));
902 mgr->pool = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, poolmax * sizeof(BtPool));
903 mgr->hash = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, hashsize * sizeof(ushort));
904 mgr->latch = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, hashsize * sizeof(BtSpinLatch));
910 // initialize an empty b-tree with latch page, root page, page of leaves
911 // and page(s) of latches
913 memset (latchmgr, 0, 1 << bits);
914 nlatchpage = BT_latchtable / (mgr->page_size / sizeof(BtLatchSet)) + 1;
915 bt_putid(latchmgr->alloc->right, MIN_lvl+1+nlatchpage);
916 latchmgr->alloc->bits = mgr->page_bits;
918 latchmgr->nlatchpage = nlatchpage;
919 latchmgr->latchtotal = nlatchpage * (mgr->page_size / sizeof(BtLatchSet));
921 // initialize latch manager
923 latchhash = (mgr->page_size - sizeof(BtLatchMgr)) / sizeof(BtHashEntry);
925 // size of hash table = total number of latchsets
927 if( latchhash > latchmgr->latchtotal )
928 latchhash = latchmgr->latchtotal;
930 latchmgr->latchhash = latchhash;
933 if( write (mgr->idx, latchmgr, mgr->page_size) < mgr->page_size )
934 return bt_mgrclose (mgr), NULL;
936 if( !WriteFile (mgr->idx, (char *)latchmgr, mgr->page_size, amt, NULL) )
937 return bt_mgrclose (mgr), NULL;
939 if( *amt < mgr->page_size )
940 return bt_mgrclose (mgr), NULL;
943 memset (latchmgr, 0, 1 << bits);
944 latchmgr->alloc->bits = mgr->page_bits;
946 for( lvl=MIN_lvl; lvl--; ) {
947 slotptr(latchmgr->alloc, 1)->off = mgr->page_size - 3 - (lvl ? BtId + 1: 1);
948 key = keyptr(latchmgr->alloc, 1);
949 key->len = 2; // create stopper key
953 bt_putid(value, MIN_lvl - lvl + 1);
954 val = valptr(latchmgr->alloc, 1);
955 val->len = lvl ? BtId : 0;
956 memcpy (val->value, value, val->len);
958 latchmgr->alloc->min = slotptr(latchmgr->alloc, 1)->off;
959 latchmgr->alloc->lvl = lvl;
960 latchmgr->alloc->cnt = 1;
961 latchmgr->alloc->act = 1;
963 if( write (mgr->idx, latchmgr, mgr->page_size) < mgr->page_size )
964 return bt_mgrclose (mgr), NULL;
966 if( !WriteFile (mgr->idx, (char *)latchmgr, mgr->page_size, amt, NULL) )
967 return bt_mgrclose (mgr), NULL;
969 if( *amt < mgr->page_size )
970 return bt_mgrclose (mgr), NULL;
974 // clear out latch manager locks
975 // and rest of pages to round out segment
977 memset(latchmgr, 0, mgr->page_size);
980 while( last <= ((MIN_lvl + 1 + nlatchpage) | mgr->poolmask) ) {
982 pwrite(mgr->idx, latchmgr, mgr->page_size, last << mgr->page_bits);
984 SetFilePointer (mgr->idx, last << mgr->page_bits, NULL, FILE_BEGIN);
985 if( !WriteFile (mgr->idx, (char *)latchmgr, mgr->page_size, amt, NULL) )
986 return bt_mgrclose (mgr), NULL;
987 if( *amt < mgr->page_size )
988 return bt_mgrclose (mgr), NULL;
995 flag = PROT_READ | PROT_WRITE;
996 mgr->latchmgr = mmap (0, mgr->page_size, flag, MAP_SHARED, mgr->idx, ALLOC_page * mgr->page_size);
997 if( mgr->latchmgr == MAP_FAILED )
998 return bt_mgrclose (mgr), NULL;
999 mgr->latchsets = (BtLatchSet *)mmap (0, mgr->latchmgr->nlatchpage * mgr->page_size, flag, MAP_SHARED, mgr->idx, LATCH_page * mgr->page_size);
1000 if( mgr->latchsets == MAP_FAILED )
1001 return bt_mgrclose (mgr), NULL;
1003 flag = PAGE_READWRITE;
1004 mgr->halloc = CreateFileMapping(mgr->idx, NULL, flag, 0, (BT_latchtable / (mgr->page_size / sizeof(BtLatchSet)) + 1 + LATCH_page) * mgr->page_size, NULL);
1006 return bt_mgrclose (mgr), NULL;
1008 flag = FILE_MAP_WRITE;
1009 mgr->latchmgr = MapViewOfFile(mgr->halloc, flag, 0, 0, (BT_latchtable / (mgr->page_size / sizeof(BtLatchSet)) + 1 + LATCH_page) * mgr->page_size);
1010 if( !mgr->latchmgr )
1011 return GetLastError(), bt_mgrclose (mgr), NULL;
1013 mgr->latchsets = (void *)((char *)mgr->latchmgr + LATCH_page * mgr->page_size);
1019 VirtualFree (latchmgr, 0, MEM_RELEASE);
1024 // open BTree access method
1025 // based on buffer manager
1027 BtDb *bt_open (BtMgr *mgr)
1029 BtDb *bt = malloc (sizeof(*bt));
1031 memset (bt, 0, sizeof(*bt));
1034 bt->mem = malloc (2 *mgr->page_size);
1036 bt->mem = VirtualAlloc(NULL, 2 * mgr->page_size, MEM_COMMIT, PAGE_READWRITE);
1038 bt->frame = (BtPage)bt->mem;
1039 bt->cursor = (BtPage)(bt->mem + 1 * mgr->page_size);
1043 // compare two keys, returning > 0, = 0, or < 0
1044 // as the comparison value
1046 int keycmp (BtKey key1, unsigned char *key2, uint len2)
1048 uint len1 = key1->len;
1051 if( ans = memcmp (key1->key, key2, len1 > len2 ? len2 : len1) )
1064 // find segment in pool
1065 // must be called with hashslot idx locked
1066 // return NULL if not there
1067 // otherwise return node
1069 BtPool *bt_findpool(BtDb *bt, uid page_no, uint idx)
1074 // compute start of hash chain in pool
1076 if( slot = bt->mgr->hash[idx] )
1077 pool = bt->mgr->pool + slot;
1081 page_no &= ~bt->mgr->poolmask;
1083 while( pool->basepage != page_no )
1084 if( pool = pool->hashnext )
1092 // add segment to hash table
1094 void bt_linkhash(BtDb *bt, BtPool *pool, uid page_no, int idx)
1099 pool->hashprev = pool->hashnext = NULL;
1100 pool->basepage = page_no & ~bt->mgr->poolmask;
1101 pool->pin = CLOCK_bit + 1;
1103 if( slot = bt->mgr->hash[idx] ) {
1104 node = bt->mgr->pool + slot;
1105 pool->hashnext = node;
1106 node->hashprev = pool;
1109 bt->mgr->hash[idx] = pool->slot;
1112 // map new buffer pool segment to virtual memory
1114 BTERR bt_mapsegment(BtDb *bt, BtPool *pool, uid page_no)
1116 off64_t off = (page_no & ~bt->mgr->poolmask) << bt->mgr->page_bits;
1117 off64_t limit = off + ((bt->mgr->poolmask+1) << bt->mgr->page_bits);
1121 flag = PROT_READ | ( bt->mgr->mode == BT_ro ? 0 : PROT_WRITE );
1122 pool->map = mmap (0, (bt->mgr->poolmask+1) << bt->mgr->page_bits, flag, MAP_SHARED, bt->mgr->idx, off);
1123 if( pool->map == MAP_FAILED )
1124 return bt->err = BTERR_map;
1127 flag = ( bt->mgr->mode == BT_ro ? PAGE_READONLY : PAGE_READWRITE );
1128 pool->hmap = CreateFileMapping(bt->mgr->idx, NULL, flag, (DWORD)(limit >> 32), (DWORD)limit, NULL);
1130 return bt->err = BTERR_map;
1132 flag = ( bt->mgr->mode == BT_ro ? FILE_MAP_READ : FILE_MAP_WRITE );
1133 pool->map = MapViewOfFile(pool->hmap, flag, (DWORD)(off >> 32), (DWORD)off, (bt->mgr->poolmask+1) << bt->mgr->page_bits);
1135 return bt->err = BTERR_map;
1140 // calculate page within pool
1142 BtPage bt_page (BtDb *bt, BtPool *pool, uid page_no)
1144 uint subpage = (uint)(page_no & bt->mgr->poolmask); // page within mapping
1147 page = (BtPage)(pool->map + (subpage << bt->mgr->page_bits));
1153 void bt_unpinpool (BtPool *pool)
1156 __sync_fetch_and_add(&pool->pin, -1);
1158 _InterlockedDecrement16 (&pool->pin);
1162 // find or place requested page in segment-pool
1163 // return pool table entry, incrementing pin
1165 BtPool *bt_pinpool(BtDb *bt, uid page_no)
1167 uint slot, hashidx, idx, victim;
1168 BtPool *pool, *node, *next;
1170 // lock hash table chain
1172 hashidx = (uint)(page_no >> bt->mgr->seg_bits) % bt->mgr->hashsize;
1173 bt_spinwritelock (&bt->mgr->latch[hashidx]);
1175 // look up in hash table
1177 if( pool = bt_findpool(bt, page_no, hashidx) ) {
1179 __sync_fetch_and_or(&pool->pin, CLOCK_bit);
1180 __sync_fetch_and_add(&pool->pin, 1);
1182 _InterlockedOr16 (&pool->pin, CLOCK_bit);
1183 _InterlockedIncrement16 (&pool->pin);
1185 bt_spinreleasewrite (&bt->mgr->latch[hashidx]);
1189 // allocate a new pool node
1190 // and add to hash table
1193 slot = __sync_fetch_and_add(&bt->mgr->poolcnt, 1);
1195 slot = _InterlockedIncrement16 (&bt->mgr->poolcnt) - 1;
1198 if( ++slot < bt->mgr->poolmax ) {
1199 pool = bt->mgr->pool + slot;
1202 if( bt_mapsegment(bt, pool, page_no) )
1205 bt_linkhash(bt, pool, page_no, hashidx);
1206 bt_spinreleasewrite (&bt->mgr->latch[hashidx]);
1210 // pool table is full
1211 // find best pool entry to evict
1214 __sync_fetch_and_add(&bt->mgr->poolcnt, -1);
1216 _InterlockedDecrement16 (&bt->mgr->poolcnt);
1221 victim = __sync_fetch_and_add(&bt->mgr->evicted, 1);
1223 victim = _InterlockedIncrement (&bt->mgr->evicted) - 1;
1225 victim %= bt->mgr->poolmax;
1226 pool = bt->mgr->pool + victim;
1227 idx = (uint)(pool->basepage >> bt->mgr->seg_bits) % bt->mgr->hashsize;
1232 // try to get write lock
1233 // skip entry if not obtained
1235 if( !bt_spinwritetry (&bt->mgr->latch[idx]) )
1238 // skip this entry if
1240 // or clock bit is set
1244 __sync_fetch_and_and(&pool->pin, ~CLOCK_bit);
1246 _InterlockedAnd16 (&pool->pin, ~CLOCK_bit);
1248 bt_spinreleasewrite (&bt->mgr->latch[idx]);
1252 // unlink victim pool node from hash table
1254 if( node = pool->hashprev )
1255 node->hashnext = pool->hashnext;
1256 else if( node = pool->hashnext )
1257 bt->mgr->hash[idx] = node->slot;
1259 bt->mgr->hash[idx] = 0;
1261 if( node = pool->hashnext )
1262 node->hashprev = pool->hashprev;
1264 bt_spinreleasewrite (&bt->mgr->latch[idx]);
1266 // remove old file mapping
1268 munmap (pool->map, (bt->mgr->poolmask+1) << bt->mgr->page_bits);
1270 // FlushViewOfFile(pool->map, 0);
1271 UnmapViewOfFile(pool->map);
1272 CloseHandle(pool->hmap);
1276 // create new pool mapping
1277 // and link into hash table
1279 if( bt_mapsegment(bt, pool, page_no) )
1282 bt_linkhash(bt, pool, page_no, hashidx);
1283 bt_spinreleasewrite (&bt->mgr->latch[hashidx]);
1288 // place write, read, or parent lock on requested page_no.
1290 void bt_lockpage(BtLock mode, BtLatchSet *set)
1294 ReadLock (set->readwr);
1297 WriteLock (set->readwr);
1300 ReadLock (set->access);
1303 WriteLock (set->access);
1306 WriteLock (set->parent);
1311 // remove write, read, or parent lock on requested page
1313 void bt_unlockpage(BtLock mode, BtLatchSet *set)
1317 ReadRelease (set->readwr);
1320 WriteRelease (set->readwr);
1323 ReadRelease (set->access);
1326 WriteRelease (set->access);
1329 WriteRelease (set->parent);
1334 // allocate a new page and write page into it
1336 uid bt_newpage(BtDb *bt, BtPage page)
1342 // lock allocation page
1344 bt_spinwritelock(bt->mgr->latchmgr->lock);
1346 // use empty chain first
1347 // else allocate empty page
1349 if( new_page = bt_getid(bt->mgr->latchmgr->chain) ) {
1350 if( set->pool = bt_pinpool (bt, new_page) )
1351 set->page = bt_page (bt, set->pool, new_page);
1355 bt_putid(bt->mgr->latchmgr->chain, bt_getid(set->page->right));
1356 bt_unpinpool (set->pool);
1358 new_page = bt_getid(bt->mgr->latchmgr->alloc->right);
1359 bt_putid(bt->mgr->latchmgr->alloc->right, new_page+1);
1361 // if writing first page of pool block, set file length thru last page
1363 if( (new_page & bt->mgr->poolmask) == 0 )
1364 ftruncate (bt->mgr->idx, (new_page + bt->mgr->poolmask + 1) << bt->mgr->page_bits);
1368 // unlock allocation latch
1370 bt_spinreleasewrite(bt->mgr->latchmgr->lock);
1373 // bring new page into pool and copy page.
1374 // this will extend the file into the new pages on WIN32.
1376 if( set->pool = bt_pinpool (bt, new_page) )
1377 set->page = bt_page (bt, set->pool, new_page);
1381 memcpy(set->page, page, bt->mgr->page_size);
1382 bt_unpinpool (set->pool);
1385 // unlock allocation latch
1387 bt_spinreleasewrite(bt->mgr->latchmgr->lock);
1392 // find slot in page for given key at a given level
1394 int bt_findslot (BtPageSet *set, unsigned char *key, uint len)
1396 uint diff, higher = set->page->cnt, low = 1, slot;
1399 // make stopper key an infinite fence value
1401 if( bt_getid (set->page->right) )
1406 // low is the lowest candidate.
1407 // loop ends when they meet
1409 // higher is already
1410 // tested as .ge. the passed key.
1412 while( diff = higher - low ) {
1413 slot = low + ( diff >> 1 );
1414 if( keycmp (keyptr(set->page, slot), key, len) < 0 )
1417 higher = slot, good++;
1420 // return zero if key is on right link page
1422 return good ? higher : 0;
1425 // find and load page at given level for given key
1426 // leave page rd or wr locked as requested
1428 int bt_loadpage (BtDb *bt, BtPageSet *set, unsigned char *key, uint len, uint lvl, BtLock lock)
1430 uid page_no = ROOT_page, prevpage = 0;
1431 uint drill = 0xff, slot;
1432 BtLatchSet *prevlatch;
1433 uint mode, prevmode;
1436 // start at root of btree and drill down
1439 // determine lock mode of drill level
1440 mode = (drill == lvl) ? lock : BtLockRead;
1442 set->latch = bt_pinlatch (bt, page_no);
1443 set->page_no = page_no;
1445 // pin page contents
1447 if( set->pool = bt_pinpool (bt, page_no) )
1448 set->page = bt_page (bt, set->pool, page_no);
1452 // obtain access lock using lock chaining with Access mode
1454 if( page_no > ROOT_page )
1455 bt_lockpage(BtLockAccess, set->latch);
1457 // release & unpin parent page
1460 bt_unlockpage(prevmode, prevlatch);
1461 bt_unpinlatch (prevlatch);
1462 bt_unpinpool (prevpool);
1466 // obtain read lock using lock chaining
1468 bt_lockpage(mode, set->latch);
1470 if( set->page->free )
1471 return bt->err = BTERR_struct, 0;
1473 if( page_no > ROOT_page )
1474 bt_unlockpage(BtLockAccess, set->latch);
1476 // re-read and re-lock root after determining actual level of root
1478 if( set->page->lvl != drill) {
1479 if( set->page_no != ROOT_page )
1480 return bt->err = BTERR_struct, 0;
1482 drill = set->page->lvl;
1484 if( lock != BtLockRead && drill == lvl ) {
1485 bt_unlockpage(mode, set->latch);
1486 bt_unpinlatch (set->latch);
1487 bt_unpinpool (set->pool);
1492 prevpage = set->page_no;
1493 prevlatch = set->latch;
1494 prevpool = set->pool;
1497 // find key on page at this level
1498 // and descend to requested level
1500 if( !set->page->kill )
1501 if( slot = bt_findslot (set, key, len) ) {
1505 while( slotptr(set->page, slot)->dead )
1506 if( slot++ < set->page->cnt )
1511 page_no = bt_getid(valptr(set->page, slot)->value);
1516 // or slide right into next page
1519 page_no = bt_getid(set->page->right);
1523 // return error on end of right chain
1525 bt->err = BTERR_struct;
1526 return 0; // return error
1529 // return page to free list
1530 // page must be delete & write locked
1532 void bt_freepage (BtDb *bt, BtPageSet *set)
1534 // lock allocation page
1536 bt_spinwritelock (bt->mgr->latchmgr->lock);
1539 memcpy(set->page->right, bt->mgr->latchmgr->chain, BtId);
1540 bt_putid(bt->mgr->latchmgr->chain, set->page_no);
1541 set->page->free = 1;
1543 // unlock released page
1545 bt_unlockpage (BtLockDelete, set->latch);
1546 bt_unlockpage (BtLockWrite, set->latch);
1547 bt_unpinlatch (set->latch);
1548 bt_unpinpool (set->pool);
1550 // unlock allocation page
1552 bt_spinreleasewrite (bt->mgr->latchmgr->lock);
1555 // a fence key was deleted from a page
1556 // push new fence value upwards
1558 BTERR bt_fixfence (BtDb *bt, BtPageSet *set, uint lvl)
1560 unsigned char leftkey[256], rightkey[256];
1561 unsigned char value[BtId];
1565 // remove the old fence value
1567 ptr = keyptr(set->page, set->page->cnt);
1568 memcpy (rightkey, ptr, ptr->len + 1);
1570 memset (slotptr(set->page, set->page->cnt--), 0, sizeof(BtSlot));
1571 set->page->dirty = 1;
1573 ptr = keyptr(set->page, set->page->cnt);
1574 memcpy (leftkey, ptr, ptr->len + 1);
1575 page_no = set->page_no;
1577 bt_lockpage (BtLockParent, set->latch);
1578 bt_unlockpage (BtLockWrite, set->latch);
1580 // insert new (now smaller) fence key
1582 bt_putid (value, page_no);
1584 if( bt_insertkey (bt, leftkey+1, *leftkey, lvl+1, value, BtId) )
1587 // now delete old fence key
1589 if( bt_deletekey (bt, rightkey+1, *rightkey, lvl+1) )
1592 bt_unlockpage (BtLockParent, set->latch);
1593 bt_unpinlatch(set->latch);
1594 bt_unpinpool (set->pool);
1598 // root has a single child
1599 // collapse a level from the tree
1601 BTERR bt_collapseroot (BtDb *bt, BtPageSet *root)
1606 // find the child entry and promote as new root contents
1609 for( idx = 0; idx++ < root->page->cnt; )
1610 if( !slotptr(root->page, idx)->dead )
1613 child->page_no = bt_getid (valptr(root->page, idx)->value);
1615 child->latch = bt_pinlatch (bt, child->page_no);
1616 bt_lockpage (BtLockDelete, child->latch);
1617 bt_lockpage (BtLockWrite, child->latch);
1619 if( child->pool = bt_pinpool (bt, child->page_no) )
1620 child->page = bt_page (bt, child->pool, child->page_no);
1624 memcpy (root->page, child->page, bt->mgr->page_size);
1625 bt_freepage (bt, child);
1627 } while( root->page->lvl > 1 && root->page->act == 1 );
1629 bt_unlockpage (BtLockWrite, root->latch);
1630 bt_unpinlatch (root->latch);
1631 bt_unpinpool (root->pool);
1635 // find and delete key on page by marking delete flag bit
1636 // if page becomes empty, delete it from the btree
1638 BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl)
1640 unsigned char lowerfence[256], higherfence[256];
1641 uint slot, idx, dirty = 0, fence, found;
1642 BtPageSet set[1], right[1];
1643 unsigned char value[BtId];
1646 if( slot = bt_loadpage (bt, set, key, len, lvl, BtLockWrite) )
1647 ptr = keyptr(set->page, slot);
1651 // are we deleting a fence slot?
1653 fence = slot == set->page->cnt;
1655 // if key is found delete it, otherwise ignore request
1657 if( found = !keycmp (ptr, key, len) )
1658 if( found = slotptr(set->page, slot)->dead == 0 ) {
1659 dirty = slotptr(set->page, slot)->dead = 1;
1660 set->page->dirty = 1;
1663 // collapse empty slots
1665 while( idx = set->page->cnt - 1 )
1666 if( slotptr(set->page, idx)->dead ) {
1667 *slotptr(set->page, idx) = *slotptr(set->page, idx + 1);
1668 memset (slotptr(set->page, set->page->cnt--), 0, sizeof(BtSlot));
1673 // did we delete a fence key in an upper level?
1675 if( dirty && lvl && set->page->act && fence )
1676 if( bt_fixfence (bt, set, lvl) )
1679 return bt->found = found, 0;
1681 // is this a collapsed root?
1683 if( lvl > 1 && set->page_no == ROOT_page && set->page->act == 1 )
1684 if( bt_collapseroot (bt, set) )
1687 return bt->found = found, 0;
1689 // return if page is not empty
1691 if( set->page->act ) {
1692 bt_unlockpage(BtLockWrite, set->latch);
1693 bt_unpinlatch (set->latch);
1694 bt_unpinpool (set->pool);
1695 return bt->found = found, 0;
1698 // cache copy of fence key
1699 // to post in parent
1701 ptr = keyptr(set->page, set->page->cnt);
1702 memcpy (lowerfence, ptr, ptr->len + 1);
1704 // obtain lock on right page
1706 right->page_no = bt_getid(set->page->right);
1707 right->latch = bt_pinlatch (bt, right->page_no);
1708 bt_lockpage (BtLockWrite, right->latch);
1710 // pin page contents
1712 if( right->pool = bt_pinpool (bt, right->page_no) )
1713 right->page = bt_page (bt, right->pool, right->page_no);
1717 if( right->page->kill )
1718 return bt->err = BTERR_struct;
1720 // pull contents of right peer into our empty page
1722 memcpy (set->page, right->page, bt->mgr->page_size);
1724 // cache copy of key to update
1726 ptr = keyptr(right->page, right->page->cnt);
1727 memcpy (higherfence, ptr, ptr->len + 1);
1729 // mark right page deleted and point it to left page
1730 // until we can post parent updates
1732 bt_putid (right->page->right, set->page_no);
1733 right->page->kill = 1;
1735 bt_lockpage (BtLockParent, right->latch);
1736 bt_unlockpage (BtLockWrite, right->latch);
1738 bt_lockpage (BtLockParent, set->latch);
1739 bt_unlockpage (BtLockWrite, set->latch);
1741 // redirect higher key directly to our new node contents
1743 bt_putid (value, set->page_no);
1745 if( bt_insertkey (bt, higherfence+1, *higherfence, lvl+1, value, BtId) )
1748 // delete old lower key to our node
1750 if( bt_deletekey (bt, lowerfence+1, *lowerfence, lvl+1) )
1753 // obtain delete and write locks to right node
1755 bt_unlockpage (BtLockParent, right->latch);
1756 bt_lockpage (BtLockDelete, right->latch);
1757 bt_lockpage (BtLockWrite, right->latch);
1758 bt_freepage (bt, right);
1760 bt_unlockpage (BtLockParent, set->latch);
1761 bt_unpinlatch (set->latch);
1762 bt_unpinpool (set->pool);
1767 // find key in leaf level and return number of value bytes
1768 // or (-1) if not found
1770 int bt_findkey (BtDb *bt, unsigned char *key, uint keylen, unsigned char *value, uint valmax)
1778 if( slot = bt_loadpage (bt, set, key, keylen, 0, BtLockRead) )
1779 ptr = keyptr(set->page, slot);
1783 // if key exists, return TRUE
1784 // otherwise return FALSE
1786 if( !keycmp (ptr, key, keylen) ) {
1787 val = valptr (set->page,slot);
1788 if( valmax > val->len )
1790 memcpy (value, val->value, valmax);
1795 bt_unlockpage (BtLockRead, set->latch);
1796 bt_unpinlatch (set->latch);
1797 bt_unpinpool (set->pool);
1801 // check page for space available,
1802 // clean if necessary and return
1803 // 0 - page needs splitting
1804 // >0 new slot value
1806 uint bt_cleanpage(BtDb *bt, BtPage page, uint keylen, uint slot, uint vallen)
1808 uint nxt = bt->mgr->page_size;
1809 uint cnt = 0, idx = 0;
1810 uint max = page->cnt;
1815 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + keylen + 1 + vallen + 1)
1818 // skip cleanup if nothing to reclaim
1823 memcpy (bt->frame, page, bt->mgr->page_size);
1825 // skip page info and set rest of page to zero
1827 memset (page+1, 0, bt->mgr->page_size - sizeof(*page));
1831 // try cleaning up page first
1832 // by removing deleted keys
1834 while( cnt++ < max ) {
1837 if( cnt < max && slotptr(bt->frame,cnt)->dead )
1840 // copy the key across
1842 key = keyptr(bt->frame, cnt);
1843 nxt -= key->len + 1;
1844 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1846 // copy the value across
1848 val = valptr(bt->frame, cnt);
1849 nxt -= val->len + 1;
1850 ((unsigned char *)page)[nxt] = val->len;
1851 memcpy ((unsigned char *)page + nxt + 1, val, val->len);
1855 slotptr(page, idx)->off = nxt;
1857 if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
1864 // see if page has enough space now, or does it need splitting?
1866 if( page->min >= (idx+1) * sizeof(BtSlot) + sizeof(*page) + keylen + 1 + vallen + 1 )
1872 // split the root and raise the height of the btree
1874 BTERR bt_splitroot(BtDb *bt, BtPageSet *root, unsigned char *leftkey, uid page_no2)
1876 uint nxt = bt->mgr->page_size;
1877 unsigned char value[BtId];
1880 // Obtain an empty page to use, and copy the current
1881 // root contents into it, e.g. lower keys
1883 if( !(left = bt_newpage(bt, root->page)) )
1886 // preserve the page info at the bottom
1887 // of higher keys and set rest to zero
1889 memset(root->page+1, 0, bt->mgr->page_size - sizeof(*root->page));
1891 // insert lower keys page fence key on newroot page as first key
1894 bt_putid (value, left);
1895 ((unsigned char *)root->page)[nxt] = BtId;
1896 memcpy ((unsigned char *)root->page + nxt + 1, value, BtId);
1898 nxt -= *leftkey + 1;
1899 memcpy ((unsigned char *)root->page + nxt, leftkey, *leftkey + 1);
1900 slotptr(root->page, 1)->off = nxt;
1902 // insert stopper key on newroot page
1903 // and increase the root height
1905 nxt -= 3 + BtId + 1;
1906 ((unsigned char *)root->page)[nxt] = 2;
1907 ((unsigned char *)root->page)[nxt+1] = 0xff;
1908 ((unsigned char *)root->page)[nxt+2] = 0xff;
1910 bt_putid (value, page_no2);
1911 ((unsigned char *)root->page)[nxt+3] = BtId;
1912 memcpy ((unsigned char *)root->page + nxt + 4, value, BtId);
1913 slotptr(root->page, 2)->off = nxt;
1915 bt_putid(root->page->right, 0);
1916 root->page->min = nxt; // reset lowest used offset and key count
1917 root->page->cnt = 2;
1918 root->page->act = 2;
1921 // release and unpin root
1923 bt_unlockpage(BtLockWrite, root->latch);
1924 bt_unpinlatch (root->latch);
1925 bt_unpinpool (root->pool);
1929 // split already locked full node
1932 BTERR bt_splitpage (BtDb *bt, BtPageSet *set)
1934 uint cnt = 0, idx = 0, max, nxt = bt->mgr->page_size;
1935 unsigned char fencekey[256], rightkey[256];
1936 unsigned char value[BtId];
1937 uint lvl = set->page->lvl;
1943 // split higher half of keys to bt->frame
1945 memset (bt->frame, 0, bt->mgr->page_size);
1946 max = set->page->cnt;
1950 while( cnt++ < max ) {
1951 val = valptr(set->page, cnt);
1952 nxt -= val->len + 1;
1953 ((unsigned char *)bt->frame)[nxt] = val->len;
1954 memcpy ((unsigned char *)bt->frame + nxt + 1, val->value, val->len);
1956 key = keyptr(set->page, cnt);
1957 nxt -= key->len + 1;
1958 memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
1960 slotptr(bt->frame, ++idx)->off = nxt;
1962 if( !(slotptr(bt->frame, idx)->dead = slotptr(set->page, cnt)->dead) )
1966 // remember existing fence key for new page to the right
1968 memcpy (rightkey, key, key->len + 1);
1970 bt->frame->bits = bt->mgr->page_bits;
1971 bt->frame->min = nxt;
1972 bt->frame->cnt = idx;
1973 bt->frame->lvl = lvl;
1977 if( set->page_no > ROOT_page )
1978 memcpy (bt->frame->right, set->page->right, BtId);
1980 // get new free page and write higher keys to it.
1982 if( !(right->page_no = bt_newpage(bt, bt->frame)) )
1985 // update lower keys to continue in old page
1987 memcpy (bt->frame, set->page, bt->mgr->page_size);
1988 memset (set->page+1, 0, bt->mgr->page_size - sizeof(*set->page));
1989 nxt = bt->mgr->page_size;
1990 set->page->dirty = 0;
1995 // assemble page of smaller keys
1997 while( cnt++ < max / 2 ) {
1998 val = valptr(bt->frame, cnt);
1999 nxt -= val->len + 1;
2000 ((unsigned char *)set->page)[nxt] = val->len;
2001 memcpy ((unsigned char *)set->page + nxt + 1, val->value, val->len);
2003 key = keyptr(bt->frame, cnt);
2004 nxt -= key->len + 1;
2005 memcpy ((unsigned char *)set->page + nxt, key, key->len + 1);
2006 slotptr(set->page, ++idx)->off = nxt;
2010 // remember fence key for smaller page
2012 memcpy(fencekey, key, key->len + 1);
2014 bt_putid(set->page->right, right->page_no);
2015 set->page->min = nxt;
2016 set->page->cnt = idx;
2018 // if current page is the root page, split it
2020 if( set->page_no == ROOT_page )
2021 return bt_splitroot (bt, set, fencekey, right->page_no);
2023 // insert new fences in their parent pages
2025 right->latch = bt_pinlatch (bt, right->page_no);
2026 bt_lockpage (BtLockParent, right->latch);
2028 bt_lockpage (BtLockParent, set->latch);
2029 bt_unlockpage (BtLockWrite, set->latch);
2031 // insert new fence for reformulated left block of smaller keys
2033 bt_putid (value, set->page_no);
2035 if( bt_insertkey (bt, fencekey+1, *fencekey, lvl+1, value, BtId) )
2038 // switch fence for right block of larger keys to new right page
2040 bt_putid (value, right->page_no);
2042 if( bt_insertkey (bt, rightkey+1, *rightkey, lvl+1, value, BtId) )
2045 bt_unlockpage (BtLockParent, set->latch);
2046 bt_unpinlatch (set->latch);
2047 bt_unpinpool (set->pool);
2049 bt_unlockpage (BtLockParent, right->latch);
2050 bt_unpinlatch (right->latch);
2053 // Insert new key into the btree at given level.
2055 BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint keylen, uint lvl, unsigned char *value, uint vallen)
2064 if( slot = bt_loadpage (bt, set, key, keylen, lvl, BtLockWrite) )
2065 ptr = keyptr(set->page, slot);
2069 bt->err = BTERR_ovflw;
2073 // if key already exists, update id and return
2075 if( reuse = !keycmp (ptr, key, keylen) )
2076 if( val = valptr(set->page, slot), val->len >= vallen ) {
2077 if( slotptr(set->page, slot)->dead )
2079 slotptr(set->page, slot)->dead = 0;
2081 memcpy (val->value, value, vallen);
2082 bt_unlockpage(BtLockWrite, set->latch);
2083 bt_unpinlatch (set->latch);
2084 bt_unpinpool (set->pool);
2087 if( !slotptr(set->page, slot)->dead )
2089 slotptr(set->page, slot)->dead = 1;
2090 set->page->dirty = 1;
2093 // check if page has enough space
2095 if( slot = bt_cleanpage (bt, set->page, keylen, slot, vallen) )
2098 if( bt_splitpage (bt, set) )
2102 // calculate next available slot and copy key into page
2104 set->page->min -= vallen + 1; // reset lowest used offset
2105 ((unsigned char *)set->page)[set->page->min] = vallen;
2106 memcpy ((unsigned char *)set->page + set->page->min +1, value, vallen );
2108 set->page->min -= keylen + 1; // reset lowest used offset
2109 ((unsigned char *)set->page)[set->page->min] = keylen;
2110 memcpy ((unsigned char *)set->page + set->page->min +1, key, keylen );
2112 for( idx = slot; idx < set->page->cnt; idx++ )
2113 if( slotptr(set->page, idx)->dead )
2116 // now insert key into array before slot
2118 if( !reuse && idx == set->page->cnt )
2119 idx++, set->page->cnt++;
2124 *slotptr(set->page, idx) = *slotptr(set->page, idx -1), idx--;
2126 slotptr(set->page, slot)->off = set->page->min;
2127 slotptr(set->page, slot)->dead = 0;
2129 bt_unlockpage (BtLockWrite, set->latch);
2130 bt_unpinlatch (set->latch);
2131 bt_unpinpool (set->pool);
2135 // cache page of keys into cursor and return starting slot for given key
2137 uint bt_startkey (BtDb *bt, unsigned char *key, uint len)
2142 // cache page for retrieval
2144 if( slot = bt_loadpage (bt, set, key, len, 0, BtLockRead) )
2145 memcpy (bt->cursor, set->page, bt->mgr->page_size);
2149 bt->cursor_page = set->page_no;
2151 bt_unlockpage(BtLockRead, set->latch);
2152 bt_unpinlatch (set->latch);
2153 bt_unpinpool (set->pool);
2157 // return next slot for cursor page
2158 // or slide cursor right into next page
2160 uint bt_nextkey (BtDb *bt, uint slot)
2166 right = bt_getid(bt->cursor->right);
2168 while( slot++ < bt->cursor->cnt )
2169 if( slotptr(bt->cursor,slot)->dead )
2171 else if( right || (slot < bt->cursor->cnt) ) // skip infinite stopper
2179 bt->cursor_page = right;
2181 if( set->pool = bt_pinpool (bt, right) )
2182 set->page = bt_page (bt, set->pool, right);
2186 set->latch = bt_pinlatch (bt, right);
2187 bt_lockpage(BtLockRead, set->latch);
2189 memcpy (bt->cursor, set->page, bt->mgr->page_size);
2191 bt_unlockpage(BtLockRead, set->latch);
2192 bt_unpinlatch (set->latch);
2193 bt_unpinpool (set->pool);
2201 BtKey bt_key(BtDb *bt, uint slot)
2203 return keyptr(bt->cursor, slot);
2206 BtVal bt_val(BtDb *bt, uint slot)
2208 return valptr(bt->cursor,slot);
2214 double getCpuTime(int type)
2217 FILETIME xittime[1];
2218 FILETIME systime[1];
2219 FILETIME usrtime[1];
2220 SYSTEMTIME timeconv[1];
2223 memset (timeconv, 0, sizeof(SYSTEMTIME));
2227 GetSystemTimeAsFileTime (xittime);
2228 FileTimeToSystemTime (xittime, timeconv);
2229 ans = (double)timeconv->wDayOfWeek * 3600 * 24;
2232 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
2233 FileTimeToSystemTime (usrtime, timeconv);
2236 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
2237 FileTimeToSystemTime (systime, timeconv);
2241 ans += (double)timeconv->wHour * 3600;
2242 ans += (double)timeconv->wMinute * 60;
2243 ans += (double)timeconv->wSecond;
2244 ans += (double)timeconv->wMilliseconds / 1000;
2249 #include <sys/resource.h>
2251 double getCpuTime(int type)
2253 struct rusage used[1];
2254 struct timeval tv[1];
2258 gettimeofday(tv, NULL);
2259 return (double)tv->tv_sec + (double)tv->tv_usec / 1000000;
2262 getrusage(RUSAGE_SELF, used);
2263 return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
2266 getrusage(RUSAGE_SELF, used);
2267 return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
2274 uint bt_latchaudit (BtDb *bt)
2276 ushort idx, hashidx;
2283 posix_fadvise( bt->mgr->idx, 0, 0, POSIX_FADV_SEQUENTIAL);
2285 if( *(ushort *)(bt->mgr->latchmgr->lock) )
2286 fprintf(stderr, "Alloc page locked\n");
2287 *(ushort *)(bt->mgr->latchmgr->lock) = 0;
2289 for( idx = 1; idx <= bt->mgr->latchmgr->latchdeployed; idx++ ) {
2290 latch = bt->mgr->latchsets + idx;
2291 if( *latch->readwr->rin & MASK )
2292 fprintf(stderr, "latchset %d rwlocked for page %.8x\n", idx, latch->page_no);
2293 memset ((ushort *)latch->readwr, 0, sizeof(RWLock));
2295 if( *latch->access->rin & MASK )
2296 fprintf(stderr, "latchset %d accesslocked for page %.8x\n", idx, latch->page_no);
2297 memset ((ushort *)latch->access, 0, sizeof(RWLock));
2299 if( *latch->parent->rin & MASK )
2300 fprintf(stderr, "latchset %d parentlocked for page %.8x\n", idx, latch->page_no);
2301 memset ((ushort *)latch->access, 0, sizeof(RWLock));
2304 fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
2309 for( hashidx = 0; hashidx < bt->mgr->latchmgr->latchhash; hashidx++ ) {
2310 if( *(ushort *)(bt->mgr->latchmgr->table[hashidx].latch) )
2311 fprintf(stderr, "hash entry %d locked\n", hashidx);
2313 *(ushort *)(bt->mgr->latchmgr->table[hashidx].latch) = 0;
2315 if( idx = bt->mgr->latchmgr->table[hashidx].slot ) do {
2316 latch = bt->mgr->latchsets + idx;
2317 if( *(ushort *)latch->busy )
2318 fprintf(stderr, "latchset %d busylocked for page %.8x\n", idx, latch->page_no);
2319 *(ushort *)latch->busy = 0;
2321 fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
2322 } while( idx = latch->next );
2325 next = bt->mgr->latchmgr->nlatchpage + LATCH_page;
2326 page_no = LEAF_page;
2328 while( page_no < bt_getid(bt->mgr->latchmgr->alloc->right) ) {
2329 off64_t off = page_no << bt->mgr->page_bits;
2331 pread (bt->mgr->idx, bt->frame, bt->mgr->page_size, off);
2335 SetFilePointer (bt->mgr->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
2337 if( !ReadFile(bt->mgr->idx, bt->frame, bt->mgr->page_size, amt, NULL))
2338 fprintf(stderr, "page %.8x unable to read\n", page_no);
2340 if( *amt < bt->mgr->page_size )
2341 fprintf(stderr, "page %.8x unable to read\n", page_no);
2343 if( !bt->frame->free ) {
2344 for( idx = 0; idx++ < bt->frame->cnt - 1; ) {
2345 ptr = keyptr(bt->frame, idx+1);
2346 if( keycmp (keyptr(bt->frame, idx), ptr->key, ptr->len) >= 0 )
2347 fprintf(stderr, "page %.8x idx %.2x out of order\n", page_no, idx);
2349 if( !bt->frame->lvl )
2350 cnt += bt->frame->act;
2352 if( page_no > LEAF_page )
2366 // standalone program to index file of keys
2367 // then list them onto std-out
2370 void *index_file (void *arg)
2372 uint __stdcall index_file (void *arg)
2375 int line = 0, found = 0, cnt = 0;
2376 uid next, page_no = LEAF_page; // start on first page of leaves
2377 unsigned char key[256];
2378 ThreadArg *args = arg;
2379 int ch, len = 0, slot;
2385 bt = bt_open (args->mgr);
2387 switch(args->type | 0x20)
2390 fprintf(stderr, "started latch mgr audit\n");
2391 cnt = bt_latchaudit (bt);
2392 fprintf(stderr, "finished latch mgr audit, found %d keys\n", cnt);
2396 fprintf(stderr, "started pennysort for %s\n", args->infile);
2397 if( in = fopen (args->infile, "rb") )
2398 while( ch = getc(in), ch != EOF )
2403 if( bt_insertkey (bt, key, 10, 0, key + 10, len - 10) )
2404 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2407 else if( len < 255 )
2409 fprintf(stderr, "finished %s for %d keys\n", args->infile, line);
2413 fprintf(stderr, "started indexing for %s\n", args->infile);
2414 if( in = fopen (args->infile, "rb") )
2415 while( ch = getc(in), ch != EOF )
2420 if( args->num == 1 )
2421 sprintf((char *)key+len, "%.9d", 1000000000 - line), len += 9;
2423 else if( args->num )
2424 sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
2426 if( bt_insertkey (bt, key, len, 0, NULL, 0) )
2427 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2430 else if( len < 255 )
2432 fprintf(stderr, "finished %s for %d keys\n", args->infile, line);
2436 fprintf(stderr, "started deleting keys for %s\n", args->infile);
2437 if( in = fopen (args->infile, "rb") )
2438 while( ch = getc(in), ch != EOF )
2442 if( args->num == 1 )
2443 sprintf((char *)key+len, "%.9d", 1000000000 - line), len += 9;
2445 else if( args->num )
2446 sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
2448 if( bt_deletekey (bt, key, len, 0) )
2449 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
2452 else if( len < 255 )
2454 fprintf(stderr, "finished %s for keys, %d \n", args->infile, line);
2458 fprintf(stderr, "started finding keys for %s\n", args->infile);
2459 if( in = fopen (args->infile, "rb") )
2460 while( ch = getc(in), ch != EOF )
2464 if( args->num == 1 )
2465 sprintf((char *)key+len, "%.9d", 1000000000 - line), len += 9;
2467 else if( args->num )
2468 sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
2470 if( bt_findkey (bt, key, len, NULL, 0) == 0 )
2473 fprintf(stderr, "Error %d Syserr %d Line: %d\n", bt->err, errno, line), exit(0);
2476 else if( len < 255 )
2478 fprintf(stderr, "finished %s for %d keys, found %d\n", args->infile, line, found);
2482 fprintf(stderr, "started scanning\n");
2484 if( set->pool = bt_pinpool (bt, page_no) )
2485 set->page = bt_page (bt, set->pool, page_no);
2488 set->latch = bt_pinlatch (bt, page_no);
2489 bt_lockpage (BtLockRead, set->latch);
2490 next = bt_getid (set->page->right);
2491 cnt += set->page->act;
2493 for( slot = 0; slot++ < set->page->cnt; )
2494 if( next || slot < set->page->cnt )
2495 if( !slotptr(set->page, slot)->dead ) {
2496 ptr = keyptr(set->page, slot);
2497 fwrite (ptr->key, ptr->len, 1, stdout);
2498 fputc ('\n', stdout);
2501 bt_unlockpage (BtLockRead, set->latch);
2502 bt_unpinlatch (set->latch);
2503 bt_unpinpool (set->pool);
2504 } while( page_no = next );
2506 cnt--; // remove stopper key
2507 fprintf(stderr, " Total keys read %d\n", cnt);
2512 posix_fadvise( bt->mgr->idx, 0, 0, POSIX_FADV_SEQUENTIAL);
2514 fprintf(stderr, "started counting\n");
2515 next = bt->mgr->latchmgr->nlatchpage + LATCH_page;
2516 page_no = LEAF_page;
2518 while( page_no < bt_getid(bt->mgr->latchmgr->alloc->right) ) {
2519 uid off = page_no << bt->mgr->page_bits;
2521 pread (bt->mgr->idx, bt->frame, bt->mgr->page_size, off);
2525 SetFilePointer (bt->mgr->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
2527 if( !ReadFile(bt->mgr->idx, bt->frame, bt->mgr->page_size, amt, NULL))
2528 return bt->err = BTERR_map;
2530 if( *amt < bt->mgr->page_size )
2531 return bt->err = BTERR_map;
2533 if( !bt->frame->free && !bt->frame->lvl )
2534 cnt += bt->frame->act;
2535 if( page_no > LEAF_page )
2540 cnt--; // remove stopper key
2541 fprintf(stderr, " Total keys read %d\n", cnt);
2553 typedef struct timeval timer;
2555 int main (int argc, char **argv)
2557 int idx, cnt, len, slot, err;
2558 int segsize, bits = 16;
2575 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]);
2576 fprintf (stderr, " where page_bits is the page size in bits\n");
2577 fprintf (stderr, " mapped_segments is the number of mmap segments in buffer pool\n");
2578 fprintf (stderr, " seg_bits is the size of individual segments in buffer pool in pages in bits\n");
2579 fprintf (stderr, " line_numbers = 1 to append line numbers to keys\n");
2580 fprintf (stderr, " src_file1 thru src_filen are files of keys separated by newline\n");
2584 start = getCpuTime(0);
2587 bits = atoi(argv[3]);
2590 poolsize = atoi(argv[4]);
2593 fprintf (stderr, "Warning: no mapped_pool\n");
2595 if( poolsize > 65535 )
2596 fprintf (stderr, "Warning: mapped_pool > 65535 segments\n");
2599 segsize = atoi(argv[5]);
2601 segsize = 4; // 16 pages per mmap segment
2604 num = atoi(argv[6]);
2608 threads = malloc (cnt * sizeof(pthread_t));
2610 threads = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, cnt * sizeof(HANDLE));
2612 args = malloc (cnt * sizeof(ThreadArg));
2614 mgr = bt_mgr ((argv[1]), BT_rw, bits, poolsize, segsize, poolsize / 8);
2617 fprintf(stderr, "Index Open Error %s\n", argv[1]);
2623 for( idx = 0; idx < cnt; idx++ ) {
2624 args[idx].infile = argv[idx + 7];
2625 args[idx].type = argv[2][0];
2626 args[idx].mgr = mgr;
2627 args[idx].num = num;
2628 args[idx].idx = idx;
2630 if( err = pthread_create (threads + idx, NULL, index_file, args + idx) )
2631 fprintf(stderr, "Error creating thread %d\n", err);
2633 threads[idx] = (HANDLE)_beginthreadex(NULL, 65536, index_file, args + idx, 0, NULL);
2637 // wait for termination
2640 for( idx = 0; idx < cnt; idx++ )
2641 pthread_join (threads[idx], NULL);
2643 WaitForMultipleObjects (cnt, threads, TRUE, INFINITE);
2645 for( idx = 0; idx < cnt; idx++ )
2646 CloseHandle(threads[idx]);
2649 elapsed = getCpuTime(0) - start;
2650 fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2651 elapsed = getCpuTime(1);
2652 fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2653 elapsed = getCpuTime(2);
2654 fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);