42#define BUSY_TIMEOUT_MS 1000
50#define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, \
51 "namecache-sqlite", _ ( \
52 "`%s' failed at %s:%d with error: %s\n"), \
59#define LOG(kind, ...) GNUNET_log_from (kind, "namecache-sqlite", __VA_ARGS__)
122 " query BLOB NOT NULL,"
123 " block BLOB NOT NULL,"
124 " expiration_time INT8 NOT NULL"
127 "ON ns096blocks (query,expiration_time)"),
129 "ON ns096blocks (expiration_time)"),
134 "INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)",
139 "DELETE FROM ns096blocks WHERE query=? AND expiration_time<=?",
142 "ORDER BY expiration_time DESC LIMIT 1",
178 _ (
"Unable to initialize SQLite: %s.\n"),
179 sqlite3_errmsg (
plugin->dbh));
188 _ (
"Failed to setup database at `%s'\n"),
193 sqlite3_busy_timeout (
plugin->dbh,
202 _ (
"Failed to setup database at `%s'\n"),
222 if (NULL !=
plugin->cache_block)
223 sqlite3_finalize (
plugin->cache_block);
224 if (NULL !=
plugin->lookup_block)
225 sqlite3_finalize (
plugin->lookup_block);
226 if (NULL !=
plugin->expire_blocks)
227 sqlite3_finalize (
plugin->expire_blocks);
228 if (NULL !=
plugin->delete_block)
229 sqlite3_finalize (
plugin->delete_block);
231 if (
result == SQLITE_BUSY)
235 "Tried to close sqlite without finalizing all prepared statements.\n"));
236 stmt = sqlite3_next_stmt (
plugin->dbh,
242 "Closing statement %p\n",
244 result = sqlite3_finalize (stmt);
248 "Failed to close statement %p: %d\n",
251 stmt = sqlite3_next_stmt (
plugin->dbh,
286 "sqlite3_bind_XXXX");
291 n = sqlite3_step (
plugin->expire_blocks);
299 "Records expired\n");
358 "Caching new version of block %s (expires %s)\n",
361 if (block_size > 64 * 65536)
373 "sqlite3_bind_XXXX");
378 n = sqlite3_step (
plugin->delete_block);
384 "Old block deleted\n");
409 "sqlite3_bind_XXXX");
415 "Caching block under derived key `%s'\n",
417 n = sqlite3_step (
plugin->cache_block);
479 "sqlite3_bind_XXXX");
486 (sret = sqlite3_step (
plugin->lookup_block)))
504 "Found block under derived key `%s'\n",
514 if (SQLITE_DONE != sret)
524 "No block found under derived key `%s'\n",
563 _ (
"Sqlite database running\n"));
586 "sqlite plugin is finished\n");
static int ret
Final status code.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static struct GNUNET_TESTING_PluginFunctions * plugin
Plugin to dynamically load a test case.
static struct GNUNET_PEERSTORE_Handle * ps
Handle to the PEERSTORE service.
static struct GNUNET_TIME_Relative expiration
User supplied expiration value.
static int result
Global testing status.
API that can be used to manipulate GNS record data.
Plugin API for the namecache database backend.
helper functions for Sqlite3 DB interactions
struct GNUNET_SQ_PrepareStatement GNUNET_SQ_make_prepare(const char *sql, sqlite3_stmt **pstmt)
Create a struct GNUNET_SQ_PrepareStatement
struct GNUNET_SQ_ExecuteStatement GNUNET_SQ_make_execute(const char *sql)
Create a struct GNUNET_SQ_ExecuteStatement where errors are fatal.
struct GNUNET_SQ_ExecuteStatement GNUNET_SQ_make_try_execute(const char *sql)
Create a struct GNUNET_SQ_ExecuteStatement where errors should be tolerated.
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_PREPARE_END
Terminator for executable statement list.
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().
enum GNUNET_GenericReturnValue GNUNET_SQ_exec_statements(sqlite3 *dbh, const struct GNUNET_SQ_ExecuteStatement *es)
Request execution of an array of statements es from Postgres.
enum GNUNET_GenericReturnValue GNUNET_SQ_prepare(sqlite3 *dbh, const struct GNUNET_SQ_PrepareStatement *ps)
Prepare all statements given in the (NULL,NULL)-terminated array at ps.
#define GNUNET_SQ_EXECUTE_STATEMENT_END
Terminator for executable statement list.
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.
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.
size_t GNUNET_GNSRECORD_block_get_size(const struct GNUNET_GNSRECORD_Block *block)
Returns the length of this block in bytes.
enum GNUNET_GenericReturnValue GNUNET_GNSRECORD_query_from_block(const struct GNUNET_GNSRECORD_Block *block, struct GNUNET_HashCode *query)
Builds the query hash from a block.
struct GNUNET_TIME_Absolute GNUNET_GNSRECORD_block_get_expiration(const struct GNUNET_GNSRECORD_Block *block)
Returns the expiration of a block.
#define GNUNET_log(kind,...)
#define GNUNET_log_from(kind, comp,...)
const char * GNUNET_h2s_full(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_h2s(const struct GNUNET_HashCode *hc)
Convert a hash value to a string (for printing debug messages).
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.
void(* GNUNET_NAMECACHE_BlockCallback)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Function called for matching blocks.
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
#define GNUNET_TIME_UNIT_HOURS
One hour.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_get(void)
Get the current time.
const char * GNUNET_STRINGS_absolute_time_to_string(struct GNUNET_TIME_Absolute t)
Like asctime, except for GNUnet time.
#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 void namecache_sqlite_expire_blocks(struct Plugin *plugin)
Removes any expired block.
#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...
void * libgnunet_plugin_namecache_sqlite_init(void *cls)
Entry point for the plugin.
void * libgnunet_plugin_namecache_sqlite_done(void *cls)
Exit point from the plugin.
static void database_shutdown(struct Plugin *plugin)
Shutdown database connection and associate data structures.
static int namecache_sqlite_cache_block(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
static int database_setup(struct Plugin *plugin)
Initialize the database connections and associated data structures (create tables and indices as need...
static int namecache_sqlite_lookup_block(void *cls, const struct GNUNET_HashCode *query, GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
Get the block for a particular zone and label in the datastore.
void * cls
Closure for all of the callbacks.
struct returned by the initialization function of the plugin
int(* lookup_block)(void *cls, const struct GNUNET_HashCode *query, GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
Get the block for a particular zone and label in the datastore.
int(* cache_block)(void *cls, const struct GNUNET_GNSRECORD_Block *block)
Cache a block in the datastore.
void * cls
Closure to pass to all plugin functions.
Information needed to run a list of SQL statements using GNUNET_SQ_exec_statements().
Information needed to run a list of SQL statements using GNUNET_SQ_exec_statements().
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 * cache_block
Precompiled SQL for caching a block.
sqlite3_stmt * lookup_block
Precompiled SQL for looking up a block.
sqlite3_stmt * delete_block
Precompiled SQL for deleting an older block.
char * fn
Filename used for the DB.
sqlite3_stmt * expire_blocks
Precompiled SQL for removing expired blocks.
struct GNUNET_PQ_Context * dbh
Native Postgres database handle.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.