1: #include    <btree.h>
   2: #include    <sccs.h>
   3: 
   4: SCCSID(@(#)get_tid.c	8.3	1/31/86)
   5: 
   6: /*	GET_TID -- BTree retrieval routine
   7: **
   8: **	Retrieve the tid corresponding to the given lid
   9: **
  10: **	Parameters :
  11: **		tree - BTree filename (I)
  12: **		lid - given lid (I)
  13: **		tid_id - storage information of location of the tid (O)
  14: **
  15: **	Returns :
  16: **		> 0 	tid
  17: **		-1 	lid corresponds to very last lid in tree
  18: **		-2   	lid does not exist
  19: */
  20: 
  21: long
  22: get_tid(rootpage, lid, tid_id)
  23: 
  24: long rootpage;
  25: long lid;
  26: register struct locator *tid_id;
  27: {
  28:     struct BTreeNode    p;
  29:     long            pgno, sum, k, t;
  30:     register int        i;
  31: 
  32: #	ifdef xATR1
  33:     if (tTf(24, 0))
  34:         printf("getting btree tid for lid %ld in rootpage %d\n", lid, rootpage);
  35: #	endif
  36: 
  37: 
  38:     if (lid <= 0)
  39:         return(-2); /* negative lids nonexistent */
  40: 
  41:     get_node(rootpage, &p);
  42:     pgno = rootpage;
  43:     sum = 0;
  44:     t = 0;
  45: 
  46:     /* find the leaf node containing desired tid */
  47:     while (p.nodetype != 'L')
  48:     {
  49:         if (t == -1)
  50:             /* continue along path to very last lid */
  51:             i = p.nelmts - 1;
  52:         else
  53:         {
  54:             /* find pointer in node which will lead down to proper leaf */
  55:             for (i = 0, k = p.node.intnode.key[0]; sum + k < lid && i < p.nelmts; )
  56:             {
  57:                 sum += k;
  58:                 ++i;
  59:                 if (i < p.nelmts)
  60:                     k = p.node.intnode.key[i];
  61:             }
  62: 
  63:             if (i >= p.nelmts)
  64:             {
  65:                 if (sum < lid - 1)
  66:                     return(-2); /* lid doesn't exist */
  67:                 else
  68:                 {
  69:                     --i;
  70:                     t = -1;
  71:                 }
  72:             }
  73:         }
  74: 
  75:         pgno = p.node.intnode.ptr[i];
  76:         get_node(pgno, &p);
  77:     }
  78: 
  79:     if (t == -1)
  80:         /* new lid is to be inserted at very last leaf position */
  81:         tid_id->offset = p.nelmts;
  82:     else
  83:     {
  84:         /* search through the leaf for the proper tid */
  85:         for (i = 0, ++sum, t = p.node.leafnode.tid_pos[p.node.leafnode.tid_loc[0]]; sum < lid && i < p.nelmts; )
  86:         {
  87:             ++sum;
  88:             ++i;
  89:             if (i < p.nelmts)
  90:                 t = p.node.leafnode.tid_pos[p.node.leafnode.tid_loc[i]];
  91:         }
  92:         if (i >= p.nelmts)
  93:         {
  94:             if (sum < lid)
  95:                 return(-2); /* lid doesn't exist */
  96:             else
  97:                 t = -1;
  98:         }
  99:         tid_id->offset = i;
 100:     }
 101: 
 102:     bmove(&p, &tid_id->page, sizeof p);
 103:     tid_id->pageno = pgno;
 104: 
 105: #	ifdef xATR1
 106:     if (tTf(24, 0))
 107:     {
 108:         printf("Main relation tid found in tree:");
 109:         dumptid(&t);
 110:         printf("Btree tid location:");
 111:         dumptid(tid_id);
 112:     }
 113: #	endif
 114: 
 115:     return(t);
 116: }
Last modified: 1986-04-17
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 601
Valid CSS Valid XHTML 1.0 Strict