GNUnet 0.25.2-11-g84e94e98c
 
Loading...
Searching...
No Matches
plugin_datacache_sqlite.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2006, 2009, 2015, 2022 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
26#include "platform.h"
27#include "gnunet_util_lib.h"
29#include "gnunet_sq_lib.h"
30#include <sqlite3.h>
31
32#define LOG(kind, ...) GNUNET_log_from (kind, "datacache-sqlite", __VA_ARGS__)
33
34#define LOG_STRERROR_FILE(kind, op, fn) \
35 GNUNET_log_from_strerror_file (kind, "datacache-sqlite", op, fn)
36
37
42#define OVERHEAD (sizeof(struct GNUNET_HashCode) + 36)
43
47struct Plugin
48{
53
57 sqlite3 *dbh;
58
62 char *fn;
63
67 sqlite3_stmt *insert_stmt;
68
72 sqlite3_stmt *get_count_stmt;
73
77 sqlite3_stmt *get_count_any_stmt;
78
82 sqlite3_stmt *get_stmt;
83
87 sqlite3_stmt *get_any_stmt;
88
92 sqlite3_stmt *del_select_stmt;
93
97 sqlite3_stmt *del_expired_stmt;
98
102 sqlite3_stmt *del_stmt;
103
107 sqlite3_stmt *get_closest_stmt;
108
112 unsigned int num_items;
113};
114
115
124#define LOG_SQLITE(db, level, cmd) \
125 do \
126 { \
127 LOG (level, \
128 _ ("`%s' failed at %s:%d with error: %s\n"), \
129 cmd, \
130 __FILE__, \
131 __LINE__, \
132 sqlite3_errmsg (db)); \
133 } while (0)
134
135
142#define SQLITE3_EXEC(db, cmd) \
143 do \
144 { \
145 emsg = NULL; \
146 if (SQLITE_OK != \
147 sqlite3_exec (db, cmd, NULL, NULL, &emsg)) \
148 { \
149 LOG (GNUNET_ERROR_TYPE_ERROR, \
150 _ ("`%s' failed at %s:%d with error: %s\n"), \
151 "sqlite3_exec", \
152 __FILE__, \
153 __LINE__, \
154 emsg); \
155 sqlite3_free (emsg); \
156 } \
157 } while (0)
158
159
168static int
169sq_prepare (sqlite3 *dbh,
170 const char *zSql, /* SQL statement, UTF-8 encoded */
171 sqlite3_stmt **ppStmt)
172{ /* OUT: Statement handle */
173 char *dummy;
174
175 return sqlite3_prepare (dbh,
176 zSql,
177 strlen (zSql),
178 ppStmt,
179 (const char **) &dummy);
180}
181
182
191static ssize_t
193 uint32_t xor_distance,
194 const struct GNUNET_DATACACHE_Block *block)
195{
196 struct Plugin *plugin = cls;
197 uint32_t type32 = (uint32_t) block->type;
198 uint32_t ro32 = (uint32_t) block->ro;
199 struct GNUNET_SQ_QueryParam params[] = {
204 GNUNET_SQ_query_param_uint32 (&xor_distance),
206 block->data_size),
208 block->put_path_length
209 * sizeof(struct GNUNET_DHT_PathElement)),
212 };
213
215 "Processing PUT of %u bytes with key `%s' and expiration %s\n",
216 (unsigned int) block->data_size,
217 GNUNET_h2s (&block->key),
220 block->expiration_time),
221 GNUNET_YES));
222 if (GNUNET_OK !=
223 GNUNET_SQ_bind (plugin->insert_stmt,
224 params))
225 {
226 LOG_SQLITE (plugin->dbh,
228 "sqlite3_bind_xxx");
230 plugin->insert_stmt);
231 return -1;
232 }
233 if (SQLITE_DONE !=
234 sqlite3_step (plugin->insert_stmt))
235 {
236 LOG_SQLITE (plugin->dbh,
238 "sqlite3_step");
240 plugin->insert_stmt);
241 return -1;
242 }
243 plugin->num_items++;
245 plugin->insert_stmt);
246 return block->data_size + OVERHEAD;
247}
248
249
260static unsigned int
261get_any (void *cls,
262 const struct GNUNET_HashCode *key,
264 void *iter_cls)
265{
266 struct Plugin *plugin = cls;
267 struct GNUNET_TIME_Absolute now;
268 unsigned int cnt;
269 uint32_t off;
270 uint32_t btype32;
271 uint32_t bro32;
272 unsigned int total;
273 struct GNUNET_DATACACHE_Block block;
274 void *path;
275 void *data;
276 size_t path_size;
277
279 {
280 struct GNUNET_SQ_QueryParam params_count[] = {
284 };
286 "Processing GET for key `%s'\n",
287 GNUNET_h2s (key));
288
289 if (GNUNET_OK !=
290 GNUNET_SQ_bind (plugin->get_count_any_stmt,
291 params_count))
292 {
293 LOG_SQLITE (plugin->dbh,
295 "sqlite3_bind_xxx");
297 plugin->get_count_any_stmt);
298 return 0;
299 }
300 if (SQLITE_ROW !=
301 sqlite3_step (plugin->get_count_any_stmt))
302 {
303 LOG_SQLITE (plugin->dbh,
305 "sqlite_step");
307 plugin->get_count_any_stmt);
309 "No content found when processing GET for key `%s'\n",
310 GNUNET_h2s (key));
311 return 0;
312 }
313 total = sqlite3_column_int (plugin->get_count_any_stmt,
314 0);
316 plugin->get_count_any_stmt);
317 if ( (0 == total) ||
318 (NULL == iter) )
319 {
320 if (0 == total)
322 "No content found when processing GET for key `%s'\n",
323 GNUNET_h2s (key));
324 return total;
325 }
326 }
327 cnt = 0;
328 block.key = *key;
330 total);
331 {
332 struct GNUNET_SQ_QueryParam params_select[] = {
337 };
338 struct GNUNET_SQ_ResultSpec rs[] = {
340 &block.data_size),
343 &path_size),
348 };
349
350 while (cnt < total)
351 {
352 off = (off + 1) % total;
353 if (GNUNET_OK !=
354 GNUNET_SQ_bind (plugin->get_any_stmt,
355 params_select))
356 {
357 LOG_SQLITE (plugin->dbh,
359 "sqlite3_bind_xxx");
361 plugin->get_any_stmt);
362 return cnt;
363 }
364 if (SQLITE_ROW !=
365 sqlite3_step (plugin->get_any_stmt))
366 break;
367 if (GNUNET_OK !=
368 GNUNET_SQ_extract_result (plugin->get_any_stmt,
369 rs))
370 {
371 GNUNET_break (0);
373 plugin->get_any_stmt);
374 break;
375 }
376 if (0 != path_size % sizeof(struct GNUNET_DHT_PathElement))
377 {
378 GNUNET_break (0);
379 path_size = 0;
380 path = NULL;
381 }
382 block.data = data;
383 block.put_path = path;
384 block.put_path_length = path_size / sizeof(struct GNUNET_DHT_PathElement);
385 block.type = (enum GNUNET_BLOCK_Type) btype32;
386 block.ro = (enum GNUNET_DHT_RouteOption) bro32;
387 cnt++;
389 "Found %u-byte result when processing GET for key `%s'\n",
390 (unsigned int) block.data_size,
391 GNUNET_h2s (&block.key));
392 if (GNUNET_OK !=
393 iter (iter_cls,
394 &block))
395 {
398 plugin->get_any_stmt);
399 break;
400 }
403 plugin->get_any_stmt);
404 }
406 plugin->get_any_stmt);
407 }
408
409 return cnt;
410}
411
412
424static unsigned int
425get_typed (void *cls,
426 const struct GNUNET_HashCode *key,
429 void *iter_cls)
430{
431 struct Plugin *plugin = cls;
432 uint32_t type32 = type;
433 struct GNUNET_TIME_Absolute now;
434 unsigned int cnt;
435 uint32_t off;
436 uint32_t bro32;
437 unsigned int total;
438 struct GNUNET_DATACACHE_Block block;
439 void *path;
440 void *data;
441 size_t path_size;
442 struct GNUNET_SQ_ResultSpec rs[] = {
444 &block.data_size),
447 &path_size),
451 };
452
455 "Processing GET for key `%s'\n",
456 GNUNET_h2s (key));
457 {
458 struct GNUNET_SQ_QueryParam params_count[] = {
463 };
464
465 if (GNUNET_OK !=
466 GNUNET_SQ_bind (plugin->get_count_stmt,
467 params_count))
468 {
469 LOG_SQLITE (plugin->dbh,
471 "sqlite3_bind_xxx");
473 plugin->get_count_stmt);
474 return 0;
475 }
476 }
477 if (SQLITE_ROW !=
478 sqlite3_step (plugin->get_count_stmt))
479 {
480 LOG_SQLITE (plugin->dbh,
482 "sqlite_step");
484 plugin->get_count_stmt);
486 "No content found when processing GET for key `%s'\n",
487 GNUNET_h2s (key));
488 return 0;
489 }
490 total = sqlite3_column_int (plugin->get_count_stmt,
491 0);
493 plugin->get_count_stmt);
494 if ( (0 == total) ||
495 (NULL == iter) )
496 {
497 if (0 == total)
499 "No content found when processing GET for key `%s'\n",
500 GNUNET_h2s (key));
501 return total;
502 }
503
504 cnt = 0;
505 block.key = *key;
507 total);
508 {
509 struct GNUNET_SQ_QueryParam params_select[] = {
515 };
516 while (cnt < total)
517 {
518 off = (off + 1) % total;
519 if (GNUNET_OK !=
520 GNUNET_SQ_bind (plugin->get_stmt,
521 params_select))
522 {
523 LOG_SQLITE (plugin->dbh,
525 "sqlite3_bind_xxx");
527 plugin->get_stmt);
528 return cnt;
529 }
530 if (SQLITE_ROW !=
531 sqlite3_step (plugin->get_stmt))
532 break;
533 if (GNUNET_OK !=
535 rs))
536 {
537 GNUNET_break (0);
539 plugin->get_stmt);
540 break;
541 }
542 if (0 != path_size % sizeof(struct GNUNET_DHT_PathElement))
543 {
544 GNUNET_break (0);
545 path_size = 0;
546 path = NULL;
547 }
548 block.data = data;
549 block.put_path = path;
550 block.put_path_length = path_size / sizeof(struct GNUNET_DHT_PathElement);
551 block.type = type;
552 block.ro = (enum GNUNET_DHT_RouteOption) bro32;
553 cnt++;
555 "Found %u-byte result when processing GET for key `%s'\n",
556 (unsigned int) block.data_size,
557 GNUNET_h2s (&block.key));
558 if ( (NULL != iter) &&
559 (GNUNET_OK !=
560 iter (iter_cls,
561 &block)) )
562 {
565 plugin->get_stmt);
566 break;
567 }
570 plugin->get_stmt);
571 }
573 plugin->get_stmt);
574 }
575 return cnt;
576}
577
578
590static unsigned int
592 const struct GNUNET_HashCode *key,
595 void *iter_cls)
596{
598 return get_any (cls,
599 key,
600 iter,
601 iter_cls);
602 return get_typed (cls,
603 key,
604 type,
605 iter,
606 iter_cls);
607}
608
609
619{
620 struct Plugin *plugin = cls;
621 uint64_t rowid;
622 void *data;
623 size_t data_size;
624 struct GNUNET_HashCode hc;
625 struct GNUNET_TIME_Absolute now;
626 struct GNUNET_SQ_ResultSpec rs[] = {
630 &data_size),
632 };
633 struct GNUNET_SQ_QueryParam params[] = {
636 };
638 "Processing DEL\n");
640 {
641 struct GNUNET_SQ_QueryParam time_params[] = {
644 };
645
646 if (GNUNET_OK !=
647 GNUNET_SQ_bind (plugin->del_expired_stmt,
648 time_params))
649 {
650 LOG_SQLITE (plugin->dbh,
652 "sqlite3_bind");
654 plugin->del_expired_stmt);
655 return GNUNET_SYSERR;
656 }
657 }
658 if ( (SQLITE_ROW !=
659 sqlite3_step (plugin->del_expired_stmt)) ||
660 (GNUNET_OK !=
661 GNUNET_SQ_extract_result (plugin->del_expired_stmt,
662 rs)))
663 {
665 plugin->del_expired_stmt);
666 if (SQLITE_ROW !=
667 sqlite3_step (plugin->del_select_stmt))
668 {
669 LOG_SQLITE (plugin->dbh,
671 "sqlite3_step");
673 plugin->del_select_stmt);
674 return GNUNET_SYSERR;
675 }
676 if (GNUNET_OK !=
677 GNUNET_SQ_extract_result (plugin->del_select_stmt,
678 rs))
679 {
681 plugin->del_select_stmt);
682 GNUNET_break (0);
683 return GNUNET_SYSERR;
684 }
685 }
688 plugin->del_select_stmt);
689 if (GNUNET_OK !=
690 GNUNET_SQ_bind (plugin->del_stmt,
691 params))
692 {
693 LOG_SQLITE (plugin->dbh,
695 "sqlite3_bind");
697 plugin->del_stmt);
698 return GNUNET_SYSERR;
699 }
700 if (SQLITE_DONE !=
701 sqlite3_step (plugin->del_stmt))
702 {
703 LOG_SQLITE (plugin->dbh,
705 "sqlite3_step");
707 plugin->del_stmt);
708 return GNUNET_SYSERR;
709 }
710 plugin->num_items--;
711 plugin->env->delete_notify (plugin->env->cls,
712 &hc,
715 plugin->del_stmt);
716 return GNUNET_OK;
717}
718
719
734static unsigned int
736 const struct GNUNET_HashCode *key,
738 unsigned int num_results,
740 void *iter_cls)
741{
742 struct Plugin *plugin = cls;
743 uint32_t type32 = type;
744 uint32_t num_results32 = num_results;
745 struct GNUNET_TIME_Absolute now;
746 void *data;
747 void *path;
748 size_t path_size;
749 unsigned int cnt;
750 uint32_t bro32;
751 struct GNUNET_DATACACHE_Block block;
752 uint32_t rtype32;
753 struct GNUNET_SQ_ResultSpec rs[] = {
755 &block.data_size),
758 &path_size),
764 };
765
768 "Processing GET_CLOSEST for key `%s'\n",
769 GNUNET_h2s (key));
770 {
771 struct GNUNET_SQ_QueryParam params[] = {
775 GNUNET_SQ_query_param_uint32 (&num_results32),
777 };
778 if (GNUNET_OK !=
779 GNUNET_SQ_bind (plugin->get_closest_stmt,
780 params))
781 {
782 LOG_SQLITE (plugin->dbh,
784 "sqlite3_bind_xxx");
786 plugin->get_closest_stmt);
787 return 0;
788 }
789 }
790 cnt = 0;
791 while (SQLITE_ROW ==
792 sqlite3_step (plugin->get_closest_stmt))
793 {
794 if (GNUNET_OK !=
795 GNUNET_SQ_extract_result (plugin->get_closest_stmt,
796 rs))
797 {
798 GNUNET_break (0);
799 break;
800 }
801 if (0 != path_size % sizeof(struct GNUNET_DHT_PathElement))
802 {
803 GNUNET_break (0);
804 path_size = 0;
805 path = NULL;
806 }
807 block.put_path_length
808 = path_size / sizeof(struct GNUNET_DHT_PathElement);
809 block.put_path = path;
810 block.data = data;
811 block.type = (enum GNUNET_BLOCK_Type) rtype32;
812 block.ro = (enum GNUNET_DHT_RouteOption) bro32;
813 cnt++;
815 "Found %u-byte result at %s when processing GET_CLOSE\n",
816 (unsigned int) block.data_size,
817 GNUNET_h2s (&block.key));
818
819 if (GNUNET_OK !=
820 iter (iter_cls,
821 &block))
822 {
824 break;
825 }
827 }
829 plugin->get_closest_stmt);
830 return cnt;
831}
832
833
834void *
836
843void *
845{
848 struct Plugin *plugin;
849 char *fn;
850 char *fn_utf8;
851 sqlite3 *dbh;
852 char *emsg;
853
854 if (GNUNET_YES ==
856 "datacache-sqlite",
857 "IN_MEMORY"))
858 {
859 if (SQLITE_OK !=
860 sqlite3_open (":memory:",
861 &dbh))
862 return NULL;
863 fn_utf8 = NULL;
864 }
865 else
866 {
867 fn = GNUNET_DISK_mktemp ("gnunet-datacache");
868 if (NULL == fn)
869 {
870 GNUNET_break (0);
871 return NULL;
872 }
873 /* fn should be UTF-8-encoded. If it isn't, it's a bug. */
874 fn_utf8 = GNUNET_strdup (fn);
875 if (SQLITE_OK !=
876 sqlite3_open (fn_utf8,
877 &dbh))
878 {
879 GNUNET_free (fn);
880 GNUNET_free (fn_utf8);
881 return NULL;
882 }
883 GNUNET_free (fn);
884 }
885
886 SQLITE3_EXEC (dbh, "PRAGMA temp_store=MEMORY");
887 SQLITE3_EXEC (dbh, "PRAGMA locking_mode=EXCLUSIVE");
888 SQLITE3_EXEC (dbh, "PRAGMA journal_mode=OFF");
889 SQLITE3_EXEC (dbh, "PRAGMA synchronous=OFF");
890 SQLITE3_EXEC (dbh, "PRAGMA page_size=4092");
891 if (GNUNET_YES ==
893 "datacache-sqlite",
894 "IN_MEMORY"))
895 SQLITE3_EXEC (dbh, "PRAGMA sqlite_temp_store=3");
896
898 "CREATE TABLE ds180 ("
899 " type INTEGER NOT NULL DEFAULT 0,"
900 " ro INTEGER NOT NULL DEFAULT 0,"
901 " expire INTEGER NOT NULL,"
902 " key BLOB NOT NULL DEFAULT '',"
903 " prox INTEGER NOT NULL,"
904 " value BLOB NOT NULL,"
905 " trunc BLOB NOT NULL,"
906 " path BLOB DEFAULT '')");
908 "CREATE INDEX idx_hashidx"
909 " ON ds180 (key,type,expire)");
911 "CREATE INDEX idx_prox_expire"
912 " ON ds180 (prox,expire)");
914 "CREATE INDEX idx_expire_only"
915 " ON ds180 (expire)");
916 plugin = GNUNET_new (struct Plugin);
917 plugin->env = env;
918 plugin->dbh = dbh;
919 plugin->fn = fn_utf8;
920
921 if ((SQLITE_OK !=
922 sq_prepare (plugin->dbh,
923 "INSERT INTO ds180"
924 " (type, ro, expire, key, prox, value, path, trunc)"
925 " VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
926 &plugin->insert_stmt)) ||
927 (SQLITE_OK !=
928 sq_prepare (plugin->dbh,
929 "SELECT COUNT(*) FROM ds180 "
930 "WHERE key=?"
931 " AND type=?"
932 " AND expire >= ?",
933 &plugin->get_count_stmt)) ||
934 (SQLITE_OK !=
935 sq_prepare (plugin->dbh,
936 "SELECT COUNT(*) FROM ds180 "
937 "WHERE key=? AND expire >= ?",
938 &plugin->get_count_any_stmt)) ||
939 (SQLITE_OK !=
940 sq_prepare (plugin->dbh,
941 "SELECT value,expire,path,trunc,ro"
942 " FROM ds180"
943 " WHERE key=?"
944 " AND type=?"
945 " AND expire >= ?"
946 " LIMIT 1 OFFSET ?",
947 &plugin->get_stmt)) ||
948 (SQLITE_OK !=
949 sq_prepare (plugin->dbh,
950 "SELECT value,expire,path,trunc,type,ro"
951 " FROM ds180"
952 " WHERE key=?"
953 " AND expire >= ?"
954 " LIMIT 1 OFFSET ?",
955 &plugin->get_any_stmt)) ||
956 (SQLITE_OK !=
957 sq_prepare (plugin->dbh,
958 "SELECT _ROWID_,key,value FROM ds180"
959 " WHERE expire < ?1"
960 " ORDER BY expire ASC LIMIT 1",
961 &plugin->del_expired_stmt)) ||
962 (SQLITE_OK !=
963 sq_prepare (plugin->dbh,
964 "SELECT _ROWID_,key,value FROM ds180"
965 " ORDER BY prox ASC, expire ASC LIMIT 1",
966 &plugin->del_select_stmt)) ||
967 (SQLITE_OK !=
968 sq_prepare (plugin->dbh,
969 "DELETE FROM ds180 WHERE _ROWID_=?",
970 &plugin->del_stmt)) ||
971 (SQLITE_OK !=
972 sq_prepare (plugin->dbh,
973 "SELECT * FROM ("
974 " SELECT value,expire,path,trunc,type,ro,key"
975 " FROM ds180 "
976 " WHERE key>=?1 "
977 " AND expire >= ?2"
978 " AND ( (type=?3) or (0 == ?3) )"
979 " ORDER BY KEY ASC LIMIT ?4)"
980 "UNION "
981 "SELECT * FROM ("
982 " SELECT value,expire,path,trunc,type,ro,key"
983 " FROM ds180 "
984 " WHERE key<=?1 "
985 " AND expire >= ?2"
986 " AND ( (type=?3) or (0 == ?3) )"
987 " ORDER BY KEY DESC LIMIT ?4)",
988 &plugin->get_closest_stmt)))
989 {
990 LOG_SQLITE (plugin->dbh,
992 "sq_prepare");
993 GNUNET_break (SQLITE_OK ==
994 sqlite3_close (plugin->dbh));
996 return NULL;
997 }
998
1000 api->cls = plugin;
1001 api->get = &sqlite_plugin_get;
1002 api->put = &sqlite_plugin_put;
1003 api->del = &sqlite_plugin_del;
1004 api->get_closest = &sqlite_plugin_get_closest;
1006 "Sqlite datacache running\n");
1007 return api;
1008}
1009
1010
1011void *
1013
1020void *
1022{
1024 struct Plugin *plugin = api->cls;
1025 int result;
1026
1027#if SQLITE_VERSION_NUMBER >= 3007000
1028 sqlite3_stmt *stmt;
1029#endif
1030
1031#if ! WINDOWS || defined(__CYGWIN__)
1032 if ( (NULL != plugin->fn) &&
1033 (0 != unlink (plugin->fn)) )
1035 "unlink",
1036 plugin->fn);
1037 GNUNET_free (plugin->fn);
1038#endif
1039 sqlite3_finalize (plugin->insert_stmt);
1040 sqlite3_finalize (plugin->get_count_stmt);
1041 sqlite3_finalize (plugin->get_count_any_stmt);
1042 sqlite3_finalize (plugin->get_stmt);
1043 sqlite3_finalize (plugin->get_any_stmt);
1044 sqlite3_finalize (plugin->del_select_stmt);
1045 sqlite3_finalize (plugin->del_expired_stmt);
1046 sqlite3_finalize (plugin->del_stmt);
1047 sqlite3_finalize (plugin->get_closest_stmt);
1048 result = sqlite3_close (plugin->dbh);
1049#if SQLITE_VERSION_NUMBER >= 3007000
1050 if (SQLITE_BUSY == result)
1051 {
1053 _ (
1054 "Tried to close sqlite without finalizing all prepared statements.\n"));
1055 stmt = sqlite3_next_stmt (plugin->dbh,
1056 NULL);
1057 while (NULL != stmt)
1058 {
1059 result = sqlite3_finalize (stmt);
1060 if (result != SQLITE_OK)
1062 "Failed to close statement %p: %d\n",
1063 stmt,
1064 result);
1065 stmt = sqlite3_next_stmt (plugin->dbh,
1066 NULL);
1067 }
1068 result = sqlite3_close (plugin->dbh);
1069 }
1070#endif
1071 if (SQLITE_OK != result)
1072 LOG_SQLITE (plugin->dbh,
1074 "sqlite3_close");
1075
1077 GNUNET_free (api);
1078 return NULL;
1079}
1080
1081
1082/* end of plugin_datacache_sqlite.c */
struct GNUNET_MQ_Envelope * env
Definition 005.c:1
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static uint32_t type
Type string converted to DNS type value.
static size_t data_size
Number of bytes in data.
static int result
Global testing status.
API for database backends for the datacache.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
helper functions for Sqlite3 DB interactions
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
enum GNUNET_GenericReturnValue GNUNET_SQ_extract_result(sqlite3_stmt *result, struct GNUNET_SQ_ResultSpec *rs)
Extract results from a query result according to the given specification.
Definition sq.c:75
void GNUNET_SQ_cleanup_result(struct GNUNET_SQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_SQ_extract_result().
Definition sq.c:104
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint32(uint32_t *u32)
uint32_t expected.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
#define GNUNET_SQ_result_spec_end
End of result parameter specification.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_variable_size(void **dst, size_t *sptr)
Variable-size result expected.
#define GNUNET_SQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
enum GNUNET_GenericReturnValue GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition sq.c:30
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition sq.c:119
#define GNUNET_SQ_query_param_end
End of query parameter specification.
#define GNUNET_SQ_result_spec_auto_from_type(dst)
We expect a fixed-size result, with size determined by the type of * dst
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint64(uint64_t *u64)
uint64_t expected.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
enum GNUNET_GenericReturnValue(* GNUNET_DATACACHE_Iterator)(void *cls, const struct GNUNET_DATACACHE_Block *block)
An iterator over a set of items stored in the datacache.
GNUNET_DHT_RouteOption
Options for routing.
char * GNUNET_DISK_mktemp(const char *t)
Create an (empty) temporary file on disk.
Definition disk.c:431
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_BULK
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition time.c:406
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition strings.c:599
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111
#define _(String)
GNU gettext support macro.
Definition platform.h:179
static ssize_t sqlite_plugin_put(void *cls, uint32_t xor_distance, const struct GNUNET_DATACACHE_Block *block)
Store an item in the datastore.
#define OVERHEAD
How much overhead do we assume per entry in the datacache?
#define SQLITE3_EXEC(db, cmd)
Execute SQL statement.
void * libgnunet_plugin_datacache_sqlite_init(void *cls)
Entry point for the plugin.
#define LOG_SQLITE(db, level, cmd)
Log an error message at log-level level that indicates a failure of the command cmd with the error fr...
static unsigned int sqlite_plugin_get(void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datastore.
static int sq_prepare(sqlite3 *dbh, const char *zSql, sqlite3_stmt **ppStmt)
Prepare a SQL statement.
static enum GNUNET_GenericReturnValue sqlite_plugin_del(void *cls)
Delete the entry with the lowest expiration value from the datacache right now.
static unsigned int get_any(void *cls, const struct GNUNET_HashCode *key, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datastore.
#define LOG_STRERROR_FILE(kind, op, fn)
static unsigned int get_typed(void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datastore.
void * libgnunet_plugin_datacache_sqlite_done(void *cls)
Exit point from the plugin.
#define LOG(kind,...)
static unsigned int sqlite_plugin_get_closest(void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, unsigned int num_results, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results that are "close" to a particular key in the datacache.
void * cls
Closure for all of the callbacks.
Information about a block stored in the datacache.
const struct GNUNET_DHT_PathElement * put_path
PUT path taken by the block, array of peer identities.
enum GNUNET_BLOCK_Type type
Type of the block.
const void * data
Actual block data.
enum GNUNET_DHT_RouteOption ro
Options for routing for the block.
struct GNUNET_PeerIdentity trunc_peer
If the path was truncated, this is the peer ID at which the path was truncated.
struct GNUNET_HashCode key
Key of the block.
size_t data_size
Number of bytes in data.
unsigned int put_path_length
Length of the put_path array.
struct GNUNET_TIME_Absolute expiration_time
When does the block expire?
The datastore service will pass a pointer to a struct of this type as the first and only argument to ...
void * cls
Closure to use for callbacks.
struct returned by the initialization function of the plugin
void * cls
Closure to pass to all plugin functions.
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
A 512-bit hashcode.
Description of a DB query parameter.
Description of a DB result cell.
void * cls
Closure to pass to start_testcase.
Time for absolute times used by GNUnet, in microseconds.
Handle for a plugin.
Definition block.c:38
sqlite3 * dbh
Handle to the sqlite database.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition block.c:47
sqlite3_stmt * get_any_stmt
Prepared statement for sqlite_plugin_get.
unsigned int num_items
Number of key-value pairs in the database.
sqlite3_stmt * get_stmt
Prepared statement for sqlite_plugin_get.
sqlite3_stmt * insert_stmt
Prepared statement for sqlite_plugin_put.
sqlite3_stmt * del_select_stmt
Prepared statement for sqlite_plugin_del.
sqlite3_stmt * get_count_stmt
Prepared statement for sqlite_plugin_get.
sqlite3_stmt * del_expired_stmt
Prepared statement for sqlite_plugin_del.
char * fn
Filename used for the DB.
sqlite3_stmt * get_closest_stmt
Prepared statement for sqlite_plugin_get_closest.
sqlite3_stmt * del_stmt
Prepared statement for sqlite_plugin_del.
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.
sqlite3_stmt * get_count_any_stmt
Prepared statement for sqlite_plugin_get.
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.