GNUnet  0.10.x
Data Structures | Macros | Functions
plugin_datastore_postgres.c File Reference

postgres-based datastore backend More...

#include "platform.h"
#include "gnunet_datastore_plugin.h"
#include "gnunet_pq_lib.h"
Include dependency graph for plugin_datastore_postgres.c:

Go to the source code of this file.

Data Structures

struct  Plugin
 Handle for a plugin. More...
 
struct  ProcessResultContext
 Closure for process_result. More...
 
struct  ReplCtx
 Context for repl_proc() function. More...
 
struct  ProcessKeysContext
 Closure for process_keys. More...
 

Macros

#define BUSY_TIMEOUT   GNUNET_TIME_UNIT_SECONDS
 After how many ms "busy" should a DB operation fail for good? A low value makes sure that we are more responsive to requests (especially PUTs). More...
 
#define RESULT_COLUMNS   "repl, type, prio, anonLevel, expire, hash, value, oid"
 

Functions

static int init_connection (struct Plugin *plugin)
 Get a database handle. More...
 
static void postgres_plugin_estimate_size (void *cls, unsigned long long *estimate)
 Get an estimate of how much space the database is currently using. More...
 
static void postgres_plugin_put (void *cls, const struct GNUNET_HashCode *key, bool absent, uint32_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, PluginPutCont cont, void *cont_cls)
 Store an item in the datastore. More...
 
static void process_result (void *cls, PGresult *res, unsigned int num_results)
 Function invoked to process the result and call the processor of cls. More...
 
static void postgres_plugin_get_key (void *cls, uint64_t next_uid, bool random, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc, void *proc_cls)
 Get one of the results for a particular key in the datastore. More...
 
static void postgres_plugin_get_zero_anonymity (void *cls, uint64_t next_uid, enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc, void *proc_cls)
 Select a subset of the items in the datastore and call the given iterator for each of them. More...
 
static int repl_proc (void *cls, const struct GNUNET_HashCode *key, uint32_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid)
 Wrapper for the iterator for 'sqlite_plugin_replication_get'. More...
 
static void postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc, void *proc_cls)
 Get a random item for replication. More...
 
static void postgres_plugin_get_expiration (void *cls, PluginDatumProcessor proc, void *proc_cls)
 Get a random item for expiration. More...
 
static void process_keys (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 void postgres_plugin_get_keys (void *cls, PluginKeyProcessor proc, void *proc_cls)
 Get all of the keys in the datastore. More...
 
static void postgres_plugin_drop (void *cls)
 Drop database. More...
 
static void postgres_plugin_remove_key (void *cls, const struct GNUNET_HashCode *key, uint32_t size, const void *data, PluginRemoveCont cont, void *cont_cls)
 Remove a particular key in the datastore. More...
 
void * libgnunet_plugin_datastore_postgres_init (void *cls)
 Entry point for the plugin. More...
 
void * libgnunet_plugin_datastore_postgres_done (void *cls)
 Exit point from the plugin. More...
 

Detailed Description

postgres-based datastore backend

Author
Christian Grothoff

Definition in file plugin_datastore_postgres.c.

Macro Definition Documentation

◆ BUSY_TIMEOUT

#define BUSY_TIMEOUT   GNUNET_TIME_UNIT_SECONDS

After how many ms "busy" should a DB operation fail for good? A low value makes sure that we are more responsive to requests (especially PUTs).

A high value guarantees a higher success rate (SELECTs in iterate can take several seconds despite LIMIT=1).

The default value of 1s should ensure that users do not experience huge latencies while at the same time allowing operations to succeed with reasonable probability.

Definition at line 41 of file plugin_datastore_postgres.c.

◆ RESULT_COLUMNS

#define RESULT_COLUMNS   "repl, type, prio, anonLevel, expire, hash, value, oid"

Referenced by init_connection().

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 69 of file plugin_datastore_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, GNUNET_SYSERR, and RESULT_COLUMNS.

Referenced by libgnunet_plugin_datastore_postgres_init().

70 {
71  struct GNUNET_PQ_ExecuteStatement es[] = {
72  /* FIXME: PostgreSQL does not have unsigned integers! This is ok for the type column because
73  * we only test equality on it and can cast it to/from uint32_t. For repl, prio, and anonLevel
74  * we do math or inequality tests, so we can't handle the entire range of uint32_t.
75  * This will also cause problems for expiration times after 294247-01-10-04:00:54 UTC.
76  * PostgreSQL also recommends against using WITH OIDS.
77  */
78  GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS gn090 ("
79  " repl INTEGER NOT NULL DEFAULT 0,"
80  " type INTEGER NOT NULL DEFAULT 0,"
81  " prio INTEGER NOT NULL DEFAULT 0,"
82  " anonLevel INTEGER NOT NULL DEFAULT 0,"
83  " expire BIGINT NOT NULL DEFAULT 0,"
84  " rvalue BIGINT NOT NULL DEFAULT 0,"
85  " hash BYTEA NOT NULL DEFAULT '',"
86  " vhash BYTEA NOT NULL DEFAULT '',"
87  " value BYTEA NOT NULL DEFAULT '')"
88  "WITH OIDS"),
89  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)"),
90  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_prio ON gn090 (prio)"),
91  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_expire ON gn090 (expire)"),
92  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_prio_anon ON gn090 (prio,anonLevel)"),
93  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_prio_hash_anon ON gn090 (prio,hash,anonLevel)"),
94  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_repl_rvalue ON gn090 (repl,rvalue)"),
95  GNUNET_PQ_make_try_execute ("CREATE INDEX IF NOT EXISTS idx_expire_hash ON gn090 (expire,hash)"),
96  GNUNET_PQ_make_execute ("ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL"),
97  GNUNET_PQ_make_execute ("ALTER TABLE gn090 ALTER hash SET STORAGE PLAIN"),
98  GNUNET_PQ_make_execute ("ALTER TABLE gn090 ALTER vhash SET STORAGE PLAIN"),
100  };
101 #define RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, oid"
102  struct GNUNET_PQ_PreparedStatement ps[] = {
103  GNUNET_PQ_make_prepare ("get",
104  "SELECT " RESULT_COLUMNS " FROM gn090"
105  " WHERE oid >= $1::bigint AND"
106  " (rvalue >= $2 OR 0 = $3::smallint) AND"
107  " (hash = $4 OR 0 = $5::smallint) AND"
108  " (type = $6 OR 0 = $7::smallint)"
109  " ORDER BY oid ASC LIMIT 1",
110  7),
111  GNUNET_PQ_make_prepare ("put",
112  "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
113  "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
114  9),
115  GNUNET_PQ_make_prepare ("update",
116  "UPDATE gn090"
117  " SET prio = prio + $1,"
118  " repl = repl + $2,"
119  " expire = GREATEST(expire, $3)"
120  " WHERE hash = $4 AND vhash = $5",
121  5),
122  GNUNET_PQ_make_prepare ("decrepl",
123  "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
124  "WHERE oid = $1",
125  1),
126  GNUNET_PQ_make_prepare ("select_non_anonymous",
127  "SELECT " RESULT_COLUMNS " FROM gn090 "
128  "WHERE anonLevel = 0 AND type = $1 AND oid >= $2::bigint "
129  "ORDER BY oid ASC LIMIT 1",
130  2),
131  GNUNET_PQ_make_prepare ("select_expiration_order",
132  "(SELECT " RESULT_COLUMNS " FROM gn090 "
133  "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) "
134  "UNION "
135  "(SELECT " RESULT_COLUMNS " FROM gn090 "
136  "ORDER BY prio ASC LIMIT 1) "
137  "ORDER BY expire ASC LIMIT 1",
138  1),
139  GNUNET_PQ_make_prepare ("select_replication_order",
140  "SELECT " RESULT_COLUMNS " FROM gn090 "
141  "ORDER BY repl DESC,RANDOM() LIMIT 1",
142  0),
143  GNUNET_PQ_make_prepare ("delrow",
144  "DELETE FROM gn090 "
145  "WHERE oid=$1",
146  1),
147  GNUNET_PQ_make_prepare ("remove",
148  "DELETE FROM gn090"
149  " WHERE hash = $1 AND"
150  " value = $2",
151  2),
152  GNUNET_PQ_make_prepare ("get_keys",
153  "SELECT hash FROM gn090",
154  0),
155  GNUNET_PQ_make_prepare ("estimate_size",
156  "SELECT CASE WHEN NOT EXISTS"
157  " (SELECT 1 FROM gn090)"
158  " THEN 0"
159  " ELSE (SELECT SUM(LENGTH(value))+256*COUNT(*) FROM gn090)"
160  "END AS total",
161  0),
163  };
164 #undef RESULT_COLUMNS
165 
166  plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg,
167  "datastore-postgres");
168  if (NULL == plugin->dbh)
169  return GNUNET_SYSERR;
170 
171  if ( (GNUNET_OK !=
173  es)) ||
174  (GNUNET_OK !=
176  ps)) )
177  {
178  PQfinish (plugin->dbh);
179  plugin->dbh = NULL;
180  return GNUNET_SYSERR;
181  }
182  return GNUNET_OK;
183 }
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
#define RESULT_COLUMNS
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_estimate_size()

static void postgres_plugin_estimate_size ( void *  cls,
unsigned long long *  estimate 
)
static

Get an estimate of how much space the database is currently using.

Parameters
clsour struct Plugin *
Returns
number of bytes used on disk

Definition at line 194 of file plugin_datastore_postgres.c.

References Plugin::dbh, GNUNET_DB_STATUS_SUCCESS_ONE_RESULT, GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_query_param_end, GNUNET_PQ_result_spec_end, GNUNET_PQ_result_spec_uint64(), plugin, and ret.

