sqlite-based datastore backend More...
#include "platform.h"
#include "gnunet_datastore_plugin.h"
#include "gnunet_sq_lib.h"
#include <sqlite3.h>
Go to the source code of this file.
Data Structures | |
struct | Plugin |
Handle for a plugin. More... | |
struct | ReplCtx |
Context for #repl_iter() function. More... | |
Macros | |
#define | MAX_ITEM_SIZE 65536 |
We allocate items on the stack at times. More... | |
#define | BUSY_TIMEOUT_MS 250 |
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_SQLITE_MSG(db, msg, 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 | ENULL &e |
#define | ENULL_DEFINED 1 |
#define | CHECK(a) |
#define | RESULT_COLUMNS "repl, type, prio, anonLevel, expire, hash, value, _ROWID_" |
Functions | |
static int | sq_prepare (sqlite3 *dbh, const char *zSql, sqlite3_stmt **ppStmt) |
Prepare a SQL statement. More... | |
static void | create_indices (sqlite3 *dbh) |
Create our database indices. More... | |
static int | database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg, 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... | |
static int | delete_by_rowid (struct Plugin *plugin, uint64_t rid) |
Delete the database entry with the given row identifier. More... | |
static void | sqlite_plugin_put (void *cls, const struct GNUNET_HashCode *key, bool absent, uint32_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, PluginPutCont cont, void *cont_cls) |
Store an item in the datastore. More... | |
static void | execute_get (struct Plugin *plugin, sqlite3_stmt *stmt, PluginDatumProcessor proc, void *proc_cls) |
Execute statement that gets a row and call the callback with the result. More... | |
static void | sqlite_plugin_get_zero_anonymity (void *cls, uint64_t next_uid, enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc, void *proc_cls) |
Select a subset of the items in the datastore and call the given processor for the item. More... | |
static void | sqlite_plugin_get_key (void *cls, uint64_t next_uid, bool random, const struct GNUNET_HashCode *key, enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc, void *proc_cls) |
Get results for a particular key in the datastore. More... | |
static int | repl_proc (void *cls, const struct GNUNET_HashCode *key, uint32_t size, const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority, uint32_t anonymity, uint32_t replication, struct GNUNET_TIME_Absolute expiration, uint64_t uid) |
Wrapper for the processor for sqlite_plugin_get_replication(). More... | |
static void | sqlite_plugin_get_replication (void *cls, PluginDatumProcessor proc, void *proc_cls) |
Get a random item for replication. More... | |
static void | sqlite_plugin_get_expiration (void *cls, PluginDatumProcessor proc, void *proc_cls) |
Get a random item that has expired or has low priority. More... | |
static void | sqlite_plugin_get_keys (void *cls, PluginKeyProcessor proc, void *proc_cls) |
Get all of the keys in the datastore. More... | |
static void | sqlite_plugin_drop (void *cls) |
Drop database. More... | |
static void | sqlite_plugin_remove_key (void *cls, const struct GNUNET_HashCode *key, uint32_t size, const void *data, PluginRemoveCont cont, void *cont_cls) |
Remove a particular key in the datastore. More... | |
static void | sqlite_plugin_estimate_size (void *cls, unsigned long long *estimate) |
Get an estimate of how much space the database is currently using. More... | |
void * | libgnunet_plugin_datastore_sqlite_init (void *cls) |
Entry point for the plugin. More... | |
void * | libgnunet_plugin_datastore_sqlite_done (void *cls) |
Exit point from the plugin. More... | |
sqlite-based datastore backend
Definition in file plugin_datastore_sqlite.c.
#define MAX_ITEM_SIZE 65536 |
We allocate items on the stack at times.
To prevent a stack overflow, we impose a limit on the maximum size for the data per item. 64k should be enough.
Definition at line 38 of file plugin_datastore_sqlite.c.
#define BUSY_TIMEOUT_MS 250 |
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 250ms should ensure that users do not experience huge latencies while at the same time allowing operations to succeed with reasonable probability.
Definition at line 50 of file plugin_datastore_sqlite.c.
#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).
Definition at line 58 of file plugin_datastore_sqlite.c.
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 76 of file plugin_datastore_sqlite.c.
#define ENULL &e |
Definition at line 249 of file plugin_datastore_sqlite.c.
#define ENULL_DEFINED 1 |
Definition at line 250 of file plugin_datastore_sqlite.c.
#define CHECK | ( | a | ) |
Definition at line 251 of file plugin_datastore_sqlite.c.
|
static |
Prepare a SQL statement.
dbh | handle to the database |
zSql | SQL statement, UTF-8 encoded |
ppStmt | set to the prepared statement |
Definition at line 181 of file plugin_datastore_sqlite.c.
References dummy, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log_from, and result.
Referenced by database_setup(), sqlite_plugin_estimate_size(), and sqlite_plugin_get_keys().
|
static |
Create our database indices.
dbh | handle to the database |
Definition at line 207 of file plugin_datastore_sqlite.c.
References GNUNET_ERROR_TYPE_ERROR, and GNUNET_log_from.
Referenced by database_setup().
|
static |
Initialize the database connections and associated data structures (create tables and indices as needed as well).
cfg | our configuration |
plugin | the plugin context (state for this module) |
Definition at line 271 of file plugin_datastore_sqlite.c.
References _, BUSY_TIMEOUT_MS, cfg, CHECK, GNUNET_TESTING_PluginFunctions::cls, create_indices(), ENULL, 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_log_from, GNUNET_OK, GNUNET_SYSERR, LOG_SQLITE, plugin, RESULT_COLUMNS, and sq_prepare().
Referenced by execute_get(), libgnunet_plugin_datastore_sqlite_init(), and sqlite_plugin_put().
|
static |
Shutdown database connection and associate data structures.
plugin | the plugin context (state for this module) |
Definition at line 495 of file plugin_datastore_sqlite.c.
References _, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_from, LOG_SQLITE, plugin, and result.
Referenced by execute_get(), libgnunet_plugin_datastore_sqlite_done(), libgnunet_plugin_datastore_sqlite_init(), and sqlite_plugin_put().
|
static |
Delete the database entry with the given row identifier.
plugin | the plugin context (state for this module) |
rid | the ID of the row to delete |
Definition at line 566 of file plugin_datastore_sqlite.c.
References GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_ERROR, GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_uint64(), GNUNET_SQ_reset(), GNUNET_SYSERR, LOG_SQLITE, and plugin.
Referenced by execute_get().
|
static |
Store an item in the datastore.
cls | closure |
key | key for the item |
absent | true if the key was not found in the bloom filter |
size | number of bytes in data |
data | content stored |
type | type of the content |
priority | priority of the content |
anonymity | anonymity-level for the content |
replication | replication-level for the content |
expiration | expiration time for the content |
cont | continuation called with success or failure status |
cont_cls | continuation closure |
Definition at line 603 of file plugin_datastore_sqlite.c.
References _, anonymity, GNUNET_TESTING_PluginFunctions::cls, data, database_setup(), database_shutdown(), expiration, GNUNET_break, GNUNET_CRYPTO_hash(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_DATASTORE_ENTRY_OVERHEAD, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_h2s(), GNUNET_log_from, GNUNET_NO, 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_query_param_uint64(), GNUNET_SQ_reset(), GNUNET_STRINGS_absolute_time_to_string(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_SYSERR, GNUNET_TIME_absolute_get_remaining(), GNUNET_YES, key, LOG_SQLITE_MSG, MAX_ITEM_SIZE, msg, plugin, replication, ret, size, and type.
Referenced by libgnunet_plugin_datastore_sqlite_init().
|
static |
Execute statement that gets a row and call the callback with the result.
Resets the statement afterwards.
plugin | the plugin |
stmt | the statement |
proc | processor to call |
proc_cls | closure for proc |
Definition at line 753 of file plugin_datastore_sqlite.c.
References anonymity, GNUNET_TESTING_PluginFunctions::cls, database_setup(), database_shutdown(), delete_by_rowid(), expiration, GNUNET_break, GNUNET_DATASTORE_ENTRY_OVERHEAD, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_log_from, GNUNET_NO, GNUNET_OK, GNUNET_SQ_cleanup_result(), GNUNET_SQ_extract_result(), 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_uint64(), GNUNET_SQ_result_spec_variable_size(), GNUNET_STRINGS_absolute_time_to_string(), GNUNET_TIME_UNIT_ZERO_ABS, key, LOG_SQLITE, plugin, replication, ret, type, and value.
Referenced by sqlite_plugin_get_expiration(), sqlite_plugin_get_key(), sqlite_plugin_get_replication(), and sqlite_plugin_get_zero_anonymity().
|
static |
Select a subset of the items in the datastore and call the given processor for the item.
cls | our plugin context |
next_uid | return the result with lowest uid >= next_uid |
type | entries of which type should be considered? Must not be zero (ANY). |
proc | function to call on the matching value; will be called with NULL if no value matches |
proc_cls | closure for proc |
Definition at line 850 of file plugin_datastore_sqlite.c.
References execute_get(), GNUNET_assert, GNUNET_BLOCK_TYPE_ANY, GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_uint32(), GNUNET_SQ_query_param_uint64(), GNUNET_TIME_UNIT_ZERO_ABS, plugin, and type.
Referenced by libgnunet_plugin_datastore_sqlite_init().
|
static |
Get results for a particular key in the datastore.
cls | closure |
next_uid | return the result with lowest uid >= next_uid |
random | if true, return a random result instead of using next_uid |
key | maybe NULL (to match all entries) |
type | entries of which type are relevant? Use 0 for any type. |
proc | function to call on the matching value; will be called with NULL if nothing matches |
proc_cls | closure for proc |
Definition at line 888 of file plugin_datastore_sqlite.c.
References execute_get(), GNUNET_BLOCK_TYPE_ANY, GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_query_param_auto_from_type, GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_uint32(), GNUNET_SQ_query_param_uint64(), GNUNET_TIME_UNIT_ZERO_ABS, key, plugin, and type.
Referenced by libgnunet_plugin_datastore_sqlite_init().
|
static |
Wrapper for the processor for sqlite_plugin_get_replication().
Decrements the replication counter and calls the original processor.
cls | closure |
key | key for the content |
size | number of bytes in data |
data | content stored |
type | type of the content |
priority | priority of the content |
anonymity | anonymity-level for the content |
replication | replication-level for the content |
expiration | expiration time for the content |
uid | unique identifier for the datum; maybe 0 if no unique identifier is available |
Definition at line 987 of file plugin_datastore_sqlite.c.
References anonymity, data, expiration, GNUNET_NO, GNUNET_SYSERR, GNUNET_YES, ReplCtx::have_uid, key, ReplCtx::proc, ReplCtx::proc_cls, replication, ret, size, type, and ReplCtx::uid.
Referenced by sqlite_plugin_get_replication().
|
static |
Get a random item for replication.
Returns a single random item from those with the highest replication counters. The item's replication counter is decremented by one IF it was positive before. Call proc with all values ZERO or NULL if the datastore is empty.
cls | closure |
proc | function to call the value (once only). |
proc_cls | closure for proc |
Definition at line 1033 of file plugin_datastore_sqlite.c.
References execute_get(), GNUNET_CRYPTO_QUALITY_WEAK, GNUNET_CRYPTO_random_u64(), GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_log_from, GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_uint32(), GNUNET_SQ_query_param_uint64(), GNUNET_SQ_reset(), GNUNET_SYSERR, GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_YES, ReplCtx::have_uid, LOG_SQLITE, plugin, ReplCtx::proc, ReplCtx::proc_cls, repl_proc(), and ReplCtx::uid.
Referenced by libgnunet_plugin_datastore_sqlite_init().
|
static |
Get a random item that has expired or has low priority.
Call proc with all values ZERO or NULL if the datastore is empty.
cls | closure |
proc | function to call the value (once only). |
proc_cls | closure for proc |
Definition at line 1100 of file plugin_datastore_sqlite.c.
References execute_get(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_log_from, GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_query_param_absolute_time(), GNUNET_SQ_query_param_end, GNUNET_TIME_absolute_get(), GNUNET_TIME_UNIT_ZERO_ABS, and plugin.
Referenced by libgnunet_plugin_datastore_sqlite_init().
|
static |
Get all of the keys in the datastore.
cls | closure |
proc | function to call on each key |
proc_cls | closure for proc |
Definition at line 1134 of file plugin_datastore_sqlite.c.
References GNUNET_assert, GNUNET_break, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_ERROR, GNUNET_OK, GNUNET_SQ_extract_result(), GNUNET_SQ_result_spec_auto_from_type, GNUNET_SQ_result_spec_end, key, LOG_SQLITE, plugin, results, ret, and sq_prepare().
Referenced by libgnunet_plugin_datastore_sqlite_init().
|
static |
Drop database.
cls | our plugin context |
Definition at line 1172 of file plugin_datastore_sqlite.c.
References GNUNET_YES, and plugin.
Referenced by libgnunet_plugin_datastore_sqlite_init().
|
static |
Remove a particular key in the datastore.
cls | closure |
key | key for the content |
size | number of bytes in data |
data | content stored |
cont | continuation called with success or failure status |
cont_cls | continuation closure for cont |
Definition at line 1191 of file plugin_datastore_sqlite.c.
References GNUNET_TESTING_PluginFunctions::cls, data, GNUNET_DATASTORE_ENTRY_OVERHEAD, GNUNET_ERROR_TYPE_BULK, GNUNET_ERROR_TYPE_ERROR, GNUNET_NO, GNUNET_OK, GNUNET_SQ_bind(), GNUNET_SQ_query_param_auto_from_type, GNUNET_SQ_query_param_end, GNUNET_SQ_query_param_fixed_size(), GNUNET_SQ_reset(), GNUNET_SYSERR, key, LOG_SQLITE, plugin, and size.
Referenced by libgnunet_plugin_datastore_sqlite_init().
|
static |
Get an estimate of how much space the database is currently using.
cls | the struct Plugin |
Definition at line 1242 of file plugin_datastore_sqlite.c.
References _, CHECK, ENULL, GNUNET_ERROR_TYPE_INFO, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_from, plugin, and sq_prepare().
Referenced by libgnunet_plugin_datastore_sqlite_init().
void * libgnunet_plugin_datastore_sqlite_init | ( | void * | cls | ) |
Entry point for the plugin.
cls | the struct GNUNET_DATASTORE_PluginEnvironment * |
Definition at line 1321 of file plugin_datastore_sqlite.c.
References _, GNUNET_DATASTORE_PluginEnvironment::cls, GNUNET_DATASTORE_PluginFunctions::cls, database_setup(), database_shutdown(), GNUNET_DATASTORE_PluginFunctions::drop, env, GNUNET_DATASTORE_PluginFunctions::estimate_size, GNUNET_DATASTORE_PluginFunctions::get_expiration, GNUNET_DATASTORE_PluginFunctions::get_key, GNUNET_DATASTORE_PluginFunctions::get_keys, GNUNET_DATASTORE_PluginFunctions::get_replication, GNUNET_DATASTORE_PluginFunctions::get_zero_anonymity, GNUNET_ERROR_TYPE_INFO, GNUNET_log_from, GNUNET_new, GNUNET_OK, plugin, GNUNET_DATASTORE_PluginFunctions::put, GNUNET_DATASTORE_PluginFunctions::remove_key, sqlite_plugin_drop(), sqlite_plugin_estimate_size(), sqlite_plugin_get_expiration(), sqlite_plugin_get_key(), sqlite_plugin_get_keys(), sqlite_plugin_get_replication(), sqlite_plugin_get_zero_anonymity(), sqlite_plugin_put(), and sqlite_plugin_remove_key().
void * libgnunet_plugin_datastore_sqlite_done | ( | void * | cls | ) |
Exit point from the plugin.
cls | the plugin context (as returned by "init") |
Definition at line 1364 of file plugin_datastore_sqlite.c.
References Plugin::api, GNUNET_BLOCK_PluginFunctions::cls, GNUNET_DATASTORE_PluginFunctions::cls, database_shutdown(), Plugin::fn, GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_from, GNUNET_log_strerror_file, GNUNET_strdup, and plugin.