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

131 {
132  struct Plugin *plugin = cls;
133  sqlite3_stmt *stmt = plugin->delete_peerstoredata;
134  struct GNUNET_SQ_QueryParam params[] = {
135  GNUNET_SQ_query_param_string (sub_system),
139  };
140  int ret;
141 
142  if (GNUNET_OK !=
143  GNUNET_SQ_bind (stmt,
144  params))
145  {
146  LOG_SQLITE (plugin,
148  "sqlite3_bind");
149  GNUNET_SQ_reset (plugin->dbh,
150  stmt);
151  return GNUNET_SYSERR;
152  }
153  if (SQLITE_DONE !=
154  sqlite3_step (stmt))
155  {
156  LOG_SQLITE (plugin,
158  "sqlite3_step");
159  ret = GNUNET_SYSERR;
160  }
161  else
162  {
163  ret = sqlite3_changes (plugin->dbh);
164  }
165  GNUNET_SQ_reset (plugin->dbh,
166  stmt);
167  return ret;
168 }
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:78
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:79
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:87
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 182 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().

185 {
186  struct Plugin *plugin = cls;
187  sqlite3_stmt *stmt = plugin->expire_peerstoredata;
188  struct GNUNET_SQ_QueryParam params[] = {
191  };
192 
193  if (GNUNET_OK !=
194  GNUNET_SQ_bind (stmt,
195  params))
196  {
197  LOG_SQLITE (plugin,
199  "sqlite3_bind");
200  GNUNET_SQ_reset (plugin->dbh,
201  stmt);
202  return GNUNET_SYSERR;
203  }
204  if (SQLITE_DONE != sqlite3_step (stmt))
205  {
206  LOG_SQLITE (plugin,
208  "sqlite3_step");
209  GNUNET_SQ_reset (plugin->dbh,
210  stmt);
211  return GNUNET_SYSERR;
212  }
213  if (NULL != cont)
214  cont (cont_cls,
215  sqlite3_changes (plugin->dbh));
216  GNUNET_SQ_reset (plugin->dbh,
217  stmt);
218  return GNUNET_OK;
219 }
sqlite3_stmt * expire_peerstoredata
Precompiled SQL for deleting expired records from peerstoredata.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
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:79
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:87
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 237 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().

243 {
244  struct Plugin *plugin = cls;
245  sqlite3_stmt *stmt;
246  int err = 0;
247  int sret;
248  struct GNUNET_PEERSTORE_Record rec;
249 
251  "Executing iterate request on sqlite db.\n");
252  if (NULL == peer)
253  {
254  if (NULL == key)
255  {
256  struct GNUNET_SQ_QueryParam params[] = {
257  GNUNET_SQ_query_param_string (sub_system),
259  };
260 
261  stmt = plugin->select_peerstoredata;
262  err = GNUNET_SQ_bind (stmt,
263  params);
264  }
265  else
266  {
267  struct GNUNET_SQ_QueryParam params[] = {
268  GNUNET_SQ_query_param_string (sub_system),
271  };
272 
273  stmt = plugin->select_peerstoredata_by_key;
274  err = GNUNET_SQ_bind (stmt,
275  params);
276  }
277  }
278  else
279  {
280  if (NULL == key)
281  {
282  struct GNUNET_SQ_QueryParam params[] = {
283  GNUNET_SQ_query_param_string (sub_system),
286  };
287 
288  stmt = plugin->select_peerstoredata_by_pid;
289  err = GNUNET_SQ_bind (stmt,
290  params);
291  }
292  else
293  {
294  struct GNUNET_SQ_QueryParam params[] = {
295  GNUNET_SQ_query_param_string (sub_system),
299  };
300 
301  stmt = plugin->select_peerstoredata_by_all;
302  err = GNUNET_SQ_bind (stmt,
303  params);
304  }
305  }
306 
307  if (GNUNET_OK != err)
308  {
309  LOG_SQLITE (plugin,
311  "sqlite3_bind_XXXX");
312  GNUNET_SQ_reset (plugin->dbh,
313  stmt);
314  return GNUNET_SYSERR;
315  }
316 
317  err = 0;
318  while (SQLITE_ROW == (sret = sqlite3_step (stmt)))
319  {
321  "Returning a matched record.\n");
322  struct GNUNET_SQ_ResultSpec rs[] = {
323  GNUNET_SQ_result_spec_string (&rec.sub_system),
325  GNUNET_SQ_result_spec_string (&rec.key),
326  GNUNET_SQ_result_spec_variable_size (&rec.value, &rec.value_size),
329  };
330 
331  if (GNUNET_OK !=
333  rs))
334  {
335  GNUNET_break (0);
336  break;
337  }
338  if (NULL != iter)
339  iter (iter_cls,
340  &rec,
341  NULL);
343  }
344  if (SQLITE_DONE != sret)
345  {
346  LOG_SQLITE (plugin,
348  "sqlite_step");
349  err = 1;
350  }
351  GNUNET_SQ_reset (plugin->dbh,
352  stmt);
353  if (NULL != iter)
354  iter (iter_cls,
355  NULL,
356  err ? "sqlite error" : NULL);
357  return GNUNET_OK;
358 }
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:78
#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:79
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:87
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 379 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().

389 {
390  struct Plugin *plugin = cls;
391  sqlite3_stmt *stmt = plugin->insert_peerstoredata;
392  struct GNUNET_SQ_QueryParam params[] = {
393  GNUNET_SQ_query_param_string (sub_system),
399  };
400 
402  {
404  sub_system,
405  peer,
406  key);
407  }
408  if (GNUNET_OK !=
409  GNUNET_SQ_bind (stmt,
410  params))
411  LOG_SQLITE (plugin,
413  "sqlite3_bind");
414  else if (SQLITE_DONE != sqlite3_step (stmt))
415  {
416  LOG_SQLITE (plugin,
418  "sqlite3_step");
419  }
420  GNUNET_SQ_reset (plugin->dbh,
421  stmt);
422  if (NULL != cont)
423  cont (cont_cls,
424  GNUNET_OK);
425  return GNUNET_OK;
426 }
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:78
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:67
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:87
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 437 of file plugin_peerstore_sqlite.c.

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

Referenced by database_setup().

439 {
440  int result;
441 
442  result = sqlite3_exec (dbh,
443  sql,
444  NULL,
445  NULL,
446  NULL);
448  "Executed `%s' / %d\n",
449  sql,
450  result);
451  if (SQLITE_OK != result)
453  _("Error executing SQL query: %s\n %s\n"),
454  sqlite3_errmsg (dbh),
455  sql);
456  return result;
457 }
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
#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 469 of file plugin_peerstore_sqlite.c.

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

Referenced by database_setup().

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

505 {
506  char *filename;
507 
508  if (GNUNET_OK !=
510  "peerstore-sqlite",
511  "FILENAME",
512  &filename))
513  {
515  "peerstore-sqlite",
516  "FILENAME");
517  return GNUNET_SYSERR;
518  }
519  if (GNUNET_OK != GNUNET_DISK_file_test (filename))
520  {
522  {
523  GNUNET_break (0);
524  GNUNET_free (filename);
525  return GNUNET_SYSERR;
526  }
527  }
528  /* filename should be UTF-8-encoded. If it isn't, it's a bug */
529  plugin->fn = filename;
530  /* Open database and precompile statements */
531  if (SQLITE_OK != sqlite3_open (plugin->fn,
532  &plugin->dbh))
533  {
535  _("Unable to initialize SQLite: %s.\n"),
536  sqlite3_errmsg (plugin->dbh));
537  return GNUNET_SYSERR;
538  }
539  sql_exec (plugin->dbh,
540  "PRAGMA temp_store=MEMORY");
541  sql_exec (plugin->dbh,
542  "PRAGMA synchronous=OFF");
543  sql_exec (plugin->dbh,
544  "PRAGMA legacy_file_format=OFF");
545  sql_exec (plugin->dbh,
546  "PRAGMA auto_vacuum=INCREMENTAL");
547  sql_exec (plugin->dbh,
548  "PRAGMA encoding=\"UTF-8\"");
549  sql_exec (plugin->dbh,
550  "PRAGMA page_size=4096");
551  sqlite3_busy_timeout (plugin->dbh,
553  /* Create tables */
554  sql_exec (plugin->dbh,
555  "CREATE TABLE IF NOT EXISTS peerstoredata (\n"
556  " sub_system TEXT NOT NULL,\n"
557  " peer_id BLOB NOT NULL,\n"
558  " key TEXT NOT NULL,\n"
559  " value BLOB NULL,\n"
560  " expiry INT8 NOT NULL" ");");
561  /* Create Indices */
562  if (SQLITE_OK !=
563  sqlite3_exec (plugin->dbh,
564  "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)",
565  NULL,
566  NULL,
567  NULL))
568  {
570  _("Unable to create indices: %s.\n"),
571  sqlite3_errmsg (plugin->dbh));
572  return GNUNET_SYSERR;
573  }
574  /* Prepare statements */
575 
576  sql_prepare (plugin->dbh,
577  "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry)"
578  " VALUES (?,?,?,?,?);",
579  &plugin->insert_peerstoredata);
580  sql_prepare (plugin->dbh,
581  "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
582  " WHERE sub_system = ?",
583  &plugin->select_peerstoredata);
584  sql_prepare (plugin->dbh,
585  "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
586  " WHERE sub_system = ?"
587  " AND peer_id = ?",
588  &plugin->select_peerstoredata_by_pid);
589  sql_prepare (plugin->dbh,
590  "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
591  " WHERE sub_system = ?"
592  " AND key = ?",
593  &plugin->select_peerstoredata_by_key);
594  sql_prepare (plugin->dbh,
595  "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
596  " WHERE sub_system = ?"
597  " AND peer_id = ?" " AND key = ?",
598  &plugin->select_peerstoredata_by_all);
599  sql_prepare (plugin->dbh,
600  "DELETE FROM peerstoredata"
601  " WHERE expiry < ?",
602  &plugin->expire_peerstoredata);
603  sql_prepare (plugin->dbh,
604  "DELETE FROM peerstoredata"
605  " WHERE sub_system = ?"
606  " AND peer_id = ?" " AND key = ?",
607  &plugin->delete_peerstoredata);
608  return GNUNET_OK;
609 }
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:669
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:833
sqlite3_stmt * expire_peerstoredata
Precompiled SQL for deleting expired records from peerstoredata.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#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:208
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:79
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 618 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().

619 {
620  int result;
621  sqlite3_stmt *stmt;
622 
623  while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh,
624  NULL)))
625  {
626  result = sqlite3_finalize (stmt);
627  if (SQLITE_OK != result)
629  "Failed to close statement %p: %d\n",
630  stmt,
631  result);
632  }
633  if (SQLITE_OK != sqlite3_close (plugin->dbh))
634  LOG_SQLITE (plugin,
636  "sqlite3_close");
637  GNUNET_free_non_null (plugin->fn);
638 }
#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 648 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.

649 {
650  static struct Plugin plugin;
651  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
653 
654  if (NULL != plugin.cfg)
655  return NULL; /* can only initialize once! */
656  memset (&plugin,
657  0,
658  sizeof (struct Plugin));
659  plugin.cfg = cfg;
660  if (GNUNET_OK != database_setup (&plugin))
661  {
663  return NULL;
664  }
666  api->cls = &plugin;
671  "Sqlite plugin is running\n");
672  return api;
673 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static 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:85
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 683 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.

684 {
686  struct Plugin *plugin = api->cls;
687 
688  database_shutdown (plugin);
689  plugin->cfg = NULL;
690  GNUNET_free (api);
692  "Sqlite plugin is finished\n");
693  return NULL;
694 }
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: