GNUnet 0.25.2-11-g84e94e98c
 
Loading...
Searching...
No Matches
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_iter() 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).
 
#define RESULT_COLUMNS   "repl, type, prio, anonLevel, expire, hash, value, oid"
 

Functions

static enum GNUNET_GenericReturnValue init_connection (struct Plugin *plugin)
 Get a database handle.
 
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_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.
 
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 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.
 
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.
 
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'.
 
static void postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc, void *proc_cls)
 Get a random item for replication.
 
static void postgres_plugin_get_expiration (void *cls, PluginDatumProcessor proc, void *proc_cls)
 Get a random item for expiration.
 
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.
 
static void postgres_plugin_get_keys (void *cls, PluginKeyProcessor proc, void *proc_cls)
 Get all of the keys in the datastore.
 
static void postgres_plugin_drop (void *cls)
 Drop database.
 
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.
 
void * libgnunet_plugin_datastore_postgres_init (void *cls)
 Entry point for the plugin.
 
void * libgnunet_plugin_datastore_postgres_done (void *cls)
 Exit point from the plugin.
 

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"

Function Documentation

◆ init_connection()

static enum GNUNET_GenericReturnValue init_connection ( struct Plugin plugin)
static

Get a database handle.

Parameters
pluginglobal context
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 68 of file plugin_datastore_postgres.c.

69{
70#define RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, oid"
73 "SELECT " RESULT_COLUMNS
74 " FROM datastore.gn090"
75 " WHERE oid >= $1::bigint AND"
76 " (rvalue >= $2 OR 0 = $3::smallint) AND"
77 " (hash = $4 OR 0 = $5::smallint) AND"
78 " (type = $6 OR 0 = $7::smallint)"
79 " ORDER BY oid ASC LIMIT 1"),
81 "INSERT INTO datastore.gn090"
82 " (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) "
83 "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"),
84 GNUNET_PQ_make_prepare ("update",
85 "UPDATE datastore.gn090"
86 " SET prio = prio + $1,"
87 " repl = repl + $2,"
88 " expire = GREATEST(expire, $3)"
89 " WHERE hash = $4 AND vhash = $5"),
90 GNUNET_PQ_make_prepare ("decrepl",
91 "UPDATE datastore.gn090"
92 " SET repl = GREATEST (repl - 1, 0)"
93 " WHERE oid = $1"),
94 GNUNET_PQ_make_prepare ("select_non_anonymous",
95 "SELECT " RESULT_COLUMNS
96 " FROM datastore.gn090"
97 " WHERE anonLevel = 0 AND type = $1 AND oid >= $2::bigint"
98 " ORDER BY oid ASC LIMIT 1"),
99 GNUNET_PQ_make_prepare ("select_expiration_order",
100 "(SELECT " RESULT_COLUMNS
101 " FROM datastore.gn090"
102 " WHERE expire < $1 ORDER BY prio ASC LIMIT 1) "
103 "UNION "
104 "(SELECT " RESULT_COLUMNS
105 " FROM datastore.gn090"
106 " ORDER BY prio ASC LIMIT 1)"
107 " ORDER BY expire ASC LIMIT 1"),
108 GNUNET_PQ_make_prepare ("select_replication_order",
109 "SELECT " RESULT_COLUMNS
110 " FROM datastore.gn090"
111 " ORDER BY repl DESC,RANDOM() LIMIT 1"),
112 GNUNET_PQ_make_prepare ("delrow",
113 "DELETE FROM datastore.gn090"
114 " WHERE oid=$1"),
115 GNUNET_PQ_make_prepare ("remove",
116 "DELETE FROM datastore.gn090"
117 " WHERE hash = $1 AND"
118 " value = $2"),
119 GNUNET_PQ_make_prepare ("get_keys",
120 "SELECT hash"
121 " FROM datastore.gn090"),
122 GNUNET_PQ_make_prepare ("estimate_size",
123 "SELECT CASE WHEN NOT EXISTS"
124 " (SELECT 1 FROM datastore.gn090)"
125 " THEN 0"
126 " ELSE (SELECT SUM(LENGTH(value))+256*COUNT(*)"
127 " FROM datastore.gn090)"
128 "END AS total"),
130 };
131#undef RESULT_COLUMNS
132
133 plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->env->cfg,
134 "datastore-postgres",
135 "datastore-",
136 NULL,
137 ps);
138 if (NULL == plugin->dbh)
139 return GNUNET_SYSERR;
140 return GNUNET_OK;
141}
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
struct GNUNET_PQ_Context * GNUNET_PQ_connect_with_cfg(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *load_path_suffix, 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:700
struct GNUNET_PQ_PreparedStatement GNUNET_PQ_make_prepare(const char *name, const char *sql)
Create a struct GNUNET_PQ_PreparedStatement.
Definition pq_prepare.c:30
#define GNUNET_PQ_PREPARED_STATEMENT_END
Terminator for prepared statement list.
@ GNUNET_OK
@ GNUNET_SYSERR
#define RESULT_COLUMNS
Information needed to prepare a list of SQL statements using GNUNET_PQ_prepare_statements().

References GNUNET_OK, GNUNET_PQ_connect_with_cfg(), GNUNET_PQ_make_prepare(), GNUNET_PQ_PREPARED_STATEMENT_END, GNUNET_SYSERR, plugin, ps, and RESULT_COLUMNS.

Referenced by libgnunet_plugin_datastore_postgres_init().

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 152 of file plugin_datastore_postgres.c.

154{
155 struct Plugin *plugin = cls;
156 uint64_t total;
157 struct GNUNET_PQ_QueryParam params[] = {
159 };
160 struct GNUNET_PQ_ResultSpec rs[] = {
162 &total),
164 };
166
167 if (NULL == estimate)
168 return;
170 "estimate_size",
171 params,
172 rs);
174 {
175 *estimate = 0LL;
176 return;
177 }
178 *estimate = total;
179}
static int ret
Final status code.
Definition gnunet-arm.c:93
GNUNET_DB_QueryStatus
Status code returned from functions running database commands.
@ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT
The transaction succeeded, and yielded one result.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_singleton_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, struct GNUNET_PQ_ResultSpec *rs)
Execute a named prepared statement that is a SELECT statement which must return a single result in co...
Definition pq_eval.c:199
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint64(const char *name, uint64_t *u64)
uint64_t expected.
#define GNUNET_PQ_query_param_end
End of query parameter specification.
#define GNUNET_PQ_result_spec_end
End of result parameter specification.
Description of a DB query parameter.
Description of a DB result cell.
Handle for a plugin.
Definition block.c:38

References 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().

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 199 of file plugin_datastore_postgres.c.

211{
212 struct Plugin *plugin = cls;
213 struct GNUNET_HashCode vhash;
215
217 size,
218 &vhash);
219 if (! absent)
220 {
221 struct GNUNET_PQ_QueryParam params[] = {
228 };
230 "update",
231 params);
232 if (0 > ret)
233 {
234 cont (cont_cls,
235 key,
236 size,
238 _ ("Postgresql exec failure"));
239 return;
240 }
241 if (0 != ret)
242 {
243 cont (cont_cls,
244 key,
245 size,
246 GNUNET_NO,
247 NULL);
248 return;
249 }
250 }
251
252 {
253 uint32_t utype = (uint32_t) type;
255 UINT64_MAX);
256 struct GNUNET_PQ_QueryParam params[] = {
267 };
268
270 "put",
271 params);
272 if (0 > ret)
273 {
274 cont (cont_cls,
275 key,
276 size,
278 "Postgresql exec failure");
279 return;
280 }
281 }
282 plugin->env->duc (plugin->env->cls,
285 "datastore-postgres",
286 "Stored %u bytes in database\n",
287 (unsigned int) size);
288 cont (cont_cls,
289 key,
290 size,
291 GNUNET_OK,
292 NULL);
293}
static unsigned int replication
Desired replication level.
static char * data
The data to insert into the dht.
struct GNUNET_HashCode key
The key used in the DHT.
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
static unsigned int anonymity
static uint32_t type
Type string converted to DNS type value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint64_t in host byte order.
#define GNUNET_PQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
struct GNUNET_PQ_QueryParam GNUNET_PQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_non_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params)
Execute a named prepared statement that is NOT a SELECT statement in connection using the given param...
Definition pq_eval.c:135
uint64_t GNUNET_CRYPTO_random_u64(enum GNUNET_CRYPTO_Quality mode, uint64_t max)
Generate a random unsigned 64-bit value.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
#define GNUNET_DATASTORE_ENTRY_OVERHEAD
How many bytes of overhead will we assume per entry in any DB (for reservations)?
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition crypto_hash.c:41
#define GNUNET_log_from(kind, comp,...)
@ GNUNET_NO
@ GNUNET_ERROR_TYPE_DEBUG
static unsigned int size
Size of the "table".
Definition peer.c:68
#define _(String)
GNU gettext support macro.
Definition platform.h:179
A 512-bit hashcode.
void * cls
Closure to pass to start_testcase.

References _, anonymity, GNUNET_TESTING_PluginFunctions::cls, data, expiration, 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, key, plugin, replication, ret, size, and type.

Referenced by libgnunet_plugin_datastore_postgres_init().

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 327 of file plugin_datastore_postgres.c.

330{
331 struct ProcessResultContext *prc = cls;
332 struct Plugin *plugin = prc->plugin;
333
334 if (0 == num_results)
335 {
336 /* no result */
338 "datastore-postgres",
339 "Ending iteration (no more results)\n");
340 prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
342 return;
343 }
344 if (1 != num_results)
345 {
346 GNUNET_break (0);
347 prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
349 return;
350 }
351 /* Technically we don't need the loop here, but nicer in case
352 we ever relax the condition above. */
353 for (unsigned int i = 0; i < num_results; i++)
354 {
355 int iret;
356 uint64_t rowid;
357 uint32_t utype;
358 uint32_t anonymity;
359 uint32_t replication;
360 uint32_t priority;
361 size_t size;
362 void *data;
363 struct GNUNET_TIME_Absolute expiration_time;
364 struct GNUNET_HashCode key;
365 struct GNUNET_PQ_ResultSpec rs[] = {
367 GNUNET_PQ_result_spec_uint32 ("type", &utype),
368 GNUNET_PQ_result_spec_uint32 ("prio", &priority),
370 GNUNET_PQ_result_spec_absolute_time ("expire", &expiration_time),
373 GNUNET_PQ_result_spec_uint64 ("oid", &rowid),
375 };
376
377 if (GNUNET_OK !=
379 rs,
380 i))
381 {
382 GNUNET_break (0);
383 prc->proc (prc->proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
385 return;
386 }
387
389 "datastore-postgres",
390 "Found result of size %u bytes and type %u in database\n",
391 (unsigned int) size,
392 (unsigned int) utype);
393 iret = prc->proc (prc->proc_cls,
394 &key,
395 size,
396 data,
397 (enum GNUNET_BLOCK_Type) utype,
398 priority,
399 anonymity,
401 expiration_time,
402 rowid);
403 if (iret == GNUNET_NO)
404 {
405 struct GNUNET_PQ_QueryParam param[] = {
408 };
409
411 "Processor asked for item %u to be removed.\n",
412 (unsigned int) rowid);
413 if (0 <
415 "delrow",
416 param))
417 {
419 "datastore-postgres",
420 "Deleting %u bytes from database\n",
421 (unsigned int) size);
422 plugin->env->duc (plugin->env->cls,
425 "datastore-postgres",
426 "Deleted %u bytes from database\n",
427 (unsigned int) size);
428 }
429 }
431 } /* for (i) */
432}
static char * res
Currently read line or NULL on EOF.
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_uint32(const char *name, uint32_t *u32)
uint32_t expected.
#define GNUNET_PQ_result_spec_auto_from_type(name, dst)
We expect a fixed-size result, with size determined by the type of * dst
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_absolute_time(const char *name, struct GNUNET_TIME_Absolute *at)
Absolute time expected.
struct GNUNET_PQ_ResultSpec GNUNET_PQ_result_spec_variable_size(const char *name, void **dst, size_t *sptr)
Variable-size result expected.
void GNUNET_PQ_cleanup_result(struct GNUNET_PQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_PQ_extract_result().
Definition pq.c:142
enum GNUNET_GenericReturnValue GNUNET_PQ_extract_result(PGresult *result, struct GNUNET_PQ_ResultSpec *rs, int row)
Extract results from a query result according to the given specification.
Definition pq.c:152
#define GNUNET_log(kind,...)
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_TIME_UNIT_ZERO_ABS
Absolute time zero.
Time for absolute times used by GNUnet, in microseconds.
Closure for process_result.
struct Plugin * plugin
The plugin handle.
void * proc_cls
Closure for proc.
PluginDatumProcessor proc
Function to call on each result.

References anonymity, GNUNET_TESTING_PluginFunctions::cls, data, 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_uint64(), 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_uint64(), GNUNET_PQ_result_spec_variable_size(), GNUNET_TIME_UNIT_ZERO_ABS, key, plugin, ProcessResultContext::plugin, ProcessResultContext::proc, ProcessResultContext::proc_cls, replication, res, and size.

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

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 449 of file plugin_datastore_postgres.c.

456{
457 struct Plugin *plugin = cls;
458 uint32_t utype = type;
459 uint16_t use_rvalue = random;
460 uint16_t use_key = NULL != key;
461 uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type;
462 uint64_t rvalue;
463 struct ProcessResultContext prc;
465
466 if (random)
467 {
469 UINT64_MAX);
470 next_uid = 0;
471 }
472 else
473 {
474 rvalue = 0;
475 }
476
477 {
478 struct GNUNET_PQ_QueryParam params[] = {
481 GNUNET_PQ_query_param_uint16 (&use_rvalue),
487 };
488 prc.plugin = plugin;
489 prc.proc = proc;
490 prc.proc_cls = proc_cls;
491
493 "get",
494 params,
496 &prc);
497 }
498 if (0 > res)
499 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
501}
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
enum GNUNET_DB_QueryStatus GNUNET_PQ_eval_prepared_multi_select(struct GNUNET_PQ_Context *db, const char *statement_name, const struct GNUNET_PQ_QueryParam *params, GNUNET_PQ_PostgresResultHandler rh, void *rh_cls)
Execute a named prepared statement that is a SELECT statement which may return multiple results in co...
Definition pq_eval.c:165
struct GNUNET_PQ_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.

