]> pd.if.org Git - btree/blobdiff - threadskv1.c
Fix errors in threadskv1 and threadskv3
[btree] / threadskv1.c
index 57111acb3f7544143bf0bb9d0daf2d028043a3f6..3ce5b8a6d9c58fd020d4ba6ecb756ad4cefbefa4 100644 (file)
@@ -281,7 +281,7 @@ typedef enum {
 // B-Tree functions
 extern void bt_close (BtDb *bt);
 extern BtDb *bt_open (BtMgr *mgr);
-extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, unsigned char *value, uint vallen);
+extern BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint len, uint lvl, void *value, uint vallen);
 extern BTERR  bt_deletekey (BtDb *bt, unsigned char *key, uint len, uint lvl);
 extern int bt_findkey    (BtDb *bt, unsigned char *key, uint keylen, unsigned char *value, uint vallen);
 extern uint bt_startkey  (BtDb *bt, unsigned char *key, uint len);
@@ -292,15 +292,16 @@ extern BtMgr *bt_mgr (char *name, uint mode, uint bits, uint poolsize, uint segs
 void bt_mgrclose (BtMgr *mgr);
 
 //  Helper functions to return slot values
+//     from the cursor page.
 
 extern BtKey bt_key (BtDb *bt, uint slot);
 extern BtVal bt_val (BtDb *bt, uint slot);
 
 //  BTree page number constants
-#define ALLOC_page             0       // allocation & lock manager hash table
+#define ALLOC_page             0       // allocation & latch manager hash table
 #define ROOT_page              1       // root of the btree
 #define LEAF_page              2       // first page of leaves
-#define LATCH_page             3       // pages for lock manager
+#define LATCH_page             3       // pages for latch manager
 
 //     Number of levels to create in a new BTree
 
@@ -309,7 +310,7 @@ extern BtVal bt_val (BtDb *bt, uint slot);
 //  The page is allocated from low and hi ends.
 //  The key slots are allocated from the bottom,
 //     while the text and value of the key
-//  is allocated from the top.  When the two
+//  are allocated from the top.  When the two
 //  areas meet, the page is split into two.
 
 //  A key consists of a length byte, two bytes of
@@ -741,7 +742,7 @@ uint slot;
                pool = mgr->pool + slot;
                if( pool->slot )
 #ifdef unix
-                       munmap (pool->map, (mgr->poolmask+1) << mgr->page_bits);
+                       munmap (pool->map, (uid)(mgr->poolmask+1) << mgr->page_bits);
 #else
                {
                        FlushViewOfFile(pool->map, 0);
@@ -753,7 +754,7 @@ uint slot;
 
 #ifdef unix
        munmap (mgr->latchsets, mgr->latchmgr->nlatchpage * mgr->page_size);
-       munmap (mgr->latchmgr, mgr->page_size);
+       munmap (mgr->latchmgr, 2 * mgr->page_size);
 #else
        FlushViewOfFile(mgr->latchmgr, 0);
        UnmapViewOfFile(mgr->latchmgr);
@@ -992,13 +993,18 @@ SYSTEM_INFO sysinfo[1];
 
 mgrlatch:
 #ifdef unix
+       // mlock the root page and the latchmgr page
+
        flag = PROT_READ | PROT_WRITE;
-       mgr->latchmgr = mmap (0, mgr->page_size, flag, MAP_SHARED, mgr->idx, ALLOC_page * mgr->page_size);
+       mgr->latchmgr = mmap (0, 2 * mgr->page_size, flag, MAP_SHARED, mgr->idx, ALLOC_page * mgr->page_size);
        if( mgr->latchmgr == MAP_FAILED )
                return bt_mgrclose (mgr), NULL;
+       mlock (mgr->latchmgr, 2 * mgr->page_size);
+
        mgr->latchsets = (BtLatchSet *)mmap (0, mgr->latchmgr->nlatchpage * mgr->page_size, flag, MAP_SHARED, mgr->idx, LATCH_page * mgr->page_size);
        if( mgr->latchsets == MAP_FAILED )
                return bt_mgrclose (mgr), NULL;
+       mlock (mgr->latchsets, mgr->latchmgr->nlatchpage * mgr->page_size);
 #else
        flag = PAGE_READWRITE;
        mgr->halloc = CreateFileMapping(mgr->idx, NULL, flag, 0, (BT_latchtable / (mgr->page_size / sizeof(BtLatchSet)) + 1 + LATCH_page) * mgr->page_size, NULL);
@@ -1119,7 +1125,7 @@ int flag;
 
 #ifdef unix
        flag = PROT_READ | ( bt->mgr->mode == BT_ro ? 0 : PROT_WRITE );
-       pool->map = mmap (0, (bt->mgr->poolmask+1) << bt->mgr->page_bits, flag, MAP_SHARED, bt->mgr->idx, off);
+       pool->map = mmap (0, (uid)(bt->mgr->poolmask+1) << bt->mgr->page_bits, flag, MAP_SHARED, bt->mgr->idx, off);
        if( pool->map == MAP_FAILED )
                return bt->err = BTERR_map;
 
@@ -1130,7 +1136,7 @@ int flag;
                return bt->err = BTERR_map;
 
        flag = ( bt->mgr->mode == BT_ro ? FILE_MAP_READ : FILE_MAP_WRITE );
-       pool->map = MapViewOfFile(pool->hmap, flag, (DWORD)(off >> 32), (DWORD)off, (bt->mgr->poolmask+1) << bt->mgr->page_bits);
+       pool->map = MapViewOfFile(pool->hmap, flag, (DWORD)(off >> 32), (DWORD)off, (uid)(bt->mgr->poolmask+1) << bt->mgr->page_bits);
        if( !pool->map )
                return bt->err = BTERR_map;
 #endif
@@ -1145,6 +1151,7 @@ uint subpage = (uint)(page_no & bt->mgr->poolmask); // page within mapping
 BtPage page;
 
        page = (BtPage)(pool->map + (subpage << bt->mgr->page_bits));
+//     madvise (page, bt->mgr->page_size, MADV_WILLNEED);
        return page;
 }
 
@@ -1265,7 +1272,7 @@ BtPool *pool, *node, *next;
 
                //      remove old file mapping
 #ifdef unix
-               munmap (pool->map, (bt->mgr->poolmask+1) << bt->mgr->page_bits);
+               munmap (pool->map, (uid)(bt->mgr->poolmask+1) << bt->mgr->page_bits);
 #else
 //             FlushViewOfFile(pool->map, 0);
                UnmapViewOfFile(pool->map);
@@ -1497,8 +1504,10 @@ BtPool *prevpool;
        //  find key on page at this level
        //  and descend to requested level
 
-       if( !set->page->kill )
-        if( slot = bt_findslot (set, key, len) ) {
+       if( set->page->kill )
+         goto slideright;
+
+       if( slot = bt_findslot (set, key, len) ) {
          if( drill == lvl )
                return slot;
 
@@ -1511,7 +1520,7 @@ BtPool *prevpool;
          page_no = bt_getid(valptr(set->page, slot)->value);
          drill--;
          continue;
-        }
+       }
 
        //  or slide right into next page
 
@@ -1559,7 +1568,6 @@ BTERR bt_fixfence (BtDb *bt, BtPageSet *set, uint lvl)
 {
 unsigned char leftkey[256], rightkey[256];
 unsigned char value[BtId];
-uid page_no;
 BtKey ptr;
 
        //      remove the old fence value
@@ -1572,14 +1580,13 @@ BtKey ptr;
 
        ptr = keyptr(set->page, set->page->cnt);
        memcpy (leftkey, ptr, ptr->len + 1);
-       page_no = set->page_no;
 
        bt_lockpage (BtLockParent, set->latch);
        bt_unlockpage (BtLockWrite, set->latch);
 
        //      insert new (now smaller) fence key
 
-       bt_putid (value, page_no);
+       bt_putid (value, set->page_no);
 
        if( bt_insertkey (bt, leftkey+1, *leftkey, lvl+1, value, BtId) )
          return bt->err;
@@ -1780,10 +1787,10 @@ int ret;
        else
                return 0;
 
-       // if key exists, return TRUE
-       //      otherwise return FALSE
+       // if key exists, return >= 0 value bytes copied
+       //      otherwise return (-1)
 
-       if( !keycmp (ptr, key, keylen) ) {
+       if( !slotptr(set->page, slot)->dead && !keycmp (ptr, key, keylen) ) {
                val = valptr (set->page,slot);
                if( valmax > val->len )
                        valmax = val->len;
@@ -1837,22 +1844,22 @@ BtVal val;
                if( cnt < max && slotptr(bt->frame,cnt)->dead )
                        continue;
 
-               // copy the key across
-
-               key = keyptr(bt->frame, cnt);
-               nxt -= key->len + 1;
-               memcpy ((unsigned char *)page + nxt, key, key->len + 1);
-
                // copy the value across
 
                val = valptr(bt->frame, cnt);
                nxt -= val->len + 1;
                ((unsigned char *)page)[nxt] = val->len;
-               memcpy ((unsigned char *)page + nxt + 1, val, val->len);
+               memcpy ((unsigned char *)page + nxt + 1, val->value, val->len);
+
+               // copy the key across
+
+               key = keyptr(bt->frame, cnt);
+               nxt -= key->len + 1;
+               memcpy ((unsigned char *)page + nxt, key, key->len + 1);
 
                // set up the slot
 
-               slotptr(page, idx)->off = nxt;
+               slotptr(page, ++idx)->off = nxt;
 
                if( !(slotptr(page, idx)->dead = slotptr(bt->frame, cnt)->dead) )
                        page->act++;
@@ -2052,7 +2059,7 @@ BtVal val;
 }
 //  Insert new key into the btree at given level.
 
-BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint keylen, uint lvl, unsigned char *value, uint vallen)
+BTERR bt_insertkey (BtDb *bt, unsigned char *key, uint keylen, uint lvl, void *value, uint vallen)
 {
 BtPageSet set[1];
 uint slot, idx;
@@ -2070,7 +2077,7 @@ BtVal val;
                        return bt->err;
                }
 
-               // if key already exists, update id and return
+               // if key already exists, update value and return
 
                if( reuse = !keycmp (ptr, key, keylen) )
                  if( val = valptr(set->page, slot), val->len >= vallen ) {
@@ -2298,7 +2305,7 @@ BtKey ptr;
 
                if( *latch->parent->rin & MASK )
                        fprintf(stderr, "latchset %d parentlocked for page %.8x\n", idx, latch->page_no);
-               memset ((ushort *)latch->access, 0, sizeof(RWLock));
+               memset ((ushort *)latch->parent, 0, sizeof(RWLock));
 
                if( latch->pin ) {
                        fprintf(stderr, "latchset %d pinned for page %.8x\n", idx, latch->page_no);
@@ -2357,7 +2364,8 @@ BtKey ptr;
 }
 
 typedef struct {
-       char type, idx;
+       char idx;
+       char *type;
        char *infile;
        BtMgr *mgr;
        int num;
@@ -2384,7 +2392,7 @@ FILE *in;
 
        bt = bt_open (args->mgr);
 
-       switch(args->type | 0x20)
+       switch(args->type[0] | 0x20)
        {
        case 'a':
                fprintf(stderr, "started latch mgr audit\n");
@@ -2445,6 +2453,8 @@ FILE *in;
                          else if( args->num )
                                sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
 
+                         if( (args->type[1] | 0x20) == 'p' )
+                               len = 10;
                          if( bt_deletekey (bt, key, len, 0) )
                                fprintf(stderr, "Error %d Line: %d\n", bt->err, line), exit(0);
                          len = 0;
@@ -2467,6 +2477,8 @@ FILE *in;
                          else if( args->num )
                                sprintf((char *)key+len, "%.9d", line + args->idx * args->num), len += 9;
 
+                         if( (args->type[1] | 0x20) == 'p' )
+                               len = 10;
                          if( bt_findkey (bt, key, len, NULL, 0) == 0 )
                                found++;
                          else if( bt->err )
@@ -2622,7 +2634,7 @@ BtDb *bt;
 
        for( idx = 0; idx < cnt; idx++ ) {
                args[idx].infile = argv[idx + 7];
-               args[idx].type = argv[2][0];
+               args[idx].type = argv[2];
                args[idx].mgr = mgr;
                args[idx].num = num;
                args[idx].idx = idx;