2 This file is part of GNUnet
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file datastore/plugin_datastore_mysql.c
23 * @brief mysql-based datastore backend
24 * @author Igor Wronsky
25 * @author Christian Grothoff
27 * NOTE: This db module does NOT work with mysql prior to 4.1 since
28 * it uses prepared statements. MySQL 5.0.46 promises to fix a bug
29 * in MyISAM that is causing us grief. At the time of this writing,
30 * that version is yet to be released. In anticipation, the code
31 * will use MyISAM with 5.0.46 (and higher). If you run such a
32 * version, please run "make check" to verify that the MySQL bug
33 * was actually fixed in your version (and if not, change the
34 * code below to use MyISAM for gn071).
39 * + On up-to-date hardware where mysql can be used comfortably, this
40 * module will have better performance than the other db choices
41 * (according to our tests).
42 * + Its often possible to recover the mysql database from internal
43 * inconsistencies. The other db choices do not support repair!
45 * - Memory usage (Comment: "I have 1G and it never caused me trouble")
48 * MANUAL SETUP INSTRUCTIONS
50 * 1) in /etc/gnunet.conf, set
55 * 2) Then access mysql as root,
59 * and do the following. [You should replace $USER with the username
60 * that will be running the gnunetd process].
62 CREATE DATABASE gnunet;
63 GRANT select,insert,update,delete,create,alter,drop,create temporary tables
64 ON gnunet.* TO $USER@localhost;
65 SET PASSWORD FOR $USER@localhost=PASSWORD('$the_password_you_like');
68 * 3) In the $HOME directory of $USER, create a ".my.cnf" file
69 * with the following lines
73 password=$the_password_you_like
76 * Thats it. Note that .my.cnf file is a security risk unless its on
77 * a safe partition etc. The $HOME/.my.cnf can of course be a symbolic
78 * link. Even greater security risk can be achieved by setting no
79 * password for $USER. Luckily $USER has only priviledges to mess
80 * up GNUnet's tables, nothing else (unless you give him more,
83 * 4) Still, perhaps you should briefly try if the DB connection
84 * works. First, login as $USER. Then use,
87 $ mysql -u $USER -p $the_password_you_like
91 * If you get the message "Database changed" it probably works.
93 * [If you get "ERROR 2002: Can't connect to local MySQL server
94 * through socket '/tmp/mysql.sock' (2)" it may be resolvable by
95 * "ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock"
96 * so there may be some additional trouble depending on your mysql setup.]
100 * - Its probably healthy to check your tables for inconsistencies
101 * every now and then.
102 * - If you get odd SEGVs on gnunetd startup, it might be that the mysql
103 * databases have been corrupted.
104 * - The tables can be verified/fixed in two ways;
105 * 1) by running mysqlcheck -A, or
106 * 2) by executing (inside of mysql using the GNUnet database):
108 mysql> REPAIR TABLE gn090;
113 * If you have problems related to the mysql module, your best
114 * friend is probably the mysql manual. The first thing to check
115 * is that mysql is basically operational, that you can connect
116 * to it, create tables, issue queries etc.
119 #include "platform.h"
120 #include "gnunet_datastore_plugin.h"
121 #include "gnunet_util_lib.h"
122 #include <mysql/mysql.h>
124 #define DEBUG_MYSQL GNUNET_NO
126 #define MAX_DATUM_SIZE 65536
129 * Maximum number of supported parameters for a prepared
130 * statement. Increase if needed.
135 * Die with an error message that indicates
136 * a failure of the command 'cmd' with the message given
137 * by strerror(errno).
139 #define DIE_MYSQL(cmd, dbh) do { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, mysql_error((dbh)->dbf)); abort(); } while(0);
142 * Log an error message at log-level 'level' that indicates
143 * a failure of the command 'cmd' on file 'filename'
144 * with the message given by strerror(errno).
146 #define LOG_MYSQL(level, cmd, dbh) do { GNUNET_log(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, mysql_error((dbh)->dbf)); } while(0);
149 struct GNUNET_MysqlStatementHandle
151 struct GNUNET_MysqlStatementHandle *next;
153 struct GNUNET_MysqlStatementHandle *prev;
157 MYSQL_STMT *statement;
164 * Context for the universal iterator.
166 struct NextRequestClosure;
169 * Type of a function that will prepare
170 * the next iteration.
173 * @param nc the next context; NULL for the last
174 * call which gives the callback a chance to
175 * clean up the closure
176 * @return GNUNET_OK on success, GNUNET_NO if there are
177 * no more values, GNUNET_SYSERR on error
179 typedef int (*PrepareFunction)(void *cls,
180 struct NextRequestClosure *nc);
183 struct NextRequestClosure
185 struct Plugin *plugin;
187 struct GNUNET_TIME_Absolute now;
190 * Function to call to prepare the next
193 PrepareFunction prep;
202 enum GNUNET_BLOCK_Type type;
204 PluginIterator dviter;
215 * Context for all functions in this plugin.
220 * Our execution environment.
222 struct GNUNET_DATASTORE_PluginEnvironment *env;
225 * Handle to talk to MySQL.
230 * We keep all prepared statements in a DLL. This is the head.
232 struct GNUNET_MysqlStatementHandle *shead;
235 * We keep all prepared statements in a DLL. This is the tail.
237 struct GNUNET_MysqlStatementHandle *stail;
240 * Filename of "my.cnf" (msyql configuration).
245 * Closure of the 'next_task' (must be freed if 'next_task' is cancelled).
247 struct NextRequestClosure *next_task_nc;
250 * Pending task with scheduler for running the next request.
252 GNUNET_SCHEDULER_TaskIdentifier next_task;
255 * Prepared statements.
257 #define INSERT_ENTRY "INSERT INTO gn090 (repl,type,prio,anonLevel,expire,hash,vhash,value) VALUES (?,?,?,?,?,?,?,?)"
258 struct GNUNET_MysqlStatementHandle *insert_entry;
260 #define DELETE_ENTRY_BY_UID "DELETE FROM gn090 WHERE uid=?"
261 struct GNUNET_MysqlStatementHandle *delete_entry_by_uid;
263 #define COUNT_ENTRY_BY_HASH "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash) WHERE hash=?"
264 struct GNUNET_MysqlStatementHandle *count_entry_by_hash;
266 #define SELECT_ENTRY_BY_HASH "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_uid) WHERE hash=? ORDER BY uid LIMIT 1 OFFSET ?"
267 struct GNUNET_MysqlStatementHandle *select_entry_by_hash;
269 #define COUNT_ENTRY_BY_HASH_AND_VHASH "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=?"
270 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_vhash;
272 #define SELECT_ENTRY_BY_HASH_AND_VHASH "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? ORDER BY uid LIMIT 1 OFFSET ?"
273 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_vhash;
275 #define COUNT_ENTRY_BY_HASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_type_uid) WHERE hash=? AND type=?"
276 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_type;
278 #define SELECT_ENTRY_BY_HASH_AND_TYPE "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_type_uid) WHERE hash=? AND type=? ORDER BY uid LIMIT 1 OFFSET ?"
279 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_type;
281 #define COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? AND type=?"
282 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_vhash_and_type;
284 #define SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? AND type=? ORDER BY uid ASC LIMIT 1 OFFSET ?"
285 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_vhash_and_type;
287 #define UPDATE_ENTRY "UPDATE gn090 SET prio=prio+?,expire=IF(expire>=?,expire,?) WHERE uid=? LIMIT 1"
288 struct GNUNET_MysqlStatementHandle *update_entry;
290 #define SELECT_SIZE "SELECT SUM(BIT_LENGTH(value) DIV 8) FROM gn090"
291 struct GNUNET_MysqlStatementHandle *get_size;
293 #define SELECT_IT_NON_ANONYMOUS "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX(idx_anonLevel_uid) WHERE anonLevel=0 ORDER BY uid DESC LIMIT 1 OFFSET ?"
294 struct GNUNET_MysqlStatementHandle *zero_iter;
296 #define SELECT_IT_EXPIRATION "(SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX(idx_expire_prio) WHERE expire < ? ORDER BY prio ASC LIMIT 1) "\
298 "(SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX(idx_prio) ORDER BY prio ASC LIMIT 1) "\
299 "ORDER BY expire ASC LIMIT 1"
300 struct GNUNET_MysqlStatementHandle *select_expiration;
302 #define SELECT_IT_REPLICATION "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX(idx_repl) ORDER BY repl DESC,RAND() LIMIT 1"
303 struct GNUNET_MysqlStatementHandle *select_replication;
309 * Obtain the location of ".my.cnf".
311 * @param cfg our configuration
312 * @return NULL on error
315 get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg)
326 pw = getpwuid (getuid ());
329 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
334 GNUNET_CONFIGURATION_have_value (cfg,
335 "datastore-mysql", "CONFIG"))
337 GNUNET_assert (GNUNET_OK ==
338 GNUNET_CONFIGURATION_get_value_filename (cfg,
339 "datastore-mysql", "CONFIG", &cnffile));
340 configured = GNUNET_YES;
344 home_dir = GNUNET_strdup (pw->pw_dir);
346 home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
347 plibc_conv_to_win_path ("~/", home_dir);
349 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
350 GNUNET_free (home_dir);
351 configured = GNUNET_NO;
353 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
354 _("Trying to use file `%s' for MySQL configuration.\n"),
356 if ((0 != STAT (cnffile, &st)) ||
357 (0 != ACCESS (cnffile, R_OK)) || (!S_ISREG (st.st_mode)))
359 if (configured == GNUNET_YES)
360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
361 _("Could not access file `%s': %s\n"), cnffile,
363 GNUNET_free (cnffile);
372 * Free a prepared statement.
374 * @param plugin plugin context
375 * @param s prepared statement
378 prepared_statement_destroy (struct Plugin *plugin,
379 struct GNUNET_MysqlStatementHandle
382 GNUNET_CONTAINER_DLL_remove (plugin->shead,
386 mysql_stmt_close (s->statement);
387 GNUNET_free (s->query);
393 * Close database connection and all prepared statements (we got a DB
397 iclose (struct Plugin *plugin)
399 struct GNUNET_MysqlStatementHandle *spos;
401 spos = plugin->shead;
402 while (NULL != plugin->shead)
403 prepared_statement_destroy (plugin,
405 if (plugin->dbf != NULL)
407 mysql_close (plugin->dbf);
415 * Open the connection with the database (and initialize
416 * our default options).
418 * @return GNUNET_OK on success
421 iopen (struct Plugin *ret)
426 char *mysql_password;
427 unsigned long long mysql_port;
429 unsigned int timeout;
431 ret->dbf = mysql_init (NULL);
432 if (ret->dbf == NULL)
433 return GNUNET_SYSERR;
434 if (ret->cnffile != NULL)
435 mysql_options (ret->dbf, MYSQL_READ_DEFAULT_FILE, ret->cnffile);
436 mysql_options (ret->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
438 mysql_options (ret->dbf, MYSQL_OPT_RECONNECT, &reconnect);
439 mysql_options (ret->dbf,
440 MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
441 mysql_options(ret->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
442 timeout = 60; /* in seconds */
443 mysql_options (ret->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
444 mysql_options (ret->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
446 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
447 "datastore-mysql", "DATABASE"))
448 GNUNET_assert (GNUNET_OK ==
449 GNUNET_CONFIGURATION_get_value_string (ret->env->cfg,
450 "datastore-mysql", "DATABASE",
453 mysql_dbname = GNUNET_strdup ("gnunet");
455 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
456 "datastore-mysql", "USER"))
458 GNUNET_assert (GNUNET_OK ==
459 GNUNET_CONFIGURATION_get_value_string (ret->env->cfg,
460 "datastore-mysql", "USER",
463 mysql_password = NULL;
464 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
465 "datastore-mysql", "PASSWORD"))
467 GNUNET_assert (GNUNET_OK ==
468 GNUNET_CONFIGURATION_get_value_string (ret->env->cfg,
469 "datastore-mysql", "PASSWORD",
473 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
474 "datastore-mysql", "HOST"))
476 GNUNET_assert (GNUNET_OK ==
477 GNUNET_CONFIGURATION_get_value_string (ret->env->cfg,
478 "datastore-mysql", "HOST",
482 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
483 "datastore-mysql", "PORT"))
485 GNUNET_assert (GNUNET_OK ==
486 GNUNET_CONFIGURATION_get_value_number (ret->env->cfg, "datastore-mysql",
487 "PORT", &mysql_port));
490 GNUNET_assert (mysql_dbname != NULL);
491 mysql_real_connect (ret->dbf,
493 mysql_user, mysql_password,
495 (unsigned int) mysql_port, NULL,
496 CLIENT_IGNORE_SIGPIPE);
497 GNUNET_free_non_null (mysql_server);
498 GNUNET_free_non_null (mysql_user);
499 GNUNET_free_non_null (mysql_password);
500 GNUNET_free (mysql_dbname);
501 if (mysql_error (ret->dbf)[0])
503 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR,
504 "mysql_real_connect", ret);
505 return GNUNET_SYSERR;
512 * Run the given MySQL statement.
514 * @param plugin plugin context
515 * @param statement SQL statement to run
516 * @return GNUNET_OK on success, GNUNET_SYSERR on error
519 run_statement (struct Plugin *plugin,
520 const char *statement)
522 if ((NULL == plugin->dbf) && (GNUNET_OK != iopen (plugin)))
523 return GNUNET_SYSERR;
524 mysql_query (plugin->dbf, statement);
525 if (mysql_error (plugin->dbf)[0])
527 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR,
528 "mysql_query", plugin);
530 return GNUNET_SYSERR;
537 * Create a prepared statement.
539 * @param plugin plugin context
540 * @param statement SQL statement text to prepare
541 * @return NULL on error
543 static struct GNUNET_MysqlStatementHandle *
544 prepared_statement_create (struct Plugin *plugin,
545 const char *statement)
547 struct GNUNET_MysqlStatementHandle *ret;
549 ret = GNUNET_malloc (sizeof (struct GNUNET_MysqlStatementHandle));
550 ret->query = GNUNET_strdup (statement);
551 GNUNET_CONTAINER_DLL_insert (plugin->shead,
559 * Prepare a statement for running.
561 * @param plugin plugin context
562 * @param ret handle to prepared statement
563 * @return GNUNET_OK on success
566 prepare_statement (struct Plugin *plugin,
567 struct GNUNET_MysqlStatementHandle *ret)
569 if (GNUNET_YES == ret->valid)
571 if ((NULL == plugin->dbf) &&
572 (GNUNET_OK != iopen (plugin)))
573 return GNUNET_SYSERR;
574 ret->statement = mysql_stmt_init (plugin->dbf);
575 if (ret->statement == NULL)
578 return GNUNET_SYSERR;
580 if (mysql_stmt_prepare (ret->statement,
582 strlen (ret->query)))
584 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR,
585 "mysql_stmt_prepare",
587 mysql_stmt_close (ret->statement);
588 ret->statement = NULL;
590 return GNUNET_SYSERR;
592 ret->valid = GNUNET_YES;
599 * Bind the parameters for the given MySQL statement
602 * @param plugin plugin context
603 * @param s statement to bind and run
604 * @param ap arguments for the binding
605 * @return GNUNET_SYSERR on error, GNUNET_OK on success
608 init_params (struct Plugin *plugin,
609 struct GNUNET_MysqlStatementHandle *s,
612 MYSQL_BIND qbind[MAX_PARAM];
615 enum enum_field_types ft;
617 pc = mysql_stmt_param_count (s->statement);
620 /* increase internal constant! */
622 return GNUNET_SYSERR;
624 memset (qbind, 0, sizeof (qbind));
627 while ((pc > 0) && (-1 != (int) (ft = va_arg (ap, enum enum_field_types))))
629 qbind[off].buffer_type = ft;
632 case MYSQL_TYPE_FLOAT:
633 qbind[off].buffer = va_arg (ap, float *);
635 case MYSQL_TYPE_LONGLONG:
636 qbind[off].buffer = va_arg (ap, unsigned long long *);
637 qbind[off].is_unsigned = va_arg (ap, int);
639 case MYSQL_TYPE_LONG:
640 qbind[off].buffer = va_arg (ap, unsigned int *);
641 qbind[off].is_unsigned = va_arg (ap, int);
643 case MYSQL_TYPE_VAR_STRING:
644 case MYSQL_TYPE_STRING:
645 case MYSQL_TYPE_BLOB:
646 qbind[off].buffer = va_arg (ap, void *);
647 qbind[off].buffer_length = va_arg (ap, unsigned long);
648 qbind[off].length = va_arg (ap, unsigned long *);
651 /* unsupported type */
653 return GNUNET_SYSERR;
658 if (! ( (pc == 0) && (-1 != (int) ft) && (va_arg (ap, int) == -1) ) )
661 return GNUNET_SYSERR;
663 if (mysql_stmt_bind_param (s->statement, qbind))
665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
666 _("`%s' failed at %s:%d with error: %s\n"),
667 "mysql_stmt_bind_param",
668 __FILE__, __LINE__, mysql_stmt_error (s->statement));
670 return GNUNET_SYSERR;
672 if (mysql_stmt_execute (s->statement))
674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
675 _("`%s' failed at %s:%d with error: %s\n"),
676 "mysql_stmt_execute",
677 __FILE__, __LINE__, mysql_stmt_error (s->statement));
679 return GNUNET_SYSERR;
685 * Type of a callback that will be called for each
686 * data set returned from MySQL.
688 * @param cls user-defined argument
689 * @param num_values number of elements in values
690 * @param values values returned by MySQL
691 * @return GNUNET_OK to continue iterating, GNUNET_SYSERR to abort
693 typedef int (*GNUNET_MysqlDataProcessor) (void *cls,
694 unsigned int num_values,
699 * Run a prepared SELECT statement.
701 * @param plugin plugin context
702 * @param s statement to run
703 * @param result_size number of elements in results array
704 * @param results pointer to already initialized MYSQL_BIND
705 * array (of sufficient size) for passing results
706 * @param processor function to call on each result
707 * @param processor_cls extra argument to processor
708 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
709 * values (size + buffer-reference for pointers); terminated
711 * @return GNUNET_SYSERR on error, otherwise
712 * the number of successfully affected (or queried) rows
715 prepared_statement_run_select (struct Plugin *plugin,
716 struct GNUNET_MysqlStatementHandle *s,
717 unsigned int result_size,
719 GNUNET_MysqlDataProcessor processor, void *processor_cls,
727 if (GNUNET_OK != prepare_statement (plugin, s))
730 return GNUNET_SYSERR;
732 va_start (ap, processor_cls);
733 if (GNUNET_OK != init_params (plugin, s, ap))
737 return GNUNET_SYSERR;
740 rsize = mysql_stmt_field_count (s->statement);
741 if (rsize > result_size)
744 return GNUNET_SYSERR;
746 if (mysql_stmt_bind_result (s->statement, results))
748 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
749 _("`%s' failed at %s:%d with error: %s\n"),
750 "mysql_stmt_bind_result",
751 __FILE__, __LINE__, mysql_stmt_error (s->statement));
753 return GNUNET_SYSERR;
759 ret = mysql_stmt_fetch (s->statement);
760 if (ret == MYSQL_NO_DATA)
764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
765 _("`%s' failed at %s:%d with error: %s\n"),
767 __FILE__, __LINE__, mysql_stmt_error (s->statement));
769 return GNUNET_SYSERR;
771 if (processor != NULL)
772 if (GNUNET_OK != processor (processor_cls, rsize, results))
776 mysql_stmt_reset (s->statement);
782 * Run a prepared statement that does NOT produce results.
784 * @param plugin plugin context
785 * @param s statement to run
786 * @param insert_id NULL or address where to store the row ID of whatever
787 * was inserted (only for INSERT statements!)
788 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
789 * values (size + buffer-reference for pointers); terminated
791 * @return GNUNET_SYSERR on error, otherwise
792 * the number of successfully affected rows
795 prepared_statement_run (struct Plugin *plugin,
796 struct GNUNET_MysqlStatementHandle *s,
797 unsigned long long *insert_id, ...)
802 if (GNUNET_OK != prepare_statement (plugin, s))
803 return GNUNET_SYSERR;
804 va_start (ap, insert_id);
805 if (GNUNET_OK != init_params (plugin, s, ap))
808 return GNUNET_SYSERR;
811 affected = mysql_stmt_affected_rows (s->statement);
812 if (NULL != insert_id)
813 *insert_id = (unsigned long long) mysql_stmt_insert_id (s->statement);
814 mysql_stmt_reset (s->statement);
820 * Delete an entry from the gn090 table.
822 * @param plugin plugin context
823 * @param uid unique ID of the entry to delete
824 * @return GNUNET_OK on success, GNUNET_NO if no such value exists, GNUNET_SYSERR on error
827 do_delete_entry (struct Plugin *plugin,
828 unsigned long long uid)
833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
834 "Deleting value %llu from gn090 table\n",
837 ret = prepared_statement_run (plugin,
838 plugin->delete_entry_by_uid,
840 MYSQL_TYPE_LONGLONG, &uid, GNUNET_YES,
844 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
845 "Deleting value %llu from gn090 table failed\n",
852 * Function that simply returns GNUNET_OK
854 * @param cls closure, not used
855 * @param num_values not used
856 * @param values not used
860 return_ok (void *cls,
861 unsigned int num_values,
869 * Continuation of "mysql_next_request".
871 * @param next_cls the next context
872 * @param tc the task context (unused)
875 mysql_next_request_cont (void *next_cls,
876 const struct GNUNET_SCHEDULER_TaskContext *tc)
878 struct NextRequestClosure *nrc = next_cls;
879 struct Plugin *plugin;
882 unsigned int priority;
883 unsigned int anonymity;
884 unsigned long long exp;
885 unsigned long hashSize;
887 unsigned long long uid;
888 char value[GNUNET_DATASTORE_MAX_VALUE_SIZE];
890 struct GNUNET_TIME_Absolute expiration;
891 MYSQL_BIND *rbind = nrc->rbind;
893 plugin = nrc->plugin;
894 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
895 plugin->next_task_nc = NULL;
897 if (GNUNET_YES == nrc->end_it)
899 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
900 nrc->now = GNUNET_TIME_absolute_get ();
901 hashSize = sizeof (GNUNET_HashCode);
902 memset (nrc->rbind, 0, sizeof (nrc->rbind));
904 rbind[0].buffer_type = MYSQL_TYPE_LONG;
905 rbind[0].buffer = &type;
906 rbind[0].is_unsigned = 1;
907 rbind[1].buffer_type = MYSQL_TYPE_LONG;
908 rbind[1].buffer = &priority;
909 rbind[1].is_unsigned = 1;
910 rbind[2].buffer_type = MYSQL_TYPE_LONG;
911 rbind[2].buffer = &anonymity;
912 rbind[2].is_unsigned = 1;
913 rbind[3].buffer_type = MYSQL_TYPE_LONGLONG;
914 rbind[3].buffer = &exp;
915 rbind[3].is_unsigned = 1;
916 rbind[4].buffer_type = MYSQL_TYPE_BLOB;
917 rbind[4].buffer = &key;
918 rbind[4].buffer_length = hashSize;
919 rbind[4].length = &hashSize;
920 rbind[5].buffer_type = MYSQL_TYPE_BLOB;
921 rbind[5].buffer = value;
922 rbind[5].buffer_length = size = sizeof (value);
923 rbind[5].length = &size;
924 rbind[6].buffer_type = MYSQL_TYPE_LONGLONG;
925 rbind[6].buffer = &uid;
926 rbind[6].is_unsigned = 1;
928 if (GNUNET_OK != nrc->prep (nrc->prep_cls,
931 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
932 GNUNET_assert (size <= sizeof(value));
933 if ( (rbind[4].buffer_length != sizeof (GNUNET_HashCode)) ||
934 (hashSize != sizeof (GNUNET_HashCode)) )
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941 "Found %u-byte value under key `%s' with prio %u, anon %u, expire %llu selecting from gn090 table\n",
948 expiration.abs_value = exp;
949 ret = nrc->dviter (nrc->dviter_cls, (nrc->end_it == GNUNET_YES) ? NULL : nrc,
952 type, priority, anonymity, expiration,
954 if (ret == GNUNET_SYSERR)
956 nrc->end_it = GNUNET_YES;
959 if (ret == GNUNET_NO)
961 do_delete_entry (plugin, uid);
963 plugin->env->duc (plugin->env->cls,
968 /* call dviter with "end of set" */
969 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
970 nrc->dviter (nrc->dviter_cls,
971 NULL, NULL, 0, NULL, 0, 0, 0,
972 GNUNET_TIME_UNIT_ZERO_ABS, 0);
973 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
974 nrc->prep (nrc->prep_cls, NULL);
975 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
981 * Function invoked on behalf of a "PluginIterator"
982 * asking the database plugin to call the iterator
983 * with the next item.
985 * @param next_cls whatever argument was given
986 * to the PluginIterator as "next_cls".
987 * @param end_it set to GNUNET_YES if we
988 * should terminate the iteration early
989 * (iterator should be still called once more
990 * to signal the end of the iteration).
993 mysql_plugin_next_request (void *next_cls,
996 struct NextRequestClosure *nrc = next_cls;
998 if (GNUNET_YES == end_it)
999 nrc->end_it = GNUNET_YES;
1000 nrc->plugin->next_task_nc = nrc;
1001 nrc->plugin->next_task = GNUNET_SCHEDULER_add_now (&mysql_next_request_cont,
1007 * Get an estimate of how much space the database is
1010 * @param cls our "struct Plugin *"
1011 * @return number of bytes used on disk
1013 static unsigned long long
1014 mysql_plugin_get_size (void *cls)
1016 struct Plugin *plugin = cls;
1017 MYSQL_BIND cbind[1];
1020 memset (cbind, 0, sizeof (cbind));
1022 cbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
1023 cbind[0].buffer = &total;
1024 cbind[0].is_unsigned = GNUNET_NO;
1026 prepared_statement_run_select (plugin,
1037 * Store an item in the datastore.
1039 * @param cls closure
1040 * @param key key for the item
1041 * @param size number of bytes in data
1042 * @param data content stored
1043 * @param type type of the content
1044 * @param priority priority of the content
1045 * @param anonymity anonymity-level for the content
1046 * @param replication replication-level for the content
1047 * @param expiration expiration time for the content
1048 * @param msg set to error message
1049 * @return GNUNET_OK on success
1052 mysql_plugin_put (void *cls,
1053 const GNUNET_HashCode * key,
1056 enum GNUNET_BLOCK_Type type,
1059 uint32_t replication,
1060 struct GNUNET_TIME_Absolute expiration,
1063 struct Plugin *plugin = cls;
1064 unsigned int irepl = replication;
1065 unsigned int itype = type;
1066 unsigned int ipriority = priority;
1067 unsigned int ianonymity = anonymity;
1068 unsigned long long lexpiration = expiration.abs_value;
1069 unsigned long hashSize;
1070 unsigned long hashSize2;
1071 unsigned long lsize;
1072 GNUNET_HashCode vhash;
1074 if (size > MAX_DATUM_SIZE)
1077 return GNUNET_SYSERR;
1079 hashSize = sizeof (GNUNET_HashCode);
1080 hashSize2 = sizeof (GNUNET_HashCode);
1082 GNUNET_CRYPTO_hash (data, size, &vhash);
1084 prepared_statement_run (plugin,
1085 plugin->insert_entry,
1087 MYSQL_TYPE_LONG, &irepl, GNUNET_YES,
1088 MYSQL_TYPE_LONG, &itype, GNUNET_YES,
1089 MYSQL_TYPE_LONG, &ipriority, GNUNET_YES,
1090 MYSQL_TYPE_LONG, &ianonymity, GNUNET_YES,
1091 MYSQL_TYPE_LONGLONG, &lexpiration, GNUNET_YES,
1092 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1093 MYSQL_TYPE_BLOB, &vhash, hashSize2, &hashSize2,
1094 MYSQL_TYPE_BLOB, data, lsize, &lsize,
1096 return GNUNET_SYSERR;
1098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1099 "Inserted value `%s' with size %u into gn090 table\n",
1101 (unsigned int) size);
1104 plugin->env->duc (plugin->env->cls,
1111 * Update the priority for a particular key in the datastore. If
1112 * the expiration time in value is different than the time found in
1113 * the datastore, the higher value should be kept. For the
1114 * anonymity level, the lower value is to be used. The specified
1115 * priority should be added to the existing priority, ignoring the
1116 * priority in value.
1118 * Note that it is possible for multiple values to match this put.
1119 * In that case, all of the respective values are updated.
1121 * @param cls our "struct Plugin*"
1122 * @param uid unique identifier of the datum
1123 * @param delta by how much should the priority
1124 * change? If priority + delta < 0 the
1125 * priority should be set to 0 (never go
1127 * @param expire new expiration time should be the
1128 * MAX of any existing expiration time and
1130 * @param msg set to error message
1131 * @return GNUNET_OK on success
1134 mysql_plugin_update (void *cls,
1137 struct GNUNET_TIME_Absolute expire,
1140 struct Plugin *plugin = cls;
1141 unsigned long long vkey = uid;
1142 unsigned long long lexpire = expire.abs_value;
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Updating value %llu adding %d to priority and maxing exp at %llu\n",
1152 ret = prepared_statement_run (plugin,
1153 plugin->update_entry,
1155 MYSQL_TYPE_LONG, &delta, GNUNET_NO,
1156 MYSQL_TYPE_LONGLONG, &lexpire, GNUNET_YES,
1157 MYSQL_TYPE_LONGLONG, &lexpire, GNUNET_YES,
1158 MYSQL_TYPE_LONGLONG, &vkey, GNUNET_YES,
1160 if (ret != GNUNET_OK)
1162 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1163 "Failed to update value %llu\n",
1172 GNUNET_HashCode key;
1173 GNUNET_HashCode vhash;
1176 unsigned int anonymity;
1177 unsigned long long expiration;
1178 unsigned long long vkey;
1179 unsigned long long total;
1187 get_statement_prepare (void *cls,
1188 struct NextRequestClosure *nrc)
1190 struct GetContext *gc = cls;
1191 struct Plugin *plugin;
1193 unsigned long hashSize;
1200 if (gc->count == gc->total)
1202 plugin = nrc->plugin;
1203 hashSize = sizeof (GNUNET_HashCode);
1204 if (++gc->off >= gc->total)
1207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1208 "Obtaining result number %d/%lld at offset %u for GET `%s'\n",
1212 GNUNET_h2s (&gc->key));
1218 ret = prepared_statement_run_select (plugin,
1219 plugin->select_entry_by_hash_vhash_and_type,
1222 MYSQL_TYPE_BLOB, &gc->key, hashSize, &hashSize,
1223 MYSQL_TYPE_BLOB, &gc->vhash, hashSize, &hashSize,
1224 MYSQL_TYPE_LONG, &nrc->type, GNUNET_YES,
1225 MYSQL_TYPE_LONG, &gc->off, GNUNET_YES,
1231 prepared_statement_run_select (plugin,
1232 plugin->select_entry_by_hash_and_type,
1235 MYSQL_TYPE_BLOB, &gc->key, hashSize, &hashSize,
1236 MYSQL_TYPE_LONG, &nrc->type, GNUNET_YES,
1237 MYSQL_TYPE_LONG, &gc->off, GNUNET_YES,
1246 prepared_statement_run_select (plugin,
1247 plugin->select_entry_by_hash_and_vhash,
1250 MYSQL_TYPE_BLOB, &gc->key, hashSize, &hashSize,
1251 MYSQL_TYPE_BLOB, &gc->vhash, hashSize, &hashSize,
1252 MYSQL_TYPE_LONG, &gc->off, GNUNET_YES,
1258 prepared_statement_run_select (plugin,
1259 plugin->select_entry_by_hash,
1262 MYSQL_TYPE_BLOB, &gc->key, hashSize, &hashSize,
1263 MYSQL_TYPE_LONG, &gc->off, GNUNET_YES,
1273 * Iterate over the results for a particular key in the datastore.
1275 * @param cls closure
1276 * @param key maybe NULL (to match all entries)
1277 * @param vhash hash of the value, maybe NULL (to
1278 * match all values that have the right key).
1279 * Note that for DBlocks there is no difference
1280 * betwen key and vhash, but for other blocks
1282 * @param type entries of which type are relevant?
1283 * Use 0 for any type.
1284 * @param iter function to call on each matching value;
1285 * will be called once with a NULL value at the end
1286 * @param iter_cls closure for iter
1289 mysql_plugin_get (void *cls,
1290 const GNUNET_HashCode *key,
1291 const GNUNET_HashCode *vhash,
1292 enum GNUNET_BLOCK_Type type,
1293 PluginIterator iter, void *iter_cls)
1295 struct Plugin *plugin = cls;
1296 unsigned int itype = type;
1298 MYSQL_BIND cbind[1];
1299 struct GetContext *gc;
1300 struct NextRequestClosure *nrc;
1302 unsigned long hashSize;
1304 GNUNET_assert (key != NULL);
1307 hashSize = sizeof (GNUNET_HashCode);
1308 memset (cbind, 0, sizeof (cbind));
1310 cbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
1311 cbind[0].buffer = &total;
1312 cbind[0].is_unsigned = GNUNET_NO;
1318 prepared_statement_run_select (plugin,
1319 plugin->count_entry_by_hash_vhash_and_type,
1322 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1323 MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize,
1324 MYSQL_TYPE_LONG, &itype, GNUNET_YES,
1330 prepared_statement_run_select (plugin,
1331 plugin->count_entry_by_hash_and_type,
1334 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1335 MYSQL_TYPE_LONG, &itype, GNUNET_YES,
1344 prepared_statement_run_select (plugin,
1345 plugin->count_entry_by_hash_and_vhash,
1348 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1349 MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize,
1356 prepared_statement_run_select (plugin,
1357 plugin->count_entry_by_hash,
1360 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1364 if ((ret != GNUNET_OK) || (0 >= total))
1367 NULL, NULL, 0, NULL, 0, 0, 0,
1368 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1373 "Iterating over %lld results for GET `%s'\n",
1377 gc = GNUNET_malloc (sizeof (struct GetContext));
1381 gc->have_vhash = GNUNET_YES;
1385 gc->off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
1388 nrc = GNUNET_malloc (sizeof (struct NextRequestClosure));
1389 nrc->plugin = plugin;
1392 nrc->dviter_cls = iter_cls;
1393 nrc->prep = &get_statement_prepare;
1395 mysql_plugin_next_request (nrc, GNUNET_NO);
1400 * Run the prepared statement to get the next data item ready.
1402 * @param cls not used
1403 * @param nrc closure for the next request iterator
1404 * @return GNUNET_OK on success, GNUNET_NO if there is no additional item
1407 iterator_zero_prepare (void *cls,
1408 struct NextRequestClosure *nrc)
1410 struct Plugin *plugin;
1415 plugin = nrc->plugin;
1416 ret = prepared_statement_run_select (plugin,
1420 MYSQL_TYPE_LONG, &nrc->count, GNUNET_YES,
1428 * Select a subset of the items in the datastore and call
1429 * the given iterator for each of them.
1431 * @param cls our "struct Plugin*"
1432 * @param type entries of which type should be considered?
1433 * Use 0 for any type.
1434 * @param iter function to call on each matching value;
1435 * will be called once with a NULL value at the end
1436 * @param iter_cls closure for iter
1439 mysql_plugin_iter_zero_anonymity (void *cls,
1440 enum GNUNET_BLOCK_Type type,
1441 PluginIterator iter,
1444 struct Plugin *plugin = cls;
1445 struct NextRequestClosure *nrc;
1447 nrc = GNUNET_malloc (sizeof (struct NextRequestClosure));
1448 nrc->plugin = plugin;
1451 nrc->dviter_cls = iter_cls;
1452 nrc->prep = &iterator_zero_prepare;
1453 mysql_plugin_next_request (nrc, GNUNET_NO);
1458 * Run the SELECT statement for the replication function.
1460 * @param cls the 'struct Plugin'
1461 * @param nrc the context (not used)
1464 replication_prepare (void *cls,
1465 struct NextRequestClosure *nrc)
1467 struct Plugin *plugin = cls;
1469 nrc->end_it = GNUNET_YES;
1470 return prepared_statement_run_select (plugin,
1471 plugin->select_replication,
1479 * Get a random item for replication. Returns a single, not expired, random item
1480 * from those with the highest replication counters. The item's
1481 * replication counter is decremented by one IF it was positive before.
1482 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
1484 * @param cls closure
1485 * @param iter function to call the value (once only).
1486 * @param iter_cls closure for iter
1489 mysql_plugin_replication_get (void *cls,
1490 PluginIterator iter, void *iter_cls)
1492 struct Plugin *plugin = cls;
1493 struct NextRequestClosure nrc;
1495 memset (&nrc, 0, sizeof (nrc));
1496 nrc.plugin = plugin;
1497 nrc.now = GNUNET_TIME_absolute_get ();
1498 nrc.prep = &replication_prepare;
1499 nrc.prep_cls = plugin;
1502 nrc.dviter_cls = iter_cls;
1503 nrc.end_it = GNUNET_NO;
1504 mysql_next_request_cont (&nrc, NULL);
1509 * Run the SELECT statement for the expiration function.
1511 * @param cls the 'struct Plugin'
1512 * @param nrc the context (not used)
1513 * @return GNUNET_OK on success, GNUNET_NO if there are
1514 * no more values, GNUNET_SYSERR on error
1517 expiration_prepare (void *cls,
1518 struct NextRequestClosure *nrc)
1520 struct Plugin *plugin = cls;
1525 nrc->end_it = GNUNET_YES;
1526 nt = (long long) nrc->now.abs_value;
1527 return prepared_statement_run_select
1529 plugin->select_expiration,
1532 MYSQL_TYPE_LONGLONG, &nt, GNUNET_YES,
1538 * Get a random item for expiration.
1539 * Call 'iter' with all values ZERO or NULL if the datastore is empty.
1541 * @param cls closure
1542 * @param iter function to call the value (once only).
1543 * @param iter_cls closure for iter
1546 mysql_plugin_expiration_get (void *cls,
1547 PluginIterator iter, void *iter_cls)
1549 struct Plugin *plugin = cls;
1550 struct NextRequestClosure nrc;
1552 memset (&nrc, 0, sizeof (nrc));
1553 nrc.plugin = plugin;
1554 nrc.now = GNUNET_TIME_absolute_get ();
1555 nrc.prep = &expiration_prepare;
1556 nrc.prep_cls = plugin;
1559 nrc.dviter_cls = iter_cls;
1560 nrc.end_it = GNUNET_NO;
1561 mysql_next_request_cont (&nrc, NULL);
1568 * @param cls the "struct Plugin*"
1571 mysql_plugin_drop (void *cls)
1573 struct Plugin *plugin = cls;
1575 if (GNUNET_OK != run_statement (plugin,
1576 "DROP TABLE gn090"))
1578 plugin->env->duc (plugin->env->cls, 0);
1583 * Entry point for the plugin.
1585 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1586 * @return our "struct Plugin*"
1589 libgnunet_plugin_datastore_mysql_init (void *cls)
1591 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1592 struct GNUNET_DATASTORE_PluginFunctions *api;
1593 struct Plugin *plugin;
1595 plugin = GNUNET_malloc (sizeof (struct Plugin));
1597 plugin->cnffile = get_my_cnf_path (env->cfg);
1598 if (GNUNET_OK != iopen (plugin))
1601 GNUNET_free_non_null (plugin->cnffile);
1602 GNUNET_free (plugin);
1605 #define MRUNS(a) (GNUNET_OK != run_statement (plugin, a) )
1606 #define PINIT(a,b) (NULL == (a = prepared_statement_create(plugin, b)))
1607 if (MRUNS ("CREATE TABLE IF NOT EXISTS gn090 ("
1608 " repl INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1609 " type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1610 " prio INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1611 " anonLevel INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1612 " expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
1613 " hash BINARY(64) NOT NULL DEFAULT '',"
1614 " vhash BINARY(64) NOT NULL DEFAULT '',"
1615 " value BLOB NOT NULL DEFAULT '',"
1616 " uid BIGINT NOT NULL AUTO_INCREMENT,"
1617 " PRIMARY KEY (uid),"
1618 " INDEX idx_hash (hash(64)),"
1619 " INDEX idx_hash_uid (hash(64),uid),"
1620 " INDEX idx_hash_vhash (hash(64),vhash(64)),"
1621 " INDEX idx_hash_type_uid (hash(64),type,uid),"
1622 " INDEX idx_prio (prio),"
1623 " INDEX idx_repl (repl),"
1624 " INDEX idx_expire_prio (expire,prio),"
1625 " INDEX idx_anonLevel_uid (anonLevel,uid)"
1626 ") ENGINE=InnoDB") ||
1627 MRUNS ("SET AUTOCOMMIT = 1") ||
1628 PINIT (plugin->insert_entry, INSERT_ENTRY) ||
1629 PINIT (plugin->delete_entry_by_uid, DELETE_ENTRY_BY_UID) ||
1630 PINIT (plugin->select_entry_by_hash, SELECT_ENTRY_BY_HASH) ||
1631 PINIT (plugin->select_entry_by_hash_and_vhash, SELECT_ENTRY_BY_HASH_AND_VHASH)
1632 || PINIT (plugin->select_entry_by_hash_and_type, SELECT_ENTRY_BY_HASH_AND_TYPE)
1633 || PINIT (plugin->select_entry_by_hash_vhash_and_type,
1634 SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE)
1635 || PINIT (plugin->count_entry_by_hash, COUNT_ENTRY_BY_HASH)
1636 || PINIT (plugin->get_size, SELECT_SIZE)
1637 || PINIT (plugin->count_entry_by_hash_and_vhash, COUNT_ENTRY_BY_HASH_AND_VHASH)
1638 || PINIT (plugin->count_entry_by_hash_and_type, COUNT_ENTRY_BY_HASH_AND_TYPE)
1639 || PINIT (plugin->count_entry_by_hash_vhash_and_type,
1640 COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE)
1641 || PINIT (plugin->update_entry, UPDATE_ENTRY)
1642 || PINIT (plugin->zero_iter, SELECT_IT_NON_ANONYMOUS)
1643 || PINIT (plugin->select_expiration, SELECT_IT_EXPIRATION)
1644 || PINIT (plugin->select_replication, SELECT_IT_REPLICATION) )
1647 GNUNET_free_non_null (plugin->cnffile);
1648 GNUNET_free (plugin);
1654 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1656 api->get_size = &mysql_plugin_get_size;
1657 api->put = &mysql_plugin_put;
1658 api->next_request = &mysql_plugin_next_request;
1659 api->get = &mysql_plugin_get;
1660 api->replication_get = &mysql_plugin_replication_get;
1661 api->expiration_get = &mysql_plugin_expiration_get;
1662 api->update = &mysql_plugin_update;
1663 api->iter_zero_anonymity = &mysql_plugin_iter_zero_anonymity;
1664 api->drop = &mysql_plugin_drop;
1665 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1666 "mysql", _("Mysql database running\n"));
1672 * Exit point from the plugin.
1673 * @param cls our "struct Plugin*"
1674 * @return always NULL
1677 libgnunet_plugin_datastore_mysql_done (void *cls)
1679 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1680 struct Plugin *plugin = api->cls;
1683 if (plugin->next_task != GNUNET_SCHEDULER_NO_TASK)
1685 GNUNET_SCHEDULER_cancel (plugin->next_task);
1686 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
1687 plugin->next_task_nc->prep (plugin->next_task_nc->prep_cls, NULL);
1688 GNUNET_free (plugin->next_task_nc);
1689 plugin->next_task_nc = NULL;
1691 GNUNET_free_non_null (plugin->cnffile);
1692 GNUNET_free (plugin);
1694 mysql_library_end ();
1698 /* end of plugin_datastore_mysql.c */