GNUnet  0.10.x
Data Structures | Macros | Functions
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. More...
 

Functions

static int init_connection (struct Plugin *plugin)
 Get a database handle. More...
 
static ssize_t postgres_plugin_put (void *cls, const struct GNUNET_HashCode *key, uint32_t prox, size_t data_size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute discard_time, unsigned int path_info_len, const struct GNUNET_PeerIdentity *path_info)
 Store an item in the datastore. More...
 
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. More...
 
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. More...
 
static int postgres_plugin_del (void *cls)
 Delete the entry with the lowest expiration value from the datacache right now. More...
 
static unsigned int postgres_plugin_get_random (void *cls, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
 Obtain a random key-value pair from the datacache. More...
 
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. More...
 
static unsigned int postgres_plugin_get_closest (void *cls, const struct GNUNET_HashCode *key, 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. More...
 
void * libgnunet_plugin_datacache_postgres_init (void *cls)
 Entry point for the plugin. More...
 
void * libgnunet_plugin_datacache_postgres_done (void *cls)
 Exit point from the plugin. More...
 

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

◆ OVERHEAD

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

Per-entry overhead estimate.

Definition at line 36 of file plugin_datacache_postgres.c.

Referenced by postgres_plugin_del(), and postgres_plugin_put().

Function Documentation

◆ init_connection()

static int 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 67 of file plugin_datacache_postgres.c.

References GNUNET_DATACACHE_PluginEnvironment::cfg, Plugin::dbh, Plugin::env, GNUNET_OK, GNUNET_PQ_connect_with_cfg(), GNUNET_PQ_exec_statements(), GNUNET_PQ_EXECUTE_STATEMENT_END, GNUNET_PQ_make_execute(), GNUNET_PQ_make_prepare(), GNUNET_PQ_make_try_execute(), GNUNET_PQ_prepare_statements(), GNUNET_PQ_PREPARED_STATEMENT_END, and GNUNET_SYSERR.

Referenced by libgnunet_plugin_datacache_postgres_init().

68 {
69  struct GNUNET_PQ_ExecuteStatement es[] = {
70  GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS gn011dc ("
71  " type INTEGER NOT NULL,"
72  " prox INTEGER NOT NULL,"
73  " discard_time BIGINT NOT NULL,"
74  " key BYTEA NOT NULL,"
75  " value BYTEA NOT NULL,"
76  " path BYTEA DEFAULT NULL)"
77  "WITH OIDS"),
78  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_key ON gn011dc (key)"),
79  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_dt ON gn011dc (discard_time)"),
80  GNUNET_PQ_make_execute ("ALTER TABLE gn011dc ALTER value SET STORAGE EXTERNAL"),
81  GNUNET_PQ_make_execute ("ALTER TABLE gn011dc ALTER key SET STORAGE PLAIN"),
83  };
84  struct GNUNET_PQ_PreparedStatement ps[] = {
85  GNUNET_PQ_make_prepare ("getkt",
86  "SELECT discard_time,type,value,path FROM gn011dc "
87  "WHERE key=$1 AND type=$2 AND discard_time >= $3",
88  3),
89  GNUNET_PQ_make_prepare ("getk",
90  "SELECT discard_time,type,value,path FROM gn011dc "
91  "WHERE key=$1 AND discard_time >= $2",
92  2),
93  GNUNET_PQ_make_prepare ("getex",
94  "SELECT length(value) AS len,oid,key FROM gn011dc"
95  " WHERE discard_time < $1"
96  " ORDER BY discard_time ASC LIMIT 1",
97  1),
98  GNUNET_PQ_make_prepare ("getm",
99  "SELECT length(value) AS len,oid,key FROM gn011dc"
100  " ORDER BY prox ASC, discard_time ASC LIMIT 1",
101  0),
102  GNUNET_PQ_make_prepare ("get_random",
103  "SELECT discard_time,type,value,path,key FROM gn011dc"
104  " WHERE discard_time >= $1"
105  " ORDER BY key ASC LIMIT 1 OFFSET $2",
106  2),
107  GNUNET_PQ_make_prepare ("get_closest",
108  "SELECT discard_time,type,value,path,key FROM gn011dc "
109  "WHERE key>=$1 AND discard_time >= $2 ORDER BY key ASC LIMIT $3",
110  3),
111  GNUNET_PQ_make_prepare ("delrow",
112  "DELETE FROM gn011dc WHERE oid=$1",
113  1),
114  GNUNET_PQ_make_prepare ("put",
115  "INSERT INTO gn011dc (type, prox, discard_time, key, value, path) "
116  "VALUES ($1, $2, $3, $4, $5, $6)",
117  6),
119  };
120 
121  plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg,
122  "datacache-postgres");
123  if (NULL == plugin->dbh)
124  return GNUNET_SYSERR;
125  if (GNUNET_OK !=
127  es))
128  {
129  PQfinish (plugin->dbh);
130  plugin->dbh = NULL;
131  return GNUNET_SYSERR;
132  }
133 
134  if (GNUNET_OK !=
136  ps))
137  {
138  PQfinish (plugin->dbh);
139  plugin->dbh = NULL;
140  return GNUNET_SYSERR;
141  }
142  return GNUNET_OK;
143 }
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_execute(const char *sql)
Create a struct GNUNET_PQ_ExecuteStatement where errors are fatal.
Definition: pq_exec.c:37
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
int GNUNET_PQ_prepare_statements(PGconn *connection, const struct GNUNET_PQ_PreparedStatement *ps)
Request creation of prepared statements ps from Postgres.
Definition: pq_prepare.c:63
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.
PGconn * GNUNET_PQ_connect_with_cfg(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Connect to a postgres database using the configuration option "CONFIG" in section.
Definition: pq_connect.c:112
#define GNUNET_PQ_PREPARED_STATEMENT_END
Terminator for prepared statement list.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
int GNUNET_PQ_exec_statements(PGconn *connection, const struct GNUNET_PQ_ExecuteStatement *es)
Request execution of an array of statements es from Postgres.
Definition: pq_exec.c:77
PGconn * dbh
Native Postgres database handle.
#define GNUNET_PQ_EXECUTE_STATEMENT_END
Terminator for executable statement list.
struct GNUNET_PQ_ExecuteStatement GNUNET_PQ_make_try_execute(const char *sql)
Create a struct GNUNET_PQ_ExecuteStatement where errors should be tolerated.
Definition: pq_exec.c:56
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare(const char *name, const char *sql, unsigned int num_args)
Create a struct GNUNET_PQ_PreparedStatement.
Definition: pq_prepare.c:39
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,
const struct GNUNET_HashCode key,
uint32_t  prox,
size_t  data_size,
const char *  data,
enum GNUNET_BLOCK_Type  type,
struct GNUNET_TIME_Absolute  discard_time,
unsigned int  path_info_len,
const struct GNUNET_PeerIdentity path_info 
)
static

Store an item in the datastore.

Parameters
clsclosure (our struct Plugin)
keykey to store data under
proxproximity of key to my PID
data_sizenumber of bytes in data
datadata to store
typetype of the value
discard_timewhen to discard the value in any case
path_info_lennumber of entries in path_info
path_infoa path through the network
Returns
0 if duplicate, -1 on error, number of bytes used otherwise

Definition at line 161 of file plugin_datacache_postgres.c.

References Plugin::dbh, 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_uint32(), Plugin::num_items, OVERHEAD, plugin, and ret.

Referenced by libgnunet_plugin_datacache_postgres_init().

170 {
171  struct Plugin *plugin = cls;
172  uint32_t type32 = (uint32_t) type;
173  struct GNUNET_PQ_QueryParam params[] = {
176  GNUNET_PQ_query_param_absolute_time (&discard_time),
180  path_info_len * sizeof (struct GNUNET_PeerIdentity)),
182  };
184 
186  "put",
187  params);
188  if (0 > ret)
189  return -1;
190  plugin->num_items++;
191  return data_size + OVERHEAD;
192 }
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(PGconn *connection, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connnection using the given para...
Definition: pq_eval.c:151
static int ret
Final status code.
Definition: gnunet-arm.c:89
#define OVERHEAD
Per-entry overhead estimate.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
#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_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
The identity of the host (wraps the signing key of the peer).
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
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.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
unsigned int num_items
Number of key-value pairs in the database.
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
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_resultthe number of results in result

Definition at line 228 of file plugin_datacache_postgres.c.

References data, data_size, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_PQ_cleanup_result(), GNUNET_PQ_extract_result(), GNUNET_PQ_result_spec_absolute_time(), 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, HandleResultContext::key, LOG, and type.

Referenced by postgres_plugin_get().

231 {
232  struct HandleResultContext *hrc = cls;
233 
234  for (unsigned int i=0;i<num_results;i++)
235  {
236  struct GNUNET_TIME_Absolute expiration_time;
237  uint32_t type;
238  void *data;
239  size_t data_size;
240  struct GNUNET_PeerIdentity *path;
241  size_t path_len;
242  struct GNUNET_PQ_ResultSpec rs[] = {
243  GNUNET_PQ_result_spec_absolute_time ("discard_time",
244  &expiration_time),
246  &type),
248  &data,
249  &data_size),
251  (void **) &path,
252  &path_len),
254  };
255 
256  if (GNUNET_YES !=
258  rs,
259  i))
260  {
261  GNUNET_break (0);
262  return;
263  }
264  if (0 != (path_len % sizeof (struct GNUNET_PeerIdentity)))
265  {
266  GNUNET_break (0);
267  path_len = 0;
268  }
269  path_len %= sizeof (struct GNUNET_PeerIdentity);
271  "Found result of size %u bytes and type %u in database\n",
272  (unsigned int) data_size,
273  (unsigned int) type);
274  if ( (NULL != hrc->iter) &&
275  (GNUNET_SYSERR ==
276  hrc->iter (hrc->iter_cls,
277  hrc->key,
278  data_size,
279  data,
280  (enum GNUNET_BLOCK_Type) type,
281  expiration_time,
282  path_len,
283  path)) )
284  {
286  "Ending iteration (client error)\n");
288  return;
289  }
291  }
292 }
void * iter_cls
Closure for iter.
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
#define LOG(kind,...)
Description of a DB result cell.
Closure for handle_results.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size(const char *name, void **dst, size_t *sptr)
Variable-size result expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time(const char *name, struct GNUNET_TIME_Absolute *at)
Absolute time expected.
static int result
Global testing status.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
GNUNET_DATACACHE_Iterator iter
Function to call on each result, may be NULL.
The identity of the host (wraps the signing key of the peer).
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:80
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
const struct GNUNET_HashCode * key
Key used.
int 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:144
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:121
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 307 of file plugin_datacache_postgres.c.

References Plugin::dbh, 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(), Plugin::iter, HandleResultContext::iter, Plugin::iter_cls, HandleResultContext::iter_cls, Plugin::key, HandleResultContext::key, plugin, and res.

Referenced by libgnunet_plugin_datacache_postgres_init().

312 {
313  struct Plugin *plugin = cls;
314  uint32_t type32 = (uint32_t) type;
315  struct GNUNET_TIME_Absolute now;
316  struct GNUNET_PQ_QueryParam paramk[] = {
320  };
321  struct GNUNET_PQ_QueryParam paramkt[] = {
326  };
328  struct HandleResultContext hr_ctx;
329 
330  now = GNUNET_TIME_absolute_get ();
331  hr_ctx.iter = iter;
332  hr_ctx.iter_cls = iter_cls;
333  hr_ctx.key = key;
335  (0 == type) ? "getk" : "getkt",
336  (0 == type) ? paramk : paramkt,
338  &hr_ctx);
339  if (res < 0)
340  return 0;
341  return res;
342 }
void * iter_cls
Closure for iter.
Closure for handle_results.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
#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_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
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 int res
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
struct GNUNET_HashCode key
The key used in the DHT.
GNUNET_DATACACHE_Iterator iter
Function to call on each result, may be NULL.
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(PGconn *connection, 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:194
Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_del()

static int 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 353 of file plugin_datacache_postgres.c.

References GNUNET_DATACACHE_PluginEnvironment::cls, Plugin::dbh, GNUNET_DATACACHE_PluginEnvironment::delete_notify, Plugin::env, 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_uint32(), GNUNET_PQ_result_spec_auto_from_type, GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_uint32(), GNUNET_SYSERR, GNUNET_TIME_absolute_get(), LOG, Plugin::num_items, OVERHEAD, plugin, res, and Plugin::size.

Referenced by libgnunet_plugin_datacache_postgres_init().

354 {
355  struct Plugin *plugin = cls;
356  struct GNUNET_PQ_QueryParam pempty[] = {
358  };
359  uint32_t size;
360  uint32_t oid;
361  struct GNUNET_HashCode key;
362  struct GNUNET_PQ_ResultSpec rs[] = {
364  &size),
366  &oid),
368  &key),
370  };
372  struct GNUNET_PQ_QueryParam dparam[] = {
375  };
376  struct GNUNET_TIME_Absolute now;
377  struct GNUNET_PQ_QueryParam xparam[] = {
380  };
381 
382  now = GNUNET_TIME_absolute_get ();
384  "getex",
385  xparam,
386  rs);
387  if (0 >= res)
389  "getm",
390  pempty,
391  rs);
392  if (0 > res)
393  return GNUNET_SYSERR;
395  {
396  /* no result */
398  "Ending iteration (no more results)\n");
399  return 0;
400  }
402  "delrow",
403  dparam);
404  if (0 > res)
405  {
407  return GNUNET_SYSERR;
408  }
409  plugin->num_items--;
410  plugin->env->delete_notify (plugin->env->cls,
411  &key,
412  size + OVERHEAD);
414  return GNUNET_OK;
415 }
#define LOG(kind,...)
Description of a DB result cell.
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_OK
Named constants for return values.
Definition: gnunet_common.h:78
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(PGconn *connection, 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:241
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(PGconn *connection, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connnection using the given para...
Definition: pq_eval.c:151
GNUNET_DATACACHE_DeleteNotifyCallback delete_notify
Function to call whenever the plugin needs to discard content that it was asked to store...
#define OVERHEAD
Per-entry overhead estimate.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
A 512-bit hashcode.
static int res
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
#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_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
void * cls
Closure to use for callbacks.
Time for absolute times used by GNUnet, in microseconds.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
unsigned int num_items
Number of key-value pairs in the database.
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:53
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:121
Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_get_random()

static unsigned int postgres_plugin_get_random ( void *  cls,
GNUNET_DATACACHE_Iterator  iter,
void *  iter_cls 
)
static

Obtain a random key-value pair from the datacache.

Parameters
clsclosure (our struct Plugin)
itermaybe NULL (to just count)
iter_clsclosure for iter
Returns
the number of results found, zero (datacache empty) or one

Definition at line 427 of file plugin_datacache_postgres.c.

References data, data_size, Plugin::dbh, GNUNET_break, GNUNET_CRYPTO_QUALITY_NONCE, GNUNET_CRYPTO_random_u32(), GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_ERROR_TYPE_DEBUG, GNUNET_h2s(), GNUNET_PQ_cleanup_result(), GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_query_param_absolute_time(), GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_uint32(), 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_TIME_absolute_get(), LOG, Plugin::num_items, plugin, res, and type.

Referenced by libgnunet_plugin_datacache_postgres_init().

430 {
431  struct Plugin *plugin = cls;
432  uint32_t off;
433  struct GNUNET_TIME_Absolute now;
434  struct GNUNET_TIME_Absolute expiration_time;
435  size_t data_size;
436  void *data;
437  size_t path_len;
438  struct GNUNET_PeerIdentity *path;
439  struct GNUNET_HashCode key;
440  uint32_t type;
442  struct GNUNET_PQ_QueryParam params[] = {
446  };
447  struct GNUNET_PQ_ResultSpec rs[] = {
448  GNUNET_PQ_result_spec_absolute_time ("discard_time",
449  &expiration_time),
451  &type),
453  &data,
454  &data_size),
456  (void **) &path,
457  &path_len),
459  &key),
461  };
462 
463  if (0 == plugin->num_items)
464  return 0;
465  if (NULL == iter)
466  return 1;
467  now = GNUNET_TIME_absolute_get ();
469  plugin->num_items);
471  "get_random",
472  params,
473  rs);
474  if (0 > res)
475  {
476  GNUNET_break (0);
477  return 0;
478  }
480  {
481  GNUNET_break (0);
482  return 0;
483  }
484  if (0 != (path_len % sizeof (struct GNUNET_PeerIdentity)))
485  {
486  GNUNET_break (0);
487  path_len = 0;
488  }
489  path_len %= sizeof (struct GNUNET_PeerIdentity);
491  "Found random value with key %s of size %u bytes and type %u in database\n",
492  GNUNET_h2s (&key),
493  (unsigned int) data_size,
494  (unsigned int) type);
495  (void) iter (iter_cls,
496  &key,
497  data_size,
498  data,
499  (enum GNUNET_BLOCK_Type) type,
500  expiration_time,
501  path_len,
502  path);
504  return 1;
505 }
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
#define LOG(kind,...)
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
Description of a DB result cell.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(PGconn *connection, 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:241
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size(const char *name, void **dst, size_t *sptr)
Variable-size result expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time(const char *name, struct GNUNET_TIME_Absolute *at)
Absolute time expected.
Randomness for IVs etc.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
A 512-bit hashcode.
static int res
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
#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_HashCode key
The key used in the DHT.
The identity of the host (wraps the signing key of the peer).
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
unsigned int num_items
Number of key-value pairs in the database.
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:53
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
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:121
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_resultthe number of results in result

Definition at line 536 of file plugin_datacache_postgres.c.

References data, data_size, 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, LOG, and type.

Referenced by postgres_plugin_get_closest().

539 {
540  struct ExtractResultContext *erc = cls;
541 
542  if (NULL == erc->iter)
543  return;
544  for (unsigned int i=0;i<num_results;i++)
545  {
546  struct GNUNET_TIME_Absolute expiration_time;
547  uint32_t type;
548  void *data;
549  size_t data_size;
550  struct GNUNET_PeerIdentity *path;
551  size_t path_len;
552  struct GNUNET_HashCode key;
553  struct GNUNET_PQ_ResultSpec rs[] = {
555  &expiration_time),
557  &type),
559  &data,
560  &data_size),
562  (void **) &path,
563  &path_len),
565  &key),
567  };
568 
569  if (GNUNET_YES !=
571  rs,
572  i))
573  {
574  GNUNET_break (0);
575  return;
576  }
577  if (0 != (path_len % sizeof (struct GNUNET_PeerIdentity)))
578  {
579  GNUNET_break (0);
580  path_len = 0;
581  }
582  path_len %= sizeof (struct GNUNET_PeerIdentity);
584  "Found result of size %u bytes and type %u in database\n",
585  (unsigned int) data_size,
586  (unsigned int) type);
587  if (GNUNET_SYSERR ==
588  erc->iter (erc->iter_cls,
589  &key,
590  data_size,
591  data,
592  (enum GNUNET_BLOCK_Type) type,
593  expiration_time,
594  path_len,
595  path))
596  {
598  "Ending iteration (client error)\n");
600  break;
601  }
603  }
604 }
void * iter_cls
Closure for iter.
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
#define LOG(kind,...)
Description of a DB result cell.
Closure for extract_result_cb.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size(const char *name, void **dst, size_t *sptr)
Variable-size result expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time(const char *name, struct GNUNET_TIME_Absolute *at)
Absolute time expected.
static int result
Global testing status.
A 512-bit hashcode.
#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_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
The identity of the host (wraps the signing key of the peer).
GNUNET_DATACACHE_Iterator iter
Function to call for each result found.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
Time for absolute times used by GNUnet, in microseconds.
#define GNUNET_YES
Definition: gnunet_common.h:80
uint32_t data
The data value.
static size_t data_size
Number of bytes in data.
int 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:144
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:121
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,
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
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 621 of file plugin_datacache_postgres.c.

References Plugin::dbh, 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(), Plugin::iter, ExtractResultContext::iter, Plugin::iter_cls, ExtractResultContext::iter_cls, LOG, plugin, and res.

Referenced by libgnunet_plugin_datacache_postgres_init().

626 {
627  struct Plugin *plugin = cls;
628  uint32_t num_results32 = (uint32_t) num_results;
629  struct GNUNET_TIME_Absolute now;
630  struct GNUNET_PQ_QueryParam params[] = {
633  GNUNET_PQ_query_param_uint32 (&num_results32),
635  };
637  struct ExtractResultContext erc;
638 
639  erc.iter = iter;
640  erc.iter_cls = iter_cls;
641  now = GNUNET_TIME_absolute_get ();
643  "get_closest",
644  params,
646  &erc);
647  if (0 > res)
648  {
650  "Ending iteration (postgres error)\n");
651  return 0;
652  }
654  {
655  /* no result */
657  "Ending iteration (no more results)\n");
658  return 0;
659  }
660  return res;
661 }
void * iter_cls
Closure for iter.
#define LOG(kind,...)
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
Closure for extract_result_cb.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
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 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_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
static int res
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
GNUNET_DATACACHE_Iterator iter
Function to call for each result found.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
Time for absolute times used by GNUnet, in microseconds.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:53
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(PGconn *connection, 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:194
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 671 of file plugin_datacache_postgres.c.

References Plugin::api, GNUNET_DATACACHE_PluginEnvironment::cls, GNUNET_DATACACHE_PluginFunctions::cls, GNUNET_DATACACHE_PluginFunctions::del, Plugin::env, GNUNET_DATACACHE_PluginFunctions::get, GNUNET_DATACACHE_PluginFunctions::get_closest, GNUNET_DATACACHE_PluginFunctions::get_random, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_new, GNUNET_OK, init_connection(), LOG, plugin, postgres_plugin_del(), postgres_plugin_get(), postgres_plugin_get_closest(), postgres_plugin_get_random(), postgres_plugin_put(), and GNUNET_DATACACHE_PluginFunctions::put.

672 {
675  struct Plugin *plugin;
676 
677  plugin = GNUNET_new (struct Plugin);
678  plugin->env = env;
679 
680  if (GNUNET_OK != init_connection (plugin))
681  {
682  GNUNET_free (plugin);
683  return NULL;
684  }
685 
687  api->cls = plugin;
688  api->get = &postgres_plugin_get;
689  api->put = &postgres_plugin_put;
690  api->del = &postgres_plugin_del;
694  "Postgres datacache running\n");
695  return api;
696 }
void * cls
Closure to pass to all plugin functions.
static unsigned int postgres_plugin_get_random(void *cls, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Obtain a random key-value pair from the datacache.
ssize_t(* put)(void *cls, const struct GNUNET_HashCode *key, uint32_t xor_distance, size_t size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute discard_time, unsigned int path_info_len, const struct GNUNET_PeerIdentity *path_info)
Store an item in the datastore.
int(* del)(void *cls)
Delete the entry with the lowest expiration value from the datacache right now.
static int postgres_plugin_del(void *cls)
Delete the entry with the lowest expiration value from the datacache right now.
#define LOG(kind,...)
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
static unsigned int postgres_plugin_get_closest(void *cls, const struct GNUNET_HashCode *key, 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 struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.
unsigned int(* get_closest)(void *cls, const struct GNUNET_HashCode *key, 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 ssize_t postgres_plugin_put(void *cls, const struct GNUNET_HashCode *key, uint32_t prox, size_t data_size, const char *data, enum GNUNET_BLOCK_Type type, struct GNUNET_TIME_Absolute discard_time, unsigned int path_info_len, const struct GNUNET_PeerIdentity *path_info)
Store an item in the datastore.
struct returned by the initialization function of the plugin
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
Handle for a plugin.
Definition: block.c:37
void * cls
Closure to use for callbacks.
unsigned int(* get_random)(void *cls, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Return a random value from the datastore.
static int 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.
The datastore service will pass a pointer to a struct of this type as the first and only argument to ...
unsigned int(* 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.
#define GNUNET_free(ptr)
Wrapper around free.
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 706 of file plugin_datacache_postgres.c.

References Plugin::api, GNUNET_DATACACHE_PluginFunctions::cls, Plugin::dbh, GNUNET_free, and plugin.

707 {
709  struct Plugin *plugin = api->cls;
710 
711  PQfinish (plugin->dbh);
712  GNUNET_free (plugin);
713  GNUNET_free (api);
714  return NULL;
715 }
void * cls
Closure to pass to all plugin functions.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
struct returned by the initialization function of the plugin
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
#define GNUNET_free(ptr)
Wrapper around free.