aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore/plugin_peerstore_sqlite.c
diff options
context:
space:
mode:
authorOmar Tarabai <tarabai@devegypt.com>2014-08-04 11:06:52 +0000
committerOmar Tarabai <tarabai@devegypt.com>2014-08-04 11:06:52 +0000
commit4371b0fcd9b50f78ca27ac48adf2f4663b2dfc9b (patch)
tree05c40696267088fa94cdb0a94b3c079b38ea8ed2 /src/peerstore/plugin_peerstore_sqlite.c
parentef494e57d189e2b7c038104e44398d11026ff6a6 (diff)
downloadgnunet-4371b0fcd9b50f78ca27ac48adf2f4663b2dfc9b.tar.gz
gnunet-4371b0fcd9b50f78ca27ac48adf2f4663b2dfc9b.zip
peerstore: indentation
Diffstat (limited to 'src/peerstore/plugin_peerstore_sqlite.c')
-rw-r--r--src/peerstore/plugin_peerstore_sqlite.c310
1 files changed, 157 insertions, 153 deletions
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c
index 2726eef3d..588fae73c 100644
--- a/src/peerstore/plugin_peerstore_sqlite.c
+++ b/src/peerstore/plugin_peerstore_sqlite.c
@@ -121,19 +121,24 @@ struct Plugin
121 * @return number of deleted records 121 * @return number of deleted records
122 */ 122 */
123static int 123static int
124peerstore_sqlite_delete_records(void *cls, 124peerstore_sqlite_delete_records (void *cls, const char *sub_system,
125 const char *sub_system, 125 const struct GNUNET_PeerIdentity *peer,
126 const struct GNUNET_PeerIdentity *peer, 126 const char *key)
127 const char *key)
128{ 127{
129 struct Plugin *plugin = cls; 128 struct Plugin *plugin = cls;
130 sqlite3_stmt *stmt = plugin->delete_peerstoredata; 129 sqlite3_stmt *stmt = plugin->delete_peerstoredata;
131 130
132 if((SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)) 131 if ((SQLITE_OK !=
133 || (SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)) 132 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1,
134 || (SQLITE_OK != sqlite3_bind_text(stmt, 3, key, strlen(key) + 1, SQLITE_STATIC))) 133 SQLITE_STATIC)) ||
134 (SQLITE_OK !=
135 sqlite3_bind_blob (stmt, 2, peer, sizeof (struct GNUNET_PeerIdentity),
136 SQLITE_STATIC)) ||
137 (SQLITE_OK !=
138 sqlite3_bind_text (stmt, 3, key, strlen (key) + 1, SQLITE_STATIC)))
135 { 139 {
136 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind"); 140 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
141 "sqlite3_bind");
137 } 142 }
138 else if (SQLITE_DONE != sqlite3_step (stmt)) 143 else if (SQLITE_DONE != sqlite3_step (stmt))
139 { 144 {
@@ -146,9 +151,10 @@ peerstore_sqlite_delete_records(void *cls,
146 "sqlite3_reset"); 151 "sqlite3_reset");
147 return 0; 152 return 0;
148 } 153 }
149 return sqlite3_changes(plugin->dbh); 154 return sqlite3_changes (plugin->dbh);
150} 155}
151 156
157
152/** 158/**
153 * Delete expired records (expiry < now) 159 * Delete expired records (expiry < now)
154 * 160 *
@@ -157,15 +163,16 @@ peerstore_sqlite_delete_records(void *cls,
157 * @return number of records deleted 163 * @return number of records deleted
158 */ 164 */
159static int 165static int
160peerstore_sqlite_expire_records(void *cls, 166peerstore_sqlite_expire_records (void *cls, struct GNUNET_TIME_Absolute now)
161 struct GNUNET_TIME_Absolute now)
162{ 167{
163 struct Plugin *plugin = cls; 168 struct Plugin *plugin = cls;
164 sqlite3_stmt *stmt = plugin->expire_peerstoredata; 169 sqlite3_stmt *stmt = plugin->expire_peerstoredata;
165 170
166 if(SQLITE_OK != sqlite3_bind_int64(stmt, 1, (sqlite3_uint64)now.abs_value_us)) 171 if (SQLITE_OK !=
172 sqlite3_bind_int64 (stmt, 1, (sqlite3_uint64) now.abs_value_us))
167 { 173 {
168 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind"); 174 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
175 "sqlite3_bind");
169 } 176 }
170 else if (SQLITE_DONE != sqlite3_step (stmt)) 177 else if (SQLITE_DONE != sqlite3_step (stmt))
171 { 178 {
@@ -178,10 +185,10 @@ peerstore_sqlite_expire_records(void *cls,
178 "sqlite3_reset"); 185 "sqlite3_reset");
179 return 0; 186 return 0;
180 } 187 }
181 return sqlite3_changes(plugin->dbh); 188 return sqlite3_changes (plugin->dbh);
182
183} 189}
184 190
191
185/** 192/**
186 * Iterate over the records given an optional peer id 193 * Iterate over the records given an optional peer id
187 * and/or key. 194 * and/or key.
@@ -195,11 +202,11 @@ peerstore_sqlite_expire_records(void *cls,
195 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 202 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
196 */ 203 */
197static int 204static int
198peerstore_sqlite_iterate_records (void *cls, 205peerstore_sqlite_iterate_records (void *cls, const char *sub_system,
199 const char *sub_system, 206 const struct GNUNET_PeerIdentity *peer,
200 const struct GNUNET_PeerIdentity *peer, 207 const char *key,
201 const char *key, 208 GNUNET_PEERSTORE_Processor iter,
202 GNUNET_PEERSTORE_Processor iter, void *iter_cls) 209 void *iter_cls)
203{ 210{
204 struct Plugin *plugin = cls; 211 struct Plugin *plugin = cls;
205 sqlite3_stmt *stmt; 212 sqlite3_stmt *stmt;
@@ -207,59 +214,76 @@ peerstore_sqlite_iterate_records (void *cls,
207 int sret; 214 int sret;
208 struct GNUNET_PEERSTORE_Record *ret; 215 struct GNUNET_PEERSTORE_Record *ret;
209 216
210 LOG(GNUNET_ERROR_TYPE_DEBUG, "Executing iterate request on sqlite db.\n"); 217 LOG (GNUNET_ERROR_TYPE_DEBUG, "Executing iterate request on sqlite db.\n");
211 if(NULL == peer && NULL == key) 218 if (NULL == peer && NULL == key)
212 { 219 {
213 stmt = plugin->select_peerstoredata; 220 stmt = plugin->select_peerstoredata;
214 err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)); 221 err =
222 (SQLITE_OK !=
223 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1,
224 SQLITE_STATIC));
215 } 225 }
216 else if(NULL == key) 226 else if (NULL == key)
217 { 227 {
218 stmt = plugin->select_peerstoredata_by_pid; 228 stmt = plugin->select_peerstoredata_by_pid;
219 err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)) 229 err =
220 || (SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)); 230 (SQLITE_OK !=
231 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1,
232 SQLITE_STATIC)) ||
233 (SQLITE_OK !=
234 sqlite3_bind_blob (stmt, 2, peer, sizeof (struct GNUNET_PeerIdentity),
235 SQLITE_STATIC));
221 } 236 }
222 else if(NULL == peer) 237 else if (NULL == peer)
223 { 238 {
224 stmt = plugin->select_peerstoredata_by_key; 239 stmt = plugin->select_peerstoredata_by_key;
225 err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)) 240 err =
226 || (SQLITE_OK != sqlite3_bind_text(stmt, 2, key, strlen(key) + 1, SQLITE_STATIC)); 241 (SQLITE_OK !=
242 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1,
243 SQLITE_STATIC)) ||
244 (SQLITE_OK !=
245 sqlite3_bind_text (stmt, 2, key, strlen (key) + 1, SQLITE_STATIC));
227 } 246 }
228 else 247 else
229 { 248 {
230 stmt = plugin->select_peerstoredata_by_all; 249 stmt = plugin->select_peerstoredata_by_all;
231 err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)) 250 err =
232 || (SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)) 251 (SQLITE_OK !=
233 || (SQLITE_OK != sqlite3_bind_text(stmt, 3, key, strlen(key) + 1, SQLITE_STATIC)); 252 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1,
253 SQLITE_STATIC)) ||
254 (SQLITE_OK !=
255 sqlite3_bind_blob (stmt, 2, peer, sizeof (struct GNUNET_PeerIdentity),
256 SQLITE_STATIC)) ||
257 (SQLITE_OK !=
258 sqlite3_bind_text (stmt, 3, key, strlen (key) + 1, SQLITE_STATIC));
234 } 259 }
235 260
236 if (err) 261 if (err)
237 { 262 {
238 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 263 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
239 "sqlite3_bind_XXXX"); 264 "sqlite3_bind_XXXX");
240 if (SQLITE_OK != sqlite3_reset (stmt)) 265 if (SQLITE_OK != sqlite3_reset (stmt))
241 LOG_SQLITE (plugin, 266 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
242 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 267 "sqlite3_reset");
243 "sqlite3_reset");
244 return GNUNET_SYSERR; 268 return GNUNET_SYSERR;
245 } 269 }
246 while (SQLITE_ROW == (sret = sqlite3_step (stmt))) 270 while (SQLITE_ROW == (sret = sqlite3_step (stmt)))
247 { 271 {
248 LOG(GNUNET_ERROR_TYPE_DEBUG, "Returning a matched record.\n"); 272 LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning a matched record.\n");
249 ret = GNUNET_new(struct GNUNET_PEERSTORE_Record); 273 ret = GNUNET_new (struct GNUNET_PEERSTORE_Record);
250 ret->sub_system = (char *)sqlite3_column_text(stmt, 0); 274
251 ret->peer = (struct GNUNET_PeerIdentity *)sqlite3_column_blob(stmt, 1); 275 ret->sub_system = (char *) sqlite3_column_text (stmt, 0);
252 ret->key = (char *)sqlite3_column_text(stmt, 2); 276 ret->peer = (struct GNUNET_PeerIdentity *) sqlite3_column_blob (stmt, 1);
253 ret->value = (void *)sqlite3_column_blob(stmt, 3); 277 ret->key = (char *) sqlite3_column_text (stmt, 2);
254 ret->value_size = sqlite3_column_bytes(stmt, 3); 278 ret->value = (void *) sqlite3_column_blob (stmt, 3);
255 ret->expiry = GNUNET_new(struct GNUNET_TIME_Absolute); 279 ret->value_size = sqlite3_column_bytes (stmt, 3);
256 ret->expiry->abs_value_us = (uint64_t)sqlite3_column_int64(stmt, 4); 280 ret->expiry = GNUNET_new (struct GNUNET_TIME_Absolute);
281
282 ret->expiry->abs_value_us = (uint64_t) sqlite3_column_int64 (stmt, 4);
257 if (NULL != iter) 283 if (NULL != iter)
258 iter (iter_cls, 284 iter (iter_cls, ret, NULL);
259 ret, 285 GNUNET_free (ret->expiry);
260 NULL); 286 GNUNET_free (ret);
261 GNUNET_free(ret->expiry);
262 GNUNET_free(ret);
263 } 287 }
264 if (SQLITE_DONE != sret) 288 if (SQLITE_DONE != sret)
265 { 289 {
@@ -268,16 +292,16 @@ peerstore_sqlite_iterate_records (void *cls,
268 } 292 }
269 if (SQLITE_OK != sqlite3_reset (stmt)) 293 if (SQLITE_OK != sqlite3_reset (stmt))
270 { 294 {
271 LOG_SQLITE (plugin, 295 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
272 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 296 "sqlite3_reset");
273 "sqlite3_reset");
274 err = 1; 297 err = 1;
275 } 298 }
276 if(err) 299 if (err)
277 return GNUNET_SYSERR; 300 return GNUNET_SYSERR;
278 return GNUNET_OK; 301 return GNUNET_OK;
279} 302}
280 303
304
281/** 305/**
282 * Store a record in the peerstore. 306 * Store a record in the peerstore.
283 * Key is the combination of sub system and peer identity. 307 * Key is the combination of sub system and peer identity.
@@ -294,29 +318,32 @@ peerstore_sqlite_iterate_records (void *cls,
294 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 318 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
295 */ 319 */
296static int 320static int
297peerstore_sqlite_store_record (void *cls, 321peerstore_sqlite_store_record (void *cls, const char *sub_system,
298 const char *sub_system,
299 const struct GNUNET_PeerIdentity *peer, 322 const struct GNUNET_PeerIdentity *peer,
300 const char *key, 323 const char *key, const void *value, size_t size,
301 const void *value,
302 size_t size,
303 struct GNUNET_TIME_Absolute expiry, 324 struct GNUNET_TIME_Absolute expiry,
304 enum GNUNET_PEERSTORE_StoreOption options) 325 enum GNUNET_PEERSTORE_StoreOption options)
305{ 326{
306 struct Plugin *plugin = cls; 327 struct Plugin *plugin = cls;
307 sqlite3_stmt *stmt = plugin->insert_peerstoredata; 328 sqlite3_stmt *stmt = plugin->insert_peerstoredata;
308 329
309 if(GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) 330 if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options)
310 { 331 {
311 peerstore_sqlite_delete_records(cls, sub_system, peer, key); 332 peerstore_sqlite_delete_records (cls, sub_system, peer, key);
312 } 333 }
313 if(SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC) 334 if (SQLITE_OK !=
314 || SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC) 335 sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1,
315 || SQLITE_OK != sqlite3_bind_text(stmt, 3, key, strlen(key) + 1, SQLITE_STATIC) 336 SQLITE_STATIC) ||
316 || SQLITE_OK != sqlite3_bind_blob(stmt, 4, value, size, SQLITE_STATIC) 337 SQLITE_OK != sqlite3_bind_blob (stmt, 2, peer,
317 || SQLITE_OK != sqlite3_bind_int64(stmt, 5, (sqlite3_uint64)expiry.abs_value_us)) 338 sizeof (struct GNUNET_PeerIdentity),
339 SQLITE_STATIC) ||
340 SQLITE_OK != sqlite3_bind_text (stmt, 3, key, strlen (key) + 1,
341 SQLITE_STATIC) ||
342 SQLITE_OK != sqlite3_bind_blob (stmt, 4, value, size, SQLITE_STATIC) ||
343 SQLITE_OK != sqlite3_bind_int64 (stmt, 5,
344 (sqlite3_uint64) expiry.abs_value_us))
318 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 345 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
319 "sqlite3_bind"); 346 "sqlite3_bind");
320 else if (SQLITE_DONE != sqlite3_step (stmt)) 347 else if (SQLITE_DONE != sqlite3_step (stmt))
321 { 348 {
322 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 349 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
@@ -328,7 +355,6 @@ peerstore_sqlite_store_record (void *cls,
328 "sqlite3_reset"); 355 "sqlite3_reset");
329 return GNUNET_SYSERR; 356 return GNUNET_SYSERR;
330 } 357 }
331
332 return GNUNET_OK; 358 return GNUNET_OK;
333} 359}
334 360
@@ -341,20 +367,19 @@ peerstore_sqlite_store_record (void *cls,
341 * @return 0 on success 367 * @return 0 on success
342 */ 368 */
343static int 369static int
344sql_exec (sqlite3 *dbh, const char *sql) 370sql_exec (sqlite3 * dbh, const char *sql)
345{ 371{
346 int result; 372 int result;
347 373
348 result = sqlite3_exec (dbh, sql, NULL, NULL, NULL); 374 result = sqlite3_exec (dbh, sql, NULL, NULL, NULL);
349 LOG (GNUNET_ERROR_TYPE_DEBUG, 375 LOG (GNUNET_ERROR_TYPE_DEBUG, "Executed `%s' / %d\n", sql, result);
350 "Executed `%s' / %d\n", sql, result);
351 if (result != SQLITE_OK) 376 if (result != SQLITE_OK)
352 LOG (GNUNET_ERROR_TYPE_ERROR, 377 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error executing SQL query: %s\n %s\n"),
353 _("Error executing SQL query: %s\n %s\n"), 378 sqlite3_errmsg (dbh), sql);
354 sqlite3_errmsg (dbh), sql);
355 return result; 379 return result;
356} 380}
357 381
382
358/** 383/**
359 * @brief Prepare a SQL statement 384 * @brief Prepare a SQL statement
360 * 385 *
@@ -364,37 +389,37 @@ sql_exec (sqlite3 *dbh, const char *sql)
364 * @return 0 on success 389 * @return 0 on success
365 */ 390 */
366static int 391static int
367sql_prepare (sqlite3 *dbh, const char *sql, sqlite3_stmt **stmt) 392sql_prepare (sqlite3 * dbh, const char *sql, sqlite3_stmt ** stmt)
368{ 393{
369 char *tail; 394 char *tail;
370 int result; 395 int result;
371 396
372 result = sqlite3_prepare_v2 (dbh, sql, strlen (sql), stmt, 397 result =
373 (const char **) &tail); 398 sqlite3_prepare_v2 (dbh, sql, strlen (sql), stmt, (const char **) &tail);
374 LOG (GNUNET_ERROR_TYPE_DEBUG, 399 LOG (GNUNET_ERROR_TYPE_DEBUG, "Prepared `%s' / %p: %d\n", sql, *stmt, result);
375 "Prepared `%s' / %p: %d\n", sql, *stmt, result);
376 if (result != SQLITE_OK) 400 if (result != SQLITE_OK)
377 LOG (GNUNET_ERROR_TYPE_ERROR, 401 LOG (GNUNET_ERROR_TYPE_ERROR, _("Error preparing SQL query: %s\n %s\n"),
378 _("Error preparing SQL query: %s\n %s\n"), 402 sqlite3_errmsg (dbh), sql);
379 sqlite3_errmsg (dbh), sql);
380 return result; 403 return result;
381} 404}
382 405
406
383/** 407/**
384 * sqlite3 custom function for comparison of uint64_t values 408 * sqlite3 custom function for comparison of uint64_t values
385 * since it is not supported by default 409 * since it is not supported by default
386 */ 410 */
387void sqlite3_lessthan(sqlite3_context* ctx, int dummy, 411void
388 sqlite3_value** values) 412sqlite3_lessthan (sqlite3_context * ctx, int dummy, sqlite3_value ** values)
389{ 413{
390 uint64_t v1; 414 uint64_t v1;
391 uint64_t v2; 415 uint64_t v2;
392 416
393 v1 = (uint64_t)sqlite3_value_int64(values[0]); 417 v1 = (uint64_t) sqlite3_value_int64 (values[0]);
394 v2 = (uint64_t)sqlite3_value_int64(values[1]); 418 v2 = (uint64_t) sqlite3_value_int64 (values[1]);
395 sqlite3_result_int(ctx, v1 < v2); 419 sqlite3_result_int (ctx, v1 < v2);
396} 420}
397 421
422
398/** 423/**
399 * Initialize the database connections and associated 424 * Initialize the database connections and associated
400 * data structures (create tables and indices 425 * data structures (create tables and indices
@@ -412,8 +437,8 @@ database_setup (struct Plugin *plugin)
412 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "peerstore-sqlite", 437 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "peerstore-sqlite",
413 "FILENAME", &filename)) 438 "FILENAME", &filename))
414 { 439 {
415 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 440 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, "peerstore-sqlite",
416 "peerstore-sqlite", "FILENAME"); 441 "FILENAME");
417 return GNUNET_SYSERR; 442 return GNUNET_SYSERR;
418 } 443 }
419 if (GNUNET_OK != GNUNET_DISK_file_test (filename)) 444 if (GNUNET_OK != GNUNET_DISK_file_test (filename))
@@ -427,16 +452,13 @@ database_setup (struct Plugin *plugin)
427 } 452 }
428 /* filename should be UTF-8-encoded. If it isn't, it's a bug */ 453 /* filename should be UTF-8-encoded. If it isn't, it's a bug */
429 plugin->fn = filename; 454 plugin->fn = filename;
430
431 /* Open database and precompile statements */ 455 /* Open database and precompile statements */
432 if (SQLITE_OK != sqlite3_open (plugin->fn, &plugin->dbh)) 456 if (SQLITE_OK != sqlite3_open (plugin->fn, &plugin->dbh))
433 { 457 {
434 LOG (GNUNET_ERROR_TYPE_ERROR, 458 LOG (GNUNET_ERROR_TYPE_ERROR, _("Unable to initialize SQLite: %s.\n"),
435 _("Unable to initialize SQLite: %s.\n"), 459 sqlite3_errmsg (plugin->dbh));
436 sqlite3_errmsg (plugin->dbh));
437 return GNUNET_SYSERR; 460 return GNUNET_SYSERR;
438 } 461 }
439
440 sql_exec (plugin->dbh, "PRAGMA temp_store=MEMORY"); 462 sql_exec (plugin->dbh, "PRAGMA temp_store=MEMORY");
441 sql_exec (plugin->dbh, "PRAGMA synchronous=OFF"); 463 sql_exec (plugin->dbh, "PRAGMA synchronous=OFF");
442 sql_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF"); 464 sql_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF");
@@ -444,73 +466,54 @@ database_setup (struct Plugin *plugin)
444 sql_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\""); 466 sql_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\"");
445 sql_exec (plugin->dbh, "PRAGMA count_changes=OFF"); 467 sql_exec (plugin->dbh, "PRAGMA count_changes=OFF");
446 sql_exec (plugin->dbh, "PRAGMA page_size=4096"); 468 sql_exec (plugin->dbh, "PRAGMA page_size=4096");
447
448 sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS); 469 sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS);
449
450 /* Create tables */ 470 /* Create tables */
451
452 sql_exec (plugin->dbh, 471 sql_exec (plugin->dbh,
453 "CREATE TABLE IF NOT EXISTS peerstoredata (\n" 472 "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
454 " sub_system TEXT NOT NULL,\n" 473 " sub_system TEXT NOT NULL,\n" " peer_id BLOB NOT NULL,\n"
455 " peer_id BLOB NOT NULL,\n" 474 " key TEXT NOT NULL,\n" " value BLOB NULL,\n"
456 " key TEXT NOT NULL,\n" 475 " expiry sqlite3_uint64 NOT NULL" ");");
457 " value BLOB NULL,\n" 476 sqlite3_create_function (plugin->dbh, "UINT64_LT", 2, SQLITE_UTF8, NULL,
458 " expiry sqlite3_uint64 NOT NULL" 477 &sqlite3_lessthan, NULL, NULL);
459 ");");
460
461 sqlite3_create_function(plugin->dbh, "UINT64_LT", 2, SQLITE_UTF8, NULL, &sqlite3_lessthan, NULL, NULL);
462
463 /* Create Indices */ 478 /* Create Indices */
464 if (SQLITE_OK != 479 if (SQLITE_OK !=
465 sqlite3_exec(plugin->dbh, 480 sqlite3_exec (plugin->dbh,
466 "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)", 481 "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)",
467 NULL, NULL, NULL)) 482 NULL, NULL, NULL))
468 { 483 {
469 LOG (GNUNET_ERROR_TYPE_ERROR, 484 LOG (GNUNET_ERROR_TYPE_ERROR, _("Unable to create indices: %s.\n"),
470 _("Unable to create indices: %s.\n"), 485 sqlite3_errmsg (plugin->dbh));
471 sqlite3_errmsg (plugin->dbh)); 486 return GNUNET_SYSERR;
472 return GNUNET_SYSERR;
473 } 487 }
474
475 /* Prepare statements */ 488 /* Prepare statements */
476 489
477 sql_prepare (plugin->dbh, 490 sql_prepare (plugin->dbh,
478 "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry) VALUES (?,?,?,?,?);", 491 "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry) VALUES (?,?,?,?,?);",
479 &plugin->insert_peerstoredata); 492 &plugin->insert_peerstoredata);
480 sql_prepare(plugin->dbh, 493 sql_prepare (plugin->dbh,
481 "SELECT * FROM peerstoredata" 494 "SELECT * FROM peerstoredata" " WHERE sub_system = ?",
482 " WHERE sub_system = ?", 495 &plugin->select_peerstoredata);
483 &plugin->select_peerstoredata); 496 sql_prepare (plugin->dbh,
484 sql_prepare(plugin->dbh, 497 "SELECT * FROM peerstoredata" " WHERE sub_system = ?"
485 "SELECT * FROM peerstoredata" 498 " AND peer_id = ?", &plugin->select_peerstoredata_by_pid);
486 " WHERE sub_system = ?" 499 sql_prepare (plugin->dbh,
487 " AND peer_id = ?", 500 "SELECT * FROM peerstoredata" " WHERE sub_system = ?"
488 &plugin->select_peerstoredata_by_pid); 501 " AND key = ?", &plugin->select_peerstoredata_by_key);
489 sql_prepare(plugin->dbh, 502 sql_prepare (plugin->dbh,
490 "SELECT * FROM peerstoredata" 503 "SELECT * FROM peerstoredata" " WHERE sub_system = ?"
491 " WHERE sub_system = ?" 504 " AND peer_id = ?" " AND key = ?",
492 " AND key = ?", 505 &plugin->select_peerstoredata_by_all);
493 &plugin->select_peerstoredata_by_key); 506 sql_prepare (plugin->dbh,
494 sql_prepare(plugin->dbh, 507 "DELETE FROM peerstoredata" " WHERE UINT64_LT(expiry, ?)",
495 "SELECT * FROM peerstoredata" 508 &plugin->expire_peerstoredata);
496 " WHERE sub_system = ?" 509 sql_prepare (plugin->dbh,
497 " AND peer_id = ?" 510 "DELETE FROM peerstoredata" " WHERE sub_system = ?"
498 " AND key = ?", 511 " AND peer_id = ?" " AND key = ?",
499 &plugin->select_peerstoredata_by_all); 512 &plugin->delete_peerstoredata);
500 sql_prepare(plugin->dbh,
501 "DELETE FROM peerstoredata"
502 " WHERE UINT64_LT(expiry, ?)",
503 &plugin->expire_peerstoredata);
504 sql_prepare(plugin->dbh,
505 "DELETE FROM peerstoredata"
506 " WHERE sub_system = ?"
507 " AND peer_id = ?"
508 " AND key = ?",
509 &plugin->delete_peerstoredata);
510
511 return GNUNET_OK; 513 return GNUNET_OK;
512} 514}
513 515
516
514/** 517/**
515 * Shutdown database connection and associate data 518 * Shutdown database connection and associate data
516 * structures. 519 * structures.
@@ -521,19 +524,20 @@ database_shutdown (struct Plugin *plugin)
521{ 524{
522 int result; 525 int result;
523 sqlite3_stmt *stmt; 526 sqlite3_stmt *stmt;
527
524 while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh, NULL))) 528 while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh, NULL)))
525 { 529 {
526 result = sqlite3_finalize (stmt); 530 result = sqlite3_finalize (stmt);
527 if (SQLITE_OK != result) 531 if (SQLITE_OK != result)
528 LOG (GNUNET_ERROR_TYPE_WARNING, 532 LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to close statement %p: %d\n",
529 "Failed to close statement %p: %d\n", stmt, result); 533 stmt, result);
530 } 534 }
531 if (SQLITE_OK != sqlite3_close (plugin->dbh)) 535 if (SQLITE_OK != sqlite3_close (plugin->dbh))
532 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close"); 536 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
533
534 GNUNET_free_non_null (plugin->fn); 537 GNUNET_free_non_null (plugin->fn);
535} 538}
536 539
540
537/** 541/**
538 * Entry point for the plugin. 542 * Entry point for the plugin.
539 * 543 *
@@ -561,10 +565,11 @@ libgnunet_plugin_peerstore_sqlite_init (void *cls)
561 api->store_record = &peerstore_sqlite_store_record; 565 api->store_record = &peerstore_sqlite_store_record;
562 api->iterate_records = &peerstore_sqlite_iterate_records; 566 api->iterate_records = &peerstore_sqlite_iterate_records;
563 api->expire_records = &peerstore_sqlite_expire_records; 567 api->expire_records = &peerstore_sqlite_expire_records;
564 LOG(GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is running\n"); 568 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is running\n");
565 return api; 569 return api;
566} 570}
567 571
572
568/** 573/**
569 * Exit point from the plugin. 574 * Exit point from the plugin.
570 * 575 *
@@ -582,7 +587,6 @@ libgnunet_plugin_peerstore_sqlite_done (void *cls)
582 GNUNET_free (api); 587 GNUNET_free (api);
583 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is finished\n"); 588 LOG (GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is finished\n");
584 return NULL; 589 return NULL;
585
586} 590}
587 591
588/* end of plugin_peerstore_sqlite.c */ 592/* end of plugin_peerstore_sqlite.c */