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

📄 arxclean.c

📁 roots--一个轻量级的内存数据库系统。基于Hash Map的table设计。快速轻巧。
💻 C
字号:
/*
Copyright (c) 2003, Dan Kranz and Arnold Rom
All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

    * Redistributions of source code must retain the above
      copyright notice, this list of conditions and the following
      disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials
      provided with the distribution.

    * The names of its contributors may not be used to endorse or
      promote products derived from this software without specific
      prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <roots.h>

/* ################################################################## */
/*      clean data base                                             # */
/*                                                                  # */
/*      output: ReadBase       = 0 if data base was cleaned         # */
/*                             = 1     if we had an error           # */
/*                                                                  # */
/*      input:  pointer to DataBaseStructure                        # */
/* ################################################################## */


        BYTE CleanDataBase(DataBase* db)

/* ---------------------------------------------------------------------------- */
{       /* CleanDataBase START */
/* ---------------------------------------------------------------------------- */

        long cShort[2]={1,sizeof(short)},bfi,nf,match,f,drop,NewTabNline;
        long kt,first,v[2],flds[MAXFLD][2],n1=1,newi,i,nfields=db->nfields;
        unsigned short   fnextf[MAXFLD];

        ShortArray map={NULL,0},lnextl={NULL,0};
        LongArray  rank={NULL,0},sorti={NULL,0};
		  short type[1]={T_TYPE};

        for(kt=1; kt<=db->nbtabs; ++kt)

/* --------------------------------------- */
{       /*  loop over all tables START */
/* --------------------------------------- */

        seqlst(&nfields,&first,fnextf);
        v[0]=v[1]=kt;
        rngprnArray(db->tabinx,&cShort[1],cShort,v,&first,fnextf,&match);

        if(!match) continue;            /* table is not used */

/*      find all fields */

        nf=0;
        ScanLinkedList(match,fnextf,bfi)
              {
              flds[nf][0]=  db->blkfld[bfi-1][0];
              flds[nf++][1]=db->blkfld[bfi-1][1];
              }

/*      make required space for clean operation */

        ExpandShort(map,db->arx[kt-1].nline)
        ExpandShort(lnextl,db->arx[kt-1].nline)
        ExpandShort(lnextl,db->base.nline)              /* bigger of the 2 */

        ExpandLong(rank, db->arx[kt-1].nline)
        ExpandLong(sorti,db->arx[kt-1].nline)

/* -------------------------------------------------------------------------- */
/*      perform the cleanup */
/* -------------------------------------------------------------------------- */

        if(db->base.nline==0) db->arx[kt-1].nline=0;
        if(db->arx[kt-1].nline==0) continue;            /* skip empty tables */

/*      generate table entry deletion map */

        memset(map.array,'\0',sizeof(short)*(db->arx[kt-1].nline));
        seqlst(&db->base.nline,&first,lnextl.array);

        for(f=0; f<nf; f++)
        idxmap(BLKCPL(db->base),flds[f],&first,lnextl.array,map.array);

/*      generate old-table's sorti. */

        v[0]=1; v[1]=db->arx[kt-1].cpl;
        mrsort(BLOCK(db->arx[kt-1]),sorti.array,v,type,&n1,tableCompare);

/*      delete statle table entry. i.e.map(stale)=0 */

        seqlst(&db->arx[kt-1].nline,&first,lnextl.array);
        v[0]=v[1]=0;
        rngprnArray(map.array,&cShort[1],cShort,v,&first,lnextl.array,&drop);

        NewTabNline=db->arx[kt-1].nline;
        delent(BLKCPL(db->arx[kt-1]),&NewTabNline,&drop,lnextl.array);

/*      build rank-translation. old-new : rank(sorti)=newi */

        memset(rank.array,'\0',sizeof(long)*(db->arx[kt-1].nline));

        newi=0;
        for(i=0; i<db->arx[kt-1].nline; i++)
        if(*(map.array+(*(sorti.array+i)-1)))
                            *(rank.array+(*(sorti.array+i)-1))=++newi;

/*      build sorti for new table given the new rank column */

        newi=0;
        for(i=0; i<db->arx[kt-1].nline; i++)
        if(*(rank.array+i)) *(sorti.array+(*(rank.array+i)-1))=++newi;

/*      define new table length. */

        db->arx[kt-1].nline=NewTabNline;

/*      re-arrange new table : sort */

        srmove(sorti.array,BLOCK(db->arx[kt-1]));

/*      re-set block's table refs */

        for(f=0; f<nf; f++)
        lgmap(BLOCK(db->base),flds[f],rank.array);

/* --------------------------------------- */
}       /*  loop over all tables END */
/* --------------------------------------- */

/*      release space */

        if(map.array!=NULL)    free(map.array);
        if(lnextl.array!=NULL) free(lnextl.array);
        if(rank.array!=NULL)   free(rank.array);
        if(sorti.array!=NULL)  free(sorti.array);

        return 0;

/* ---------------------------------------------------------------------------- */
}       /* CleanDataBase END */
/* ---------------------------------------------------------------------------- */

⌨️ 快捷键说明

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