GNUnet 0.28.0-dev.2-27-gc87478450
 
Loading...
Searching...
No Matches
plugin_datacache_postgres.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet
3 Copyright (C) 2006, 2009, 2010, 2012, 2015, 2017, 2018, 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"
28#include "gnunet_pq_lib.h"
30
31#define LOG(kind, ...) GNUNET_log_from (kind, "datacache-postgres", __VA_ARGS__)
32
36#define OVERHEAD (sizeof(struct GNUNET_HashCode) + 24)
37
41struct Plugin
42{
47
52
56 unsigned int num_items;
57
61 bool ready;
62
63};
64
65
73static void
74reconnect_setup (void *cls,
75 struct GNUNET_PQ_Context *pq)
76{
77 struct Plugin *plugin = cls;
80 "SELECT expiration_time,type,ro,value,trunc,path"
81 " FROM datacache.gn180dc"
82 " WHERE key=$1 AND type=$2 AND expiration_time >= $3"),
84 "SELECT expiration_time,type,ro,value,trunc,path"
85 " FROM datacache.gn180dc"
86 " WHERE key=$1 AND expiration_time >= $2"),
88 "SELECT LENGTH(value) AS len,oid,key"
89 " FROM datacache.gn180dc"
90 " WHERE expiration_time < $1"
91 " ORDER BY expiration_time ASC LIMIT 1"),
93 "SELECT LENGTH(value) AS len,oid,key"
94 " FROM datacache.gn180dc"
95 " ORDER BY prox ASC, expiration_time ASC LIMIT 1"),
96 GNUNET_PQ_make_prepare ("get_closest",
97 "(SELECT expiration_time,type,ro,value,trunc,path,key"
98 " FROM datacache.gn180dc"
99 " WHERE key >= $1"
100 " AND expiration_time >= $2"
101 " AND ( (type = $3) OR ( 0 = $3) )"
102 " ORDER BY key ASC"
103 " LIMIT $4)"
104 " UNION "
105 "(SELECT expiration_time,type,ro,value,trunc,path,key"
106 " FROM datacache.gn180dc"
107 " WHERE key <= $1"
108 " AND expiration_time >= $2"
109 " AND ( (type = $3) OR ( 0 = $3) )"
110 " ORDER BY key DESC"
111 " LIMIT $4)"),
112 GNUNET_PQ_make_prepare ("delrow",
113 "DELETE FROM datacache.gn180dc"
114 " WHERE oid=$1"),
116 "INSERT INTO datacache.gn180dc"
117 " (type, ro, prox, expiration_time, key, value, trunc, path) "
118 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"),
120 };
121
122 if (GNUNET_OK !=
124 "datacache-"))
125 {
126 plugin->ready = false;
127 return;
128 }
129 if (GNUNET_OK !=
131 ps))
132 {
133 plugin->ready = false;
134 return;
135 }
136 plugin->ready = true;
137}
138
139
148{
149 plugin->dbh = GNUNET_PQ_init (plugin->env->cfg,
150 "datacache-postgres",
152 plugin);
153 if (NULL == plugin->dbh)
154 return GNUNET_SYSERR;
155 if (! plugin->ready)
156 return GNUNET_NO;
157 return GNUNET_OK;
158}
159
160
169static ssize_t
171 uint32_t prox,
172 const struct GNUNET_DATACACHE_Block *block)
173{
174 struct Plugin *plugin = cls;
175 uint32_t type32 = (uint32_t) block->type;
176 uint32_t ro32 = (uint32_t) block->type;
177 struct GNUNET_PQ_QueryParam params[] = {
184 block->data_size),
186 (0 == block->put_path_length)
189 block->put_path,
190 block->put_path_length
191 * sizeof(struct GNUNET_DHT_PathElement)),
193 };
195
197 "put",
198 params);
199 if (0 > ret)
200 return -1;
201 plugin->num_items++;
202 return block->data_size + OVERHEAD;
203}
204
205
226
227
237static void
238handle_results (void *cls,
239 PGresult *result,
240 unsigned int num_results)
241{
242 struct HandleResultContext *hrc = cls;
243
244 for (unsigned int i = 0; i < num_results; i++)
245 {
246 uint32_t type32;
247 uint32_t bro32;
248 void *data;
249 struct GNUNET_DATACACHE_Block block;
250 void *path = NULL;
251 size_t path_size = 0;
252 struct GNUNET_PQ_ResultSpec rs[] = {
253 GNUNET_PQ_result_spec_absolute_time ("expiration_time",
254 &block.expiration_time),
256 &type32),
258 &bro32),
260 &data,
261 &block.data_size),
263 &block.trunc_peer),
266 &path,
267 &path_size),
268 NULL),
270 };
271
272 if (GNUNET_YES !=
274 rs,
275 i))
276 {
277 GNUNET_break (0);
278 return;
279 }
280 if (0 != (path_size % sizeof(struct GNUNET_DHT_PathElement)))
281 {
282 GNUNET_break (0);
283 path_size = 0;
284 path = NULL;
285 }
286 block.data = data;
287 block.put_path = path;
288 block.put_path_length
289 = path_size / sizeof (struct GNUNET_DHT_PathElement);
290 block.type = (enum GNUNET_BLOCK_Type) type32;
291 block.ro = (enum GNUNET_DHT_RouteOption) bro32;
292 block.key = *hrc->key;
294 "Found result of size %u bytes and type %u in database\n",
295 (unsigned int) block.data_size,
296 (unsigned int) block.type);
297 if ( (NULL != hrc->iter) &&
298 (GNUNET_SYSERR ==
299 hrc->iter (hrc->iter_cls,
300 &block)) )
301 {
303 "Ending iteration (client error)\n");
305 return;
306 }
308 }
309}
310
311
323static unsigned int
325 const struct GNUNET_HashCode *key,
328 void *iter_cls)
329{
330 struct Plugin *plugin = cls;
331 uint32_t type32 = (uint32_t) type;
332 struct GNUNET_TIME_Absolute now = { 0 };
333 struct GNUNET_PQ_QueryParam paramk[] = {
337 };
338 struct GNUNET_PQ_QueryParam paramkt[] = {
343 };
345 struct HandleResultContext hr_ctx;
346
348 hr_ctx.iter = iter;
349 hr_ctx.iter_cls = iter_cls;
350 hr_ctx.key = key;
352 (0 == type) ? "getk" : "getkt",
353 (0 == type) ? paramk : paramkt,
355 &hr_ctx);
356 if (res < 0)
357 return 0;
358 return res;
359}
360
361
371{
372 struct Plugin *plugin = cls;
373 struct GNUNET_PQ_QueryParam pempty[] = {
375 };
376 uint32_t size;
377 uint64_t oid;
378 struct GNUNET_HashCode key;
379 struct GNUNET_PQ_ResultSpec rs[] = {
381 &size),
383 &oid),
385 &key),
387 };
389 struct GNUNET_TIME_Absolute now;
391 {
392 struct GNUNET_PQ_QueryParam xparam[] = {
395 };
396
398 "getex",
399 xparam,
400 rs);
401 }
402 if (0 >= res)
404 "getm",
405 pempty,
406 rs);
407 if (0 > res)
408 return GNUNET_SYSERR;
410 {
411 /* no result */
413 "Ending iteration (no more results)\n");
414 return 0;
415 }
416 {
417 struct GNUNET_PQ_QueryParam dparam[] = {
420 };
422 "delrow",
423 dparam);
424 }
425 if (0 > res)
426 {
428 return GNUNET_SYSERR;
429 }
430 plugin->num_items--;
431 plugin->env->delete_notify (plugin->env->cls,
432 &key,
433 size + OVERHEAD);
435 return GNUNET_OK;
436}
437
438
454
455
465static void
467 PGresult *result,
468 unsigned int num_results)
469{
470 struct ExtractResultContext *erc = cls;
471
472 if (NULL == erc->iter)
473 return;
474 for (unsigned int i = 0; i < num_results; i++)
475 {
476 uint32_t type32;
477 uint32_t bro32;
478 struct GNUNET_DATACACHE_Block block;
479 void *data;
480 void *path;
481 size_t path_size;
482 struct GNUNET_PQ_ResultSpec rs[] = {
483 GNUNET_PQ_result_spec_absolute_time ("expiration_time",
484 &block.expiration_time),
486 &type32),
488 &bro32),
490 &data,
491 &block.data_size),
493 &block.trunc_peer),
495 &path,
496 &path_size),
498 &block.key),
500 };
501
502 if (GNUNET_YES !=
504 rs,
505 i))
506 {
507 GNUNET_break (0);
508 return;
509 }
510 if (0 != (path_size % sizeof(struct GNUNET_DHT_PathElement)))
511 {
512 GNUNET_break (0);
513 path_size = 0;
514 path = NULL;
515 }
516 block.type = (enum GNUNET_BLOCK_Type) type32;
517 block.ro = (enum GNUNET_DHT_RouteOption) bro32;
518 block.data = data;
519 block.put_path = path;
520 block.put_path_length = path_size / sizeof (struct GNUNET_DHT_PathElement);
522 "Found result of size %u bytes and type %u in database\n",
523 (unsigned int) block.data_size,
524 (unsigned int) block.type);
525 if ( (NULL != erc->iter) &&
526 (GNUNET_SYSERR ==
527 erc->iter (erc->iter_cls,
528 &block)) )
529 {
531 "Ending iteration (client error)\n");
533 break;
534 }
536 }
537}
538
539
554static unsigned int
556 const struct GNUNET_HashCode *key,
558 unsigned int num_results,
560 void *iter_cls)
561{
562 struct Plugin *plugin = cls;
563 uint32_t num_results32 = (uint32_t) num_results;
564 uint32_t type32 = (uint32_t) type;
565 struct GNUNET_TIME_Absolute now;
567 struct ExtractResultContext erc;
568
569 erc.iter = iter;
570 erc.iter_cls = iter_cls;
572 {
573 struct GNUNET_PQ_QueryParam params[] = {
577 GNUNET_PQ_query_param_uint32 (&num_results32),
579 };
581 "get_closest",
582 params,
584 &erc);
585 }
586 if (0 > res)
587 {
589 "Ending iteration (postgres error)\n");
590 return 0;
591 }
593 {
594 /* no result */
596 "Ending iteration (no more results)\n");
597 return 0;
598 }
599 return res;
600}
601
602
603void *
605
612void *
614{
617 struct Plugin *plugin;
618
619 plugin = GNUNET_new (struct Plugin);
620 plugin->env = env;
621
623 {
625 return NULL;
626 }
627
629 api->cls = plugin;
630 api->get = &postgres_plugin_get;
631 api->put = &postgres_plugin_put;
632 api->del = &postgres_plugin_del;
633 api->get_closest = &postgres_plugin_get_closest;
635 "Postgres datacache running\n");
636 return api;
637}
638
639
640void *
642
649void *
651{
653 struct Plugin *plugin = api->cls;
654
657 "datacache-drop"));
661 return NULL;
662}
663
664
665/* end of plugin_datacache_postgres.c */
struct GNUNET_MQ_Envelope * env
Definition 005.c:1
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static char * res
Currently read line or NULL on EOF.
static uint32_t type
Type string converted to DNS type value.
static int result
Global testing status.
API for database backends for the datacache.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
helper functions for Postgres DB interactions
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint64_t in host byte order.
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
Execute a named prepared statement that is a SELECT statement which may return multiple results in co...
Definition pq_eval.c:165
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_null(void)
Generate query parameter to create a NULL value.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, struct GNUNET_PQ_ResultSpec *rs)
Execute a named prepared statement that is a SELECT statement which must return a single result in co...
Definition pq_eval.c:199
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
#define GNUNET_PQ_result_spec_auto_from_type(name, dst)
We expect a fixed-size result, with size determined by the type of * dst
enum GNUNET_GenericReturnValue GNUNET_PQ_prepare_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition pq_prepare.c:73
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64(const char *name, uint64_t *u64)
uint64_t expected.
void GNUNET_PQ_disconnect(struct GNUNET_PQ_Context *db)
Disconnect from the database, destroying the prepared statements and releasing other associated resou...
Definition pq_connect.c:678
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_allow_null(struct GNUNET_PQ_ResultSpec rs, bool *is_null)
Allow NULL value to be found in the database for the given value.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time(const char *name, struct GNUNET_TIME_Absolute *at)
Absolute time expected.
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare(const char *name, const char *sql)
Create a struct GNUNET_PQ_PreparedStatement.
Definition pq_prepare.c:30
#define GNUNET_PQ_query_param_end
End of query parameter specification.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
struct GNUNET_PQ_Context * GNUNET_PQ_init(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_PQ_ReconnectCallback rc, void *rc_cls)
Connect to a postgres database using the configuration option "CONFIG" in section.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size(const char *name, void **dst, size_t *sptr)
Variable-size result expected.
void GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_PQ_extract_result().
Definition pq.c:142
#define GNUNET_PQ_PREPARED_STATEMENT_END
Terminator for prepared statement list.
enum GNUNET_GenericReturnValue GNUNET_PQ_extract_result(PGresult *result, struct GNUNET_PQ_ResultSpec *rs, int row)
Extract results from a query result according to the given specification.
Definition pq.c:152
uint32_t oid
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connection using the given param...
Definition pq_eval.c:135
enum GNUNET_GenericReturnValue GNUNET_PQ_exec_sql(struct GNUNET_PQ_Context *db, const char *buf)
Execute SQL statements from buf against db.
Definition pq_connect.c:294
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
enum GNUNET_GenericReturnValue GNUNET_PQ_run_sql(struct GNUNET_PQ_Context *db, const char *load_suffix)
Within the db context, run all the SQL files in the load path where the name starts with the load_suf...
Definition pq_connect.c:398
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.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111
static unsigned int size
Size of the "table".
Definition peer.c:68
#define OVERHEAD
Per-entry overhead estimate.
void * libgnunet_plugin_datacache_postgres_init(void *cls)
Entry point for the plugin.
static ssize_t postgres_plugin_put(void *cls, uint32_t prox, const struct GNUNET_DATACACHE_Block *block)
Store an item in the datastore.
static enum GNUNET_GenericReturnValue postgres_plugin_del(void *cls)
Delete the entry with the lowest expiration value from the datacache right now.
static enum GNUNET_GenericReturnValue init_connection(struct Plugin *plugin)
Get a database handle.
static unsigned int postgres_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.
void * libgnunet_plugin_datacache_postgres_done(void *cls)
Exit point from the plugin.
static unsigned int postgres_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.
static void extract_result_cb(void *cls, PGresult *result, unsigned int num_results)
Function to be called with the results of a SELECT statement that has returned num_results results.
#define LOG(kind,...)
static void reconnect_setup(void *cls, struct GNUNET_PQ_Context *pq)
Function called whenever we reconnect to the DB.
static void handle_results(void *cls, PGresult *result, unsigned int num_results)
Function to be called with the results of a SELECT statement that has returned num_results results.
Closure for extract_result_cb.
void * iter_cls
Closure for iter.
GNUNET_DATACACHE_Iterator iter
Function to call for each result found.
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.
Handle to Postgres database.
Definition pq.h:36
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
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.
Closure for handle_results.
void * iter_cls
Closure for iter.
const struct GNUNET_HashCode * key
Key used.
GNUNET_DATACACHE_Iterator iter
Function to call on each result, may be NULL.
Handle for a plugin.
Definition block.c:38
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition block.c:47
unsigned int num_items
Number of key-value pairs in the database.
bool ready
Set to true if the DB is ready for action.
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.