GNUnet  0.17.6
Data Structures | Macros | Functions
plugin_datacache_sqlite.c File Reference

sqlite for an implementation of a database backend for the datacache More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_datacache_plugin.h"
#include "gnunet_sq_lib.h"
#include <sqlite3.h>
Include dependency graph for plugin_datacache_sqlite.c:

Go to the source code of this file.

Data Structures

struct  Plugin
 Handle for a plugin. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "datacache-sqlite", __VA_ARGS__)
 
#define LOG_STRERROR_FILE(kind, op, fn)    GNUNET_log_from_strerror_file (kind, "datacache-sqlite", op, fn)
 
#define OVERHEAD   (sizeof(struct GNUNET_HashCode) + 36)
 How much overhead do we assume per entry in the datacache? More...
 
#define LOG_SQLITE(db, level, cmd)
 Log an error message at log-level level that indicates a failure of the command cmd with the error from the database db. More...
 
#define SQLITE3_EXEC(db, cmd)
 Execute SQL statement. More...
 

Functions

static int sq_prepare (sqlite3 *dbh, const char *zSql, sqlite3_stmt **ppStmt)
 Prepare a SQL statement. More...
 
static ssize_t sqlite_plugin_put (void *cls, uint32_t xor_distance, const struct GNUNET_DATACACHE_Block *block)
 Store an item in the datastore. More...
 
static unsigned int get_any (void *cls, const struct GNUNET_HashCode *key, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
 Iterate over the results for a particular key in the datastore. More...
 
static unsigned int get_typed (void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
 Iterate over the results for a particular key in the datastore. More...
 
static unsigned int sqlite_plugin_get (void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
 Iterate over the results for a particular key in the datastore. More...
 
static enum GNUNET_GenericReturnValue sqlite_plugin_del (void *cls)
 Delete the entry with the lowest expiration value from the datacache right now. More...
 
static unsigned int sqlite_plugin_get_closest (void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, unsigned int num_results, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
 Iterate over the results that are "close" to a particular key in the datacache. More...
 
void * libgnunet_plugin_datacache_sqlite_init (void *cls)
 Entry point for the plugin. More...
 
void * libgnunet_plugin_datacache_sqlite_done (void *cls)
 Exit point from the plugin. More...
 

Detailed Description

sqlite for an implementation of a database backend for the datacache

Author
Christian Grothoff

Definition in file plugin_datacache_sqlite.c.

Macro Definition Documentation

◆ LOG

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

Definition at line 32 of file plugin_datacache_sqlite.c.

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  op,
  fn 
)     GNUNET_log_from_strerror_file (kind, "datacache-sqlite", op, fn)

Definition at line 34 of file plugin_datacache_sqlite.c.

◆ OVERHEAD

#define OVERHEAD   (sizeof(struct GNUNET_HashCode) + 36)

How much overhead do we assume per entry in the datacache?

Definition at line 42 of file plugin_datacache_sqlite.c.

◆ LOG_SQLITE

#define LOG_SQLITE (   db,
  level,
  cmd 
)
Value:
do \
{ \
LOG (level, \
_ ("`%s' failed at %s:%d with error: %s\n"), \
cmd, \
__FILE__, \
__LINE__, \
sqlite3_errmsg (db)); \
} while (0)
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:94
#define _(String)
GNU gettext support macro.
Definition: platform.h:177

Log an error message at log-level level that indicates a failure of the command cmd with the error from the database db.

Parameters
dbdatabase handle
levellog level
cmdfailed command

Definition at line 124 of file plugin_datacache_sqlite.c.

◆ SQLITE3_EXEC

#define SQLITE3_EXEC (   db,
  cmd 
)
Value:
do \
{ \
emsg = NULL; \
if (SQLITE_OK != \
sqlite3_exec (db, cmd, NULL, NULL, &emsg)) \
{ \
_ ("`%s' failed at %s:%d with error: %s\n"), \
"sqlite3_exec", \
__FILE__, \
__LINE__, \
emsg); \
sqlite3_free (emsg); \
} \
} while (0)
@ GNUNET_ERROR_TYPE_ERROR

Execute SQL statement.

Parameters
dbdatabase handle
cmdSQL command to execute

Definition at line 142 of file plugin_datacache_sqlite.c.

Function Documentation

◆ sq_prepare()

static int sq_prepare ( sqlite3 *  dbh,
const char *  zSql,
sqlite3_stmt **  ppStmt 
)
static

Prepare a SQL statement.

Parameters
dbhdatabase handle
zsqlSQL statement text
[out]ppStmtset to the prepared statement
Returns
0 on success

Definition at line 169 of file plugin_datacache_sqlite.c.

172 { /* OUT: Statement handle */
173  char *dummy;
174 
175  return sqlite3_prepare (dbh,
176  zSql,
177  strlen (zSql),
178  ppStmt,
179  (const char **) &dummy);
180 }
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.

References dummy.

Referenced by libgnunet_plugin_datacache_sqlite_init().

Here is the caller graph for this function:

◆ sqlite_plugin_put()

static ssize_t sqlite_plugin_put ( void *  cls,
uint32_t  xor_distance,
const struct GNUNET_DATACACHE_Block block 
)
static

Store an item in the datastore.

Parameters
clsclosure (our struct Plugin)
xor_distancehow close is key to our PID?
blockdata to store
Returns
0 if duplicate, -1 on error, number of bytes used otherwise

Definition at line 192 of file plugin_datacache_sqlite.c.

195 {
196  struct Plugin *plugin = cls;
197  uint32_t type32 = (uint32_t) block->type;
198  uint32_t ro32 = (uint32_t) block->ro;
199  struct GNUNET_SQ_QueryParam params[] = {
204  GNUNET_SQ_query_param_uint32 (&xor_distance),
206  block->data_size),
208  block->put_path_length
209  * sizeof(struct GNUNET_DHT_PathElement)),
212  };
213 
215  "Processing PUT of %u bytes with key `%s' and expiration %s\n",
216  (unsigned int) block->data_size,
217  GNUNET_h2s (&block->key),
220  block->expiration_time),
221  GNUNET_YES));
222  if (GNUNET_OK !=
224  params))
225  {
228  "sqlite3_bind_xxx");
231  return -1;
232  }
233  if (SQLITE_DONE !=
234  sqlite3_step (plugin->insert_stmt))
235  {
238  "sqlite3_step");
241  return -1;
242  }
243  plugin->num_items++;
246  return block->data_size + OVERHEAD;
247 }
struct Plugin * plugin
The process handle to the testbed service.
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.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint32(const uint32_t *x)
Generate query parameter for an uint32_t in host byte order.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_absolute_time(const struct GNUNET_TIME_Absolute *x)
Generate query parameter for an absolute time value.
#define GNUNET_SQ_query_param_auto_from_type(x)
Generate fixed-size query parameter with size determined by variable type.
enum GNUNET_GenericReturnValue GNUNET_SQ_bind(sqlite3_stmt *stmt, const struct GNUNET_SQ_QueryParam *params)
Execute binding operations for a prepared statement.
Definition: sq.c:37
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
Definition: sq.c:132
#define GNUNET_SQ_query_param_end
End of query parameter specification.
Definition: gnunet_sq_lib.h:86
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_YES
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
Definition: time.c:404
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:570
#define OVERHEAD
How much overhead do we assume per entry in the datacache?
#define LOG_SQLITE(db, level, cmd)
Log an error message at log-level level that indicates a failure of the command cmd with the error fr...
#define LOG(kind,...)
const struct GNUNET_DHT_PathElement * put_path
PUT path taken by the block, array of peer identities.
enum GNUNET_BLOCK_Type type
Type of the block.
const void * data
Actual block data.
enum GNUNET_DHT_RouteOption ro
Options for routing for the block.
struct GNUNET_PeerIdentity trunc_peer
If the path was truncated, this is the peer ID at which the path was truncated.
struct GNUNET_HashCode key
Key of the block.
size_t data_size
Number of bytes in data.
unsigned int put_path_length
Length of the put_path array.
struct GNUNET_TIME_Absolute expiration_time
When does the block expire?
A (signed) path tracking a block's flow through the DHT is represented by an array of path elements,...
Description of a DB query parameter.
Definition: gnunet_sq_lib.h:55
Handle for a plugin.
Definition: block.c:38
unsigned int num_items
Number of key-value pairs in the database.
sqlite3_stmt * insert_stmt
Prepared statement for sqlite_plugin_put.
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.

References GNUNET_DATACACHE_Block::data, GNUNET_DATACACHE_Block::data_size, Plugin::dbh, GNUNET_DATACACHE_Block::expiration_time, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_h2s(), GNUNET_OK, 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_uint32(), GNUNET_SQ_reset(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_absolute_get_remaining(), GNUNET_YES, Plugin::insert_stmt, GNUNET_DATACACHE_Block::key, LOG, LOG_SQLITE, Plugin::num_items, OVERHEAD, plugin, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, GNUNET_DATACACHE_Block::ro, GNUNET_DATACACHE_Block::trunc_peer, and GNUNET_DATACACHE_Block::type.

Referenced by libgnunet_plugin_datacache_sqlite_init().

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

◆ get_any()

static unsigned int get_any ( void *  cls,
const struct GNUNET_HashCode key,
GNUNET_DATACACHE_Iterator  iter,
void *  iter_cls 
)
static

Iterate over the results for a particular key in the datastore.

Parameters
clsclosure (our struct Plugin)
key
itermaybe NULL (to just count)
iter_clsclosure for iter
Returns
the number of results found

Definition at line 261 of file plugin_datacache_sqlite.c.

265 {
266  struct Plugin *plugin = cls;
267  struct GNUNET_TIME_Absolute now;
268  unsigned int cnt;
269  uint32_t off;
270  uint32_t btype32;
271  uint32_t bro32;
272  unsigned int total;
273  struct GNUNET_DATACACHE_Block block;
274  void *path;
275  void *data;
276  size_t path_size;
277  struct GNUNET_SQ_QueryParam params_count[] = {
281  };
282  struct GNUNET_SQ_QueryParam params_select[] = {
287  };
288  struct GNUNET_SQ_ResultSpec rs[] = {
290  &block.data_size),
291  GNUNET_SQ_result_spec_absolute_time (&block.expiration_time),
293  &path_size),
294  GNUNET_SQ_result_spec_auto_from_type (&block.trunc_peer),
295  GNUNET_SQ_result_spec_uint32 (&btype32),
298  };
299 
300  now = GNUNET_TIME_absolute_get ();
302  "Processing GET for key `%s'\n",
303  GNUNET_h2s (key));
304 
305  if (GNUNET_OK !=
307  params_count))
308  {
311  "sqlite3_bind_xxx");
314  return 0;
315  }
316  if (SQLITE_ROW !=
317  sqlite3_step (plugin->get_count_any_stmt))
318  {
321  "sqlite_step");
325  "No content found when processing GET for key `%s'\n",
326  GNUNET_h2s (key));
327  return 0;
328  }
329  total = sqlite3_column_int (plugin->get_count_any_stmt,
330  0);
333  if ( (0 == total) ||
334  (NULL == iter) )
335  {
336  if (0 == total)
338  "No content found when processing GET for key `%s'\n",
339  GNUNET_h2s (key));
340  return total;
341  }
342 
343  cnt = 0;
344  block.key = *key;
346  total);
347  while (cnt < total)
348  {
349  off = (off + 1) % total;
350  if (GNUNET_OK !=
352  params_select))
353  {
356  "sqlite3_bind_xxx");
359  return cnt;
360  }
361  if (SQLITE_ROW !=
362  sqlite3_step (plugin->get_any_stmt))
363  break;
364  if (GNUNET_OK !=
366  rs))
367  {
368  GNUNET_break (0);
371  break;
372  }
373  if (0 != path_size % sizeof(struct GNUNET_DHT_PathElement))
374  {
375  GNUNET_break (0);
376  path_size = 0;
377  path = NULL;
378  }
379  block.data = data;
380  block.put_path = path;
381  block.put_path_length = path_size / sizeof(struct GNUNET_DHT_PathElement);
382  block.type = (enum GNUNET_BLOCK_Type) btype32;
383  block.ro = (enum GNUNET_DHT_RouteOption) bro32;
384  cnt++;
386  "Found %u-byte result when processing GET for key `%s'\n",
387  (unsigned int) block.data_size,
388  GNUNET_h2s (&block.key));
389  if (GNUNET_OK !=
390  iter (iter_cls,
391  &block))
392  {
396  break;
397  }
401  }
404  return cnt;
405 }
GNUNET_BLOCK_Type
WARNING: This header is generated! In order to add DHT block types, you must register them in GANA,...
struct GNUNET_HashCode key
The key used in the DHT.
uint32_t data
The data value.
enum GNUNET_GenericReturnValue GNUNET_SQ_extract_result(sqlite3_stmt *result, struct GNUNET_SQ_ResultSpec *rs)
Extract results from a query result according to the given specification.
Definition: sq.c: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
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint32(uint32_t *u32)
uint32_t expected.
#define GNUNET_SQ_result_spec_end
End of result parameter specification.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_variable_size(void **dst, size_t *sptr)
Variable-size result expected.
#define GNUNET_SQ_result_spec_auto_from_type(dst)
We expect a fixed-size result, with size determined by the type of * dst
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
GNUNET_DHT_RouteOption
Options for routing.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
Definition: time.c:110
Information about a block stored in the datacache.
Description of a DB result cell.
Time for absolute times used by GNUnet, in microseconds.
sqlite3_stmt * get_any_stmt
Prepared statement for sqlite_plugin_get.
sqlite3_stmt * get_count_any_stmt
Prepared statement for sqlite_plugin_get.

References GNUNET_DATACACHE_Block::data, data, GNUNET_DATACACHE_Block::data_size, Plugin::dbh, GNUNET_DATACACHE_Block::expiration_time, Plugin::get_any_stmt, Plugin::get_count_any_stmt, GNUNET_break, GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u32(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_h2s(), GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_cleanup_result(), GNUNET_SQ_extract_result(), GNUNET_SQ_query_param_absolute_time(), GNUNET_SQ_query_param_auto_from_type, GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_uint32(), 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_uint32(), GNUNET_SQ_result_spec_variable_size(), GNUNET_TIME_absolute_get(), key, GNUNET_DATACACHE_Block::key, LOG, LOG_SQLITE, plugin, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, GNUNET_DATACACHE_Block::ro, GNUNET_DATACACHE_Block::trunc_peer, and GNUNET_DATACACHE_Block::type.

Referenced by sqlite_plugin_del().

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

◆ get_typed()

static unsigned int get_typed ( void *  cls,
const struct GNUNET_HashCode key,
enum GNUNET_BLOCK_Type  type,
GNUNET_DATACACHE_Iterator  iter,
void *  iter_cls 
)
static

Iterate over the results for a particular key in the datastore.

Parameters
clsclosure (our struct Plugin)
key
typeentries of which type are relevant?
itermaybe NULL (to just count)
iter_clsclosure for iter
Returns
the number of results found

Definition at line 420 of file plugin_datacache_sqlite.c.

425 {
426  struct Plugin *plugin = cls;
427  uint32_t type32 = type;
428  struct GNUNET_TIME_Absolute now;
429  unsigned int cnt;
430  uint32_t off;
431  uint32_t bro32;
432  unsigned int total;
433  struct GNUNET_DATACACHE_Block block;
434  void *path;
435  void *data;
436  size_t path_size;
437  struct GNUNET_SQ_QueryParam params_count[] = {
442  };
443  struct GNUNET_SQ_QueryParam params_select[] = {
449  };
450  struct GNUNET_SQ_ResultSpec rs[] = {
452  &block.data_size),
453  GNUNET_SQ_result_spec_absolute_time (&block.expiration_time),
455  &path_size),
456  GNUNET_SQ_result_spec_auto_from_type (&block.trunc_peer),
459  };
460 
461  now = GNUNET_TIME_absolute_get ();
463  "Processing GET for key `%s'\n",
464  GNUNET_h2s (key));
465 
466  if (GNUNET_OK !=
468  params_count))
469  {
472  "sqlite3_bind_xxx");
475  return 0;
476  }
477  if (SQLITE_ROW !=
478  sqlite3_step (plugin->get_count_stmt))
479  {
482  "sqlite_step");
486  "No content found when processing GET for key `%s'\n",
487  GNUNET_h2s (key));
488  return 0;
489  }
490  total = sqlite3_column_int (plugin->get_count_stmt,
491  0);
494  if ( (0 == total) ||
495  (NULL == iter) )
496  {
497  if (0 == total)
499  "No content found when processing GET for key `%s'\n",
500  GNUNET_h2s (key));
501  return total;
502  }
503 
504  cnt = 0;
505  block.key = *key;
507  total);
508  while (cnt < total)
509  {
510  off = (off + 1) % total;
511  if (GNUNET_OK !=
513  params_select))
514  {
517  "sqlite3_bind_xxx");
519  plugin->get_stmt);
520  return cnt;
521  }
522  if (SQLITE_ROW !=
523  sqlite3_step (plugin->get_stmt))
524  break;
525  if (GNUNET_OK !=
527  rs))
528  {
529  GNUNET_break (0);
531  plugin->get_stmt);
532  break;
533  }
534  if (0 != path_size % sizeof(struct GNUNET_DHT_PathElement))
535  {
536  GNUNET_break (0);
537  path_size = 0;
538  path = NULL;
539  }
540  block.data = data;
541  block.put_path = path;
542  block.put_path_length = path_size / sizeof(struct GNUNET_DHT_PathElement);
543  block.type = type;
544  block.ro = (enum GNUNET_DHT_RouteOption) bro32;
545  cnt++;
547  "Found %u-byte result when processing GET for key `%s'\n",
548  (unsigned int) block.data_size,
549  GNUNET_h2s (&block.key));
550  if ( (NULL != iter) &&
551  (GNUNET_OK !=
552  iter (iter_cls,
553  &block)) )
554  {
557  plugin->get_stmt);
558  break;
559  }
562  plugin->get_stmt);
563  }
565  plugin->get_stmt);
566  return cnt;
567 }
sqlite3_stmt * get_stmt
Prepared statement for sqlite_plugin_get.
sqlite3_stmt * get_count_stmt
Prepared statement for sqlite_plugin_get.
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

References GNUNET_DATACACHE_Block::data, data, GNUNET_DATACACHE_Block::data_size, Plugin::dbh, GNUNET_DATACACHE_Block::expiration_time, Plugin::get_count_stmt, Plugin::get_stmt, GNUNET_break, GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u32(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_h2s(), GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_cleanup_result(), GNUNET_SQ_extract_result(), GNUNET_SQ_query_param_absolute_time(), GNUNET_SQ_query_param_auto_from_type, GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_uint32(), 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_uint32(), GNUNET_SQ_result_spec_variable_size(), GNUNET_TIME_absolute_get(), key, GNUNET_DATACACHE_Block::key, LOG, LOG_SQLITE, plugin, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, GNUNET_DATACACHE_Block::ro, GNUNET_DATACACHE_Block::trunc_peer, GNUNET_DATACACHE_Block::type, and type.

Referenced by sqlite_plugin_del().

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

◆ sqlite_plugin_get()

static unsigned int sqlite_plugin_get ( void *  cls,
const struct GNUNET_HashCode key,
enum GNUNET_BLOCK_Type  type,
GNUNET_DATACACHE_Iterator  iter,
void *  iter_cls 
)
static

Iterate over the results for a particular key in the datastore.

Parameters
clsclosure (our struct Plugin)
key
typeentries of which type are relevant?
itermaybe NULL (to just count)
iter_clsclosure for iter
Returns
the number of results found

Definition at line 582 of file plugin_datacache_sqlite.c.

587 {
589  return get_any (cls,
590  key,
591  iter,
592  iter_cls);
593  return get_typed (cls,
594  key,
595  type,
596  iter,
597  iter_cls);
598 }
@ GNUNET_BLOCK_TYPE_ANY
Identifier for any block.
static unsigned int get_any(void *cls, const struct GNUNET_HashCode *key, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datastore.
static unsigned int get_typed(void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datastore.

Referenced by libgnunet_plugin_datacache_sqlite_init().

Here is the caller graph for this function:

◆ sqlite_plugin_del()

static enum GNUNET_GenericReturnValue sqlite_plugin_del ( void *  cls)
static

Delete the entry with the lowest expiration value from the datacache right now.

Parameters
clsclosure (our struct Plugin)
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 582 of file plugin_datacache_sqlite.c.

610 {
611  struct Plugin *plugin = cls;
612  uint64_t rowid;
613  void *data;
614  size_t data_size;
615  struct GNUNET_HashCode hc;
616  struct GNUNET_TIME_Absolute now;
617  struct GNUNET_SQ_ResultSpec rs[] = {
621  &data_size),
623  };
624  struct GNUNET_SQ_QueryParam params[] = {
627  };
628  struct GNUNET_SQ_QueryParam time_params[] = {
631  };
632 
634  "Processing DEL\n");
635  now = GNUNET_TIME_absolute_get ();
636  if (GNUNET_OK !=
638  time_params))
639  {
642  "sqlite3_bind");
645  return GNUNET_SYSERR;
646  }
647  if ( (SQLITE_ROW !=
648  sqlite3_step (plugin->del_expired_stmt)) ||
649  (GNUNET_OK !=
651  rs)))
652  {
655  if (SQLITE_ROW !=
656  sqlite3_step (plugin->del_select_stmt))
657  {
660  "sqlite3_step");
663  return GNUNET_SYSERR;
664  }
665  if (GNUNET_OK !=
667  rs))
668  {
671  GNUNET_break (0);
672  return GNUNET_SYSERR;
673  }
674  }
678  if (GNUNET_OK !=
680  params))
681  {
684  "sqlite3_bind");
686  plugin->del_stmt);
687  return GNUNET_SYSERR;
688  }
689  if (SQLITE_DONE !=
690  sqlite3_step (plugin->del_stmt))
691  {
694  "sqlite3_step");
696  plugin->del_stmt);
697  return GNUNET_SYSERR;
698  }
699  plugin->num_items--;
701  &hc,
702  data_size + OVERHEAD);
704  plugin->del_stmt);
705  return GNUNET_OK;
706 }
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_uint64(const uint64_t *x)
Generate query parameter for an uint16_t in host byte order.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_uint64(uint64_t *u64)
uint64_t expected.
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
@ GNUNET_ERROR_TYPE_BULK
GNUNET_DATACACHE_DeleteNotifyCallback delete_notify
Function to call whenever the plugin needs to discard content that it was asked to store.
void * cls
Closure to use for callbacks.
A 512-bit hashcode.
sqlite3_stmt * del_select_stmt
Prepared statement for sqlite_plugin_del.
sqlite3_stmt * del_expired_stmt
Prepared statement for sqlite_plugin_del.
sqlite3_stmt * del_stmt
Prepared statement for sqlite_plugin_del.
struct GNUNET_DATACACHE_PluginEnvironment * env
Our execution environment.

References get_any(), get_typed(), GNUNET_BLOCK_TYPE_ANY, key, and type.

Referenced by libgnunet_plugin_datacache_sqlite_init().

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

◆ sqlite_plugin_get_closest()

static unsigned int sqlite_plugin_get_closest ( void *  cls,
const struct GNUNET_HashCode key,
enum GNUNET_BLOCK_Type  type,
unsigned int  num_results,
GNUNET_DATACACHE_Iterator  iter,
void *  iter_cls 
)
static

Iterate over the results that are "close" to a particular key in the datacache.

"close" is defined as numerically larger than key (when interpreted as a circular address space), with small distance.

Parameters
clsclosure (internal context for the plugin)
keyarea of the keyspace to look into
typedesired block type for the replies
num_resultsnumber of results that should be returned to iter
itermaybe NULL (to just count)
iter_clsclosure for iter
Returns
the number of results found

Definition at line 724 of file plugin_datacache_sqlite.c.

730 {
731  struct Plugin *plugin = cls;
732  uint32_t type32 = type;
733  uint32_t num_results32 = num_results;
734  struct GNUNET_TIME_Absolute now;
735  void *data;
736  void *path;
737  size_t path_size;
738  unsigned int cnt;
739  uint32_t bro32;
740  struct GNUNET_DATACACHE_Block block;
741  uint32_t rtype32;
742  struct GNUNET_SQ_QueryParam params[] = {
746  GNUNET_SQ_query_param_uint32 (&num_results32),
748  };
749  struct GNUNET_SQ_ResultSpec rs[] = {
751  &block.data_size),
752  GNUNET_SQ_result_spec_absolute_time (&block.expiration_time),
754  &path_size),
755  GNUNET_SQ_result_spec_auto_from_type (&block.trunc_peer),
756  GNUNET_SQ_result_spec_uint32 (&rtype32),
760  };
761 
762  now = GNUNET_TIME_absolute_get ();
764  "Processing GET_CLOSEST for key `%s'\n",
765  GNUNET_h2s (key));
766  if (GNUNET_OK !=
768  params))
769  {
772  "sqlite3_bind_xxx");
775  return 0;
776  }
777  cnt = 0;
778  while (SQLITE_ROW ==
779  sqlite3_step (plugin->get_closest_stmt))
780  {
781  if (GNUNET_OK !=
783  rs))
784  {
785  GNUNET_break (0);
786  break;
787  }
788  if (0 != path_size % sizeof(struct GNUNET_DHT_PathElement))
789  {
790  GNUNET_break (0);
791  path_size = 0;
792  path = NULL;
793  }
794  block.put_path_length
795  = path_size / sizeof(struct GNUNET_DHT_PathElement);
796  block.put_path = path;
797  block.data = data;
798  block.type = (enum GNUNET_BLOCK_Type) rtype32;
799  block.ro = (enum GNUNET_DHT_RouteOption) bro32;
800  cnt++;
802  "Found %u-byte result at %s when processing GET_CLOSE\n",
803  (unsigned int) block.data_size,
804  GNUNET_h2s (&block.key));
805 
806  if (GNUNET_OK !=
807  iter (iter_cls,
808  &block))
809  {
811  break;
812  }
814  }
817  return cnt;
818 }
sqlite3_stmt * get_closest_stmt
Prepared statement for sqlite_plugin_get_closest.

References GNUNET_DATACACHE_Block::data, data, GNUNET_DATACACHE_Block::data_size, Plugin::dbh, GNUNET_DATACACHE_Block::expiration_time, Plugin::get_closest_stmt, GNUNET_break, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_h2s(), GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_cleanup_result(), GNUNET_SQ_extract_result(), GNUNET_SQ_query_param_absolute_time(), GNUNET_SQ_query_param_auto_from_type, GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_uint32(), 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_uint32(), GNUNET_SQ_result_spec_variable_size(), GNUNET_TIME_absolute_get(), key, GNUNET_DATACACHE_Block::key, LOG, LOG_SQLITE, plugin, GNUNET_DATACACHE_Block::put_path, GNUNET_DATACACHE_Block::put_path_length, GNUNET_DATACACHE_Block::ro, GNUNET_DATACACHE_Block::trunc_peer, GNUNET_DATACACHE_Block::type, and type.

Referenced by libgnunet_plugin_datacache_sqlite_init().

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

◆ libgnunet_plugin_datacache_sqlite_init()

void* libgnunet_plugin_datacache_sqlite_init ( void *  cls)

Entry point for the plugin.

Parameters
clsclosure (the struct GNUNET_DATACACHE_PluginEnvironment)
Returns
the plugin's closure (our struct Plugin)

Definition at line 828 of file plugin_datacache_sqlite.c.

829 {
832  struct Plugin *plugin;
833  char *fn;
834  char *fn_utf8;
835  sqlite3 *dbh;
836  char *emsg;
837 
838  if (GNUNET_YES ==
840  "datacache-sqlite",
841  "IN_MEMORY"))
842  {
843  if (SQLITE_OK !=
844  sqlite3_open (":memory:",
845  &dbh))
846  return NULL;
847  fn_utf8 = NULL;
848  }
849  else
850  {
851  fn = GNUNET_DISK_mktemp ("gnunet-datacache");
852  if (NULL == fn)
853  {
854  GNUNET_break (0);
855  return NULL;
856  }
857  /* fn should be UTF-8-encoded. If it isn't, it's a bug. */
858  fn_utf8 = GNUNET_strdup (fn);
859  if (SQLITE_OK !=
860  sqlite3_open (fn_utf8,
861  &dbh))
862  {
863  GNUNET_free (fn);
864  GNUNET_free (fn_utf8);
865  return NULL;
866  }
867  GNUNET_free (fn);
868  }
869 
870  SQLITE3_EXEC (dbh, "PRAGMA temp_store=MEMORY");
871  SQLITE3_EXEC (dbh, "PRAGMA locking_mode=EXCLUSIVE");
872  SQLITE3_EXEC (dbh, "PRAGMA journal_mode=OFF");
873  SQLITE3_EXEC (dbh, "PRAGMA synchronous=OFF");
874  SQLITE3_EXEC (dbh, "PRAGMA page_size=4092");
875  if (GNUNET_YES ==
877  "datacache-sqlite",
878  "IN_MEMORY"))
879  SQLITE3_EXEC (dbh, "PRAGMA sqlite_temp_store=3");
880 
881  SQLITE3_EXEC (dbh,
882  "CREATE TABLE ds180 ("
883  " type INTEGER NOT NULL DEFAULT 0,"
884  " ro INTEGER NOT NULL DEFAULT 0,"
885  " expire INTEGER NOT NULL,"
886  " key BLOB NOT NULL DEFAULT '',"
887  " prox INTEGER NOT NULL,"
888  " value BLOB NOT NULL,"
889  " trunc BLOB NOT NULL,"
890  " path BLOB DEFAULT '')");
891  SQLITE3_EXEC (dbh,
892  "CREATE INDEX idx_hashidx"
893  " ON ds180 (key,type,expire)");
894  SQLITE3_EXEC (dbh,
895  "CREATE INDEX idx_prox_expire"
896  " ON ds180 (prox,expire)");
897  SQLITE3_EXEC (dbh,
898  "CREATE INDEX idx_expire_only"
899  " ON ds180 (expire)");
900  plugin = GNUNET_new (struct Plugin);
901  plugin->env = env;
902  plugin->dbh = dbh;
903  plugin->fn = fn_utf8;
904 
905  if ((SQLITE_OK !=
907  "INSERT INTO ds180"
908  " (type, ro, expire, key, prox, value, path, trunc)"
909  " VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
910  &plugin->insert_stmt)) ||
911  (SQLITE_OK !=
913  "SELECT COUNT(*) FROM ds180 "
914  "WHERE key=?"
915  " AND type=?"
916  " AND expire >= ?",
917  &plugin->get_count_stmt)) ||
918  (SQLITE_OK !=
920  "SELECT COUNT(*) FROM ds180 "
921  "WHERE key=? AND expire >= ?",
923  (SQLITE_OK !=
925  "SELECT value,expire,path,trunc,ro"
926  " FROM ds180"
927  " WHERE key=?"
928  " AND type=?"
929  " AND expire >= ?"
930  " LIMIT 1 OFFSET ?",
931  &plugin->get_stmt)) ||
932  (SQLITE_OK !=
934  "SELECT value,expire,path,trunc,type,ro"
935  " FROM ds180"
936  " WHERE key=?"
937  " AND expire >= ?"
938  " LIMIT 1 OFFSET ?",
939  &plugin->get_any_stmt)) ||
940  (SQLITE_OK !=
942  "SELECT _ROWID_,key,value FROM ds180"
943  " WHERE expire < ?1"
944  " ORDER BY expire ASC LIMIT 1",
945  &plugin->del_expired_stmt)) ||
946  (SQLITE_OK !=
948  "SELECT _ROWID_,key,value FROM ds180"
949  " ORDER BY prox ASC, expire ASC LIMIT 1",
950  &plugin->del_select_stmt)) ||
951  (SQLITE_OK !=
953  "DELETE FROM ds180 WHERE _ROWID_=?",
954  &plugin->del_stmt)) ||
955  (SQLITE_OK !=
957  "SELECT * FROM ("
958  " SELECT value,expire,path,trunc,type,ro,key"
959  " FROM ds180 "
960  " WHERE key>=?1 "
961  " AND expire >= ?2"
962  " AND ( (type=?3) or (0 == ?3) )"
963  " ORDER BY KEY ASC LIMIT ?4)"
964  "UNION "
965  "SELECT * FROM ("
966  " SELECT value,expire,path,trunc,type,ro,key"
967  " FROM ds180 "
968  " WHERE key<=?1 "
969  " AND expire >= ?2"
970  " AND ( (type=?3) or (0 == ?3) )"
971  " ORDER BY KEY DESC LIMIT ?4)",
973  {
976  "sq_prepare");
977  GNUNET_break (SQLITE_OK ==
978  sqlite3_close (plugin->dbh));
980  return NULL;
981  }
982 
984  api->cls = plugin;
985  api->get = &sqlite_plugin_get;
986  api->put = &sqlite_plugin_put;
987  api->del = &sqlite_plugin_del;
988  api->get_closest = &sqlite_plugin_get_closest;
990  "Sqlite datacache running\n");
991  return api;
992 }
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
char * GNUNET_DISK_mktemp(const char *t)
Create an (empty) temporary file on disk.
Definition: disk.c:381
@ GNUNET_ERROR_TYPE_INFO
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
static ssize_t sqlite_plugin_put(void *cls, uint32_t xor_distance, const struct GNUNET_DATACACHE_Block *block)
Store an item in the datastore.
#define SQLITE3_EXEC(db, cmd)
Execute SQL statement.
static unsigned int sqlite_plugin_get(void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results for a particular key in the datastore.
static int sq_prepare(sqlite3 *dbh, const char *zSql, sqlite3_stmt **ppStmt)
Prepare a SQL statement.
static enum GNUNET_GenericReturnValue sqlite_plugin_del(void *cls)
Delete the entry with the lowest expiration value from the datacache right now.
static unsigned int sqlite_plugin_get_closest(void *cls, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, unsigned int num_results, GNUNET_DATACACHE_Iterator iter, void *iter_cls)
Iterate over the results that are "close" to a particular key in the datacache.
void * cls
Closure for all of the callbacks.
The datastore service will pass a pointer to a struct of this type as the first and only argument to ...
struct returned by the initialization function of the plugin
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
Definition: block.c:47
char * fn
Filename used for the DB.

References Plugin::api, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_DATACACHE_PluginEnvironment::cls, Plugin::dbh, Plugin::del_expired_stmt, Plugin::del_select_stmt, Plugin::del_stmt, env, Plugin::env, Plugin::fn, Plugin::get_any_stmt, Plugin::get_closest_stmt, Plugin::get_count_any_stmt, Plugin::get_count_stmt, Plugin::get_stmt, GNUNET_break, GNUNET_CONFIGURATION_get_value_yesno(), GNUNET_DISK_mktemp(), GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_new, GNUNET_strdup, GNUNET_YES, Plugin::insert_stmt, LOG, LOG_SQLITE, plugin, sq_prepare(), SQLITE3_EXEC, sqlite_plugin_del(), sqlite_plugin_get(), sqlite_plugin_get_closest(), and sqlite_plugin_put().

Here is the call graph for this function:

◆ libgnunet_plugin_datacache_sqlite_done()

void* libgnunet_plugin_datacache_sqlite_done ( void *  cls)

Exit point from the plugin.

Parameters
clsclosure (our struct Plugin)
Returns
NULL

Definition at line 1002 of file plugin_datacache_sqlite.c.

1003 {
1004  struct GNUNET_DATACACHE_PluginFunctions *api = cls;
1005  struct Plugin *plugin = api->cls;
1006  int result;
1007 
1008 #if SQLITE_VERSION_NUMBER >= 3007000
1009  sqlite3_stmt *stmt;
1010 #endif
1011 
1012 #if ! WINDOWS || defined(__CYGWIN__)
1013  if ( (NULL != plugin->fn) &&
1014  (0 != unlink (plugin->fn)) )
1016  "unlink",
1017  plugin->fn);
1018  GNUNET_free (plugin->fn);
1019 #endif
1020  sqlite3_finalize (plugin->insert_stmt);
1021  sqlite3_finalize (plugin->get_count_stmt);
1022  sqlite3_finalize (plugin->get_count_any_stmt);
1023  sqlite3_finalize (plugin->get_stmt);
1024  sqlite3_finalize (plugin->get_any_stmt);
1025  sqlite3_finalize (plugin->del_select_stmt);
1026  sqlite3_finalize (plugin->del_expired_stmt);
1027  sqlite3_finalize (plugin->del_stmt);
1028  sqlite3_finalize (plugin->get_closest_stmt);
1029  result = sqlite3_close (plugin->dbh);
1030 #if SQLITE_VERSION_NUMBER >= 3007000
1031  if (SQLITE_BUSY == result)
1032  {
1034  _ (
1035  "Tried to close sqlite without finalizing all prepared statements.\n"));
1036  stmt = sqlite3_next_stmt (plugin->dbh,
1037  NULL);
1038  while (NULL != stmt)
1039  {
1040  result = sqlite3_finalize (stmt);
1041  if (result != SQLITE_OK)
1043  "Failed to close statement %p: %d\n",
1044  stmt,
1045  result);
1046  stmt = sqlite3_next_stmt (plugin->dbh,
1047  NULL);
1048  }
1049  result = sqlite3_close (plugin->dbh);
1050  }
1051 #endif
1052  if (SQLITE_OK != result)
1053  LOG_SQLITE (plugin->dbh,
1055  "sqlite3_close");
1056 
1057  GNUNET_free (plugin);
1058  GNUNET_free (api);
1059  return NULL;
1060 }
static int result
Global testing status.
@ GNUNET_ERROR_TYPE_WARNING
#define LOG_STRERROR_FILE(kind, op, fn)
void * cls
Closure to pass to all plugin functions.

References _, Plugin::api, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_DATACACHE_PluginFunctions::cls, Plugin::dbh, Plugin::del_expired_stmt, Plugin::del_select_stmt, Plugin::del_stmt, Plugin::fn, Plugin::get_any_stmt, Plugin::get_closest_stmt, Plugin::get_count_any_stmt, Plugin::get_count_stmt, Plugin::get_stmt, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, Plugin::insert_stmt, LOG, LOG_SQLITE, LOG_STRERROR_FILE, plugin, and result.