Referenced by libgnunet_plugin_datastore_postgres_init().

196 {
197  struct Plugin *plugin = cls;
198  uint64_t total;
199  struct GNUNET_PQ_QueryParam params[] = {
201  };
202  struct GNUNET_PQ_ResultSpec rs[] = {
204  &total),
206  };
208 
209  if (NULL == estimate)
210  return;
212  "estimate_size",
213  params,
214  rs);
216  {
217  *estimate = 0LL;
218  return;
219  }
220  *estimate = total;
221 }
Description of a DB result cell.
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
static int ret
Final status code.
Definition: gnunet-arm.c:89
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
The transaction succeeded, and yielded one result.
Definition: gnunet_db_lib.h:58
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
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64(const char *name, uint64_t *u64)
uint64_t expected.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_put()

static void postgres_plugin_put ( void *  cls,
const struct GNUNET_HashCode key,
bool  absent,
uint32_t  size,
const void *  data,
enum GNUNET_BLOCK_Type  type,
uint32_t  priority,
uint32_t  anonymity,
uint32_t  replication,
struct GNUNET_TIME_Absolute  expiration,
PluginPutCont  cont,
void *  cont_cls 
)
static

Store an item in the datastore.

Parameters
clsclosure with the struct Plugin
keykey for the item
absenttrue if the key was not found in the bloom filter
sizenumber of bytes in data
datacontent stored
typetype of the content
prioritypriority of the content
anonymityanonymity-level for the content
replicationreplication-level for the content
expirationexpiration time for the content
contcontinuation called with success or failure status
cont_clscontinuation closure

Definition at line 241 of file plugin_datastore_postgres.c.

References _, GNUNET_DATACACHE_PluginEnvironment::cls, Plugin::dbh, Plugin::env, GNUNET_CRYPTO_hash(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_DATASTORE_ENTRY_OVERHEAD, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log_from, GNUNET_NO, GNUNET_OK, 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(), GNUNET_PQ_query_param_uint64(), GNUNET_SYSERR, plugin, and ret.

Referenced by libgnunet_plugin_datastore_postgres_init().

253 {
254  struct Plugin *plugin = cls;
255  struct GNUNET_HashCode vhash;
257 
259  size,
260  &vhash);
261  if (! absent)
262  {
263  struct GNUNET_PQ_QueryParam params[] = {
264  GNUNET_PQ_query_param_uint32 (&priority),
270  };
272  "update",
273  params);
274  if (0 > ret)
275  {
276  cont (cont_cls,
277  key,
278  size,
280  _("Postgress exec failure"));
281  return;
282  }
283  bool affected = (0 != ret);
284  if (affected)
285  {
286  cont (cont_cls,
287  key,
288  size,
289  GNUNET_NO,
290  NULL);
291  return;
292  }
293  }
294 
295  {
296  uint32_t utype = (uint32_t) type;
298  UINT64_MAX);
299  struct GNUNET_PQ_QueryParam params[] = {
302  GNUNET_PQ_query_param_uint32 (&priority),
310  };
311 
313  "put",
314  params);
315  if (0 > ret)
316  {
317  cont (cont_cls,
318  key,
319  size,
321  "Postgress exec failure");
322  return;
323  }
324  }
325  plugin->env->duc (plugin->env->cls,
328  "datastore-postgres",
329  "Stored %u bytes in database\n",
330  (unsigned int) size);
331  cont (cont_cls,
332  key,
333  size,
334  GNUNET_OK,
335  NULL);
336 }
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Random on unsigned 64-bit values.
static unsigned int replication
#define GNUNET_NO
Definition: gnunet_common.h:81
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_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 _(String)
GNU gettext support macro.
Definition: platform.h:208
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
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.
#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.
A 512-bit hashcode.
#define GNUNET_DATASTORE_ENTRY_OVERHEAD
How many bytes of overhead will we assume per entry in any DB (for reservations)? ...
#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.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
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.
void * cls
Closure to use for callbacks.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
static unsigned int anonymity
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
#define GNUNET_log_from(kind, comp,...)
uint32_t data
The data value.
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ process_result()

static void process_result ( void *  cls,
PGresult *  res,
unsigned int  num_results 
)
static

Function invoked to process the result and call the processor of cls.

Parameters
clsour struct ProcessResultContext
resresult from exec
num_resultsnumber of results in res

Definition at line 372 of file plugin_datastore_postgres.c.

References anonymity, GNUNET_DATACACHE_PluginEnvironment::cls, data, Plugin::dbh, Plugin::env, GNUNET_break, GNUNET_DATASTORE_ENTRY_OVERHEAD, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_log_from, GNUNET_NO, GNUNET_OK, GNUNET_PQ_cleanup_result(), GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_extract_result(), 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_UNIT_ZERO_ABS, plugin, ProcessResultContext::plugin, ProcessResultContext::proc, ProcessResultContext::proc_cls, replication, and Plugin::size.

Referenced by postgres_plugin_get_expiration(), postgres_plugin_get_key(), postgres_plugin_get_replication(), and postgres_plugin_get_zero_anonymity().

375 {
376  struct ProcessResultContext *prc = cls;
377  struct Plugin *plugin = prc->plugin;
378 
379  if (0 == num_results)
380  {
381  /* no result */
383  "datastore-postgres",
384  "Ending iteration (no more results)\n");
385  prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
387  return;
388  }
389  if (1 != num_results)
390  {
391  GNUNET_break (0);
392  prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
394  return;
395  }
396  /* Technically we don't need the loop here, but nicer in case
397  we ever relax the condition above. */
398  for (unsigned int i=0;i<num_results;i++)
399  {
400  int iret;
401  uint32_t rowid;
402  uint32_t utype;
403  uint32_t anonymity;
404  uint32_t replication;
405  uint32_t priority;
406  size_t size;
407  void *data;
408  struct GNUNET_TIME_Absolute expiration_time;
409  struct GNUNET_HashCode key;
410  struct GNUNET_PQ_ResultSpec rs[] = {
411  GNUNET_PQ_result_spec_uint32 ("repl", &replication),
412  GNUNET_PQ_result_spec_uint32 ("type", &utype),
413  GNUNET_PQ_result_spec_uint32 ("prio", &priority),
414  GNUNET_PQ_result_spec_uint32 ("anonLevel", &anonymity),
415  GNUNET_PQ_result_spec_absolute_time ("expire", &expiration_time),
417  GNUNET_PQ_result_spec_variable_size ("value", &data, &size),
418  GNUNET_PQ_result_spec_uint32 ("oid", &rowid),
420  };
421 
422  if (GNUNET_OK !=
424  rs,
425  i))
426  {
427  GNUNET_break (0);
428  prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
430  return;
431  }
432 
434  "datastore-postgres",
435  "Found result of size %u bytes and type %u in database\n",
436  (unsigned int) size,
437  (unsigned int) utype);
438  iret = prc->proc (prc->proc_cls,
439  &key,
440  size,
441  data,
442  (enum GNUNET_BLOCK_Type) utype,
443  priority,
444  anonymity,
445  replication,
446  expiration_time,
447  rowid);
448  if (iret == GNUNET_NO)
449  {
450  struct GNUNET_PQ_QueryParam param[] = {
453  };
454 
456  "Processor asked for item %u to be removed.\n",
457  (unsigned int) rowid);
458  if (0 <
460  "delrow",
461  param))
462  {
464  "datastore-postgres",
465  "Deleting %u bytes from database\n",
466  (unsigned int) size);
467  plugin->env->duc (plugin->env->cls,
470  "datastore-postgres",
471  "Deleted %u bytes from database\n",
472  (unsigned int) size);
473  }
474  }
476  } /* for (i) */
477 }
PluginDatumProcessor proc
Function to call on each result.
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
GNUNET_BLOCK_Type
Blocks in the datastore and the datacache must have a unique type.
static unsigned int replication
Description of a DB result cell.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
struct Plugin * plugin
The plugin handle.
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
#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.
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.
Closure for process_result.
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.
#define GNUNET_DATASTORE_ENTRY_OVERHEAD
How many bytes of overhead will we assume per entry in any DB (for reservations)? ...
static int res
#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.
static unsigned int size
Size of the "table".
Definition: peer.c:67
void * proc_cls
Closure for proc.
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
#define GNUNET_log(kind,...)
void * cls
Closure to use for callbacks.
Time for absolute times used by GNUnet, in microseconds.
static unsigned int anonymity
#define GNUNET_log_from(kind, comp,...)
uint32_t data
The data value.
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_key()

static void postgres_plugin_get_key ( void *  cls,
uint64_t  next_uid,
bool  random,
const struct GNUNET_HashCode key,
enum GNUNET_BLOCK_Type  type,
PluginDatumProcessor  proc,
void *  proc_cls 
)
static

Get one of the results for a particular key in the datastore.

Parameters
clsclosure with the struct Plugin
next_uidreturn the result with lowest uid >= next_uid
randomif true, return a random result instead of using next_uid
keymaybe NULL (to match all entries)
typeentries of which type are relevant? Use 0 for any type.
procfunction to call on the matching value; will be called with NULL if nothing matches
proc_clsclosure for proc

Definition at line 494 of file plugin_datastore_postgres.c.

References Plugin::dbh, GNUNET_BLOCK_TYPE_ANY, GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_uint16(), GNUNET_PQ_query_param_uint32(), GNUNET_PQ_query_param_uint64(), GNUNET_TIME_UNIT_ZERO_ABS, Plugin::key, plugin, ProcessResultContext::plugin, ProcessResultContext::proc, ProcessResultContext::proc_cls, process_result(), res, and type.

Referenced by libgnunet_plugin_datastore_postgres_init().

501 {
502  struct Plugin *plugin = cls;
503  uint32_t utype = type;
504  uint16_t use_rvalue = random;
505  uint16_t use_key = NULL != key;
506  uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type;
507  uint64_t rvalue;
508  struct GNUNET_PQ_QueryParam params[] = {
509  GNUNET_PQ_query_param_uint64 (&next_uid),
511  GNUNET_PQ_query_param_uint16 (&use_rvalue),
513  GNUNET_PQ_query_param_uint16 (&use_key),
515  GNUNET_PQ_query_param_uint16 (&use_type),
517  };
518  struct ProcessResultContext prc;
520 
521  if (random)
522  {
524  UINT64_MAX);
525  next_uid = 0;
526  }
527  else
528  {
529  rvalue = 0;
530  }
531  prc.plugin = plugin;
532  prc.proc = proc;
533  prc.proc_cls = proc_cls;
534 
536  "get",
537  params,
539  &prc);
540  if (0 > res)
541  proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
543 }
PluginDatumProcessor proc
Function to call on each result.
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
Any type of block, used as a wildcard when searching.
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Random on unsigned 64-bit values.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint16(const uint16_t *x)
Generate query parameter for an uint16_t in host byte order.
static void process_result(void *cls, PGresult *res, unsigned int num_results)
Function invoked to process the result and call the processor of cls.
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.
Closure for process_result.
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_HashCode key
The key used in the DHT.
void * proc_cls
Closure for proc.
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
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
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
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_get_zero_anonymity()

static void postgres_plugin_get_zero_anonymity ( void *  cls,
uint64_t  next_uid,
enum GNUNET_BLOCK_Type  type,
PluginDatumProcessor  proc,
void *  proc_cls 
)
static

Select a subset of the items in the datastore and call the given iterator for each of them.

Parameters
clsour struct Plugin *
next_uidreturn the result with lowest uid >= next_uid
typeentries of which type should be considered? Must not be zero (ANY).
procfunction to call on the matching value; will be called with NULL if no value matches
proc_clsclosure for proc

Definition at line 559 of file plugin_datastore_postgres.c.

References Plugin::dbh, GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_uint32(), GNUNET_PQ_query_param_uint64(), GNUNET_TIME_UNIT_ZERO_ABS, plugin, ProcessResultContext::plugin, ProcessResultContext::proc, ProcessResultContext::proc_cls, process_result(), res, and type.

Referenced by libgnunet_plugin_datastore_postgres_init().

564 {
565  struct Plugin *plugin = cls;
566  uint32_t utype = type;
567  struct GNUNET_PQ_QueryParam params[] = {
569  GNUNET_PQ_query_param_uint64 (&next_uid),
571  };
572  struct ProcessResultContext prc;
574 
575  prc.plugin = plugin;
576  prc.proc = proc;
577  prc.proc_cls = proc_cls;
579  "select_non_anonymous",
580  params,
582  &prc);
583  if (0 > res)
584  proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
586 }
PluginDatumProcessor proc
Function to call on each result.
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
static void process_result(void *cls, PGresult *res, unsigned int num_results)
Function invoked to process the result and call the processor of cls.
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
Closure for process_result.
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
void * proc_cls
Closure for proc.
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
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
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:

◆ repl_proc()

static int repl_proc ( void *  cls,
const struct GNUNET_HashCode key,
uint32_t  size,
const void *  data,
enum GNUNET_BLOCK_Type  type,
uint32_t  priority,
uint32_t  anonymity,
uint32_t  replication,
struct GNUNET_TIME_Absolute  expiration,
uint64_t  uid 
)
static

Wrapper for the iterator for 'sqlite_plugin_replication_get'.

Decrements the replication counter and calls the original iterator.

Parameters
clsclosure with the struct ReplCtx *
keykey for the content
sizenumber of bytes in data
datacontent stored
typetype of the content
prioritypriority of the content
anonymityanonymity-level for the content
replicationreplication-level for the content
expirationexpiration time for the content
uidunique identifier for the datum; maybe 0 if no unique identifier is available
Returns
GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue (continue on call to "next", of course), GNUNET_NO to delete the item and continue (if supported)

Definition at line 634 of file plugin_datastore_postgres.c.

References Plugin::dbh, GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_uint32(), GNUNET_SYSERR, plugin, ReplCtx::plugin, ReplCtx::proc, ReplCtx::proc_cls, and ret.

Referenced by postgres_plugin_get_replication().

644 {
645  struct ReplCtx *rc = cls;
646  struct Plugin *plugin = rc->plugin;
647  int ret;
648  uint32_t oid = (uint32_t) uid;
649  struct GNUNET_PQ_QueryParam params[] = {
652  };
653  enum GNUNET_DB_QueryStatus qret;
654 
655  ret = rc->proc (rc->proc_cls,
656  key,
657  size,
658  data,
659  type,
660  priority,
661  anonymity,
662  replication,
663  expiration,
664  uid);
665  if (NULL == key)
666  return ret;
667  qret = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
668  "decrepl",
669  params);
670  if (0 > qret)
671  return GNUNET_SYSERR;
672  return ret;
673 }
void * proc_cls
Closure for proc.
struct Plugin * plugin
Plugin handle.
static unsigned int replication
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
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
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
static unsigned int size
Size of the "table".
Definition: peer.c:67
Context for repl_proc() function.
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
PluginDatumProcessor proc
Function to call for the result (or the NULL).
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:63
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model
static unsigned int anonymity
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
uint32_t data
The data value.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_get_replication()

static void postgres_plugin_get_replication ( void *  cls,
PluginDatumProcessor  proc,
void *  proc_cls 
)
static

Get a random item for replication.

Returns a single, not expired, random item from those with the highest replication counters. The item's replication counter is decremented by one IF it was positive before. Call proc with all values ZERO or NULL if the datastore is empty.

Parameters
clsclosure with the struct Plugin
procfunction to call the value (once only).
proc_clsclosure for proc

Definition at line 688 of file plugin_datastore_postgres.c.

References Plugin::dbh, GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_end, GNUNET_TIME_UNIT_ZERO_ABS, plugin, ProcessResultContext::plugin, ReplCtx::plugin, ProcessResultContext::proc, ReplCtx::proc, ProcessResultContext::proc_cls, ReplCtx::proc_cls, process_result(), repl_proc(), and res.

Referenced by libgnunet_plugin_datastore_postgres_init().

691 {
692  struct Plugin *plugin = cls;
693  struct GNUNET_PQ_QueryParam params[] = {
695  };
696  struct ReplCtx rc;
697  struct ProcessResultContext prc;
699 
700  rc.plugin = plugin;
701  rc.proc = proc;
702  rc.proc_cls = proc_cls;
703  prc.plugin = plugin;
704  prc.proc = &repl_proc;
705  prc.proc_cls = &rc;
707  "select_replication_order",
708  params,
710  &prc);
711  if (0 > res)
712  proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
714 }
PluginDatumProcessor proc
Function to call on each result.
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
static int repl_proc(void *cls, const struct GNUNET_HashCode *key, uint32_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid)
Wrapper for the iterator for &#39;sqlite_plugin_replication_get&#39;.
static void process_result(void *cls, PGresult *res, unsigned int num_results)
Function invoked to process the result and call the processor of cls.
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
Closure for process_result.
static int res
Context for repl_proc() function.
void * proc_cls
Closure for proc.
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
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_get_expiration()

static void postgres_plugin_get_expiration ( void *  cls,
PluginDatumProcessor  proc,
void *  proc_cls 
)
static

Get a random item for expiration.

Call proc with all values ZERO or NULL if the datastore is empty.

Parameters
clsclosure with the struct Plugin
procfunction to call the value (once only).
proc_clsclosure for proc

Definition at line 726 of file plugin_datastore_postgres.c.

References Plugin::dbh, GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_absolute_time(), GNUNET_PQ_query_param_end, GNUNET_TIME_absolute_get(), plugin, ProcessResultContext::plugin, ProcessResultContext::proc, ProcessResultContext::proc_cls, and process_result().

Referenced by libgnunet_plugin_datastore_postgres_init().

729 {
730  struct Plugin *plugin = cls;
731  struct GNUNET_TIME_Absolute now;
732  struct GNUNET_PQ_QueryParam params[] = {
735  };
736  struct ProcessResultContext prc;
737 
738  now = GNUNET_TIME_absolute_get ();
739  prc.plugin = plugin;
740  prc.proc = proc;
741  prc.proc_cls = proc_cls;
743  "select_expiration_order",
744  params,
746  &prc);
747 }
PluginDatumProcessor proc
Function to call on each result.
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 void process_result(void *cls, PGresult *res, unsigned int num_results)
Function invoked to process the result and call the processor of cls.
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
Closure for process_result.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:118
void * proc_cls
Closure for proc.
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
Time for absolute times used by GNUnet, in microseconds.
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:

◆ process_keys()

static void process_keys ( 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.

Parameters
clsclosure with a struct ProcessKeysContext
resultthe postgres result
num_resultthe number of results in result

Definition at line 777 of file plugin_datastore_postgres.c.

References GNUNET_break, GNUNET_OK, GNUNET_PQ_cleanup_result(), GNUNET_PQ_extract_result(), GNUNET_PQ_result_spec_auto_from_type, GNUNET_PQ_result_spec_end, ProcessKeysContext::proc, and ProcessKeysContext::proc_cls.

Referenced by postgres_plugin_get_keys().

780 {
781  struct ProcessKeysContext *pkc = cls;
782 
783  for (unsigned i=0;i<num_results;i++)
784  {
785  struct GNUNET_HashCode key;
786  struct GNUNET_PQ_ResultSpec rs[] = {
788  &key),
790  };
791 
792  if (GNUNET_OK !=
794  rs,
795  i))
796  {
797  GNUNET_break (0);
798  continue;
799  }
800  pkc->proc (pkc->proc_cls,
801  &key,
802  1);
804  }
805 }
Description of a DB result cell.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
PluginKeyProcessor proc
Function to call for each key.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
void * proc_cls
Closure for proc.
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.
Closure for process_keys.
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_keys()

static void postgres_plugin_get_keys ( void *  cls,
PluginKeyProcessor  proc,
void *  proc_cls 
)
static

Get all of the keys in the datastore.

Parameters
clsclosure with the struct Plugin *
procfunction to call on each key
proc_clsclosure for proc

Definition at line 816 of file plugin_datastore_postgres.c.

References Plugin::dbh, GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_query_param_end, plugin, ProcessKeysContext::proc, ProcessKeysContext::proc_cls, and process_keys().

Referenced by libgnunet_plugin_datastore_postgres_init().

819 {
820  struct Plugin *plugin = cls;
821  struct GNUNET_PQ_QueryParam params[] = {
823  };
824  struct ProcessKeysContext pkc;
825 
826  pkc.proc = proc;
827  pkc.proc_cls = proc_cls;
829  "get_keys",
830  params,
831  &process_keys,
832  &pkc);
833  proc (proc_cls,
834  NULL,
835  0);
836 }
PluginKeyProcessor proc
Function to call for each key.
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
void * proc_cls
Closure for proc.
Closure for process_keys.
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
static void process_keys(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...
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_drop()

static void postgres_plugin_drop ( void *  cls)
static

Drop database.

Parameters
clsclosure with the struct Plugin *

Definition at line 845 of file plugin_datastore_postgres.c.

References _, Plugin::dbh, GNUNET_ERROR_TYPE_WARNING, GNUNET_log_from, GNUNET_OK, GNUNET_PQ_exec_statements(), GNUNET_PQ_EXECUTE_STATEMENT_END, GNUNET_PQ_make_execute(), and plugin.

Referenced by libgnunet_plugin_datastore_postgres_init().

846 {
847  struct Plugin *plugin = cls;
848  struct GNUNET_PQ_ExecuteStatement es[] = {
849  GNUNET_PQ_make_execute ("DROP TABLE gn090"),
851  };
852 
853  if (GNUNET_OK !=
855  es))
857  "postgres",
858  _("Failed to drop table from database.\n"));
859 }
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
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
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
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
#define GNUNET_PQ_EXECUTE_STATEMENT_END
Terminator for executable statement list.
#define GNUNET_log_from(kind, comp,...)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ postgres_plugin_remove_key()

static void postgres_plugin_remove_key ( void *  cls,
const struct GNUNET_HashCode key,
uint32_t  size,
const void *  data,
PluginRemoveCont  cont,
void *  cont_cls 
)
static

Remove a particular key in the datastore.

Parameters
clsclosure
keykey for the content
sizenumber of bytes in data
datacontent stored
contcontinuation called with success or failure status
cont_clscontinuation closure for cont

Definition at line 873 of file plugin_datastore_postgres.c.

References _, GNUNET_DATACACHE_PluginEnvironment::cls, Plugin::dbh, Plugin::env, GNUNET_DATASTORE_ENTRY_OVERHEAD, GNUNET_DB_STATUS_SUCCESS_NO_RESULTS, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log_from, GNUNET_NO, GNUNET_OK, GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_query_param_auto_from_type, GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_fixed_size(), GNUNET_SYSERR, plugin, and ret.

Referenced by libgnunet_plugin_datastore_postgres_init().

879 {
880  struct Plugin *plugin = cls;
882  struct GNUNET_PQ_QueryParam params[] = {
886  };
887 
889  "remove",
890  params);
891  if (0 > ret)
892  {
893  cont (cont_cls,
894  key,
895  size,
897  _("Postgress exec failure"));
898  return;
899  }
901  {
902  cont (cont_cls,
903  key,
904  size,
905  GNUNET_NO,
906  NULL);
907  return;
908  }
909  plugin->env->duc (plugin->env->cls,
912  "datastore-postgres",
913  "Deleted %u bytes from database\n",
914  (unsigned int) size);
915  cont (cont_cls,
916  key,
917  size,
918  GNUNET_OK,
919  NULL);
920 }
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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 _(String)
GNU gettext support macro.
Definition: platform.h:208
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
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
#define GNUNET_DATASTORE_ENTRY_OVERHEAD
How many bytes of overhead will we assume per entry in any DB (for reservations)? ...
#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
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.
void * cls
Closure to use for callbacks.
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
#define GNUNET_log_from(kind, comp,...)
The transaction succeeded, but yielded zero results.
Definition: gnunet_db_lib.h:53
uint32_t data
The data value.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ libgnunet_plugin_datastore_postgres_init()

void* libgnunet_plugin_datastore_postgres_init ( void *  cls)

Entry point for the plugin.

Parameters
clsthe struct GNUNET_DATASTORE_PluginEnvironment*
Returns
our struct Plugin *

Definition at line 930 of file plugin_datastore_postgres.c.

References _, Plugin::api, GNUNET_DATASTORE_PluginEnvironment::cls, GNUNET_DATASTORE_PluginFunctions::cls, GNUNET_DATASTORE_PluginFunctions::drop, Plugin::env, GNUNET_DATASTORE_PluginFunctions::estimate_size, GNUNET_DATASTORE_PluginFunctions::get_expiration, GNUNET_DATASTORE_PluginFunctions::get_key, GNUNET_DATASTORE_PluginFunctions::get_keys, GNUNET_DATASTORE_PluginFunctions::get_replication, GNUNET_DATASTORE_PluginFunctions::get_zero_anonymity, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_log_from, GNUNET_new, GNUNET_OK, init_connection(), plugin, postgres_plugin_drop(), postgres_plugin_estimate_size(), postgres_plugin_get_expiration(), postgres_plugin_get_key(), postgres_plugin_get_keys(), postgres_plugin_get_replication(), postgres_plugin_get_zero_anonymity(), postgres_plugin_put(), postgres_plugin_remove_key(), GNUNET_DATASTORE_PluginFunctions::put, and GNUNET_DATASTORE_PluginFunctions::remove_key.

931 {
934  struct Plugin *plugin;
935 
936  plugin = GNUNET_new (struct Plugin);
937  plugin->env = env;
938  if (GNUNET_OK != init_connection (plugin))
939  {
940  GNUNET_free (plugin);
941  return NULL;
942  }
944  api->cls = plugin;
946  api->put = &postgres_plugin_put;
952  api->drop = &postgres_plugin_drop;
955  "datastore-postgres",
956  _("Postgres database running\n"));
957  return api;
958 }
PluginGetKeys get_keys
Iterate over all keys in the database.
PluginPut put
Function to store an item in the datastore.
PluginGetType get_zero_anonymity
Get datum (of the specified type) with anonymity level zero.
static void postgres_plugin_get_zero_anonymity(void *cls, uint64_t next_uid, enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc, void *proc_cls)
Select a subset of the items in the datastore and call the given iterator for each of them...
PluginRemoveKey remove_key
Function to remove an item from the database.
PluginEstimateSize estimate_size
Calculate the current on-disk size of the SQ store.
static void postgres_plugin_put(void *cls, const struct GNUNET_HashCode *key, bool absent, uint32_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, PluginPutCont cont, void *cont_cls)
Store an item in the datastore.
#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.
static void postgres_plugin_get_expiration(void *cls, PluginDatumProcessor proc, void *proc_cls)
Get a random item for expiration.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
PluginGetRandom get_replication
Function to get a random item with high replication score from the database, lowering the item&#39;s repl...
PluginDrop drop
Delete the database.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
PluginGetRandom get_expiration
Function to get a random expired item or, if none are expired, either the oldest entry or one with a ...
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.
static void postgres_plugin_remove_key(void *cls, const struct GNUNET_HashCode *key, uint32_t size, const void *data, PluginRemoveCont cont, void *cont_cls)
Remove a particular key in the datastore.
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
The datastore service will pass a pointer to a struct of this type as the first and only argument to ...
static void postgres_plugin_drop(void *cls)
Drop database.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static void postgres_plugin_get_replication(void *cls, PluginDatumProcessor proc, void *proc_cls)
Get a random item for replication.
void * cls
Closure to use for all of the following callbacks (except "next_request").
static void postgres_plugin_get_keys(void *cls, PluginKeyProcessor proc, void *proc_cls)
Get all of the keys in the datastore.
Handle for a plugin.
Definition: block.c:37
static int init_connection(struct Plugin *plugin)
Get a database handle.
#define GNUNET_log_from(kind, comp,...)
PluginGetKey get_key
Get a particular datum matching a given hash from the datastore.
static void postgres_plugin_estimate_size(void *cls, unsigned long long *estimate)
Get an estimate of how much space the database is currently using.
static void postgres_plugin_get_key(void *cls, uint64_t next_uid, bool random, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc, void *proc_cls)
Get one of 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_datastore_postgres_done()

void* libgnunet_plugin_datastore_postgres_done ( void *  cls)

Exit point from the plugin.

Parameters
clsour struct Plugin *
Returns
always NULL

Definition at line 968 of file plugin_datastore_postgres.c.

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

969 {
971  struct Plugin *plugin = api->cls;
972 
973  PQfinish (plugin->dbh);
974  GNUNET_free (plugin);
975  GNUNET_free (api);
976  return NULL;
977 }
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
void * cls
Closure to use for all of the following callbacks (except "next_request").
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
#define GNUNET_free(ptr)
Wrapper around free.