GNUnet 0.28.1-dev.5-1-gae1c02d74
 
Loading...
Searching...
No Matches
plugin_datacache_postgres.c File Reference

postgres for an implementation of a database backend for the datacache More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_pq_lib.h"
#include "gnunet_datacache_plugin.h"
Include dependency graph for plugin_datacache_postgres.c:

Go to the source code of this file.

Data Structures

struct  Plugin
 Handle for a plugin. More...
 
struct  HandleResultContext
 Closure for handle_results. More...
 
struct  ExtractResultContext
 Closure for extract_result_cb. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "datacache-postgres", __VA_ARGS__)
 
#define OVERHEAD   (sizeof(struct GNUNET_HashCode) + 24)
 Per-entry overhead estimate.
 

Functions

static void reconnect_setup (void *cls, struct GNUNET_PQ_Context *pq)
 Function called whenever we reconnect to the DB.
 
static enum GNUNET_GenericReturnValue init_connection (struct Plugin *plugin)
 Get a database handle.
 
static ssize_t postgres_plugin_put (void *cls, uint32_t prox, const struct GNUNET_DATACACHE_Block *block)
 Store an item in the datastore.
 
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.
 
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.
 
static enum GNUNET_GenericReturnValue postgres_plugin_del (void *cls)
 Delete the entry with the lowest expiration value from the datacache right now.
 
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.
 
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.
 
void * libgnunet_plugin_datacache_postgres_init (void *cls)
 Entry point for the plugin.
 
void * libgnunet_plugin_datacache_postgres_done (void *cls)
 Exit point from the plugin.
 

Detailed Description

postgres for an implementation of a database backend for the datacache

Author
Christian Grothoff

Definition in file plugin_datacache_postgres.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "datacache-postgres", __VA_ARGS__)

Definition at line 31 of file plugin_datacache_postgres.c.

◆ OVERHEAD

#define OVERHEAD   (sizeof(struct GNUNET_HashCode) + 24)

Per-entry overhead estimate.

Definition at line 36 of file plugin_datacache_postgres.c.

Function Documentation

◆ reconnect_setup()

static void reconnect_setup ( void *  cls,
struct GNUNET_PQ_Context pq 
)
static

Function called whenever we reconnect to the DB.

Sets up the options, possibly the tables and the prepared statements.

Parameters
clsthe struct Plugin
pqdatabase handle

Definition at line 74 of file plugin_datacache_postgres.c.

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"
83 " ORDER BY expiration_time DESC"),
85 "SELECT expiration_time,type,ro,value,trunc,path"
86 " FROM datacache.gn180dc"
87 " WHERE key=$1 AND expiration_time >= $2"
88 " ORDER BY expiration_time DESC"),
90 "SELECT LENGTH(value) AS len,oid,key"
91 " FROM datacache.gn180dc"
92 " WHERE expiration_time < $1"
93 " ORDER BY expiration_time ASC LIMIT 1"),
95 "SELECT LENGTH(value) AS len,oid,key"
96 " FROM datacache.gn180dc"
97 " ORDER BY prox ASC, expiration_time ASC LIMIT 1"),
98 GNUNET_PQ_make_prepare ("get_closest",
99 "(SELECT expiration_time,type,ro,value,trunc,path,key"
100 " FROM datacache.gn180dc"
101 " WHERE key >= $1"
102 " AND expiration_time >= $2"
103 " AND ( (type = $3) OR ( 0 = $3) )"
104 " ORDER BY key ASC"
105 " LIMIT $4)"
106 " UNION "
107 "(SELECT expiration_time,type,ro,value,trunc,path,key"
108 " FROM datacache.gn180dc"
109 " WHERE key <= $1"
110 " AND expiration_time >= $2"
111 " AND ( (type = $3) OR ( 0 = $3) )"
112 " ORDER BY key DESC"
113 " LIMIT $4)"),
114 GNUNET_PQ_make_prepare ("delrow",
115 "DELETE FROM datacache.gn180dc"
116 " WHERE oid=$1"),
118 "INSERT INTO datacache.gn180dc"
119 " (type, ro, prox, expiration_time, key, value, trunc, path) "
120 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"),
122 };
123
124 if (GNUNET_OK !=
126 "datacache-"))
127 {
128 plugin->ready = false;
129 return;
130 }
131 if (GNUNET_OK !=
133 ps))
134 {
135 plugin->ready = false;
136 return;
137 }
138 plugin->ready = true;
139}
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
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:77
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_PREPARED_STATEMENT_END
Terminator for prepared statement list.
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:486
@ GNUNET_OK
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
Handle for a plugin.
Definition block.c:38

References GNUNET_OK, GNUNET_PQ_make_prepare(), GNUNET_PQ_prepare_statements(), GNUNET_PQ_PREPARED_STATEMENT_END, GNUNET_PQ_run_sql(), plugin, and ps.

Referenced by init_connection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_connection()

static enum GNUNET_GenericReturnValue init_connection ( struct Plugin plugin)
static

Get a database handle.

Parameters
pluginglobal context
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 149 of file plugin_datacache_postgres.c.

150{
151 plugin->dbh = GNUNET_PQ_init (plugin->env->cfg,
152 "datacache-postgres",
154 plugin);
155 if (NULL == plugin->dbh)
156 return GNUNET_SYSERR;
157 if (! plugin->ready)
158 return GNUNET_NO;
159 return GNUNET_OK;
160}
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.
@ GNUNET_NO
@ GNUNET_SYSERR
static void reconnect_setup(void *cls, struct GNUNET_PQ_Context *pq)
Function called whenever we reconnect to the DB.

References GNUNET_NO, GNUNET_OK, GNUNET_PQ_init(), GNUNET_SYSERR, plugin, and reconnect_setup().

Referenced by libgnunet_plugin_datacache_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_put()

static ssize_t postgres_plugin_put ( void *  cls,
uint32_t  prox,
const struct GNUNET_DATACACHE_Block block 
)
static

Store an item in the datastore.

Parameters
clsclosure (our struct Plugin)
proxproximity of key to my PID
blockdata to store
Returns
0 if duplicate, -1 on error, number of bytes used otherwise

Definition at line 172 of file plugin_datacache_postgres.c.

175{
176 struct Plugin *plugin = cls;
177 uint32_t type32 = (uint32_t) block->type;
178 uint32_t ro32 = (uint32_t) block->type;
179 struct GNUNET_PQ_QueryParam params[] = {
186 block->data_size),
188 (0 == block->put_path_length)
191 block->put_path,
192 block->put_path_length
193 * sizeof(struct GNUNET_DHT_PathElement)),
195 };
197
199 "put",
200 params);
201 if (0 > ret)
202 return -1;
203 plugin->num_items++;
204 return block->data_size + OVERHEAD;
205}
static int ret
Final status code.
Definition gnunet-arm.c:93
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
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.
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_null(void)
Generate query parameter to create a NULL value.
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_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.
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
#define OVERHEAD
Per-entry overhead estimate.
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.
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?
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
Description of a DB query parameter.

References GNUNET_DATACACHE_Block::data, GNUNET_DATACACHE_Block::data_size, GNUNET_DATACACHE_Block::expiration_time, GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_query_param_absolute_time(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_fixed_size(), GNUNET_PQ_query_param_null(), GNUNET_PQ_query_param_uint32(), GNUNET_DATACACHE_Block::key, OVERHEAD, plugin, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, ret, GNUNET_DATACACHE_Block::trunc_peer, and GNUNET_DATACACHE_Block::type.

Referenced by libgnunet_plugin_datacache_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_results()

static void handle_results ( void *  cls,
PGresult *  result,
unsigned int  num_results 
)
static

Function to be called with the results of a SELECT statement that has returned num_results results.

Parse the result and call the callback given in cls

Parameters
clsclosure of type struct HandleResultContext
resultthe postgres result
num_resultsthe number of results in result

Definition at line 240 of file plugin_datacache_postgres.c.

243{
244 struct HandleResultContext *hrc = cls;
245
246 for (unsigned int i = 0; i < num_results; i++)
247 {
248 uint32_t type32;
249 uint32_t bro32;
250 void *data;
251 struct GNUNET_DATACACHE_Block block;
252 void *path = NULL;
253 size_t path_size = 0;
254 struct GNUNET_PQ_ResultSpec rs[] = {
255 GNUNET_PQ_result_spec_absolute_time ("expiration_time",
256 &block.expiration_time),
258 &type32),
260 &bro32),
262 &data,
263 &block.data_size),
265 &block.trunc_peer),
268 &path,
269 &path_size),
270 NULL),
272 };
273
274 if (GNUNET_YES !=
276 rs,
277 i))
278 {
279 GNUNET_break (0);
280 return;
281 }
282 if (0 != (path_size % sizeof(struct GNUNET_DHT_PathElement)))
283 {
284 GNUNET_break (0);
285 path_size = 0;
286 path = NULL;
287 }
288 block.data = data;
289 block.put_path = path;
290 block.put_path_length
291 = path_size / sizeof (struct GNUNET_DHT_PathElement);
292 block.type = (enum GNUNET_BLOCK_Type) type32;
293 block.ro = (enum GNUNET_DHT_RouteOption) bro32;
294 block.key = *hrc->key;
296 "Found result of size %u bytes and type %u in database\n",
297 (unsigned int) block.data_size,
298 (unsigned int) block.type);
299 if ( (NULL != hrc->iter) &&
300 (GNUNET_SYSERR ==
301 hrc->iter (hrc->iter_cls,
302 &block)) )
303 {
305 "Ending iteration (client error)\n");
307 return;
308 }
310 }
311}
static char * data
The data to insert into the dht.
static int result
Global testing status.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
#define GNUNET_PQ_result_spec_auto_from_type(name, dst)
We expect a fixed-size result, with size determined by the type of * dst
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_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
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
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
GNUNET_DHT_RouteOption
Options for routing.
@ GNUNET_YES
#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
#define LOG(kind,...)
Information about a block stored in the datacache.
Description of a DB result cell.
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.

References data, GNUNET_DATACACHE_Block::data, GNUNET_DATACACHE_Block::data_size, GNUNET_DATACACHE_Block::expiration_time, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_PQ_cleanup_result(), GNUNET_PQ_extract_result(), GNUNET_PQ_result_spec_absolute_time(), GNUNET_PQ_result_spec_allow_null(), GNUNET_PQ_result_spec_auto_from_type, GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_uint32(), GNUNET_PQ_result_spec_variable_size(), GNUNET_SYSERR, GNUNET_YES, HandleResultContext::iter, HandleResultContext::iter_cls, GNUNET_DATACACHE_Block::key, HandleResultContext::key, LOG, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, result, GNUNET_DATACACHE_Block::ro, GNUNET_DATACACHE_Block::trunc_peer, and GNUNET_DATACACHE_Block::type.

Referenced by postgres_plugin_get().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_get()

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 
)
static

Iterate over the results for a particular key in the datastore.

Parameters
clsclosure (our struct Plugin)
keykey to look for
typeentries of which type are relevant?
itermaybe NULL (to just count)
iter_clsclosure for iter
Returns
the number of results found

Definition at line 326 of file plugin_datacache_postgres.c.

331{
332 struct Plugin *plugin = cls;
333 uint32_t type32 = (uint32_t) type;
334 struct GNUNET_TIME_Absolute now = { 0 };
335 struct GNUNET_PQ_QueryParam paramk[] = {
339 };
340 struct GNUNET_PQ_QueryParam paramkt[] = {
345 };
347 struct HandleResultContext hr_ctx;
348
350 hr_ctx.iter = iter;
351 hr_ctx.iter_cls = iter_cls;
352 hr_ctx.key = key;
354 (0 == type) ? "getk" : "getkt",
355 (0 == type) ? paramk : paramkt,
357 &hr_ctx);
358 if (res < 0)
359 return 0;
360 return res;
361}
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.
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_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111
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.
Time for absolute times used by GNUnet, in microseconds.

References GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_absolute_time(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_uint32(), GNUNET_TIME_absolute_get(), handle_results(), HandleResultContext::iter, HandleResultContext::iter_cls, key, HandleResultContext::key, plugin, res, and type.

Referenced by libgnunet_plugin_datacache_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_del()

static enum GNUNET_GenericReturnValue postgres_plugin_del ( void *  cls)
static

Delete the entry with the lowest expiration value from the datacache right now.

Parameters
clsclosure (our struct Plugin)
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 372 of file plugin_datacache_postgres.c.

373{
374 struct Plugin *plugin = cls;
375 struct GNUNET_PQ_QueryParam pempty[] = {
377 };
378 uint32_t size;
379 uint64_t oid;
380 struct GNUNET_HashCode key;
381 struct GNUNET_PQ_ResultSpec rs[] = {
383 &size),
385 &oid),
387 &key),
389 };
391 struct GNUNET_TIME_Absolute now;
393 {
394 struct GNUNET_PQ_QueryParam xparam[] = {
397 };
398
400 "getex",
401 xparam,
402 rs);
403 }
404 if (0 >= res)
406 "getm",
407 pempty,
408 rs);
409 if (0 > res)
410 return GNUNET_SYSERR;
412 {
413 /* no result */
415 "Ending iteration (no more results)\n");
416 return 0;
417 }
418 {
419 struct GNUNET_PQ_QueryParam dparam[] = {
422 };
424 "delrow",
425 dparam);
426 }
427 if (0 > res)
428 {
430 return GNUNET_SYSERR;
431 }
432 plugin->num_items--;
433 plugin->env->delete_notify (plugin->env->cls,
434 &key,
435 size + OVERHEAD);
437 return GNUNET_OK;
438}
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint64_t in host byte order.
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_ResultSpec GNUNET_PQ_result_spec_uint64(const char *name, uint64_t *u64)
uint64_t expected.
uint32_t oid
static unsigned int size
Size of the "table".
Definition peer.c:68
A 512-bit hashcode.
void * cls
Closure to pass to start_testcase.

References GNUNET_TESTING_PluginFunctions::cls, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_ERROR_TYPE_DEBUG, GNUNET_OK, GNUNET_PQ_cleanup_result(), GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_query_param_absolute_time(), GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_uint64(), GNUNET_PQ_result_spec_auto_from_type, GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_uint32(), GNUNET_PQ_result_spec_uint64(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), key, LOG, oid, OVERHEAD, plugin, res, and size.

Referenced by libgnunet_plugin_datacache_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ extract_result_cb()

static void extract_result_cb ( void *  cls,
PGresult *  result,
unsigned int  num_results 
)
static

Function to be called with the results of a SELECT statement that has returned num_results results.

Calls the iter from cls for each result.

Parameters
clsclosure with the struct ExtractResultContext
resultthe postgres result
num_resultsthe number of results in result

Definition at line 468 of file plugin_datacache_postgres.c.

471{
472 struct ExtractResultContext *erc = cls;
473
474 if (NULL == erc->iter)
475 return;
476 for (unsigned int i = 0; i < num_results; i++)
477 {
478 uint32_t type32;
479 uint32_t bro32;
480 struct GNUNET_DATACACHE_Block block;
481 void *data;
482 void *path;
483 size_t path_size;
484 struct GNUNET_PQ_ResultSpec rs[] = {
485 GNUNET_PQ_result_spec_absolute_time ("expiration_time",
486 &block.expiration_time),
488 &type32),
490 &bro32),
492 &data,
493 &block.data_size),
495 &block.trunc_peer),
497 &path,
498 &path_size),
500 &block.key),
502 };
503
504 if (GNUNET_YES !=
506 rs,
507 i))
508 {
509 GNUNET_break (0);
510 return;
511 }
512 if (0 != (path_size % sizeof(struct GNUNET_DHT_PathElement)))
513 {
514 GNUNET_break (0);
515 path_size = 0;
516 path = NULL;
517 }
518 block.type = (enum GNUNET_BLOCK_Type) type32;
519 block.ro = (enum GNUNET_DHT_RouteOption) bro32;
520 block.data = data;
521 block.put_path = path;
522 block.put_path_length = path_size / sizeof (struct GNUNET_DHT_PathElement);
524 "Found result of size %u bytes and type %u in database\n",
525 (unsigned int) block.data_size,
526 (unsigned int) block.type);
527 if ( (NULL != erc->iter) &&
528 (GNUNET_SYSERR ==
529 erc->iter (erc->iter_cls,
530 &block)) )
531 {
533 "Ending iteration (client error)\n");
535 break;
536 }
538 }
539}
Closure for extract_result_cb.
void * iter_cls
Closure for iter.
GNUNET_DATACACHE_Iterator iter
Function to call for each result found.

References data, GNUNET_DATACACHE_Block::data, GNUNET_DATACACHE_Block::data_size, GNUNET_DATACACHE_Block::expiration_time, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_PQ_cleanup_result(), GNUNET_PQ_extract_result(), GNUNET_PQ_result_spec_absolute_time(), GNUNET_PQ_result_spec_auto_from_type, GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_uint32(), GNUNET_PQ_result_spec_variable_size(), GNUNET_SYSERR, GNUNET_YES, ExtractResultContext::iter, ExtractResultContext::iter_cls, GNUNET_DATACACHE_Block::key, LOG, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, result, GNUNET_DATACACHE_Block::ro, GNUNET_DATACACHE_Block::trunc_peer, and GNUNET_DATACACHE_Block::type.

Referenced by postgres_plugin_get_closest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_get_closest()

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 
)
static

Iterate over the results that are "close" to a particular key in the datacache.

"close" is defined as numerically larger than key (when interpreted as a circular address space), with small distance.

Parameters
clsclosure (internal context for the plugin)
keyarea of the keyspace to look into
typedesired block type for the replies
num_resultsnumber of results that should be returned to iter
itermaybe NULL (to just count)
iter_clsclosure for iter
Returns
the number of results found

Definition at line 557 of file plugin_datacache_postgres.c.

563{
564 struct Plugin *plugin = cls;
565 uint32_t num_results32 = (uint32_t) num_results;
566 uint32_t type32 = (uint32_t) type;
567 struct GNUNET_TIME_Absolute now;
569 struct ExtractResultContext erc;
570
571 erc.iter = iter;
572 erc.iter_cls = iter_cls;
574 {
575 struct GNUNET_PQ_QueryParam params[] = {
579 GNUNET_PQ_query_param_uint32 (&num_results32),
581 };
583 "get_closest",
584 params,
586 &erc);
587 }
588 if (0 > res)
589 {
591 "Ending iteration (postgres error)\n");
592 return 0;
593 }
595 {
596 /* no result */
598 "Ending iteration (no more results)\n");
599 return 0;
600 }
601 return res;
602}
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.

References extract_result_cb(), GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_ERROR_TYPE_DEBUG, GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_absolute_time(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_uint32(), GNUNET_TIME_absolute_get(), ExtractResultContext::iter, ExtractResultContext::iter_cls, key, LOG, plugin, res, and type.

Referenced by libgnunet_plugin_datacache_postgres_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ libgnunet_plugin_datacache_postgres_init()

void * libgnunet_plugin_datacache_postgres_init ( void *  cls)

Entry point for the plugin.

Parameters
clsclosure (the struct GNUNET_DATACACHE_PluginEnvironmnet)
Returns
the plugin's closure (our struct Plugin)

Definition at line 615 of file plugin_datacache_postgres.c.

616{
619 struct Plugin *plugin;
620
621 plugin = GNUNET_new (struct Plugin);
622 plugin->env = env;
623
625 {
627 return NULL;
628 }
629
631 api->cls = plugin;
632 api->get = &postgres_plugin_get;
633 api->put = &postgres_plugin_put;
634 api->del = &postgres_plugin_del;
635 api->get_closest = &postgres_plugin_get_closest;
637 "Postgres datacache running\n");
638 return api;
639}
struct GNUNET_MQ_Envelope * env
Definition 005.c:1
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
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.
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.
void * cls
Closure for all of the callbacks.
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
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition block.c:47

References Plugin::api, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_DATACACHE_PluginEnvironment::cls, env, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_new, GNUNET_OK, init_connection(), LOG, plugin, postgres_plugin_del(), postgres_plugin_get(), postgres_plugin_get_closest(), and postgres_plugin_put().

Here is the call graph for this function:

◆ libgnunet_plugin_datacache_postgres_done()

void * libgnunet_plugin_datacache_postgres_done ( void *  cls)

Exit point from the plugin.

Parameters
clsclosure (our struct Plugin)
Returns
NULL

Definition at line 652 of file plugin_datacache_postgres.c.

653{
655 struct Plugin *plugin = api->cls;
656
659 "datacache-drop"));
663 return NULL;
664}
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:788
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:388
void * cls
Closure to pass to all plugin functions.

References Plugin::api, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_DATACACHE_PluginFunctions::cls, GNUNET_break, GNUNET_free, GNUNET_OK, GNUNET_PQ_disconnect(), GNUNET_PQ_exec_sql(), and plugin.

Here is the call graph for this function: