-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgensqlstatehashfunc.c
167 lines (150 loc) · 4.09 KB
/
gensqlstatehashfunc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Perfect hash function generator for PostgreSQL SQLSTATEs.
*
* Copyright (c) 2014, Oskari Saarenmaa <[email protected]>
* All rights reserved.
*
* This file is under the Apache License, Version 2.0.
* See the file `LICENSE` for details.
*
*/
#include "postgres.h"
#include "utils/elog.h"
#undef qsort
#include <time.h>
#ifndef VERIFYFUNC
static inline unsigned int
hashm(unsigned int h1, unsigned int modulo, unsigned int c1, unsigned int c2)
{
h1 ^= h1 >> 16;
h1 *= 0x85ebca6b + c1;
h1 ^= h1 >> 13;
h1 *= 0xc2b2ae35 + c2;
h1 ^= h1 >> 16;
return h1 % modulo;
}
static int
getperfect(unsigned int *nums, unsigned int cnt, unsigned int modulo)
{
unsigned int best = cnt, c1, best_c1 = 0, best_c2 = 0, iters = 0;
time_t now;
#pragma omp parallel for reduction(+:iters)
for (c1=0; c1<10000; c1++)
{
unsigned int result[modulo], c2, i, collisions;
if (best == 0)
continue;
for (c2=0; c2<10000; c2++)
{
iters ++;
collisions = 0;
memset(result, 0xff, sizeof(result));
for (i=0; i<cnt; i++)
{
unsigned int h = hashm(nums[i], modulo, c1, c2);
if (result[h] != 0xffffffff)
collisions ++;
else
result[h] = i;
}
#pragma omp critical
{
if (collisions == 0)
{
best = 0;
best_c1 = c1;
best_c2 = c2;
}
else if (collisions < best)
best = collisions;
}
if (best == 0)
break;
}
}
fprintf(stderr, "%u iterations, modulo %u, best function had %u duplicates\n", iters, modulo, best);
if (best > 0)
return 1;
now = time(NULL);
printf(
"/* Generated by gensqlstatehashfunc.c on %s */\n"
"#define HASH_SQLSTATE_MODULO %u\n"
"static inline unsigned int\n"
"hash_sqlstate(unsigned int s)\n"
"{\n"
" s ^= s >> 16;\n"
" s *= 0x85ebca6b + %u;\n"
" s ^= s >> 13;\n"
" s *= 0xc2b2ae35 + %u;\n"
" s ^= s >> 16;\n"
" return s %% HASH_SQLSTATE_MODULO;\n"
"}\n",
ctime(&now), modulo, best_c1, best_c2);
return 0;
}
#else /* VERIFYFUNC */
#include "sqlstatehashfunc.c"
static int
verifyfunc(unsigned int *nums, unsigned int cnt)
{
unsigned int result[HASH_SQLSTATE_MODULO], collisions = 0, i;
memset(result, 0xff, sizeof(result));
for (i=0; i<cnt; i++)
{
unsigned int h = hash_sqlstate(nums[i]);
if (result[h] != 0xffffffff)
collisions ++;
else
result[h] = i;
}
fprintf(stderr, "found %u collisions\n", collisions);
return collisions ? 1 : 0;
}
#endif /* VERIFYFUNC */
static int
cmp_uints(const void *a, const void *b)
{
unsigned int p1 = *(unsigned int *) a, p2 = *(unsigned int *) b;
return (p1 > p2) ? 1 : (p2 > p1) ? -1 : 0;
}
int
main(int argc, char **argv)
{
FILE *fp;
char errcodes_h_path[1000], line[200], a, b, c, d, e;
unsigned int nums[1000], cnt = 0, uniq_nums[1000], uniq_cnt = 0, i;
unsigned int modulos[] = { 1409, 2027, 3061, 4583 };
if (argc != 2)
{
fprintf(stderr, "usage: %s `pg_config --includedir-server`\n", argv[0]);
return 1;
}
snprintf(errcodes_h_path, sizeof(errcodes_h_path), "%s/utils/errcodes.h", argv[1]);
fp = fopen(errcodes_h_path, "r");
if (fp == NULL)
{
perror(errcodes_h_path);
return 1;
}
while (fgets(line, sizeof(line), fp) != NULL)
if (sscanf(line, "#define ERRCODE_%*s MAKE_SQLSTATE('%c','%c','%c','%c','%c')",
&a, &b, &c, &d, &e) == 5)
nums[cnt++] = MAKE_SQLSTATE(a, b, c, d, e);
fclose(fp);
qsort(nums, cnt, sizeof(unsigned int), cmp_uints);
for (i=0; i<cnt; i++)
if (i == 0 || nums[i] != nums[i-1])
uniq_nums[uniq_cnt++] = nums[i];
fprintf(stderr, "input set size: %u\n", uniq_cnt);
#ifndef VERIFYFUNC
for (i=0; i<sizeof(modulos)/sizeof(modulos[0]); i++)
{
int res = getperfect(uniq_nums, uniq_cnt, modulos[i]);
if (res == 0)
return 0;
}
return 1;
#else /* VERIFYFUNC */
return verifyfunc(uniq_nums, uniq_cnt);
#endif /* VERIFYFUNC */
}