-
Notifications
You must be signed in to change notification settings - Fork 27
/
smlar_guc.c
229 lines (192 loc) · 3.69 KB
/
smlar_guc.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "smlar.h"
#include "fmgr.h"
#include "utils/guc.h"
#include "smlar.h"
/*
* Smlar limit
*/
static double smlar_limit = 0.6;
/*
* Smlar table stores table-wide statistic
*/
static char * smlar_table = "";
static bool smlar_use_malloc = false;
/*
* State of GUC initialization
*/
static bool smlar_guc_inited = false;
static void
SmlarTableAssign(const char *newval, void *extra)
{
resetStatCache();
}
static bool smlar_logadd = false;
static void
SmlarLogAssign(bool newval, void *extra)
{
resetStatCache();
}
static int smlar_smltype = ST_COSINE;
static const struct config_enum_entry SmlarTypeOptions[] = {
{"cosine", ST_COSINE, false},
{"tfidf", ST_TFIDF, false},
{"overlap", ST_OVERLAP, false},
{NULL, 0, false}
};
static int smlar_tf_method = TF_N;
static const struct config_enum_entry SmlarTFOptions[] = {
{"n", TF_N, false},
{"log", TF_LOG, false},
{"const", TF_CONST, false},
{NULL, 0, false}
};
static void
initSmlarGUC()
{
if (smlar_guc_inited)
return;
DefineCustomRealVariable(
"smlar.threshold",
"Lower threshold of array's similarity",
"Array's with similarity lower than threshold are not similar by % operation",
&smlar_limit,
0.6,
0.0,
1e10,
PGC_USERSET,
0,
NULL,
NULL,
NULL
);
DefineCustomStringVariable(
"smlar.stattable",
"Name of table stored set-wide statistic",
"Named table stores global frequencies of array's elements",
&smlar_table,
"",
PGC_USERSET,
GUC_IS_NAME,
NULL,
SmlarTableAssign,
NULL
);
DefineCustomEnumVariable(
"smlar.type",
"Type of similarity formula",
"Type of similarity formula: cosine(default), tfidf, overlap",
&smlar_smltype,
smlar_smltype,
SmlarTypeOptions,
PGC_SUSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable(
"smlar.persistent_cache",
"Usage of persistent cache of global stat",
"Cache of global stat is stored in transaction-independent memory",
&smlar_use_malloc,
false,
PGC_USERSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable(
"smlar.idf_plus_one",
"Calculate idf by log(1+d/df)",
"Calculate idf by log(1+d/df)",
&smlar_logadd,
false,
PGC_USERSET,
0,
NULL,
SmlarLogAssign,
NULL
);
DefineCustomEnumVariable(
"smlar.tf_method",
"Method of TF caclulation",
"TF method: n => number of entries, log => 1+log(n), const => constant value",
&smlar_tf_method,
smlar_tf_method,
SmlarTFOptions,
PGC_SUSET,
0,
NULL,
NULL,
NULL
);
smlar_guc_inited = true;
}
double
getOneAdd(void)
{
if (!smlar_guc_inited)
initSmlarGUC();
return (smlar_logadd) ? 1.0 : 0.0;
}
int
getTFMethod(void)
{
if (!smlar_guc_inited)
initSmlarGUC();
return smlar_tf_method;
}
double
GetSmlarLimit(void)
{
if (!smlar_guc_inited)
initSmlarGUC();
return smlar_limit;
}
const char*
GetSmlarTable(void)
{
if (!smlar_guc_inited)
initSmlarGUC();
return smlar_table;
}
int
getSmlType(void)
{
if (!smlar_guc_inited)
initSmlarGUC();
return smlar_smltype;
}
bool
GetSmlarUsePersistent(void)
{
if (!smlar_guc_inited)
initSmlarGUC();
return smlar_use_malloc;
}
PG_FUNCTION_INFO_V1(set_smlar_limit);
Datum set_smlar_limit(PG_FUNCTION_ARGS);
Datum
set_smlar_limit(PG_FUNCTION_ARGS)
{
float4 nlimit = PG_GETARG_FLOAT4(0);
char buf[32];
/* init smlar guc */
initSmlarGUC();
sprintf(buf,"%f", nlimit);
set_config_option("smlar.threshold", buf,
PGC_USERSET, PGC_S_SESSION ,GUC_ACTION_SET, true, 0
#if PG_VERSION_NUM >= 90500
,false
#endif
);
PG_RETURN_FLOAT4((float4)GetSmlarLimit());
}
PG_FUNCTION_INFO_V1(show_smlar_limit);
Datum show_smlar_limit(PG_FUNCTION_ARGS);
Datum
show_smlar_limit(PG_FUNCTION_ARGS)
{
PG_RETURN_FLOAT4((float4)GetSmlarLimit());
}