GNUnet  0.10.x
Data Structures | Macros | Functions
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 "peerstore.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)   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)
 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, GNUNET_PEERSTORE_Processor 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.

Referenced by database_setup().

◆ LOG_SQLITE

#define LOG_SQLITE (   db,
  level,
  cmd 
)    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)

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.

Referenced by database_shutdown(), peerstore_sqlite_delete_records(), peerstore_sqlite_expire_records(), peerstore_sqlite_iterate_records(), and peerstore_sqlite_store_record().

◆ LOG

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

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

References Plugin::dbh, Plugin::delete_peerstoredata, 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, LOG_SQLITE, plugin, and ret.

Referenced by peerstore_sqlite_store_record().

128 {
129  struct Plugin *plugin = cls;
130  sqlite3_stmt *stmt = plugin->delete_peerstoredata;
131  struct GNUNET_SQ_QueryParam params[] = {
132  GNUNET_SQ_query_param_string(sub_system),
136  };
137  int ret;
138 
139  if (GNUNET_OK !=
140  GNUNET_SQ_bind(stmt,
141  params))
142  {
143  LOG_SQLITE(plugin,
145  "sqlite3_bind");
146  GNUNET_SQ_reset(plugin->dbh,
147  stmt);
148  return GNUNET_SYSERR;
149  }
150  if (SQLITE_DONE !=
151  sqlite3_step(stmt))
152  {
153  LOG_SQLITE(plugin,
155  "sqlite3_step");
156  ret = GNUNET_SYSERR;
157  }
158  else
159  {
160  ret = sqlite3_changes(plugin->dbh);
161  }
162  GNUNET_SQ_reset(plugin->dbh,
163  stmt);
164  return ret;
165 }
sqlite3_stmt * delete_peerstoredata
Precompiled SQL for deleting records with given key.
#define GNUNET_SQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static int ret
Final status code.
Definition: gnunet-arm.c:89
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
int GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:37
#define LOG_SQLITE(db, level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_string(const char *ptr)
Generate query parameter for a string.
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:54
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition: sq.c:132
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
#define GNUNET_SQ_query_param_end
End of query parameter specification.
Definition: gnunet_sq_lib.h:85
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 179 of file plugin_peerstore_sqlite.c.

References Plugin::dbh, Plugin::expire_peerstoredata, 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().

182 {
183  struct Plugin *plugin = cls;
184  sqlite3_stmt *stmt = plugin->expire_peerstoredata;
185  struct GNUNET_SQ_QueryParam params[] = {
188  };
189 
190  if (GNUNET_OK !=
191  GNUNET_SQ_bind(stmt,
192  params))
193  {
194  LOG_SQLITE(plugin,
196  "sqlite3_bind");
197  GNUNET_SQ_reset(plugin->dbh,
198  stmt);
199  return GNUNET_SYSERR;
200  }
201  if (SQLITE_DONE != sqlite3_step(stmt))
202  {
203  LOG_SQLITE(plugin,
205  "sqlite3_step");
206  GNUNET_SQ_reset(plugin->dbh,
207  stmt);
208  return GNUNET_SYSERR;
209  }
210  if (NULL != cont)
211  cont(cont_cls,
212  sqlite3_changes(plugin->dbh));
213  GNUNET_SQ_reset(plugin->dbh,
214  stmt);
215  return GNUNET_OK;
216 }
sqlite3_stmt * expire_peerstoredata
Precompiled SQL for deleting expired records from peerstoredata.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
int GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:37
#define LOG_SQLITE(db, level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:54
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition: sq.c:132
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
#define GNUNET_SQ_query_param_end
End of query parameter specification.
Definition: gnunet_sq_lib.h:85
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,
GNUNET_PEERSTORE_Processor  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 234 of file plugin_peerstore_sqlite.c.

References Plugin::dbh, GNUNET_PEERSTORE_Record::expiry, GNUNET_break, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, 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_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_variable_size(), GNUNET_SYSERR, Plugin::iter, GNUNET_PEERSTORE_Record::key, LOG, LOG_SQLITE, GNUNET_PEERSTORE_Record::peer, plugin, Plugin::select_peerstoredata, Plugin::select_peerstoredata_by_all, Plugin::select_peerstoredata_by_key, Plugin::select_peerstoredata_by_pid, GNUNET_PEERSTORE_Record::sub_system, GNUNET_PEERSTORE_Record::value, and GNUNET_PEERSTORE_Record::value_size.

Referenced by libgnunet_plugin_peerstore_sqlite_init().

240 {
241  struct Plugin *plugin = cls;
242  sqlite3_stmt *stmt;
243  int err = 0;
244  int sret;
245  struct GNUNET_PEERSTORE_Record rec;
246 
248  "Executing iterate request on sqlite db.\n");
249  if (NULL == peer)
250  {
251  if (NULL == key)
252  {
253  struct GNUNET_SQ_QueryParam params[] = {
254  GNUNET_SQ_query_param_string(sub_system),
256  };
257 
258  stmt = plugin->select_peerstoredata;
259  err = GNUNET_SQ_bind(stmt,
260  params);
261  }
262  else
263  {
264  struct GNUNET_SQ_QueryParam params[] = {
265  GNUNET_SQ_query_param_string(sub_system),
268  };
269 
270  stmt = plugin->select_peerstoredata_by_key;
271  err = GNUNET_SQ_bind(stmt,
272  params);
273  }
274  }
275  else
276  {
277  if (NULL == key)
278  {
279  struct GNUNET_SQ_QueryParam params[] = {
280  GNUNET_SQ_query_param_string(sub_system),
283  };
284 
285  stmt = plugin->select_peerstoredata_by_pid;
286  err = GNUNET_SQ_bind(stmt,
287  params);
288  }
289  else
290  {
291  struct GNUNET_SQ_QueryParam params[] = {
292  GNUNET_SQ_query_param_string(sub_system),
296  };
297 
298  stmt = plugin->select_peerstoredata_by_all;
299  err = GNUNET_SQ_bind(stmt,
300  params);
301  }
302  }
303 
304  if (GNUNET_OK != err)
305  {
306  LOG_SQLITE(plugin,
308  "sqlite3_bind_XXXX");
309  GNUNET_SQ_reset(plugin->dbh,
310  stmt);
311  return GNUNET_SYSERR;
312  }
313 
314  err = 0;
315  while (SQLITE_ROW == (sret = sqlite3_step(stmt)))
316  {
318  "Returning a matched record.\n");
319  struct GNUNET_SQ_ResultSpec rs[] = {
320  GNUNET_SQ_result_spec_string(&rec.sub_system),
323  GNUNET_SQ_result_spec_variable_size(&rec.value, &rec.value_size),
326  };
327 
328  if (GNUNET_OK !=
330  rs))
331  {
332  GNUNET_break(0);
333  break;
334  }
335  if (NULL != iter)
336  iter(iter_cls,
337  &rec,
338  NULL);
340  }
341  if (SQLITE_DONE != sret)
342  {
343  LOG_SQLITE(plugin,
345  "sqlite_step");
346  err = 1;
347  }
348  GNUNET_SQ_reset(plugin->dbh,
349  stmt);
350  if (NULL != iter)
351  iter(iter_cls,
352  NULL,
353  err ? "sqlite error" : NULL);
354  return GNUNET_OK;
355 }
sqlite3_stmt * select_peerstoredata_by_key
Precompiled SQL for selecting from peerstoredata.
Description of a DB result cell.
sqlite3_stmt * select_peerstoredata
Precompiled SQL for selecting from peerstoredata.
sqlite3_stmt * select_peerstoredata_by_pid
Precompiled SQL for selecting from peerstoredata.
#define GNUNET_SQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
#define GNUNET_SQ_result_spec_end
End of result parameter specification.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_SQ_result_spec_auto_from_type(dst)
We expect a fixed-size result, with size determined by the type of * dst
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
int 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:82
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
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:117
#define LOG(kind,...)
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_variable_size(void **dst, size_t *sptr)
Variable-size result expected.
int GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:37
#define LOG_SQLITE(db, level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_string(const char *ptr)
Generate query parameter for a string.
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:54
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition: sq.c:132
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
sqlite3_stmt * select_peerstoredata_by_all
Precompiled SQL for selecting from peerstoredata.
Single PEERSTORE record.
#define GNUNET_SQ_query_param_end
End of query parameter specification.
Definition: gnunet_sq_lib.h:85
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_string(char **dst)
0-terminated string expected.
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 376 of file plugin_peerstore_sqlite.c.

References Plugin::dbh, 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(), Plugin::insert_peerstoredata, LOG_SQLITE, peerstore_sqlite_delete_records(), and plugin.

Referenced by libgnunet_plugin_peerstore_sqlite_init().

386 {
387  struct Plugin *plugin = cls;
388  sqlite3_stmt *stmt = plugin->insert_peerstoredata;
389  struct GNUNET_SQ_QueryParam params[] = {
390  GNUNET_SQ_query_param_string(sub_system),
396  };
397 
399  {
401  sub_system,
402  peer,
403  key);
404  }
405  if (GNUNET_OK !=
406  GNUNET_SQ_bind(stmt,
407  params))
408  LOG_SQLITE(plugin,
410  "sqlite3_bind");
411  else if (SQLITE_DONE != sqlite3_step(stmt))
412  {
413  LOG_SQLITE(plugin,
415  "sqlite3_step");
416  }
417  GNUNET_SQ_reset(plugin->dbh,
418  stmt);
419  if (NULL != cont)
420  cont(cont_cls,
421  GNUNET_OK);
422  return GNUNET_OK;
423 }
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.
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
Delete any previous values for the given key before storing the given value.
#define GNUNET_SQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
static char * value
Value of the record to add/remove.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
int GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:37
#define LOG_SQLITE(db, level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
struct GNUNET_HashCode key
The key used in the DHT.
static unsigned int size
Size of the "table".
Definition: peer.c:66
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_string(const char *ptr)
Generate query parameter for a string.
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:54
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition: sq.c:132
Handle for a plugin.
Definition: block.c:37
PGconn * dbh
Native Postgres database handle.
sqlite3_stmt * insert_peerstoredata
Precompiled SQL for inserting into peerstoredata.
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_SQ_query_param_end
End of query parameter specification.
Definition: gnunet_sq_lib.h:85
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 434 of file plugin_peerstore_sqlite.c.

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

Referenced by database_setup().

436 {
437  int result;
438 
439  result = sqlite3_exec(dbh,
440  sql,
441  NULL,
442  NULL,
443  NULL);
445  "Executed `%s' / %d\n",
446  sql,
447  result);
448  if (SQLITE_OK != result)
450  _("Error executing SQL query: %s\n %s\n"),
451  sqlite3_errmsg(dbh),
452  sql);
453  return result;
454 }
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define LOG(kind,...)
static int result
Global testing status.
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 466 of file plugin_peerstore_sqlite.c.

References _, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, LOG, result, and Plugin::tail.

Referenced by database_setup().

469 {
470  char *tail;
471  int result;
472 
473  result = sqlite3_prepare_v2(dbh,
474  sql,
475  strlen(sql),
476  stmt,
477  (const char **)&tail);
479  "Prepared `%s' / %p: %d\n",
480  sql,
481  *stmt,
482  result);
483  if (SQLITE_OK != result)
485  _("Error preparing SQL query: %s\n %s\n"),
486  sqlite3_errmsg(dbh),
487  sql);
488  return result;
489 }
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define LOG(kind,...)
static int result
Global testing status.
static struct PendingResolutions * tail
Tail of list of pending resolution requests.
Definition: gnunet-ats.c:233
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 501 of file plugin_peerstore_sqlite.c.

References _, BUSY_TIMEOUT_MS, Plugin::cfg, Plugin::dbh, Plugin::delete_peerstoredata, Plugin::expire_peerstoredata, filename, Plugin::fn, 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, Plugin::insert_peerstoredata, LOG, Plugin::select_peerstoredata, Plugin::select_peerstoredata_by_all, Plugin::select_peerstoredata_by_key, Plugin::select_peerstoredata_by_pid, sql_exec(), and sql_prepare().

Referenced by libgnunet_plugin_peerstore_sqlite_init().

502 {
503  char *filename;
504 
505  if (GNUNET_OK !=
507  "peerstore-sqlite",
508  "FILENAME",
509  &filename))
510  {
512  "peerstore-sqlite",
513  "FILENAME");
514  return GNUNET_SYSERR;
515  }
516  if (GNUNET_OK != GNUNET_DISK_file_test(filename))
517  {
519  {
520  GNUNET_break(0);
521  GNUNET_free(filename);
522  return GNUNET_SYSERR;
523  }
524  }
525  /* filename should be UTF-8-encoded. If it isn't, it's a bug */
526  plugin->fn = filename;
527  /* Open database and precompile statements */
528  if (SQLITE_OK != sqlite3_open(plugin->fn,
529  &plugin->dbh))
530  {
532  _("Unable to initialize SQLite: %s.\n"),
533  sqlite3_errmsg(plugin->dbh));
534  return GNUNET_SYSERR;
535  }
536  sql_exec(plugin->dbh,
537  "PRAGMA temp_store=MEMORY");
538  sql_exec(plugin->dbh,
539  "PRAGMA synchronous=OFF");
540  sql_exec(plugin->dbh,
541  "PRAGMA legacy_file_format=OFF");
542  sql_exec(plugin->dbh,
543  "PRAGMA auto_vacuum=INCREMENTAL");
544  sql_exec(plugin->dbh,
545  "PRAGMA encoding=\"UTF-8\"");
546  sql_exec(plugin->dbh,
547  "PRAGMA page_size=4096");
548  sqlite3_busy_timeout(plugin->dbh,
550  /* Create tables */
551  sql_exec(plugin->dbh,
552  "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
553  " sub_system TEXT NOT NULL,\n"
554  " peer_id BLOB NOT NULL,\n"
555  " key TEXT NOT NULL,\n"
556  " value BLOB NULL,\n"
557  " expiry INT8 NOT NULL" ");");
558  /* Create Indices */
559  if (SQLITE_OK !=
560  sqlite3_exec(plugin->dbh,
561  "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)",
562  NULL,
563  NULL,
564  NULL))
565  {
567  _("Unable to create indices: %s.\n"),
568  sqlite3_errmsg(plugin->dbh));
569  return GNUNET_SYSERR;
570  }
571  /* Prepare statements */
572 
573  sql_prepare(plugin->dbh,
574  "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry)"
575  " VALUES (?,?,?,?,?);",
576  &plugin->insert_peerstoredata);
577  sql_prepare(plugin->dbh,
578  "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
579  " WHERE sub_system = ?",
580  &plugin->select_peerstoredata);
581  sql_prepare(plugin->dbh,
582  "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
583  " WHERE sub_system = ?"
584  " AND peer_id = ?",
585  &plugin->select_peerstoredata_by_pid);
586  sql_prepare(plugin->dbh,
587  "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
588  " WHERE sub_system = ?"
589  " AND key = ?",
590  &plugin->select_peerstoredata_by_key);
591  sql_prepare(plugin->dbh,
592  "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
593  " WHERE sub_system = ?"
594  " AND peer_id = ?" " AND key = ?",
595  &plugin->select_peerstoredata_by_all);
596  sql_prepare(plugin->dbh,
597  "DELETE FROM peerstoredata"
598  " WHERE expiry < ?",
599  &plugin->expire_peerstoredata);
600  sql_prepare(plugin->dbh,
601  "DELETE FROM peerstoredata"
602  " WHERE sub_system = ?"
603  " AND peer_id = ?" " AND key = ?",
604  &plugin->delete_peerstoredata);
605  return GNUNET_OK;
606 }
int 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:541
static int sql_prepare(sqlite3 *dbh, const char *sql, sqlite3_stmt **stmt)
Prepare a SQL statement.
sqlite3_stmt * select_peerstoredata_by_key
Precompiled SQL for selecting from peerstoredata.
sqlite3_stmt * select_peerstoredata
Precompiled SQL for selecting from peerstoredata.
sqlite3_stmt * delete_peerstoredata
Precompiled SQL for deleting records with given key.
sqlite3_stmt * select_peerstoredata_by_pid
Precompiled SQL for selecting from peerstoredata.
static int sql_exec(sqlite3 *dbh, const char *sql)
Prepare a SQL statement.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
int GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition: disk.c:681
sqlite3_stmt * expire_peerstoredata
Precompiled SQL for deleting expired records from peerstoredata.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#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...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define LOG(kind,...)
static char * filename
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
char * fn
Filename used for the DB.
PGconn * dbh
Native Postgres database handle.
sqlite3_stmt * select_peerstoredata_by_all
Precompiled SQL for selecting from peerstoredata.
int 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.
sqlite3_stmt * insert_peerstoredata
Precompiled SQL for inserting into peerstoredata.
#define GNUNET_free(ptr)
Wrapper around free.
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 615 of file plugin_peerstore_sqlite.c.

