d1cae326d9c16d3d6af42b5073b15cd8205718a3
[smlar.git] / smlar_guc.c
1 #include "smlar.h"
2
3 #include "fmgr.h"
4 #include "utils/guc.h"
5
6 #include "smlar.h"
7
8 /*
9  *  Smlar limit
10  */
11 static double smlar_limit = 0.6;
12
13 /*
14  * Smlar table stores table-wide statistic
15  */
16 static char * smlar_table = "";
17
18 static bool smlar_use_malloc = false;
19
20 /*
21  * State of GUC initialization
22  */
23 static bool smlar_guc_inited = false;
24
25 static void
26 SmlarTableAssign(const char *newval, void *extra)
27 {
28         resetStatCache();
29 }
30
31 static bool smlar_logadd = false;
32
33 static void
34 SmlarLogAssign(bool newval, void *extra)
35 {
36         resetStatCache();
37 }
38
39 static int smlar_smltype = ST_COSINE;
40
41 static const struct config_enum_entry SmlarTypeOptions[] = {
42     {"cosine", ST_COSINE, false},
43         {"tfidf", ST_TFIDF, false},
44         {"overlap", ST_OVERLAP, false},
45         {NULL, 0, false}
46 };
47
48 static int      smlar_tf_method = TF_N;
49 static const struct config_enum_entry SmlarTFOptions[] = {
50     {"n", TF_N, false},
51         {"log", TF_LOG, false},
52         {"const", TF_CONST, false},
53         {NULL, 0, false}
54 };
55
56 static void
57 initSmlarGUC()
58 {
59         if (smlar_guc_inited)
60                 return;
61
62         DefineCustomRealVariable(
63                         "smlar.threshold",
64                         "Lower threshold of array's similarity",
65                         "Array's with similarity lower than threshold are not similar by % operation",
66                         &smlar_limit,
67                         0.6, 
68                         0.0,
69                         1e10,
70                         PGC_USERSET,
71                         0,
72                         NULL,
73                         NULL,
74                         NULL
75         );
76
77         DefineCustomStringVariable(
78                         "smlar.stattable",
79                         "Name of table stored set-wide statistic",
80                         "Named table stores global frequencies of array's elements",
81                         &smlar_table,
82                         "",
83                         PGC_USERSET,
84                         GUC_IS_NAME,
85                         NULL,
86                         SmlarTableAssign,
87                         NULL
88         );
89
90         DefineCustomEnumVariable(
91                         "smlar.type",
92                         "Type of similarity formula",
93                         "Type of similarity formula: cosine(default), tfidf, overlap",
94                         &smlar_smltype,
95                         smlar_smltype,
96                         SmlarTypeOptions,
97                         PGC_SUSET,
98                         0,
99                         NULL,
100                         NULL,
101                         NULL
102         );
103
104         DefineCustomBoolVariable(
105                         "smlar.persistent_cache",
106                         "Usage of persistent cache of global stat",
107                         "Cache of global stat is stored in transaction-independent memory",
108                         &smlar_use_malloc,
109                         false,
110                         PGC_USERSET,
111                         0,
112                         NULL,
113                         NULL,
114                         NULL
115         );
116
117         DefineCustomBoolVariable(
118                         "smlar.idf_plus_one",
119                         "Calculate idf by log(1+d/df)",
120                         "Calculate idf by log(1+d/df)",
121                         &smlar_logadd,
122                         false,
123                         PGC_USERSET,
124                         0,
125                         NULL,
126                         SmlarLogAssign,
127                         NULL
128         );
129
130         DefineCustomEnumVariable(
131                         "smlar.tf_method",
132                         "Method of TF caclulation",
133                         "TF method: n => number of entries, log => 1+log(n), const => constant value",
134                         &smlar_tf_method,
135                         smlar_tf_method,
136                         SmlarTFOptions,
137                         PGC_SUSET,
138                         0,
139                         NULL,
140                         NULL,
141                         NULL
142         );
143
144         smlar_guc_inited = true;        
145 }
146
147 double 
148 getOneAdd(void)
149 {
150         if (!smlar_guc_inited)
151                 initSmlarGUC();
152
153         return (smlar_logadd) ? 1.0 : 0.0;
154 }
155
156 int
157 getTFMethod(void)
158 {
159         if (!smlar_guc_inited)
160                 initSmlarGUC();
161
162         return smlar_tf_method;
163 }
164
165 double
166 GetSmlarLimit(void)
167 {
168         if (!smlar_guc_inited)
169                 initSmlarGUC();
170
171         return smlar_limit;
172 }
173
174 const char*
175 GetSmlarTable(void)
176 {
177         if (!smlar_guc_inited)
178                 initSmlarGUC();
179
180         return smlar_table;
181 }
182
183 int
184 getSmlType(void)
185 {
186         if (!smlar_guc_inited)
187                 initSmlarGUC();
188
189         return smlar_smltype;
190 }
191
192 bool
193 GetSmlarUsePersistent(void)
194 {
195         if (!smlar_guc_inited)
196                 initSmlarGUC();
197
198         return smlar_use_malloc;
199 }
200
201 PG_FUNCTION_INFO_V1(set_smlar_limit);
202 Datum       set_smlar_limit(PG_FUNCTION_ARGS);
203 Datum
204 set_smlar_limit(PG_FUNCTION_ARGS)
205 {
206         float4      nlimit = PG_GETARG_FLOAT4(0);
207         char            buf[32];
208
209         /* init smlar guc */
210         initSmlarGUC();
211
212         sprintf(buf,"%f", nlimit);
213         set_config_option("smlar.threshold", buf,
214                                                 PGC_USERSET, PGC_S_SESSION ,GUC_ACTION_SET, true, 0
215 #if PG_VERSION_NUM >= 90500
216                                                 ,false
217 #endif
218                                                 );
219         PG_RETURN_FLOAT4((float4)GetSmlarLimit());
220 }
221
222 PG_FUNCTION_INFO_V1(show_smlar_limit);
223 Datum       show_smlar_limit(PG_FUNCTION_ARGS);
224 Datum
225 show_smlar_limit(PG_FUNCTION_ARGS)
226 {
227         PG_RETURN_FLOAT4((float4)GetSmlarLimit());
228 }
229