GNUnet 0.22.2
plugin_peerstore_sqlite.c File Reference

sqlite-based peerstore backend More...

#include "platform.h"
#include "gnunet_peerstore_plugin.h"
#include "gnunet_peerstore_service.h"
#include "gnunet_sq_lib.h"
#include <sqlite3.h>
Include dependency graph for plugin_peerstore_sqlite.c:

Go to the source code of this file.

Data Structures

struct  Plugin
 Handle for a plugin. More...
 

Macros

#define BUSY_TIMEOUT_MS   1000
 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 LOG_SQLITE(db, level, cmd)
 Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'filename' with the message given by strerror(errno). More...
 
#define LOG(kind, ...)   GNUNET_log_from (kind, "peerstore-sqlite", __VA_ARGS__)
 

Functions

static int peerstore_sqlite_delete_records (void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key)
 Delete records with the given key. More...
 
static int peerstore_sqlite_expire_records (void *cls, struct GNUNET_TIME_Absolute now, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
 Delete expired records (expiry < now) More...
 
static int peerstore_sqlite_iterate_records (void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, uint64_t serial, uint64_t limit, GNUNET_PEERSTORE_PluginProcessor iter, void *iter_cls)
 Iterate over the records given an optional peer id and/or key. More...
 
static int peerstore_sqlite_store_record (void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, const void *value, size_t size, struct GNUNET_TIME_Absolute expiry, enum GNUNET_PEERSTORE_StoreOption options, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
 Store a record in the peerstore. More...
 
static int sql_exec (sqlite3 *dbh, const char *sql)
 Prepare a SQL statement. More...
 
static int sql_prepare (sqlite3 *dbh, const char *sql, sqlite3_stmt **stmt)
 Prepare a SQL statement. More...
 
static int database_setup (struct Plugin *plugin)
 Initialize the database connections and associated data structures (create tables and indices as needed as well). More...
 
static void database_shutdown (struct Plugin *plugin)
 Shutdown database connection and associate data structures. More...
 
void * libgnunet_plugin_peerstore_sqlite_init (void *cls)
 Entry point for the plugin. More...
 
void * libgnunet_plugin_peerstore_sqlite_done (void *cls)
 Exit point from the plugin. More...
 

Detailed Description

sqlite-based peerstore backend

Author
Omar Tarabai
Christian Grothoff

Definition in file plugin_peerstore_sqlite.c.

Macro Definition Documentation

◆ BUSY_TIMEOUT_MS

#define BUSY_TIMEOUT_MS   1000

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 45 of file plugin_peerstore_sqlite.c.

◆ LOG_SQLITE

#define LOG_SQLITE (   db,
  level,
  cmd 
)
Value:
do { GNUNET_log_from (level, \
"peerstore-sqlite", _ ( \
"`%s' failed at %s:%d with error: %s\n"), \
cmd, \
__FILE__, __LINE__, \
sqlite3_errmsg ( \
db->dbh)); \
} while (0)
static struct GNUNET_FS_DirectoryBuilder * db
#define GNUNET_log_from(kind, comp,...)
#define _(String)
GNU gettext support macro.
Definition: platform.h:179

Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'filename' with the message given by strerror(errno).

Definition at line 52 of file plugin_peerstore_sqlite.c.

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "peerstore-sqlite", __VA_ARGS__)

Definition at line 61 of file plugin_peerstore_sqlite.c.

Function Documentation

◆ peerstore_sqlite_delete_records()

static int peerstore_sqlite_delete_records ( void *  cls,
const char *  sub_system,
const struct GNUNET_PeerIdentity peer,
const char *  key 
)
static

Delete records with the given key.

Parameters
clsclosure (internal context for the plugin)
sub_systemname of sub system
peerPeer identity (can be NULL)
keyentry key string (can be NULL)
Returns
number of deleted records, #GNUNE_SYSERR on error

Definition at line 132 of file plugin_peerstore_sqlite.c.

136{
137 struct Plugin *plugin = cls;
138 sqlite3_stmt *stmt = plugin->delete_peerstoredata;
139 struct GNUNET_SQ_QueryParam params[] = {
140 GNUNET_SQ_query_param_string (sub_system),
144 };
145 int ret;
146
147 if (GNUNET_OK !=
148 GNUNET_SQ_bind (stmt,
149 params))
150 {
153 "sqlite3_bind");
155 stmt);
156 return GNUNET_SYSERR;
157 }
158 if (SQLITE_DONE !=
159 sqlite3_step (stmt))
160 {
163 "sqlite3_step");
165 }
166 else
167 {
168 ret = sqlite3_changes (plugin->dbh);
169 }
171 stmt);
172 return ret;
173}
static int ret
Final status code.
Definition: gnunet-arm.c:93
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_SQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
enum GNUNET_GenericReturnValue GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:30
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition: sq.c:119
#define GNUNET_SQ_query_param_end
End of query parameter specification.
Definition: gnunet_sq_lib.h:87
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_string(const char *ptr)
Generate query parameter for a string.
@ GNUNET_OK
@ GNUNET_SYSERR
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_BULK
#define LOG_SQLITE(db, level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'file...
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:56
Handle for a plugin.
Definition: block.c:38

References GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_ERROR, GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_query_param_auto_from_type, GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_string(), GNUNET_SQ_reset(), GNUNET_SYSERR, key, LOG_SQLITE, plugin, and ret.

Referenced by peerstore_sqlite_store_record().

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

◆ peerstore_sqlite_expire_records()

static int peerstore_sqlite_expire_records ( void *  cls,
struct GNUNET_TIME_Absolute  now,
GNUNET_PEERSTORE_Continuation  cont,
void *  cont_cls 
)
static

Delete expired records (expiry < now)

Parameters
clsclosure (internal context for the plugin)
nowtime to use as reference
contcontinuation called with the number of records expired
cont_clscontinuation closure
Returns
GNUNET_OK on success, GNUNET_SYSERR on error and cont is not called

Definition at line 187 of file plugin_peerstore_sqlite.c.

190{
191 struct Plugin *plugin = cls;
192 sqlite3_stmt *stmt = plugin->expire_peerstoredata;
193 struct GNUNET_SQ_QueryParam params[] = {
196 };
197
198 if (GNUNET_OK !=
199 GNUNET_SQ_bind (stmt,
200 params))
201 {
204 "sqlite3_bind");
206 stmt);
207 return GNUNET_SYSERR;
208 }
209 if (SQLITE_DONE != sqlite3_step (stmt))
210 {
213 "sqlite3_step");
215 stmt);
216 return GNUNET_SYSERR;
217 }
218 if (NULL != cont)
219 cont (cont_cls,
220 sqlite3_changes (plugin->dbh));
222 stmt);
223 return GNUNET_OK;
224}
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.

References GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_ERROR, GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_query_param_absolute_time(), GNUNET_SQ_query_param_end, GNUNET_SQ_reset(), GNUNET_SYSERR, LOG_SQLITE, and plugin.

Referenced by libgnunet_plugin_peerstore_sqlite_init().

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

◆ peerstore_sqlite_iterate_records()

static int peerstore_sqlite_iterate_records ( void *  cls,
const char *  sub_system,
const struct GNUNET_PeerIdentity peer,
const char *  key,
uint64_t  serial,
uint64_t  limit,
GNUNET_PEERSTORE_PluginProcessor  iter,
void *  iter_cls 
)
static

Iterate over the records given an optional peer id and/or key.

Parameters
clsclosure (internal context for the plugin)
sub_systemname of sub system
peerPeer identity (can be NULL)
keyentry key string (can be NULL)
iterfunction to call asynchronously with the results, terminated by a NULL result
iter_clsclosure for iter
Returns
GNUNET_OK on success, GNUNET_SYSERR on error and iter is not called

Definition at line 242 of file plugin_peerstore_sqlite.c.

250{
251 struct Plugin *plugin = cls;
252 sqlite3_stmt *stmt;
253 int err = 0;
254 int sret;
255 int ret;
256 uint64_t seq;
257 struct GNUNET_PEERSTORE_Record rec;
258
260 "Executing iterate request on sqlite db.\n");
261 if (NULL == peer)
262 {
263 if (NULL == key)
264 {
265 struct GNUNET_SQ_QueryParam params[] = {
266 GNUNET_SQ_query_param_string (sub_system),
270 };
271
272 stmt = plugin->select_peerstoredata;
273 err = GNUNET_SQ_bind (stmt,
274 params);
275 }
276 else
277 {
278 struct GNUNET_SQ_QueryParam params[] = {
279 GNUNET_SQ_query_param_string (sub_system),
284 };
285
286 stmt = plugin->select_peerstoredata_by_key;
287 err = GNUNET_SQ_bind (stmt,
288 params);
289 }
290 }
291 else
292 {
293 if (NULL == key)
294 {
295 struct GNUNET_SQ_QueryParam params[] = {
296 GNUNET_SQ_query_param_string (sub_system),
301 };
302
303 stmt = plugin->select_peerstoredata_by_pid;
304 err = GNUNET_SQ_bind (stmt,
305 params);
306 }
307 else
308 {
309 struct GNUNET_SQ_QueryParam params[] = {
310 GNUNET_SQ_query_param_string (sub_system),
316 };
317
318 stmt = plugin->select_peerstoredata_by_all;
319 err = GNUNET_SQ_bind (stmt,
320 params);
321 }
322 }
323
324 if (GNUNET_OK != err)
325 {
328 "sqlite3_bind_XXXX");
330 stmt);
331 return GNUNET_SYSERR;
332 }
333
334 err = 0;
335 ret = GNUNET_OK;
336 for (uint64_t i = 0; i < limit; i++)
337 {
338 sret = sqlite3_step (stmt);
339 if (SQLITE_DONE == sret)
340 {
342 "Iteration done (no results)\n");
343 ret = GNUNET_NO;
344 break;
345 }
346 if (SQLITE_ROW != sret)
347 {
350 "sqlite_step");
352 break;
353 }
354 {
355 struct GNUNET_SQ_ResultSpec rs[] = {
357 GNUNET_SQ_result_spec_string (&rec.sub_system),
360 GNUNET_SQ_result_spec_variable_size (&rec.value, &rec.value_size),
363 };
365 "Returning a matched record.\n");
366
367 if (GNUNET_OK !=
369 rs))
370 {
371 GNUNET_break (0);
372 break;
373 }
374 if (NULL != iter)
375 iter (iter_cls,
376 seq,
377 &rec,
378 NULL);
380 }
381 }
383 stmt);
384 return ret;
385}
enum GNUNET_GenericReturnValue GNUNET_SQ_extract_result(sqlite3_stmt *result, struct GNUNET_SQ_ResultSpec *rs)
Extract results from a query result according to the given specification.
Definition: sq.c:75
void GNUNET_SQ_cleanup_result(struct GNUNET_SQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_SQ_extract_result().
Definition: sq.c:104
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_string(char **dst)
0-terminated string expected.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
#define GNUNET_SQ_result_spec_end
End of result parameter specification.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_variable_size(void **dst, size_t *sptr)
Variable-size result expected.
#define GNUNET_SQ_result_spec_auto_from_type(dst)
We expect a fixed-size result, with size determined by the type of * dst
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint64(uint64_t *u64)
uint64_t expected.
#define GNUNET_log(kind,...)
@ GNUNET_NO
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_DEBUG
#define LOG(kind,...)
Single PEERSTORE record.
struct GNUNET_PeerIdentity peer
Peer Identity.
Description of a DB result cell.

References GNUNET_PEERSTORE_Record::expiry, GNUNET_break, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_NO, GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_cleanup_result(), GNUNET_SQ_extract_result(), GNUNET_SQ_query_param_auto_from_type, GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_string(), GNUNET_SQ_query_param_uint64(), GNUNET_SQ_reset(), GNUNET_SQ_result_spec_absolute_time(), GNUNET_SQ_result_spec_auto_from_type, GNUNET_SQ_result_spec_end, GNUNET_SQ_result_spec_string(), GNUNET_SQ_result_spec_uint64(), GNUNET_SQ_result_spec_variable_size(), GNUNET_SYSERR, key, GNUNET_PEERSTORE_Record::key, LOG, LOG_SQLITE, GNUNET_PEERSTORE_Record::peer, plugin, ret, GNUNET_PEERSTORE_Record::sub_system, GNUNET_PEERSTORE_Record::value, and GNUNET_PEERSTORE_Record::value_size.

Referenced by libgnunet_plugin_peerstore_sqlite_init().

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

◆ peerstore_sqlite_store_record()

static int peerstore_sqlite_store_record ( void *  cls,
const char *  sub_system,
const struct GNUNET_PeerIdentity peer,
const char *  key,
const void *  value,
size_t  size,
struct GNUNET_TIME_Absolute  expiry,
enum GNUNET_PEERSTORE_StoreOption  options,
GNUNET_PEERSTORE_Continuation  cont,
void *  cont_cls 
)
static

Store a record in the peerstore.

Key is the combination of sub system and peer identity. One key can store multiple values.

Parameters
clsclosure (internal context for the plugin)
sub_systemname of the GNUnet sub system responsible
peerpeer identity
keyrecord key string
valuevalue to be stored
sizesize of value to be stored
expiryabsolute time after which the record is (possibly) deleted
optionsoptions related to the store operation
contcontinuation called when record is stored
cont_clscontinuation closure
Returns
GNUNET_OK on success, else GNUNET_SYSERR and cont is not called

Definition at line 406 of file plugin_peerstore_sqlite.c.

416{
417 struct Plugin *plugin = cls;
418 sqlite3_stmt *stmt;
419 struct GNUNET_SQ_QueryParam params[] = {
420 GNUNET_SQ_query_param_string (sub_system),
426 };
427
429 {
431 sub_system,
432 peer,
433 key);
434 }
435
436 stmt = plugin->insert_peerstoredata;
437 if (GNUNET_OK !=
438 GNUNET_SQ_bind (stmt,
439 params))
440 {
443 "sqlite3_bind");
444 GNUNET_assert (0);
445 }
446 if (SQLITE_DONE != sqlite3_step (stmt))
447 {
450 "sqlite3_step");
451 }
453 stmt);
454 if (NULL != cont)
455 cont (cont_cls,
456 GNUNET_OK);
457 return GNUNET_OK;
458}
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
static char * value
Value of the record to add/remove.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_fixed_size(const void *ptr, size_t ptr_size)
Generate query parameter for a buffer ptr of ptr_size bytes.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_PEERSTORE_STOREOPTION_REPLACE
Delete any previous values for the given key before storing the given value.
static unsigned int size
Size of the "table".
Definition: peer.c:68
static int peerstore_sqlite_delete_records(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key)
Delete records with the given key.

References GNUNET_assert, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_ERROR, GNUNET_OK, GNUNET_PEERSTORE_STOREOPTION_REPLACE, GNUNET_SQ_bind(), GNUNET_SQ_query_param_absolute_time(), GNUNET_SQ_query_param_auto_from_type, GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_fixed_size(), GNUNET_SQ_query_param_string(), GNUNET_SQ_reset(), key, LOG_SQLITE, options, peerstore_sqlite_delete_records(), plugin, size, and value.

Referenced by libgnunet_plugin_peerstore_sqlite_init().

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

◆ sql_exec()

static int sql_exec ( sqlite3 *  dbh,
const char *  sql 
)
static

Prepare a SQL statement.

Parameters
dbhhandle to the database
sqlSQL statement, UTF-8 encoded
Returns
0 on success

Definition at line 469 of file plugin_peerstore_sqlite.c.

471{
472 int result;
473
474 result = sqlite3_exec (dbh,
475 sql,
476 NULL,
477 NULL,
478 NULL);
480 "Executed `%s' / %d\n",
481 sql,
482 result);
483 if (SQLITE_OK != result)
485 _ ("Error executing SQL query: %s\n %s\n"),
486 sqlite3_errmsg (dbh),
487 sql);
488 return result;
489}
static int result
Global testing status.

References _, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, LOG, and result.

Referenced by database_setup().

Here is the caller graph for this function:

◆ sql_prepare()

static int sql_prepare ( sqlite3 *  dbh,
const char *  sql,
sqlite3_stmt **  stmt 
)
static

Prepare a SQL statement.

Parameters
dbhhandle to the database
sqlSQL statement, UTF-8 encoded
stmtset to the prepared statement
Returns
0 on success

Definition at line 501 of file plugin_peerstore_sqlite.c.

504{
505 char *tail;
506 int result;
507
508 result = sqlite3_prepare_v2 (dbh,
509 sql,
510 strlen (sql),
511 stmt,
512 (const char **) &tail);
514 "Prepared `%s' / %p: %d\n",
515 sql,
516 *stmt,
517 result);
518 if (SQLITE_OK != result)
520 _ ("Error preparing SQL query: %s\n %s\n"),
521 sqlite3_errmsg (dbh),
522 sql);
523 return result;
524}

References _, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, LOG, and result.

Referenced by database_setup().

Here is the caller graph for this function:

◆ database_setup()

static int database_setup ( struct Plugin plugin)
static

Initialize the database connections and associated data structures (create tables and indices as needed as well).

Parameters
pluginthe plugin context (state for this module)
Returns
GNUNET_OK on success

Definition at line 536 of file plugin_peerstore_sqlite.c.

537{
538 char *filename;
539
540 if (GNUNET_OK !=
542 "peerstore-sqlite",
543 "FILENAME",
544 &filename))
545 {
547 "peerstore-sqlite",
548 "FILENAME");
549 return GNUNET_SYSERR;
550 }
552 {
554 {
555 GNUNET_break (0);
557 return GNUNET_SYSERR;
558 }
559 }
560 /* filename should be UTF-8-encoded. If it isn't, it's a bug */
561 plugin->fn = filename;
562 /* Open database and precompile statements */
563 if (SQLITE_OK != sqlite3_open (plugin->fn,
564 &plugin->dbh))
565 {
567 _ ("Unable to initialize SQLite: %s.\n"),
568 sqlite3_errmsg (plugin->dbh));
569 return GNUNET_SYSERR;
570 }
571 sql_exec (plugin->dbh,
572 "PRAGMA temp_store=MEMORY");
573 sql_exec (plugin->dbh,
574 "PRAGMA synchronous=OFF");
575 sql_exec (plugin->dbh,
576 "PRAGMA legacy_file_format=OFF");
577 sql_exec (plugin->dbh,
578 "PRAGMA auto_vacuum=INCREMENTAL");
579 sql_exec (plugin->dbh,
580 "PRAGMA encoding=\"UTF-8\"");
581 sql_exec (plugin->dbh,
582 "PRAGMA page_size=4096");
583 sqlite3_busy_timeout (plugin->dbh,
585 /* Create tables */
586 sql_exec (plugin->dbh,
587 "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
588 " uid INTEGER PRIMARY KEY,"
589 " sub_system TEXT NOT NULL,\n"
590 " peer_id BLOB NOT NULL,\n"
591 " key TEXT NOT NULL,\n"
592 " value BLOB NULL,\n"
593 " expiry INT8 NOT NULL" ");");
594 /* Create Indices */
595 if (SQLITE_OK !=
596 sqlite3_exec (plugin->dbh,
597 "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key, uid)",
598 NULL,
599 NULL,
600 NULL))
601 {
603 _ ("Unable to create indices: %s.\n"),
604 sqlite3_errmsg (plugin->dbh));
605 return GNUNET_SYSERR;
606 }
607 /* Prepare statements */
608
609 sql_prepare (plugin->dbh,
610 "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry)"
611 " VALUES (?,?,?,?,?);",
612 &plugin->insert_peerstoredata);
613 sql_prepare (plugin->dbh,
614 "SELECT uid,sub_system,peer_id,key,value,expiry FROM peerstoredata"
615 " WHERE sub_system = ?"
616 " AND uid > ?"
617 " ORDER BY uid ASC"
618 " LIMIT ?",
619 &plugin->select_peerstoredata);
620 sql_prepare (plugin->dbh,
621 "SELECT uid,sub_system,peer_id,key,value,expiry FROM peerstoredata"
622 " WHERE sub_system = ?"
623 " AND peer_id = ?"
624 " AND uid > ?"
625 " ORDER BY uid ASC"
626 " LIMIT ?",
627 &plugin->select_peerstoredata_by_pid);
628 sql_prepare (plugin->dbh,
629 "SELECT uid,sub_system,peer_id,key,value,expiry FROM peerstoredata"
630 " WHERE sub_system = ?"
631 " AND key = ?"
632 " AND uid > ?"
633 " ORDER BY uid ASC"
634 " LIMIT ?",
635 &plugin->select_peerstoredata_by_key);
636 sql_prepare (plugin->dbh,
637 "SELECT uid,sub_system,peer_id,key,value,expiry FROM peerstoredata"
638 " WHERE sub_system = ?"
639 " AND peer_id = ?"
640 " AND key = ?"
641 " AND uid > ?"
642 " ORDER BY uid ASC"
643 " LIMIT ?",
644 &plugin->select_peerstoredata_by_all);
645 sql_prepare (plugin->dbh,
646 "DELETE FROM peerstoredata"
647 " WHERE expiry < ?",
648 &plugin->expire_peerstoredata);
649 sql_prepare (plugin->dbh,
650 "DELETE FROM peerstoredata"
651 " WHERE sub_system = ?"
652 " AND peer_id = ?" " AND key = ?",
653 &plugin->delete_peerstoredata);
654 return GNUNET_OK;
655}
static char * filename
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
Definition: disk.c:520
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:620
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_free(ptr)
Wrapper around free.
#define BUSY_TIMEOUT_MS
After how many ms "busy" should a DB operation fail for good? A low value makes sure that we are more...
static int sql_prepare(sqlite3 *dbh, const char *sql, sqlite3_stmt **stmt)
Prepare a SQL statement.
static int sql_exec(sqlite3 *dbh, const char *sql)
Prepare a SQL statement.

References _, BUSY_TIMEOUT_MS, filename, GNUNET_break, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_file_test(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_log_config_missing(), GNUNET_OK, GNUNET_SYSERR, LOG, plugin, sql_exec(), and sql_prepare().

Referenced by libgnunet_plugin_peerstore_sqlite_init().

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

◆ database_shutdown()

static void database_shutdown ( struct Plugin plugin)
static

Shutdown database connection and associate data structures.

Parameters
pluginthe plugin context (state for this module)

Definition at line 664 of file plugin_peerstore_sqlite.c.

665{
666 int result;
667 sqlite3_stmt *stmt;
668
669 while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh,
670 NULL)))
671 {
672 result = sqlite3_finalize (stmt);
673 if (SQLITE_OK != result)
675 "Failed to close statement %p: %d\n",
676 stmt,
677 result);
678 }
679 if (SQLITE_OK != sqlite3_close (plugin->dbh))
682 "sqlite3_close");
683 GNUNET_free (plugin->fn);
684}
@ GNUNET_ERROR_TYPE_WARNING

References GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, LOG, LOG_SQLITE, plugin, and result.

Referenced by libgnunet_plugin_peerstore_sqlite_done(), and libgnunet_plugin_peerstore_sqlite_init().

Here is the caller graph for this function:

◆ libgnunet_plugin_peerstore_sqlite_init()

void * libgnunet_plugin_peerstore_sqlite_init ( void *  cls)

Entry point for the plugin.

Parameters
clsThe struct GNUNET_CONFIGURATION_Handle.
Returns
NULL on error, otherwise the plugin context

Definition at line 696 of file plugin_peerstore_sqlite.c.

697{
698 static struct Plugin plugin;
699 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
701
702 if (NULL != plugin.cfg)
703 return NULL; /* can only initialize once! */
704 memset (&plugin,
705 0,
706 sizeof(struct Plugin));
707 plugin.cfg = cfg;
709 {
711 return NULL;
712 }
714 api->cls = &plugin;
719 "Sqlite plugin is running\n");
720 return api;
721}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static int peerstore_sqlite_store_record(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, const void *value, size_t size, struct GNUNET_TIME_Absolute expiry, enum GNUNET_PEERSTORE_StoreOption options, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Store a record in the peerstore.
static int peerstore_sqlite_expire_records(void *cls, struct GNUNET_TIME_Absolute now, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Delete expired records (expiry < now)
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
static int peerstore_sqlite_iterate_records(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, uint64_t serial, uint64_t limit, GNUNET_PEERSTORE_PluginProcessor iter, void *iter_cls)
Iterate over the records given an optional peer id and/or key.
static int database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
struct returned by the initialization function of the plugin
void * cls
Closure to pass to all plugin functions.
int(* iterate_records)(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, uint64_t serial, uint64_t limit, GNUNET_PEERSTORE_PluginProcessor iter, void *iter_cls)
Iterate over the records given an optional peer id and/or key.
int(* store_record)(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, const void *value, size_t size, struct GNUNET_TIME_Absolute expiry, enum GNUNET_PEERSTORE_StoreOption options, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Store a record in the peerstore.
int(* expire_records)(void *cls, struct GNUNET_TIME_Absolute now, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Delete expired records (expiry < now)

References cfg, GNUNET_PEERSTORE_PluginFunctions::cls, database_setup(), database_shutdown(), GNUNET_PEERSTORE_PluginFunctions::expire_records, GNUNET_ERROR_TYPE_DEBUG, GNUNET_new, GNUNET_OK, GNUNET_PEERSTORE_PluginFunctions::iterate_records, LOG, peerstore_sqlite_expire_records(), peerstore_sqlite_iterate_records(), peerstore_sqlite_store_record(), plugin, and GNUNET_PEERSTORE_PluginFunctions::store_record.

Here is the call graph for this function:

◆ libgnunet_plugin_peerstore_sqlite_done()

void * libgnunet_plugin_peerstore_sqlite_done ( void *  cls)

Exit point from the plugin.

Parameters
clsThe plugin context (as returned by "init")
Returns
Always NULL

Definition at line 733 of file plugin_peerstore_sqlite.c.

734{
736 struct Plugin *plugin = api->cls;
737
739 plugin->cfg = NULL;
742 "Sqlite plugin is finished\n");
743 return NULL;
744}
void * cls
Closure for all of the callbacks.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47

References Plugin::api, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_PEERSTORE_PluginFunctions::cls, database_shutdown(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_free, LOG, and plugin.

Here is the call graph for this function: