~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to sql/gen_lex_hash.cc

  • 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-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
/**
 
17
  @file
 
18
 
 
19
  @details
 
20
@verbatim
 
21
The idea of presented algorithm see in 
 
22
"The Art of Computer Programming" by Donald E. Knuth
 
23
Volume 3 "Sorting and searching"
 
24
(chapter 6.3 "Digital searching" - name and number of chapter 
 
25
   is back translation from Russian edition :))
 
26
 
 
27
as illustration of data structures, imagine next table:
 
28
 
 
29
static SYMBOL symbols[] = {
 
30
  { "ADD",              SYM(ADD),0,0},
 
31
  { "AND",              SYM(AND),0,0},
 
32
  { "DAY",              SYM(DAY_SYM),0,0},
 
33
};
 
34
 
 
35
for this structure, presented program generate next searching-structure:
 
36
 
 
37
+-----------+-+-+-+
 
38
|       len |1|2|3|
 
39
+-----------+-+-+-+
 
40
|first_char |0|0|a|
 
41
|last_char  |0|0|d|
 
42
|link       |0|0|+|
 
43
                 |
 
44
                 V
 
45
       +----------+-+-+-+--+
 
46
       |    1 char|a|b|c|d |
 
47
       +----------+-+-+-+--+
 
48
       |first_char|b|0|0|0 |
 
49
       |last_char |n|0|0|-1|
 
50
       |link      |+|0|0|+ |
 
51
                   |     |
 
52
                   |     V
 
53
                   |  symbols[2] ( "DAY" )
 
54
                   V
 
55
+----------+--+-+-+-+-+-+-+-+-+-+--+
 
56
|    2 char|d |e|f|j|h|i|j|k|l|m|n |
 
57
+----------+--+-+-+-+-+-+-+-+-+-+--+
 
58
|first_char|0 |0|0|0|0|0|0|0|0|0|0 |
 
59
|last_char |-1|0|0|0|0|0|0|0|0|0|-1|
 
60
|link      |+ |0|0|0|0|0|0|0|0|0|+ |
 
61
            |                    |
 
62
            V                    V
 
63
         symbols[0] ( "ADD" )  symbols[1] ( "AND" )
 
64
 
 
65
for optimization, link is the 16-bit index in 'symbols' or 'sql_functions'
 
66
or search-array..
 
67
 
 
68
So, we can read full search-structure as 32-bit word
 
69
@endverbatim
 
70
 
 
71
@todo
 
72
    use instead to_upper_lex, special array 
 
73
    (substitute chars) without skip codes..
 
74
@todo
 
75
    try use reverse order of comparing..
 
76
 
 
77
*/
 
78
 
 
79
#define NO_YACC_SYMBOLS
 
80
#include "my_global.h"
 
81
#include "my_sys.h"
 
82
#include "m_string.h"
 
83
#ifndef __GNU_LIBRARY__
 
84
#define __GNU_LIBRARY__                         // Skip warnings in getopt.h
 
85
#endif
 
86
#include <my_getopt.h>
 
87
#include "mysql_version.h"
 
88
#include "lex.h"
 
89
 
 
90
const char *default_dbug_option="d:t:o,/tmp/gen_lex_hash.trace";
 
91
 
 
92
struct my_option my_long_options[] =
 
93
{
 
94
#ifdef DBUG_OFF
 
95
  {"debug", '#', "This is a non-debug version. Catch this and exit",
 
96
   0,0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
97
#else
 
98
  {"debug", '#', "Output debug log", (uchar**) &default_dbug_option,
 
99
   (uchar**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
100
#endif
 
101
  {"help", '?', "Display help and exit",
 
102
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
103
  {"version", 'V', "Output version information and exit",
 
104
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
105
  {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
106
};
 
107
 
 
108
struct hash_lex_struct
 
109
{
 
110
  int first_char;
 
111
  char last_char;
 
112
  union{
 
113
    hash_lex_struct *char_tails;
 
114
    int iresult;
 
115
  };
 
116
  int ithis;
 
117
};
 
118
 
 
119
hash_lex_struct *get_hash_struct_by_len(hash_lex_struct **root_by_len,
 
120
                                            int len, int *max_len)
 
121
{
 
122
  if (*max_len<len){
 
123
    *root_by_len= (hash_lex_struct *)realloc((char*)*root_by_len,
 
124
                                             sizeof(hash_lex_struct)*len);
 
125
    hash_lex_struct *cur, *end= *root_by_len + len;
 
126
    for (cur= *root_by_len + *max_len; cur<end; cur++)
 
127
      cur->first_char= 0;
 
128
    *max_len= len;
 
129
  }
 
130
  return (*root_by_len)+(len-1);
 
131
}
 
132
 
 
133
void insert_into_hash(hash_lex_struct *root, const char *name, 
 
134
                      int len_from_begin, int index, int function)
 
135
{
 
136
  hash_lex_struct *end, *cur, *tails;
 
137
 
 
138
  if (!root->first_char)
 
139
  {
 
140
    root->first_char= -1;
 
141
    root->iresult= index;
 
142
    return;
 
143
  }
 
144
 
 
145
  if (root->first_char == -1)
 
146
  {
 
147
    int index2= root->iresult;
 
148
    const char *name2= (index2 < 0 ? sql_functions[-index2-1] :
 
149
                        symbols[index2]).name + len_from_begin;
 
150
    root->first_char= (int) (uchar) name2[0];
 
151
    root->last_char= (char) root->first_char;
 
152
    tails= (hash_lex_struct*)malloc(sizeof(hash_lex_struct));
 
153
    root->char_tails= tails;
 
154
    tails->first_char= -1;
 
155
    tails->iresult= index2;
 
156
  }
 
157
 
 
158
  size_t real_size= (root->last_char-root->first_char+1);
 
159
 
 
160
  if (root->first_char>(*name))
 
161
  {
 
162
    size_t new_size= root->last_char-(*name)+1;
 
163
    if (new_size<real_size) printf("error!!!!\n");
 
164
    tails= root->char_tails;
 
165
    tails= (hash_lex_struct*)realloc((char*)tails,
 
166
                                       sizeof(hash_lex_struct)*new_size);
 
167
    root->char_tails= tails;
 
168
    memmove(tails+(new_size-real_size),tails,real_size*sizeof(hash_lex_struct));
 
169
    end= tails + new_size - real_size;
 
170
    for (cur= tails; cur<end; cur++)
 
171
      cur->first_char= 0;
 
172
    root->first_char= (int) (uchar) *name;
 
173
  }
 
174
 
 
175
  if (root->last_char<(*name))
 
176
  {
 
177
    size_t new_size= (*name)-root->first_char+1;
 
178
    if (new_size<real_size) printf("error!!!!\n");
 
179
    tails= root->char_tails;
 
180
    tails= (hash_lex_struct*)realloc((char*)tails,
 
181
                                    sizeof(hash_lex_struct)*new_size);
 
182
    root->char_tails= tails;
 
183
    end= tails + new_size;
 
184
    for (cur= tails+real_size; cur<end; cur++)
 
185
      cur->first_char= 0;
 
186
    root->last_char= (*name);
 
187
  }
 
188
 
 
189
  insert_into_hash(root->char_tails+(*name)-root->first_char,
 
190
                   name+1,len_from_begin+1,index,function);
 
191
}
 
192
 
 
193
 
 
194
hash_lex_struct *root_by_len= 0;
 
195
int max_len=0;
 
196
 
 
197
hash_lex_struct *root_by_len2= 0;
 
198
int max_len2=0;
 
199
 
 
200
void insert_symbols()
 
201
{
 
202
  size_t i= 0;
 
203
  SYMBOL *cur;
 
204
  for (cur= symbols; i<array_elements(symbols); cur++, i++){
 
205
    hash_lex_struct *root= 
 
206
      get_hash_struct_by_len(&root_by_len,cur->length,&max_len);
 
207
    insert_into_hash(root,cur->name,0,i,0);
 
208
  }
 
209
}
 
210
 
 
211
void insert_sql_functions()
 
212
{
 
213
  int i= 0;
 
214
  SYMBOL *cur;
 
215
  for (cur= sql_functions; i < (int) array_elements(sql_functions); cur++, i++)
 
216
  {
 
217
    hash_lex_struct *root= 
 
218
      get_hash_struct_by_len(&root_by_len,cur->length,&max_len);
 
219
    insert_into_hash(root,cur->name,0,-i-1,1);
 
220
  }
 
221
}
 
222
 
 
223
void calc_length()
 
224
{
 
225
  SYMBOL *cur, *end= symbols + array_elements(symbols);
 
226
  for (cur= symbols; cur < end; cur++)
 
227
    cur->length=(uchar) strlen(cur->name);
 
228
  end= sql_functions + array_elements(sql_functions);
 
229
  for (cur= sql_functions; cur<end; cur++)
 
230
    cur->length=(uchar) strlen(cur->name);
 
231
}
 
232
 
 
233
void generate_find_structs()
 
234
{
 
235
  root_by_len= 0;
 
236
  max_len=0;
 
237
 
 
238
  insert_symbols();
 
239
 
 
240
  root_by_len2= root_by_len;
 
241
  max_len2= max_len;
 
242
 
 
243
  root_by_len= 0;
 
244
  max_len= 0;
 
245
 
 
246
  insert_symbols();
 
247
  insert_sql_functions();
 
248
}
 
249
 
 
250
char *hash_map= 0;
 
251
int size_hash_map= 0;
 
252
 
 
253
void add_struct_to_map(hash_lex_struct *st)
 
254
{
 
255
  st->ithis= size_hash_map/4;
 
256
  size_hash_map+= 4;
 
257
  hash_map= (char*)realloc((char*)hash_map,size_hash_map);
 
258
  hash_map[size_hash_map-4]= (char) (st->first_char == -1 ? 0 :
 
259
                                     st->first_char);
 
260
  hash_map[size_hash_map-3]= (char) (st->first_char == -1 ||
 
261
                                     st->first_char == 0 ? 0 : st->last_char);
 
262
  if (st->first_char == -1)
 
263
  {
 
264
    hash_map[size_hash_map-2]= ((unsigned int)(int16)st->iresult)&255;
 
265
    hash_map[size_hash_map-1]= ((unsigned int)(int16)st->iresult)>>8;
 
266
  }
 
267
  else if (st->first_char == 0)
 
268
  {
 
269
    hash_map[size_hash_map-2]= ((unsigned int)(int16)array_elements(symbols))&255;
 
270
    hash_map[size_hash_map-1]= ((unsigned int)(int16)array_elements(symbols))>>8;
 
271
  }
 
272
}
 
273
 
 
274
 
 
275
void add_structs_to_map(hash_lex_struct *st, int len)
 
276
{
 
277
  hash_lex_struct *cur, *end= st+len;
 
278
  for (cur= st; cur<end; cur++)
 
279
    add_struct_to_map(cur);
 
280
  for (cur= st; cur<end; cur++)
 
281
  {
 
282
    if (cur->first_char && cur->first_char != -1)
 
283
      add_structs_to_map(cur->char_tails,cur->last_char-cur->first_char+1);
 
284
  }
 
285
}
 
286
 
 
287
void set_links(hash_lex_struct *st, int len)
 
288
{
 
289
  hash_lex_struct *cur, *end= st+len;
 
290
  for (cur= st; cur<end; cur++)
 
291
  {
 
292
    if (cur->first_char != 0 && cur->first_char != -1)
 
293
    {
 
294
      int ilink= cur->char_tails->ithis;
 
295
      hash_map[cur->ithis*4+2]= ilink%256;
 
296
      hash_map[cur->ithis*4+3]= ilink/256;
 
297
      set_links(cur->char_tails,cur->last_char-cur->first_char+1);
 
298
    }
 
299
  }
 
300
}
 
301
 
 
302
 
 
303
void print_hash_map(const char *name)
 
304
{
 
305
  char *cur;
 
306
  int i;
 
307
 
 
308
  printf("static uchar %s[%d]= {\n",name,size_hash_map);
 
309
  for (i=0, cur= hash_map; i<size_hash_map; i++, cur++)
 
310
  {
 
311
    switch(i%4){
 
312
    case 0: case 1:
 
313
      if (!*cur)
 
314
        printf("0,   ");
 
315
      else
 
316
        printf("\'%c\', ",*cur);
 
317
      break;
 
318
    case 2: printf("%u, ",(uint)(uchar)*cur); break;
 
319
    case 3: printf("%u,\n",(uint)(uchar)*cur); break;
 
320
    }
 
321
  }
 
322
  printf("};\n");
 
323
}
 
324
 
 
325
 
 
326
void print_find_structs()
 
327
{
 
328
  add_structs_to_map(root_by_len,max_len);
 
329
  set_links(root_by_len,max_len);
 
330
  print_hash_map("sql_functions_map");
 
331
 
 
332
  hash_map= 0;
 
333
  size_hash_map= 0;
 
334
 
 
335
  printf("\n");
 
336
 
 
337
  add_structs_to_map(root_by_len2,max_len2);
 
338
  set_links(root_by_len2,max_len2);
 
339
  print_hash_map("symbols_map");
 
340
}
 
341
 
 
342
 
 
343
static void usage(int version)
 
344
{
 
345
  printf("%s  Ver 3.6 Distrib %s, for %s (%s)\n",
 
346
         my_progname, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
 
347
  if (version)
 
348
    return;
 
349
  puts("Copyright (C) 2001 MySQL AB, by VVA and Monty");
 
350
  puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
 
351
and you are welcome to modify and redistribute it under the GPL license\n");
 
352
  puts("This program generates a perfect hashing function for the sql_lex.cc");
 
353
  printf("Usage: %s [OPTIONS]\n\n", my_progname);
 
354
  my_print_help(my_long_options);
 
355
}
 
356
 
 
357
 
 
358
extern "C" my_bool
 
359
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
360
               char *argument __attribute__((unused)))
 
361
{
 
362
  switch(optid) {
 
363
  case 'V':
 
364
    usage(1);
 
365
    exit(0);
 
366
  case 'I':
 
367
  case '?':
 
368
    usage(0);
 
369
    exit(0);
 
370
  case '#':
 
371
    DBUG_PUSH(argument ? argument : default_dbug_option);
 
372
    break;
 
373
  }
 
374
  return 0;
 
375
}
 
376
 
 
377
 
 
378
static int get_options(int argc, char **argv)
 
379
{
 
380
  int ho_error;
 
381
 
 
382
  if ((ho_error= handle_options(&argc, &argv, my_long_options, get_one_option)))
 
383
    exit(ho_error);
 
384
 
 
385
  if (argc >= 1)
 
386
  {
 
387
    usage(0);
 
388
     exit(1);
 
389
  }
 
390
  return(0);
 
391
}
 
392
 
 
393
 
 
394
int check_dup_symbols(SYMBOL *s1, SYMBOL *s2)
 
395
{
 
396
  if (s1->length!=s2->length || strncmp(s1->name,s2->name,s1->length))
 
397
    return 0;
 
398
 
 
399
  const char *err_tmpl= "\ngen_lex_hash fatal error : \
 
400
Unfortunately gen_lex_hash can not generate a hash,\n since \
 
401
your lex.h has duplicate definition for a symbol \"%s\"\n\n";
 
402
  printf (err_tmpl,s1->name);
 
403
  fprintf (stderr,err_tmpl,s1->name);
 
404
 
 
405
  return 1;
 
406
}
 
407
 
 
408
 
 
409
int check_duplicates()
 
410
{
 
411
  SYMBOL *cur1, *cur2, *s_end, *f_end;
 
412
 
 
413
  s_end= symbols + array_elements(symbols);
 
414
  f_end= sql_functions + array_elements(sql_functions);
 
415
 
 
416
  for (cur1= symbols; cur1<s_end; cur1++)
 
417
  {
 
418
    for (cur2= cur1+1; cur2<s_end; cur2++)
 
419
    {
 
420
      if (check_dup_symbols(cur1,cur2))
 
421
        return 1;
 
422
    }
 
423
    for (cur2= sql_functions; cur2<f_end; cur2++)
 
424
    {
 
425
      if (check_dup_symbols(cur1,cur2))
 
426
        return 1;
 
427
    }
 
428
  }
 
429
 
 
430
  for (cur1= sql_functions; cur1<f_end; cur1++)
 
431
  {
 
432
    for (cur2= cur1+1; cur2< f_end; cur2++)
 
433
    {
 
434
      if (check_dup_symbols(cur1,cur2))
 
435
        return 1;
 
436
    }
 
437
  }
 
438
  return 0;
 
439
}
 
440
 
 
441
 
 
442
int main(int argc,char **argv)
 
443
{
 
444
  MY_INIT(argv[0]);
 
445
  DBUG_PROCESS(argv[0]);
 
446
 
 
447
  if (get_options(argc,(char **) argv))
 
448
    exit(1);
 
449
 
 
450
  /* Broken up to indicate that it's not advice to you, gentle reader. */
 
451
  printf("/*\n\n  Do " "not " "edit " "this " "file " "directly!\n\n*/\n");
 
452
 
 
453
  printf("\
 
454
/* Copyright (C) 2001-2004 MySQL AB\n\
 
455
\n\
 
456
   This program is free software; you can redistribute it and/or modify\n\
 
457
   it under the terms of the GNU General Public License as published by\n\
 
458
   the Free Software Foundation; version 2 of the License.\n\
 
459
\n\
 
460
   This program is distributed in the hope that it will be useful,\n\
 
461
   but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
 
462
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
 
463
   GNU General Public License for more details.\n\
 
464
\n\
 
465
   You should have received a copy of the GNU General Public License\n\
 
466
   along with this program; see the file COPYING. If not, write to the\n\
 
467
   Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston\n\
 
468
   MA  02110-1301  USA. */\n\
 
469
\n\
 
470
");
 
471
 
 
472
  /* Broken up to indicate that it's not advice to you, gentle reader. */
 
473
  printf("/* Do " "not " "edit " "this " "file!  This is generated by "
 
474
         "gen_lex_hash.cc\nthat seeks for a perfect hash function */\n\n");
 
475
  printf("#include \"lex.h\"\n\n");
 
476
 
 
477
  calc_length();
 
478
 
 
479
  if (check_duplicates())
 
480
    exit(1);
 
481
 
 
482
  generate_find_structs();
 
483
  print_find_structs();
 
484
 
 
485
  printf("\nstatic unsigned int sql_functions_max_len=%d;\n", max_len);
 
486
  printf("\nstatic unsigned int symbols_max_len=%d;\n\n", max_len2);
 
487
 
 
488
  printf("\
 
489
static SYMBOL *get_hash_symbol(const char *s,\n\
 
490
                               unsigned int len,bool function)\n\
 
491
{\n\
 
492
  register uchar *hash_map;\n\
 
493
  register const char *cur_str= s;\n\
 
494
\n\
 
495
  if (len == 0) {\n\
 
496
    DBUG_PRINT(\"warning\", (\"get_hash_symbol() received a request for a zero-length symbol, which is probably a mistake.\"));\
 
497
    return(NULL);\n\
 
498
  }\n"
 
499
);
 
500
 
 
501
  printf("\
 
502
  if (function){\n\
 
503
    if (len>sql_functions_max_len) return 0;\n\
 
504
    hash_map= sql_functions_map;\n\
 
505
    register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\
 
506
\n\
 
507
    for (;;){\n\
 
508
      register uchar first_char= (uchar)cur_struct;\n\
 
509
\n\
 
510
      if (first_char == 0)\n\
 
511
      {\n\
 
512
        register int16 ires= (int16)(cur_struct>>16);\n\
 
513
        if (ires==array_elements(symbols)) return 0;\n\
 
514
        register SYMBOL *res;\n\
 
515
        if (ires>=0) \n\
 
516
          res= symbols+ires;\n\
 
517
        else\n\
 
518
          res= sql_functions-ires-1;\n\
 
519
        register uint count= cur_str-s;\n\
 
520
        return lex_casecmp(cur_str,res->name+count,len-count) ? 0 : res;\n\
 
521
      }\n\
 
522
\n\
 
523
      register uchar cur_char= (uchar)to_upper_lex[(uchar)*cur_str];\n\
 
524
      if (cur_char<first_char) return 0;\n\
 
525
      cur_struct>>=8;\n\
 
526
      if (cur_char>(uchar)cur_struct) return 0;\n\
 
527
\n\
 
528
      cur_struct>>=8;\n\
 
529
      cur_struct= uint4korr(hash_map+\n\
 
530
                        (((uint16)cur_struct + cur_char - first_char)*4));\n\
 
531
      cur_str++;\n\
 
532
    }\n"
 
533
);
 
534
 
 
535
  printf("\
 
536
  }else{\n\
 
537
    if (len>symbols_max_len) return 0;\n\
 
538
    hash_map= symbols_map;\n\
 
539
    register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\
 
540
\n\
 
541
    for (;;){\n\
 
542
      register uchar first_char= (uchar)cur_struct;\n\
 
543
\n\
 
544
      if (first_char==0){\n\
 
545
        register int16 ires= (int16)(cur_struct>>16);\n\
 
546
        if (ires==array_elements(symbols)) return 0;\n\
 
547
        register SYMBOL *res= symbols+ires;\n\
 
548
        register uint count= cur_str-s;\n\
 
549
        return lex_casecmp(cur_str,res->name+count,len-count)!=0 ? 0 : res;\n\
 
550
      }\n\
 
551
\n\
 
552
      register uchar cur_char= (uchar)to_upper_lex[(uchar)*cur_str];\n\
 
553
      if (cur_char<first_char) return 0;\n\
 
554
      cur_struct>>=8;\n\
 
555
      if (cur_char>(uchar)cur_struct) return 0;\n\
 
556
\n\
 
557
      cur_struct>>=8;\n\
 
558
      cur_struct= uint4korr(hash_map+\n\
 
559
                        (((uint16)cur_struct + cur_char - first_char)*4));\n\
 
560
      cur_str++;\n\
 
561
    }\n\
 
562
  }\n\
 
563
}\n"
 
564
);
 
565
  my_end(0);
 
566
  exit(0);
 
567
}
 
568