Annotation of 42BSD/ingres/source/iutil/replace.c, revision 1.1.1.1

1.1       root        1: # include      <ingres.h>
                      2: # include      <access.h>
                      3: # include      <sccs.h>
                      4: 
                      5: SCCSID(@(#)replace.c   7.1     2/5/81)
                      6: 
                      7: # define       SAMETUP         0
                      8: # define       SAMEKEYS        1
                      9: # define       DIFFTUP         2
                     10: 
                     11: /*
                     12: **  REPLACE - replace an already existing tuple
                     13: **
                     14: **     Replace will replace the tuple specified by TID
                     15: **     with the new tuple. An attempt is made to not
                     16: **     move the tuple if at all possible.
                     17: **
                     18: **     Three separate conditions are delt with. If the
                     19: **     new tuple is the same as the old tuple, a return 
                     20: **     of zero occures and the page is not changed.
                     21: **
                     22: **     If the keys(if any) are the same and the canonical
                     23: **     tuple lengths are the same, then the new tuple will
                     24: **     be placed in the same location.
                     25: **
                     26: **     If the lengths or the keys are different, then the
                     27: **     tuple is deleted and the new tuple inserted
                     28: **
                     29: **     Checkdups specifies whether to check for duplicates.
                     30: **     If the new tuple is a duplicate of one already there,
                     31: **     then the tuple at TID is deleted
                     32: **
                     33: **     Returns:
                     34: **             <0  fatal error
                     35: **              1  new tuple was duplicate of returned tid
                     36: **              2  tuple identified by tid has been deleted
                     37: **
                     38: **             If replace returns 1 then tid is set to the
                     39: **             duplicate tuple. This is necessary for updating
                     40: **             secondary indices.
                     41: **
                     42: **     Trace Flags:
                     43: **             24.4-7
                     44: */
                     45: 
                     46: 
                     47: replace(d, tid, tuple, checkdups)      
                     48: register DESC  *d;
                     49: register TID   *tid;
                     50: char           *tuple;
                     51: int            checkdups;
                     52: {
                     53:        register int    i;
                     54:        char            oldtuple[MAXTUP];
                     55:        TID             primtid;
                     56:        long            primpage;
                     57:        int             need, same;
                     58:        int             len, oldlength;
                     59:        char            *new, *old, *oldt;
                     60:        char            *getint_tuple();
                     61: 
                     62: #      ifdef xATR1
                     63:        if (tTf(24, 4))
                     64:        {
                     65:                printf("replace: %.14s,", d->reldum.relid);
                     66:                dumptid(tid);
                     67:                printf("replace: ");
                     68:                printup(d, tuple);
                     69:        }
                     70: #      endif
                     71: 
                     72:        /* make tuple canonical */
                     73:        need = canonical(d, tuple);
                     74: 
                     75:        /* if heap, no dup checking */
                     76:        if (abs(d->reldum.relspec) == M_HEAP)
                     77:                checkdups = FALSE;
                     78: 
                     79:        if (i = get_page(d, tid))
                     80:                return (i);     /* fatal error */
                     81: 
                     82:        /* check if tid exists */
                     83:        if (i = invalid(tid))
                     84:                return (i);     /* already deleted or invalid */
                     85: 
                     86:        oldt = getint_tuple(d, tid, oldtuple);
                     87:        oldlength = tup_len(tid);
                     88: 
                     89:        /* check whether tuples are the same, different lengths, different keys */
                     90:        same = DIFFTUP; /* assume diff lengths or keys */
                     91:        if (oldlength == need)
                     92:        {
                     93:                /* same size. check for same domains */
                     94:                same = SAMETUP; /* assume identical */
                     95:                new = tuple;
                     96:                old = oldt;
                     97:                for (i = 1; i <= d->reldum.relatts; i++)
                     98:                {
                     99:                        len = d->relfrml[i] & I1MASK;
                    100:                        if (icompare(new, old, d->relfrmt[i], len))
                    101:                        {
                    102:                                if (d->relxtra[i])
                    103:                                {
                    104:                                        same = DIFFTUP;
                    105:                                        break;
                    106:                                }
                    107:                                same = SAMEKEYS;
                    108:                        }
                    109:                        old += len;
                    110:                        new += len;
                    111:                }
                    112:        }
                    113: 
                    114: #      ifdef xATR2
                    115:        if (tTf(24, 5))
                    116:                printf("replace:same=%d\n", same);
                    117: #      endif
                    118:        switch (same)
                    119:        {
                    120: 
                    121:          case SAMETUP:
                    122:                /* new tuple same as old tuple */
                    123:                i = 1;  /* flag as duplicate */
                    124:                /* though character strings may compare equal,
                    125:                **  they can look different, so if they do look different
                    126:                **  go ahead and do the replace using put_tuple.  */
                    127:                if (!bequal(tuple, oldt, d->reldum.relwid))
                    128:                        goto puttuple;
                    129:                break;
                    130: 
                    131:          case SAMEKEYS:
                    132:                /* keys the same, lengths the same, tuples different */
                    133:                if (checkdups)
                    134:                {
                    135:                        /* This is either an ISAM or HASH file. If mainpg
                    136:                        ** is non-zero, then the primary page=mainpg -1.
                    137:                        ** Otherwise, "find" must be called to determine
                    138:                        ** the primary page
                    139:                        */
                    140:                        if (Acc_head->mainpg)
                    141:                        {
                    142:                                primpage = Acc_head->mainpg -1;
                    143:                                stuff_page(&primtid, &primpage);
                    144:                        }
                    145:                        else
                    146:                        {
                    147:                                if (i = find(d, FULLKEY, &primtid, &primtid, tuple))
                    148:                                        return (i);     /* fatal error */
                    149:                                if (i = get_page(d, tid))       /* restore page for tuple */
                    150:                                        return (i);
                    151:                        }
                    152:        
                    153:                        if (i = scan_dups(d, &primtid, tuple))
                    154:                        {
                    155:                                if (i == 1)
                    156:                                {
                    157:                                        del_tuple(tid, oldlength);      /* tuple a duplicate */
                    158:                                        d->reladds--;
                    159:                                        /* copy tid of duplicate tuple */
                    160:                                        bmove(&primtid, tid, sizeof(primtid));
                    161:                                }
                    162:                                break;
                    163:                        }
                    164:                }
                    165:                goto puttuple;
                    166: 
                    167:          case DIFFTUP:
                    168:                /* keys different or lengths different */
                    169:                del_tuple(tid, oldlength);
                    170: 
                    171:                /* find where to put tuple */
                    172:                if (i = findbest(d, tid, tuple, need, checkdups))
                    173:                {
                    174:                        d->reladds--;
                    175:                        break;
                    176:                }
                    177: 
                    178:                /* place new tuple in page */
                    179:        puttuple:
                    180:                put_tuple(tid, Acctuple, need);
                    181:                i = 0;
                    182:        }
                    183: 
                    184: #      ifdef xATR1
                    185:        if (tTf(24, 6))
                    186:        {
                    187:                printf("replace rets %d,", i);
                    188:                dumptid(tid);
                    189:        }
                    190: #      endif
                    191: 
                    192:        return (i);
                    193: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.