2 This file is part of GNUnet
3 (C) 2009, 2010 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,
61 * and do the following. [You should replace $USER with the username
62 * that will be running the gnunetd process].
65 CREATE DATABASE gnunet;
66 GRANT select,insert,update,delete,create,alter,drop,create temporary tables
67 ON gnunet.* TO $USER@localhost;
68 SET PASSWORD FOR $USER@localhost=PASSWORD('$the_password_you_like');
72 * 3) In the $HOME directory of $USER, create a ".my.cnf" file
73 * with the following lines
78 password=$the_password_you_like
82 * Thats it. Note that .my.cnf file is a security risk unless its on
83 * a safe partition etc. The $HOME/.my.cnf can of course be a symbolic
84 * link. Even greater security risk can be achieved by setting no
85 * password for $USER. Luckily $USER has only priviledges to mess
86 * up GNUnet's tables, nothing else (unless you give him more,
89 * 4) Still, perhaps you should briefly try if the DB connection
90 * works. First, login as $USER. Then use,
93 * $ mysql -u $USER -p $the_password_you_like
97 * If you get the message "Database changed" it probably works.
99 * [If you get "ERROR 2002: Can't connect to local MySQL server
100 * through socket '/tmp/mysql.sock' (2)" it may be resolvable by
101 * "ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock"
102 * so there may be some additional trouble depending on your mysql setup.]
106 * - Its probably healthy to check your tables for inconsistencies
107 * every now and then.
108 * - If you get odd SEGVs on gnunetd startup, it might be that the mysql
109 * databases have been corrupted.
110 * - The tables can be verified/fixed in two ways;
111 * 1) by running mysqlcheck -A, or
112 * 2) by executing (inside of mysql using the GNUnet database):
113 * mysql> REPAIR TABLE gn090;
114 * mysql> REPAIR TABLE gn072;
118 * If you have problems related to the mysql module, your best
119 * friend is probably the mysql manual. The first thing to check
120 * is that mysql is basically operational, that you can connect
121 * to it, create tables, issue queries etc.
124 * - use FOREIGN KEY for 'uid/vkey'
125 * - consistent naming of uid/vkey
128 #include "platform.h"
129 #include "plugin_datastore.h"
130 #include "gnunet_util_lib.h"
131 #include <mysql/mysql.h>
133 #define DEBUG_MYSQL GNUNET_NO
135 #define MAX_DATUM_SIZE 65536
138 * Maximum number of supported parameters for a prepared
139 * statement. Increase if needed.
144 * Die with an error message that indicates
145 * a failure of the command 'cmd' with the message given
146 * by strerror(errno).
148 #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);
151 * Log an error message at log-level 'level' that indicates
152 * a failure of the command 'cmd' on file 'filename'
153 * with the message given by strerror(errno).
155 #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);
158 /* warning, slighly crazy mysql statements ahead. Essentially, MySQL does not handle
159 "OR" very well, so we need to use UNION instead. And UNION does not
160 automatically apply a LIMIT on the outermost clause, so we need to
161 repeat ourselves quite a bit. All hail the performance gods (and thanks
162 to #mysql on freenode) */
163 #define SELECT_IT_LOW_PRIORITY "(SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX(prio) WHERE (prio = ? AND vkey > ?) "\
164 "ORDER BY prio ASC,vkey ASC LIMIT 1) " \
166 "(SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX(prio) WHERE (prio > ? AND vkey != ?)"\
167 "ORDER BY prio ASC,vkey ASC LIMIT 1)"\
168 "ORDER BY prio ASC,vkey ASC LIMIT 1"
170 #define SELECT_IT_NON_ANONYMOUS "(SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX(prio) WHERE (prio = ? AND vkey < ?)"\
171 " AND anonLevel=0 ORDER BY prio DESC,vkey DESC LIMIT 1) "\
173 "(SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX(prio) WHERE (prio < ? AND vkey != ?)"\
174 " AND anonLevel=0 ORDER BY prio DESC,vkey DESC LIMIT 1) "\
175 "ORDER BY prio DESC,vkey DESC LIMIT 1"
177 #define SELECT_IT_EXPIRATION_TIME "(SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX(expire) WHERE (expire = ? AND vkey > ?) "\
178 "ORDER BY expire ASC,vkey ASC LIMIT 1) "\
180 "(SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX(expire) WHERE (expire > ? AND vkey != ?) "\
181 "ORDER BY expire ASC,vkey ASC LIMIT 1)"\
182 "ORDER BY expire ASC,vkey ASC LIMIT 1"
185 #define SELECT_IT_MIGRATION_ORDER "(SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX(expire) WHERE (expire = ? AND vkey < ?)"\
186 " AND expire > ? AND type!=3"\
187 " ORDER BY expire DESC,vkey DESC LIMIT 1) "\
189 "(SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX(expire) WHERE (expire < ? AND vkey != ?)"\
190 " AND expire > ? AND type!=3"\
191 " ORDER BY expire DESC,vkey DESC LIMIT 1)"\
192 "ORDER BY expire DESC,vkey DESC LIMIT 1"
196 struct GNUNET_MysqlStatementHandle
198 struct GNUNET_MysqlStatementHandle *next;
200 struct GNUNET_MysqlStatementHandle *prev;
204 MYSQL_STMT *statement;
211 * Context for the universal iterator.
213 struct NextRequestClosure;
216 * Type of a function that will prepare
217 * the next iteration.
220 * @param nc the next context; NULL for the last
221 * call which gives the callback a chance to
222 * clean up the closure
223 * @return GNUNET_OK on success, GNUNET_NO if there are
224 * no more values, GNUNET_SYSERR on error
226 typedef int (*PrepareFunction)(void *cls,
227 struct NextRequestClosure *nc);
230 struct NextRequestClosure
232 struct Plugin *plugin;
234 struct GNUNET_TIME_Absolute now;
237 * Function to call to prepare the next
240 PrepareFunction prep;
251 unsigned int iter_select;
253 PluginIterator dviter;
257 unsigned int last_prio;
259 unsigned long long last_expire;
261 unsigned long long last_vkey;
268 * Context for all functions in this plugin.
273 * Our execution environment.
275 struct GNUNET_DATASTORE_PluginEnvironment *env;
279 struct GNUNET_MysqlStatementHandle *shead;
281 struct GNUNET_MysqlStatementHandle *stail;
284 * Filename of "my.cnf" (msyql configuration).
289 * Closure of the 'next_task' (must be freed if 'next_task' is cancelled).
291 struct NextRequestClosure *next_task_nc;
294 * Pending task with scheduler for running the next request.
296 GNUNET_SCHEDULER_TaskIdentifier next_task;
299 * Statements dealing with gn072 table
301 #define SELECT_VALUE "SELECT value FROM gn072 WHERE vkey=?"
302 struct GNUNET_MysqlStatementHandle *select_value;
304 #define DELETE_VALUE "DELETE FROM gn072 WHERE vkey=?"
305 struct GNUNET_MysqlStatementHandle *delete_value;
307 #define INSERT_VALUE "INSERT INTO gn072 (value) VALUES (?)"
308 struct GNUNET_MysqlStatementHandle *insert_value;
311 * Statements dealing with gn090 table
313 #define INSERT_ENTRY "INSERT INTO gn090 (type,prio,anonLevel,expire,hash,vhash,vkey) VALUES (?,?,?,?,?,?,?)"
314 struct GNUNET_MysqlStatementHandle *insert_entry;
316 #define DELETE_ENTRY_BY_VKEY "DELETE FROM gn090 WHERE vkey=?"
317 struct GNUNET_MysqlStatementHandle *delete_entry_by_vkey;
319 #define SELECT_ENTRY_BY_HASH "SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX (hash_vkey) WHERE hash=? AND vkey > ? ORDER BY vkey ASC LIMIT 1 OFFSET ?"
320 struct GNUNET_MysqlStatementHandle *select_entry_by_hash;
322 #define SELECT_ENTRY_BY_HASH_AND_VHASH "SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX (hash_vhash_vkey) WHERE hash=? AND vhash=? AND vkey > ? ORDER BY vkey ASC LIMIT 1 OFFSET ?"
323 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_vhash;
325 #define SELECT_ENTRY_BY_HASH_AND_TYPE "SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX (hash_vkey) WHERE hash=? AND vkey > ? AND type=? ORDER BY vkey ASC LIMIT 1 OFFSET ?"
326 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_and_type;
328 #define SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT type,prio,anonLevel,expire,hash,vkey FROM gn090 FORCE INDEX (hash_vhash_vkey) WHERE hash=? AND vhash=? AND vkey > ? AND type=? ORDER BY vkey ASC LIMIT 1 OFFSET ?"
329 struct GNUNET_MysqlStatementHandle *select_entry_by_hash_vhash_and_type;
331 #define COUNT_ENTRY_BY_HASH "SELECT count(*) FROM gn090 FORCE INDEX (hash) WHERE hash=?"
332 struct GNUNET_MysqlStatementHandle *count_entry_by_hash;
334 #define COUNT_ENTRY_BY_HASH_AND_VHASH "SELECT count(*) FROM gn090 FORCE INDEX (hash_vhash_vkey) WHERE hash=? AND vhash=?"
335 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_vhash;
337 #define COUNT_ENTRY_BY_HASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (hash) WHERE hash=? AND type=?"
338 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_and_type;
340 #define COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE "SELECT count(*) FROM gn090 FORCE INDEX (hash_vhash) WHERE hash=? AND vhash=? AND type=?"
341 struct GNUNET_MysqlStatementHandle *count_entry_by_hash_vhash_and_type;
343 #define UPDATE_ENTRY "UPDATE gn090 SET prio=prio+?,expire=IF(expire>=?,expire,?) WHERE vkey=?"
344 struct GNUNET_MysqlStatementHandle *update_entry;
346 #define SELECT_SIZE "SELECT SUM(BIT_LENGTH(value) DIV 8) FROM gn072"
347 struct GNUNET_MysqlStatementHandle *get_size;
349 struct GNUNET_MysqlStatementHandle *iter[4];
355 * Obtain the location of ".my.cnf".
356 * @return NULL on error
359 get_my_cnf_path (const struct GNUNET_CONFIGURATION_Handle *cfg)
370 pw = getpwuid (getuid ());
373 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
378 GNUNET_CONFIGURATION_have_value (cfg,
379 "datastore-mysql", "CONFIG"))
381 GNUNET_assert (GNUNET_OK ==
382 GNUNET_CONFIGURATION_get_value_filename (cfg,
383 "datastore-mysql", "CONFIG", &cnffile));
384 configured = GNUNET_YES;
388 home_dir = GNUNET_strdup (pw->pw_dir);
390 home_dir = (char *) GNUNET_malloc (_MAX_PATH + 1);
391 plibc_conv_to_win_path ("~/", home_dir);
393 GNUNET_asprintf (&cnffile, "%s/.my.cnf", home_dir);
394 GNUNET_free (home_dir);
395 configured = GNUNET_NO;
397 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
398 _("Trying to use file `%s' for MySQL configuration.\n"),
400 if ((0 != STAT (cnffile, &st)) ||
401 (0 != ACCESS (cnffile, R_OK)) || (!S_ISREG (st.st_mode)))
403 if (configured == GNUNET_YES)
404 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
405 _("Could not access file `%s': %s\n"), cnffile,
407 GNUNET_free (cnffile);
416 * Free a prepared statement.
419 prepared_statement_destroy (struct Plugin *plugin,
420 struct GNUNET_MysqlStatementHandle
423 GNUNET_CONTAINER_DLL_remove (plugin->shead,
427 mysql_stmt_close (s->statement);
428 GNUNET_free (s->query);
434 * Close database connection and all prepared statements (we got a DB
438 iclose (struct Plugin *plugin)
440 struct GNUNET_MysqlStatementHandle *spos;
442 spos = plugin->shead;
443 while (NULL != plugin->shead)
444 prepared_statement_destroy (plugin,
446 if (plugin->dbf != NULL)
448 mysql_close (plugin->dbf);
456 * Open the connection with the database (and initialize
457 * our default options).
459 * @return GNUNET_OK on success
462 iopen (struct Plugin *ret)
467 char *mysql_password;
468 unsigned long long mysql_port;
470 unsigned int timeout;
472 ret->dbf = mysql_init (NULL);
473 if (ret->dbf == NULL)
474 return GNUNET_SYSERR;
475 if (ret->cnffile != NULL)
476 mysql_options (ret->dbf, MYSQL_READ_DEFAULT_FILE, ret->cnffile);
477 mysql_options (ret->dbf, MYSQL_READ_DEFAULT_GROUP, "client");
479 mysql_options (ret->dbf, MYSQL_OPT_RECONNECT, &reconnect);
480 mysql_options (ret->dbf,
481 MYSQL_OPT_CONNECT_TIMEOUT, (const void *) &timeout);
482 mysql_options(ret->dbf, MYSQL_SET_CHARSET_NAME, "UTF8");
483 timeout = 60; /* in seconds */
484 mysql_options (ret->dbf, MYSQL_OPT_READ_TIMEOUT, (const void *) &timeout);
485 mysql_options (ret->dbf, MYSQL_OPT_WRITE_TIMEOUT, (const void *) &timeout);
487 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
488 "datastore-mysql", "DATABASE"))
489 GNUNET_assert (GNUNET_OK ==
490 GNUNET_CONFIGURATION_get_value_string (ret->env->cfg,
491 "datastore-mysql", "DATABASE",
494 mysql_dbname = GNUNET_strdup ("gnunet");
496 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
497 "datastore-mysql", "USER"))
499 GNUNET_assert (GNUNET_OK ==
500 GNUNET_CONFIGURATION_get_value_string (ret->env->cfg,
501 "datastore-mysql", "USER",
504 mysql_password = NULL;
505 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
506 "datastore-mysql", "PASSWORD"))
508 GNUNET_assert (GNUNET_OK ==
509 GNUNET_CONFIGURATION_get_value_string (ret->env->cfg,
510 "datastore-mysql", "PASSWORD",
514 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
515 "datastore-mysql", "HOST"))
517 GNUNET_assert (GNUNET_OK ==
518 GNUNET_CONFIGURATION_get_value_string (ret->env->cfg,
519 "datastore-mysql", "HOST",
523 if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (ret->env->cfg,
524 "datastore-mysql", "PORT"))
526 GNUNET_assert (GNUNET_OK ==
527 GNUNET_CONFIGURATION_get_value_number (ret->env->cfg, "datastore-mysql",
528 "PORT", &mysql_port));
531 GNUNET_assert (mysql_dbname != NULL);
532 mysql_real_connect (ret->dbf, mysql_server, mysql_user, mysql_password,
533 mysql_dbname, (unsigned int) mysql_port, NULL,
534 CLIENT_IGNORE_SIGPIPE);
535 GNUNET_free_non_null (mysql_server);
536 GNUNET_free_non_null (mysql_user);
537 GNUNET_free_non_null (mysql_password);
538 GNUNET_free (mysql_dbname);
539 if (mysql_error (ret->dbf)[0])
541 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR,
542 "mysql_real_connect", ret);
543 return GNUNET_SYSERR;
550 * Run the given MySQL statement.
552 * @return GNUNET_OK on success, GNUNET_SYSERR on error
555 run_statement (struct Plugin *plugin,
556 const char *statement)
558 if ((NULL == plugin->dbf) && (GNUNET_OK != iopen (plugin)))
559 return GNUNET_SYSERR;
560 mysql_query (plugin->dbf, statement);
561 if (mysql_error (plugin->dbf)[0])
563 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR,
564 "mysql_query", plugin);
566 return GNUNET_SYSERR;
573 * Create a prepared statement.
575 * @return NULL on error
577 static struct GNUNET_MysqlStatementHandle *
578 prepared_statement_create (struct Plugin *plugin,
579 const char *statement)
581 struct GNUNET_MysqlStatementHandle *ret;
583 ret = GNUNET_malloc (sizeof (struct GNUNET_MysqlStatementHandle));
584 ret->query = GNUNET_strdup (statement);
585 GNUNET_CONTAINER_DLL_insert (plugin->shead,
593 * Prepare a statement for running.
595 * @return GNUNET_OK on success
598 prepare_statement (struct Plugin *plugin,
599 struct GNUNET_MysqlStatementHandle *ret)
601 if (GNUNET_YES == ret->valid)
603 if ((NULL == plugin->dbf) &&
604 (GNUNET_OK != iopen (plugin)))
605 return GNUNET_SYSERR;
606 ret->statement = mysql_stmt_init (plugin->dbf);
607 if (ret->statement == NULL)
610 return GNUNET_SYSERR;
612 if (mysql_stmt_prepare (ret->statement,
614 strlen (ret->query)))
616 LOG_MYSQL (GNUNET_ERROR_TYPE_ERROR,
617 "mysql_stmt_prepare",
619 mysql_stmt_close (ret->statement);
620 ret->statement = NULL;
622 return GNUNET_SYSERR;
624 ret->valid = GNUNET_YES;
631 * Bind the parameters for the given MySQL statement
634 * @param s statement to bind and run
635 * @param ap arguments for the binding
636 * @return GNUNET_SYSERR on error, GNUNET_OK on success
639 init_params (struct Plugin *plugin,
640 struct GNUNET_MysqlStatementHandle *s,
643 MYSQL_BIND qbind[MAX_PARAM];
646 enum enum_field_types ft;
648 pc = mysql_stmt_param_count (s->statement);
651 /* increase internal constant! */
653 return GNUNET_SYSERR;
655 memset (qbind, 0, sizeof (qbind));
658 while ((pc > 0) && (-1 != (ft = va_arg (ap, enum enum_field_types))))
660 qbind[off].buffer_type = ft;
663 case MYSQL_TYPE_FLOAT:
664 qbind[off].buffer = va_arg (ap, float *);
666 case MYSQL_TYPE_LONGLONG:
667 qbind[off].buffer = va_arg (ap, unsigned long long *);
668 qbind[off].is_unsigned = va_arg (ap, int);
670 case MYSQL_TYPE_LONG:
671 qbind[off].buffer = va_arg (ap, unsigned int *);
672 qbind[off].is_unsigned = va_arg (ap, int);
674 case MYSQL_TYPE_VAR_STRING:
675 case MYSQL_TYPE_STRING:
676 case MYSQL_TYPE_BLOB:
677 qbind[off].buffer = va_arg (ap, void *);
678 qbind[off].buffer_length = va_arg (ap, unsigned long);
679 qbind[off].length = va_arg (ap, unsigned long *);
682 /* unsupported type */
684 return GNUNET_SYSERR;
689 if (!((pc == 0) && (ft != -1) && (va_arg (ap, int) == -1)))
692 return GNUNET_SYSERR;
694 if (mysql_stmt_bind_param (s->statement, qbind))
696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
697 _("`%s' failed at %s:%d with error: %s\n"),
698 "mysql_stmt_bind_param",
699 __FILE__, __LINE__, mysql_stmt_error (s->statement));
701 return GNUNET_SYSERR;
703 if (mysql_stmt_execute (s->statement))
705 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
706 _("`%s' failed at %s:%d with error: %s\n"),
707 "mysql_stmt_execute",
708 __FILE__, __LINE__, mysql_stmt_error (s->statement));
710 return GNUNET_SYSERR;
716 * Type of a callback that will be called for each
717 * data set returned from MySQL.
719 * @param cls user-defined argument
720 * @param num_values number of elements in values
721 * @param values values returned by MySQL
722 * @return GNUNET_OK to continue iterating, GNUNET_SYSERR to abort
724 typedef int (*GNUNET_MysqlDataProcessor) (void *cls,
725 unsigned int num_values,
726 MYSQL_BIND * values);
730 * Run a prepared SELECT statement.
732 * @param result_size number of elements in results array
733 * @param results pointer to already initialized MYSQL_BIND
734 * array (of sufficient size) for passing results
735 * @param processor function to call on each result
736 * @param processor_cls extra argument to processor
737 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
738 * values (size + buffer-reference for pointers); terminated
740 * @return GNUNET_SYSERR on error, otherwise
741 * the number of successfully affected (or queried) rows
744 prepared_statement_run_select (struct Plugin *plugin,
745 struct GNUNET_MysqlStatementHandle
747 unsigned int result_size,
748 MYSQL_BIND * results,
749 GNUNET_MysqlDataProcessor
750 processor, void *processor_cls,
758 if (GNUNET_OK != prepare_statement (plugin, s))
761 return GNUNET_SYSERR;
763 va_start (ap, processor_cls);
764 if (GNUNET_OK != init_params (plugin, s, ap))
768 return GNUNET_SYSERR;
771 rsize = mysql_stmt_field_count (s->statement);
772 if (rsize > result_size)
775 return GNUNET_SYSERR;
777 if (mysql_stmt_bind_result (s->statement, results))
779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
780 _("`%s' failed at %s:%d with error: %s\n"),
781 "mysql_stmt_bind_result",
782 __FILE__, __LINE__, mysql_stmt_error (s->statement));
784 return GNUNET_SYSERR;
790 ret = mysql_stmt_fetch (s->statement);
791 if (ret == MYSQL_NO_DATA)
795 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
796 _("`%s' failed at %s:%d with error: %s\n"),
798 __FILE__, __LINE__, mysql_stmt_error (s->statement));
800 return GNUNET_SYSERR;
802 if (processor != NULL)
803 if (GNUNET_OK != processor (processor_cls, rsize, results))
807 mysql_stmt_reset (s->statement);
813 * Run a prepared statement that does NOT produce results.
815 * @param ... pairs and triplets of "MYSQL_TYPE_XXX" keys and their respective
816 * values (size + buffer-reference for pointers); terminated
818 * @param insert_id NULL or address where to store the row ID of whatever
819 * was inserted (only for INSERT statements!)
820 * @return GNUNET_SYSERR on error, otherwise
821 * the number of successfully affected rows
824 prepared_statement_run (struct Plugin *plugin,
825 struct GNUNET_MysqlStatementHandle *s,
826 unsigned long long *insert_id, ...)
831 if (GNUNET_OK != prepare_statement (plugin, s))
832 return GNUNET_SYSERR;
833 va_start (ap, insert_id);
834 if (GNUNET_OK != init_params (plugin, s, ap))
837 return GNUNET_SYSERR;
840 affected = mysql_stmt_affected_rows (s->statement);
841 if (NULL != insert_id)
842 *insert_id = (unsigned long long) mysql_stmt_insert_id (s->statement);
843 mysql_stmt_reset (s->statement);
849 * Delete an value from the gn072 table.
851 * @param vkey vkey identifying the value to delete
852 * @return GNUNET_OK on success, GNUNET_NO if no such value exists, GNUNET_SYSERR on error
855 do_delete_value (struct Plugin *plugin,
856 unsigned long long vkey)
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Deleting value %llu from gn072 table\n",
865 ret = prepared_statement_run (plugin,
866 plugin->delete_value,
869 &vkey, GNUNET_YES, -1);
876 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
877 "Deleting value %llu from gn072 table failed\n",
884 * Insert a value into the gn072 table.
886 * @param value the value to insert
887 * @param size size of the value
888 * @param vkey vkey identifying the value henceforth (set)
889 * @return GNUNET_OK on success, GNUNET_SYSERR on error
892 do_insert_value (struct Plugin *plugin,
893 const void *value, unsigned int size,
894 unsigned long long *vkey)
896 unsigned long length = size;
899 ret = prepared_statement_run (plugin,
900 plugin->insert_value,
903 value, length, &length, -1);
904 if (ret == GNUNET_OK)
907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908 "Inserted value number %llu with length %u into gn072 table\n",
915 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
916 "Failed to insert %u byte value into gn072 table\n",
923 * Delete an entry from the gn090 table.
925 * @param vkey vkey identifying the entry to delete
926 * @return GNUNET_OK on success, GNUNET_NO if no such value exists, GNUNET_SYSERR on error
929 do_delete_entry_by_vkey (struct Plugin *plugin,
930 unsigned long long vkey)
935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
936 "Deleting value %llu from gn090 table\n",
939 ret = prepared_statement_run (plugin,
940 plugin->delete_entry_by_vkey,
943 &vkey, GNUNET_YES, -1);
950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
951 "Deleting value %llu from gn090 table failed\n",
959 return_ok (void *cls,
960 unsigned int num_values,
968 iterator_helper_prepare (void *cls,
969 struct NextRequestClosure *nrc)
971 struct Plugin *plugin;
976 plugin = nrc->plugin;
978 switch (nrc->iter_select)
982 ret = prepared_statement_run_select (plugin,
983 plugin->iter[nrc->iter_select],
1002 ret = prepared_statement_run_select (plugin,
1003 plugin->iter[nrc->iter_select],
1008 MYSQL_TYPE_LONGLONG,
1011 MYSQL_TYPE_LONGLONG,
1014 MYSQL_TYPE_LONGLONG,
1017 MYSQL_TYPE_LONGLONG,
1022 ret = prepared_statement_run_select (plugin,
1023 plugin->iter[nrc->iter_select],
1028 MYSQL_TYPE_LONGLONG,
1031 MYSQL_TYPE_LONGLONG,
1034 MYSQL_TYPE_LONGLONG,
1037 MYSQL_TYPE_LONGLONG,
1040 MYSQL_TYPE_LONGLONG,
1043 MYSQL_TYPE_LONGLONG,
1055 * Continuation of "mysql_next_request".
1057 * @param next_cls the next context
1058 * @param tc the task context (unused)
1061 mysql_next_request_cont (void *next_cls,
1062 const struct GNUNET_SCHEDULER_TaskContext *tc)
1064 struct NextRequestClosure *nrc = next_cls;
1065 struct Plugin *plugin;
1068 unsigned int priority;
1069 unsigned int anonymity;
1070 unsigned long long exp;
1071 unsigned long long vkey;
1072 unsigned long hashSize;
1073 GNUNET_HashCode key;
1074 struct GNUNET_TIME_Absolute expiration;
1075 unsigned long length;
1076 MYSQL_BIND *rbind; /* size 7 */
1077 MYSQL_BIND dbind[1];
1078 char datum[GNUNET_SERVER_MAX_MESSAGE_SIZE];
1080 plugin = nrc->plugin;
1081 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
1082 plugin->next_task_nc = NULL;
1085 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
1086 nrc->now = GNUNET_TIME_absolute_get ();
1087 hashSize = sizeof (GNUNET_HashCode);
1088 memset (nrc->rbind, 0, sizeof (nrc->rbind));
1090 rbind[0].buffer_type = MYSQL_TYPE_LONG;
1091 rbind[0].buffer = &type;
1092 rbind[0].is_unsigned = 1;
1093 rbind[1].buffer_type = MYSQL_TYPE_LONG;
1094 rbind[1].buffer = &priority;
1095 rbind[1].is_unsigned = 1;
1096 rbind[2].buffer_type = MYSQL_TYPE_LONG;
1097 rbind[2].buffer = &anonymity;
1098 rbind[2].is_unsigned = 1;
1099 rbind[3].buffer_type = MYSQL_TYPE_LONGLONG;
1100 rbind[3].buffer = &exp;
1101 rbind[3].is_unsigned = 1;
1102 rbind[4].buffer_type = MYSQL_TYPE_BLOB;
1103 rbind[4].buffer = &key;
1104 rbind[4].buffer_length = hashSize;
1105 rbind[4].length = &hashSize;
1106 rbind[5].buffer_type = MYSQL_TYPE_LONGLONG;
1107 rbind[5].buffer = &vkey;
1108 rbind[5].is_unsigned = GNUNET_YES;
1110 if ( (GNUNET_YES == nrc->end_it) ||
1111 (GNUNET_OK != nrc->prep (nrc->prep_cls,
1114 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
1115 nrc->last_vkey = vkey;
1116 nrc->last_prio = priority;
1117 nrc->last_expire = exp;
1118 if ( (rbind[4].buffer_length != sizeof (GNUNET_HashCode)) ||
1119 (hashSize != sizeof (GNUNET_HashCode)) )
1125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1126 "Found value %llu with prio %u, anon %u, expire %llu selecting from gn090 table\n",
1132 /* now do query on gn072 */
1133 length = sizeof (datum);
1134 memset (dbind, 0, sizeof (dbind));
1135 dbind[0].buffer_type = MYSQL_TYPE_BLOB;
1136 dbind[0].buffer_length = length;
1137 dbind[0].length = &length;
1138 dbind[0].buffer = datum;
1139 ret = prepared_statement_run_select (plugin,
1140 plugin->select_value,
1145 MYSQL_TYPE_LONGLONG,
1146 &vkey, GNUNET_YES, -1);
1147 GNUNET_break (ret <= 1); /* should only have one rbind! */
1150 if (ret != GNUNET_OK)
1153 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1154 _("Failed to obtain value %llu from table `%s'\n"),
1159 GNUNET_break (length <= sizeof(datum));
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "Calling iterator with value `%s' number %llu of size %u with type %u, priority %u, anonymity %u and expiration %llu\n",
1171 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
1172 expiration.value = exp;
1173 ret = nrc->dviter (nrc->dviter_cls,
1183 if (ret == GNUNET_SYSERR)
1185 nrc->end_it = GNUNET_YES;
1188 if (ret == GNUNET_NO)
1190 do_delete_value (plugin, vkey);
1191 do_delete_entry_by_vkey (plugin, vkey);
1193 plugin->env->duc (plugin->env->cls,
1198 /* call dviter with "end of set" */
1199 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
1200 nrc->dviter (nrc->dviter_cls,
1201 NULL, NULL, 0, NULL, 0, 0, 0,
1202 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1203 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
1204 nrc->prep (nrc->prep_cls, NULL);
1205 GNUNET_assert (nrc->plugin->next_task == GNUNET_SCHEDULER_NO_TASK);
1211 * Function invoked on behalf of a "PluginIterator"
1212 * asking the database plugin to call the iterator
1213 * with the next item.
1215 * @param next_cls whatever argument was given
1216 * to the PluginIterator as "next_cls".
1217 * @param end_it set to GNUNET_YES if we
1218 * should terminate the iteration early
1219 * (iterator should be still called once more
1220 * to signal the end of the iteration).
1223 mysql_plugin_next_request (void *next_cls,
1226 struct NextRequestClosure *nrc = next_cls;
1228 if (GNUNET_YES == end_it)
1229 nrc->end_it = GNUNET_YES;
1230 nrc->plugin->next_task_nc = nrc;
1231 nrc->plugin->next_task = GNUNET_SCHEDULER_add_now (nrc->plugin->env->sched,
1232 &mysql_next_request_cont,
1238 * Iterate over the items in the datastore
1239 * using the given query to select and order
1242 * @param type entries of which type should be considered?
1243 * Use 0 for any type.
1244 * @param iter never NULL
1245 * @param is_asc are we using ascending order?
1248 iterateHelper (struct Plugin *plugin,
1251 unsigned int iter_select,
1252 PluginIterator dviter,
1255 struct NextRequestClosure *nrc;
1257 nrc = GNUNET_malloc (sizeof (struct NextRequestClosure));
1258 nrc->plugin = plugin;
1260 nrc->iter_select = iter_select;
1261 nrc->dviter = dviter;
1262 nrc->dviter_cls = dviter_cls;
1263 nrc->prep = &iterator_helper_prepare;
1268 nrc->last_expire = 0;
1272 nrc->last_prio = 0x7FFFFFFFL;
1273 nrc->last_vkey = 0x7FFFFFFFFFFFFFFFLL; /* MySQL only supports 63 bits */
1274 nrc->last_expire = 0x7FFFFFFFFFFFFFFFLL; /* MySQL only supports 63 bits */
1276 mysql_plugin_next_request (nrc, GNUNET_NO);
1281 * Get an estimate of how much space the database is
1284 * @param cls our "struct Plugin *"
1285 * @return number of bytes used on disk
1287 static unsigned long long
1288 mysql_plugin_get_size (void *cls)
1290 struct Plugin *plugin = cls;
1291 MYSQL_BIND cbind[1];
1294 memset (cbind, 0, sizeof (cbind));
1296 cbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
1297 cbind[0].buffer = &total;
1298 cbind[0].is_unsigned = GNUNET_NO;
1300 prepared_statement_run_select (plugin,
1311 * Store an item in the datastore.
1313 * @param cls closure
1314 * @param key key for the item
1315 * @param size number of bytes in data
1316 * @param data content stored
1317 * @param type type of the content
1318 * @param priority priority of the content
1319 * @param anonymity anonymity-level for the content
1320 * @param expiration expiration time for the content
1321 * @param msg set to error message
1322 * @return GNUNET_OK on success
1325 mysql_plugin_put (void *cls,
1326 const GNUNET_HashCode * key,
1329 enum GNUNET_BLOCK_Type type,
1332 struct GNUNET_TIME_Absolute expiration,
1335 struct Plugin *plugin = cls;
1336 unsigned int itype = type;
1337 unsigned int ipriority = priority;
1338 unsigned int ianonymity = anonymity;
1339 unsigned long long lexpiration = expiration.value;
1340 unsigned long hashSize;
1341 unsigned long hashSize2;
1342 unsigned long long vkey;
1343 GNUNET_HashCode vhash;
1345 if (size > MAX_DATUM_SIZE)
1348 return GNUNET_SYSERR;
1350 hashSize = sizeof (GNUNET_HashCode);
1351 hashSize2 = sizeof (GNUNET_HashCode);
1352 GNUNET_CRYPTO_hash (data, size, &vhash);
1353 if (GNUNET_OK != do_insert_value (plugin,
1355 return GNUNET_SYSERR;
1357 prepared_statement_run (plugin,
1358 plugin->insert_entry,
1369 MYSQL_TYPE_LONGLONG,
1380 MYSQL_TYPE_LONGLONG,
1381 &vkey, GNUNET_YES, -1))
1383 do_delete_value (plugin, vkey);
1384 return GNUNET_SYSERR;
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "Inserted value `%s' number %llu with size %u into gn090 table\n",
1391 (unsigned int) size);
1394 plugin->env->duc (plugin->env->cls,
1401 * Select a subset of the items in the datastore and call
1402 * the given iterator for each of them.
1404 * @param cls our "struct Plugin*"
1405 * @param type entries of which type should be considered?
1406 * Use 0 for any type.
1407 * @param iter function to call on each matching value;
1408 * will be called once with a NULL value at the end
1409 * @param iter_cls closure for iter
1412 mysql_plugin_iter_low_priority (void *cls,
1413 enum GNUNET_BLOCK_Type type,
1414 PluginIterator iter,
1417 struct Plugin *plugin = cls;
1418 iterateHelper (plugin, type, GNUNET_YES,
1425 GNUNET_HashCode key;
1426 GNUNET_HashCode vhash;
1429 unsigned int anonymity;
1430 unsigned long long expiration;
1431 unsigned long long vkey;
1432 unsigned long long total;
1440 get_statement_prepare (void *cls,
1441 struct NextRequestClosure *nrc)
1443 struct GetContext *gc = cls;
1444 struct Plugin *plugin;
1446 unsigned int limit_off;
1447 unsigned long hashSize;
1454 if (gc->count == gc->total)
1456 plugin = nrc->plugin;
1457 hashSize = sizeof (GNUNET_HashCode);
1458 if (gc->count + gc->off == gc->total)
1459 nrc->last_vkey = 0; /* back to start */
1461 limit_off = gc->off;
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1466 "Obtaining result number %d/%lld at offset %d with lvc %llu for GET `%s'\n",
1471 GNUNET_h2s (&gc->key));
1478 prepared_statement_run_select
1480 plugin->select_entry_by_hash_vhash_and_type, 6, nrc->rbind, &return_ok,
1481 NULL, MYSQL_TYPE_BLOB, &gc->key, hashSize, &hashSize,
1482 MYSQL_TYPE_BLOB, &gc->vhash, hashSize, &hashSize,
1483 MYSQL_TYPE_LONGLONG, &nrc->last_vkey, GNUNET_YES, MYSQL_TYPE_LONG,
1484 &nrc->type, GNUNET_YES, MYSQL_TYPE_LONG, &limit_off, GNUNET_YES,
1490 prepared_statement_run_select
1492 plugin->select_entry_by_hash_and_type, 6, nrc->rbind, &return_ok, NULL,
1493 MYSQL_TYPE_BLOB, &gc->key, hashSize, &hashSize,
1494 MYSQL_TYPE_LONGLONG, &nrc->last_vkey, GNUNET_YES, MYSQL_TYPE_LONG,
1495 &nrc->type, GNUNET_YES, MYSQL_TYPE_LONG, &limit_off, GNUNET_YES,
1504 prepared_statement_run_select
1506 plugin->select_entry_by_hash_and_vhash, 6, nrc->rbind, &return_ok, NULL,
1507 MYSQL_TYPE_BLOB, &gc->key, hashSize, &hashSize, MYSQL_TYPE_BLOB,
1508 &gc->vhash, hashSize, &hashSize, MYSQL_TYPE_LONGLONG,
1509 &nrc->last_vkey, GNUNET_YES, MYSQL_TYPE_LONG, &limit_off,
1515 prepared_statement_run_select
1517 plugin->select_entry_by_hash, 6, nrc->rbind, &return_ok, NULL,
1518 MYSQL_TYPE_BLOB, &gc->key, hashSize, &hashSize,
1519 MYSQL_TYPE_LONGLONG, &nrc->last_vkey, GNUNET_YES, MYSQL_TYPE_LONG,
1520 &limit_off, GNUNET_YES, -1);
1529 * Iterate over the results for a particular key
1532 * @param cls closure
1533 * @param key maybe NULL (to match all entries)
1534 * @param vhash hash of the value, maybe NULL (to
1535 * match all values that have the right key).
1536 * Note that for DBlocks there is no difference
1537 * betwen key and vhash, but for other blocks
1539 * @param type entries of which type are relevant?
1540 * Use 0 for any type.
1541 * @param iter function to call on each matching value;
1542 * will be called once with a NULL value at the end
1543 * @param iter_cls closure for iter
1546 mysql_plugin_get (void *cls,
1547 const GNUNET_HashCode * key,
1548 const GNUNET_HashCode * vhash,
1549 enum GNUNET_BLOCK_Type type,
1550 PluginIterator iter, void *iter_cls)
1552 struct Plugin *plugin = cls;
1553 unsigned int itype = type;
1555 MYSQL_BIND cbind[1];
1556 struct GetContext *gc;
1557 struct NextRequestClosure *nrc;
1559 unsigned long hashSize;
1565 mysql_plugin_iter_low_priority (plugin,
1570 hashSize = sizeof (GNUNET_HashCode);
1571 memset (cbind, 0, sizeof (cbind));
1573 cbind[0].buffer_type = MYSQL_TYPE_LONGLONG;
1574 cbind[0].buffer = &total;
1575 cbind[0].is_unsigned = GNUNET_NO;
1581 prepared_statement_run_select
1583 plugin->count_entry_by_hash_vhash_and_type, 1, cbind, &return_ok, NULL,
1584 MYSQL_TYPE_BLOB, key, hashSize, &hashSize, MYSQL_TYPE_BLOB,
1585 vhash, hashSize, &hashSize, MYSQL_TYPE_LONG, &itype, GNUNET_YES,
1591 prepared_statement_run_select
1593 plugin->count_entry_by_hash_and_type, 1, cbind, &return_ok, NULL,
1594 MYSQL_TYPE_BLOB, key, hashSize, &hashSize, MYSQL_TYPE_LONG,
1595 &itype, GNUNET_YES, -1);
1604 prepared_statement_run_select
1606 plugin->count_entry_by_hash_and_vhash, 1, cbind, &return_ok, NULL,
1607 MYSQL_TYPE_BLOB, key, hashSize, &hashSize, MYSQL_TYPE_BLOB,
1608 vhash, hashSize, &hashSize, -1);
1614 prepared_statement_run_select (plugin,
1615 plugin->count_entry_by_hash,
1616 1, cbind, &return_ok,
1617 NULL, MYSQL_TYPE_BLOB,
1622 if ((ret != GNUNET_OK) || (0 >= total))
1625 NULL, NULL, 0, NULL, 0, 0, 0,
1626 GNUNET_TIME_UNIT_ZERO_ABS, 0);
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Iterating over %lld results for GET `%s'\n",
1635 gc = GNUNET_malloc (sizeof (struct GetContext));
1639 gc->have_vhash = GNUNET_YES;
1643 gc->off = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, total);
1646 nrc = GNUNET_malloc (sizeof (struct NextRequestClosure));
1647 nrc->plugin = plugin;
1649 nrc->iter_select = -1;
1651 nrc->dviter_cls = iter_cls;
1652 nrc->prep = &get_statement_prepare;
1655 mysql_plugin_next_request (nrc, GNUNET_NO);
1660 * Update the priority for a particular key in the datastore. If
1661 * the expiration time in value is different than the time found in
1662 * the datastore, the higher value should be kept. For the
1663 * anonymity level, the lower value is to be used. The specified
1664 * priority should be added to the existing priority, ignoring the
1665 * priority in value.
1667 * Note that it is possible for multiple values to match this put.
1668 * In that case, all of the respective values are updated.
1670 * @param cls our "struct Plugin*"
1671 * @param uid unique identifier of the datum
1672 * @param delta by how much should the priority
1673 * change? If priority + delta < 0 the
1674 * priority should be set to 0 (never go
1676 * @param expire new expiration time should be the
1677 * MAX of any existing expiration time and
1679 * @param msg set to error message
1680 * @return GNUNET_OK on success
1683 mysql_plugin_update (void *cls,
1686 struct GNUNET_TIME_Absolute expire,
1689 struct Plugin *plugin = cls;
1690 unsigned long long vkey = uid;
1691 unsigned long long lexpire = expire.value;
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696 "Updating value %llu adding %d to priority and maxing exp at %llu\n",
1701 ret = prepared_statement_run (plugin,
1702 plugin->update_entry,
1707 MYSQL_TYPE_LONGLONG,
1710 MYSQL_TYPE_LONGLONG,
1713 MYSQL_TYPE_LONGLONG,
1716 if (ret != GNUNET_OK)
1718 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1719 "Failed to update value %llu\n",
1727 * Select a subset of the items in the datastore and call
1728 * the given iterator for each of them.
1730 * @param cls our "struct Plugin*"
1731 * @param type entries of which type should be considered?
1732 * Use 0 for any type.
1733 * @param iter function to call on each matching value;
1734 * will be called once with a NULL value at the end
1735 * @param iter_cls closure for iter
1738 mysql_plugin_iter_zero_anonymity (void *cls,
1739 enum GNUNET_BLOCK_Type type,
1740 PluginIterator iter,
1743 struct Plugin *plugin = cls;
1744 iterateHelper (plugin, type, GNUNET_NO, 1, iter, iter_cls);
1749 * Select a subset of the items in the datastore and call
1750 * the given iterator for each of them.
1752 * @param cls our "struct Plugin*"
1753 * @param type entries of which type should be considered?
1754 * Use 0 for any type.
1755 * @param iter function to call on each matching value;
1756 * will be called once with a NULL value at the end
1757 * @param iter_cls closure for iter
1760 mysql_plugin_iter_ascending_expiration (void *cls,
1761 enum GNUNET_BLOCK_Type type,
1762 PluginIterator iter,
1765 struct Plugin *plugin = cls;
1766 iterateHelper (plugin, type, GNUNET_YES, 2, iter, iter_cls);
1771 * Select a subset of the items in the datastore and call
1772 * the given iterator for each of them.
1774 * @param cls our "struct Plugin*"
1775 * @param type entries of which type should be considered?
1776 * Use 0 for any type.
1777 * @param iter function to call on each matching value;
1778 * will be called once with a NULL value at the end
1779 * @param iter_cls closure for iter
1782 mysql_plugin_iter_migration_order (void *cls,
1783 enum GNUNET_BLOCK_Type type,
1784 PluginIterator iter,
1787 struct Plugin *plugin = cls;
1788 iterateHelper (plugin, 0, GNUNET_NO, 3, iter, iter_cls);
1793 * Select a subset of the items in the datastore and call
1794 * the given iterator for each of them.
1796 * @param cls our "struct Plugin*"
1797 * @param type entries of which type should be considered?
1798 * Use 0 for any type.
1799 * @param iter function to call on each matching value;
1800 * will be called once with a NULL value at the end
1801 * @param iter_cls closure for iter
1804 mysql_plugin_iter_all_now (void *cls,
1805 enum GNUNET_BLOCK_Type type,
1806 PluginIterator iter,
1809 struct Plugin *plugin = cls;
1810 iterateHelper (plugin, 0, GNUNET_YES, 0, iter, iter_cls);
1818 mysql_plugin_drop (void *cls)
1820 struct Plugin *plugin = cls;
1822 if ((GNUNET_OK != run_statement (plugin,
1823 "DROP TABLE gn090")) ||
1824 (GNUNET_OK != run_statement (plugin,
1825 "DROP TABLE gn072")))
1827 plugin->env->duc (plugin->env->cls, 0);
1832 * Entry point for the plugin.
1834 * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
1835 * @return our "struct Plugin*"
1838 libgnunet_plugin_datastore_mysql_init (void *cls)
1840 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1841 struct GNUNET_DATASTORE_PluginFunctions *api;
1842 struct Plugin *plugin;
1844 plugin = GNUNET_malloc (sizeof (struct Plugin));
1846 plugin->cnffile = get_my_cnf_path (env->cfg);
1847 if (GNUNET_OK != iopen (plugin))
1850 GNUNET_free_non_null (plugin->cnffile);
1851 GNUNET_free (plugin);
1854 #define MRUNS(a) (GNUNET_OK != run_statement (plugin, a) )
1855 #define PINIT(a,b) (NULL == (a = prepared_statement_create(plugin, b)))
1856 if (MRUNS ("CREATE TABLE IF NOT EXISTS gn090 ("
1857 " type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1858 " prio INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1859 " anonLevel INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1860 " expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
1861 " hash BINARY(64) NOT NULL DEFAULT '',"
1862 " vhash BINARY(64) NOT NULL DEFAULT '',"
1863 " vkey BIGINT UNSIGNED NOT NULL DEFAULT 0,"
1864 " INDEX hash (hash(64)),"
1865 " INDEX hash_vhash_vkey (hash(64),vhash(64),vkey),"
1866 " INDEX hash_vkey (hash(64),vkey),"
1867 " INDEX vkey (vkey),"
1868 " INDEX prio (prio,vkey),"
1869 " INDEX expire (expire,vkey,type),"
1870 " INDEX anonLevel (anonLevel,prio,vkey,type)"
1871 ") ENGINE=InnoDB") ||
1872 MRUNS ("CREATE TABLE IF NOT EXISTS gn072 ("
1873 " vkey BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,"
1874 " value BLOB NOT NULL DEFAULT '') ENGINE=MyISAM") ||
1875 MRUNS ("SET AUTOCOMMIT = 1") ||
1876 PINIT (plugin->select_value, SELECT_VALUE) ||
1877 PINIT (plugin->delete_value, DELETE_VALUE) ||
1878 PINIT (plugin->insert_value, INSERT_VALUE) ||
1879 PINIT (plugin->insert_entry, INSERT_ENTRY) ||
1880 PINIT (plugin->delete_entry_by_vkey, DELETE_ENTRY_BY_VKEY) ||
1881 PINIT (plugin->select_entry_by_hash, SELECT_ENTRY_BY_HASH) ||
1882 PINIT (plugin->select_entry_by_hash_and_vhash, SELECT_ENTRY_BY_HASH_AND_VHASH)
1883 || PINIT (plugin->select_entry_by_hash_and_type, SELECT_ENTRY_BY_HASH_AND_TYPE)
1884 || PINIT (plugin->select_entry_by_hash_vhash_and_type,
1885 SELECT_ENTRY_BY_HASH_VHASH_AND_TYPE)
1886 || PINIT (plugin->count_entry_by_hash, COUNT_ENTRY_BY_HASH)
1887 || PINIT (plugin->get_size, SELECT_SIZE)
1888 || PINIT (plugin->count_entry_by_hash_and_vhash, COUNT_ENTRY_BY_HASH_AND_VHASH)
1889 || PINIT (plugin->count_entry_by_hash_and_type, COUNT_ENTRY_BY_HASH_AND_TYPE)
1890 || PINIT (plugin->count_entry_by_hash_vhash_and_type,
1891 COUNT_ENTRY_BY_HASH_VHASH_AND_TYPE)
1892 || PINIT (plugin->update_entry, UPDATE_ENTRY)
1893 || PINIT (plugin->iter[0], SELECT_IT_LOW_PRIORITY)
1894 || PINIT (plugin->iter[1], SELECT_IT_NON_ANONYMOUS)
1895 || PINIT (plugin->iter[2], SELECT_IT_EXPIRATION_TIME)
1896 || PINIT (plugin->iter[3], SELECT_IT_MIGRATION_ORDER))
1899 GNUNET_free_non_null (plugin->cnffile);
1900 GNUNET_free (plugin);
1906 api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
1908 api->get_size = &mysql_plugin_get_size;
1909 api->put = &mysql_plugin_put;
1910 api->next_request = &mysql_plugin_next_request;
1911 api->get = &mysql_plugin_get;
1912 api->update = &mysql_plugin_update;
1913 api->iter_low_priority = &mysql_plugin_iter_low_priority;
1914 api->iter_zero_anonymity = &mysql_plugin_iter_zero_anonymity;
1915 api->iter_ascending_expiration = &mysql_plugin_iter_ascending_expiration;
1916 api->iter_migration_order = &mysql_plugin_iter_migration_order;
1917 api->iter_all_now = &mysql_plugin_iter_all_now;
1918 api->drop = &mysql_plugin_drop;
1919 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1920 "mysql", _("Mysql database running\n"));
1926 * Exit point from the plugin.
1927 * @param cls our "struct Plugin*"
1928 * @return always NULL
1931 libgnunet_plugin_datastore_mysql_done (void *cls)
1933 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1934 struct Plugin *plugin = api->cls;
1937 if (plugin->next_task != GNUNET_SCHEDULER_NO_TASK)
1939 GNUNET_SCHEDULER_cancel (plugin->env->sched,
1941 plugin->next_task = GNUNET_SCHEDULER_NO_TASK;
1942 plugin->next_task_nc->prep (plugin->next_task_nc->prep_cls, NULL);
1943 GNUNET_free (plugin->next_task_nc);
1944 plugin->next_task_nc = NULL;
1946 GNUNET_free_non_null (plugin->cnffile);
1947 GNUNET_free (plugin);
1949 mysql_library_end ();
1953 /* end of plugin_datastore_mysql.c */