~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/heap/hp_test1.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2003, 2006 MySQL 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; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/* Test av heap-database */
 
17
/* Programmet skapar en heap-databas. Till denna skrivs ett antal poster.
 
18
   Databasen st{ngs. D{refter |ppnas den p} nytt och en del av posterna
 
19
   raderas.
 
20
*/
 
21
 
 
22
#include <my_global.h>
 
23
#include <my_sys.h>
 
24
#include <m_string.h>
 
25
#include "heap.h"
 
26
 
 
27
static int get_options(int argc, char *argv[]);
 
28
 
 
29
static int flag=0,verbose=0,remove_ant=0,flags[50];
 
30
 
 
31
int main(int argc, char **argv)
 
32
{
 
33
  int i,j,error,deleted;
 
34
  HP_INFO *file;
 
35
  uchar record[128],key[32];
 
36
  const char *filename;
 
37
  HP_KEYDEF keyinfo[10];
 
38
  HA_KEYSEG keyseg[4];
 
39
  HP_CREATE_INFO hp_create_info;
 
40
  HP_SHARE *tmp_share;
 
41
  MY_INIT(argv[0]);
 
42
 
 
43
  filename= "test1";
 
44
  get_options(argc,argv);
 
45
 
 
46
  bzero(&hp_create_info, sizeof(hp_create_info));
 
47
  hp_create_info.max_table_size= 1024L*1024L;
 
48
 
 
49
  keyinfo[0].keysegs=1;
 
50
  keyinfo[0].seg=keyseg;
 
51
  keyinfo[0].algorithm= HA_KEY_ALG_HASH;
 
52
  keyinfo[0].seg[0].type=HA_KEYTYPE_BINARY;
 
53
  keyinfo[0].seg[0].start=1;
 
54
  keyinfo[0].seg[0].length=6;
 
55
  keyinfo[0].seg[0].charset= &my_charset_latin1;
 
56
  keyinfo[0].seg[0].null_bit= 0;
 
57
  keyinfo[0].flag = HA_NOSAME;
 
58
  
 
59
  deleted=0;
 
60
  bzero((uchar*) flags,sizeof(flags));
 
61
 
 
62
  printf("- Creating heap-file\n");
 
63
  if (heap_create(filename,1,keyinfo,30,(ulong) flag*100000L,10L,
 
64
                  &hp_create_info, &tmp_share) ||
 
65
      !(file= heap_open(filename, 2)))
 
66
    goto err;
 
67
  printf("- Writing records:s\n");
 
68
  strmov((char*) record,"          ..... key           ");
 
69
 
 
70
  for (i=49 ; i>=1 ; i-=2 )
 
71
  {
 
72
    j=i%25 +1;
 
73
    sprintf((char*) key,"%6d",j);
 
74
    bmove(record+1,key,6);
 
75
    error=heap_write(file,record);
 
76
    if (heap_check_heap(file,0))
 
77
    {
 
78
      puts("Heap keys crashed");
 
79
      goto err;
 
80
    }
 
81
    flags[j]=1;
 
82
    if (verbose || error) printf("J= %2d  heap_write: %d  my_errno: %d\n",
 
83
       j,error,my_errno);
 
84
  }
 
85
  if (heap_close(file))
 
86
    goto err;
 
87
  printf("- Reopening file\n");
 
88
  if (!(file=heap_open(filename, 2)))
 
89
    goto err;
 
90
 
 
91
  printf("- Removing records\n");
 
92
  for (i=1 ; i<=10 ; i++)
 
93
  {
 
94
    if (i == remove_ant) { VOID(heap_close(file)) ; return (0) ; }
 
95
    sprintf((char*) key,"%6d",(j=(int) ((rand() & 32767)/32767.*25)));
 
96
    if ((error = heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT)))
 
97
    {
 
98
      if (verbose || (flags[j] == 1 ||
 
99
                      (error && my_errno != HA_ERR_KEY_NOT_FOUND)))
 
100
        printf("key: %s  rkey:   %3d  my_errno: %3d\n",(char*) key,error,my_errno);
 
101
    }
 
102
    else
 
103
    {
 
104
      error=heap_delete(file,record);
 
105
      if (error || verbose)
 
106
        printf("key: %s  delete: %d  my_errno: %d\n",(char*) key,error,my_errno);
 
107
      flags[j]=0;
 
108
      if (! error)
 
109
        deleted++;
 
110
    }
 
111
    if (heap_check_heap(file,0))
 
112
    {
 
113
      puts("Heap keys crashed");
 
114
      goto err;
 
115
    }
 
116
  }
 
117
 
 
118
  printf("- Reading records with key\n");
 
119
  for (i=1 ; i<=25 ; i++)
 
120
  {
 
121
    sprintf((char*) key,"%6d",i);
 
122
    bmove(record+1,key,6);
 
123
    my_errno=0;
 
124
    error=heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT);
 
125
    if (verbose ||
 
126
        (error == 0 && flags[i] != 1) ||
 
127
        (error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND)))
 
128
    {
 
129
      printf("key: %s  rkey: %3d  my_errno: %3d  record: %s\n",
 
130
             (char*) key,error,my_errno,record+1);
 
131
    }
 
132
  }
 
133
 
 
134
#ifdef OLD_HEAP_VERSION
 
135
  {
 
136
    int found;
 
137
    printf("- Reading records with position\n");
 
138
    for (i=1,found=0 ; i <= 30 ; i++)
 
139
    {
 
140
      my_errno=0;
 
141
      if ((error=heap_rrnd(file,record,i == 1 ? 0L : (ulong) -1)) ==
 
142
          HA_ERR_END_OF_FILE)
 
143
      {
 
144
        if (found != 25-deleted)
 
145
          printf("Found only %d of %d records\n",found,25-deleted);
 
146
        break;
 
147
      }
 
148
      if (!error)
 
149
        found++;
 
150
      if (verbose || (error != 0 && error != HA_ERR_RECORD_DELETED))
 
151
      {
 
152
        printf("pos: %2d  ni_rrnd: %3d  my_errno: %3d  record: %s\n",
 
153
               i-1,error,my_errno,(char*) record+1);
 
154
      }
 
155
    }
 
156
  }
 
157
#endif
 
158
 
 
159
  if (heap_close(file) || hp_panic(HA_PANIC_CLOSE))
 
160
    goto err;
 
161
  my_end(MY_GIVE_INFO);
 
162
  return(0);
 
163
err:
 
164
  printf("got error: %d when using heap-database\n",my_errno);
 
165
  return(1);
 
166
} /* main */
 
167
 
 
168
 
 
169
/* Read options */
 
170
 
 
171
static int get_options(int argc, char **argv)
 
172
{
 
173
  char *pos;
 
174
 
 
175
  while (--argc >0 && *(pos = *(++argv)) == '-' ) {
 
176
    switch(*++pos) {
 
177
    case 'B':                           /* Big file */
 
178
      flag=1;
 
179
      break;
 
180
    case 'v':                           /* verbose */
 
181
      verbose=1;
 
182
      break;
 
183
    case 'm':
 
184
      remove_ant=atoi(++pos);
 
185
      break;
 
186
    case 'V':
 
187
      printf("hp_test1    Ver 3.0 \n");
 
188
      exit(0);
 
189
    case '#':
 
190
      DBUG_PUSH (++pos);
 
191
      break;
 
192
    }
 
193
  }
 
194
  return 0;
 
195
} /* get options */