References 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, key, plugin, ProcessResultContext::plugin, ProcessResultContext::proc, ProcessResultContext::proc_cls, process_result(), res, and type.

Referenced by libgnunet_plugin_datastore_postgres_init().

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 517 of file plugin_datastore_postgres.c.

522{
523 struct Plugin *plugin = cls;
524 uint32_t utype = type;
525 struct GNUNET_PQ_QueryParam params[] = {
529 };
530 struct ProcessResultContext prc;
532
533 prc.plugin = plugin;
534 prc.proc = proc;
535 prc.proc_cls = proc_cls;
537 "select_non_anonymous",
538 params,
540 &prc);
541 if (0 > res)
542 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
544}

References 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().

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 591 of file plugin_datastore_postgres.c.

601{
602 struct ReplCtx *rc = cls;
603 struct Plugin *plugin = rc->plugin;
604 int ret;
605 struct GNUNET_PQ_QueryParam params[] = {
608 };
609 enum GNUNET_DB_QueryStatus qret;
610
611 ret = rc->proc (rc->proc_cls,
612 key,
613 size,
614 data,
615 type,
616 priority,
617 anonymity,
620 uid);
621 if (NULL == key)
622 return ret;
624 "decrepl",
625 params);
626 if (0 > qret)
627 return GNUNET_SYSERR;
628 return ret;
629}
Context for #repl_iter() function.
void * proc_cls
Closure for proc.
struct Plugin * plugin
Plugin handle.
PluginDatumProcessor proc
Function to call for the result (or the NULL).

References anonymity, data, expiration, GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_query_param_end, GNUNET_PQ_query_param_uint64(), GNUNET_SYSERR, key, plugin, ReplCtx::plugin, ReplCtx::proc, ReplCtx::proc_cls, replication, ret, size, and type.

Referenced by postgres_plugin_get_replication().

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 644 of file plugin_datastore_postgres.c.

647{
648 struct Plugin *plugin = cls;
649 struct GNUNET_PQ_QueryParam params[] = {
651 };
652 struct ReplCtx rc;
653 struct ProcessResultContext prc;
655
656 rc.plugin = plugin;
657 rc.proc = proc;
658 rc.proc_cls = proc_cls;
659 prc.plugin = plugin;
660 prc.proc = &repl_proc;
661 prc.proc_cls = &rc;
663 "select_replication_order",
664 params,
666 &prc);
667 if (0 > res)
668 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, 0,
670}
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'.

References 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().

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 682 of file plugin_datastore_postgres.c.

685{
686 struct Plugin *plugin = cls;
687 struct GNUNET_TIME_Absolute now = { 0 };
688 struct GNUNET_PQ_QueryParam params[] = {
691 };
692 struct ProcessResultContext prc;
693
695 prc.plugin = plugin;
696 prc.proc = proc;
697 prc.proc_cls = proc_cls;
699 "select_expiration_order",
700 params,
702 &prc);
703}
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition time.c:111

References 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().

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_resultsthe number of results in result

Definition at line 732 of file plugin_datastore_postgres.c.

735{
736 struct ProcessKeysContext *pkc = cls;
737
738 for (unsigned i = 0; i < num_results; i++)
739 {
740 struct GNUNET_HashCode key;
741 struct GNUNET_PQ_ResultSpec rs[] = {
743 &key),
745 };
746
747 if (GNUNET_OK !=
749 rs,
750 i))
751 {
752 GNUNET_break (0);
753 continue;
754 }
755 pkc->proc (pkc->proc_cls,
756 &key,
757 1);
759 }
760}
static int result
Global testing status.
Closure for process_keys.
PluginKeyProcessor proc
Function to call for each key.
void * proc_cls
Closure for proc.

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, key, ProcessKeysContext::proc, ProcessKeysContext::proc_cls, and result.

Referenced by postgres_plugin_get_keys().

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 771 of file plugin_datastore_postgres.c.

774{
775 struct Plugin *plugin = cls;
776 struct GNUNET_PQ_QueryParam params[] = {
778 };
779 struct ProcessKeysContext pkc;
780
781 pkc.proc = proc;
782 pkc.proc_cls = proc_cls;
784 "get_keys",
785 params,
787 &pkc);
788 proc (proc_cls,
789 NULL,
790 0);
791}
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.

References 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().

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 800 of file plugin_datastore_postgres.c.

801{
802 struct Plugin *plugin = cls;
803 struct GNUNET_PQ_ExecuteStatement es[] = {
804 GNUNET_PQ_make_execute ("DROP TABLE gn090"),
806 };
807
808 if (GNUNET_OK !=
810 es))
812 "postgres",
813 _ ("Failed to drop table from database.\n"));
814}
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:30
enum GNUNET_GenericReturnValue 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:54
#define GNUNET_PQ_EXECUTE_STATEMENT_END
Terminator for executable statement list.
@ GNUNET_ERROR_TYPE_WARNING
Information needed to run a list of SQL statements using GNUNET_PQ_exec_statements().

References _, 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().

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 828 of file plugin_datastore_postgres.c.

834{
835 struct Plugin *plugin = cls;
837 struct GNUNET_PQ_QueryParam params[] = {
841 };
842
844 "remove",
845 params);
846 if (0 > ret)
847 {
848 cont (cont_cls,
849 key,
850 size,
852 _ ("Postgresql exec failure"));
853 return;
854 }
856 {
857 cont (cont_cls,
858 key,
859 size,
860 GNUNET_NO,
861 NULL);
862 return;
863 }
864 plugin->env->duc (plugin->env->cls,
867 "datastore-postgres",
868 "Deleted %u bytes from database\n",
869 (unsigned int) size);
870 cont (cont_cls,
871 key,
872 size,
873 GNUNET_OK,
874 NULL);
875}
@ GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
The transaction succeeded, but yielded zero results.

References _, GNUNET_TESTING_PluginFunctions::cls, data, 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, key, plugin, ret, and size.

Referenced by libgnunet_plugin_datastore_postgres_init().

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 888 of file plugin_datastore_postgres.c.

889{
892 struct Plugin *plugin;
893
894 plugin = GNUNET_new (struct Plugin);
895 plugin->env = env;
897 {
899 return NULL;
900 }
902 api->cls = plugin;
903 api->estimate_size = &postgres_plugin_estimate_size;
904 api->put = &postgres_plugin_put;
906 api->get_replication = &postgres_plugin_get_replication;
907 api->get_expiration = &postgres_plugin_get_expiration;
908 api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity;
909 api->get_keys = &postgres_plugin_get_keys;
910 api->drop = &postgres_plugin_drop;
911 api->remove_key = &postgres_plugin_remove_key;
912 return api;
913}
struct GNUNET_MQ_Envelope * env
Definition 005.c:1
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
static void postgres_plugin_get_replication(void *cls, PluginDatumProcessor proc, void *proc_cls)
Get a random item for replication.
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_drop(void *cls)
Drop database.
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.
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.
static enum GNUNET_GenericReturnValue init_connection(struct Plugin *plugin)
Get a database handle.
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.
static void postgres_plugin_get_expiration(void *cls, PluginDatumProcessor proc, void *proc_cls)
Get a random item for expiration.
static void postgres_plugin_get_keys(void *cls, PluginKeyProcessor proc, void *proc_cls)
Get all of the keys in the datastore.
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.
GNUNET_BLOCK_GetKeyFunction get_key
Obtain the key for a given block (if possible).
void * cls
Closure for all of the callbacks.
The datastore service will pass a pointer to a struct of this type as the first and only argument to ...
Each plugin is required to return a pointer to a struct of this type as the return value from its ent...
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition block.c:47

References Plugin::api, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_DATASTORE_PluginEnvironment::cls, env, GNUNET_BLOCK_PluginFunctions::get_key, GNUNET_free, 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(), and postgres_plugin_remove_key().

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 926 of file plugin_datastore_postgres.c.

927{
929 struct Plugin *plugin = api->cls;
930
934 return NULL;
935}
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:765
void * cls
Closure to use for all of the following callbacks (except "next_request").

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

Here is the call graph for this function: