45 #define BUSY_TIMEOUT_MS 1000
52 #define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, \
53 "peerstore-sqlite", _ ( \
54 "`%s' failed at %s:%d with error: %s\n"), \
61 #define LOG(kind, ...) GNUNET_log_from (kind, "peerstore-sqlite", __VA_ARGS__)
133 const char *sub_system,
138 sqlite3_stmt *stmt =
plugin->delete_peerstoredata;
192 sqlite3_stmt *stmt =
plugin->expire_peerstoredata;
209 if (SQLITE_DONE != sqlite3_step (stmt))
220 sqlite3_changes (
plugin->dbh));
243 const char *sub_system,
256 "Executing iterate request on sqlite db.\n");
266 stmt =
plugin->select_peerstoredata;
278 stmt =
plugin->select_peerstoredata_by_key;
293 stmt =
plugin->select_peerstoredata_by_pid;
306 stmt =
plugin->select_peerstoredata_by_all;
316 "sqlite3_bind_XXXX");
323 while (SQLITE_ROW == (sret = sqlite3_step (stmt)))
326 "Returning a matched record.\n");
349 if (SQLITE_DONE != sret)
361 err ?
"sqlite error" : NULL);
385 const char *sub_system,
396 sqlite3_stmt *stmt =
plugin->insert_peerstoredata;
419 else if (SQLITE_DONE != sqlite3_step (stmt))
447 result = sqlite3_exec (dbh,
453 "Executed `%s' / %d\n",
458 _ (
"Error executing SQL query: %s\n %s\n"),
459 sqlite3_errmsg (dbh),
481 result = sqlite3_prepare_v2 (dbh,
485 (
const char **) &
tail);
487 "Prepared `%s' / %p: %d\n",
493 _ (
"Error preparing SQL query: %s\n %s\n"),
494 sqlite3_errmsg (dbh),
536 if (SQLITE_OK != sqlite3_open (
plugin->fn,
540 _ (
"Unable to initialize SQLite: %s.\n"),
541 sqlite3_errmsg (
plugin->dbh));
545 "PRAGMA temp_store=MEMORY");
547 "PRAGMA synchronous=OFF");
549 "PRAGMA legacy_file_format=OFF");
551 "PRAGMA auto_vacuum=INCREMENTAL");
553 "PRAGMA encoding=\"UTF-8\"");
555 "PRAGMA page_size=4096");
556 sqlite3_busy_timeout (
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" ");");
568 sqlite3_exec (
plugin->dbh,
569 "CREATE INDEX IF NOT EXISTS peerstoredata_key_index ON peerstoredata (sub_system, peer_id, key)",
575 _ (
"Unable to create indices: %s.\n"),
576 sqlite3_errmsg (
plugin->dbh));
582 "INSERT INTO peerstoredata (sub_system, peer_id, key, value, expiry)"
583 " VALUES (?,?,?,?,?);",
584 &
plugin->insert_peerstoredata);
586 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
587 " WHERE sub_system = ?",
588 &
plugin->select_peerstoredata);
590 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
591 " WHERE sub_system = ?"
593 &
plugin->select_peerstoredata_by_pid);
595 "SELECT sub_system,peer_id,key,value,expiry FROM peerstoredata"
596 " WHERE sub_system = ?"
598 &
plugin->select_peerstoredata_by_key);
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);
605 "DELETE FROM peerstoredata"
607 &
plugin->expire_peerstoredata);
609 "DELETE FROM peerstoredata"
610 " WHERE sub_system = ?"
611 " AND peer_id = ?" " AND key = ?",
612 &
plugin->delete_peerstoredata);
628 while (NULL != (stmt = sqlite3_next_stmt (
plugin->dbh,
631 result = sqlite3_finalize (stmt);
634 "Failed to close statement %p: %d\n",
638 if (SQLITE_OK != sqlite3_close (
plugin->dbh))
676 "Sqlite plugin is running\n");
697 "Sqlite plugin is finished\n");
struct GNUNET_GETOPT_CommandLineOption options[]
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
static int ret
Return value of the commandline.
static struct PendingResolutions * tail
Tail of list of pending resolution requests.
struct TestcasePlugin * plugin
The process handle to the testbed service.
struct GNUNET_HashCode key
The key used in the DHT.
static char * value
Value of the record to add/remove.
static int result
Global testing status.
Plugin API for the peerstore database backend.
API to the peerstore service.
helper functions for Sqlite3 DB interactions
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.
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.
void GNUNET_SQ_cleanup_result(struct GNUNET_SQ_ResultSpec *rs)
Free all memory that was allocated in rs during GNUNET_SQ_extract_result().
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_string(char **dst)
0-terminated string expected.
struct GNUNET_SQ_ResultSpec GNUNET_SQ_result_spec_absolute_time(struct GNUNET_TIME_Absolute *at)
Absolute time expected.
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_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_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.
void GNUNET_SQ_reset(sqlite3 *dbh, sqlite3_stmt *stmt)
Reset stmt and log error.
#define GNUNET_SQ_query_param_end
End of query parameter specification.
struct GNUNET_SQ_QueryParam GNUNET_SQ_query_param_string(const char *ptr)
Generate query parameter for a string.
#define GNUNET_SQ_result_spec_auto_from_type(dst)
We expect a fixed-size result, with size determined by the type of * dst
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory).
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_PEERSTORE_StoreOption
Options for storing values in PEERSTORE.
void(* GNUNET_PEERSTORE_Processor)(void *cls, const struct GNUNET_PEERSTORE_Record *record, const char *emsg)
Function called by PEERSTORE for each matching record.
void(* GNUNET_PEERSTORE_Continuation)(void *cls, int success)
Continuation called with a status result.
@ GNUNET_PEERSTORE_STOREOPTION_REPLACE
Delete any previous values for the given key before storing the given value.
static unsigned int size
Size of the "table".
#define BUSY_TIMEOUT_MS
After how many ms "busy" should a DB operation fail for good? A low value makes sure that we are more...
static int sql_prepare(sqlite3 *dbh, const char *sql, sqlite3_stmt **stmt)
Prepare a SQL statement.
static int 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.
#define LOG_SQLITE(db, level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' on file 'file...
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)
void * libgnunet_plugin_peerstore_sqlite_done(void *cls)
Exit point from the plugin.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
void * libgnunet_plugin_peerstore_sqlite_init(void *cls)
Entry point for the plugin.
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 sql_exec(sqlite3 *dbh, const char *sql)
Prepare a SQL statement.
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.
static int database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
void * cls
Closure for all of the callbacks.
struct returned by the initialization function of the plugin
void * cls
Closure to pass to all plugin functions.
int(* store_record)(void *cls, const char *sub_system, const struct GNUNET_PeerIdentity *peer, const char *key, const void *value, size_t size, struct GNUNET_TIME_Absolute expiry, enum GNUNET_PEERSTORE_StoreOption options, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Store a record in the peerstore.
int(* expire_records)(void *cls, struct GNUNET_TIME_Absolute now, GNUNET_PEERSTORE_Continuation cont, void *cont_cls)
Delete expired records (expiry < now)
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.
size_t value_size
Size of value BLOB.
struct GNUNET_PeerIdentity peer
Peer Identity.
void * value
Record value BLOB.
struct GNUNET_TIME_Absolute expiry
Expiry time of entry.
char * sub_system
Responsible sub system string.
char * key
Record key string.
The identity of the host (wraps the signing key of the peer).
Description of a DB query parameter.
Description of a DB result cell.
void * cls
Closure for conv and cleaner.
Time for absolute times used by GNUnet, in microseconds.
struct GNUNET_BLOCK_PluginFunctions * api
Plugin API.
sqlite3_stmt * expire_peerstoredata
Precompiled SQL for deleting expired records from peerstoredata.
sqlite3_stmt * insert_peerstoredata
Precompiled SQL for inserting into peerstoredata.
char * fn
Filename used for the DB.
sqlite3_stmt * select_peerstoredata
Precompiled SQL for selecting from peerstoredata.
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
sqlite3_stmt * select_peerstoredata_by_all
Precompiled SQL for selecting from peerstoredata.
sqlite3_stmt * delete_peerstoredata
Precompiled SQL for deleting records with given key.
sqlite3_stmt * select_peerstoredata_by_key
Precompiled SQL for selecting from peerstoredata.
sqlite3_stmt * select_peerstoredata_by_pid
Precompiled SQL for selecting from peerstoredata.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.