⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pblisam.c

📁 B树算法实现
💻 C
📖 第 1 页 / 共 5 页
字号:
    if( isam->keydup[ index ] )    {        okey[ keylen ] = 0;    }    return( keylen );}/* * read a key from the allkeys record of an entry in the main file * * if the reference is requested it is appended to the key */static int pblIsamThisKey(PBLISAMFILE_t  * isam,int              index,int              reference,unsigned char  * okey){    int             okeylen = -1;    char            rkey[ PBLKEYLENGTH ];    int             rkeylen;    char            fkey[ PBLKEYLENGTH ];    int             fkeylen;    unsigned char   data[ PBLDATALENGTH ];    long            datalen;    long            rc;    unsigned char * key;    int             n;    /*     * make sure the index is in bounds     */    if( index >= isam->nkeys )    {        pbl_errno = PBL_ERROR_PARAM_INDEX;        return( -1 );    }    /*     * read the key of the current record in the main file     */    datalen = pblKfThis( isam->mainfile, fkey, &fkeylen );    if( datalen < 0 )    {        return( -1 );    }    /*     * length 8 or 17 is for allkeys records     * if the length is 9 or 18, the record is a data record     */    if( fkeylen != 8 && fkeylen != 17 )    {        pbl_errno = PBL_ERROR_POSITION;        return( -1 );    }    fkey[ fkeylen ] = 0;    /*     * the allkeys record can not be longer than a single data record     */    if( datalen > PBLDATALENGTH )    {        pbl_errno = PBL_ERROR_BAD_FILE;        return( -1 );    }    /*     * read all the keys     */    rc = pblKfRead( isam->mainfile, data, datalen );    if( rc < 0 )    {        return( -1 );    }    else if( rc != datalen )    {        pbl_errno = PBL_ERROR_BAD_FILE;        return( -1 );    }    /*     * get the key      */    for( key = data, n = 0; n <= index; n++ )    {        okeylen = 0xff & *key++;        if( key + okeylen > data + datalen )        {            pbl_errno = PBL_ERROR_BAD_FILE;            return( -1 );        }        if( n < index )        {            /*             * move the key along the keys             */            key += okeylen;            continue;        }        memcpy( okey, key, okeylen );        /*         * if the reference of a duplicate key is requested         */        if( reference && isam->keydup[ n ] )        {            /*             * create the reference postfix for the key             */            rkeylen = pblMainKey2RKey( fkey, fkeylen, rkey );            if( rkeylen < 0 )            {                pbl_errno = PBL_ERROR_BAD_FILE;                return( -1 );            }            /*             * concatenate the key and the reference             */            if( okeylen + rkeylen > PBLKEYLENGTH )            {                pbl_errno = PBL_ERROR_BAD_FILE;                return( -1 );            }            memcpy( okey + okeylen, rkey, rkeylen );            okeylen += rkeylen;        }    }    return( okeylen );}/** * get the key and keylen of a record * * parameter which specifies which record to get relative * to the search key specified by index. * the following values for which are possible * * <BR><B> PBLTHIS  </B> - get key and keylen of current record * <BR><B> PBLNEXT  </B> - get key and keylen of next record * <BR><B> PBLPREV  </B> - get key and keylen of previous record * <BR><B> PBLFIRST </B> - get key and keylen of first record * <BR><B> PBLLAST  </B> - get key and keylen of last record * * parameter index specifies which of the keys to get, * the pseudo index value -1 can be used in order to access the file * sequentially in the order the records were inserted. * okey is not set in this case, a keylength of 0 is returned in case * the call went ok. * * <P> * <B>RESTRICTIONS</B>: * <BR> - the out parameter okey must point to a memory area that is *        big enough to hold any possible key, i.e 255 bytes * * @return int rc >= 0: * <UL> * <LI>                  call went ok, *                       the value returned is the length *                       of the key of the record found, * <LI>                  the key of the record is copied to okey, * <LI>                  the current record of the file is set to the *                       record found * </UL>                  *                        * @return int rc <  0:  * <UL>                   * <LI>                  some error occured, see pbl_errno  * </UL> */int pblIsamGet(pblIsamFile_t  * isamfile,  /** ISAM file to read in                      */int              which,     /** mode to use for read                      */int              index,     /** index of key to use for read              */unsigned char  * okey       /** buffer for result key                     */){    PBLISAMFILE_t * isam = ( PBLISAMFILE_t * ) isamfile;    char            rkey[ PBLKEYLENGTH ];    int             rkeylen = 0;    int             okeylen;    long            datalen;    /*     * make sure the index is in bounds     */    if(( index != -1 ) && ( index >= isam->nkeys ))    {        pbl_errno = PBL_ERROR_PARAM_INDEX;        return( -1 );    }    if( index >= 0 && isam->keydup[ index ] )    {        /*         * make sure any user defined key compare function gets used         */        pblkeycompare = isam->keycompare[ index ];    }    switch( which )    {      case PBLNEXT:        if( index == -1 )        {            /*             * save the current position of the main file             */             if( pblKfSavePosition( isam->mainfile ))            {                   return( -1 );            }        }        while( index == -1 )        {            /*             * read the next entry in the main file             */            datalen = pblKfNext( isam->mainfile, rkey, &rkeylen );            if( datalen < 0 )            {                pblKfRestorePosition( isam->mainfile );                return( -1 );            }            /*             * length 8 or 17 is for allkeys records             * if the length is 9 or 18, the record is a data record             */            if( rkeylen != 8 && rkeylen != 17 )            {                /*                 * found a datarecord, continue reading records                 */                continue;            }            return( 0 );        }        /*         * position to next record in the index file         */        datalen = pblKfNext( isam->keyfiles[ index ], okey, &okeylen );        if( datalen < 0 )        {            return( -1 );        }        break;      case PBLPREV:        if( index == -1 )        {            /*             * save the current position of the main file             */            if( pblKfSavePosition( isam->mainfile ))            {                return( -1 );            }        }        while( index == -1 )        {            /*             * read the previous entry in the main file             */            datalen = pblKfPrev( isam->mainfile, rkey, &rkeylen );            if( datalen < 0 )            {                pblKfRestorePosition( isam->mainfile );                return( -1 );            }            /*             * length 8 or 17 is for allkeys records             * if the length is 9 or 18, the record is a data record             */            if( rkeylen != 8 && rkeylen != 17 )            {                /*                 * found a datarecord, continue reading records                 */                continue;            }            return( 0 );        }        /*         * position to previous record in the index file         */        datalen = pblKfPrev( isam->keyfiles[ index ], okey, &okeylen );        if( datalen < 0 )        {            return( -1 );        }        break;      case PBLFIRST:        if( index == -1 )        {            datalen = pblKfFirst( isam->mainfile, 0, 0 );            if( datalen < 0 )            {                return( -1 );            }            return( 0 );        }        datalen = pblKfFirst( isam->keyfiles[ index ], okey, &okeylen );        if( datalen < 0 )        {            return( -1 );        }        break;      case PBLLAST:        if( index == -1 )        {            datalen = pblKfLast( isam->mainfile, rkey, &rkeylen );            if( datalen < 0 )            {                return( -1 );            }            if( rkeylen == 8 || rkeylen == 17 )            {                return( 0 );            }        }        while( index == -1 )        {            /*             * read the next entry in the main file             */            datalen = pblKfPrev( isam->mainfile, rkey, &rkeylen );            if( datalen < 0 )            {                return( -1 );            }            /*             * length 8 or 17 is for allkeys records             * if the length is 9 or 18, the record is a data record             */            if( rkeylen != 8 && rkeylen != 17 )            {                /*                 * found a datarecord, continue reading records                 */                continue;            }            return( 0 );        }        datalen = pblKfLast( isam->keyfiles[ index ], okey, &okeylen );        if( datalen < 0 )        {            return( -1 );        }        break;      case PBLTHIS:        if( index == -1 )        {            datalen = pblKfThis( isam->mainfile, 0, 0 );            if( datalen < 0 )            {                return( -1 );            }            return( 0 );        }        /*         * read the key with the reference         */        okeylen = pblIsamThisKey( isam, index, 1, okey );        if( okeylen <= 0 )        {            return( okeylen );        }        /*         * find the record in the key file         * position the current record in the index file         */        datalen = pblKfFind( isam->keyfiles[ index ],                             PBLEQ, okey, okeylen, 0, 0 );        if( datalen < 0 )        {            return( -1 );        }        break;      default:        pbl_errno = PBL_ERROR_PARAM_MODE;        return( -1 );    }    /*     * no need to position the current record of the main file in this case     */    if( which != PBLTHIS )    {        /*         * position the current record of the main file         */        rkeylen = pblIsamGetMainKey( isam, index, okey, okeylen, rkey );        if( rkeylen < 0 )        {            return( -1 );        }    }    /*     * if the key has duplicates     */    if( isam->keydup[ index ] )    {        /*         * calculate the length of the key without the reference postfix         */        okeylen = pblIsamDupKeyLen( okey, okeylen );        if( okeylen < 0 )        {            return( -1 );        }        okey[ okeylen ] = 0;    }    return( okeylen );}/** * read the key and keylen of the current record * * parameter index specifies which of the keys to read * * <P> * <B>RESTRICTIONS</B>: * <BR> - the out parameter okey must point to a memory area that is *        big enough to hold any possible key, i.e 255 bytes * * @return int rc >= 0: * <UL> * <LI>                  call went ok, *                       the value returned is the length of the key * <LI>                  the key of the record is copied to okey, * <LI>                  the current record of the file is not affected *                       by this function * </UL>                  * @return int rc <  0:  * <UL>                   * <LI>                  some error occured, see pbl_errno * </UL> */int pblIsamReadKey(pblIsamFile_t  * isamfile,  /** ISAM file to read in                      */int              index,     /** index of key read                         */unsigned char  * okey       /** buffer for result key                     */){    PBLISAMFILE_t * isam = ( PBLISAMFILE_t * ) isamfile;    int             okeylen;    /*     * make sure the index is in bounds     */    if( index >= isam->nkeys )    {        pbl_errno = PBL_ERROR_PARAM_INDEX;        return( -1 );    }    /*     * read the key without the reference     */    okeylen = pblIsamThisKey( isam, index, 0, okey );    return( okeylen );}/** * read the datalen of the current record * * @return long rc >= 0: call went ok, the value returned is the length *                       of the data of the record, * @return long rc <  0: some error occured, see pbl_errno */long pblIsamReadDatalen(pblIsamFile_t * isamfile   /** ISAM file to read length of data from */){    PBLISAMFILE_t * isam = ( PBLISAMFILE_t * ) isamfile;    char            rkey[ PBLKEYLENGTH ];    int             rkeylen;    char            fkey[ PBLKEY

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -