diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-12-19 18:22:20 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-12-19 18:22:20 +0000 |
commit | 15de71a29f22caee6b15956b162e1e5a0f5f4f3e (patch) | |
tree | 9118b73d3253af3cec9fff3a5599dca966a02ec4 /src/datastore/plugin_datastore_sqlite.c | |
parent | fcfd24ac2947d5129307614e85f6fbba45104530 (diff) | |
download | gnunet-15de71a29f22caee6b15956b162e1e5a0f5f4f3e.tar.gz gnunet-15de71a29f22caee6b15956b162e1e5a0f5f4f3e.zip |
LRN patch from SVN 1630
Diffstat (limited to 'src/datastore/plugin_datastore_sqlite.c')
-rw-r--r-- | src/datastore/plugin_datastore_sqlite.c | 108 |
1 files changed, 88 insertions, 20 deletions
diff --git a/src/datastore/plugin_datastore_sqlite.c b/src/datastore/plugin_datastore_sqlite.c index 457f75c45..1b8382537 100644 --- a/src/datastore/plugin_datastore_sqlite.c +++ b/src/datastore/plugin_datastore_sqlite.c | |||
@@ -28,7 +28,7 @@ | |||
28 | #include "gnunet_datastore_plugin.h" | 28 | #include "gnunet_datastore_plugin.h" |
29 | #include <sqlite3.h> | 29 | #include <sqlite3.h> |
30 | 30 | ||
31 | #define DEBUG_SQLITE GNUNET_YES | 31 | #define DEBUG_SQLITE GNUNET_NO |
32 | 32 | ||
33 | 33 | ||
34 | /** | 34 | /** |
@@ -105,6 +105,11 @@ struct Plugin | |||
105 | sqlite3 *dbh; | 105 | sqlite3 *dbh; |
106 | 106 | ||
107 | /** | 107 | /** |
108 | * Precompiled SQL for deletion. | ||
109 | */ | ||
110 | sqlite3_stmt *delRow; | ||
111 | |||
112 | /** | ||
108 | * Precompiled SQL for update. | 113 | * Precompiled SQL for update. |
109 | */ | 114 | */ |
110 | sqlite3_stmt *updPrio; | 115 | sqlite3_stmt *updPrio; |
@@ -145,9 +150,16 @@ sq_prepare (sqlite3 * dbh, const char *zSql, | |||
145 | sqlite3_stmt ** ppStmt) | 150 | sqlite3_stmt ** ppStmt) |
146 | { | 151 | { |
147 | char *dummy; | 152 | char *dummy; |
148 | return sqlite3_prepare_v2 (dbh, | 153 | int result; |
149 | zSql, | 154 | result = sqlite3_prepare_v2 (dbh, |
150 | strlen (zSql), ppStmt, (const char **) &dummy); | 155 | zSql, |
156 | strlen (zSql), ppStmt, (const char **) &dummy); | ||
157 | #if DEBUG_SQLITE | ||
158 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
159 | "sqlite", | ||
160 | "Prepared %p: %d\n", *ppStmt, result); | ||
161 | #endif | ||
162 | return result; | ||
151 | } | 163 | } |
152 | 164 | ||
153 | 165 | ||
@@ -324,12 +336,16 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
324 | "INSERT INTO gn080 (size, type, prio, " | 336 | "INSERT INTO gn080 (size, type, prio, " |
325 | "anonLevel, expire, hash, vhash, value) VALUES " | 337 | "anonLevel, expire, hash, vhash, value) VALUES " |
326 | "(?, ?, ?, ?, ?, ?, ?, ?)", | 338 | "(?, ?, ?, ?, ?, ?, ?, ?)", |
327 | &plugin->insertContent) != SQLITE_OK)) | 339 | &plugin->insertContent) != SQLITE_OK) || |
340 | (sq_prepare (plugin->dbh, | ||
341 | "DELETE FROM gn080 WHERE _ROWID_ = ?", | ||
342 | &plugin->delRow) != SQLITE_OK)) | ||
328 | { | 343 | { |
329 | LOG_SQLITE (plugin, NULL, | 344 | LOG_SQLITE (plugin, NULL, |
330 | GNUNET_ERROR_TYPE_ERROR, "precompiling"); | 345 | GNUNET_ERROR_TYPE_ERROR, "precompiling"); |
331 | return GNUNET_SYSERR; | 346 | return GNUNET_SYSERR; |
332 | } | 347 | } |
348 | |||
333 | return GNUNET_OK; | 349 | return GNUNET_OK; |
334 | } | 350 | } |
335 | 351 | ||
@@ -342,11 +358,36 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg, | |||
342 | static void | 358 | static void |
343 | database_shutdown (struct Plugin *plugin) | 359 | database_shutdown (struct Plugin *plugin) |
344 | { | 360 | { |
361 | int result; | ||
362 | if (plugin->delRow != NULL) | ||
363 | sqlite3_finalize (plugin->delRow); | ||
345 | if (plugin->updPrio != NULL) | 364 | if (plugin->updPrio != NULL) |
346 | sqlite3_finalize (plugin->updPrio); | 365 | sqlite3_finalize (plugin->updPrio); |
347 | if (plugin->insertContent != NULL) | 366 | if (plugin->insertContent != NULL) |
348 | sqlite3_finalize (plugin->insertContent); | 367 | sqlite3_finalize (plugin->insertContent); |
349 | sqlite3_close (plugin->dbh); | 368 | result = sqlite3_close(plugin->dbh); |
369 | while (result == SQLITE_BUSY) | ||
370 | { | ||
371 | sqlite3_stmt *stmt; | ||
372 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
373 | "sqlite", | ||
374 | _("Tried to close sqlite without finalizing all prepared statements.\n")); | ||
375 | for (stmt = sqlite3_next_stmt(plugin->dbh, NULL); stmt != NULL; stmt = sqlite3_next_stmt(plugin->dbh, NULL)) | ||
376 | { | ||
377 | #if DEBUG_SQLITE | ||
378 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
379 | "sqlite", "Closing statement %p\n", stmt); | ||
380 | #endif | ||
381 | result = sqlite3_finalize(stmt); | ||
382 | #if DEBUG_SQLITE | ||
383 | if (result != SQLITE_OK) | ||
384 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
385 | "sqlite", | ||
386 | "Failed to close statement %p: %d\n", stmt, result); | ||
387 | #endif | ||
388 | } | ||
389 | result = sqlite3_close(plugin->dbh); | ||
390 | } | ||
350 | GNUNET_free_non_null (plugin->fn); | 391 | GNUNET_free_non_null (plugin->fn); |
351 | } | 392 | } |
352 | 393 | ||
@@ -362,26 +403,23 @@ static int | |||
362 | delete_by_rowid (struct Plugin* plugin, | 403 | delete_by_rowid (struct Plugin* plugin, |
363 | unsigned long long rid) | 404 | unsigned long long rid) |
364 | { | 405 | { |
365 | sqlite3_stmt *stmt; | ||
366 | 406 | ||
367 | if (sq_prepare (plugin->dbh, | 407 | sqlite3_bind_int64 (plugin->delRow, 1, rid); |
368 | "DELETE FROM gn080 WHERE _ROWID_ = ?", &stmt) != SQLITE_OK) | 408 | if (SQLITE_DONE != sqlite3_step (plugin->delRow)) |
369 | { | 409 | { |
370 | LOG_SQLITE (plugin, NULL, | 410 | LOG_SQLITE (plugin, NULL, |
371 | GNUNET_ERROR_TYPE_ERROR | | 411 | GNUNET_ERROR_TYPE_ERROR | |
372 | GNUNET_ERROR_TYPE_BULK, "sq_prepare"); | 412 | GNUNET_ERROR_TYPE_BULK, "sqlite3_step"); |
413 | if (SQLITE_OK != sqlite3_reset (plugin->delRow)) | ||
414 | LOG_SQLITE (plugin, NULL, | ||
415 | GNUNET_ERROR_TYPE_ERROR | | ||
416 | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); | ||
373 | return GNUNET_SYSERR; | 417 | return GNUNET_SYSERR; |
374 | } | 418 | } |
375 | sqlite3_bind_int64 (stmt, 1, rid); | 419 | if (SQLITE_OK != sqlite3_reset (plugin->delRow)) |
376 | if (SQLITE_DONE != sqlite3_step (stmt)) | ||
377 | { | ||
378 | LOG_SQLITE (plugin, NULL, | 420 | LOG_SQLITE (plugin, NULL, |
379 | GNUNET_ERROR_TYPE_ERROR | | 421 | GNUNET_ERROR_TYPE_ERROR | |
380 | GNUNET_ERROR_TYPE_BULK, "sqlite3_step"); | 422 | GNUNET_ERROR_TYPE_BULK, "sqlite3_reset"); |
381 | sqlite3_finalize (stmt); | ||
382 | return GNUNET_SYSERR; | ||
383 | } | ||
384 | sqlite3_finalize (stmt); | ||
385 | return GNUNET_OK; | 423 | return GNUNET_OK; |
386 | } | 424 | } |
387 | 425 | ||
@@ -1167,7 +1205,7 @@ sqlite_plugin_iter_migration_order (void *cls, | |||
1167 | * Call sqlite using the already prepared query to get | 1205 | * Call sqlite using the already prepared query to get |
1168 | * the next result. | 1206 | * the next result. |
1169 | * | 1207 | * |
1170 | * @param cls not used | 1208 | * @param cls context with the prepared query |
1171 | * @param nc context with the prepared query | 1209 | * @param nc context with the prepared query |
1172 | * @return GNUNET_OK on success, GNUNET_SYSERR on error, GNUNET_NO if | 1210 | * @return GNUNET_OK on success, GNUNET_SYSERR on error, GNUNET_NO if |
1173 | * there are no more results | 1211 | * there are no more results |
@@ -1185,6 +1223,10 @@ all_next_prepare (void *cls, | |||
1185 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | 1223 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, |
1186 | "Asked to clean up iterator state.\n"); | 1224 | "Asked to clean up iterator state.\n"); |
1187 | #endif | 1225 | #endif |
1226 | nc = (struct NextContext *)cls; | ||
1227 | if (nc->stmt) | ||
1228 | sqlite3_finalize (nc->stmt); | ||
1229 | nc->stmt = NULL; | ||
1188 | return GNUNET_SYSERR; | 1230 | return GNUNET_SYSERR; |
1189 | } | 1231 | } |
1190 | plugin = nc->plugin; | 1232 | plugin = nc->plugin; |
@@ -1241,7 +1283,7 @@ sqlite_plugin_iter_all_now (void *cls, | |||
1241 | nc->iter_cls = iter_cls; | 1283 | nc->iter_cls = iter_cls; |
1242 | nc->stmt = stmt; | 1284 | nc->stmt = stmt; |
1243 | nc->prep = &all_next_prepare; | 1285 | nc->prep = &all_next_prepare; |
1244 | nc->prep_cls = NULL; | 1286 | nc->prep_cls = nc; |
1245 | sqlite_next_request (nc, GNUNET_NO); | 1287 | sqlite_next_request (nc, GNUNET_NO); |
1246 | } | 1288 | } |
1247 | 1289 | ||
@@ -1595,10 +1637,26 @@ libgnunet_plugin_datastore_sqlite_done (void *cls) | |||
1595 | struct GNUNET_DATASTORE_PluginFunctions *api = cls; | 1637 | struct GNUNET_DATASTORE_PluginFunctions *api = cls; |
1596 | struct Plugin *plugin = api->cls; | 1638 | struct Plugin *plugin = api->cls; |
1597 | 1639 | ||
1640 | #if DEBUG_SQLITE | ||
1641 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
1642 | "sqlite", | ||
1643 | "sqlite plugin is doneing\n"); | ||
1644 | #endif | ||
1645 | |||
1598 | if (plugin->next_task != GNUNET_SCHEDULER_NO_TASK) | 1646 | if (plugin->next_task != GNUNET_SCHEDULER_NO_TASK) |
1599 | { | 1647 | { |
1648 | #if DEBUG_SQLITE | ||
1649 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
1650 | "sqlite", | ||
1651 | "Canceling next task\n"); | ||
1652 | #endif | ||
1600 | GNUNET_SCHEDULER_cancel (plugin->next_task); | 1653 | GNUNET_SCHEDULER_cancel (plugin->next_task); |
1601 | plugin->next_task = GNUNET_SCHEDULER_NO_TASK; | 1654 | plugin->next_task = GNUNET_SCHEDULER_NO_TASK; |
1655 | #if DEBUG_SQLITE | ||
1656 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
1657 | "sqlite", | ||
1658 | "Prep'ing next task\n"); | ||
1659 | #endif | ||
1602 | plugin->next_task_nc->prep (plugin->next_task_nc->prep_cls, NULL); | 1660 | plugin->next_task_nc->prep (plugin->next_task_nc->prep_cls, NULL); |
1603 | GNUNET_free (plugin->next_task_nc); | 1661 | GNUNET_free (plugin->next_task_nc); |
1604 | plugin->next_task_nc = NULL; | 1662 | plugin->next_task_nc = NULL; |
@@ -1606,6 +1664,11 @@ libgnunet_plugin_datastore_sqlite_done (void *cls) | |||
1606 | fn = NULL; | 1664 | fn = NULL; |
1607 | if (plugin->drop_on_shutdown) | 1665 | if (plugin->drop_on_shutdown) |
1608 | fn = GNUNET_strdup (plugin->fn); | 1666 | fn = GNUNET_strdup (plugin->fn); |
1667 | #if DEBUG_SQLITE | ||
1668 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
1669 | "sqlite", | ||
1670 | "Shutting down database\n"); | ||
1671 | #endif | ||
1609 | database_shutdown (plugin); | 1672 | database_shutdown (plugin); |
1610 | plugin->env = NULL; | 1673 | plugin->env = NULL; |
1611 | GNUNET_free (api); | 1674 | GNUNET_free (api); |
@@ -1617,6 +1680,11 @@ libgnunet_plugin_datastore_sqlite_done (void *cls) | |||
1617 | fn); | 1680 | fn); |
1618 | GNUNET_free (fn); | 1681 | GNUNET_free (fn); |
1619 | } | 1682 | } |
1683 | #if DEBUG_SQLITE | ||
1684 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
1685 | "sqlite", | ||
1686 | "sqlite plugin is finished doneing\n"); | ||
1687 | #endif | ||
1620 | return NULL; | 1688 | return NULL; |
1621 | } | 1689 | } |
1622 | 1690 | ||