]> pd.if.org Git - btree/blob - fosterbtreec.c
foster btree final code release
[btree] / fosterbtreec.c
1 // foster btree version d
2 // 26 DEC 2013
3
4 // author: karl malbrain, malbrain@cal.berkeley.edu
5
6 /*
7 This work, including the source code, documentation
8 and related data, is placed into the public domain.
9
10 The orginal author is Karl Malbrain.
11
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.
18 */
19
20 // Please see the project home page for documentation
21 // code.google.com/p/high-concurrency-btree
22
23 #define _FILE_OFFSET_BITS 64
24 #define _LARGEFILE64_SOURCE
25
26 #ifdef linux
27 #define _GNU_SOURCE
28 #endif
29
30 #ifdef unix
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <sys/time.h>
36 #include <sys/mman.h>
37 #include <errno.h>
38 #include <pthread.h>
39 #else
40 #define WIN32_LEAN_AND_MEAN
41 #include <windows.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <time.h>
45 #include <fcntl.h>
46 #include <process.h>
47 #include <intrin.h>
48 #endif
49
50 #include <memory.h>
51 #include <string.h>
52
53 typedef unsigned long long      uid;
54
55 #ifndef unix
56 typedef unsigned long long      off64_t;
57 typedef unsigned short          ushort;
58 typedef unsigned int            uint;
59 #endif
60
61 #define BT_ro 0x6f72    // ro
62 #define BT_rw 0x7772    // rw
63
64 #define BT_maxbits              24                                      // maximum page size in bits
65 #define BT_minbits              9                                       // minimum page size in bits
66 #define BT_minpage              (1 << BT_minbits)       // minimum page size
67 #define BT_maxpage              (1 << BT_maxbits)       // maximum page size
68
69 /*
70 There are five lock types for each node in three independent sets: 
71 1. (set 1) AccessIntent: Sharable. Going to Read the node. Incompatible with NodeDelete. 
72 2. (set 1) NodeDelete: Exclusive. About to release the node. Incompatible with AccessIntent. 
73 3. (set 2) ReadLock: Sharable. Read the node. Incompatible with WriteLock. 
74 4. (set 2) WriteLock: Exclusive. Modify the node. Incompatible with ReadLock and other WriteLocks. 
75 5. (set 3) ParentLock: Exclusive. Have parent adopt/delete maximum foster child from the node.
76 */
77
78 typedef enum{
79         BtLockAccess,
80         BtLockDelete,
81         BtLockRead,
82         BtLockWrite,
83         BtLockParent
84 }BtLock;
85
86 //      Define the length of the page and key pointers
87
88 #define BtId 6
89
90 //      Page key slot definition.
91
92 //      If BT_maxbits is 15 or less, you can save 4 bytes
93 //      for each key stored by making the first two uints
94 //      into ushorts.  You can also save 4 bytes by removing
95 //      the tod field from the key.
96
97 //      Keys are marked dead, but remain on the page until
98 //      it cleanup is called. The fence key (highest key) for
99 //      the page is always present, even after cleanup.
100
101 typedef struct {
102         uint off:BT_maxbits;            // page offset for key start
103         uint dead:1;                            // set for deleted key
104         uint tod;                                       // time-stamp for key
105         unsigned char id[BtId];         // id associated with key
106 } BtSlot;
107
108 //      The key structure occupies space at the upper end of
109 //      each page.  It's a length byte followed by the value
110 //      bytes.
111
112 typedef struct {
113         unsigned char len;
114         unsigned char key[1];
115 } *BtKey;
116
117 //      The first part of an index page.
118 //      It is immediately followed
119 //      by the BtSlot array of keys.
120
121 typedef struct Page {
122         uint cnt;                                       // count of keys in page
123         uint act;                                       // count of active keys
124         uint min;                                       // next key offset
125         uint foster;                            // count of foster children
126         unsigned char bits;                     // page size in bits
127         unsigned char lvl:6;            // level of page
128         unsigned char kill:1;           // page is being deleted
129         unsigned char dirty:1;          // page needs to be cleaned
130         unsigned char right[BtId];      // page number to right
131 } *BtPage;
132
133 //      latch table lock structure
134
135 // exclusive is set for write access
136 // share is count of read accessors
137 // pending is count of waiting writers
138 // grant write lock when share == 0
139
140 typedef struct {
141 #ifdef unix
142         volatile uint exclusive:1;
143         volatile uint pending:15;
144         volatile uint share:16;
145         pthread_mutex_t mut[1];
146         pthread_cond_t cond[1];
147 #else
148         SRWLOCK srw[1];
149 #endif
150 } BtLatch;
151
152 typedef struct {
153         BtLatch readwr[1];              // read/write page lock
154         BtLatch access[1];              // Access Intent/Page delete
155         BtLatch parent[1];              // adoption of foster children
156 } BtLatchSet;
157
158 //      The memory mapping pool table buffer manager entry
159
160 typedef struct {
161         unsigned long long int lru;     // number of times accessed
162         uid  basepage;                          // mapped base page number
163         char *map;                                      // mapped memory pointer
164         uint pin;                                       // mapped page pin counter
165         uint slot;                                      // slot index in this array
166         void *hashprev;                         // previous pool entry for the same hash idx
167         void *hashnext;                         // next pool entry for the same hash idx
168 #ifndef unix
169         HANDLE hmap;
170 #endif
171 //      array of page latch sets, one for each page in map segment
172         BtLatchSet pagelatch[0];
173 } BtPool;
174
175 //      The object structure for Btree access
176
177 typedef struct {
178         uint page_size;                         // page size    
179         uint page_bits;                         // page size in bits    
180         uint seg_bits;                          // seg size in pages in bits
181         uint mode;                                      // read-write mode
182 #ifdef unix
183         int idx;
184         char *pooladvise;                       // bit maps for pool page advisements
185 #else
186         HANDLE idx;
187 #endif
188         uint poolcnt;                           // highest page pool node in use
189         uint poolmax;                           // highest page pool node allocated
190         uint poolmask;                          // total size of pages in mmap segment - 1
191         uint hashsize;                          // size of Hash Table for pool entries
192         volatile uint evicted;          // last evicted hash table slot
193         ushort *hash;                           // hash table of pool entries
194         BtLatch *latch;                         // latches for hash table slots
195         char *nodes;                            // memory pool page segments
196 } BtMgr;
197
198 typedef struct {
199         BtMgr *mgr;                     // buffer manager for thread
200         BtPage temp;            // temporary frame buffer (memory mapped/file IO)
201         BtPage alloc;           // frame buffer for alloc page ( page 0 )
202         BtPage cursor;          // cached frame for start/next (never mapped)
203         BtPage frame;           // spare frame for the page split (never mapped)
204         BtPage zero;            // page frame for zeroes at end of file
205         BtPage page;            // current page
206         uid page_no;            // current page number  
207         uid cursor_page;        // current cursor page number   
208         unsigned char *mem;     // frame, cursor, page memory buffer
209         int err;                        // last error
210 } BtDb;
211
212 typedef enum {
213         BTERR_ok = 0,
214         BTERR_struct,
215         BTERR_ovflw,
216         BTERR_lock,
217         BTERR_map,
218         BTERR_wrt,
219         BTERR_hash
220 } BTERR;
221
222 // B-Tree functions
223 extern void bt_close (BtDb *bt);
224 extern BtDb *bt_open (BtMgr *mgr);
225 extern BTERR  bt_insertkey (BtDb *bt, unsigned char *key, uint len, uid id, uint tod);
226 extern BTERR  bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl);
227 extern uid bt_findkey    (BtDb *bt, unsigned char *key, uint len);
228 extern uint bt_startkey  (BtDb *bt, unsigned char *key, uint len);
229 extern uint bt_nextkey   (BtDb *bt, uint slot);
230
231 //      manager functions
232 extern BtMgr *bt_mgr (char *name, uint mode, uint bits, uint poolsize, uint segsize, uint hashsize);
233 void bt_mgrclose (BtMgr *mgr);
234
235 //  Helper functions to return cursor slot values
236
237 extern BtKey bt_key (BtDb *bt, uint slot);
238 extern uid bt_uid (BtDb *bt, uint slot);
239 extern uint bt_tod (BtDb *bt, uint slot);
240
241 //  BTree page number constants
242 #define ALLOC_page              0
243 #define ROOT_page               1
244 #define LEAF_page               2
245
246 //      Number of levels to create in a new BTree
247
248 #define MIN_lvl                 2
249
250 //  The page is allocated from low and hi ends.
251 //  The key offsets and row-id's are allocated
252 //  from the bottom, while the text of the key
253 //  is allocated from the top.  When the two
254 //  areas meet, the page is split into two.
255
256 //  A key consists of a length byte, two bytes of
257 //  index number (0 - 65534), and up to 253 bytes
258 //  of key value.  Duplicate keys are discarded.
259 //  Associated with each key is a 48 bit row-id.
260
261 //  The b-tree root is always located at page 1.
262 //      The first leaf page of level zero is always
263 //      located on page 2.
264
265 //      When to root page fills, it is split in two and
266 //      the tree height is raised by a new root at page
267 //      one with two keys.
268
269 //      Deleted keys are marked with a dead bit until
270 //      page cleanup The fence key for a node is always
271 //      present, even after deletion and cleanup.
272
273 //  Groups of pages called segments from the btree are
274 //  cached with memory mapping. A hash table is used to keep
275 //  track of the cached segments.  This behaviour is controlled
276 //  by the cache block size parameter to bt_open.
277
278 //  To achieve maximum concurrency one page is locked at a time
279 //  as the tree is traversed to find leaf key in question.
280
281 //      An adoption traversal leaves the parent node locked as the
282 //      tree is traversed to the level in quesiton.
283
284 //  Page 0 is dedicated to lock for new page extensions,
285 //      and chains empty pages together for reuse.
286
287 //      Empty pages are chained together through the ALLOC page and reused.
288
289 //      Access macros to address slot and key values from the page
290
291 #define slotptr(page, slot) (((BtSlot *)(page+1)) + (slot-1))
292 #define keyptr(page, slot) ((BtKey)((unsigned char*)(page) + slotptr(page, slot)->off))
293
294 void bt_putid(unsigned char *dest, uid id)
295 {
296 int i = BtId;
297
298         while( i-- )
299                 dest[i] = (unsigned char)id, id >>= 8;
300 }
301
302 uid bt_getid(unsigned char *src)
303 {
304 uid id = 0;
305 int i;
306
307         for( i = 0; i < BtId; i++ )
308                 id <<= 8, id |= *src++; 
309
310         return id;
311 }
312
313 void bt_mgrclose (BtMgr *mgr)
314 {
315 BtPool *pool;
316 uint slot;
317
318         // release mapped pages
319         //      note that slot zero is never used
320
321         for( slot = 1; slot < mgr->poolmax; slot++ ) {
322                 pool = (BtPool *)(mgr->nodes + slot * (sizeof(BtPool) + (mgr->poolmask + 1) * sizeof(BtLatchSet)));
323                 if( pool->slot )
324 #ifdef unix
325                         munmap (pool->map, (mgr->poolmask+1) << mgr->page_bits);
326 #else
327                 {
328                         FlushViewOfFile(pool->map, 0);
329                         UnmapViewOfFile(pool->map);
330                         CloseHandle(pool->hmap);
331                 }
332 #endif
333         }
334
335 #ifdef unix
336         close (mgr->idx);
337         free (mgr->nodes);
338         free (mgr->hash);
339         free (mgr->latch);
340         free (mgr->pooladvise);
341         free (mgr);
342 #else
343         FlushFileBuffers(mgr->idx);
344         CloseHandle(mgr->idx);
345         GlobalFree (mgr->nodes);
346         GlobalFree (mgr->hash);
347         GlobalFree (mgr->latch);
348         GlobalFree (mgr);
349 #endif
350 }
351
352 //      close and release memory
353
354 void bt_close (BtDb *bt)
355 {
356 #ifdef unix
357         if ( bt->mem )
358                 free (bt->mem);
359 #else
360         if ( bt->mem)
361                 VirtualFree (bt->mem, 0, MEM_RELEASE);
362 #endif
363         free (bt);
364 }
365
366 //  open/create new btree buffer manager
367
368 //      call with file_name, BT_openmode, bits in page size (e.g. 16),
369 //              size of mapped page pool (e.g. 8192)
370
371 BtMgr *bt_mgr (char *name, uint mode, uint bits, uint poolmax, uint segsize, uint hashsize)
372 {
373 uint lvl, attr, cacheblk, last, slot, idx;
374 BtPage alloc;
375 int lockmode;
376 off64_t size;
377 uint amt[1];
378 BtMgr* mgr;
379 BtKey key;
380
381 #ifndef unix
382 SYSTEM_INFO sysinfo[1];
383 #endif
384
385         // determine sanity of page size and buffer pool
386
387         if( bits > BT_maxbits )
388                 bits = BT_maxbits;
389         else if( bits < BT_minbits )
390                 bits = BT_minbits;
391
392         if( !poolmax )
393                 return NULL;    // must have buffer pool
394
395 #ifdef unix
396         mgr = calloc (1, sizeof(BtMgr));
397
398         switch (mode & 0x7fff)
399         {
400         case BT_rw:
401                 mgr->idx = open ((char*)name, O_RDWR | O_CREAT, 0666);
402                 lockmode = 1;
403                 break;
404
405         case BT_ro:
406         default:
407                 mgr->idx = open ((char*)name, O_RDONLY);
408                 lockmode = 0;
409                 break;
410         }
411         if( mgr->idx == -1 )
412                 return free(mgr), NULL;
413         
414         cacheblk = 4096;        // minimum mmap segment size for unix
415
416 #else
417         mgr = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, sizeof(BtMgr));
418         attr = FILE_ATTRIBUTE_NORMAL;
419         switch (mode & 0x7fff)
420         {
421         case BT_rw:
422                 mgr->idx = CreateFile(name, GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, attr, NULL);
423                 lockmode = 1;
424                 break;
425
426         case BT_ro:
427         default:
428                 mgr->idx = CreateFile(name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, attr, NULL);
429                 lockmode = 0;
430                 break;
431         }
432         if( mgr->idx == INVALID_HANDLE_VALUE )
433                 return GlobalFree(mgr), NULL;
434
435         // normalize cacheblk to multiple of sysinfo->dwAllocationGranularity
436         GetSystemInfo(sysinfo);
437         cacheblk = sysinfo->dwAllocationGranularity;
438 #endif
439
440 #ifdef unix
441         alloc = malloc (BT_maxpage);
442         *amt = 0;
443
444         // read minimum page size to get root info
445
446         if( size = lseek (mgr->idx, 0L, 2) ) {
447                 if( pread(mgr->idx, alloc, BT_minpage, 0) == BT_minpage )
448                         bits = alloc->bits;
449                 else
450                         return free(mgr), free(alloc), NULL;
451         } else if( mode == BT_ro )
452                 return bt_mgrclose (mgr), NULL;
453 #else
454         alloc = VirtualAlloc(NULL, BT_maxpage, MEM_COMMIT, PAGE_READWRITE);
455         size = GetFileSize(mgr->idx, amt);
456
457         if( size || *amt ) {
458                 if( !ReadFile(mgr->idx, (char *)alloc, BT_minpage, amt, NULL) )
459                         return bt_mgrclose (mgr), NULL;
460                 bits = alloc->bits;
461         } else if( mode == BT_ro )
462                 return bt_mgrclose (mgr), NULL;
463 #endif
464
465         mgr->page_size = 1 << bits;
466         mgr->page_bits = bits;
467
468         mgr->poolmax = poolmax;
469         mgr->mode = mode;
470
471         if( cacheblk < mgr->page_size )
472                 cacheblk = mgr->page_size;
473
474         //  mask for partial memmaps
475
476         mgr->poolmask = (cacheblk >> bits) - 1;
477
478         //      see if requested size of pages per memmap is greater
479
480         if( (1 << segsize) > mgr->poolmask )
481                 mgr->poolmask = (1 << segsize) - 1;
482
483         mgr->seg_bits = 0;
484
485         while( (1 << mgr->seg_bits) <= mgr->poolmask )
486                 mgr->seg_bits++;
487
488         mgr->hashsize = hashsize;
489
490 #ifdef unix
491         mgr->nodes = calloc (poolmax, (sizeof(BtPool) + (mgr->poolmask + 1) * sizeof(BtLatchSet)));
492         mgr->hash = calloc (hashsize, sizeof(ushort));
493         mgr->latch = calloc (hashsize, sizeof(BtLatch));
494         mgr->pooladvise = calloc (poolmax, (mgr->poolmask + 1) / 8);
495 #else
496         mgr->nodes = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, poolmax * (sizeof(BtPool) + (mgr->poolmask + 1) * sizeof(BtLatchSet)));
497         mgr->hash = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, hashsize * sizeof(ushort));
498         mgr->latch = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, hashsize * sizeof(BtLatch));
499 #endif
500
501         //  initialize buffer pool page latches
502
503         for( slot = 1; slot < poolmax; slot++ ) {
504           BtLatchSet *latchset = (BtLatchSet *)(mgr->nodes + slot * (sizeof(BtPool) + (mgr->poolmask + 1) * sizeof(BtLatchSet)));
505           for( idx = 0; idx < mgr->poolmask + 1; idx++ ) {
506 #ifdef  unix
507                 pthread_mutex_init (latchset[idx].readwr->mut, NULL);
508                 pthread_cond_init (latchset[idx].readwr->cond, NULL);
509                 pthread_mutex_init (latchset[idx].access->mut, NULL);
510                 pthread_cond_init (latchset[idx].access->cond, NULL);
511                 pthread_mutex_init (latchset[idx].parent->mut, NULL);
512                 pthread_cond_init (latchset[idx].parent->cond, NULL);
513 #else
514                 InitializeSRWLock (latchset[idx].readwr->srw);
515                 InitializeSRWLock (latchset[idx].access->srw);
516                 InitializeSRWLock (latchset[idx].parent->srw);
517 #endif
518           }
519         }
520
521         //  initialize buffer pool mgr latches
522
523         for( slot = 0; slot < hashsize; slot++ ) {
524 #ifdef unix
525                 pthread_mutex_init (mgr->latch[slot].mut, NULL);
526                 pthread_cond_init (mgr->latch[slot].cond, NULL);
527 #else
528                 InitializeSRWLock (mgr->latch[slot].srw);
529 #endif
530         }
531
532         if( size || *amt )
533                 goto mgrxit;
534
535         // initializes an empty b-tree with root page and page of leaves
536
537         memset (alloc, 0, 1 << bits);
538         bt_putid(alloc->right, MIN_lvl+1);
539         alloc->bits = mgr->page_bits;
540
541 #ifdef unix
542         if( write (mgr->idx, alloc, mgr->page_size) < mgr->page_size )
543                 return bt_mgrclose (mgr), NULL;
544 #else
545         if( !WriteFile (mgr->idx, (char *)alloc, mgr->page_size, amt, NULL) )
546                 return bt_mgrclose (mgr), NULL;
547
548         if( *amt < mgr->page_size )
549                 return bt_mgrclose (mgr), NULL;
550 #endif
551
552         memset (alloc, 0, 1 << bits);
553         alloc->bits = mgr->page_bits;
554
555         for( lvl=MIN_lvl; lvl--; ) {
556                 slotptr(alloc, 1)->off = mgr->page_size - 3;
557                 bt_putid(slotptr(alloc, 1)->id, lvl ? MIN_lvl - lvl + 1 : 0);           // next(lower) page number
558                 key = keyptr(alloc, 1);
559                 key->len = 2;                   // create stopper key
560                 key->key[0] = 0xff;
561                 key->key[1] = 0xff;
562                 alloc->min = mgr->page_size - 3;
563                 alloc->lvl = lvl;
564                 alloc->cnt = 1;
565                 alloc->act = 1;
566 #ifdef unix
567                 if( write (mgr->idx, alloc, mgr->page_size) < mgr->page_size )
568                         return bt_mgrclose (mgr), NULL;
569 #else
570                 if( !WriteFile (mgr->idx, (char *)alloc, mgr->page_size, amt, NULL) )
571                         return bt_mgrclose (mgr), NULL;
572
573                 if( *amt < mgr->page_size )
574                         return bt_mgrclose (mgr), NULL;
575 #endif
576         }
577
578         // create empty page area by writing last page of first
579         // segment area (other pages are zeroed by O/S)
580
581         if( mgr->poolmask ) {
582                 memset(alloc, 0, mgr->page_size);
583                 last = mgr->poolmask;
584
585                 while( last < MIN_lvl + 1 )
586                         last += mgr->poolmask + 1;
587
588 #ifdef unix
589                 pwrite(mgr->idx, alloc, mgr->page_size, last << mgr->page_bits);
590 #else
591                 SetFilePointer (mgr->idx, last << mgr->page_bits, NULL, FILE_BEGIN);
592                 if( !WriteFile (mgr->idx, (char *)alloc, mgr->page_size, amt, NULL) )
593                         return bt_mgrclose (mgr), NULL;
594                 if( *amt < mgr->page_size )
595                         return bt_mgrclose (mgr), NULL;
596 #endif
597         }
598
599 mgrxit:
600 #ifdef unix
601         free (alloc);
602 #else
603         VirtualFree (alloc, 0, MEM_RELEASE);
604 #endif
605         return mgr;
606 }
607
608 //      open BTree access method
609 //      based on buffer manager
610
611 BtDb *bt_open (BtMgr *mgr)
612 {
613 BtDb *bt = malloc (sizeof(*bt));
614
615         memset (bt, 0, sizeof(*bt));
616         bt->mgr = mgr;
617 #ifdef unix
618         bt->mem = malloc (3 *mgr->page_size);
619 #else
620         bt->mem = VirtualAlloc(NULL, 3 * mgr->page_size, MEM_COMMIT, PAGE_READWRITE);
621 #endif
622         bt->frame = (BtPage)bt->mem;
623         bt->zero = (BtPage)(bt->mem + 1 * mgr->page_size);
624         bt->cursor = (BtPage)(bt->mem + 2 * mgr->page_size);
625         return bt;
626 }
627
628 //  compare two keys, returning > 0, = 0, or < 0
629 //  as the comparison value
630
631 int keycmp (BtKey key1, unsigned char *key2, uint len2)
632 {
633 uint len1 = key1->len;
634 int ans;
635
636         if( ans = memcmp (key1->key, key2, len1 > len2 ? len2 : len1) )
637                 return ans;
638
639         if( len1 > len2 )
640                 return 1;
641         if( len1 < len2 )
642                 return -1;
643
644         return 0;
645 }
646
647 //      Latch Manager
648
649 //  wait if exclusive request is pending, or granted
650 //      and add 1 to the share count
651
652 void bt_readlock(BtLatch *latch)
653 {
654 #ifdef unix
655         pthread_mutex_lock (latch->mut);
656
657         while( latch->pending || latch->exclusive )
658                 pthread_cond_wait (latch->cond, latch->mut);
659
660         //  add one to readers counter
661
662         latch->share++;
663         pthread_mutex_unlock (latch->mut);
664 #else
665         AcquireSRWLockShared (latch->srw);
666 #endif
667 }
668
669 //      wait for other read and write latches to relinquish
670
671 void bt_writelock(BtLatch *latch)
672 {
673 #ifdef unix
674         pthread_mutex_lock (latch->mut);
675         latch->pending++;
676
677         while( latch->share || latch->exclusive )
678                 pthread_cond_wait (latch->cond, latch->mut);
679
680         latch->exclusive = 1;
681         latch->pending--;
682         pthread_mutex_unlock (latch->mut);
683 #else
684         AcquireSRWLockExclusive (latch->srw);
685 #endif
686 }
687
688 //      try to obtain write lock
689
690 //      return 1 if obtained,
691 //              0 if already write or read locked
692
693 int bt_writetry(BtLatch *latch)
694 {
695 int result = 0;
696
697 #ifdef unix
698         pthread_mutex_lock (latch->mut);
699
700         if( !latch->share && !latch->exclusive )
701                 result = latch->exclusive = 1;
702
703         pthread_mutex_unlock (latch->mut);
704 #else
705         result = TryAcquireSRWLockExclusive (latch->srw);
706 #endif
707         return result;
708 }
709
710 //      clear write mode
711
712 void bt_releasewrite(BtLatch *latch)
713 {
714 #ifdef unix
715         pthread_mutex_lock (latch->mut);
716         latch->exclusive = 0;
717         pthread_cond_broadcast (latch->cond);
718         pthread_mutex_unlock (latch->mut);
719 #else
720         ReleaseSRWLockExclusive (latch->srw);
721 #endif
722 }
723
724 //      decrement reader count
725
726 void bt_releaseread(BtLatch *latch)
727 {
728 #ifdef unix
729         pthread_mutex_lock (latch->mut);
730
731         if( !--latch->share && latch->pending )
732                 pthread_cond_broadcast (latch->cond);
733
734         pthread_mutex_unlock (latch->mut);
735 #else
736         ReleaseSRWLockShared (latch->srw);
737 #endif
738 }
739
740 //      Buffer Pool mgr
741
742 // find segment in pool
743 // must be called with hashslot idx locked
744 //      return NULL if not there
745 //      otherwise return node
746
747 BtPool *bt_findpool(BtDb *bt, uid page_no, uint idx)
748 {
749 BtPool *pool;
750 uint slot;
751
752         // compute start of hash chain in pool
753
754         if( slot = bt->mgr->hash[idx] ) 
755                 pool = (BtPool *)(bt->mgr->nodes + slot * (sizeof(BtPool) + (bt->mgr->poolmask + 1) * sizeof(BtLatchSet)));
756         else
757                 return NULL;
758
759         page_no &= ~bt->mgr->poolmask;
760
761         while( pool->basepage != page_no )
762           if( pool = pool->hashnext )
763                 continue;
764           else
765                 return NULL;
766
767         return pool;
768 }
769
770 // add segment to hash table
771
772 void bt_linkhash(BtDb *bt, BtPool *pool, uid page_no, int idx)
773 {
774 BtPool *node;
775 uint slot;
776
777         pool->hashprev = pool->hashnext = NULL;
778         pool->basepage = page_no & ~bt->mgr->poolmask;
779         pool->lru = 1;
780
781         if( slot = bt->mgr->hash[idx] ) {
782                 node = (BtPool *)(bt->mgr->nodes + slot * (sizeof(BtPool) + (bt->mgr->poolmask + 1) * sizeof(BtLatchSet)));
783                 pool->hashnext = node;
784                 node->hashprev = pool;
785         }
786
787         bt->mgr->hash[idx] = pool->slot;
788 }
789
790 //      find best segment to evict from buffer pool
791
792 BtPool *bt_findlru (BtDb *bt, uint hashslot)
793 {
794 unsigned long long int target = ~0LL;
795 BtPool *pool = NULL, *node;
796
797         if( !hashslot )
798                 return NULL;
799
800         node = (BtPool *)(bt->mgr->nodes + hashslot * (sizeof(BtPool) + (bt->mgr->poolmask + 1) * sizeof(BtLatchSet)));
801
802         //      scan pool entries under hash table slot
803
804         do {
805           if( node->pin )
806                 continue;
807           if( node->lru > target )
808                 continue;
809           target = node->lru;
810           pool = node;
811         } while( node = node->hashnext );
812
813         return pool;
814 }
815
816 //  map new buffer pool segment to virtual memory
817
818 BTERR bt_mapsegment(BtDb *bt, BtPool *pool, uid page_no)
819 {
820 off64_t off = (page_no & ~bt->mgr->poolmask) << bt->mgr->page_bits;
821 off64_t limit = off + ((bt->mgr->poolmask+1) << bt->mgr->page_bits);
822 int flag;
823
824 #ifdef unix
825         flag = PROT_READ | ( bt->mgr->mode == BT_ro ? 0 : PROT_WRITE );
826         pool->map = mmap (0, (bt->mgr->poolmask+1) << bt->mgr->page_bits, flag, MAP_SHARED, bt->mgr->idx, off);
827         if( pool->map == MAP_FAILED )
828                 return bt->err = BTERR_map;
829         // clear out madvise issued bits
830         memset (bt->mgr->pooladvise + pool->slot * (bt->mgr->poolmask + 1) / 8, 0, (bt->mgr->poolmask + 1)/8);
831 #else
832         flag = ( bt->mgr->mode == BT_ro ? PAGE_READONLY : PAGE_READWRITE );
833         pool->hmap = CreateFileMapping(bt->mgr->idx, NULL, flag, (DWORD)(limit >> 32), (DWORD)limit, NULL);
834         if( !pool->hmap )
835                 return bt->err = BTERR_map;
836
837         flag = ( bt->mgr->mode == BT_ro ? FILE_MAP_READ : FILE_MAP_WRITE );
838         pool->map = MapViewOfFile(pool->hmap, flag, (DWORD)(off >> 32), (DWORD)off, (bt->mgr->poolmask+1) << bt->mgr->page_bits);
839         if( !pool->map )
840                 return bt->err = BTERR_map;
841 #endif
842         return bt->err = 0;
843 }
844
845 //      find or place requested page in segment-pool
846 //      return pool table entry, incrementing pin
847
848 BtPool *bt_pinpage(BtDb *bt, uid page_no)
849 {
850 BtPool *pool, *node, *next;
851 uint slot, idx, victim;
852 BtLatchSet *set;
853
854         //      lock hash table chain
855
856         idx = (uint)(page_no >> bt->mgr->seg_bits) % bt->mgr->hashsize;
857         bt_readlock (&bt->mgr->latch[idx]);
858
859         //      look up in hash table
860
861         if( pool = bt_findpool(bt, page_no, idx) ) {
862 #ifdef unix
863                 __sync_fetch_and_add(&pool->pin, 1);
864 #else
865                 _InterlockedIncrement (&pool->pin);
866 #endif
867                 bt_releaseread (&bt->mgr->latch[idx]);
868                 pool->lru++;
869                 return pool;
870         }
871
872         //      upgrade to write lock
873
874         bt_releaseread (&bt->mgr->latch[idx]);
875         bt_writelock (&bt->mgr->latch[idx]);
876
877         // try to find page in pool with write lock
878
879         if( pool = bt_findpool(bt, page_no, idx) ) {
880 #ifdef unix
881                 __sync_fetch_and_add(&pool->pin, 1);
882 #else
883                 _InterlockedIncrement (&pool->pin);
884 #endif
885                 bt_releasewrite (&bt->mgr->latch[idx]);
886                 pool->lru++;
887                 return pool;
888         }
889
890         // allocate a new pool node
891         // and add to hash table
892
893 #ifdef unix
894         slot = __sync_fetch_and_add(&bt->mgr->poolcnt, 1);
895 #else
896         slot = _InterlockedIncrement (&bt->mgr->poolcnt) - 1;
897 #endif
898
899         if( ++slot < bt->mgr->poolmax ) {
900                 pool = (BtPool *)(bt->mgr->nodes + slot * (sizeof(BtPool) + (bt->mgr->poolmask + 1) * sizeof(BtLatchSet)));
901                 pool->slot = slot;
902
903                 if( bt_mapsegment(bt, pool, page_no) )
904                         return NULL;
905
906                 bt_linkhash(bt, pool, page_no, idx);
907 #ifdef unix
908                 __sync_fetch_and_add(&pool->pin, 1);
909 #else
910                 _InterlockedIncrement (&pool->pin);
911 #endif
912                 bt_releasewrite (&bt->mgr->latch[idx]);
913                 return pool;
914         }
915
916         // pool table is full
917         //      find best pool entry to evict
918
919 #ifdef unix
920         __sync_fetch_and_add(&bt->mgr->poolcnt, -1);
921 #else
922         _InterlockedDecrement (&bt->mgr->poolcnt);
923 #endif
924
925         while( 1 ) {
926 #ifdef unix
927                 victim = __sync_fetch_and_add(&bt->mgr->evicted, 1);
928 #else
929                 victim = _InterlockedIncrement (&bt->mgr->evicted) - 1;
930 #endif
931                 victim %= bt->mgr->hashsize;
932
933                 // try to get write lock
934                 //      skip entry if not obtained
935
936                 if( !bt_writetry (&bt->mgr->latch[victim]) )
937                         continue;
938
939                 //  if cache entry is empty
940                 //      or no slots are unpinned
941                 //      skip this entry
942
943                 if( !(pool = bt_findlru(bt, bt->mgr->hash[victim])) ) {
944                         bt_releasewrite (&bt->mgr->latch[victim]);
945                         continue;
946                 }
947
948                 // unlink victim pool node from hash table
949
950                 if( node = pool->hashprev )
951                         node->hashnext = pool->hashnext;
952                 else if( node = pool->hashnext )
953                         bt->mgr->hash[victim] = node->slot;
954                 else
955                         bt->mgr->hash[victim] = 0;
956
957                 if( node = pool->hashnext )
958                         node->hashprev = pool->hashprev;
959
960                 bt_releasewrite (&bt->mgr->latch[victim]);
961
962                 //      remove old file mapping
963 #ifdef unix
964                 munmap (pool->map, (bt->mgr->poolmask+1) << bt->mgr->page_bits);
965 #else
966                 FlushViewOfFile(pool->map, 0);
967                 UnmapViewOfFile(pool->map);
968                 CloseHandle(pool->hmap);
969 #endif
970                 pool->map = NULL;
971
972                 //  create new pool mapping
973                 //  and link into hash table
974
975                 if( bt_mapsegment(bt, pool, page_no) )
976                         return NULL;
977
978                 bt_linkhash(bt, pool, page_no, idx);
979 #ifdef unix
980                 __sync_fetch_and_add(&pool->pin, 1);
981 #else
982                 _InterlockedIncrement (&pool->pin);
983 #endif
984                 bt_releasewrite (&bt->mgr->latch[idx]);
985                 return pool;
986         }
987 }
988
989 // place write, read, or parent lock on requested page_no.
990 //      pin to buffer pool and return page pointer
991
992 BTERR bt_lockpage(BtDb *bt, uid page_no, BtLock mode, BtPage *pageptr)
993 {
994 BtLatchSet *set;
995 BtPool *pool;
996 uint subpage;
997 BtPage page;
998
999         //      find/create maping in pool table
1000         //        and pin our pool slot
1001
1002         if( pool = bt_pinpage(bt, page_no) )
1003                 subpage = (uint)(page_no & bt->mgr->poolmask); // page within mapping
1004         else
1005                 return bt->err;
1006
1007         set = pool->pagelatch + subpage;
1008         page = (BtPage)(pool->map + (subpage << bt->mgr->page_bits));
1009 #ifdef unix
1010         {
1011         uint idx = subpage / 8;
1012         uint bit = subpage % 8;
1013
1014                 if( !((bt->mgr->pooladvise + pool->slot * (bt->mgr->poolmask + 1)/8)[idx] >> bit) & 1 ) {
1015                   madvise (page, bt->mgr->page_size, MADV_WILLNEED);
1016                   (bt->mgr->pooladvise + pool->slot * (bt->mgr->poolmask + 1)/8)[idx] |= 1 << bit;
1017                 }
1018         }
1019 #endif
1020
1021         switch( mode ) {
1022         case BtLockRead:
1023                 bt_readlock (set->readwr);
1024                 break;
1025         case BtLockWrite:
1026                 bt_writelock (set->readwr);
1027                 break;
1028         case BtLockAccess:
1029                 bt_readlock (set->access);
1030                 break;
1031         case BtLockDelete:
1032                 bt_writelock (set->access);
1033                 break;
1034         case BtLockParent:
1035                 bt_writelock (set->parent);
1036                 break;
1037         default:
1038                 return bt->err = BTERR_lock;
1039         }
1040
1041         if( pageptr )
1042                 *pageptr = page;
1043
1044         return bt->err = 0;
1045 }
1046
1047 // remove write, read, or parent lock on requested page_no.
1048
1049 BTERR bt_unlockpage(BtDb *bt, uid page_no, BtLock mode)
1050 {
1051 uint subpage, idx;
1052 BtLatchSet *set;
1053 BtPool *pool;
1054
1055         //      since page is pinned
1056         //      it should still be in the buffer pool
1057         //      and is in no danger of being a victim for reuse
1058
1059         idx = (uint)(page_no >> bt->mgr->seg_bits) % bt->mgr->hashsize;
1060         bt_readlock (&bt->mgr->latch[idx]);
1061
1062         if( pool = bt_findpool(bt, page_no, idx) )
1063                 subpage = (uint)(page_no & bt->mgr->poolmask);
1064         else
1065                 return bt->err = BTERR_hash;
1066
1067         bt_releaseread (&bt->mgr->latch[idx]);
1068         set = pool->pagelatch + subpage;
1069
1070         switch( mode ) {
1071         case BtLockRead:
1072                 bt_releaseread (set->readwr);
1073                 break;
1074         case BtLockWrite:
1075                 bt_releasewrite (set->readwr);
1076                 break;
1077         case BtLockAccess:
1078                 bt_releaseread (set->access);
1079                 break;
1080         case BtLockDelete:
1081                 bt_releasewrite (set->access);
1082                 break;
1083         case BtLockParent:
1084                 bt_releasewrite (set->parent);
1085                 break;
1086         default:
1087                 return bt->err = BTERR_lock;
1088         }
1089
1090 #ifdef  unix
1091         __sync_fetch_and_add(&pool->pin, -1);
1092 #else
1093         _InterlockedDecrement (&pool->pin);
1094 #endif
1095         return bt->err = 0;
1096 }
1097
1098 //      deallocate a deleted page
1099 //      place on free chain out of allocator page
1100
1101 BTERR bt_freepage(BtDb *bt, uid page_no)
1102 {
1103         //  obtain delete lock on deleted page
1104
1105         if( bt_lockpage(bt, page_no, BtLockDelete, NULL) )
1106                 return bt->err;
1107
1108         //  obtain write lock on deleted page
1109
1110         if( bt_lockpage(bt, page_no, BtLockWrite, &bt->temp) )
1111                 return bt->err;
1112
1113         //      lock allocation page
1114
1115         if ( bt_lockpage(bt, ALLOC_page, BtLockWrite, &bt->alloc) )
1116                 return bt->err;
1117
1118         //      store chain in second right
1119         bt_putid(bt->temp->right, bt_getid(bt->alloc[1].right));
1120         bt_putid(bt->alloc[1].right, page_no);
1121
1122         // unlock page zero 
1123
1124         if( bt_unlockpage(bt, ALLOC_page, BtLockWrite) )
1125                 return bt->err;
1126
1127         //  remove write lock on deleted node
1128
1129         if( bt_unlockpage(bt, page_no, BtLockWrite) )
1130                 return bt->err;
1131
1132         //  remove delete lock on deleted node
1133
1134         if( bt_unlockpage(bt, page_no, BtLockDelete) )
1135                 return bt->err;
1136
1137         return 0;
1138 }
1139
1140 //      allocate a new page and write page into it
1141
1142 uid bt_newpage(BtDb *bt, BtPage page)
1143 {
1144 uid new_page;
1145 BtPage pmap;
1146 int reuse;
1147
1148         // lock page zero
1149
1150         if ( bt_lockpage(bt, ALLOC_page, BtLockWrite, &bt->alloc) )
1151                 return 0;
1152
1153         // use empty chain first
1154         // else allocate empty page
1155
1156         if( new_page = bt_getid(bt->alloc[1].right) ) {
1157                 if( bt_lockpage (bt, new_page, BtLockWrite, &bt->temp) )
1158                         return 0;
1159                 bt_putid(bt->alloc[1].right, bt_getid(bt->temp->right));
1160                 if( bt_unlockpage (bt, new_page, BtLockWrite) )
1161                         return 0;
1162                 reuse = 1;
1163         } else {
1164                 new_page = bt_getid(bt->alloc->right);
1165                 bt_putid(bt->alloc->right, new_page+1);
1166                 reuse = 0;
1167         }
1168 #ifdef unix
1169         if ( pwrite(bt->mgr->idx, page, bt->mgr->page_size, new_page << bt->mgr->page_bits) < bt->mgr->page_size )
1170                 return bt->err = BTERR_wrt, 0;
1171
1172         // if writing first page of pool block, zero last page in the block
1173
1174         if ( !reuse && bt->mgr->poolmask > 0 && (new_page & bt->mgr->poolmask) == 0 )
1175         {
1176                 // use zero buffer to write zeros
1177                 memset(bt->zero, 0, bt->mgr->page_size);
1178                 if ( pwrite(bt->mgr->idx,bt->zero, bt->mgr->page_size, (new_page | bt->mgr->poolmask) << bt->mgr->page_bits) < bt->mgr->page_size )
1179                         return bt->err = BTERR_wrt, 0;
1180         }
1181 #else
1182         //      bring new page into pool and copy page.
1183         //      this will extend the file into the new pages.
1184
1185         if( bt_lockpage(bt, new_page, BtLockWrite, &pmap) )
1186                 return 0;
1187
1188         memcpy(pmap, page, bt->mgr->page_size);
1189
1190         if( bt_unlockpage (bt, new_page, BtLockWrite) )
1191                 return 0;
1192 #endif
1193         // unlock page zero 
1194
1195         if ( bt_unlockpage(bt, ALLOC_page, BtLockWrite) )
1196                 return 0;
1197
1198         return new_page;
1199 }
1200
1201 //  find slot in page for given key at a given level
1202
1203 int bt_findslot (BtDb *bt, unsigned char *key, uint len)
1204 {
1205 uint diff, higher = bt->page->cnt, low = 1, slot;
1206
1207         //      low is the lowest candidate, higher is already
1208         //      tested as .ge. the given key, loop ends when they meet
1209
1210         while( diff = higher - low ) {
1211                 slot = low + ( diff >> 1 );
1212                 if( keycmp (keyptr(bt->page, slot), key, len) < 0 )
1213                         low = slot + 1;
1214                 else
1215                         higher = slot;
1216         }
1217
1218         return higher;
1219 }
1220
1221 //  find and load page at given level for given key
1222 //      leave page rd or wr locked as requested
1223
1224 int bt_loadpage (BtDb *bt, unsigned char *key, uint len, uint lvl, uint lock)
1225 {
1226 uid page_no = ROOT_page, prevpage = 0;
1227 uint drill = 0xff, slot;
1228 uint mode, prevmode;
1229
1230   //  start at root of btree and drill down
1231
1232   do {
1233         // determine lock mode of drill level
1234         mode = (lock == BtLockWrite) && (drill == lvl) ? BtLockWrite : BtLockRead; 
1235
1236         bt->page_no = page_no;
1237
1238         // obtain access lock using lock chaining with Access mode
1239
1240         if( page_no > ROOT_page )
1241           if( bt_lockpage(bt, page_no, BtLockAccess, NULL) )
1242                 return 0;                                                                       
1243
1244         if( prevpage )
1245           if( bt_unlockpage(bt, prevpage, prevmode) )
1246                 return 0;
1247
1248         // obtain read lock using lock chaining
1249         // and pin page contents
1250
1251         if( bt_lockpage(bt, page_no, mode, &bt->page) )
1252                 return 0;                                                                       
1253
1254         if( page_no > ROOT_page )
1255           if( bt_unlockpage(bt, page_no, BtLockAccess) )
1256                 return 0;                                                                       
1257
1258         // re-read and re-lock root after determining actual level of root
1259
1260         if( bt->page_no == ROOT_page )
1261           if( bt->page->lvl != drill) {
1262                 drill = bt->page->lvl;
1263
1264             if( lock == BtLockWrite && drill == lvl )
1265                   if( bt_unlockpage(bt, page_no, mode) )
1266                         return 0;
1267                   else
1268                         continue;
1269           }
1270
1271         //      if page is being deleted,
1272         //      move back to preceeding page
1273
1274         if( bt->page->kill ) {
1275                 page_no = bt_getid (bt->page->right);
1276                 continue;
1277         }
1278
1279         //  find key on page at this level
1280         //  and descend to requested level
1281
1282         slot = bt_findslot (bt, key, len);
1283
1284         //      is this slot a foster child?
1285
1286         if( slot <= bt->page->cnt - bt->page->foster )
1287           if( drill == lvl )
1288                 return slot;
1289           else
1290                 drill--;
1291
1292         while( slotptr(bt->page, slot)->dead )
1293           if( slot++ < bt->page->cnt )
1294                 continue;
1295           else
1296                 return bt->err = BTERR_struct, 0;
1297
1298         //  continue down / right using overlapping locks
1299         //  to protect pages being killed or split.
1300
1301         prevmode = mode;
1302         prevpage = bt->page_no;
1303         page_no = bt_getid(slotptr(bt->page, slot)->id);
1304   } while( page_no );
1305
1306   // return error on end of chain
1307
1308   bt->err = BTERR_struct;
1309   return 0;     // return error
1310 }
1311
1312 //  find and delete key on page by marking delete flag bit
1313 //  when page becomes empty, delete it from the btree
1314
1315 BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl)
1316 {
1317 unsigned char leftkey[256], rightkey[256];
1318 uid page_no, right;
1319 uint slot, tod;
1320 BtKey ptr;
1321
1322         if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1323                 ptr = keyptr(bt->page, slot);
1324         else
1325                 return bt->err;
1326
1327         // if key is found delete it, otherwise ignore request
1328
1329         if( !keycmp (ptr, key, len) )
1330                 if( slotptr(bt->page, slot)->dead == 0 ) {
1331                         slotptr(bt->page,slot)->dead = 1;
1332                         if( slot < bt->page->cnt )
1333                                 bt->page->dirty = 1;
1334                         bt->page->act--;
1335                 }
1336
1337         // return if page is not empty, or it has no right sibling
1338
1339         right = bt_getid(bt->page->right);
1340         page_no = bt->page_no;
1341
1342         if( !right || bt->page->act )
1343                 return bt_unlockpage(bt, page_no, BtLockWrite);
1344
1345         // obtain Parent lock over write lock
1346
1347         if( bt_lockpage(bt, page_no, BtLockParent, NULL) )
1348                 return bt->err;
1349
1350         // cache copy of key to delete
1351
1352         ptr = keyptr(bt->page, bt->page->cnt);
1353         memcpy(leftkey, ptr, ptr->len + 1);
1354
1355         // lock and map right page
1356
1357         if ( bt_lockpage(bt, right, BtLockWrite, &bt->temp) )
1358                 return bt->err;
1359
1360         // pull contents of next page into current empty page 
1361         memcpy (bt->page, bt->temp, bt->mgr->page_size);
1362
1363         //      cache copy of key to update
1364         ptr = keyptr(bt->temp, bt->temp->cnt);
1365         memcpy(rightkey, ptr, ptr->len + 1);
1366
1367         //  Mark right page as deleted and point it to left page
1368         //      until we can post updates at higher level.
1369
1370         bt_putid(bt->temp->right, page_no);
1371         bt->temp->kill = 1;
1372         bt->temp->cnt = 0;
1373
1374         if( bt_unlockpage(bt, right, BtLockWrite) )
1375                 return bt->err;
1376         if( bt_unlockpage(bt, page_no, BtLockWrite) )
1377                 return bt->err;
1378
1379         //  delete old lower key to consolidated node
1380
1381         if( bt_deletekey (bt, leftkey + 1, *leftkey, lvl + 1) )
1382                 return bt->err;
1383
1384         //  redirect higher key directly to consolidated node
1385
1386         if( slot = bt_loadpage (bt, rightkey+1, *rightkey, lvl+1, BtLockWrite) )
1387                 ptr = keyptr(bt->page, slot);
1388         else
1389                 return bt->err;
1390
1391         // since key already exists, update id
1392
1393         if( keycmp (ptr, rightkey+1, *rightkey) )
1394                 return bt->err = BTERR_struct;
1395
1396         slotptr(bt->page, slot)->dead = 0;
1397         bt_putid(slotptr(bt->page,slot)->id, page_no);
1398         bt_unlockpage(bt, bt->page_no, BtLockWrite);
1399
1400         //      obtain write lock and
1401         //      add right block to free chain
1402
1403         if( bt_freepage (bt, right) )
1404                 return bt->err;
1405
1406         //      remove ParentModify lock
1407
1408         if( bt_unlockpage(bt, page_no, BtLockParent) )
1409                 return bt->err;
1410         
1411         return 0;
1412 }
1413
1414 //      find key in leaf level and return row-id
1415
1416 uid bt_findkey (BtDb *bt, unsigned char *key, uint len)
1417 {
1418 uint  slot;
1419 BtKey ptr;
1420 uid id;
1421
1422         if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1423                 ptr = keyptr(bt->page, slot);
1424         else
1425                 return 0;
1426
1427         // if key exists, return row-id
1428         //      otherwise return 0
1429
1430         if( ptr->len == len && !memcmp (ptr->key, key, len) )
1431                 id = bt_getid(slotptr(bt->page,slot)->id);
1432         else
1433                 id = 0;
1434
1435         if ( bt_unlockpage(bt, bt->page_no, BtLockRead) )
1436                 return 0;
1437
1438         return id;
1439 }
1440
1441 //      check page for space available,
1442 //      clean if necessary and return
1443 //      0 - page needs splitting
1444 //      1 - go ahead
1445
1446 uint bt_cleanpage(BtDb *bt, uint amt)
1447 {
1448 uint nxt = bt->mgr->page_size;
1449 BtPage page = bt->page;
1450 uint cnt = 0, idx = 0;
1451 uint max = page->cnt;
1452 BtKey key;
1453
1454         if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1455                 return 1;
1456
1457         //      skip cleanup if nothing to reclaim
1458
1459         if( !page->dirty )
1460                 return 0;
1461
1462         memcpy (bt->frame, page, bt->mgr->page_size);
1463
1464         // skip page info and set rest of page to zero
1465
1466         memset (page+1, 0, bt->mgr->page_size - sizeof(*page));
1467         page->dirty = 0;
1468         page->act = 0;
1469
1470         // try cleaning up page first
1471
1472         while( cnt++ < max ) {
1473                 // always leave fence key and foster children in list
1474                 if( cnt < max - page->foster && slotptr(bt->frame,cnt)->dead )
1475                         continue;
1476
1477                 // copy key
1478                 key = keyptr(bt->frame, cnt);
1479                 nxt -= key->len + 1;
1480                 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1481
1482                 // copy slot
1483                 memcpy(slotptr(page, ++idx)->id, slotptr(bt->frame, cnt)->id, BtId);
1484                 if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
1485                         page->act++;
1486                 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1487                 slotptr(page, idx)->off = nxt;
1488         }
1489
1490         page->min = nxt;
1491         page->cnt = idx;
1492
1493         //      see if page has enough space now, or does it need splitting?
1494
1495         if( page->min >= (idx+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1496                 return 1;
1497
1498         return 0;
1499 }
1500
1501 //      add key to page
1502 //      return with page unlocked
1503
1504 BTERR bt_addkeytopage (BtDb *bt, uint slot, unsigned char *key, uint len, uid id, uint tod)
1505 {
1506 BtPage page = bt->page;
1507 uint idx;
1508
1509         // calculate next available slot and copy key into page
1510
1511         page->min -= len + 1;
1512         ((unsigned char *)page)[page->min] = len;
1513         memcpy ((unsigned char *)page + page->min +1, key, len );
1514
1515         for( idx = slot; idx < page->cnt; idx++ )
1516           if( slotptr(page, idx)->dead )
1517                 break;
1518
1519         // now insert key into array before slot
1520         // preserving the fence slot
1521
1522         if( idx == page->cnt )
1523                 idx++, page->cnt++;
1524
1525         page->act++;
1526
1527         while( idx > slot )
1528                 *slotptr(page, idx) = *slotptr(page, idx -1), idx--;
1529
1530         bt_putid(slotptr(page,slot)->id, id);
1531         slotptr(page, slot)->off = page->min;
1532         slotptr(page, slot)->tod = tod;
1533         slotptr(page, slot)->dead = 0;
1534
1535         return bt_unlockpage(bt, bt->page_no, BtLockWrite);
1536 }
1537
1538 // split the root and raise the height of the btree
1539
1540 BTERR bt_splitroot(BtDb *bt, uid right)
1541 {
1542 uint nxt = bt->mgr->page_size;
1543 unsigned char fencekey[256];
1544 BtPage root = bt->page;
1545 uid new_page;
1546 BtKey key;
1547
1548         //  Obtain an empty page to use, and copy the left page
1549         //  contents into it from the root.  Strip foster child key.
1550         //      (it's the stopper key)
1551
1552         root->act--;
1553         root->cnt--;
1554         root->foster--;
1555
1556         //      Save left fence key.
1557
1558         key = keyptr(root, root->cnt);
1559         memcpy (fencekey, key, key->len + 1);
1560
1561         //  copy the lower keys into a new left page
1562
1563         if( !(new_page = bt_newpage(bt, root)) )
1564                 return bt->err;
1565
1566         // preserve the page info at the bottom
1567         // and set rest of the root to zero
1568
1569         memset (root+1, 0, bt->mgr->page_size - sizeof(*root));
1570
1571         // insert left fence key on empty newroot page
1572
1573         nxt -= *fencekey + 1;
1574         memcpy ((unsigned char *)root + nxt, fencekey, *fencekey + 1);
1575         bt_putid(slotptr(root, 1)->id, new_page);
1576         slotptr(root, 1)->off = nxt;
1577         
1578         // insert stopper key on newroot page
1579         // and increase the root height
1580
1581         nxt -= 3;
1582         fencekey[0] = 2;
1583         fencekey[1] = 0xff;
1584         fencekey[2] = 0xff;
1585         memcpy ((unsigned char *)root + nxt, fencekey, *fencekey + 1);
1586         bt_putid(slotptr(root, 2)->id, right);
1587         slotptr(root, 2)->off = nxt;
1588
1589         bt_putid(root->right, 0);
1590         root->min = nxt;                // reset lowest used offset and key count
1591         root->cnt = 2;
1592         root->act = 2;
1593         root->lvl++;
1594
1595         // release root (bt->page)
1596
1597         return bt_unlockpage(bt, bt->page_no, BtLockWrite);
1598 }
1599
1600 //  split already locked full node
1601 //      return unlocked.
1602
1603 BTERR bt_splitpage (BtDb *bt)
1604 {
1605 uint slot, cnt, idx, max, nxt = bt->mgr->page_size;
1606 unsigned char fencekey[256];
1607 uid page_no = bt->page_no;
1608 BtPage page = bt->page;
1609 uint tod = time(NULL);
1610 uint lvl = page->lvl;
1611 uid new_page, right;
1612 BtKey key;
1613
1614         //      initialize frame buffer
1615
1616         memset (bt->frame, 0, bt->mgr->page_size);
1617         max = page->cnt - page->foster;
1618         tod = (uint)time(NULL);
1619         cnt = max / 2;
1620         idx = 0;
1621
1622         //  split higher half of keys to bt->frame
1623         //      leaving foster children in the left node.
1624
1625         while( cnt++ < max ) {
1626                 key = keyptr(page, cnt);
1627                 nxt -= key->len + 1;
1628                 memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
1629                 memcpy(slotptr(bt->frame,++idx)->id, slotptr(page,cnt)->id, BtId);
1630                 slotptr(bt->frame, idx)->tod = slotptr(page, cnt)->tod;
1631                 slotptr(bt->frame, idx)->off = nxt;
1632                 bt->frame->act++;
1633         }
1634
1635         // transfer right link node
1636
1637         if( page_no > ROOT_page ) {
1638                 right = bt_getid (page->right);
1639                 bt_putid(bt->frame->right, right);
1640         }
1641
1642         bt->frame->bits = bt->mgr->page_bits;
1643         bt->frame->min = nxt;
1644         bt->frame->cnt = idx;
1645         bt->frame->lvl = lvl;
1646
1647         //      get new free page and write frame to it.
1648
1649         if( !(new_page = bt_newpage(bt, bt->frame)) )
1650                 return bt->err;
1651
1652         //      remember fence key for new page to add
1653         //      as foster child
1654
1655         key = keyptr(bt->frame, idx);
1656         memcpy (fencekey, key, key->len + 1);
1657
1658         //      update lower keys and foster children to continue in old page
1659
1660         memcpy (bt->frame, page, bt->mgr->page_size);
1661         memset (page+1, 0, bt->mgr->page_size - sizeof(*page));
1662         nxt = bt->mgr->page_size;
1663         page->act = 0;
1664         cnt = 0;
1665         idx = 0;
1666
1667         //  assemble page of smaller keys
1668         //      to remain in the old page
1669
1670         while( cnt++ < max / 2 ) {
1671                 key = keyptr(bt->frame, cnt);
1672                 nxt -= key->len + 1;
1673                 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1674                 memcpy (slotptr(page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
1675                 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1676                 slotptr(page, idx)->off = nxt;
1677                 page->act++;
1678         }
1679
1680         //      insert new foster child at beginning of the current foster children
1681
1682         nxt -= *fencekey + 1;
1683         memcpy ((unsigned char *)page + nxt, fencekey, *fencekey + 1);
1684         bt_putid (slotptr(page,++idx)->id, new_page);
1685         slotptr(page, idx)->tod = tod;
1686         slotptr(page, idx)->off = nxt;
1687         page->foster++;
1688         page->act++;
1689
1690         //  continue with old foster child keys if any
1691
1692         cnt = bt->frame->cnt - bt->frame->foster;
1693
1694         while( cnt++ < bt->frame->cnt ) {
1695                 key = keyptr(bt->frame, cnt);
1696                 nxt -= key->len + 1;
1697                 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1698                 memcpy (slotptr(page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
1699                 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1700                 slotptr(page, idx)->off = nxt;
1701                 page->act++;
1702         }
1703
1704         page->min = nxt;
1705         page->cnt = idx;
1706
1707         //      link new right page
1708
1709         bt_putid (page->right, new_page);
1710
1711         // if current page is the root page, split it
1712
1713         if( page_no == ROOT_page )
1714                 return bt_splitroot (bt, new_page);
1715
1716         //  release wr lock on page
1717
1718         if( bt_unlockpage (bt, page_no, BtLockWrite) )
1719                 return bt->err;
1720
1721         //  obtain ParentModification lock for current page
1722         //      to fix fence key and highest foster child on page
1723
1724         if( bt_lockpage (bt, page_no, BtLockParent, NULL) )
1725                 return bt->err;
1726
1727         //  get our highest foster child key to find in parent node
1728
1729         if( bt_lockpage (bt, page_no, BtLockRead, &page) )
1730                 return bt->err;
1731
1732         key = keyptr(page, page->cnt);
1733         memcpy (fencekey, key, key->len+1);
1734
1735         if( bt_unlockpage (bt, page_no, BtLockRead) )
1736                 return bt->err;
1737
1738 try_again:
1739
1740         do {
1741           slot = bt_loadpage (bt, fencekey + 1, *fencekey, lvl + 1, BtLockWrite);
1742
1743           if( !slot )
1744                 return bt->err;
1745
1746           // check if parent page has enough space for any possible key
1747
1748           if( bt_cleanpage (bt, 256) )
1749                 break;
1750
1751           if( bt_splitpage (bt) )
1752                 return bt->err;
1753         } while( 1 );
1754
1755         //  see if we are still a foster child from another node
1756
1757         if( bt_getid (slotptr(bt->page, slot)->id) != page_no ) {
1758                 bt_unlockpage (bt, bt->page_no, BtLockWrite);
1759 #ifdef  unix
1760                 sched_yield();
1761 #else
1762                 SwitchToThread();
1763 #endif
1764                 goto try_again;
1765         }
1766
1767         //      wait until readers from parent get their locks
1768
1769         if( bt_lockpage (bt, page_no, BtLockDelete, NULL) )
1770                 return bt->err;
1771
1772         if( bt_lockpage (bt, page_no, BtLockWrite, &page) )
1773                 return bt->err;
1774
1775         //      switch parent fence key to foster child
1776
1777         if( slotptr(page, page->cnt)->dead )
1778                 slotptr(bt->page, slot)->dead = 1;
1779         else
1780                 bt_putid (slotptr(bt->page, slot)->id, bt_getid(slotptr(page, page->cnt)->id));
1781
1782         //      remove highest foster child from our page
1783         //      add our new fence key to parent
1784
1785         page->cnt--;
1786         page->act--;
1787         page->foster--;
1788         page->dirty = 1;
1789         key = keyptr(page, page->cnt);
1790
1791         if( bt_addkeytopage (bt, slot, key->key, key->len, page_no, tod) )
1792                 return bt->err;
1793
1794         if( bt_unlockpage (bt, page_no, BtLockDelete) )
1795                 return bt->err;
1796
1797         if( bt_unlockpage (bt, page_no, BtLockWrite) )
1798                 return bt->err;
1799
1800         return bt_unlockpage (bt, page_no, BtLockParent);
1801 }
1802
1803 //  Insert new key into the btree at leaf level.
1804
1805 BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uid id, uint tod)
1806 {
1807 uint slot, idx;
1808 BtPage page;
1809 BtKey ptr;
1810
1811         while( 1 ) {
1812                 if( slot = bt_loadpage (bt, key, len, 0, BtLockWrite) )
1813                         ptr = keyptr(bt->page, slot);
1814                 else
1815                 {
1816                         if ( !bt->err )
1817                                 bt->err = BTERR_ovflw;
1818                         return bt->err;
1819                 }
1820
1821                 // if key already exists, update id and return
1822
1823                 page = bt->page;
1824
1825                 if( !keycmp (ptr, key, len) ) {
1826                         slotptr(page, slot)->dead = 0;
1827                         slotptr(page, slot)->tod = tod;
1828                         bt_putid(slotptr(page,slot)->id, id);
1829                         return bt_unlockpage(bt, bt->page_no, BtLockWrite);
1830                 }
1831
1832                 // check if page has enough space
1833
1834                 if( bt_cleanpage (bt, len) )
1835                         break;
1836
1837                 if( bt_splitpage (bt) )
1838                         return bt->err;
1839         }
1840
1841         return bt_addkeytopage (bt, slot, key, len, id, tod);
1842 }
1843
1844 //  cache page of keys into cursor and return starting slot for given key
1845
1846 uint bt_startkey (BtDb *bt, unsigned char *key, uint len)
1847 {
1848 uint slot;
1849
1850         // cache page for retrieval
1851         if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1852                 memcpy (bt->cursor, bt->page, bt->mgr->page_size);
1853         bt->cursor_page = bt->page_no;
1854         if ( bt_unlockpage(bt, bt->page_no, BtLockRead) )
1855                 return 0;
1856
1857         return slot;
1858 }
1859
1860 //  return next slot for cursor page
1861 //  or slide cursor right into next page
1862
1863 uint bt_nextkey (BtDb *bt, uint slot)
1864 {
1865 BtPage page;
1866 uid right;
1867
1868   do {
1869         right = bt_getid(bt->cursor->right);
1870         while( slot++ < bt->cursor->cnt - bt->cursor->foster )
1871           if( slotptr(bt->cursor,slot)->dead )
1872                 continue;
1873           else if( right || (slot < bt->cursor->cnt - bt->cursor->foster) )
1874                 return slot;
1875           else
1876                 break;
1877
1878         if( !right )
1879                 break;
1880
1881         bt->cursor_page = right;
1882
1883     if( bt_lockpage(bt, right, BtLockRead, &page) )
1884                 return 0;
1885
1886         memcpy (bt->cursor, page, bt->mgr->page_size);
1887
1888         if ( bt_unlockpage(bt, right, BtLockRead) )
1889                 return 0;
1890
1891         slot = 0;
1892   } while( 1 );
1893
1894   return bt->err = 0;
1895 }
1896
1897 BtKey bt_key(BtDb *bt, uint slot)
1898 {
1899         return keyptr(bt->cursor, slot);
1900 }
1901
1902 uid bt_uid(BtDb *bt, uint slot)
1903 {
1904         return bt_getid(slotptr(bt->cursor,slot)->id);
1905 }
1906
1907 uint bt_tod(BtDb *bt, uint slot)
1908 {
1909         return slotptr(bt->cursor,slot)->tod;
1910 }
1911
1912
1913 #ifdef STANDALONE
1914
1915 typedef struct {
1916         char type, idx;
1917         char *infile;
1918         BtMgr *mgr;
1919         int num;
1920 } ThreadArg;
1921
1922 //  standalone program to index file of keys
1923 //  then list them onto std-out
1924
1925 #ifdef unix
1926 void *index_file (void *arg)
1927 #else
1928 uint __stdcall index_file (void *arg)
1929 #endif
1930 {
1931 int line = 0, found = 0, cnt = 0;
1932 uid next, page_no = LEAF_page;  // start on first page of leaves
1933 unsigned char key[256];
1934 ThreadArg *args = arg;
1935 int ch, len = 0, slot;
1936 time_t tod[1];
1937 BtPage page;
1938 BtKey ptr;
1939 BtDb *bt;
1940 FILE *in;
1941
1942         bt = bt_open (args->mgr);
1943         time (tod);
1944
1945         switch(args->type | 0x20)
1946         {
1947         case 'w':
1948                 fprintf(stderr, "started indexing for %s\n", args->infile);
1949                 if( in = fopen (args->infile, "rb") )
1950                   while( ch = getc(in), ch != EOF )
1951                         if( ch == '\n' )
1952                         {
1953                           line++;
1954
1955                           if( args->num == 1 )
1956                                 sprintf((char *)key+len, "%.9d", 1000000000 - line), len += 9;
1957
1958                           else if( args->num )
1959                                 sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
1960
1961                           if( bt_insertkey (bt, key, len, line, *tod) )
1962                                 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
1963                           len = 0;
1964                         }
1965                         else if( len < 255 )
1966                                 key[len++] = ch;
1967                 fprintf(stderr, "finished %s for %d keys\n", args->infile, line);
1968                 break;
1969
1970         case 'd':
1971                 fprintf(stderr, "started deleting keys for %s\n", args->infile);
1972                 if( in = fopen (args->infile, "rb") )
1973                   while( ch = getc(in), ch != EOF )
1974                         if( ch == '\n' )
1975                         {
1976                           line++;
1977                           if( args->num == 1 )
1978                                 sprintf((char *)key+len, "%.9d", 1000000000 - line), len += 9;
1979
1980                           else if( args->num )
1981                                 sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
1982
1983                           if( bt_deletekey (bt, key, len, 0) )
1984                                 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
1985                           len = 0;
1986                         }
1987                         else if( len < 255 )
1988                                 key[len++] = ch;
1989                 fprintf(stderr, "finished %s for keys, %d \n", args->infile, line);
1990                 break;
1991
1992         case 'f':
1993                 fprintf(stderr, "started finding keys for %s\n", args->infile);
1994                 if( in = fopen (args->infile, "rb") )
1995                   while( ch = getc(in), ch != EOF )
1996                         if( ch == '\n' )
1997                         {
1998                           line++;
1999                           if( args->num == 1 )
2000                                 sprintf((char *)key+len, "%.9d", 1000000000 - line), len += 9;
2001
2002                           else if( args->num )
2003                                 sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
2004
2005                           if( bt_findkey (bt, key, len) )
2006                                 found++;
2007                           else if( bt->err )
2008                                 fprintf(stderr, "Error %d Syserr %d Line: %d\n", bt->err, errno, line), exit(0);
2009                           len = 0;
2010                         }
2011                         else if( len < 255 )
2012                                 key[len++] = ch;
2013                 fprintf(stderr, "finished %s for %d keys, found %d\n", args->infile, line, found);
2014                 break;
2015
2016         case 's':
2017                 len = key[0] = 0;
2018
2019                 fprintf(stderr, "started reading\n");
2020
2021                 if( slot = bt_startkey (bt, key, len) )
2022                   slot--;
2023                 else
2024                   fprintf(stderr, "Error %d in StartKey. Syserror: %d\n", bt->err, errno), exit(0);
2025
2026                 while( slot = bt_nextkey (bt, slot) ) {
2027                         ptr = bt_key(bt, slot);
2028                         fwrite (ptr->key, ptr->len, 1, stdout);
2029                         fputc ('\n', stdout);
2030                 }
2031
2032                 break;
2033
2034         case 'c':
2035                 fprintf(stderr, "started reading\n");
2036
2037                 do {
2038                         bt_lockpage (bt, page_no, BtLockRead, &page);
2039                         cnt += page->act;
2040                         next = bt_getid (page->right);
2041                         bt_unlockpage (bt, page_no, BtLockRead);
2042                 } while( page_no = next );
2043
2044                 cnt--;  // remove stopper key
2045                 fprintf(stderr, " Total keys read %d\n", cnt);
2046                 break;
2047         }
2048
2049         bt_close (bt);
2050 #ifdef unix
2051         return NULL;
2052 #else
2053         return 0;
2054 #endif
2055 }
2056
2057 typedef struct timeval timer;
2058
2059 int main (int argc, char **argv)
2060 {
2061 int idx, cnt, len, slot, err;
2062 int segsize, bits = 16;
2063 #ifdef unix
2064 pthread_t *threads;
2065 timer start, stop;
2066 #else
2067 time_t start[1], stop[1];
2068 HANDLE *threads;
2069 #endif
2070 double real_time;
2071 ThreadArg *args;
2072 uint poolsize = 0;
2073 int num = 0;
2074 char key[1];
2075 BtMgr *mgr;
2076 BtKey ptr;
2077 BtDb *bt;
2078
2079         if( argc < 3 ) {
2080                 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]);
2081                 fprintf (stderr, "  where page_bits is the page size in bits\n");
2082                 fprintf (stderr, "  mapped_segments is the number of mmap segments in buffer pool\n");
2083                 fprintf (stderr, "  seg_bits is the size of individual segments in buffer pool in pages in bits\n");
2084                 fprintf (stderr, "  line_numbers = 1 to append line numbers to keys\n");
2085                 fprintf (stderr, "  src_file1 thru src_filen are files of keys separated by newline\n");
2086                 exit(0);
2087         }
2088
2089 #ifdef unix
2090         gettimeofday(&start, NULL);
2091 #else
2092         time(start);
2093 #endif
2094
2095         if( argc > 3 )
2096                 bits = atoi(argv[3]);
2097
2098         if( argc > 4 )
2099                 poolsize = atoi(argv[4]);
2100
2101         if( !poolsize )
2102                 fprintf (stderr, "Warning: no mapped_pool\n");
2103
2104         if( poolsize > 65535 )
2105                 fprintf (stderr, "Warning: mapped_pool > 65535 segments\n");
2106
2107         if( argc > 5 )
2108                 segsize = atoi(argv[5]);
2109         else
2110                 segsize = 4;    // 16 pages per mmap segment
2111
2112         if( argc > 6 )
2113                 num = atoi(argv[6]);
2114
2115         cnt = argc - 7;
2116 #ifdef unix
2117         threads = malloc (cnt * sizeof(pthread_t));
2118 #else
2119         threads = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, cnt * sizeof(HANDLE));
2120 #endif
2121         args = malloc (cnt * sizeof(ThreadArg));
2122
2123         mgr = bt_mgr ((argv[1]), BT_rw, bits, poolsize, segsize, poolsize / 8);
2124
2125         if( !mgr ) {
2126                 fprintf(stderr, "Index Open Error %s\n", argv[1]);
2127                 exit (1);
2128         }
2129
2130         //      fire off threads
2131
2132         for( idx = 0; idx < cnt; idx++ ) {
2133                 args[idx].infile = argv[idx + 7];
2134                 args[idx].type = argv[2][0];
2135                 args[idx].mgr = mgr;
2136                 args[idx].num = num;
2137                 args[idx].idx = idx;
2138 #ifdef unix
2139                 if( err = pthread_create (threads + idx, NULL, index_file, args + idx) )
2140                         fprintf(stderr, "Error creating thread %d\n", err);
2141 #else
2142                 threads[idx] = (HANDLE)_beginthreadex(NULL, 65536, index_file, args + idx, 0, NULL);
2143 #endif
2144         }
2145
2146         //      wait for termination
2147
2148 #ifdef unix
2149         for( idx = 0; idx < cnt; idx++ )
2150                 pthread_join (threads[idx], NULL);
2151         gettimeofday(&stop, NULL);
2152         real_time = 1000.0 * ( stop.tv_sec - start.tv_sec ) + 0.001 * (stop.tv_usec - start.tv_usec );
2153 #else
2154         WaitForMultipleObjects (cnt, threads, TRUE, INFINITE);
2155
2156         for( idx = 0; idx < cnt; idx++ )
2157                 CloseHandle(threads[idx]);
2158
2159         time (stop);
2160         real_time = 1000 * (*stop - *start);
2161 #endif
2162         fprintf(stderr, " Time to complete: %.2f seconds\n", real_time/1000);
2163         bt_mgrclose (mgr);
2164 }
2165
2166 #endif  //STANDALONE