2 // with reworked bt_deletekey code
5 // author: karl malbrain, malbrain@cal.berkeley.edu
8 This work, including the source code, documentation
9 and related data, is placed into the public domain.
11 The orginal author is Karl Malbrain.
13 THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY
14 OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF
15 MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE,
16 ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE
17 RESULTING FROM THE USE, MODIFICATION, OR
18 REDISTRIBUTION OF THIS SOFTWARE.
21 // Please see the project home page for documentation
22 // code.google.com/p/high-concurrency-btree
24 #define _FILE_OFFSET_BITS 64
25 #define _LARGEFILE64_SOURCE
40 #define WIN32_LEAN_AND_MEAN
51 typedef unsigned long long uid;
54 typedef unsigned long long off64_t;
55 typedef unsigned short ushort;
56 typedef unsigned int uint;
59 #define BT_ro 0x6f72 // ro
60 #define BT_rw 0x7772 // rw
61 #define BT_fl 0x6c66 // fl
63 #define BT_maxbits 24 // maximum page size in bits
64 #define BT_minbits 9 // minimum page size in bits
65 #define BT_minpage (1 << BT_minbits) // minimum page size
68 There are five lock types for each node in three independent sets:
69 1. (set 1) AccessIntent: Sharable. Going to Read the node. Incompatible with NodeDelete.
70 2. (set 1) NodeDelete: Exclusive. About to release the node. Incompatible with AccessIntent.
71 3. (set 2) ReadLock: Sharable. Read the node. Incompatible with WriteLock.
72 4. (set 2) WriteLock: Exclusive. Modify the node. Incompatible with ReadLock and other WriteLocks.
73 5. (set 3) ParentModification: Exclusive. Change the node's parent keys. Incompatible with another ParentModification.
84 // Define the length of the page and key pointers
88 // Page key slot definition.
90 // If BT_maxbits is 15 or less, you can save 2 bytes
91 // for each key stored by making the first two uints
92 // into ushorts. You can also save 4 bytes by removing
93 // the tod field from the key.
95 // Keys are marked dead, but remain on the page until
96 // cleanup is called. The fence key (highest key) for
97 // the page is always present, even if dead.
100 uint off:BT_maxbits; // page offset for key start
101 uint dead:1; // set for deleted key
102 uint tod; // time-stamp for key
103 unsigned char id[BtId]; // id associated with key
106 // The key structure occupies space at the upper end of
107 // each page. It's a length byte followed by the value
112 unsigned char key[0];
115 // The first part of an index page.
116 // It is immediately followed
117 // by the BtSlot array of keys.
120 uint cnt; // count of keys in page
121 uint act; // count of active keys
122 uint min; // next key offset
123 unsigned char bits:7; // page size in bits
124 unsigned char free:1; // page is on free list
125 unsigned char lvl:6; // level of page
126 unsigned char kill:1; // page is being deleted
127 unsigned char dirty:1; // page is dirty
128 unsigned char right[BtId]; // page number to right
131 // The memory mapping hash table entry
134 BtPage page; // mapped page pointer
135 uid page_no; // mapped page number
136 void *lruprev; // least recently used previous cache block
137 void *lrunext; // lru next cache block
138 void *hashprev; // previous cache block for the same hash idx
139 void *hashnext; // next cache block for the same hash idx
145 // The object structure for Btree access
147 typedef struct _BtDb {
148 uint page_size; // each page size
149 uint page_bits; // each page size in bits
150 uint seg_bits; // segment size in pages in bits
151 uid page_no; // current page number
152 uid cursor_page; // current cursor page number
154 uint mode; // read-write mode
155 uint mapped_io; // use memory mapping
156 BtPage temp; // temporary frame buffer (memory mapped/file IO)
157 BtPage alloc; // frame buffer for alloc page ( page 0 )
158 BtPage cursor; // cached frame for start/next (never mapped)
159 BtPage frame; // spare frame for the page split (never mapped)
160 BtPage zero; // zeroes frame buffer (never mapped)
161 BtPage page; // current page
167 unsigned char *mem; // frame, cursor, page memory buffer
168 int nodecnt; // highest page cache segment in use
169 int nodemax; // highest page cache segment allocated
170 int hashmask; // number of pages in segments - 1
171 int hashsize; // size of hash table
172 int found; // last deletekey found key
173 BtHash *lrufirst; // lru list head
174 BtHash *lrulast; // lru list tail
175 ushort *cache; // hash table for cached segments
176 BtHash nodes[1]; // segment cache follows
190 extern void bt_close (BtDb *bt);
191 extern BtDb *bt_open (char *name, uint mode, uint bits, uint cacheblk, uint pgblk);
192 extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod);
193 extern BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl);
194 extern uid bt_findkey (BtDb *bt, unsigned char *key, uint len);
195 extern uint bt_startkey (BtDb *bt, unsigned char *key, uint len);
196 extern uint bt_nextkey (BtDb *bt, uint slot);
198 // Helper functions to return slot values
200 extern BtKey bt_key (BtDb *bt, uint slot);
201 extern uid bt_uid (BtDb *bt, uint slot);
202 extern uint bt_tod (BtDb *bt, uint slot);
204 // BTree page number constants
209 // Number of levels to create in a new BTree
213 // The page is allocated from low and hi ends.
214 // The key offsets and row-id's are allocated
215 // from the bottom, while the text of the key
216 // is allocated from the top. When the two
217 // areas meet, the page is split into two.
219 // A key consists of a length byte, two bytes of
220 // index number (0 - 65534), and up to 253 bytes
221 // of key value. Duplicate keys are discarded.
222 // Associated with each key is a 48 bit row-id.
224 // The b-tree root is always located at page 1.
225 // The first leaf page of level zero is always
226 // located on page 2.
228 // The b-tree pages are linked with right
229 // pointers to facilitate enumerators,
230 // and provide for concurrency.
232 // When to root page fills, it is split in two and
233 // the tree height is raised by a new root at page
234 // one with two keys.
236 // Deleted keys are marked with a dead bit until
237 // page cleanup The fence key for a node is always
238 // present, even after deletion and cleanup.
240 // Deleted leaf pages are reclaimed on a free list.
241 // The upper levels of the btree are fixed on creation.
243 // Groups of pages from the btree are optionally
244 // cached with memory mapping. A hash table is used to keep
245 // track of the cached pages. This behaviour is controlled
246 // by the number of cache blocks parameter and pages per block
249 // To achieve maximum concurrency one page is locked at a time
250 // as the tree is traversed to find leaf key in question. The right
251 // page numbers are used in cases where the page is being split,
254 // Page 0 (ALLOC page) is dedicated to lock for new page extensions,
255 // and chains empty leaf pages together for reuse.
257 // Parent locks are obtained to prevent resplitting or deleting a node
258 // before its fence is posted into its upper level.
260 // A special open mode of BT_fl is provided to safely access files on
261 // WIN32 networks. WIN32 network operations should not use memory mapping.
262 // This WIN32 mode sets FILE_FLAG_NOBUFFERING and FILE_FLAG_WRITETHROUGH
263 // to prevent local caching of network file contents.
265 // Access macros to address slot and key values from the page.
266 // Page slots use 1 based indexing.
268 #define slotptr(page, slot) (((BtSlot *)(page+1)) + (slot-1))
269 #define keyptr(page, slot) ((BtKey)((unsigned char*)(page) + slotptr(page, slot)->off))
271 void bt_putid(unsigned char *dest, uid id)
276 dest[i] = (unsigned char)id, id >>= 8;
279 uid bt_getid(unsigned char *src)
284 for( i = 0; i < BtId; i++ )
285 id <<= 8, id |= *src++;
290 // place write, read, or parent lock on requested page_no.
292 BTERR bt_lockpage(BtDb *bt, uid page_no, BtLock mode)
294 off64_t off = page_no << bt->page_bits;
296 int flag = PROT_READ | ( bt->mode == BT_ro ? 0 : PROT_WRITE );
297 struct flock lock[1];
303 if( mode == BtLockRead || mode == BtLockWrite )
304 off += sizeof(*bt->page); // use second segment
306 if( mode == BtLockParent )
307 off += 2 * sizeof(*bt->page); // use third segment
310 memset (lock, 0, sizeof(lock));
313 lock->l_type = (mode == BtLockDelete || mode == BtLockWrite || mode == BtLockParent) ? F_WRLCK : F_RDLCK;
314 lock->l_len = sizeof(*bt->page);
317 if( fcntl (bt->idx, F_SETLKW, lock) < 0 )
318 return bt->err = BTERR_lock;
322 memset (ovl, 0, sizeof(ovl));
323 ovl->OffsetHigh = (uint)(off >> 32);
324 ovl->Offset = (uint)off;
325 len = sizeof(*bt->page);
327 // use large offsets to
328 // simulate advisory locking
330 ovl->OffsetHigh |= 0x80000000;
332 if( mode == BtLockDelete || mode == BtLockWrite || mode == BtLockParent )
333 flags |= LOCKFILE_EXCLUSIVE_LOCK;
335 if( LockFileEx (bt->idx, flags, 0, len, 0L, ovl) )
338 return bt->err = BTERR_lock;
342 // remove write, read, or parent lock on requested page_no.
344 BTERR bt_unlockpage(BtDb *bt, uid page_no, BtLock mode)
346 off64_t off = page_no << bt->page_bits;
348 struct flock lock[1];
354 if( mode == BtLockRead || mode == BtLockWrite )
355 off += sizeof(*bt->page); // use second segment
357 if( mode == BtLockParent )
358 off += 2 * sizeof(*bt->page); // use third segment
361 memset (lock, 0, sizeof(lock));
364 lock->l_type = F_UNLCK;
365 lock->l_len = sizeof(*bt->page);
368 if( fcntl (bt->idx, F_SETLK, lock) < 0 )
369 return bt->err = BTERR_lock;
371 memset (ovl, 0, sizeof(ovl));
372 ovl->OffsetHigh = (uint)(off >> 32);
373 ovl->Offset = (uint)off;
374 len = sizeof(*bt->page);
376 // use large offsets to
377 // simulate advisory locking
379 ovl->OffsetHigh |= 0x80000000;
381 if( !UnlockFileEx (bt->idx, 0, len, 0, ovl) )
382 return GetLastError(), bt->err = BTERR_lock;
388 // close and release memory
390 void bt_close (BtDb *bt)
394 // release mapped pages
396 if( hash = bt->lrufirst )
397 do munmap (hash->page, (bt->hashmask+1) << bt->page_bits);
398 while(hash = hash->lrunext);
406 if( hash = bt->lrufirst )
409 FlushViewOfFile(hash->page, 0);
410 UnmapViewOfFile(hash->page);
411 CloseHandle(hash->hmap);
412 } while(hash = hash->lrunext);
415 VirtualFree (bt->mem, 0, MEM_RELEASE);
416 FlushFileBuffers(bt->idx);
417 CloseHandle(bt->idx);
418 GlobalFree (bt->cache);
423 // open/create new btree
424 // call with file_name, BT_openmode, bits in page size (e.g. 16),
425 // size of mapped page cache (e.g. 8192) or zero for no mapping.
427 BtDb *bt_open (char *name, uint mode, uint bits, uint nodemax, uint pgblk)
429 uint lvl, attr, cacheblk, last;
430 BtLock lockmode = BtLockWrite;
438 SYSTEM_INFO sysinfo[1];
442 bt = malloc (sizeof(BtDb) + nodemax * sizeof(BtHash));
443 memset (bt, 0, sizeof(BtDb));
445 switch (mode & 0x7fff)
449 bt->idx = open ((char*)name, O_RDWR | O_CREAT, 0666);
454 bt->idx = open ((char*)name, O_RDONLY);
455 lockmode = BtLockRead;
459 return free(bt), NULL;
462 cacheblk = 4096; // page size for unix
467 bt = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, sizeof(BtDb) + nodemax * sizeof(BtHash));
468 attr = FILE_ATTRIBUTE_NORMAL;
469 switch (mode & 0x7fff)
472 attr |= FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING;
475 bt->idx = CreateFile(name, GENERIC_READ| GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, attr, NULL);
480 bt->idx = CreateFile(name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, attr, NULL);
481 lockmode = BtLockRead;
484 if( bt->idx == INVALID_HANDLE_VALUE )
485 return GlobalFree(bt), NULL;
487 // normalize cacheblk to multiple of sysinfo->dwAllocationGranularity
488 GetSystemInfo(sysinfo);
491 cacheblk = sysinfo->dwAllocationGranularity;
496 // determine sanity of page size
498 if( bits > BT_maxbits )
500 else if( bits < BT_minbits )
503 if( bt_lockpage(bt, ALLOC_page, lockmode) )
504 return bt_close (bt), NULL;
509 // read minimum page size to get root info
511 if( size = lseek (bt->idx, 0L, 2) ) {
512 alloc = malloc (BT_minpage);
513 pread(bt->idx, alloc, BT_minpage, 0);
516 } else if( mode == BT_ro )
517 return bt_close (bt), NULL;
519 size = GetFileSize(bt->idx, amt);
522 alloc = VirtualAlloc(NULL, BT_minpage, MEM_COMMIT, PAGE_READWRITE);
523 if( !ReadFile(bt->idx, (char *)alloc, BT_minpage, amt, NULL) )
524 return bt_close (bt), NULL;
526 VirtualFree (alloc, 0, MEM_RELEASE);
527 } else if( mode == BT_ro )
528 return bt_close (bt), NULL;
531 bt->page_size = 1 << bits;
532 bt->page_bits = bits;
534 bt->nodemax = nodemax;
537 // setup cache mapping
540 if( cacheblk < bt->page_size )
541 cacheblk = bt->page_size;
543 bt->hashsize = nodemax / 8;
544 bt->hashmask = (cacheblk >> bits) - 1;
548 // requested number of pages per memmap segment
551 if( (1 << pgblk) > bt->hashmask )
552 bt->hashmask = (1 << pgblk) - 1;
556 while( (1 << bt->seg_bits) <= bt->hashmask )
560 bt->mem = malloc (6 *bt->page_size);
561 bt->cache = calloc (bt->hashsize, sizeof(ushort));
563 bt->mem = VirtualAlloc(NULL, 6 * bt->page_size, MEM_COMMIT, PAGE_READWRITE);
564 bt->cache = GlobalAlloc (GMEM_FIXED|GMEM_ZEROINIT, bt->hashsize * sizeof(ushort));
566 bt->frame = (BtPage)bt->mem;
567 bt->cursor = (BtPage)(bt->mem + bt->page_size);
568 bt->page = (BtPage)(bt->mem + 2 * bt->page_size);
569 bt->alloc = (BtPage)(bt->mem + 3 * bt->page_size);
570 bt->temp = (BtPage)(bt->mem + 4 * bt->page_size);
571 bt->zero = (BtPage)(bt->mem + 5 * bt->page_size);
574 if( bt_unlockpage(bt, ALLOC_page, lockmode) )
575 return bt_close (bt), NULL;
580 // initializes an empty b-tree with root page and page of leaves
582 memset (bt->alloc, 0, bt->page_size);
583 bt_putid(bt->alloc->right, MIN_lvl+1);
584 bt->alloc->bits = bt->page_bits;
587 if( write (bt->idx, bt->alloc, bt->page_size) < bt->page_size )
588 return bt_close (bt), NULL;
590 if( !WriteFile (bt->idx, (char *)bt->alloc, bt->page_size, amt, NULL) )
591 return bt_close (bt), NULL;
593 if( *amt < bt->page_size )
594 return bt_close (bt), NULL;
597 memset (bt->frame, 0, bt->page_size);
598 bt->frame->bits = bt->page_bits;
600 for( lvl=MIN_lvl; lvl--; ) {
601 slotptr(bt->frame, 1)->off = bt->page_size - 3;
602 bt_putid(slotptr(bt->frame, 1)->id, lvl ? MIN_lvl - lvl + 1 : 0); // next(lower) page number
603 key = keyptr(bt->frame, 1);
604 key->len = 2; // create stopper key
607 bt->frame->min = bt->page_size - 3;
608 bt->frame->lvl = lvl;
612 if( write (bt->idx, bt->frame, bt->page_size) < bt->page_size )
613 return bt_close (bt), NULL;
615 if( !WriteFile (bt->idx, (char *)bt->frame, bt->page_size, amt, NULL) )
616 return bt_close (bt), NULL;
618 if( *amt < bt->page_size )
619 return bt_close (bt), NULL;
623 // create empty page area by writing last page of first
624 // cache area (other pages are zeroed by O/S)
626 if( bt->mapped_io && bt->hashmask ) {
627 memset(bt->frame, 0, bt->page_size);
630 while( last < MIN_lvl + 1 )
631 last += bt->hashmask + 1;
633 pwrite(bt->idx, bt->frame, bt->page_size, last << bt->page_bits);
635 SetFilePointer (bt->idx, last << bt->page_bits, NULL, FILE_BEGIN);
636 if( !WriteFile (bt->idx, (char *)bt->frame, bt->page_size, amt, NULL) )
637 return bt_close (bt), NULL;
638 if( *amt < bt->page_size )
639 return bt_close (bt), NULL;
643 if( bt_unlockpage(bt, ALLOC_page, lockmode) )
644 return bt_close (bt), NULL;
649 // compare two keys, returning > 0, = 0, or < 0
650 // as the comparison value
652 int keycmp (BtKey key1, unsigned char *key2, uint len2)
654 uint len1 = key1->len;
657 if( ans = memcmp (key1->key, key2, len1 > len2 ? len2 : len1) )
668 // Update current page of btree by writing file contents
669 // or flushing mapped area to disk.
671 BTERR bt_update (BtDb *bt, BtPage page, uid page_no)
673 off64_t off = page_no << bt->page_bits;
677 if( pwrite(bt->idx, page, bt->page_size, off) != bt->page_size )
678 return bt->err = BTERR_wrt;
683 SetFilePointer (bt->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
684 if( !WriteFile (bt->idx, (char *)page, bt->page_size, amt, NULL) )
685 return GetLastError(), bt->err = BTERR_wrt;
687 if( *amt < bt->page_size )
688 return GetLastError(), bt->err = BTERR_wrt;
690 else if( bt->mode == BT_fl ) {
691 FlushViewOfFile(page, bt->page_size);
692 FlushFileBuffers(bt->idx);
698 // find page in cache
700 BtHash *bt_findhash(BtDb *bt, uid page_no)
705 // compute cache block first page and hash idx
707 page_no &= ~bt->hashmask;
708 idx = (uint)(page_no >> bt->seg_bits) % bt->hashsize;
711 hash = bt->nodes + bt->cache[idx];
715 do if( hash->page_no == page_no )
717 while(hash = hash->hashnext );
722 // add page cache entry to hash index
724 void bt_linkhash(BtDb *bt, BtHash *node, uid page_no)
726 uint idx = (uint)(page_no >> bt->seg_bits) % bt->hashsize;
729 if( bt->cache[idx] ) {
730 node->hashnext = hash = bt->nodes + bt->cache[idx];
731 hash->hashprev = node;
734 node->hashprev = NULL;
735 bt->cache[idx] = (ushort)(node - bt->nodes);
738 // remove cache entry from hash table
740 void bt_unlinkhash(BtDb *bt, BtHash *node)
742 uint idx = (uint)(node->page_no >> bt->seg_bits) % bt->hashsize;
746 if( hash = node->hashprev )
747 hash->hashnext = node->hashnext;
748 else if( hash = node->hashnext )
749 bt->cache[idx] = (ushort)(hash - bt->nodes);
753 if( hash = node->hashnext )
754 hash->hashprev = node->hashprev;
757 // add cache page to lru chain and map pages
759 BtPage bt_linklru(BtDb *bt, BtHash *hash, uid page_no)
762 off64_t off = (page_no & ~bt->hashmask) << bt->page_bits;
763 off64_t limit = off + ((bt->hashmask+1) << bt->page_bits);
766 memset(hash, 0, sizeof(BtHash));
767 hash->page_no = (page_no & ~bt->hashmask);
768 bt_linkhash(bt, hash, page_no);
770 if( node = hash->lrunext = bt->lrufirst )
771 node->lruprev = hash;
778 flag = PROT_READ | ( bt->mode == BT_ro ? 0 : PROT_WRITE );
779 hash->page = (BtPage)mmap (0, (bt->hashmask+1) << bt->page_bits, flag, MAP_SHARED, bt->idx, off);
780 if( hash->page == MAP_FAILED )
781 return bt->err = BTERR_map, (BtPage)NULL;
784 flag = ( bt->mode == BT_ro ? PAGE_READONLY : PAGE_READWRITE );
785 hash->hmap = CreateFileMapping(bt->idx, NULL, flag, (DWORD)(limit >> 32), (DWORD)limit, NULL);
787 return bt->err = BTERR_map, NULL;
789 flag = ( bt->mode == BT_ro ? FILE_MAP_READ : FILE_MAP_WRITE );
790 hash->page = MapViewOfFile(hash->hmap, flag, (DWORD)(off >> 32), (DWORD)off, (bt->hashmask+1) << bt->page_bits);
792 return bt->err = BTERR_map, NULL;
795 return (BtPage)((char*)hash->page + ((uint)(page_no & bt->hashmask) << bt->page_bits));
798 // find or place requested page in page-cache
799 // return memory address where page is located.
801 BtPage bt_hashpage(BtDb *bt, uid page_no)
803 BtHash *hash, *node, *next;
806 // find page in cache and move to top of lru list
808 if( hash = bt_findhash(bt, page_no) ) {
809 page = (BtPage)((char*)hash->page + ((uint)(page_no & bt->hashmask) << bt->page_bits));
810 // swap node in lru list
811 if( node = hash->lruprev ) {
812 if( next = node->lrunext = hash->lrunext )
813 next->lruprev = node;
817 if( next = hash->lrunext = bt->lrufirst )
818 next->lruprev = hash;
820 return bt->err = BTERR_hash, (BtPage)NULL;
822 hash->lruprev = NULL;
828 // map pages and add to cache entry
830 if( bt->nodecnt < bt->nodemax ) {
831 hash = bt->nodes + ++bt->nodecnt;
832 return bt_linklru(bt, hash, page_no);
835 // hash table is already full, replace last lru entry from the cache
837 if( hash = bt->lrulast ) {
838 // unlink from lru list
839 if( node = bt->lrulast = hash->lruprev )
840 node->lrunext = NULL;
842 return bt->err = BTERR_hash, (BtPage)NULL;
845 munmap (hash->page, (bt->hashmask+1) << bt->page_bits);
847 FlushViewOfFile(hash->page, 0);
848 UnmapViewOfFile(hash->page);
849 CloseHandle(hash->hmap);
851 // unlink from hash table
853 bt_unlinkhash(bt, hash);
855 // map and add to cache
857 return bt_linklru(bt, hash, page_no);
860 return bt->err = BTERR_hash, (BtPage)NULL;
863 // map a btree page onto current page
865 BTERR bt_mappage (BtDb *bt, BtPage *page, uid page_no)
867 off64_t off = page_no << bt->page_bits;
872 if( bt->mapped_io ) {
874 *page = bt_hashpage(bt, page_no);
878 if( pread(bt->idx, *page, bt->page_size, off) < bt->page_size )
879 return bt->err = BTERR_map;
881 SetFilePointer (bt->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
883 if( !ReadFile(bt->idx, *page, bt->page_size, amt, NULL) )
884 return bt->err = BTERR_map;
886 if( *amt < bt->page_size )
887 return bt->err = BTERR_map;
892 // deallocate a deleted page
893 // place on free chain out of allocator page
895 BTERR bt_freepage(BtDb *bt, uid page_no)
897 if( bt_mappage (bt, &bt->temp, page_no) )
900 // lock allocation page
902 if( bt_lockpage(bt, ALLOC_page, BtLockWrite) )
905 if( bt_mappage (bt, &bt->alloc, ALLOC_page) )
908 // store chain in second right
909 bt_putid(bt->temp->right, bt_getid(bt->alloc[1].right));
910 bt_putid(bt->alloc[1].right, page_no);
913 if( bt_update(bt, bt->alloc, ALLOC_page) )
915 if( bt_update(bt, bt->temp, page_no) )
920 if( bt_unlockpage(bt, ALLOC_page, BtLockWrite) )
923 // remove write lock on deleted node
925 if( bt_unlockpage(bt, page_no, BtLockWrite) )
928 // remove delete lock on deleted node
930 if( bt_unlockpage(bt, page_no, BtLockDelete) )
936 // allocate a new page and write page into it
938 uid bt_newpage(BtDb *bt, BtPage page)
946 if( bt_lockpage(bt, ALLOC_page, BtLockWrite) )
949 if( bt_mappage (bt, &bt->alloc, ALLOC_page) )
952 // use empty chain first
953 // else allocate empty page
955 if( new_page = bt_getid(bt->alloc[1].right) ) {
956 if( bt_mappage (bt, &bt->temp, new_page) )
957 return 0; // don't unlock on error
958 bt_putid(bt->alloc[1].right, bt_getid(bt->temp->right));
961 new_page = bt_getid(bt->alloc->right);
962 bt_putid(bt->alloc->right, new_page+1);
966 if( bt_update(bt, bt->alloc, ALLOC_page) )
967 return 0; // don't unlock on error
969 if( !bt->mapped_io ) {
970 if( bt_update(bt, page, new_page) )
971 return 0; //don't unlock on error
975 if( bt_unlockpage(bt, ALLOC_page, BtLockWrite) )
982 if( pwrite(bt->idx, page, bt->page_size, new_page << bt->page_bits) < bt->page_size )
983 return bt->err = BTERR_wrt, 0;
985 // if writing first page of hash block, zero last page in the block
987 if( !reuse && bt->hashmask > 0 && (new_page & bt->hashmask) == 0 )
989 // use temp buffer to write zeros
990 memset(bt->zero, 0, bt->page_size);
991 if( pwrite(bt->idx,bt->zero, bt->page_size, (new_page | bt->hashmask) << bt->page_bits) < bt->page_size )
992 return bt->err = BTERR_wrt, 0;
995 // bring new page into page-cache and copy page.
996 // this will extend the file into the new pages.
998 if( !(pmap = (char*)bt_hashpage(bt, new_page & ~bt->hashmask)) )
1001 memcpy(pmap+((new_page & bt->hashmask) << bt->page_bits), page, bt->page_size);
1006 if( bt_unlockpage(bt, ALLOC_page, BtLockWrite) )
1012 // find slot in page for given key at a given level
1014 int bt_findslot (BtDb *bt, unsigned char *key, uint len)
1016 uint diff, higher = bt->page->cnt, low = 1, slot;
1019 // make stopper key an infinite fence value
1021 if( bt_getid (bt->page->right) )
1026 // low is the lowest candidate, higher is already
1027 // tested as .ge. the given key, loop ends when they meet
1029 while( diff = higher - low ) {
1030 slot = low + ( diff >> 1 );
1031 if( keycmp (keyptr(bt->page, slot), key, len) < 0 )
1034 higher = slot, good++;
1037 // return zero if key is on right link page
1039 return good ? higher : 0;
1042 // find and load page at given level for given key
1043 // leave page rd or wr locked as requested
1045 int bt_loadpage (BtDb *bt, unsigned char *key, uint len, uint lvl, uint lock)
1047 uid page_no = ROOT_page, prevpage = 0;
1048 uint drill = 0xff, slot;
1049 uint mode, prevmode;
1051 // start at root of btree and drill down
1054 // determine lock mode of drill level
1055 mode = (lock == BtLockWrite) && (drill == lvl) ? BtLockWrite : BtLockRead;
1057 bt->page_no = page_no;
1059 // obtain access lock using lock chaining
1061 if( page_no > ROOT_page )
1062 if( bt_lockpage(bt, bt->page_no, BtLockAccess) )
1066 if( bt_unlockpage(bt, prevpage, prevmode) )
1069 // obtain read lock using lock chaining
1071 if( bt_lockpage(bt, bt->page_no, mode) )
1074 if( page_no > ROOT_page )
1075 if( bt_unlockpage(bt, bt->page_no, BtLockAccess) )
1078 // map/obtain page contents
1080 if( bt_mappage (bt, &bt->page, page_no) )
1083 // re-read and re-lock root after determining actual level of root
1085 if( bt->page->lvl != drill) {
1086 if( bt->page_no != ROOT_page )
1087 return bt->err = BTERR_struct, 0;
1089 drill = bt->page->lvl;
1091 if( lock != BtLockRead && drill == lvl )
1092 if( bt_unlockpage(bt, page_no, mode) )
1098 prevpage = bt->page_no;
1101 // find key on page at this level
1102 // and descend to requested level
1104 if( !bt->page->kill )
1105 if( slot = bt_findslot (bt, key, len) ) {
1109 while( slotptr(bt->page, slot)->dead )
1110 if( slot++ < bt->page->cnt )
1115 page_no = bt_getid(slotptr(bt->page, slot)->id);
1120 // or slide right into next page
1123 page_no = bt_getid(bt->page->right);
1127 // return error on end of right chain
1129 bt->err = BTERR_struct;
1130 return 0; // return error
1133 // a fence key was deleted from a page
1134 // push new fence value upwards
1136 BTERR bt_fixfence (BtDb *bt, uid page_no, uint lvl)
1138 unsigned char leftkey[256], rightkey[256];
1141 // remove deleted key, the old fence value
1143 ptr = keyptr(bt->page, bt->page->cnt);
1144 memcpy(rightkey, ptr, ptr->len + 1);
1146 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1147 bt->page->dirty = 1;
1149 ptr = keyptr(bt->page, bt->page->cnt);
1150 memcpy(leftkey, ptr, ptr->len + 1);
1152 if( bt_update (bt, bt->page, page_no) )
1155 if( bt_lockpage (bt, page_no, BtLockParent) )
1158 if( bt_unlockpage (bt, page_no, BtLockWrite) )
1161 // insert new (now smaller) fence key
1163 if( bt_insertkey (bt, leftkey+1, *leftkey, lvl + 1, page_no, time(NULL)) )
1166 // remove old (larger) fence key
1168 if( bt_deletekey (bt, rightkey+1, *rightkey, lvl + 1) )
1171 return bt_unlockpage (bt, page_no, BtLockParent);
1174 // root has a single child
1175 // collapse a level from the btree
1176 // call with root locked in bt->page
1178 BTERR bt_collapseroot (BtDb *bt, BtPage root)
1183 // find the child entry
1184 // and promote to new root
1187 for( idx = 0; idx++ < root->cnt; )
1188 if( !slotptr(root, idx)->dead )
1191 child = bt_getid (slotptr(root, idx)->id);
1193 if( bt_lockpage (bt, child, BtLockDelete) )
1196 if( bt_lockpage (bt, child, BtLockWrite) )
1199 if( bt_mappage (bt, &bt->temp, child) )
1202 memcpy (root, bt->temp, bt->page_size);
1204 if( bt_update (bt, root, ROOT_page) )
1207 if( bt_freepage (bt, child) )
1210 } while( root->lvl > 1 && root->act == 1 );
1212 return bt_unlockpage (bt, ROOT_page, BtLockWrite);
1215 // find and delete key on page by marking delete flag bit
1216 // when page becomes empty, delete it
1218 BTERR bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl)
1220 unsigned char lowerkey[256], higherkey[256];
1221 uint slot, dirty = 0, idx, fence, found;
1225 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1226 ptr = keyptr(bt->page, slot);
1230 // are we deleting a fence slot?
1232 fence = slot == bt->page->cnt;
1234 // if key is found delete it, otherwise ignore request
1236 if( found = !keycmp (ptr, key, len) )
1237 if( found = slotptr(bt->page, slot)->dead == 0 ) {
1238 dirty = slotptr(bt->page,slot)->dead = 1;
1239 bt->page->dirty = 1;
1242 // collapse empty slots
1244 while( idx = bt->page->cnt - 1 )
1245 if( slotptr(bt->page, idx)->dead ) {
1246 *slotptr(bt->page, idx) = *slotptr(bt->page, idx + 1);
1247 memset (slotptr(bt->page, bt->page->cnt--), 0, sizeof(BtSlot));
1252 right = bt_getid(bt->page->right);
1253 page_no = bt->page_no;
1255 // did we delete a fence key in an upper level?
1257 if( dirty && lvl && bt->page->act && fence )
1258 if( bt_fixfence (bt, page_no, lvl) )
1261 return bt->found = found, 0;
1263 // is this a collapsed root?
1265 if( lvl > 1 && page_no == ROOT_page && bt->page->act == 1 )
1266 if( bt_collapseroot (bt, bt->page) )
1269 return bt->found = found, 0;
1271 // return if page is not empty
1273 if( bt->page->act ) {
1274 if( dirty && bt_update(bt, bt->page, page_no) )
1276 if( bt_unlockpage(bt, page_no, BtLockWrite) )
1278 return bt->found = found, 0;
1281 // cache copy of fence key
1282 // in order to find parent
1284 ptr = keyptr(bt->page, bt->page->cnt);
1285 memcpy(lowerkey, ptr, ptr->len + 1);
1287 // obtain lock on right page
1289 if( bt_lockpage(bt, right, BtLockWrite) )
1292 if( bt_mappage (bt, &bt->temp, right) )
1295 if( bt->temp->kill )
1296 return bt->err = BTERR_struct;
1298 // pull contents of next page into current empty page
1300 memcpy (bt->page, bt->temp, bt->page_size);
1302 // cache copy of key to update
1304 ptr = keyptr(bt->temp, bt->temp->cnt);
1305 memcpy(higherkey, ptr, ptr->len + 1);
1307 // Mark right page as deleted and point it to left page
1308 // until we can post updates at higher level.
1310 bt_putid(bt->temp->right, page_no);
1313 if( bt_update(bt, bt->page, page_no) )
1316 if( bt_update(bt, bt->temp, right) )
1319 if( bt_lockpage(bt, page_no, BtLockParent) )
1322 if( bt_unlockpage(bt, page_no, BtLockWrite) )
1325 if( bt_lockpage(bt, right, BtLockParent) )
1328 if( bt_unlockpage(bt, right, BtLockWrite) )
1331 // redirect higher key directly to consolidated node
1333 if( bt_insertkey (bt, higherkey+1, *higherkey, lvl+1, page_no, time(NULL)) )
1336 // delete old lower key to consolidated node
1338 if( bt_deletekey (bt, lowerkey + 1, *lowerkey, lvl + 1) )
1341 // obtain write & delete lock on deleted node
1342 // add right block to free chain
1344 if( bt_lockpage(bt, right, BtLockDelete) )
1347 if( bt_lockpage(bt, right, BtLockWrite) )
1350 if( bt_freepage (bt, right) )
1353 // remove ParentModify locks
1355 if( bt_unlockpage(bt, right, BtLockParent) )
1358 return bt_unlockpage(bt, page_no, BtLockParent);
1361 // find key in leaf level and return row-id
1363 uid bt_findkey (BtDb *bt, unsigned char *key, uint len)
1369 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1370 ptr = keyptr(bt->page, slot);
1374 // if key exists, return row-id
1375 // otherwise return 0
1377 if( ptr->len == len && !memcmp (ptr->key, key, len) )
1378 id = bt_getid(slotptr(bt->page,slot)->id);
1382 if( bt_unlockpage(bt, bt->page_no, BtLockRead) )
1388 // check page for space available,
1389 // clean if necessary and return
1390 // 0 - page needs splitting
1391 // >0 - go ahead with new slot
1393 uint bt_cleanpage(BtDb *bt, uint amt, uint slot)
1395 uint nxt = bt->page_size;
1396 BtPage page = bt->page;
1397 uint cnt = 0, idx = 0;
1398 uint max = page->cnt;
1399 uint newslot = slot;
1403 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1406 // skip cleanup if nothing to reclaim
1411 memcpy (bt->frame, page, bt->page_size);
1413 // skip page info and set rest of page to zero
1415 memset (page+1, 0, bt->page_size - sizeof(*page));
1418 while( cnt++ < max ) {
1421 // always leave fence key in list
1422 if( cnt < max && slotptr(bt->frame,cnt)->dead )
1426 key = keyptr(bt->frame, cnt);
1427 nxt -= key->len + 1;
1428 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1431 memcpy(slotptr(page, ++idx)->id, slotptr(bt->frame, cnt)->id, BtId);
1432 if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
1434 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1435 slotptr(page, idx)->off = nxt;
1441 if( page->min >= (max+1) * sizeof(BtSlot) + sizeof(*page) + amt + 1 )
1447 // split the root and raise the height of the btree
1449 BTERR bt_splitroot(BtDb *bt, unsigned char *leftkey, uid page_no2)
1451 uint nxt = bt->page_size;
1452 BtPage root = bt->page;
1455 // Obtain an empty page to use, and copy the current
1456 // root contents into it
1458 if( !(right = bt_newpage(bt, root)) )
1461 // preserve the page info at the bottom
1462 // and set rest to zero
1464 memset(root+1, 0, bt->page_size - sizeof(*root));
1466 // insert first key on newroot page
1468 nxt -= *leftkey + 1;
1469 memcpy ((unsigned char *)root + nxt, leftkey, *leftkey + 1);
1470 bt_putid(slotptr(root, 1)->id, right);
1471 slotptr(root, 1)->off = nxt;
1473 // insert second key on newroot page
1474 // and increase the root height
1477 ((unsigned char *)root)[nxt] = 2;
1478 ((unsigned char *)root)[nxt+1] = 0xff;
1479 ((unsigned char *)root)[nxt+2] = 0xff;
1480 bt_putid(slotptr(root, 2)->id, page_no2);
1481 slotptr(root, 2)->off = nxt;
1483 bt_putid(root->right, 0);
1484 root->min = nxt; // reset lowest used offset and key count
1489 // update and release root (bt->page)
1491 if( bt_update(bt, root, bt->page_no) )
1494 return bt_unlockpage(bt, bt->page_no, BtLockWrite);
1497 // split already locked full node
1500 BTERR bt_splitpage (BtDb *bt)
1502 uint cnt = 0, idx = 0, max, nxt = bt->page_size;
1503 unsigned char fencekey[256], rightkey[256];
1504 uid page_no = bt->page_no, right;
1505 BtPage page = bt->page;
1506 uint lvl = page->lvl;
1509 // split higher half of keys to bt->frame
1510 // the last key (fence key) might be dead
1512 memset (bt->frame, 0, bt->page_size);
1517 while( cnt++ < max ) {
1518 key = keyptr(page, cnt);
1519 nxt -= key->len + 1;
1520 memcpy ((unsigned char *)bt->frame + nxt, key, key->len + 1);
1521 memcpy(slotptr(bt->frame,++idx)->id, slotptr(page,cnt)->id, BtId);
1522 if( !(slotptr(bt->frame, idx)->dead = slotptr(page, cnt)->dead) )
1524 slotptr(bt->frame, idx)->tod = slotptr(page, cnt)->tod;
1525 slotptr(bt->frame, idx)->off = nxt;
1528 // remember fence key for new right page
1530 memcpy (rightkey, key, key->len + 1);
1532 bt->frame->bits = bt->page_bits;
1533 bt->frame->min = nxt;
1534 bt->frame->cnt = idx;
1535 bt->frame->lvl = lvl;
1539 if( page_no > ROOT_page )
1540 memcpy (bt->frame->right, page->right, BtId);
1542 // get new free page and write frame to it.
1544 if( !(right = bt_newpage(bt, bt->frame)) )
1547 // update lower keys to continue in old page
1549 memcpy (bt->frame, page, bt->page_size);
1550 memset (page+1, 0, bt->page_size - sizeof(*page));
1551 nxt = bt->page_size;
1557 // assemble page of smaller keys
1558 // (they're all active keys)
1560 while( cnt++ < max / 2 ) {
1561 key = keyptr(bt->frame, cnt);
1562 nxt -= key->len + 1;
1563 memcpy ((unsigned char *)page + nxt, key, key->len + 1);
1564 memcpy(slotptr(page,++idx)->id, slotptr(bt->frame,cnt)->id, BtId);
1565 slotptr(page, idx)->tod = slotptr(bt->frame, cnt)->tod;
1566 slotptr(page, idx)->off = nxt;
1570 // remember fence key for smaller page
1572 memcpy (fencekey, key, key->len + 1);
1574 bt_putid(page->right, right);
1578 // if current page is the root page, split it
1580 if( page_no == ROOT_page )
1581 return bt_splitroot (bt, fencekey, right);
1585 if( bt_lockpage (bt, right, BtLockParent) )
1588 // update left (containing) node
1590 if( bt_update(bt, page, page_no) )
1593 if( bt_lockpage (bt, page_no, BtLockParent) )
1596 if( bt_unlockpage (bt, page_no, BtLockWrite) )
1599 // insert new fence for reformulated left block
1601 if( bt_insertkey (bt, fencekey+1, *fencekey, lvl+1, page_no, time(NULL)) )
1604 // switch fence for right block of larger keys to new right page
1606 if( bt_insertkey (bt, rightkey+1, *rightkey, lvl+1, right, time(NULL)) )
1609 if( bt_unlockpage (bt, page_no, BtLockParent) )
1612 return bt_unlockpage (bt, right, BtLockParent);
1615 // Insert new key into the btree at requested level.
1616 // Level zero pages are leaf pages and are unlocked at exit.
1617 // Interior nodes remain locked.
1619 BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, uid id, uint tod)
1626 if( slot = bt_loadpage (bt, key, len, lvl, BtLockWrite) )
1627 ptr = keyptr(bt->page, slot);
1631 bt->err = BTERR_ovflw;
1635 // if key already exists, update id and return
1639 if( !keycmp (ptr, key, len) ) {
1640 if( slotptr(page, slot)->dead )
1642 slotptr(page, slot)->dead = 0;
1643 slotptr(page, slot)->tod = tod;
1644 bt_putid(slotptr(page,slot)->id, id);
1645 if( bt_update(bt, bt->page, bt->page_no) )
1647 return bt_unlockpage(bt, bt->page_no, BtLockWrite);
1650 // check if page has enough space
1652 if( slot = bt_cleanpage (bt, len, slot) )
1655 if( bt_splitpage (bt) )
1659 // calculate next available slot and copy key into page
1661 page->min -= len + 1; // reset lowest used offset
1662 ((unsigned char *)page)[page->min] = len;
1663 memcpy ((unsigned char *)page + page->min +1, key, len );
1665 for( idx = slot; idx < page->cnt; idx++ )
1666 if( slotptr(page, idx)->dead )
1669 // now insert key into array before slot
1670 // preserving the fence slot
1672 if( idx == page->cnt )
1678 *slotptr(page, idx) = *slotptr(page, idx -1), idx--;
1680 bt_putid(slotptr(page,slot)->id, id);
1681 slotptr(page, slot)->off = page->min;
1682 slotptr(page, slot)->tod = tod;
1683 slotptr(page, slot)->dead = 0;
1685 if( bt_update(bt, bt->page, bt->page_no) )
1688 return bt_unlockpage(bt, bt->page_no, BtLockWrite);
1691 // cache page of keys into cursor and return starting slot for given key
1693 uint bt_startkey (BtDb *bt, unsigned char *key, uint len)
1697 // cache page for retrieval
1699 if( slot = bt_loadpage (bt, key, len, 0, BtLockRead) )
1700 memcpy (bt->cursor, bt->page, bt->page_size);
1704 bt->cursor_page = bt->page_no;
1706 if( bt_unlockpage(bt, bt->page_no, BtLockRead) )
1712 // return next slot for cursor page
1713 // or slide cursor right into next page
1715 uint bt_nextkey (BtDb *bt, uint slot)
1720 right = bt_getid(bt->cursor->right);
1722 while( slot++ < bt->cursor->cnt )
1723 if( slotptr(bt->cursor,slot)->dead )
1725 else if( right || (slot < bt->cursor->cnt))
1733 bt->cursor_page = right;
1735 if( bt_lockpage(bt, right, BtLockRead) )
1738 if( bt_mappage (bt, &bt->page, right) )
1741 memcpy (bt->cursor, bt->page, bt->page_size);
1743 if( bt_unlockpage(bt, right, BtLockRead) )
1753 BtKey bt_key(BtDb *bt, uint slot)
1755 return keyptr(bt->cursor, slot);
1758 uid bt_uid(BtDb *bt, uint slot)
1760 return bt_getid(slotptr(bt->cursor,slot)->id);
1763 uint bt_tod(BtDb *bt, uint slot)
1765 return slotptr(bt->cursor,slot)->tod;
1772 double getCpuTime(int type)
1775 FILETIME xittime[1];
1776 FILETIME systime[1];
1777 FILETIME usrtime[1];
1778 SYSTEMTIME timeconv[1];
1781 memset (timeconv, 0, sizeof(SYSTEMTIME));
1785 GetSystemTimeAsFileTime (xittime);
1786 FileTimeToSystemTime (xittime, timeconv);
1787 ans = (double)timeconv->wDayOfWeek * 3600 * 24;
1790 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
1791 FileTimeToSystemTime (usrtime, timeconv);
1794 GetProcessTimes (GetCurrentProcess(), crtime, xittime, systime, usrtime);
1795 FileTimeToSystemTime (systime, timeconv);
1799 ans += (double)timeconv->wHour * 3600;
1800 ans += (double)timeconv->wMinute * 60;
1801 ans += (double)timeconv->wSecond;
1802 ans += (double)timeconv->wMilliseconds / 1000;
1807 #include <sys/resource.h>
1809 double getCpuTime(int type)
1811 struct rusage used[1];
1812 struct timeval tv[1];
1816 gettimeofday(tv, NULL);
1817 return (double)tv->tv_sec + (double)tv->tv_usec / 1000000;
1820 getrusage(RUSAGE_SELF, used);
1821 return (double)used->ru_utime.tv_sec + (double)used->ru_utime.tv_usec / 1000000;
1824 getrusage(RUSAGE_SELF, used);
1825 return (double)used->ru_stime.tv_sec + (double)used->ru_stime.tv_usec / 1000000;
1832 // standalone program to index file of keys
1833 // then list them onto std-out
1835 int main (int argc, char **argv)
1837 uint slot, line = 0, off = 0, found = 0;
1838 int dead, ch, cnt = 0, bits = 12;
1839 unsigned char key[256];
1853 fprintf (stderr, "Usage: %s idx_file src_file Read/Write/Scan/Delete/Find [page_bits mapped_pool_segments pages_per_segment start_line_number]\n", argv[0]);
1854 fprintf (stderr, " page_bits: size of btree page in bits\n");
1855 fprintf (stderr, " mapped_pool_segments: size of buffer pool in segments\n");
1856 fprintf (stderr, " pages_per_segment: size of buffer pool segment in pages in bits\n");
1860 start = getCpuTime(0);
1864 bits = atoi(argv[4]);
1867 map = atoi(argv[5]);
1870 fprintf (stderr, "Warning: buffer_pool > 65536 segments\n");
1872 if( map && map < 8 )
1873 fprintf (stderr, "Buffer_pool too small\n");
1876 pgblk = atoi(argv[6]);
1878 if( bits + pgblk > 30 )
1879 fprintf (stderr, "Warning: very large buffer pool segment size\n");
1882 off = atoi(argv[7]);
1884 bt = bt_open ((argv[1]), BT_rw, bits, map, pgblk);
1887 fprintf(stderr, "Index Open Error %s\n", argv[1]);
1891 switch(argv[3][0]| 0x20)
1894 fprintf(stderr, "started indexing for %s\n", argv[2]);
1895 if( argc > 2 && (in = fopen (argv[2], "rb")) )
1896 while( ch = getc(in), ch != EOF )
1900 sprintf((char *)key+len, "%.9d", line + off), len += 9;
1902 if( bt_insertkey (bt, key, len, 0, ++line, *tod) )
1903 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
1906 else if( len < 245 )
1908 fprintf(stderr, "finished adding keys for %s, %d \n", argv[2], line);
1912 fprintf(stderr, "started deleting keys for %s\n", argv[2]);
1913 if( argc > 2 && (in = fopen (argv[2], "rb")) )
1914 while( ch = getc(in), ch != EOF )
1918 sprintf((char *)key+len, "%.9d", line + off), len += 9;
1920 if( bt_deletekey (bt, key, len, 0) )
1921 fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
1924 else if( len < 245 )
1926 fprintf(stderr, "finished deleting keys for %s, %d \n", argv[2], line);
1930 fprintf(stderr, "started finding keys for %s\n", argv[2]);
1931 if( argc > 2 && (in = fopen (argv[2], "rb")) )
1932 while( ch = getc(in), ch != EOF )
1936 sprintf((char *)key+len, "%.9d", line + off), len += 9;
1938 if( bt_findkey (bt, key, len) )
1941 fprintf(stderr, "Error %d Syserr %d Line: %d\n", bt->err, errno, line), exit(0);
1944 else if( len < 245 )
1946 fprintf(stderr, "finished search of %d keys for %s, found %d\n", line, argv[2], found);
1950 cnt = len = key[0] = 0;
1952 if( slot = bt_startkey (bt, key, len) )
1955 fprintf(stderr, "Error %d in StartKey. Syserror: %d\n", bt->err, errno), exit(0);
1957 while( slot = bt_nextkey (bt, slot) ) {
1958 ptr = bt_key(bt, slot);
1959 fwrite (ptr->key, ptr->len, 1, stdout);
1960 fputc ('\n', stdout);
1964 fprintf(stderr, " Total keys read %d\n", cnt - 1);
1968 fprintf(stderr, "started counting\n");
1971 page_no = LEAF_page;
1975 uid off = page_no << bt->page_bits;
1977 if( !pread (bt->idx, bt->frame, bt->page_size, off) )
1982 SetFilePointer (bt->idx, (long)off, (long*)(&off)+1, FILE_BEGIN);
1984 if( !ReadFile(bt->idx, bt->frame, bt->page_size, amt, NULL))
1987 if( *amt < bt->page_size )
1988 fprintf (stderr, "unable to read page %.8x", page_no);
1990 if( !bt->frame->free && !bt->frame->lvl )
1991 cnt += bt->frame->act;
1996 cnt--; // remove stopper key
1997 fprintf(stderr, " Total keys read %d\n", cnt);
2001 done = getCpuTime(0);
2002 elapsed = (float)(done - start);
2003 fprintf(stderr, " real %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2004 elapsed = getCpuTime(1);
2005 fprintf(stderr, " user %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);
2006 elapsed = getCpuTime(2);
2007 fprintf(stderr, " sys %dm%.3fs\n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60);