References Plugin::dbh, Plugin::fn, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free_non_null, LOG, LOG_SQLITE, and result.

Referenced by libgnunet_plugin_peerstore_sqlite_done(), and libgnunet_plugin_peerstore_sqlite_init().

616 {
617  int result;
618  sqlite3_stmt *stmt;
619 
620  while (NULL != (stmt = sqlite3_next_stmt(plugin->dbh,
621  NULL)))
622  {
623  result = sqlite3_finalize(stmt);
624  if (SQLITE_OK != result)
626  "Failed to close statement %p: %d\n",
627  stmt,
628  result);
629  }
630  if (SQLITE_OK != sqlite3_close(plugin->dbh))
631  LOG_SQLITE(plugin,
633  "sqlite3_close");
634  GNUNET_free_non_null(plugin->fn);
635 }
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define LOG(kind,...)
static int result
Global testing status.
#define LOG_SQLITE(db, level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; on file &#39;file...
char * fn
Filename used for the DB.
PGconn * dbh
Native Postgres database handle.
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 645 of file plugin_peerstore_sqlite.c.

References Plugin::api, Plugin::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.

646 {
647  static struct Plugin plugin;
648  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
650 
651  if (NULL != plugin.cfg)
652  return NULL; /* can only initialize once! */
653  memset(&plugin,
654  0,
655  sizeof(struct Plugin));
656  plugin.cfg = cfg;
658  {
660  return NULL;
661  }
663  api->cls = &plugin;
668  "Sqlite plugin is running\n");
669  return api;
670 }
#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 int peerstore_sqlite_expire_records(void *cls, struct GNUNET_TIME_Absolute now, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Delete expired records (expiry < now)
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.
void * cls
Closure to pass to all plugin functions.
int(* expire_records)(void *cls, struct GNUNET_TIME_Absolute now, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Delete expired records (expiry < now)
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
#define LOG(kind,...)
struct returned by the initialization function of the plugin
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
int(* iterate_records)(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, GNUNET_PEERSTORE_Processor iter, void *iter_cls)
Iterate over the records given an optional peer id and/or key.
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.
configuration data
Definition: configuration.c:83
Handle for a plugin.
Definition: block.c:37
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, GNUNET_PEERSTORE_Processor 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...
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 680 of file plugin_peerstore_sqlite.c.

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

681 {
683  struct Plugin *plugin = api->cls;
684 
685  database_shutdown(plugin);
686  plugin->cfg = NULL;
687  GNUNET_free(api);
689  "Sqlite plugin is finished\n");
690  return NULL;
691 }
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
void * cls
Closure to pass to all plugin functions.
static struct GNUNET_ATS_SolverFunctions * plugin
Our solver.
#define LOG(kind,...)
struct returned by the initialization function of the plugin
Handle for a plugin.
Definition: block.c:37
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function: