GNUnet  0.11.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)
 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 
)
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)
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:41
#define GNUNET_log_from(kind, comp,...)

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

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  {
151  LOG_SQLITE (plugin,
153  "sqlite3_bind");
154  GNUNET_SQ_reset (plugin->dbh,
155  stmt);
156  return GNUNET_SYSERR;
157  }
158  if (SQLITE_DONE !=
159  sqlite3_step (stmt))
160  {
161  LOG_SQLITE (plugin,
163  "sqlite3_step");
164  ret = GNUNET_SYSERR;
165  }
166  else
167  {
168  ret = sqlite3_changes (plugin->dbh);
169  }
170  GNUNET_SQ_reset (plugin->dbh,
171  stmt);
172  return ret;
173 }
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.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
int GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:37
static char * plugin
Solver plugin name as string.
#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
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
#define GNUNET_SQ_query_param_end
End of query parameter specification.
Definition: gnunet_sq_lib.h:86
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.

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

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  {
202  LOG_SQLITE (plugin,
204  "sqlite3_bind");
205  GNUNET_SQ_reset (plugin->dbh,
206  stmt);
207  return GNUNET_SYSERR;
208  }
209  if (SQLITE_DONE != sqlite3_step (stmt))
210  {
211  LOG_SQLITE (plugin,
213  "sqlite3_step");
214  GNUNET_SQ_reset (plugin->dbh,
215  stmt);
216  return GNUNET_SYSERR;
217  }
218  if (NULL != cont)
219  cont (cont_cls,
220  sqlite3_changes (plugin->dbh));
221  GNUNET_SQ_reset (plugin->dbh,
222  stmt);
223  return GNUNET_OK;
224 }
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
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
static char * plugin
Solver plugin name as string.
#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
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
#define GNUNET_SQ_query_param_end
End of query parameter specification.
Definition: gnunet_sq_lib.h:86
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 242 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().

248 {
249  struct Plugin *plugin = cls;
250  sqlite3_stmt *stmt;
251  int err = 0;
252  int sret;
253  struct GNUNET_PEERSTORE_Record rec;
254 
256  "Executing iterate request on sqlite db.\n");
257  if (NULL == peer)
258  {
259  if (NULL == key)
260  {
261  struct GNUNET_SQ_QueryParam params[] = {
262  GNUNET_SQ_query_param_string (sub_system),
264  };
265 
266  stmt = plugin->select_peerstoredata;
267  err = GNUNET_SQ_bind (stmt,
268  params);
269  }
270  else
271  {
272  struct GNUNET_SQ_QueryParam params[] = {
273  GNUNET_SQ_query_param_string (sub_system),
276  };
277 
278  stmt = plugin->select_peerstoredata_by_key;
279  err = GNUNET_SQ_bind (stmt,
280  params);
281  }
282  }
283  else
284  {
285  if (NULL == key)
286  {
287  struct GNUNET_SQ_QueryParam params[] = {
288  GNUNET_SQ_query_param_string (sub_system),
291  };
292 
293  stmt = plugin->select_peerstoredata_by_pid;
294  err = GNUNET_SQ_bind (stmt,
295  params);
296  }
297  else
298  {
299  struct GNUNET_SQ_QueryParam params[] = {
300  GNUNET_SQ_query_param_string (sub_system),
304  };
305 
306  stmt = plugin->select_peerstoredata_by_all;
307  err = GNUNET_SQ_bind (stmt,
308  params);
309  }
310  }
311 
312  if (GNUNET_OK != err)
313  {
314  LOG_SQLITE (plugin,
316  "sqlite3_bind_XXXX");
317  GNUNET_SQ_reset (plugin->dbh,
318  stmt);
319  return GNUNET_SYSERR;
320  }
321 
322  err = 0;
323  while (SQLITE_ROW == (sret = sqlite3_step (stmt)))
324  {
326  "Returning a matched record.\n");
327  struct GNUNET_SQ_ResultSpec rs[] = {
328  GNUNET_SQ_result_spec_string (&rec.sub_system),
330  GNUNET_SQ_result_spec_string (&rec.key),
331  GNUNET_SQ_result_spec_variable_size (&rec.value, &rec.value_size),
334  };
335 
336  if (GNUNET_OK !=
338  rs))
339  {
340  GNUNET_break (0);
341  break;
342  }
343  if (NULL != iter)
344  iter (iter_cls,
345  &rec,
346  NULL);
348  }
349  if (SQLITE_DONE != sret)
350  {
351  LOG_SQLITE (plugin,
353  "sqlite_step");
354  err = 1;
355  }
356  GNUNET_SQ_reset (plugin->dbh,
357  stmt);
358  if (NULL != iter)
359  iter (iter_cls,
360  NULL,
361  err ? "sqlite error" : NULL);
362  return GNUNET_OK;
363 }
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
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
static char * plugin
Solver plugin name as string.
#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
struct GNUNET_PQ_Context * 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:86
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 384 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().

394 {
395  struct Plugin *plugin = cls;
396  sqlite3_stmt *stmt = plugin->insert_peerstoredata;
397  struct GNUNET_SQ_QueryParam params[] = {
398  GNUNET_SQ_query_param_string (sub_system),
404  };
405 
407  {
409  sub_system,
410  peer,
411  key);
412  }
413  if (GNUNET_OK !=
414  GNUNET_SQ_bind (stmt,
415  params))
416  LOG_SQLITE (plugin,
418  "sqlite3_bind");
419  else if (SQLITE_DONE != sqlite3_step (stmt))
420  {
421  LOG_SQLITE (plugin,
423  "sqlite3_step");
424  }
425  GNUNET_SQ_reset (plugin->dbh,
426  stmt);
427  if (NULL != cont)
428  cont (cont_cls,
429  GNUNET_OK);
430  return GNUNET_OK;
431 }
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 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
static char * plugin
Solver plugin name as string.
#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
struct GNUNET_PQ_Context * 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:86
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 442 of file plugin_peerstore_sqlite.c.

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

Referenced by database_setup().

444 {
445  int result;
446 
447  result = sqlite3_exec (dbh,
448  sql,
449  NULL,
450  NULL,
451  NULL);
453  "Executed `%s' / %d\n",
454  sql,
455  result);
456  if (SQLITE_OK != result)
458  _ ("Error executing SQL query: %s\n %s\n"),
459  sqlite3_errmsg (dbh),
460  sql);
461  return result;
462 }
#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 474 of file plugin_peerstore_sqlite.c.

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

Referenced by database_setup().

477 {
478  char *tail;
479  int result;
480 
481  result = sqlite3_prepare_v2 (dbh,
482  sql,
483  strlen (sql),
484  stmt,
485  (const char **) &tail);
487  "Prepared `%s' / %p: %d\n",
488  sql,
489  *stmt,
490  result);
491  if (SQLITE_OK != result)
493  _ ("Error preparing SQL query: %s\n %s\n"),
494  sqlite3_errmsg (dbh),
495  sql);
496  return result;
497 }
#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:235
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 509 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().

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

624 {
625  int result;
626  sqlite3_stmt *stmt;
627 
628  while (NULL != (stmt = sqlite3_next_stmt (plugin->dbh,
629  NULL)))
630  {
631  result = sqlite3_finalize (stmt);
632  if (SQLITE_OK != result)
634  "Failed to close statement %p: %d\n",
635  stmt,
636  result);
637  }
638  if (SQLITE_OK != sqlite3_close (plugin->dbh))
639  LOG_SQLITE (plugin,
641  "sqlite3_close");
642  GNUNET_free_non_null (plugin->fn);
643 }
#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.
struct GNUNET_PQ_Context * 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 653 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.

654 {
655  static struct Plugin plugin;
656  const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
658 
659  if (NULL != plugin.cfg)
660  return NULL; /* can only initialize once! */
661  memset (&plugin,
662  0,
663  sizeof(struct Plugin));
664  plugin.cfg = cfg;
665  if (GNUNET_OK != database_setup (&plugin))
666  {
668  return NULL;
669  }
671  api->cls = &plugin;
676  "Sqlite plugin is running\n");
677  return api;
678 }
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
#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)
#define LOG(kind,...)
struct returned by the initialization function of the plugin
static char * plugin
Solver plugin name as string.
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 688 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.

689 {
691  struct Plugin *plugin = api->cls;
692 
693  database_shutdown (plugin);
694  plugin->cfg = NULL;
695  GNUNET_free (api);
697  "Sqlite plugin is finished\n");
698  return NULL;
699 }
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
void * cls
Closure to pass to all plugin functions.
#define LOG(kind,...)
struct returned by the initialization function of the plugin
static char * plugin
Solver plugin name as string.
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: