GNUnet  0.11.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 68 of file plugin_datastore_postgres.c.

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

Referenced by libgnunet_plugin_datastore_postgres_init().

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

193 {
194  struct Plugin *plugin = cls;
195  uint64_t total;
196  struct GNUNET_PQ_QueryParam params[] = {
198  };
199  struct GNUNET_PQ_ResultSpec rs[] = {
201  &total),
203  };
205 
206  if (NULL == estimate)
207  return;
209  "estimate_size",
210  params,
211  rs);
213  {
214  *estimate = 0LL;
215  return;
216  }
217  *estimate = total;
218 }
Description of a DB result cell.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#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
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, struct GNUNET_PQ_ResultSpec *rs)
Execute a named prepared statement that is a SELECT statement which must return a single result in co...
Definition: pq_eval.c:258
static char * plugin
Solver plugin name as string.
The transaction succeeded, and yielded one result.
Definition: gnunet_db_lib.h:58
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
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 238 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().

250 {
251  struct Plugin *plugin = cls;
252  struct GNUNET_HashCode vhash;
254 
256  size,
257  &vhash);
258  if (! absent)
259  {
260  struct GNUNET_PQ_QueryParam params[] = {
261  GNUNET_PQ_query_param_uint32 (&priority),
267  };
269  "update",
270  params);
271  if (0 > ret)
272  {
273  cont (cont_cls,
274  key,
275  size,
277  _ ("Postgress exec failure"));
278  return;
279  }
280  bool affected = (0 != ret);
281  if (affected)
282  {
283  cont (cont_cls,
284  key,
285  size,
286  GNUNET_NO,
287  NULL);
288  return;
289  }
290  }
291 
292  {
293  uint32_t utype = (uint32_t) type;
295  UINT64_MAX);
296  struct GNUNET_PQ_QueryParam params[] = {
299  GNUNET_PQ_query_param_uint32 (&priority),
307  };
308 
310  "put",
311  params);
312  if (0 > ret)
313  {
314  cont (cont_cls,
315  key,
316  size,
318  "Postgress exec failure");
319  return;
320  }
321  }
322  plugin->env->duc (plugin->env->cls,
325  "datastore-postgres",
326  "Stored %u bytes in database\n",
327  (unsigned int) size);
328  cont (cont_cls,
329  key,
330  size,
331  GNUNET_OK,
332  NULL);
333 }
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Random on unsigned 64-bit values.
static unsigned int replication
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:78
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:75
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#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:48
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)? ...
static char * plugin
Solver plugin name as string.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:67
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connnection using the given para...
Definition: pq_eval.c:164
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * 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:64
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 367 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().

370 {
371  struct ProcessResultContext *prc = cls;
372  struct Plugin *plugin = prc->plugin;
373 
374  if (0 == num_results)
375  {
376  /* no result */
378  "datastore-postgres",
379  "Ending iteration (no more results)\n");
380  prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
382  return;
383  }
384  if (1 != num_results)
385  {
386  GNUNET_break (0);
387  prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
389  return;
390  }
391  /* Technically we don't need the loop here, but nicer in case
392  we ever relax the condition above. */
393  for (unsigned int i = 0; i < num_results; i++)
394  {
395  int iret;
396  uint32_t rowid;
397  uint32_t utype;
398  uint32_t anonymity;
399  uint32_t replication;
400  uint32_t priority;
401  size_t size;
402  void *data;
403  struct GNUNET_TIME_Absolute expiration_time;
404  struct GNUNET_HashCode key;
405  struct GNUNET_PQ_ResultSpec rs[] = {
406  GNUNET_PQ_result_spec_uint32 ("repl", &replication),
407  GNUNET_PQ_result_spec_uint32 ("type", &utype),
408  GNUNET_PQ_result_spec_uint32 ("prio", &priority),
409  GNUNET_PQ_result_spec_uint32 ("anonLevel", &anonymity),
410  GNUNET_PQ_result_spec_absolute_time ("expire", &expiration_time),
412  GNUNET_PQ_result_spec_variable_size ("value", &data, &size),
413  GNUNET_PQ_result_spec_uint32 ("oid", &rowid),
415  };
416 
417  if (GNUNET_OK !=
419  rs,
420  i))
421  {
422  GNUNET_break (0);
423  prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
425  return;
426  }
427 
429  "datastore-postgres",
430  "Found result of size %u bytes and type %u in database\n",
431  (unsigned int) size,
432  (unsigned int) utype);
433  iret = prc->proc (prc->proc_cls,
434  &key,
435  size,
436  data,
437  (enum GNUNET_BLOCK_Type) utype,
438  priority,
439  anonymity,
440  replication,
441  expiration_time,
442  rowid);
443  if (iret == GNUNET_NO)
444  {
445  struct GNUNET_PQ_QueryParam param[] = {
448  };
449 
451  "Processor asked for item %u to be removed.\n",
452  (unsigned int) rowid);
453  if (0 <
455  "delrow",
456  param))
457  {
459  "datastore-postgres",
460  "Deleting %u bytes from database\n",
461  (unsigned int) size);
462  plugin->env->duc (plugin->env->cls,
465  "datastore-postgres",
466  "Deleted %u bytes from database\n",
467  (unsigned int) size);
468  }
469  }
471  } /* for (i) */
472 }
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:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
struct Plugin * plugin
The plugin handle.
#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.
#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
static char * plugin
Solver plugin name as string.
#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
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connnection using the given para...
Definition: pq_eval.c:164
void * proc_cls
Closure for proc.
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
#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:151
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:130
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 489 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().

496 {
497  struct Plugin *plugin = cls;
498  uint32_t utype = type;
499  uint16_t use_rvalue = random;
500  uint16_t use_key = NULL != key;
501  uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type;
502  uint64_t rvalue;
503  struct GNUNET_PQ_QueryParam params[] = {
504  GNUNET_PQ_query_param_uint64 (&next_uid),
506  GNUNET_PQ_query_param_uint16 (&use_rvalue),
508  GNUNET_PQ_query_param_uint16 (&use_key),
510  GNUNET_PQ_query_param_uint16 (&use_type),
512  };
513  struct ProcessResultContext prc;
515 
516  if (random)
517  {
519  UINT64_MAX);
520  next_uid = 0;
521  }
522  else
523  {
524  rvalue = 0;
525  }
526  prc.plugin = plugin;
527  prc.proc = proc;
528  prc.proc_cls = proc_cls;
529 
531  "get",
532  params,
534  &prc);
535  if (0 > res)
536  proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
538 }
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.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
Execute a named prepared statement that is a SELECT statement which may return multiple results in co...
Definition: pq_eval.c:209
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.
#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
static char * plugin
Solver plugin name as string.
struct GNUNET_HashCode key
The key used in the DHT.
void * proc_cls
Closure for proc.
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * 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:64
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
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 554 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().

559 {
560  struct Plugin *plugin = cls;
561  uint32_t utype = type;
562  struct GNUNET_PQ_QueryParam params[] = {
564  GNUNET_PQ_query_param_uint64 (&next_uid),
566  };
567  struct ProcessResultContext prc;
569 
570  prc.plugin = plugin;
571  prc.proc = proc;
572  prc.proc_cls = proc_cls;
574  "select_non_anonymous",
575  params,
577  &prc);
578  if (0 > res)
579  proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
581 }
PluginDatumProcessor proc
Function to call on each result.
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
Execute a named prepared statement that is a SELECT statement which may return multiple results in co...
Definition: pq_eval.c:209
static void process_result(void *cls, PGresult *res, unsigned int num_results)
Function invoked to process the result and call the processor of cls.
#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
static char * plugin
Solver plugin name as string.
void * proc_cls
Closure for proc.
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * 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:64
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
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 628 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().

638 {
639  struct ReplCtx *rc = cls;
640  struct Plugin *plugin = rc->plugin;
641  int ret;
642  uint32_t oid = (uint32_t) uid;
643  struct GNUNET_PQ_QueryParam params[] = {
646  };
647  enum GNUNET_DB_QueryStatus qret;
648 
649  ret = rc->proc (rc->proc_cls,
650  key,
651  size,
652  data,
653  type,
654  priority,
655  anonymity,
656  replication,
657  expiration,
658  uid);
659  if (NULL == key)
660  return ret;
661  qret = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
662  "decrepl",
663  params);
664  if (0 > qret)
665  return GNUNET_SYSERR;
666  return ret;
667 }
void * proc_cls
Closure for proc.
struct Plugin * plugin
Plugin handle.
static unsigned int replication
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#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.
static char * plugin
Solver plugin name as string.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:67
Context for repl_proc() function.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connnection using the given para...
Definition: pq_eval.c:164
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * 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:64
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 682 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().

685 {
686  struct Plugin *plugin = cls;
687  struct GNUNET_PQ_QueryParam params[] = {
689  };
690  struct ReplCtx rc;
691  struct ProcessResultContext prc;
693 
694  rc.plugin = plugin;
695  rc.proc = proc;
696  rc.proc_cls = proc_cls;
697  prc.plugin = plugin;
698  prc.proc = &repl_proc;
699  prc.proc_cls = &rc;
701  "select_replication_order",
702  params,
704  &prc);
705  if (0 > res)
706  proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
708 }
PluginDatumProcessor proc
Function to call on each result.
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
Execute a named prepared statement that is a SELECT statement which may return multiple results in co...
Definition: pq_eval.c:209
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.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
Closure for process_result.
static int res
static char * plugin
Solver plugin name as string.
Context for repl_proc() function.
void * proc_cls
Closure for proc.
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
Definition: gnunet_db_lib.h:34
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 720 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().

723 {
724  struct Plugin *plugin = cls;
725  struct GNUNET_TIME_Absolute now;
726  struct GNUNET_PQ_QueryParam params[] = {
729  };
730  struct ProcessResultContext prc;
731 
732  now = GNUNET_TIME_absolute_get ();
733  prc.plugin = plugin;
734  prc.proc = proc;
735  prc.proc_cls = proc_cls;
737  "select_expiration_order",
738  params,
740  &prc);
741 }
PluginDatumProcessor proc
Function to call on each result.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
Execute a named prepared statement that is a SELECT statement which may return multiple results in co...
Definition: pq_eval.c:209
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.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
Closure for process_result.
static char * plugin
Solver plugin name as string.
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
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
Time for absolute times used by GNUnet, in microseconds.
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 770 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().

773 {
774  struct ProcessKeysContext *pkc = cls;
775 
776  for (unsigned i = 0; i < num_results; i++)
777  {
778  struct GNUNET_HashCode key;
779  struct GNUNET_PQ_ResultSpec rs[] = {
781  &key),
783  };
784 
785  if (GNUNET_OK !=
787  rs,
788  i))
789  {
790  GNUNET_break (0);
791  continue;
792  }
793  pkc->proc (pkc->proc_cls,
794  &key,
795  1);
797  }
798 }
Description of a DB result cell.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#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:151
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:130
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 809 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().

812 {
813  struct Plugin *plugin = cls;
814  struct GNUNET_PQ_QueryParam params[] = {
816  };
817  struct ProcessKeysContext pkc;
818 
819  pkc.proc = proc;
820  pkc.proc_cls = proc_cls;
822  "get_keys",
823  params,
824  &process_keys,
825  &pkc);
826  proc (proc_cls,
827  NULL,
828  0);
829 }
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
Execute a named prepared statement that is a SELECT statement which may return multiple results in co...
Definition: pq_eval.c:209
PluginKeyProcessor proc
Function to call for each key.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
Definition: gnunet_pq_lib.h:96
void * proc_cls
Closure for proc.
static char * plugin
Solver plugin name as string.
Closure for process_keys.
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
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...
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 838 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().

839 {
840  struct Plugin *plugin = cls;
841  struct GNUNET_PQ_ExecuteStatement es[] = {
842  GNUNET_PQ_make_execute ("DROP TABLE gn090"),
844  };
845 
846  if (GNUNET_OK !=
848  es))
850  "postgres",
851  _ ("Failed to drop table from database.\n"));
852 }
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:36
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:75
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
int GNUNET_PQ_exec_statements(struct GNUNET_PQ_Context *db, const struct GNUNET_PQ_ExecuteStatement *es)
Request execution of an array of statements es from Postgres.
Definition: pq_exec.c:76
static char * plugin
Solver plugin name as string.
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * 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 866 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().

872 {
873  struct Plugin *plugin = cls;
875  struct GNUNET_PQ_QueryParam params[] = {
879  };
880 
882  "remove",
883  params);
884  if (0 > ret)
885  {
886  cont (cont_cls,
887  key,
888  size,
890  _ ("Postgress exec failure"));
891  return;
892  }
894  {
895  cont (cont_cls,
896  key,
897  size,
898  GNUNET_NO,
899  NULL);
900  return;
901  }
902  plugin->env->duc (plugin->env->cls,
905  "datastore-postgres",
906  "Deleted %u bytes from database\n",
907  (unsigned int) size);
908  cont (cont_cls,
909  key,
910  size,
911  GNUNET_OK,
912  NULL);
913 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#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)? ...
static char * plugin
Solver plugin name as string.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:67
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connnection using the given para...
Definition: pq_eval.c:164
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
Description of a DB query parameter.
Definition: gnunet_pq_lib.h:64
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 923 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.

924 {
927  struct Plugin *plugin;
928 
929  plugin = GNUNET_new (struct Plugin);
930  plugin->env = env;
931  if (GNUNET_OK != init_connection (plugin))
932  {
933  GNUNET_free (plugin);
934  return NULL;
935  }
937  api->cls = plugin;
939  api->put = &postgres_plugin_put;
945  api->drop = &postgres_plugin_drop;
948  "datastore-postgres",
949  _ ("Postgres database running\n"));
950  return api;
951 }
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:75
#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:181
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.
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 char * plugin
Solver plugin name as string.
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 961 of file plugin_datastore_postgres.c.

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

962 {
964  struct Plugin *plugin = api->cls;
965 
966  GNUNET_PQ_disconnect (plugin->dbh);
967  GNUNET_free (plugin);
968  GNUNET_free (api);
969  return NULL;
970 }
void GNUNET_PQ_disconnect(struct GNUNET_PQ_Context *db)
Disconnect from the database, destroying the prepared statements and releasing other associated resou...
Definition: pq_connect.c:246
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
static char * plugin
Solver plugin name as string.
void * cls
Closure to use for all of the following callbacks (except "next_request").
Handle for a plugin.
Definition: block.c:37
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function: