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_EXTRA_LOGGING
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)); GNUNET_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;
165 * Context for all functions in this plugin.
170 * Our execution environment.
172 struct GNUNET_DATASTORE_PluginEnvironment *env;
175 * Handle to talk to MySQL.
180 * We keep all prepared statements in a DLL. This is the head.
182 struct GNUNET_MysqlStatementHandle *shead;
185 * We keep all prepared statements in a DLL. This is the tail.
187 struct GNUNET_MysqlStatementHandle *stail;
190 * Filename of "my.cnf" (msyql configuration).
195 * Prepared statements.
197 #define INSERT_ENTRY "INSERT INTO gn090 (repl,type,prio,anonLevel,expire,rvalue,hash,vhash,value) VALUES (?,?,?,?,?,?,?,?,?)"
198 struct GNUNET_MysqlStatementHandle *insert_entry;
200 #define DELETE_ENTRY_BY_UID "DELETE FROM gn090 WHERE uid=?"
201 struct GNUNET_MysqlStatementHandle *delete_entry_by_uid;
203 #define COUNT_ENTRY_BY_HASH "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash) WHERE hash=?"
204 struct GNUNET_MysqlStatementHandle *count_entry_by_hash;
206 #define SELECT_ENTRY_BY_HASH "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_hash) WHERE hash=? ORDER BY uid LIMIT 1 OFFSET ?"
207 struct GNUNET_MysqlStatementHandle *select_entry_by_hash;
209 #define COUNT_ENTRY_BY_HASH_AND_VHASH "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=?"
210 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_vhash;
212 #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 ?"
213 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_vhash;
215 #define COUNT_ENTRY_BY_HASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_type_uid) WHERE hash=? AND type=?"
216 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_type;
218 #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 ?"
219 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_type;
221 #define COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (idx_hash_vhash) WHERE hash=? AND vhash=? AND type=?"
222 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_vhash_and_type;
224 #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 ?"
225 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_vhash_and_type;
227 #define UPDATE_ENTRY "UPDATE gn090 SET prio=prio+?,expire=IF(expire>=?,expire,?) WHERE uid=?"
228 struct GNUNET_MysqlStatementHandle *update_entry;
230 #define DEC_REPL "UPDATE gn090 SET repl=GREATEST (0, repl - 1) WHERE uid=?"
231 struct GNUNET_MysqlStatementHandle *dec_repl;
233 #define SELECT_SIZE "SELECT SUM(BIT_LENGTH(value) DIV 8) FROM gn090"
234 struct GNUNET_MysqlStatementHandle *get_size;
236 #define SELECT_IT_NON_ANONYMOUS "SELECT type,prio,anonLevel,expire,hash,value,uid "\
237 "FROM gn090 FORCE INDEX (idx_anonLevel_type_rvalue) "\
238 "WHERE anonLevel=0 AND type=? AND "\
240 " NOT EXISTS (SELECT 1 FROM gn090 FORCE INDEX (idx_anonLevel_type_rvalue) WHERE anonLevel=0 AND type=? AND rvalue>=?)) "\
241 "ORDER BY rvalue ASC LIMIT 1"
242 struct GNUNET_MysqlStatementHandle *zero_iter;
244 #define SELECT_IT_EXPIRATION "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_expire) WHERE expire < ? ORDER BY expire ASC LIMIT 1"
245 struct GNUNET_MysqlStatementHandle *select_expiration;
247 #define SELECT_IT_PRIORITY "SELECT type,prio,anonLevel,expire,hash,value,uid FROM gn090 FORCE INDEX (idx_prio) ORDER BY prio ASC LIMIT 1"
248 struct GNUNET_MysqlStatementHandle *select_priority;
250 #define SELECT_IT_REPLICATION "SELECT type,prio,anonLevel,expire,hash,value,uid "\
251 "FROM gn090 FORCE INDEX (idx_repl_rvalue) "\
254 " NOT EXISTS (SELECT 1 FROM gn090 FORCE INDEX (idx_repl_rvalue) WHERE repl=? AND rvalue>=?)) "\
255 "ORDER BY rvalue ASC "\
257 struct GNUNET_MysqlStatementHandle *select_replication;
259 #define SELECT_MAX_REPL "SELECT MAX(repl) FROM gn090"
260 struct GNUNET_MysqlStatementHandle *max_repl;
266 * Obtain the location of ".my.cnf".
268 * @param cfg our configuration
269 * @return NULL on error
272 get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg)
284 pw = getpwuid (getuid ());
287 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "getpwuid");
291 GNUNET_CONFIGURATION_have_value (cfg, "datastore-mysql", "CONFIG"))
293 GNUNET_assert (GNUNET_OK ==
294 GNUNET_CONFIGURATION_get_value_filename (cfg,
298 configured = GNUNET_YES;
302 home_dir = GNUNET_strdup (pw->pw_dir);
303 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
304 GNUNET_free (home_dir);
305 configured = GNUNET_NO;
308 home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
309 plibc_conv_to_win_path ("~/", home_dir);
310 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
311 GNUNET_free (home_dir);
312 configured = GNUNET_NO;
315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
316 _("Trying to use file `%s' for MySQL configuration.\n"), cnffile);
317 if ((0 != STAT (cnffile, &st)) || (0 != ACCESS (cnffile, R_OK)) ||
318 (!S_ISREG (st.st_mode)))
320 if (configured == GNUNET_YES)
321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
322 _("Could not access file `%s': %s\n"), cnffile,
324 GNUNET_free (cnffile);
332 * Close database connection and all prepared statements (we got a DB
335 * @param plugin plugin context
338 iclose (struct Plugin *plugin)
340 struct GNUNET_MysqlStatementHandle *s;
342 for (s = plugin->shead; s != NULL; s = s->next)
346 mysql_stmt_close (s->statement);
347 s->valid = GNUNET_NO;
350 if (plugin->dbf != NULL)
352 mysql_close (plugin->dbf);
360 * Open the connection with the database (and initialize
361 * our default options).
363 * @param plugin plugin context
364 * @return GNUNET_OK on success
367 iopen (struct Plugin *plugin)
372 char *mysql_password;
373 unsigned long long mysql_port;
375 unsigned int timeout;
377 plugin->dbf = mysql_init (NULL);
378 if (plugin->dbf == NULL)
379 return GNUNET_SYSERR;
380 if (plugin->cnffile != NULL)
381 mysql_options (plugin->dbf, MYSQL_READ_DEFAULT_FILE, plugin->cnffile);
382 mysql_options (plugin->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
384 mysql_options (plugin->dbf, MYSQL_OPT_RECONNECT, &reconnect);
385 timeout = 120; /* in seconds */
386 mysql_options (plugin->dbf, MYSQL_OPT_CONNECT_TIMEOUT,
387 (const void *) &timeout);
388 mysql_options (plugin->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
389 timeout = 60; /* in seconds */
390 mysql_options (plugin->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
391 mysql_options (plugin->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
394 GNUNET_CONFIGURATION_have_value (plugin->env->cfg, "datastore-mysql",
396 GNUNET_assert (GNUNET_OK ==
397 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
402 mysql_dbname = GNUNET_strdup ("gnunet");
405 GNUNET_CONFIGURATION_have_value (plugin->env->cfg, "datastore-mysql",
408 GNUNET_assert (GNUNET_OK ==
409 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
411 "USER", &mysql_user));
413 mysql_password = NULL;
415 GNUNET_CONFIGURATION_have_value (plugin->env->cfg, "datastore-mysql",
418 GNUNET_assert (GNUNET_OK ==
419 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
426 GNUNET_CONFIGURATION_have_value (plugin->env->cfg, "datastore-mysql",
429 GNUNET_assert (GNUNET_OK ==
430 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
437 GNUNET_CONFIGURATION_have_value (plugin->env->cfg, "datastore-mysql",
440 GNUNET_assert (GNUNET_OK ==
441 GNUNET_CONFIGURATION_get_value_number (plugin->env->cfg,
443 "PORT", &mysql_port));
446 GNUNET_assert (mysql_dbname != NULL);
447 mysql_real_connect (plugin->dbf, mysql_server, mysql_user, mysql_password,
448 mysql_dbname, (unsigned int) mysql_port, NULL,
449 CLIENT_IGNORE_SIGPIPE);
450 GNUNET_free_non_null (mysql_server);
451 GNUNET_free_non_null (mysql_user);
452 GNUNET_free_non_null (mysql_password);
453 GNUNET_free (mysql_dbname);
454 if (mysql_error (plugin->dbf)[0])
456 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_real_connect", plugin);
457 return GNUNET_SYSERR;
464 * Run the given MySQL statement.
466 * @param plugin plugin context
467 * @param statement SQL statement to run
468 * @return GNUNET_OK on success, GNUNET_SYSERR on error
471 run_statement (struct Plugin *plugin, const char *statement)
473 if ((NULL == plugin->dbf) && (GNUNET_OK != iopen (plugin)))
474 return GNUNET_SYSERR;
475 mysql_query (plugin->dbf, statement);
476 if (mysql_error (plugin->dbf)[0])
478 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_query", plugin);
480 return GNUNET_SYSERR;
487 * Create a prepared statement.
489 * @param plugin plugin context
490 * @param statement SQL statement text to prepare
491 * @return NULL on error
493 static struct GNUNET_MysqlStatementHandle *
494 prepared_statement_create (struct Plugin *plugin, const char *statement)
496 struct GNUNET_MysqlStatementHandle *ret;
498 ret = GNUNET_malloc (sizeof (struct GNUNET_MysqlStatementHandle));
499 ret->query = GNUNET_strdup (statement);
500 GNUNET_CONTAINER_DLL_insert (plugin->shead, plugin->stail, ret);
506 * Prepare a statement for running.
508 * @param plugin plugin context
509 * @param ret handle to prepared statement
510 * @return GNUNET_OK on success
513 prepare_statement (struct Plugin *plugin,
514 struct GNUNET_MysqlStatementHandle *ret)
516 if (GNUNET_YES == ret->valid)
518 if ((NULL == plugin->dbf) && (GNUNET_OK != iopen (plugin)))
519 return GNUNET_SYSERR;
520 ret->statement = mysql_stmt_init (plugin->dbf);
521 if (ret->statement == NULL)
524 return GNUNET_SYSERR;
526 if (mysql_stmt_prepare (ret->statement, ret->query, strlen (ret->query)))
528 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
529 _("Failed to prepare statement `%s'\n"), ret->query);
530 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", plugin);
531 mysql_stmt_close (ret->statement);
532 ret->statement = NULL;
534 return GNUNET_SYSERR;
536 ret->valid = GNUNET_YES;
543 * Bind the parameters for the given MySQL statement
546 * @param plugin plugin context
547 * @param s statement to bind and run
548 * @param ap arguments for the binding
549 * @return GNUNET_SYSERR on error, GNUNET_OK on success
552 init_params (struct Plugin *plugin, struct GNUNET_MysqlStatementHandle *s,
555 MYSQL_BIND qbind[MAX_PARAM];
558 enum enum_field_types ft;
560 pc = mysql_stmt_param_count (s->statement);
563 /* increase internal constant! */
565 return GNUNET_SYSERR;
567 memset (qbind, 0, sizeof (qbind));
570 while ((pc > 0) && (-1 != (int) (ft = va_arg (ap, enum enum_field_types))))
572 qbind[off].buffer_type = ft;
575 case MYSQL_TYPE_FLOAT:
576 qbind[off].buffer = va_arg (ap, float *);
579 case MYSQL_TYPE_LONGLONG:
580 qbind[off].buffer = va_arg (ap, unsigned long long *);
581 qbind[off].is_unsigned = va_arg (ap, int);
584 case MYSQL_TYPE_LONG:
585 qbind[off].buffer = va_arg (ap, unsigned int *);
586 qbind[off].is_unsigned = va_arg (ap, int);
589 case MYSQL_TYPE_VAR_STRING:
590 case MYSQL_TYPE_STRING:
591 case MYSQL_TYPE_BLOB:
592 qbind[off].buffer = va_arg (ap, void *);
593 qbind[off].buffer_length = va_arg (ap, unsigned long);
594 qbind[off].length = va_arg (ap, unsigned long *);
598 /* unsupported type */
600 return GNUNET_SYSERR;
605 if (!((pc == 0) && (-1 != (int) ft) && (va_arg (ap, int) == -1)))
608 return GNUNET_SYSERR;
610 if (mysql_stmt_bind_param (s->statement, qbind))
612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
613 _("`%s' failed at %s:%d with error: %s\n"),
614 "mysql_stmt_bind_param", __FILE__, __LINE__,
615 mysql_stmt_error (s->statement));
617 return GNUNET_SYSERR;
619 if (mysql_stmt_execute (s->statement))
621 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
622 _("`%s' for `%s' failed at %s:%d with error: %s\n"),
623 "mysql_stmt_execute", s->query, __FILE__, __LINE__,
624 mysql_stmt_error (s->statement));
626 return GNUNET_SYSERR;
633 * Run a prepared SELECT statement.
635 * @param plugin plugin context
636 * @param s statement to run
637 * @param result_size number of elements in results array
638 * @param results pointer to already initialized MYSQL_BIND
639 * array (of sufficient size) for passing results
640 * @param ap pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
641 * values (size + buffer-reference for pointers); terminated
643 * @return GNUNET_SYSERR on error, otherwise GNUNET_OK or GNUNET_NO (no result)
646 prepared_statement_run_select_va (struct Plugin *plugin,
647 struct GNUNET_MysqlStatementHandle *s,
648 unsigned int result_size,
649 MYSQL_BIND * results, va_list ap)
654 if (GNUNET_OK != prepare_statement (plugin, s))
657 return GNUNET_SYSERR;
659 if (GNUNET_OK != init_params (plugin, s, ap))
662 return GNUNET_SYSERR;
664 rsize = mysql_stmt_field_count (s->statement);
665 if (rsize > result_size)
668 return GNUNET_SYSERR;
670 if (mysql_stmt_bind_result (s->statement, results))
672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
673 _("`%s' failed at %s:%d with error: %s\n"),
674 "mysql_stmt_bind_result", __FILE__, __LINE__,
675 mysql_stmt_error (s->statement));
677 return GNUNET_SYSERR;
679 ret = mysql_stmt_fetch (s->statement);
680 if (ret == MYSQL_NO_DATA)
684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
685 _("`%s' failed at %s:%d with error: %s\n"), "mysql_stmt_fetch",
686 __FILE__, __LINE__, mysql_stmt_error (s->statement));
688 return GNUNET_SYSERR;
690 mysql_stmt_reset (s->statement);
696 * Run a prepared SELECT statement.
698 * @param plugin plugin context
699 * @param s statement to run
700 * @param result_size number of elements in results array
701 * @param results pointer to already initialized MYSQL_BIND
702 * array (of sufficient size) for passing results
703 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
704 * values (size + buffer-reference for pointers); terminated
706 * @return GNUNET_SYSERR on error, otherwise
707 * the number of successfully affected (or queried) rows
710 prepared_statement_run_select (struct Plugin *plugin,
711 struct GNUNET_MysqlStatementHandle *s,
712 unsigned int result_size, MYSQL_BIND * results,
718 va_start (ap, results);
719 ret = prepared_statement_run_select_va (plugin, s, result_size, results, ap);
726 * Run a prepared statement that does NOT produce results.
728 * @param plugin plugin context
729 * @param s statement to run
730 * @param insert_id NULL or address where to store the row ID of whatever
731 * was inserted (only for INSERT statements!)
732 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
733 * values (size + buffer-reference for pointers); terminated
735 * @return GNUNET_SYSERR on error, otherwise
736 * the number of successfully affected rows
739 prepared_statement_run (struct Plugin *plugin,
740 struct GNUNET_MysqlStatementHandle *s,
741 unsigned long long *insert_id, ...)
746 if (GNUNET_OK != prepare_statement (plugin, s))
747 return GNUNET_SYSERR;
748 va_start (ap, insert_id);
749 if (GNUNET_OK != init_params (plugin, s, ap))
752 return GNUNET_SYSERR;
755 affected = mysql_stmt_affected_rows (s->statement);
756 if (NULL != insert_id)
757 *insert_id = (unsigned long long) mysql_stmt_insert_id (s->statement);
758 mysql_stmt_reset (s->statement);
764 * Delete an entry from the gn090 table.
766 * @param plugin plugin context
767 * @param uid unique ID of the entry to delete
768 * @return GNUNET_OK on success, GNUNET_NO if no such value exists, GNUNET_SYSERR on error
771 do_delete_entry (struct Plugin *plugin, unsigned long long uid)
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting value %llu from gn090 table\n",
780 prepared_statement_run (plugin, plugin->delete_entry_by_uid, NULL,
781 MYSQL_TYPE_LONGLONG, &uid, GNUNET_YES, -1);
784 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
785 "Deleting value %llu from gn090 table failed\n", uid);
791 * Get an estimate of how much space the database is
794 * @param cls our "struct Plugin *"
795 * @return number of bytes used on disk
797 static unsigned long long
798 mysql_plugin_estimate_size (void *cls)
800 struct Plugin *plugin = cls;
804 memset (cbind, 0, sizeof (cbind));
806 cbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
807 cbind[0].buffer = &total;
808 cbind[0].is_unsigned = GNUNET_NO;
810 prepared_statement_run_select (plugin, plugin->get_size, 1, cbind, -1))
817 * Store an item in the datastore.
820 * @param key key for the item
821 * @param size number of bytes in data
822 * @param data content stored
823 * @param type type of the content
824 * @param priority priority of the content
825 * @param anonymity anonymity-level for the content
826 * @param replication replication-level for the content
827 * @param expiration expiration time for the content
828 * @param msg set to error message
829 * @return GNUNET_OK on success
832 mysql_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
833 const void *data, enum GNUNET_BLOCK_Type type,
834 uint32_t priority, uint32_t anonymity, uint32_t replication,
835 struct GNUNET_TIME_Absolute expiration, char **msg)
837 struct Plugin *plugin = cls;
838 unsigned int irepl = replication;
839 unsigned int ipriority = priority;
840 unsigned int ianonymity = anonymity;
841 unsigned long long lexpiration = expiration.abs_value;
842 unsigned long long lrvalue =
843 (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
845 unsigned long hashSize;
846 unsigned long hashSize2;
848 GNUNET_HashCode vhash;
850 if (size > MAX_DATUM_SIZE)
853 return GNUNET_SYSERR;
855 hashSize = sizeof (GNUNET_HashCode);
856 hashSize2 = sizeof (GNUNET_HashCode);
858 GNUNET_CRYPTO_hash (data, size, &vhash);
860 prepared_statement_run (plugin, plugin->insert_entry, NULL,
861 MYSQL_TYPE_LONG, &irepl, GNUNET_YES,
862 MYSQL_TYPE_LONG, &type, GNUNET_YES,
863 MYSQL_TYPE_LONG, &ipriority, GNUNET_YES,
864 MYSQL_TYPE_LONG, &ianonymity, GNUNET_YES,
865 MYSQL_TYPE_LONGLONG, &lexpiration, GNUNET_YES,
866 MYSQL_TYPE_LONGLONG, &lrvalue, GNUNET_YES,
867 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
868 MYSQL_TYPE_BLOB, &vhash, hashSize2, &hashSize2,
869 MYSQL_TYPE_BLOB, data, lsize, &lsize, -1))
870 return GNUNET_SYSERR;
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Inserted value `%s' with size %u into gn090 table\n",
874 GNUNET_h2s (key), (unsigned int) size);
877 plugin->env->duc (plugin->env->cls, size);
883 * Update the priority for a particular key in the datastore. If
884 * the expiration time in value is different than the time found in
885 * the datastore, the higher value should be kept. For the
886 * anonymity level, the lower value is to be used. The specified
887 * priority should be added to the existing priority, ignoring the
890 * Note that it is possible for multiple values to match this put.
891 * In that case, all of the respective values are updated.
893 * @param cls our "struct Plugin*"
894 * @param uid unique identifier of the datum
895 * @param delta by how much should the priority
896 * change? If priority + delta < 0 the
897 * priority should be set to 0 (never go
899 * @param expire new expiration time should be the
900 * MAX of any existing expiration time and
902 * @param msg set to error message
903 * @return GNUNET_OK on success
906 mysql_plugin_update (void *cls, uint64_t uid, int delta,
907 struct GNUNET_TIME_Absolute expire, char **msg)
909 struct Plugin *plugin = cls;
910 unsigned long long vkey = uid;
911 unsigned long long lexpire = expire.abs_value;
915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
916 "Updating value %llu adding %d to priority and maxing exp at %llu\n",
917 vkey, delta, lexpire);
920 prepared_statement_run (plugin, plugin->update_entry, NULL,
921 MYSQL_TYPE_LONG, &delta, GNUNET_NO,
922 MYSQL_TYPE_LONGLONG, &lexpire, GNUNET_YES,
923 MYSQL_TYPE_LONGLONG, &lexpire, GNUNET_YES,
924 MYSQL_TYPE_LONGLONG, &vkey, GNUNET_YES, -1);
925 if (ret != GNUNET_OK)
927 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to update value %llu\n",
935 * Run the given select statement and call 'proc' on the resulting
936 * values (which must be in particular positions).
938 * @param plugin the plugin handle
939 * @param stmt select statement to run
940 * @param proc function to call on result
941 * @param proc_cls closure for proc
942 * @param ... arguments to initialize stmt
945 execute_select (struct Plugin *plugin, struct GNUNET_MysqlStatementHandle *stmt,
946 PluginDatumProcessor proc, void *proc_cls, ...)
951 unsigned int priority;
952 unsigned int anonymity;
953 unsigned long long exp;
954 unsigned long hashSize;
956 unsigned long long uid;
957 char value[GNUNET_DATASTORE_MAX_VALUE_SIZE];
959 struct GNUNET_TIME_Absolute expiration;
962 hashSize = sizeof (GNUNET_HashCode);
963 memset (rbind, 0, sizeof (rbind));
964 rbind[0].buffer_type = MYSQL_TYPE_LONG;
965 rbind[0].buffer = &type;
966 rbind[0].is_unsigned = 1;
967 rbind[1].buffer_type = MYSQL_TYPE_LONG;
968 rbind[1].buffer = &priority;
969 rbind[1].is_unsigned = 1;
970 rbind[2].buffer_type = MYSQL_TYPE_LONG;
971 rbind[2].buffer = &anonymity;
972 rbind[2].is_unsigned = 1;
973 rbind[3].buffer_type = MYSQL_TYPE_LONGLONG;
974 rbind[3].buffer = &exp;
975 rbind[3].is_unsigned = 1;
976 rbind[4].buffer_type = MYSQL_TYPE_BLOB;
977 rbind[4].buffer = &key;
978 rbind[4].buffer_length = hashSize;
979 rbind[4].length = &hashSize;
980 rbind[5].buffer_type = MYSQL_TYPE_BLOB;
981 rbind[5].buffer = value;
982 rbind[5].buffer_length = size = sizeof (value);
983 rbind[5].length = &size;
984 rbind[6].buffer_type = MYSQL_TYPE_LONGLONG;
985 rbind[6].buffer = &uid;
986 rbind[6].is_unsigned = 1;
988 va_start (ap, proc_cls);
989 ret = prepared_statement_run_select_va (plugin, stmt, 7, rbind, ap);
993 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
996 GNUNET_assert (size <= sizeof (value));
997 if ((rbind[4].buffer_length != sizeof (GNUNET_HashCode)) ||
998 (hashSize != sizeof (GNUNET_HashCode)))
1001 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Found %u-byte value under key `%s' with prio %u, anon %u, expire %llu selecting from gn090 table\n",
1007 (unsigned int) size, GNUNET_h2s (&key), priority, anonymity, exp);
1009 GNUNET_assert (size < MAX_DATUM_SIZE);
1010 expiration.abs_value = exp;
1012 proc (proc_cls, &key, size, value, type, priority, anonymity, expiration,
1014 if (ret == GNUNET_NO)
1016 do_delete_entry (plugin, uid);
1018 plugin->env->duc (plugin->env->cls, -size);
1025 * Get one of the results for a particular key in the datastore.
1027 * @param cls closure
1028 * @param offset offset of the result (modulo num-results);
1029 * specific ordering does not matter for the offset
1030 * @param key key to match, never NULL
1031 * @param vhash hash of the value, maybe NULL (to
1032 * match all values that have the right key).
1033 * Note that for DBlocks there is no difference
1034 * betwen key and vhash, but for other blocks
1036 * @param type entries of which type are relevant?
1037 * Use 0 for any type.
1038 * @param proc function to call on the matching value,
1039 * with NULL for if no value matches
1040 * @param proc_cls closure for proc
1043 mysql_plugin_get_key (void *cls, uint64_t offset, const GNUNET_HashCode * key,
1044 const GNUNET_HashCode * vhash,
1045 enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
1048 struct Plugin *plugin = cls;
1050 MYSQL_BIND cbind[1];
1052 unsigned long hashSize;
1053 unsigned long hashSize2;
1054 unsigned long long off;
1056 GNUNET_assert (key != NULL);
1057 GNUNET_assert (NULL != proc);
1058 hashSize = sizeof (GNUNET_HashCode);
1059 hashSize2 = sizeof (GNUNET_HashCode);
1060 memset (cbind, 0, sizeof (cbind));
1062 cbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
1063 cbind[0].buffer = &total;
1064 cbind[0].is_unsigned = GNUNET_NO;
1070 prepared_statement_run_select (plugin,
1072 count_entry_by_hash_vhash_and_type, 1,
1073 cbind, MYSQL_TYPE_BLOB, key, hashSize,
1074 &hashSize, MYSQL_TYPE_BLOB, vhash,
1075 hashSize2, &hashSize2, MYSQL_TYPE_LONG,
1076 &type, GNUNET_YES, -1);
1081 prepared_statement_run_select (plugin,
1082 plugin->count_entry_by_hash_and_type,
1083 1, cbind, MYSQL_TYPE_BLOB, key,
1084 hashSize, &hashSize, MYSQL_TYPE_LONG,
1085 &type, GNUNET_YES, -1);
1093 prepared_statement_run_select (plugin,
1094 plugin->count_entry_by_hash_and_vhash,
1095 1, cbind, MYSQL_TYPE_BLOB, key,
1096 hashSize, &hashSize, MYSQL_TYPE_BLOB,
1097 vhash, hashSize2, &hashSize2, -1);
1103 prepared_statement_run_select (plugin, plugin->count_entry_by_hash, 1,
1104 cbind, MYSQL_TYPE_BLOB, key, hashSize,
1108 if ((ret != GNUNET_OK) || (0 >= total))
1110 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1113 offset = offset % total;
1114 off = (unsigned long long) offset;
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "Obtaining %llu/%lld result for GET `%s'\n", off, total,
1121 if (type != GNUNET_BLOCK_TYPE_ANY)
1125 execute_select (plugin, plugin->select_entry_by_hash_vhash_and_type, proc,
1126 proc_cls, MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1127 MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize,
1128 MYSQL_TYPE_LONG, &type, GNUNET_YES, MYSQL_TYPE_LONGLONG,
1129 &off, GNUNET_YES, -1);
1133 execute_select (plugin, plugin->select_entry_by_hash_and_type, proc,
1134 proc_cls, MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1135 MYSQL_TYPE_LONG, &type, GNUNET_YES, MYSQL_TYPE_LONGLONG,
1136 &off, GNUNET_YES, -1);
1143 execute_select (plugin, plugin->select_entry_by_hash_and_vhash, proc,
1144 proc_cls, MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1145 MYSQL_TYPE_BLOB, vhash, hashSize, &hashSize,
1146 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES, -1);
1150 execute_select (plugin, plugin->select_entry_by_hash, proc, proc_cls,
1151 MYSQL_TYPE_BLOB, key, hashSize, &hashSize,
1152 MYSQL_TYPE_LONGLONG, &off, GNUNET_YES, -1);
1159 * Get a zero-anonymity datum from the datastore.
1161 * @param cls our "struct Plugin*"
1162 * @param offset offset of the result
1163 * @param type entries of which type should be considered?
1164 * Use 0 for any type.
1165 * @param proc function to call on a matching value or NULL
1166 * @param proc_cls closure for iter
1169 mysql_plugin_get_zero_anonymity (void *cls, uint64_t offset,
1170 enum GNUNET_BLOCK_Type type,
1171 PluginDatumProcessor proc, void *proc_cls)
1173 struct Plugin *plugin = cls;
1174 unsigned long long rvalue =
1175 (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1178 execute_select (plugin, plugin->zero_iter, proc, proc_cls, MYSQL_TYPE_LONG,
1179 &type, GNUNET_YES, MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES,
1180 MYSQL_TYPE_LONG, &type, GNUNET_YES, MYSQL_TYPE_LONGLONG,
1181 &rvalue, GNUNET_YES, -1);
1186 * Context for 'repl_proc' function.
1194 struct Plugin *plugin;
1197 * Function to call for the result (or the NULL).
1199 PluginDatumProcessor proc;
1209 * Wrapper for the processor for 'mysql_plugin_get_replication'.
1210 * Decrements the replication counter and calls the original
1213 * @param cls closure
1214 * @param key key for the content
1215 * @param size number of bytes in data
1216 * @param data content stored
1217 * @param type type of the content
1218 * @param priority priority of the content
1219 * @param anonymity anonymity-level for the content
1220 * @param expiration expiration time for the content
1221 * @param uid unique identifier for the datum;
1222 * maybe 0 if no unique identifier is available
1224 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
1225 * (continue on call to "next", of course),
1226 * GNUNET_NO to delete the item and continue (if supported)
1229 repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
1230 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
1231 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
1234 struct ReplCtx *rc = cls;
1235 struct Plugin *plugin = rc->plugin;
1236 unsigned long long oid;
1241 rc->proc (rc->proc_cls, key, size, data, type, priority, anonymity,
1245 oid = (unsigned long long) uid;
1247 prepared_statement_run (plugin, plugin->dec_repl, NULL,
1248 MYSQL_TYPE_LONGLONG, &oid, GNUNET_YES, -1);
1249 if (iret == GNUNET_SYSERR)
1251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1252 "Failed to reduce replication counter\n");
1253 return GNUNET_SYSERR;
1261 * Get a random item for replication. Returns a single, not expired,
1262 * random item from those with the highest replication counters. The
1263 * item's replication counter is decremented by one IF it was positive
1264 * before. Call 'proc' with all values ZERO or NULL if the datastore
1267 * @param cls closure
1268 * @param proc function to call the value (once only).
1269 * @param proc_cls closure for proc
1272 mysql_plugin_get_replication (void *cls, PluginDatumProcessor proc,
1275 struct Plugin *plugin = cls;
1277 unsigned long long rvalue;
1283 rc.proc_cls = proc_cls;
1284 memset (&results, 0, sizeof (results));
1285 results.buffer_type = MYSQL_TYPE_LONG;
1286 results.buffer = &repl;
1287 results.is_unsigned = GNUNET_YES;
1290 prepared_statement_run_select (plugin, plugin->max_repl, 1, &results, -1))
1292 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1297 (unsigned long long) GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
1299 execute_select (plugin, plugin->select_replication, &repl_proc, &rc,
1300 MYSQL_TYPE_LONG, &repl, GNUNET_YES, MYSQL_TYPE_LONGLONG,
1301 &rvalue, GNUNET_YES, MYSQL_TYPE_LONG, &repl, GNUNET_YES,
1302 MYSQL_TYPE_LONGLONG, &rvalue, GNUNET_YES, -1);
1308 * Get all of the keys in the datastore.
1310 * @param cls closure
1311 * @param proc function to call on each key
1312 * @param proc_cls closure for proc
1315 mysql_plugin_get_keys (void *cls,
1316 PluginKeyProcessor proc,
1319 struct Plugin *plugin = cls;
1320 const char *query = "SELECT hash FROM gn090";
1322 MYSQL_STMT *statement;
1323 GNUNET_HashCode key;
1324 MYSQL_BIND cbind[1];
1325 unsigned long length;
1327 statement = mysql_stmt_init (plugin->dbf);
1328 if (statement == NULL)
1333 if (mysql_stmt_prepare (statement, query, strlen (query)))
1335 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
1336 _("Failed to prepare statement `%s'\n"), query);
1337 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR, "mysql_stmt_prepare", plugin);
1338 mysql_stmt_close (statement);
1342 GNUNET_assert (proc != NULL);
1343 if (mysql_stmt_execute (statement))
1345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1346 _("`%s' for `%s' failed at %s:%d with error: %s\n"),
1347 "mysql_stmt_execute", query, __FILE__, __LINE__,
1348 mysql_stmt_error (statement));
1349 mysql_stmt_close (statement);
1353 memset (cbind, 0, sizeof (cbind));
1354 cbind[0].buffer_type = MYSQL_TYPE_BLOB;
1355 cbind[0].buffer = &key;
1356 cbind[0].buffer_length = sizeof (key);
1357 cbind[0].length = &length;
1358 cbind[0].is_unsigned = GNUNET_NO;
1359 if (mysql_stmt_bind_result (statement, cbind))
1361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1362 _("`%s' failed at %s:%d with error: %s\n"),
1363 "mysql_stmt_bind_result", __FILE__, __LINE__,
1364 mysql_stmt_error (statement));
1368 while (0 == (ret = mysql_stmt_fetch (statement)))
1370 if (sizeof (GNUNET_HashCode) == length)
1371 proc (proc_cls, &key, 1);
1373 if (ret != MYSQL_NO_DATA)
1375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1376 _("`%s' failed at %s:%d with error: %s\n"),
1377 "mysql_stmt_fetch", __FILE__, __LINE__,
1378 mysql_stmt_error (statement));
1379 mysql_stmt_close (statement);
1383 mysql_stmt_close (statement);
1388 * Context for 'expi_proc' function.
1396 struct Plugin *plugin;
1399 * Function to call for the result (or the NULL).
1401 PluginDatumProcessor proc;
1412 * Wrapper for the processor for 'mysql_plugin_get_expiration'.
1413 * If no expired value was found, we do a second query for
1414 * low-priority content.
1416 * @param cls closure
1417 * @param key key for the content
1418 * @param size number of bytes in data
1419 * @param data content stored
1420 * @param type type of the content
1421 * @param priority priority of the content
1422 * @param anonymity anonymity-level for the content
1423 * @param expiration expiration time for the content
1424 * @param uid unique identifier for the datum;
1425 * maybe 0 if no unique identifier is available
1427 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
1428 * (continue on call to "next", of course),
1429 * GNUNET_NO to delete the item and continue (if supported)
1432 expi_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
1433 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
1434 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
1437 struct ExpiCtx *rc = cls;
1438 struct Plugin *plugin = rc->plugin;
1442 execute_select (plugin, plugin->select_priority, rc->proc, rc->proc_cls,
1444 return GNUNET_SYSERR;
1446 return rc->proc (rc->proc_cls, key, size, data, type, priority, anonymity,
1452 * Get a random item for expiration.
1453 * Call 'proc' with all values ZERO or NULL if the datastore is empty.
1455 * @param cls closure
1456 * @param proc function to call the value (once only).
1457 * @param proc_cls closure for proc
1460 mysql_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
1463 struct Plugin *plugin = cls;
1469 rc.proc_cls = proc_cls;
1470 nt = (long long) GNUNET_TIME_absolute_get ().abs_value;
1471 execute_select (plugin, plugin->select_expiration, expi_proc, &rc,
1472 MYSQL_TYPE_LONGLONG, &nt, GNUNET_YES, -1);
1480 * @param cls the "struct Plugin*"
1483 mysql_plugin_drop (void *cls)
1485 struct Plugin *plugin = cls;
1487 if (GNUNET_OK != run_statement (plugin, "DROP TABLE gn090"))
1489 plugin->env->duc (plugin->env->cls, 0);
1494 * Entry point for the plugin.
1496 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1497 * @return our "struct Plugin*"
1500 libgnunet_plugin_datastore_mysql_init (void *cls)
1502 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1503 struct GNUNET_DATASTORE_PluginFunctions *api;
1504 struct Plugin *plugin;
1506 plugin = GNUNET_malloc (sizeof (struct Plugin));
1508 plugin->cnffile = get_my_cnf_path (env->cfg);
1509 if (GNUNET_OK != iopen (plugin))
1512 GNUNET_free_non_null (plugin->cnffile);
1513 GNUNET_free (plugin);
1516 #define MRUNS(a) (GNUNET_OK != run_statement (plugin, a) )
1517 #define PINIT(a,b) (NULL == (a = prepared_statement_create(plugin, b)))
1519 ("CREATE TABLE IF NOT EXISTS gn090 ("
1520 " repl INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1521 " type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1522 " prio INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1523 " anonLevel INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1524 " expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
1525 " rvalue BIGINT UNSIGNED NOT NULL,"
1526 " hash BINARY(64) NOT NULL DEFAULT '',"
1527 " vhash BINARY(64) NOT NULL DEFAULT '',"
1528 " value BLOB NOT NULL DEFAULT ''," " uid BIGINT NOT NULL AUTO_INCREMENT,"
1529 " PRIMARY KEY (uid)," " INDEX idx_hash (hash(64)),"
1530 " INDEX idx_hash_uid (hash(64),uid),"
1531 " INDEX idx_hash_vhash (hash(64),vhash(64)),"
1532 " INDEX idx_hash_type_uid (hash(64),type,rvalue),"
1533 " INDEX idx_prio (prio)," " INDEX idx_repl_rvalue (repl,rvalue),"
1534 " INDEX idx_expire (expire),"
1535 " INDEX idx_anonLevel_type_rvalue (anonLevel,type,rvalue)"
1536 ") ENGINE=InnoDB") || MRUNS ("SET AUTOCOMMIT = 1") ||
1537 PINIT (plugin->insert_entry, INSERT_ENTRY) ||
1538 PINIT (plugin->delete_entry_by_uid, DELETE_ENTRY_BY_UID) ||
1539 PINIT (plugin->select_entry_by_hash, SELECT_ENTRY_BY_HASH) ||
1540 PINIT (plugin->select_entry_by_hash_and_vhash,
1541 SELECT_ENTRY_BY_HASH_AND_VHASH) ||
1542 PINIT (plugin->select_entry_by_hash_and_type,
1543 SELECT_ENTRY_BY_HASH_AND_TYPE) ||
1544 PINIT (plugin->select_entry_by_hash_vhash_and_type,
1545 SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE) ||
1546 PINIT (plugin->count_entry_by_hash, COUNT_ENTRY_BY_HASH) ||
1547 PINIT (plugin->get_size, SELECT_SIZE) ||
1548 PINIT (plugin->count_entry_by_hash_and_vhash,
1549 COUNT_ENTRY_BY_HASH_AND_VHASH) ||
1550 PINIT (plugin->count_entry_by_hash_and_type, COUNT_ENTRY_BY_HASH_AND_TYPE)
1551 || PINIT (plugin->count_entry_by_hash_vhash_and_type,
1552 COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE) ||
1553 PINIT (plugin->update_entry, UPDATE_ENTRY) ||
1554 PINIT (plugin->dec_repl, DEC_REPL) ||
1555 PINIT (plugin->zero_iter, SELECT_IT_NON_ANONYMOUS) ||
1556 PINIT (plugin->select_expiration, SELECT_IT_EXPIRATION) ||
1557 PINIT (plugin->select_priority, SELECT_IT_PRIORITY) ||
1558 PINIT (plugin->max_repl, SELECT_MAX_REPL) ||
1559 PINIT (plugin->select_replication, SELECT_IT_REPLICATION))
1562 GNUNET_free_non_null (plugin->cnffile);
1563 GNUNET_free (plugin);
1569 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1571 api->estimate_size = &mysql_plugin_estimate_size;
1572 api->put = &mysql_plugin_put;
1573 api->update = &mysql_plugin_update;
1574 api->get_key = &mysql_plugin_get_key;
1575 api->get_replication = &mysql_plugin_get_replication;
1576 api->get_expiration = &mysql_plugin_get_expiration;
1577 api->get_zero_anonymity = &mysql_plugin_get_zero_anonymity;
1578 api->get_keys = &mysql_plugin_get_keys;
1579 api->drop = &mysql_plugin_drop;
1580 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "mysql",
1581 _("Mysql database running\n"));
1587 * Exit point from the plugin.
1588 * @param cls our "struct Plugin*"
1589 * @return always NULL
1592 libgnunet_plugin_datastore_mysql_done (void *cls)
1594 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1595 struct Plugin *plugin = api->cls;
1596 struct GNUNET_MysqlStatementHandle *s;
1599 while (NULL != (s = plugin->shead))
1601 GNUNET_CONTAINER_DLL_remove (plugin->shead, plugin->stail, s);
1602 GNUNET_free (s->query);
1605 GNUNET_free_non_null (plugin->cnffile);
1606 GNUNET_free (plugin);
1608 mysql_library_end ();
1612 /* end of plugin_datastore_mysql.c */