forked from teragrep/blf_02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib_mysqludf_bloom.c
163 lines (135 loc) · 3.84 KB
/
lib_mysqludf_bloom.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
/*
* Copyright 2012, XENEI.com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
#define DLLEXP __declspec(dllexport)
#else
#define DLLEXP
#endif
#include <string.h>
#include <mysql.h>
#include <ctype.h>
#include "config.h"
#include <stdio.h>
/* For Windows, define PACKAGE_STRING in the VS project */
#ifndef __WIN__
#include "config.h"
#endif
#ifndef my_bool
typedef char my_bool;
#endif
/* These must be right or mysqld will not find the symbol! */
#ifdef __cplusplus
extern "C" {
#endif
DLLEXP my_bool bloommatch_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP void bloommatch_deinit(UDF_INIT *initid);
DLLEXP my_bool bloommatch(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
DLLEXP my_bool bloomupdate_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP void bloomupdate_deinit(UDF_INIT *initid);
DLLEXP char* bloomupdate(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
#ifdef __cplusplus
}
#endif
my_bool bloommatch_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
/* make sure user has provided exactly two string arguments */
if (args->arg_count != 2 || (args->arg_type[0] != STRING_RESULT)
|| (args->arg_type[1] != STRING_RESULT)){
strcpy(message, "bloommatch requires 2 blob or string arguments");
return 1;
}
return 0;
}
void bloommatch_deinit(UDF_INIT *initid)
{
}
my_bool bloommatch(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error)
{
if (args->lengths[0] > args->lengths[1])
{
return 0;
}
char* b1=args->args[0];
char* b2=args->args[1];
int limit_a = args->lengths[0];
int limit_b = args->lengths[1];
if (limit_a != limit_b)
{
return 0;
}
unsigned char a;
unsigned char b;
int i;
for (i=0;i<limit_a;i++)
{
a = (unsigned char) b1[i];
b = (unsigned char) b2[i];
if ((a & b) != a)
{
return 0;
}
}
return 1;
}
my_bool bloomupdate_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
/* make sure user has provided exactly two string arguments */
if (args->arg_count != 2 || (args->arg_type[0] != STRING_RESULT)
|| (args->arg_type[1] != STRING_RESULT)){
strcpy(message, "bloomupdate requires 2 blob or string arguments");
return 1;
}
args->maybe_null[0] = 1;
args->maybe_null[1] = 1;
initid->ptr = malloc( initid->max_length);
if (initid->ptr == 0)
{
strcpy(message, "bloomupdate not enough memory for buffer");
return 1;
}
return 0;
}
void bloomupdate_deinit(UDF_INIT *initid)
{
if (initid->ptr != 0)
{
free( initid->ptr);
}
}
char* bloomupdate(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error)
{
char* a=args->args[0];
char* b=args->args[1];
int alimit = a==NULL?0:args->lengths[0];
int blimit = b==NULL?0:args->lengths[1];
int limit = alimit>blimit ? alimit : blimit;
int i;
for (i=0;i<limit;i++)
{
if (alimit <= i)
{
initid->ptr[i] = b[i];
} else {
if (blimit <= i)
{
initid->ptr[i] = a[i];
} else {
initid->ptr[i] = a[i] | b[i] ;
}
}
}
*length = limit;
return initid->ptr;
}