2 This file is part of GNUnet
3 Copyright (C) 2009, 2010, 2011 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file datastore/plugin_datastore_mysql.c
23 * @brief mysql-based datastore backend
24 * @author Igor Wronsky
25 * @author Christian Grothoff
26 * @author Christophe Genevey
28 * NOTE: This db module does NOT work with mysql prior to 4.1 since
29 * it uses prepared statements. MySQL 5.0.46 promises to fix a bug
30 * in MyISAM that is causing us grief. At the time of this writing,
31 * that version is yet to be released. In anticipation, the code
32 * will use MyISAM with 5.0.46 (and higher). If you run such a
33 * version, please run "make check" to verify that the MySQL bug
34 * was actually fixed in your version (and if not, change the
35 * code below to use MyISAM for gn071).
40 * + On up-to-date hardware where mysql can be used comfortably, this
41 * module will have better performance than the other db choices
42 * (according to our tests).
43 * + Its often possible to recover the mysql database from internal
44 * inconsistencies. The other db choices do not support repair!
46 * - Memory usage (Comment: "I have 1G and it never caused me trouble")
49 * MANUAL SETUP INSTRUCTIONS
51 * 1) in gnunet.conf, set
56 * 2) Then access mysql as root,
60 * and do the following. [You should replace $USER with the username
61 * that will be running the gnunetd process].
63 CREATE DATABASE gnunet;
64 GRANT select,insert,update,delete,create,alter,drop,create temporary tables
65 ON gnunet.* TO $USER@localhost;
66 SET PASSWORD FOR $USER@localhost=PASSWORD('$the_password_you_like');
69 * 3) In the $HOME directory of $USER, create a ".my.cnf" file
70 * with the following lines
74 password=$the_password_you_like
77 * Thats it. Note that .my.cnf file is a security risk unless its on
78 * a safe partition etc. The $HOME/.my.cnf can of course be a symbolic
79 * link. Even greater security risk can be achieved by setting no
80 * password for $USER. Luckily $USER has only privileges to mess
81 * up GNUnet's tables, nothing else (unless you give them more,
84 * 4) Still, perhaps you should briefly try if the DB connection
85 * works. First, login as $USER. Then use,
88 $ mysql -u $USER -p $the_password_you_like
92 * If you get the message "Database changed" it probably works.
94 * [If you get "ERROR 2002: Can't connect to local MySQL server
95 * through socket '/tmp/mysql.sock' (2)" it may be resolvable by
96 * "ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock"
97 * so there may be some additional trouble depending on your mysql setup.]
101 * - Its probably healthy to check your tables for inconsistencies
102 * every now and then.
103 * - If you get odd SEGVs on gnunetd startup, it might be that the mysql
104 * databases have been corrupted.
105 * - The tables can be verified/fixed in two ways;
106 * 1) by running mysqlcheck -A, or
107 * 2) by executing (inside of mysql using the GNUnet database):
109 mysql> REPAIR TABLE gn090;
114 * If you have problems related to the mysql module, your best
115 * friend is probably the mysql manual. The first thing to check
116 * is that mysql is basically operational, that you can connect
117 * to it, create tables, issue queries etc.
120 #include "platform.h"
121 #include "gnunet_datastore_plugin.h"
122 #include "gnunet_util_lib.h"
123 #include "gnunet_mysql_lib.h"
124 #include "gnunet_my_lib.h"
126 #define MAX_DATUM_SIZE 65536
130 * Context for all functions in this plugin.
135 * Our execution environment.
137 struct GNUNET_DATASTORE_PluginEnvironment *env;
140 * Handle to talk to MySQL.
142 struct GNUNET_MYSQL_Context *mc;
145 * Prepared statements.
147 #define INSERT_ENTRY \
148 "INSERT INTO gn090 (repl,type,prio,anonLevel,expire,rvalue,hash,vhash,value) VALUES (?,?,?,?,?,?,?,?,?)"
149 struct GNUNET_MYSQL_StatementHandle *insert_entry;
151 #define DELETE_ENTRY_BY_UID "DELETE FROM gn090 WHERE uid=?"
152 struct GNUNET_MYSQL_StatementHandle *delete_entry_by_uid;
154 #define DELETE_ENTRY_BY_HASH_VALUE "DELETE FROM gn090 " \
155 "WHERE hash = ? AND " \
158 struct GNUNET_MYSQL_StatementHandle *delete_entry_by_hash_value;
160 #define RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, uid"
162 #define SELECT_ENTRY "SELECT " RESULT_COLUMNS " FROM gn090 " \
163 "WHERE uid >= ? AND " \
164 "(rvalue >= ? OR 0 = ?) " \
165 "ORDER BY uid LIMIT 1"
166 struct GNUNET_MYSQL_StatementHandle *select_entry;
168 #define SELECT_ENTRY_BY_HASH "SELECT " RESULT_COLUMNS " FROM gn090 " \
169 "FORCE INDEX (idx_hash_type_uid) " \
170 "WHERE hash=? AND " \
172 "(rvalue >= ? OR 0 = ?) " \
173 "ORDER BY uid LIMIT 1"
174 struct GNUNET_MYSQL_StatementHandle *select_entry_by_hash;
176 #define SELECT_ENTRY_BY_HASH_AND_TYPE "SELECT " RESULT_COLUMNS " FROM gn090 " \
177 "FORCE INDEX (idx_hash_type_uid) " \
178 "WHERE hash = ? AND " \
181 "(rvalue >= ? OR 0 = ?) " \
182 "ORDER BY uid LIMIT 1"
183 struct GNUNET_MYSQL_StatementHandle *select_entry_by_hash_and_type;
185 #define UPDATE_ENTRY "UPDATE gn090 SET " \
186 "prio = prio + ?, " \
187 "repl = repl + ?, " \
188 "expire = GREATEST(expire, ?) " \
189 "WHERE hash = ? AND vhash = ?"
190 struct GNUNET_MYSQL_StatementHandle *update_entry;
192 #define DEC_REPL "UPDATE gn090 SET repl=GREATEST (1, repl) - 1 WHERE uid=?"
193 struct GNUNET_MYSQL_StatementHandle *dec_repl;
195 #define SELECT_SIZE "SELECT SUM(LENGTH(value)+256) FROM gn090"
196 struct GNUNET_MYSQL_StatementHandle *get_size;
198 #define SELECT_IT_NON_ANONYMOUS "SELECT " RESULT_COLUMNS " FROM gn090 " \
199 "FORCE INDEX (idx_anonLevel_type_rvalue) " \
200 "WHERE anonLevel=0 AND " \
203 "ORDER BY uid LIMIT 1"
204 struct GNUNET_MYSQL_StatementHandle *zero_iter;
206 #define SELECT_IT_EXPIRATION "SELECT " RESULT_COLUMNS " FROM gn090 " \
207 "FORCE INDEX (idx_expire) " \
208 "WHERE expire < ? " \
209 "ORDER BY expire ASC LIMIT 1"
210 struct GNUNET_MYSQL_StatementHandle *select_expiration;
212 #define SELECT_IT_PRIORITY "SELECT " RESULT_COLUMNS " FROM gn090 " \
213 "FORCE INDEX (idx_prio) " \
214 "ORDER BY prio ASC LIMIT 1"
215 struct GNUNET_MYSQL_StatementHandle *select_priority;
217 #define SELECT_IT_REPLICATION "SELECT " RESULT_COLUMNS " FROM gn090 " \
218 "FORCE INDEX (idx_repl_rvalue) " \
219 "WHERE repl=? AND " \
221 " NOT EXISTS (SELECT 1 FROM gn090 FORCE INDEX (idx_repl_rvalue) WHERE repl=? AND rvalue>=?)) " \
222 "ORDER BY rvalue ASC " \
224 struct GNUNET_MYSQL_StatementHandle *select_replication;
226 #define SELECT_MAX_REPL "SELECT MAX(repl) FROM gn090"
227 struct GNUNET_MYSQL_StatementHandle *max_repl;
229 #define GET_ALL_KEYS "SELECT hash from gn090"
230 struct GNUNET_MYSQL_StatementHandle *get_all_keys;
236 * Delete an entry from the gn090 table.
238 * @param plugin plugin context
239 * @param uid unique ID of the entry to delete
240 * @return #GNUNET_OK on success, #GNUNET_NO if no such value exists, #GNUNET_SYSERR on error
243 do_delete_entry (struct Plugin *plugin,
244 unsigned long long uid)
247 uint64_t uid64 = (uint64_t) uid;
248 struct GNUNET_MY_QueryParam params_delete[] = {
249 GNUNET_MY_query_param_uint64 (&uid64),
250 GNUNET_MY_query_param_end
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Deleting value %llu from gn090 table\n",
256 ret = GNUNET_MY_exec_prepared (plugin->mc,
257 plugin->delete_entry_by_uid,
263 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
264 "Deleting value %llu from gn090 table failed\n",
265 (unsigned long long) uid);
271 * Get an estimate of how much space the database is
274 * @param cls our `struct Plugin *`
275 * @return number of bytes used on disk
278 mysql_plugin_estimate_size (void *cls,
279 unsigned long long *estimate)
281 struct Plugin *plugin = cls;
284 struct GNUNET_MY_QueryParam params_get[] = {
285 GNUNET_MY_query_param_end
287 struct GNUNET_MY_ResultSpec results_get[] = {
288 GNUNET_MY_result_spec_uint64 (&total),
289 GNUNET_MY_result_spec_end
292 ret = GNUNET_MY_exec_prepared (plugin->mc,
297 if ((GNUNET_OK == ret) &&
299 GNUNET_MY_extract_result (plugin->get_size,
302 *estimate = (unsigned long long) total;
303 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
304 "Size estimate for MySQL payload is %lld\n",
306 GNUNET_assert (UINT64_MAX != total);
307 GNUNET_break (GNUNET_NO ==
308 GNUNET_MY_extract_result (plugin->get_size,
315 * Store an item in the datastore.
318 * @param key key for the item
319 * @param absent true if the key was not found in the bloom filter
320 * @param size number of bytes in @a data
321 * @param data content stored
322 * @param type type of the content
323 * @param priority priority of the content
324 * @param anonymity anonymity-level for the content
325 * @param replication replication-level for the content
326 * @param expiration expiration time for the content
327 * @param cont continuation called with success or failure status
328 * @param cont_cls closure for @a cont
331 mysql_plugin_put (void *cls,
332 const struct GNUNET_HashCode *key,
336 enum GNUNET_BLOCK_Type type,
339 uint32_t replication,
340 struct GNUNET_TIME_Absolute expiration,
344 struct Plugin *plugin = cls;
345 uint64_t lexpiration = expiration.abs_value_us;
346 struct GNUNET_HashCode vhash;
348 GNUNET_CRYPTO_hash (data,
353 struct GNUNET_MY_QueryParam params_update[] = {
354 GNUNET_MY_query_param_uint32 (&priority),
355 GNUNET_MY_query_param_uint32 (&replication),
356 GNUNET_MY_query_param_uint64 (&lexpiration),
357 GNUNET_MY_query_param_auto_from_type (key),
358 GNUNET_MY_query_param_auto_from_type (&vhash),
359 GNUNET_MY_query_param_end
363 GNUNET_MY_exec_prepared (plugin->mc,
364 plugin->update_entry,
371 _ ("MySQL statement run failure"));
375 MYSQL_STMT *stmt = GNUNET_MYSQL_statement_get_stmt (plugin->update_entry);
376 my_ulonglong rows = mysql_stmt_affected_rows (stmt);
378 GNUNET_break (GNUNET_NO ==
379 GNUNET_MY_extract_result (plugin->update_entry,
392 uint64_t lrvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
394 struct GNUNET_MY_QueryParam params_insert[] = {
395 GNUNET_MY_query_param_uint32 (&replication),
396 GNUNET_MY_query_param_uint32 (&type),
397 GNUNET_MY_query_param_uint32 (&priority),
398 GNUNET_MY_query_param_uint32 (&anonymity),
399 GNUNET_MY_query_param_uint64 (&lexpiration),
400 GNUNET_MY_query_param_uint64 (&lrvalue),
401 GNUNET_MY_query_param_auto_from_type (key),
402 GNUNET_MY_query_param_auto_from_type (&vhash),
403 GNUNET_MY_query_param_fixed_size (data, size),
404 GNUNET_MY_query_param_end
407 if (size > MAX_DATUM_SIZE)
410 cont (cont_cls, key, size, GNUNET_SYSERR, _ ("Data too large"));
415 GNUNET_MY_exec_prepared (plugin->mc,
416 plugin->insert_entry,
423 _ ("MySQL statement run failure"));
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Inserted value `%s' with size %u into gn090 table\n",
429 (unsigned int) size);
431 plugin->env->duc (plugin->env->cls,
433 GNUNET_break (GNUNET_NO ==
434 GNUNET_MY_extract_result (plugin->insert_entry,
445 * Run the given select statement and call 'proc' on the resulting
446 * values (which must be in particular positions).
448 * @param plugin the plugin handle
449 * @param stmt select statement to run
450 * @param proc function to call on result
451 * @param proc_cls closure for @a proc
452 * @param params_select arguments to initialize stmt
455 execute_select (struct Plugin *plugin,
456 struct GNUNET_MYSQL_StatementHandle *stmt,
457 PluginDatumProcessor proc,
459 struct GNUNET_MY_QueryParam *params_select)
462 uint32_t replication;
469 struct GNUNET_HashCode key;
470 struct GNUNET_TIME_Absolute expiration;
471 struct GNUNET_MY_ResultSpec results_select[] = {
472 GNUNET_MY_result_spec_uint32 (&replication),
473 GNUNET_MY_result_spec_uint32 (&type),
474 GNUNET_MY_result_spec_uint32 (&priority),
475 GNUNET_MY_result_spec_uint32 (&anonymity),
476 GNUNET_MY_result_spec_absolute_time (&expiration),
477 GNUNET_MY_result_spec_auto_from_type (&key),
478 GNUNET_MY_result_spec_variable_size (&value, &value_size),
479 GNUNET_MY_result_spec_uint64 (&uid),
480 GNUNET_MY_result_spec_end
483 ret = GNUNET_MY_exec_prepared (plugin->mc,
486 if (GNUNET_OK != ret)
489 NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
493 ret = GNUNET_MY_extract_result (stmt,
495 if (GNUNET_OK != ret)
498 NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
503 "Found %u-byte value under key `%s' with prio %u, anon %u, expire %s selecting from gn090 table\n",
504 (unsigned int) value_size,
506 (unsigned int) priority,
507 (unsigned int) anonymity,
508 GNUNET_STRINGS_absolute_time_to_string (expiration));
509 GNUNET_assert (value_size < MAX_DATUM_SIZE);
510 GNUNET_break (GNUNET_NO ==
511 GNUNET_MY_extract_result (stmt,
513 ret = proc (proc_cls,
523 GNUNET_MY_cleanup_result (results_select);
524 if (GNUNET_NO == ret)
526 do_delete_entry (plugin, uid);
528 plugin->env->duc (plugin->env->cls,
535 * Get one of the results for a particular key in the datastore.
538 * @param next_uid return the result with lowest uid >= next_uid
539 * @param random if true, return a random result instead of using next_uid
540 * @param key key to match, never NULL
541 * @param type entries of which type are relevant?
542 * Use 0 for any type.
543 * @param proc function to call on the matching value,
544 * with NULL for if no value matches
545 * @param proc_cls closure for @a proc
548 mysql_plugin_get_key (void *cls,
551 const struct GNUNET_HashCode *key,
552 enum GNUNET_BLOCK_Type type,
553 PluginDatumProcessor proc,
556 struct Plugin *plugin = cls;
561 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
570 struct GNUNET_MY_QueryParam params_select[] = {
571 GNUNET_MY_query_param_uint64 (&next_uid),
572 GNUNET_MY_query_param_uint64 (&rvalue),
573 GNUNET_MY_query_param_uint64 (&rvalue),
574 GNUNET_MY_query_param_end
577 execute_select (plugin,
578 plugin->select_entry,
583 else if (type != GNUNET_BLOCK_TYPE_ANY)
585 struct GNUNET_MY_QueryParam params_select[] = {
586 GNUNET_MY_query_param_auto_from_type (key),
587 GNUNET_MY_query_param_uint32 (&type),
588 GNUNET_MY_query_param_uint64 (&next_uid),
589 GNUNET_MY_query_param_uint64 (&rvalue),
590 GNUNET_MY_query_param_uint64 (&rvalue),
591 GNUNET_MY_query_param_end
594 execute_select (plugin,
595 plugin->select_entry_by_hash_and_type,
602 struct GNUNET_MY_QueryParam params_select[] = {
603 GNUNET_MY_query_param_auto_from_type (key),
604 GNUNET_MY_query_param_uint64 (&next_uid),
605 GNUNET_MY_query_param_uint64 (&rvalue),
606 GNUNET_MY_query_param_uint64 (&rvalue),
607 GNUNET_MY_query_param_end
610 execute_select (plugin,
611 plugin->select_entry_by_hash,
620 * Get a zero-anonymity datum from the datastore.
622 * @param cls our `struct Plugin *`
623 * @param next_uid return the result with lowest uid >= next_uid
624 * @param type entries of which type should be considered?
625 * Must not be zero (ANY).
626 * @param proc function to call on a matching value;
627 * will be called with NULL if no value matches
628 * @param proc_cls closure for @a proc
631 mysql_plugin_get_zero_anonymity (void *cls,
633 enum GNUNET_BLOCK_Type type,
634 PluginDatumProcessor proc,
637 struct Plugin *plugin = cls;
638 uint32_t typei = (uint32_t) type;
640 struct GNUNET_MY_QueryParam params_zero_iter[] = {
641 GNUNET_MY_query_param_uint32 (&typei),
642 GNUNET_MY_query_param_uint64 (&next_uid),
643 GNUNET_MY_query_param_end
646 execute_select (plugin,
655 * Context for #repl_proc() function.
662 struct Plugin *plugin;
665 * Function to call for the result (or the NULL).
667 PluginDatumProcessor proc;
670 * Closure for @e proc.
677 * Wrapper for the processor for #mysql_plugin_get_replication().
678 * Decrements the replication counter and calls the original
682 * @param key key for the content
683 * @param size number of bytes in @a data
684 * @param data content stored
685 * @param type type of the content
686 * @param priority priority of the content
687 * @param anonymity anonymity-level for the content
688 * @param replication replication-level for the content
689 * @param expiration expiration time for the content
690 * @param uid unique identifier for the datum;
691 * maybe 0 if no unique identifier is available
692 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue
693 * (continue on call to "next", of course),
694 * #GNUNET_NO to delete the item and continue (if supported)
697 repl_proc (void *cls,
698 const struct GNUNET_HashCode *key,
701 enum GNUNET_BLOCK_Type type,
704 uint32_t replication,
705 struct GNUNET_TIME_Absolute expiration,
708 struct ReplCtx *rc = cls;
709 struct Plugin *plugin = rc->plugin;
713 ret = rc->proc (rc->proc_cls,
725 struct GNUNET_MY_QueryParam params_proc[] = {
726 GNUNET_MY_query_param_uint64 (&uid),
727 GNUNET_MY_query_param_end
730 iret = GNUNET_MY_exec_prepared (plugin->mc,
733 if (GNUNET_SYSERR == iret)
735 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
736 "Failed to reduce replication counter\n");
737 return GNUNET_SYSERR;
745 * Get a random item for replication. Returns a single, not expired,
746 * random item from those with the highest replication counters. The
747 * item's replication counter is decremented by one IF it was positive
748 * before. Call @a proc with all values ZERO or NULL if the datastore
752 * @param proc function to call the value (once only).
753 * @param proc_cls closure for @a proc
756 mysql_plugin_get_replication (void *cls,
757 PluginDatumProcessor proc,
760 struct Plugin *plugin = cls;
764 struct GNUNET_MY_QueryParam params_get[] = {
765 GNUNET_MY_query_param_end
767 struct GNUNET_MY_ResultSpec results_get[] = {
768 GNUNET_MY_result_spec_uint32 (&repl),
769 GNUNET_MY_result_spec_end
771 struct GNUNET_MY_QueryParam params_select[] = {
772 GNUNET_MY_query_param_uint32 (&repl),
773 GNUNET_MY_query_param_uint64 (&rvalue),
774 GNUNET_MY_query_param_uint32 (&repl),
775 GNUNET_MY_query_param_uint64 (&rvalue),
776 GNUNET_MY_query_param_end
781 rc.proc_cls = proc_cls;
784 GNUNET_MY_exec_prepared (plugin->mc,
788 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
793 GNUNET_MY_extract_result (plugin->max_repl,
796 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
799 GNUNET_break (GNUNET_NO ==
800 GNUNET_MY_extract_result (plugin->max_repl,
802 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
805 execute_select (plugin,
806 plugin->select_replication,
814 * Get all of the keys in the datastore.
817 * @param proc function to call on each key
818 * @param proc_cls closure for @a proc
821 mysql_plugin_get_keys (void *cls,
822 PluginKeyProcessor proc,
825 struct Plugin *plugin = cls;
827 MYSQL_STMT *statement;
829 struct GNUNET_HashCode key;
830 struct GNUNET_HashCode last;
831 struct GNUNET_MY_QueryParam params_select[] = {
832 GNUNET_MY_query_param_end
834 struct GNUNET_MY_ResultSpec results_select[] = {
835 GNUNET_MY_result_spec_auto_from_type (&key),
836 GNUNET_MY_result_spec_end
839 GNUNET_assert (NULL != proc);
840 statement = GNUNET_MYSQL_statement_get_stmt (plugin->get_all_keys);
842 GNUNET_MY_exec_prepared (plugin->mc,
843 plugin->get_all_keys,
846 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
847 _ ("`%s' for `%s' failed at %s:%d with error: %s\n"),
848 "mysql_stmt_execute",
852 mysql_stmt_error (statement));
853 GNUNET_MYSQL_statements_invalidate (plugin->mc);
854 proc (proc_cls, NULL, 0);
857 memset (&last, 0, sizeof(last)); /* make static analysis happy */
860 while (ret == GNUNET_YES)
862 ret = GNUNET_MY_extract_result (plugin->get_all_keys,
864 if (0 != GNUNET_memcmp (&last,
883 /* finally, let app know we are done */
887 if (GNUNET_SYSERR == ret)
889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
890 _ ("`%s' failed at %s:%d with error: %s\n"),
894 mysql_stmt_error (statement));
895 GNUNET_MYSQL_statements_invalidate (plugin->mc);
902 * Context for #expi_proc() function.
909 struct Plugin *plugin;
912 * Function to call for the result (or the NULL).
914 PluginDatumProcessor proc;
917 * Closure for @e proc.
924 * Wrapper for the processor for #mysql_plugin_get_expiration().
925 * If no expired value was found, we do a second query for
926 * low-priority content.
929 * @param key key for the content
930 * @param size number of bytes in data
931 * @param data content stored
932 * @param type type of the content
933 * @param priority priority of the content
934 * @param anonymity anonymity-level for the content
935 * @param replication replication-level for the content
936 * @param expiration expiration time for the content
937 * @param uid unique identifier for the datum;
938 * maybe 0 if no unique identifier is available
939 * @return #GNUNET_SYSERR to abort the iteration, #GNUNET_OK to continue
940 * (continue on call to "next", of course),
941 * #GNUNET_NO to delete the item and continue (if supported)
944 expi_proc (void *cls,
945 const struct GNUNET_HashCode *key,
948 enum GNUNET_BLOCK_Type type,
951 uint32_t replication,
952 struct GNUNET_TIME_Absolute expiration,
955 struct ExpiCtx *rc = cls;
956 struct Plugin *plugin = rc->plugin;
957 struct GNUNET_MY_QueryParam params_select[] = {
958 GNUNET_MY_query_param_end
963 execute_select (plugin,
964 plugin->select_priority,
968 return GNUNET_SYSERR;
970 return rc->proc (rc->proc_cls,
984 * Get a random item for expiration.
985 * Call @a proc with all values ZERO or NULL if the datastore is empty.
988 * @param proc function to call the value (once only).
989 * @param proc_cls closure for @a proc
992 mysql_plugin_get_expiration (void *cls,
993 PluginDatumProcessor proc,
996 struct Plugin *plugin = cls;
997 struct GNUNET_TIME_Absolute now;
998 struct GNUNET_MY_QueryParam params_select[] = {
999 GNUNET_MY_query_param_absolute_time (&now),
1000 GNUNET_MY_query_param_end
1006 rc.proc_cls = proc_cls;
1007 now = GNUNET_TIME_absolute_get ();
1008 execute_select (plugin,
1009 plugin->select_expiration,
1019 * @param cls the `struct Plugin *`
1022 mysql_plugin_drop (void *cls)
1024 struct Plugin *plugin = cls;
1027 GNUNET_MYSQL_statement_run (plugin->mc,
1028 "DROP TABLE gn090"))
1030 plugin->env->duc (plugin->env->cls, 0);
1035 * Remove a particular key in the datastore.
1037 * @param cls closure
1038 * @param key key for the content
1039 * @param size number of bytes in data
1040 * @param data content stored
1041 * @param cont continuation called with success or failure status
1042 * @param cont_cls continuation closure for @a cont
1045 mysql_plugin_remove_key (void *cls,
1046 const struct GNUNET_HashCode *key,
1049 PluginRemoveCont cont,
1052 struct Plugin *plugin = cls;
1053 struct GNUNET_MY_QueryParam params_delete[] = {
1054 GNUNET_MY_query_param_auto_from_type (key),
1055 GNUNET_MY_query_param_fixed_size (data, size),
1056 GNUNET_MY_query_param_end
1060 GNUNET_MY_exec_prepared (plugin->mc,
1061 plugin->delete_entry_by_hash_value,
1064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1065 "Removing key `%s' from gn090 table failed\n",
1071 _ ("MySQL statement run failure"));
1075 MYSQL_STMT *stmt = GNUNET_MYSQL_statement_get_stmt (
1076 plugin->delete_entry_by_hash_value);
1077 my_ulonglong rows = mysql_stmt_affected_rows (stmt);
1088 plugin->env->duc (plugin->env->cls,
1099 * Entry point for the plugin.
1101 * @param cls the `struct GNUNET_DATASTORE_PluginEnvironment *`
1102 * @return our `struct Plugin *`
1105 libgnunet_plugin_datastore_mysql_init (void *cls)
1107 struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
1108 struct GNUNET_DATASTORE_PluginFunctions *api;
1109 struct Plugin *plugin;
1111 plugin = GNUNET_new (struct Plugin);
1113 plugin->mc = GNUNET_MYSQL_context_create (env->cfg,
1115 if (NULL == plugin->mc)
1117 GNUNET_free (plugin);
1120 #define MRUNS(a) (GNUNET_OK != GNUNET_MYSQL_statement_run (plugin->mc, a))
1121 #define PINIT(a, b) (NULL == (a = GNUNET_MYSQL_statement_prepare (plugin->mc, \
1124 ("CREATE TABLE IF NOT EXISTS gn090 ("
1125 " repl INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1126 " type INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1127 " prio INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1128 " anonLevel INT(11) UNSIGNED NOT NULL DEFAULT 0,"
1129 " expire BIGINT UNSIGNED NOT NULL DEFAULT 0,"
1130 " rvalue BIGINT UNSIGNED NOT NULL,"
1131 " hash BINARY(64) NOT NULL DEFAULT '',"
1132 " vhash BINARY(64) NOT NULL DEFAULT '',"
1133 " value BLOB NOT NULL DEFAULT '',"
1134 " uid BIGINT NOT NULL AUTO_INCREMENT,"
1135 " PRIMARY KEY (uid),"
1136 " INDEX idx_hash_type_uid (hash(64),type,rvalue),"
1137 " INDEX idx_prio (prio),"
1138 " INDEX idx_repl_rvalue (repl,rvalue),"
1139 " INDEX idx_expire (expire),"
1140 " INDEX idx_anonLevel_type_rvalue (anonLevel,type,rvalue)"
1141 ") ENGINE=InnoDB") || MRUNS ("SET AUTOCOMMIT = 1") ||
1142 PINIT (plugin->insert_entry, INSERT_ENTRY) ||
1143 PINIT (plugin->delete_entry_by_uid, DELETE_ENTRY_BY_UID) ||
1144 PINIT (plugin->delete_entry_by_hash_value, DELETE_ENTRY_BY_HASH_VALUE) ||
1145 PINIT (plugin->select_entry, SELECT_ENTRY) ||
1146 PINIT (plugin->select_entry_by_hash, SELECT_ENTRY_BY_HASH) ||
1147 PINIT (plugin->select_entry_by_hash_and_type,
1148 SELECT_ENTRY_BY_HASH_AND_TYPE) ||
1149 PINIT (plugin->get_size, SELECT_SIZE) ||
1150 PINIT (plugin->update_entry, UPDATE_ENTRY) ||
1151 PINIT (plugin->dec_repl, DEC_REPL) ||
1152 PINIT (plugin->zero_iter, SELECT_IT_NON_ANONYMOUS) ||
1153 PINIT (plugin->select_expiration, SELECT_IT_EXPIRATION) ||
1154 PINIT (plugin->select_priority, SELECT_IT_PRIORITY) ||
1155 PINIT (plugin->max_repl, SELECT_MAX_REPL) ||
1156 PINIT (plugin->get_all_keys, GET_ALL_KEYS) ||
1157 PINIT (plugin->select_replication, SELECT_IT_REPLICATION) ||
1160 GNUNET_MYSQL_context_destroy (plugin->mc);
1161 GNUNET_free (plugin);
1167 api = GNUNET_new (struct GNUNET_DATASTORE_PluginFunctions);
1169 api->estimate_size = &mysql_plugin_estimate_size;
1170 api->put = &mysql_plugin_put;
1171 api->get_key = &mysql_plugin_get_key;
1172 api->get_replication = &mysql_plugin_get_replication;
1173 api->get_expiration = &mysql_plugin_get_expiration;
1174 api->get_zero_anonymity = &mysql_plugin_get_zero_anonymity;
1175 api->get_keys = &mysql_plugin_get_keys;
1176 api->drop = &mysql_plugin_drop;
1177 api->remove_key = &mysql_plugin_remove_key;
1178 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "mysql",
1179 _ ("Mysql database running\n"));
1185 * Exit point from the plugin.
1187 * @param cls our `struct Plugin *`
1188 * @return always NULL
1191 libgnunet_plugin_datastore_mysql_done (void *cls)
1193 struct GNUNET_DATASTORE_PluginFunctions *api = cls;
1194 struct Plugin *plugin = api->cls;
1196 GNUNET_MYSQL_context_destroy (plugin->mc);
1197 GNUNET_free (plugin);
1203 /* end of plugin_datastore_mysql.c */