diff options
author | Christian Grothoff <christian@grothoff.org> | 2017-03-14 00:31:02 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2017-03-14 00:31:02 +0100 |
commit | 8d71f909cb22fbf6774e4042309a8eb133af3bfc (patch) | |
tree | 0efc19ac24a6521f927772364f1379154d2d0cad /src/peerstore/plugin_peerstore_sqlite.c | |
parent | 6e01dc4d91d072e8e58ba0140fb7f4ddd5109358 (diff) | |
download | gnunet-8d71f909cb22fbf6774e4042309a8eb133af3bfc.tar.gz gnunet-8d71f909cb22fbf6774e4042309a8eb133af3bfc.zip |
convert sqlite peerstore to using libgnunetsq
Diffstat (limited to 'src/peerstore/plugin_peerstore_sqlite.c')
-rw-r--r-- | src/peerstore/plugin_peerstore_sqlite.c | 357 |
1 files changed, 193 insertions, 164 deletions
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c index 236be129e..440263d44 100644 --- a/src/peerstore/plugin_peerstore_sqlite.c +++ b/src/peerstore/plugin_peerstore_sqlite.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | * This file is part of GNUnet | 2 | * This file is part of GNUnet |
3 | * Copyright (C) 2013 GNUnet e.V. | 3 | * Copyright (C) 2013, 2017 GNUnet e.V. |
4 | * | 4 | * |
5 | * GNUnet is free software; you can redistribute it and/or modify | 5 | * GNUnet is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published | 6 | * it under the terms of the GNU General Public License as published |
@@ -22,11 +22,13 @@ | |||
22 | * @file peerstore/plugin_peerstore_sqlite.c | 22 | * @file peerstore/plugin_peerstore_sqlite.c |
23 | * @brief sqlite-based peerstore backend | 23 | * @brief sqlite-based peerstore backend |
24 | * @author Omar Tarabai | 24 | * @author Omar Tarabai |
25 | * @author Christian Grothoff | ||
25 | */ | 26 | */ |
26 | 27 | ||
27 | #include "platform.h" | 28 | #include "platform.h" |
28 | #include "gnunet_peerstore_plugin.h" | 29 | #include "gnunet_peerstore_plugin.h" |
29 | #include "gnunet_peerstore_service.h" | 30 | #include "gnunet_peerstore_service.h" |
31 | #include "gnunet_sq_lib.h" | ||
30 | #include "peerstore.h" | 32 | #include "peerstore.h" |
31 | #include <sqlite3.h> | 33 | #include <sqlite3.h> |
32 | 34 | ||
@@ -111,6 +113,7 @@ struct Plugin | |||
111 | 113 | ||
112 | }; | 114 | }; |
113 | 115 | ||
116 | |||
114 | /** | 117 | /** |
115 | * Delete records with the given key | 118 | * Delete records with the given key |
116 | * | 119 | * |
@@ -118,40 +121,50 @@ struct Plugin | |||
118 | * @param sub_system name of sub system | 121 | * @param sub_system name of sub system |
119 | * @param peer Peer identity (can be NULL) | 122 | * @param peer Peer identity (can be NULL) |
120 | * @param key entry key string (can be NULL) | 123 | * @param key entry key string (can be NULL) |
121 | * @return number of deleted records | 124 | * @return number of deleted records, #GNUNE_SYSERR on error |
122 | */ | 125 | */ |
123 | static int | 126 | static int |
124 | peerstore_sqlite_delete_records (void *cls, const char *sub_system, | 127 | peerstore_sqlite_delete_records (void *cls, |
128 | const char *sub_system, | ||
125 | const struct GNUNET_PeerIdentity *peer, | 129 | const struct GNUNET_PeerIdentity *peer, |
126 | const char *key) | 130 | const char *key) |
127 | { | 131 | { |
128 | struct Plugin *plugin = cls; | 132 | struct Plugin *plugin = cls; |
129 | sqlite3_stmt *stmt = plugin->delete_peerstoredata; | 133 | sqlite3_stmt *stmt = plugin->delete_peerstoredata; |
134 | struct GNUNET_SQ_QueryParam params[] = { | ||
135 | GNUNET_SQ_query_param_string (sub_system), | ||
136 | GNUNET_SQ_query_param_auto_from_type (peer), | ||
137 | GNUNET_SQ_query_param_string (key), | ||
138 | GNUNET_SQ_query_param_end | ||
139 | }; | ||
140 | int ret; | ||
130 | 141 | ||
131 | if ((SQLITE_OK != | 142 | if (GNUNET_OK != |
132 | sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, | 143 | GNUNET_SQ_bind (stmt, |
133 | SQLITE_STATIC)) || | 144 | params)) |
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))) | ||
139 | { | 145 | { |
140 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 146 | LOG_SQLITE (plugin, |
147 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
141 | "sqlite3_bind"); | 148 | "sqlite3_bind"); |
149 | GNUNET_SQ_reset (plugin->dbh, | ||
150 | stmt); | ||
151 | return GNUNET_SYSERR; | ||
142 | } | 152 | } |
143 | else if (SQLITE_DONE != sqlite3_step (stmt)) | 153 | if (SQLITE_DONE != |
154 | sqlite3_step (stmt)) | ||
144 | { | 155 | { |
145 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 156 | LOG_SQLITE (plugin, |
157 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
146 | "sqlite3_step"); | 158 | "sqlite3_step"); |
159 | ret = GNUNET_SYSERR; | ||
147 | } | 160 | } |
148 | if (SQLITE_OK != sqlite3_reset (stmt)) | 161 | else |
149 | { | 162 | { |
150 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 163 | ret = sqlite3_changes (plugin->dbh); |
151 | "sqlite3_reset"); | ||
152 | return 0; | ||
153 | } | 164 | } |
154 | return sqlite3_changes (plugin->dbh); | 165 | GNUNET_SQ_reset (plugin->dbh, |
166 | stmt); | ||
167 | return ret; | ||
155 | } | 168 | } |
156 | 169 | ||
157 | 170 | ||
@@ -172,28 +185,36 @@ peerstore_sqlite_expire_records (void *cls, struct GNUNET_TIME_Absolute now, | |||
172 | { | 185 | { |
173 | struct Plugin *plugin = cls; | 186 | struct Plugin *plugin = cls; |
174 | sqlite3_stmt *stmt = plugin->expire_peerstoredata; | 187 | sqlite3_stmt *stmt = plugin->expire_peerstoredata; |
188 | struct GNUNET_SQ_QueryParam params[] = { | ||
189 | GNUNET_SQ_query_param_absolute_time (&now), | ||
190 | GNUNET_SQ_query_param_end | ||
191 | }; | ||
175 | 192 | ||
176 | if (SQLITE_OK != | 193 | if (GNUNET_OK != |
177 | sqlite3_bind_int64 (stmt, 1, (sqlite3_uint64) now.abs_value_us)) | 194 | GNUNET_SQ_bind (stmt, |
195 | params)) | ||
178 | { | 196 | { |
179 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 197 | LOG_SQLITE (plugin, |
198 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
180 | "sqlite3_bind"); | 199 | "sqlite3_bind"); |
200 | GNUNET_SQ_reset (plugin->dbh, | ||
201 | stmt); | ||
202 | return GNUNET_SYSERR; | ||
181 | } | 203 | } |
182 | else if (SQLITE_DONE != sqlite3_step (stmt)) | 204 | if (SQLITE_DONE != sqlite3_step (stmt)) |
183 | { | 205 | { |
184 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 206 | LOG_SQLITE (plugin, |
207 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
185 | "sqlite3_step"); | 208 | "sqlite3_step"); |
186 | } | 209 | GNUNET_SQ_reset (plugin->dbh, |
187 | if (SQLITE_OK != sqlite3_reset (stmt)) | 210 | stmt); |
188 | { | ||
189 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
190 | "sqlite3_reset"); | ||
191 | return GNUNET_SYSERR; | 211 | return GNUNET_SYSERR; |
192 | } | 212 | } |
193 | if (NULL != cont) | 213 | if (NULL != cont) |
194 | { | 214 | cont (cont_cls, |
195 | cont (cont_cls, sqlite3_changes (plugin->dbh)); | 215 | sqlite3_changes (plugin->dbh)); |
196 | } | 216 | GNUNET_SQ_reset (plugin->dbh, |
217 | stmt); | ||
197 | return GNUNET_OK; | 218 | return GNUNET_OK; |
198 | } | 219 | } |
199 | 220 | ||
@@ -224,94 +245,115 @@ peerstore_sqlite_iterate_records (void *cls, | |||
224 | sqlite3_stmt *stmt; | 245 | sqlite3_stmt *stmt; |
225 | int err = 0; | 246 | int err = 0; |
226 | int sret; | 247 | int sret; |
227 | struct GNUNET_PEERSTORE_Record *ret; | 248 | struct GNUNET_PEERSTORE_Record rec; |
228 | 249 | ||
229 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Executing iterate request on sqlite db.\n"); | 250 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
230 | if (NULL == peer && NULL == key) | 251 | "Executing iterate request on sqlite db.\n"); |
231 | { | 252 | if (NULL == peer) |
232 | stmt = plugin->select_peerstoredata; | ||
233 | err = | ||
234 | (SQLITE_OK != | ||
235 | sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, | ||
236 | SQLITE_STATIC)); | ||
237 | } | ||
238 | else if (NULL == key) | ||
239 | { | ||
240 | stmt = plugin->select_peerstoredata_by_pid; | ||
241 | err = | ||
242 | (SQLITE_OK != | ||
243 | sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, | ||
244 | SQLITE_STATIC)) || | ||
245 | (SQLITE_OK != | ||
246 | sqlite3_bind_blob (stmt, 2, peer, sizeof (struct GNUNET_PeerIdentity), | ||
247 | SQLITE_STATIC)); | ||
248 | } | ||
249 | else if (NULL == peer) | ||
250 | { | 253 | { |
251 | stmt = plugin->select_peerstoredata_by_key; | 254 | if (NULL == key) |
252 | err = | 255 | { |
253 | (SQLITE_OK != | 256 | struct GNUNET_SQ_QueryParam params[] = { |
254 | sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, | 257 | GNUNET_SQ_query_param_string (sub_system), |
255 | SQLITE_STATIC)) || | 258 | GNUNET_SQ_query_param_end |
256 | (SQLITE_OK != | 259 | }; |
257 | sqlite3_bind_text (stmt, 2, key, strlen (key) + 1, SQLITE_STATIC)); | 260 | |
261 | stmt = plugin->select_peerstoredata; | ||
262 | err = GNUNET_SQ_bind (stmt, | ||
263 | params); | ||
264 | } | ||
265 | else | ||
266 | { | ||
267 | struct GNUNET_SQ_QueryParam params[] = { | ||
268 | GNUNET_SQ_query_param_string (sub_system), | ||
269 | GNUNET_SQ_query_param_string (key), | ||
270 | GNUNET_SQ_query_param_end | ||
271 | }; | ||
272 | |||
273 | stmt = plugin->select_peerstoredata_by_key; | ||
274 | err = GNUNET_SQ_bind (stmt, | ||
275 | params); | ||
276 | } | ||
258 | } | 277 | } |
259 | else | 278 | else |
260 | { | 279 | { |
261 | stmt = plugin->select_peerstoredata_by_all; | 280 | if (NULL == key) |
262 | err = | 281 | { |
263 | (SQLITE_OK != | 282 | struct GNUNET_SQ_QueryParam params[] = { |
264 | sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, | 283 | GNUNET_SQ_query_param_string (sub_system), |
265 | SQLITE_STATIC)) || | 284 | GNUNET_SQ_query_param_auto_from_type (peer), |
266 | (SQLITE_OK != | 285 | GNUNET_SQ_query_param_end |
267 | sqlite3_bind_blob (stmt, 2, peer, sizeof (struct GNUNET_PeerIdentity), | 286 | }; |
268 | SQLITE_STATIC)) || | 287 | |
269 | (SQLITE_OK != | 288 | stmt = plugin->select_peerstoredata_by_pid; |
270 | sqlite3_bind_text (stmt, 3, key, strlen (key) + 1, SQLITE_STATIC)); | 289 | err = GNUNET_SQ_bind (stmt, |
290 | params); | ||
291 | } | ||
292 | else | ||
293 | { | ||
294 | struct GNUNET_SQ_QueryParam params[] = { | ||
295 | GNUNET_SQ_query_param_string (sub_system), | ||
296 | GNUNET_SQ_query_param_auto_from_type (peer), | ||
297 | GNUNET_SQ_query_param_string (key), | ||
298 | GNUNET_SQ_query_param_end | ||
299 | }; | ||
300 | |||
301 | stmt = plugin->select_peerstoredata_by_all; | ||
302 | err = GNUNET_SQ_bind (stmt, | ||
303 | params); | ||
304 | } | ||
271 | } | 305 | } |
272 | 306 | ||
273 | if (err) | 307 | if (GNUNET_OK != err) |
274 | { | 308 | { |
275 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 309 | LOG_SQLITE (plugin, |
310 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
276 | "sqlite3_bind_XXXX"); | 311 | "sqlite3_bind_XXXX"); |
277 | if (SQLITE_OK != sqlite3_reset (stmt)) | 312 | GNUNET_SQ_reset (plugin->dbh, |
278 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 313 | stmt); |
279 | "sqlite3_reset"); | ||
280 | return GNUNET_SYSERR; | 314 | return GNUNET_SYSERR; |
281 | } | 315 | } |
316 | |||
317 | err = 0; | ||
282 | while (SQLITE_ROW == (sret = sqlite3_step (stmt))) | 318 | while (SQLITE_ROW == (sret = sqlite3_step (stmt))) |
283 | { | 319 | { |
284 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Returning a matched record.\n"); | 320 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
285 | ret = GNUNET_new (struct GNUNET_PEERSTORE_Record); | 321 | "Returning a matched record.\n"); |
286 | 322 | struct GNUNET_SQ_ResultSpec rs[] = { | |
287 | ret->sub_system = (char *) sqlite3_column_text (stmt, 0); | 323 | GNUNET_SQ_result_spec_string (&rec.sub_system), |
288 | ret->peer = (struct GNUNET_PeerIdentity *) sqlite3_column_blob (stmt, 1); | 324 | GNUNET_SQ_result_spec_auto_from_type (&rec.peer), |
289 | ret->key = (char *) sqlite3_column_text (stmt, 2); | 325 | GNUNET_SQ_result_spec_string (&rec.key), |
290 | ret->value = (void *) sqlite3_column_blob (stmt, 3); | 326 | GNUNET_SQ_result_spec_variable_size (&rec.value, &rec.value_size), |
291 | ret->value_size = sqlite3_column_bytes (stmt, 3); | 327 | GNUNET_SQ_result_spec_absolute_time (&rec.expiry), |
292 | ret->expiry = GNUNET_new (struct GNUNET_TIME_Absolute); | 328 | GNUNET_SQ_result_spec_end |
293 | 329 | }; | |
294 | ret->expiry->abs_value_us = (uint64_t) sqlite3_column_int64 (stmt, 4); | 330 | |
331 | if (GNUNET_OK != | ||
332 | GNUNET_SQ_extract_result (stmt, | ||
333 | rs)) | ||
334 | { | ||
335 | GNUNET_break (0); | ||
336 | break; | ||
337 | } | ||
295 | if (NULL != iter) | 338 | if (NULL != iter) |
296 | iter (iter_cls, ret, NULL); | 339 | iter (iter_cls, |
297 | GNUNET_free (ret->expiry); | 340 | &rec, |
298 | GNUNET_free (ret); | 341 | NULL); |
342 | GNUNET_SQ_cleanup_result (rs); | ||
299 | } | 343 | } |
300 | if (SQLITE_DONE != sret) | 344 | if (SQLITE_DONE != sret) |
301 | { | 345 | { |
302 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); | 346 | LOG_SQLITE (plugin, |
303 | err = 1; | 347 | GNUNET_ERROR_TYPE_ERROR, |
304 | } | 348 | "sqlite_step"); |
305 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
306 | { | ||
307 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
308 | "sqlite3_reset"); | ||
309 | err = 1; | 349 | err = 1; |
310 | } | 350 | } |
351 | GNUNET_SQ_reset (plugin->dbh, | ||
352 | stmt); | ||
311 | if (NULL != iter) | 353 | if (NULL != iter) |
312 | { | 354 | iter (iter_cls, |
313 | iter (iter_cls, NULL, err ? "sqlite error" : NULL); | 355 | NULL, |
314 | } | 356 | err ? "sqlite error" : NULL); |
315 | return GNUNET_OK; | 357 | return GNUNET_OK; |
316 | } | 358 | } |
317 | 359 | ||
@@ -347,6 +389,14 @@ peerstore_sqlite_store_record (void *cls, | |||
347 | { | 389 | { |
348 | struct Plugin *plugin = cls; | 390 | struct Plugin *plugin = cls; |
349 | sqlite3_stmt *stmt = plugin->insert_peerstoredata; | 391 | sqlite3_stmt *stmt = plugin->insert_peerstoredata; |
392 | struct GNUNET_SQ_QueryParam params[] = { | ||
393 | GNUNET_SQ_query_param_string (sub_system), | ||
394 | GNUNET_SQ_query_param_auto_from_type (peer), | ||
395 | GNUNET_SQ_query_param_string (key), | ||
396 | GNUNET_SQ_query_param_fixed_size (value, size), | ||
397 | GNUNET_SQ_query_param_absolute_time (&expiry), | ||
398 | GNUNET_SQ_query_param_end | ||
399 | }; | ||
350 | 400 | ||
351 | if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) | 401 | if (GNUNET_PEERSTORE_STOREOPTION_REPLACE == options) |
352 | { | 402 | { |
@@ -355,34 +405,23 @@ peerstore_sqlite_store_record (void *cls, | |||
355 | peer, | 405 | peer, |
356 | key); | 406 | key); |
357 | } | 407 | } |
358 | if (SQLITE_OK != | 408 | if (GNUNET_OK != |
359 | sqlite3_bind_text (stmt, 1, sub_system, strlen (sub_system) + 1, | 409 | GNUNET_SQ_bind (stmt, |
360 | SQLITE_STATIC) || | 410 | params)) |
361 | SQLITE_OK != sqlite3_bind_blob (stmt, 2, peer, | 411 | LOG_SQLITE (plugin, |
362 | sizeof (struct GNUNET_PeerIdentity), | 412 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, |
363 | SQLITE_STATIC) || | ||
364 | SQLITE_OK != sqlite3_bind_text (stmt, 3, key, strlen (key) + 1, | ||
365 | SQLITE_STATIC) || | ||
366 | SQLITE_OK != sqlite3_bind_blob (stmt, 4, value, size, SQLITE_STATIC) || | ||
367 | SQLITE_OK != sqlite3_bind_int64 (stmt, 5, | ||
368 | (sqlite3_uint64) expiry.abs_value_us)) | ||
369 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
370 | "sqlite3_bind"); | 413 | "sqlite3_bind"); |
371 | else if (SQLITE_DONE != sqlite3_step (stmt)) | 414 | else if (SQLITE_DONE != sqlite3_step (stmt)) |
372 | { | 415 | { |
373 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | 416 | LOG_SQLITE (plugin, |
417 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
374 | "sqlite3_step"); | 418 | "sqlite3_step"); |
375 | } | 419 | } |
376 | if (SQLITE_OK != sqlite3_reset (stmt)) | 420 | GNUNET_SQ_reset (plugin->dbh, |
377 | { | 421 | stmt); |
378 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
379 | "sqlite3_reset"); | ||
380 | return GNUNET_SYSERR; | ||
381 | } | ||
382 | if (NULL != cont) | 422 | if (NULL != cont) |
383 | { | 423 | cont (cont_cls, |
384 | cont (cont_cls, GNUNET_OK); | 424 | GNUNET_OK); |
385 | } | ||
386 | return GNUNET_OK; | 425 | return GNUNET_OK; |
387 | } | 426 | } |
388 | 427 | ||
@@ -454,24 +493,6 @@ sql_prepare (sqlite3 *dbh, | |||
454 | 493 | ||
455 | 494 | ||
456 | /** | 495 | /** |
457 | * sqlite3 custom function for comparison of uint64_t values | ||
458 | * since it is not supported by default | ||
459 | */ | ||
460 | static void | ||
461 | sqlite3_lessthan (sqlite3_context *ctx, | ||
462 | int dummy, | ||
463 | sqlite3_value **values) | ||
464 | { | ||
465 | uint64_t v1; | ||
466 | uint64_t v2; | ||
467 | |||
468 | v1 = (uint64_t) sqlite3_value_int64 (values[0]); | ||
469 | v2 = (uint64_t) sqlite3_value_int64 (values[1]); | ||
470 | sqlite3_result_int (ctx, v1 < v2); | ||
471 | } | ||
472 | |||
473 | |||
474 | /** | ||
475 | * Initialize the database connections and associated | 496 | * Initialize the database connections and associated |
476 | * data structures (create tables and indices | 497 | * data structures (create tables and indices |
477 | * as needed as well). | 498 | * as needed as well). |
@@ -532,17 +553,11 @@ database_setup (struct Plugin *plugin) | |||
532 | /* Create tables */ | 553 | /* Create tables */ |
533 | sql_exec (plugin->dbh, | 554 | sql_exec (plugin->dbh, |
534 | "CREATE TABLE IF NOT EXISTS peerstoredata (\n" | 555 | "CREATE TABLE IF NOT EXISTS peerstoredata (\n" |
535 | " sub_system TEXT NOT NULL,\n" " peer_id BLOB NOT NULL,\n" | 556 | " sub_system TEXT NOT NULL,\n" |
536 | " key TEXT NOT NULL,\n" " value BLOB NULL,\n" | 557 | " peer_id BLOB NOT NULL,\n" |
537 | " expiry sqlite3_uint64 NOT NULL" ");"); | 558 | " key TEXT NOT NULL,\n" |
538 | sqlite3_create_function (plugin->dbh, | 559 | " value BLOB NULL,\n" |
539 | "UINT64_LT", | 560 | " expiry INT8 NOT NULL" ");"); |
540 | 2, | ||
541 | SQLITE_UTF8, | ||
542 | NULL, | ||
543 | &sqlite3_lessthan, | ||
544 | NULL, | ||
545 | NULL); | ||
546 | /* Create Indices */ | 561 | /* Create Indices */ |
547 | if (SQLITE_OK != | 562 | if (SQLITE_OK != |
548 | sqlite3_exec (plugin->dbh, | 563 | sqlite3_exec (plugin->dbh, |
@@ -559,26 +574,35 @@ database_setup (struct Plugin *plugin) | |||
559 | /* Prepare statements */ | 574 | /* Prepare statements */ |
560 | 575 | ||
561 | sql_prepare (plugin->dbh, | 576 | sql_prepare (plugin->dbh, |
562 | "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry) VALUES (?,?,?,?,?);", | 577 | "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry)" |
578 | " VALUES (?,?,?,?,?);", | ||
563 | &plugin->insert_peerstoredata); | 579 | &plugin->insert_peerstoredata); |
564 | sql_prepare (plugin->dbh, | 580 | sql_prepare (plugin->dbh, |
565 | "SELECT * FROM peerstoredata" " WHERE sub_system = ?", | 581 | "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata" |
582 | " WHERE sub_system = ?", | ||
566 | &plugin->select_peerstoredata); | 583 | &plugin->select_peerstoredata); |
567 | sql_prepare (plugin->dbh, | 584 | sql_prepare (plugin->dbh, |
568 | "SELECT * FROM peerstoredata" " WHERE sub_system = ?" | 585 | "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata" |
569 | " AND peer_id = ?", &plugin->select_peerstoredata_by_pid); | 586 | " WHERE sub_system = ?" |
587 | " AND peer_id = ?", | ||
588 | &plugin->select_peerstoredata_by_pid); | ||
570 | sql_prepare (plugin->dbh, | 589 | sql_prepare (plugin->dbh, |
571 | "SELECT * FROM peerstoredata" " WHERE sub_system = ?" | 590 | "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata" |
572 | " AND key = ?", &plugin->select_peerstoredata_by_key); | 591 | " WHERE sub_system = ?" |
592 | " AND key = ?", | ||
593 | &plugin->select_peerstoredata_by_key); | ||
573 | sql_prepare (plugin->dbh, | 594 | sql_prepare (plugin->dbh, |
574 | "SELECT * FROM peerstoredata" " WHERE sub_system = ?" | 595 | "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata" |
596 | " WHERE sub_system = ?" | ||
575 | " AND peer_id = ?" " AND key = ?", | 597 | " AND peer_id = ?" " AND key = ?", |
576 | &plugin->select_peerstoredata_by_all); | 598 | &plugin->select_peerstoredata_by_all); |
577 | sql_prepare (plugin->dbh, | 599 | sql_prepare (plugin->dbh, |
578 | "DELETE FROM peerstoredata" " WHERE UINT64_LT(expiry, ?)", | 600 | "DELETE FROM peerstoredata" |
601 | " WHERE expiry < ?", | ||
579 | &plugin->expire_peerstoredata); | 602 | &plugin->expire_peerstoredata); |
580 | sql_prepare (plugin->dbh, | 603 | sql_prepare (plugin->dbh, |
581 | "DELETE FROM peerstoredata" " WHERE sub_system = ?" | 604 | "DELETE FROM peerstoredata" |
605 | " WHERE sub_system = ?" | ||
582 | " AND peer_id = ?" " AND key = ?", | 606 | " AND peer_id = ?" " AND key = ?", |
583 | &plugin->delete_peerstoredata); | 607 | &plugin->delete_peerstoredata); |
584 | return GNUNET_OK; | 608 | return GNUNET_OK; |
@@ -596,15 +620,20 @@ database_shutdown (struct Plugin *plugin) | |||
596 | int result; | 620 | int result; |
597 | sqlite3_stmt *stmt; | 621 | sqlite3_stmt *stmt; |
598 | 622 | ||
599 | while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh, NULL))) | 623 | while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh, |
624 | NULL))) | ||
600 | { | 625 | { |
601 | result = sqlite3_finalize (stmt); | 626 | result = sqlite3_finalize (stmt); |
602 | if (SQLITE_OK != result) | 627 | if (SQLITE_OK != result) |
603 | LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to close statement %p: %d\n", | 628 | LOG (GNUNET_ERROR_TYPE_WARNING, |
604 | stmt, result); | 629 | "Failed to close statement %p: %d\n", |
630 | stmt, | ||
631 | result); | ||
605 | } | 632 | } |
606 | if (SQLITE_OK != sqlite3_close (plugin->dbh)) | 633 | if (SQLITE_OK != sqlite3_close (plugin->dbh)) |
607 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close"); | 634 | LOG_SQLITE (plugin, |
635 | GNUNET_ERROR_TYPE_ERROR, | ||
636 | "sqlite3_close"); | ||
608 | GNUNET_free_non_null (plugin->fn); | 637 | GNUNET_free_non_null (plugin->fn); |
609 | } | 638 | } |
610 | 639 | ||