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
|
/*
Copyright (c) 2015 Red Hat, Inc. <http://www.redhat.com>
This file is part of GlusterFS.
This file is licensed to you under your choice of the GNU Lesser
General Public License, version 3 or any later version (LGPLv3 or
later), or the GNU General Public License, version 2 (GPLv2), in all
cases as published by the Free Software Foundation.
*/
#ifndef __GFDB_SQLITE3_H
#define __GFDB_SQLITE3_H
/*Sqlite3 header file*/
#include <sqlite3.h>
#include "logging.h"
#include "gfdb_data_store_types.h"
#include "gfdb_mem-types.h"
#define GF_STMT_SIZE_MAX 2048
#define GF_DB_NAME "gfdb.db"
#define GF_FILE_TABLE "GF_FILE_TB"
#define GF_FILE_LINK_TABLE "GF_FLINK_TB"
#define GF_MASTER_TABLE "sqlite_master"
/*Since we have multiple tables to be created we put it in a transaction*/
#define GF_CREATE_STMT(out_str)\
do {\
sprintf (out_str , "BEGIN; CREATE TABLE IF NOT EXISTS "\
GF_FILE_TABLE\
"(GF_ID TEXT PRIMARY KEY NOT NULL, "\
"W_SEC INTEGER NOT NULL DEFAULT 0, "\
"W_MSEC INTEGER NOT NULL DEFAULT 0, "\
"UW_SEC INTEGER NOT NULL DEFAULT 0, "\
"UW_MSEC INTEGER NOT NULL DEFAULT 0, "\
"W_READ_SEC INTEGER NOT NULL DEFAULT 0, "\
"W_READ_MSEC INTEGER NOT NULL DEFAULT 0, "\
"UW_READ_SEC INTEGER NOT NULL DEFAULT 0, "\
"UW_READ_MSEC INTEGER NOT NULL DEFAULT 0, "\
"WRITE_FREQ_CNTR INTEGER NOT NULL DEFAULT 1, "\
"READ_FREQ_CNTR INTEGER NOT NULL DEFAULT 1); "\
"CREATE TABLE IF NOT EXISTS "\
GF_FILE_LINK_TABLE\
"(GF_ID TEXT NOT NULL, "\
"GF_PID TEXT NOT NULL, "\
"FNAME TEXT NOT NULL, "\
"FPATH TEXT NOT NULL, "\
"W_DEL_FLAG INTEGER NOT NULL DEFAULT 0, "\
"LINK_UPDATE INTEGER NOT NULL DEFAULT 0, "\
"PRIMARY KEY ( GF_ID, GF_PID, FNAME) "\
");"\
"COMMIT;"\
);;\
} while (0)
#define GF_COL_TB_WSEC GF_FILE_TABLE "." GF_COL_WSEC
#define GF_COL_TB_WMSEC GF_FILE_TABLE "." GF_COL_WMSEC
#define GF_COL_TB_UWSEC GF_FILE_TABLE "." GF_COL_UWSEC
#define GF_COL_TB_UWMSEC GF_FILE_TABLE "." GF_COL_UWMSEC
#define GF_COL_TB_RWSEC GF_FILE_TABLE "." GF_COL_WSEC_READ
#define GF_COL_TB_RWMSEC GF_FILE_TABLE "." GF_COL_WMSEC_READ
#define GF_COL_TB_RUWSEC GF_FILE_TABLE "." GF_COL_UWSEC_READ
#define GF_COL_TB_RUWMSEC GF_FILE_TABLE "." GF_COL_UWMSEC_READ
#define GF_COL_TB_WFC GF_FILE_TABLE "." GF_COL_WRITE_FREQ_CNTR
#define GF_COL_TB_RFC GF_FILE_TABLE "." GF_COL_READ_FREQ_CNTR
/*******************************************************************************
* SQLITE3 Connection details and PRAGMA
* ****************************************************************************/
#define GF_SQL_AV_NONE "none"
#define GF_SQL_AV_FULL "full"
#define GF_SQL_AV_INCR "incr"
#define GF_SQL_SYNC_OFF "off"
#define GF_SQL_SYNC_NORMAL "normal"
#define GF_SQL_SYNC_FULL "full"
#define GF_SQL_JM_DELETE "delete"
#define GF_SQL_JM_TRUNCATE "truncate"
#define GF_SQL_JM_PERSIST "persist"
#define GF_SQL_JM_MEMORY "memory"
#define GF_SQL_JM_WAL "wal"
#define GF_SQL_JM_OFF "off"
typedef enum gf_sql_auto_vacuum {
gf_sql_av_none = 0,
gf_sql_av_full,
gf_sql_av_incr,
gf_sql_av_invalid
} gf_sql_auto_vacuum_t;
typedef enum gf_sql_sync {
gf_sql_sync_off = 0,
gf_sql_sync_normal,
gf_sql_sync_full,
gf_sql_sync_invalid
} gf_sql_sync_t;
typedef enum gf_sql_journal_mode {
gf_sql_jm_wal = 0,
gf_sql_jm_delete,
gf_sql_jm_truncate,
gf_sql_jm_persist,
gf_sql_jm_memory,
gf_sql_jm_off,
gf_sql_jm_invalid
} gf_sql_journal_mode_t;
typedef struct gf_sql_connection {
char sqlite3_db_path[PATH_MAX];
sqlite3 *sqlite3_db_conn;
ssize_t cache_size;
ssize_t page_size;
ssize_t wal_autocheckpoint;
gf_sql_journal_mode_t journal_mode;
gf_sql_sync_t synchronous;
gf_sql_auto_vacuum_t auto_vacuum;
} gf_sql_connection_t;
#define CHECK_SQL_CONN(sql_conn, out)\
do {\
GF_VALIDATE_OR_GOTO(GFDB_STR_SQLITE3, sql_conn, out);\
if (!sql_conn->sqlite3_db_conn) {\
gf_log (GFDB_STR_SQLITE3, GF_LOG_ERROR,\
"sqlite3 connection not initialized");\
goto out;\
};\
} while (0)
#define GF_SQLITE3_SET_PRAGMA(sqlite3_config_str, param_key, format, value,\
ret, error)\
do {\
sprintf(sqlite3_config_str, "PRAGMA " param_key " = " format , value);\
ret = sqlite3_exec (sql_conn->sqlite3_db_conn, sqlite3_config_str,\
NULL, NULL, NULL);\
if (ret != SQLITE_OK) {\
gf_log (GFDB_STR_SQLITE3, GF_LOG_ERROR,\
"Failed executing: %s : %s",\
sqlite3_config_str, sqlite3_errmsg\
(sql_conn->sqlite3_db_conn));\
ret = -1;\
goto error;\
};\
} while (0)
/************************SQLITE3 PARAMS KEYS***********************************/
#define GFDB_SQL_PARAM_DBPATH "sql-db-path"
#define GFDB_SQL_PARAM_CACHE_SIZE "sql-db-cachesize"
#define GFDB_SQL_PARAM_PAGE_SIZE "sql-db-pagesize"
#define GFDB_SQL_PARAM_JOURNAL_MODE "sql-db-journalmode"
#define GFDB_SQL_PARAM_WAL_AUTOCHECK "sql-db-wal-autocheckpoint"
#define GFDB_SQL_PARAM_SYNC "sql-db-sync"
#define GFDB_SQL_PARAM_AUTO_VACUUM "sql-db-autovacuum"
#define GF_SQL_DEFAULT_DBPATH ""
#define GF_SQL_DEFAULT_PAGE_SIZE "4096"
#define GF_SQL_DEFAULT_CACHE_SIZE "1000"
#define GF_SQL_DEFAULT_WAL_AUTOCHECKPOINT "1000"
#define GF_SQL_DEFAULT_JOURNAL_MODE GF_SQL_JM_WAL
#define GF_SQL_DEFAULT_SYNC GF_SQL_SYNC_NORMAL
#define GF_SQL_DEFAULT_AUTO_VACUUM GF_SQL_AV_NONE
/* Defines the indexs for sqlite params
* The order should be maintained*/
typedef enum sqlite_param_index {
sql_dbpath_ix = 0,
sql_pagesize_ix,
sql_cachesize_ix,
sql_journalmode_ix,
sql_walautocheck_ix,
sql_dbsync_ix,
sql_autovacuum_ix,
/*This should be in the end*/
sql_index_max
} sqlite_param_index_t;
/* Array to hold the sqlite param keys
* The order should be maintained as sqlite_param_index_t*/
static char *sqlite_params_keys[] = {
GFDB_SQL_PARAM_DBPATH,
GFDB_SQL_PARAM_PAGE_SIZE,
GFDB_SQL_PARAM_CACHE_SIZE,
GFDB_SQL_PARAM_JOURNAL_MODE,
GFDB_SQL_PARAM_WAL_AUTOCHECK,
GFDB_SQL_PARAM_SYNC,
GFDB_SQL_PARAM_AUTO_VACUUM
};
/* Array of default values for sqlite params
* The order should be maintained as sqlite_param_index_t*/
static char *sqlite_params_default_value[] = {
GF_SQL_DEFAULT_DBPATH,
GF_SQL_DEFAULT_PAGE_SIZE,
GF_SQL_DEFAULT_CACHE_SIZE,
GF_SQL_DEFAULT_JOURNAL_MODE,
GF_SQL_DEFAULT_WAL_AUTOCHECKPOINT,
GF_SQL_DEFAULT_SYNC,
GF_SQL_DEFAULT_AUTO_VACUUM
};
/*Extract sql params from page_size to auto_vacumm
* The dbpath is extracted in a different way*/
static inline int
gfdb_set_sql_params(char *comp_name, dict_t *from_dict, dict_t *to_dict)
{
sqlite_param_index_t sql_index = sql_pagesize_ix;
char *_val_str = NULL;
int ret = -1;
GF_ASSERT (comp_name);
GF_ASSERT (from_dict);
GF_ASSERT (to_dict);
/*Extact and Set of the sql params from page_size*/
for (sql_index = sql_pagesize_ix; sql_index < sql_index_max;
sql_index++) {
_val_str = NULL;
GET_DB_PARAM_FROM_DICT_DEFAULT (comp_name, from_dict,
sqlite_params_keys[sql_index], _val_str,
sqlite_params_default_value[sql_index]);
SET_DB_PARAM_TO_DICT (comp_name, to_dict,
sqlite_params_keys[sql_index], _val_str, ret, out);
}
out:
return ret;
}
/*************************SQLITE3 GFDB PLUGINS*********************************/
/*Db init and fini modules*/
int gf_sqlite3_fini (void **db_conn);
int gf_sqlite3_init (dict_t *args, void **db_conn);
/*insert/update/delete modules*/
int gf_sqlite3_insert (void *db_conn, gfdb_db_record_t *);
int gf_sqlite3_delete (void *db_conn, gfdb_db_record_t *);
/*querying modules*/
int gf_sqlite3_find_all (void *db_conn, gf_query_callback_t,
void *_query_cbk_args);
int gf_sqlite3_find_unchanged_for_time (void *db_conn,
gf_query_callback_t query_callback,
void *_query_cbk_args,
gfdb_time_t *for_time);
int gf_sqlite3_find_recently_changed_files (void *db_conn,
gf_query_callback_t query_callback,
void *_query_cbk_args,
gfdb_time_t *from_time);
int gf_sqlite3_find_unchanged_for_time_freq (void *db_conn,
gf_query_callback_t query_callback,
void *_query_cbk_args,
gfdb_time_t *for_time,
int write_freq_cnt,
int read_freq_cnt,
gf_boolean_t clear_counters);
int gf_sqlite3_find_recently_changed_files_freq (void *db_conn,
gf_query_callback_t query_callback,
void *_query_cbk_args,
gfdb_time_t *from_time,
int write_freq_cnt,
int read_freq_cnt,
gf_boolean_t clear_counters);
void gf_sqlite3_fill_db_operations (gfdb_db_operations_t *gfdb_db_ops);
#endif
|