forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcbtdb.c
296 lines (246 loc) · 7.95 KB
/
tcbtdb.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* tcbtdb.c -- Tokyo Cabinet database functions
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2016 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <errno.h>
#include <tcutil.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "tcbtdb.h"
#include "tcabdb.h"
#ifdef HAVE_LIBGEOIP
#include "geolocation.h"
#endif
#include "error.h"
#include "xmalloc.h"
#ifdef TCB_BTREE
/* Get the on-disk databases path.
*
* On success, the databases path string is returned. */
char *
tc_db_set_path (const char *dbname, int module)
{
char *path;
int cx;
if (conf.db_path != NULL) {
cx = snprintf (NULL, 0, "%s%dm%s", conf.db_path, module, dbname) + 1;
path = xmalloc (cx);
sprintf (path, "%s%dm%s", conf.db_path, module, dbname);
} else {
cx = snprintf (NULL, 0, "%s%dm%s", TC_DBPATH, module, dbname) + 1;
path = xmalloc (cx);
sprintf (path, "%s%dm%s", TC_DBPATH, module, dbname);
}
return path;
}
/* Set the given database parameter into the parameters buffer.
*
* On error, a negative number is returned.
* On success, the number of characters that would have been written is
* returned. */
static int
set_dbparam (char *params, int len, const char *fmt, ...)
{
int n;
va_list args;
va_start (args, fmt);
n = vsnprintf (params + len, DB_PARAMS - len, fmt, args);
va_end (args);
if (n < 0) {
n = 0;
LOG_DEBUG (("Output error is encountered on set_dbparam\n"));
} else if (n >= DB_PARAMS - len) {
LOG_DEBUG (("Output truncated on set_dbparam\n"));
n = DB_PARAMS - len;
}
return n;
}
/* Set the on-disk database parameters from enabled options in the config file. */
void
tc_db_get_params (char *params, const char *path)
{
int len = 0;
long xmmap = conf.xmmap;
uint32_t lcnum, ncnum, lmemb, nmemb, bnum;
/* copy path name to buffer */
len += set_dbparam (params, len, "%s", path);
/* caching parameters of a B+ tree database object */
lcnum = conf.cache_lcnum > 0 ? conf.cache_lcnum : TC_LCNUM;
len += set_dbparam (params, len, "#%s=%d", "lcnum", lcnum);
ncnum = conf.cache_ncnum > 0 ? conf.cache_ncnum : TC_NCNUM;
len += set_dbparam (params, len, "#%s=%d", "ncnum", ncnum);
/* set the size of the extra mapped memory */
if (xmmap > 0)
len += set_dbparam (params, len, "#%s=%ld", "xmsiz", xmmap);
lmemb = conf.tune_lmemb > 0 ? conf.tune_lmemb : TC_LMEMB;
len += set_dbparam (params, len, "#%s=%d", "lmemb", lmemb);
nmemb = conf.tune_nmemb > 0 ? conf.tune_nmemb : TC_NMEMB;
len += set_dbparam (params, len, "#%s=%d", "nmemb", nmemb);
bnum = conf.tune_bnum > 0 ? conf.tune_bnum : TC_BNUM;
len += set_dbparam (params, len, "#%s=%d", "bnum", bnum);
/* compression */
len += set_dbparam (params, len, "#%s=%c", "opts", 'l');
if (conf.compression == TC_BZ2) {
len += set_dbparam (params, len, "%c", 'b');
} else if (conf.compression == TC_ZLIB) {
len += set_dbparam (params, len, "%c", 'd');
}
/* open flags. create a new database if not exist, otherwise read it */
len += set_dbparam (params, len, "#%s=%s", "mode", "wc");
/* if not loading from disk, truncate regardless if a db file exists */
if (!conf.load_from_disk)
len += set_dbparam (params, len, "%c", 't');
LOG_DEBUG (("%s\n", path));
LOG_DEBUG (("params: %s\n", params));
}
/* Open the database handle.
*
* On error, the program will exit.
* On success, the opened on-disk database is returned. */
TCBDB *
tc_bdb_create (const char *dbname, int module)
{
TCBDB *bdb;
char *path = NULL;
int ecode;
uint32_t lcnum, ncnum, lmemb, nmemb, bnum, flags;
path = tc_db_set_path (dbname, module);
bdb = tcbdbnew ();
lcnum = conf.cache_lcnum > 0 ? conf.cache_lcnum : TC_LCNUM;
ncnum = conf.cache_ncnum > 0 ? conf.cache_ncnum : TC_NCNUM;
/* set the caching parameters of a B+ tree database object */
if (!tcbdbsetcache (bdb, lcnum, ncnum)) {
free (path);
FATAL ("Unable to set TCB cache");
}
/* set the size of the extra mapped memory */
if (conf.xmmap > 0 && !tcbdbsetxmsiz (bdb, conf.xmmap)) {
free (path);
FATAL ("Unable to set TCB xmmap.");
}
lmemb = conf.tune_lmemb > 0 ? conf.tune_lmemb : TC_LMEMB;
nmemb = conf.tune_nmemb > 0 ? conf.tune_nmemb : TC_NMEMB;
bnum = conf.tune_bnum > 0 ? conf.tune_bnum : TC_BNUM;
/* compression */
flags = BDBTLARGE;
if (conf.compression == TC_BZ2) {
flags |= BDBTBZIP;
} else if (conf.compression == TC_ZLIB) {
flags |= BDBTDEFLATE;
}
/* set the tuning parameters */
tcbdbtune (bdb, lmemb, nmemb, bnum, 8, 10, flags);
/* open flags */
flags = BDBOWRITER | BDBOCREAT;
if (!conf.load_from_disk)
flags |= BDBOTRUNC;
/* attempt to open the database */
if (!tcbdbopen (bdb, path, flags)) {
free (path);
ecode = tcbdbecode (bdb);
FATAL ("%s", tcbdberrmsg (ecode));
}
free (path);
return bdb;
}
/* Close the database handle.
*
* On error, 1 is returned.
* On success or the database is closed, 0 is returned. */
int
tc_bdb_close (void *db, char *dbname)
{
TCBDB *bdb = db;
int ecode;
if (bdb == NULL)
return 1;
/* close the database */
if (!tcbdbclose (bdb)) {
ecode = tcbdbecode (bdb);
FATAL ("%s", tcbdberrmsg (ecode));
}
/* delete the object */
tcbdbdel (bdb);
/* remove database file */
if (!conf.keep_db_files && !tcremovelink (dbname))
LOG_DEBUG (("Unable to remove DB: %s\n", dbname));
free (dbname);
return 0;
}
/* Compare the given integer value with one in the list object.
*
* If values are the equal, 1 is returned else 0 is returned. */
static int
find_int_key_in_list (void *data, void *needle)
{
return (*(int *) data) == (*(int *) needle) ? 1 : 0;
}
/* Iterate over the list object and compare the current value with the given
* value.
*
* If the value exists, 1 is returned else 0 is returned. */
static int
is_value_in_tclist (TCLIST * tclist, void *value)
{
int i, sz;
int *val;
if (!tclist)
return 0;
for (i = 0; i < tclistnum (tclist); ++i) {
val = (int *) tclistval (tclist, i, &sz);
if (find_int_key_in_list (value, val))
return 1;
}
return 0;
}
/* Insert a string key and the corresponding string value.
* Note: If the key exists, the value is not replaced.
*
* On error, or if found, 1 is returned.
* On success, or if not in the list, 0 is returned. */
int
ins_igsl (void *hash, int key, int value)
{
TCLIST *list;
int in_list = 0;
if (!hash)
return -1;
/* key found, check if key exists within the list */
if ((list = tcbdbget4 (hash, &key, sizeof (int))) != NULL) {
if (is_value_in_tclist (list, &value))
in_list = 1;
tclistdel (list);
}
/* if not on the list, add it */
if (!in_list && tcbdbputdup (hash, &key, sizeof (int), &value, sizeof (int)))
return 0;
return -1;
}
#endif