home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 January / Chip_2001-01_cd1.bin / tema / mysql / mysql-3.23.28g-win-source.exe / isam / test1.c < prev    next >
C/C++ Source or Header  |  2000-08-31  |  5KB  |  184 lines

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This program is free software; you can redistribute it and/or modify
  4.    it under the terms of the GNU General Public License as published by
  5.    the Free Software Foundation; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16.  
  17. #include "isamdef.h"
  18.  
  19. static void get_options(int argc, char *argv[]);
  20.  
  21. static int rec_pointer_size=0,verbose=0,remove_ant=0,pack_keys=1,flags[50],
  22.   packed_field=FIELD_SKIPP_PRESPACE;
  23.  
  24. int main(argc,argv)
  25. int argc;
  26. char *argv[];
  27. {
  28.   N_INFO *file;
  29.   int i,j,error,deleted,found;
  30.   char record[128],key[32],*filename,read_record[128];
  31.   N_KEYDEF keyinfo[10];
  32.   N_RECINFO recinfo[10];
  33.   MY_INIT(argv[0]);
  34.  
  35.   filename= (char*) "test1";
  36.   my_init();
  37.   get_options(argc,argv);
  38.   keyinfo[0].seg[0].base.type=HA_KEYTYPE_NUM;
  39.   keyinfo[0].seg[0].base.flag=(uint8) (pack_keys ?
  40.                        HA_PACK_KEY | HA_SPACE_PACK : 0);
  41.   keyinfo[0].seg[0].base.start=0;
  42.   keyinfo[0].seg[0].base.length=6;
  43.   keyinfo[0].seg[1].base.type=HA_KEYTYPE_END;
  44.   keyinfo[0].base.flag = (uint8) (pack_keys ?
  45.                   HA_NOSAME | HA_PACK_KEY : HA_NOSAME);
  46.  
  47.   recinfo[0].base.type=packed_field; recinfo[0].base.length=6;
  48.   recinfo[1].base.type=FIELD_NORMAL; recinfo[1].base.length=24;
  49.   recinfo[2].base.type=FIELD_LAST;
  50.  
  51.   deleted=0;
  52.   bzero((byte*) flags,sizeof(flags));
  53.  
  54.   printf("- Creating isam-file\n");
  55.   if (nisam_create(filename,1,keyinfo,recinfo,
  56.         (ulong) (rec_pointer_size ? (1L << (rec_pointer_size*8))/40 :
  57.              0),10l,0,0,0L))
  58.     goto err;
  59.   if (!(file=nisam_open(filename,2,HA_OPEN_ABORT_IF_LOCKED)))
  60.     goto err;
  61.   printf("- Writing key:s\n");
  62.   strmov(record,"      ..... key"); strappend(record,30,' ');
  63.  
  64.   my_errno=0;
  65.   for (i=49 ; i>=1 ; i-=2 )
  66.   {
  67.     j=i%25 +1;
  68.     sprintf(key,"%6d",j);
  69.     bmove(record,key,6);
  70.     error=nisam_write(file,record);
  71.     flags[j]=1;
  72.     if (verbose || error)
  73.       printf("J= %2d  nisam_write: %d  errno: %d\n", j,error,my_errno);
  74.   }
  75.   if (nisam_close(file)) goto err;
  76.   printf("- Reopening file\n");
  77.   if (!(file=nisam_open(filename,2,HA_OPEN_ABORT_IF_LOCKED))) goto err;
  78.   printf("- Removing keys\n");
  79.   for (i=1 ; i<=10 ; i++)
  80.   {
  81.     if (i == remove_ant) { VOID(nisam_close(file)) ; exit(0) ; }
  82.     sprintf(key,"%6d",(j=(int) ((rand() & 32767)/32767.*25)));
  83.     my_errno=0;
  84.     if ((error = nisam_rkey(file,read_record,0,key,0,HA_READ_KEY_EXACT)))
  85.     {
  86.       if (verbose || (flags[j] == 1 ||
  87.               (error && my_errno != HA_ERR_KEY_NOT_FOUND)))
  88.     printf("key: %s  nisam_rkey:   %3d  errno: %3d\n",key,error,my_errno);
  89.     }
  90.     else
  91.     {
  92.       error=nisam_delete(file,read_record);
  93.       if (verbose || error)
  94.     printf("key: %s  nisam_delete: %3d  errno: %3d\n",key,error,my_errno);
  95.       flags[j]=0;
  96.       if (! error)
  97.     deleted++;
  98.     }
  99.   }
  100.   printf("- Reading records with key\n");
  101.   for (i=1 ; i<=25 ; i++)
  102.   {
  103.     sprintf(key,"%6d",i);
  104.     bmove(record,key,6);
  105.     my_errno=0;
  106.     error=nisam_rkey(file,read_record,0,key,0,HA_READ_KEY_EXACT);
  107.     if (verbose ||
  108.     (error == 0 && flags[i] != 1) ||
  109.     (error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND)))
  110.     {
  111.       printf("key: %s  nisam_rkey: %3d  errno: %3d  record: %s\n",
  112.           key,error,my_errno,record+1);
  113.     }
  114.   }
  115.  
  116.   printf("- Reading records with position\n");
  117.   for (i=1,found=0 ; i <= 30 ; i++)
  118.   {
  119.     my_errno=0;
  120.     if ((error=nisam_rrnd(file,read_record,i == 1 ? 0L : NI_POS_ERROR)) == -1)
  121.     {
  122.       if (found != 25-deleted)
  123.     printf("Found only %d of %d records\n",found,25-deleted);
  124.       break;
  125.     }
  126.     if (!error)
  127.       found++;
  128.     if (verbose || (error != 0 && error != 1))
  129.     {
  130.       printf("pos: %2d  nisam_rrnd: %3d  errno: %3d  record: %s\n",
  131.          i-1,error,my_errno,read_record+1);
  132.     }
  133.   }
  134.   if (nisam_close(file)) goto err;
  135.   my_end(MY_CHECK_ERROR);
  136.  
  137.   exit(0);
  138. err:
  139.   printf("got error: %3d when using nisam-database\n",my_errno);
  140.   exit(1);
  141.   return 0;            /* skipp warning */
  142. } /* main */
  143.  
  144.  
  145.     /* l{ser optioner */
  146.     /* OBS! intierar endast DEBUG - ingen debuggning h{r ! */
  147.  
  148. static void get_options(argc,argv)
  149. int argc;
  150. char *argv[];
  151. {
  152.   char *pos;
  153.  
  154.   while (--argc >0 && *(pos = *(++argv)) == '-' ) {
  155.     switch(*++pos) {
  156.     case 'R':                /* Length of record pointer */
  157.       rec_pointer_size=atoi(++pos);
  158.       if (rec_pointer_size > 3)
  159.     rec_pointer_size=0;
  160.       break;
  161.     case 'P':
  162.       pack_keys=0;            /* Don't use packed key */
  163.       break;
  164.     case 'S':
  165.       packed_field=FIELD_NORMAL;    /* static-size record*/
  166.       break;
  167.     case 'v':                /* verbose */
  168.       verbose=1;
  169.       break;
  170.     case 'm':
  171.       remove_ant=atoi(++pos);
  172.       break;
  173.     case 'V':
  174.       printf("isamtest1     Ver 1.0 \n");
  175.       exit(0);
  176.     case '#':
  177.       DEBUGGER_ON;
  178.       DBUG_PUSH (++pos);
  179.       break;
  180.     }
  181.   }
  182.   return;
  183. } /* get options */
  184.