/*
This file is part of GNUnet
- (C) 2009, 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
+ (C) 2009-2013 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
#include "gnunet_postgres_lib.h"
#include <postgresql/libpq-fe.h>
-#define DEBUG_POSTGRES GNUNET_EXTRA_LOGGING
/**
* After how many ms "busy" should a DB operation fail for good?
* @brief Get a database handle
*
* @param plugin global context
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
*/
static int
init_connection (struct Plugin *plugin)
plugin->dbh = GNUNET_POSTGRES_connect (plugin->env->cfg, "datastore-postgres");
if (NULL == plugin->dbh)
return GNUNET_SYSERR;
+
ret =
PQexec (plugin->dbh,
"CREATE TABLE gn090 (" " repl INTEGER NOT NULL DEFAULT 0,"
" hash BYTEA NOT NULL DEFAULT '',"
" vhash BYTEA NOT NULL DEFAULT '',"
" value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
- if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */
- PQresultErrorField
- (ret,
- PG_DIAG_SQLSTATE)))))
+ if ( (NULL == ret) ||
+ ((PQresultStatus (ret) != PGRES_COMMAND_OK) &&
+ (0 != strcmp ("42P07", /* duplicate table */
+ PQresultErrorField
+ (ret,
+ PG_DIAG_SQLSTATE)))))
{
- (void) GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090");
+ (void) GNUNET_POSTGRES_check_result (plugin->dbh,
+ ret,
+ PGRES_COMMAND_OK,
+ "CREATE TABLE",
+ "gn090");
PQfinish (plugin->dbh);
plugin->dbh = NULL;
return GNUNET_SYSERR;
}
+
if (PQresultStatus (ret) == PGRES_COMMAND_OK)
{
if ((GNUNET_OK !=
}
}
PQclear (ret);
+
ret =
PQexec (plugin->dbh,
"ALTER TABLE gn090 ALTER value SET STORAGE EXTERNAL");
* Get an estimate of how much space the database is
* currently using.
*
- * @param cls our "struct Plugin*"
+ * @param cls our `struct Plugin *`
* @return number of bytes used on disk
*/
static unsigned long long
{
return 0;
}
- if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) ||
- (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
+ if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) )
{
GNUNET_break (0);
PQclear (ret);
return 0;
}
+ if (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))
+ {
+ GNUNET_break (0 == PQgetlength (ret, 0, 0));
+ PQclear (ret);
+ return 0;
+ }
total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
PQclear (ret);
return total;
/**
* Store an item in the datastore.
*
- * @param cls closure
+ * @param cls closure with the `struct Plugin`
* @param key key for the item
* @param size number of bytes in data
* @param data content stored
* @param replication replication-level for the content
* @param expiration expiration time for the content
* @param msg set to error message
- * @return GNUNET_OK on success
+ * @return #GNUNET_OK on success
*/
static int
-postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
+postgres_plugin_put (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, char **msg)
{
struct Plugin *plugin = cls;
- GNUNET_HashCode vhash;
+ struct GNUNET_HashCode vhash;
PGresult *ret;
uint32_t btype = htonl (type);
uint32_t bprio = htonl (priority);
uint32_t banon = htonl (anonymity);
uint32_t brepl = htonl (replication);
- uint64_t bexpi = GNUNET_TIME_absolute_hton (expiration).abs_value__;
+ uint64_t bexpi = GNUNET_TIME_absolute_hton (expiration).abs_value_us__;
const char *paramValues[] = {
(const char *) &brepl,
sizeof (bprio),
sizeof (banon),
sizeof (bexpi),
- sizeof (GNUNET_HashCode),
- sizeof (GNUNET_HashCode),
+ sizeof (struct GNUNET_HashCode),
+ sizeof (struct GNUNET_HashCode),
size
};
const int paramFormats[] = { 1, 1, 1, 1, 1, 1, 1, 1 };
return GNUNET_SYSERR;
PQclear (ret);
plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
-#if DEBUG_POSTGRES
GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
"Stored %u bytes in database\n", (unsigned int) size);
-#endif
return GNUNET_OK;
}
*/
static void
process_result (struct Plugin *plugin, PluginDatumProcessor proc,
- void *proc_cls, PGresult * res,
+ void *proc_cls, PGresult * res,
const char *filename, int line)
{
int iret;
uint32_t size;
unsigned int rowid;
struct GNUNET_TIME_Absolute expiration_time;
- GNUNET_HashCode key;
+ struct GNUNET_HashCode key;
if (GNUNET_OK !=
GNUNET_POSTGRES_check_result_ (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
filename, line))
{
-#if DEBUG_POSTGRES
GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
"Ending iteration (postgres error)\n");
-#endif
proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
return;
}
if (0 == PQntuples (res))
{
/* no result */
-#if DEBUG_POSTGRES
GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
"Ending iteration (no more results)\n");
-#endif
proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
PQclear (res);
return;
(sizeof (uint32_t) != PQfsize (res, 1)) ||
(sizeof (uint32_t) != PQfsize (res, 2)) ||
(sizeof (uint64_t) != PQfsize (res, 3)) ||
- (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 4)))
+ (sizeof (struct GNUNET_HashCode) != PQgetlength (res, 0, 4)))
{
GNUNET_break (0);
PQclear (res);
type = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
priority = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
anonymity = ntohl (*(uint32_t *) PQgetvalue (res, 0, 2));
- expiration_time.abs_value =
+ expiration_time.abs_value_us =
GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3));
- memcpy (&key, PQgetvalue (res, 0, 4), sizeof (GNUNET_HashCode));
+ memcpy (&key, PQgetvalue (res, 0, 4), sizeof (struct GNUNET_HashCode));
size = PQgetlength (res, 0, 5);
-#if DEBUG_POSTGRES
GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
"Found result of size %u bytes and type %u in database\n",
(unsigned int) size, (unsigned int) type);
-#endif
iret =
proc (proc_cls, &key, size, PQgetvalue (res, 0, 5),
(enum GNUNET_BLOCK_Type) type, priority, anonymity, expiration_time,
PQclear (res);
if (iret == GNUNET_NO)
{
-#if DEBUG_POSTGRES
GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
"Processor asked for item %u to be removed.\n", rowid);
-#endif
if (GNUNET_OK == GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid))
{
-#if DEBUG_POSTGRES
GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
"Deleting %u bytes from database\n",
(unsigned int) size);
-#endif
plugin->env->duc (plugin->env->cls,
-(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
-#if DEBUG_POSTGRES
GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
"Deleted %u bytes from database\n", (unsigned int) size);
-#endif
}
}
}
* Iterate over the results for a particular key
* in the datastore.
*
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
* @param offset offset of the result (modulo num-results);
* specific ordering does not matter for the offset
* @param key maybe NULL (to match all entries)
*/
static void
postgres_plugin_get_key (void *cls, uint64_t offset,
- const GNUNET_HashCode * key,
- const GNUNET_HashCode * vhash,
+ const struct GNUNET_HashCode * key,
+ const struct GNUNET_HashCode * vhash,
enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
void *proc_cls)
{
GNUNET_assert (key != NULL);
paramValues[0] = (const char *) key;
- paramLengths[0] = sizeof (GNUNET_HashCode);
+ paramLengths[0] = sizeof (struct GNUNET_HashCode);
btype = htonl (type);
if (type != 0)
{
if (vhash != NULL)
{
paramValues[1] = (const char *) vhash;
- paramLengths[1] = sizeof (GNUNET_HashCode);
+ paramLengths[1] = sizeof (struct GNUNET_HashCode);
paramValues[2] = (const char *) &btype;
paramLengths[2] = sizeof (btype);
paramValues[3] = (const char *) &blimit_off;
if (vhash != NULL)
{
paramValues[1] = (const char *) vhash;
- paramLengths[1] = sizeof (GNUNET_HashCode);
+ paramLengths[1] = sizeof (struct GNUNET_HashCode);
paramValues[2] = (const char *) &blimit_off;
paramLengths[2] = sizeof (blimit_off);
nparams = 3;
* Decrements the replication counter and calls the original
* iterator.
*
- * @param cls closure
+ * @param cls closure with the 'struct ReplCtx*'
* @param key key for the content
* @param size number of bytes in data
* @param data content stored
* GNUNET_NO to delete the item and continue (if supported)
*/
static int
-repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
+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, struct GNUNET_TIME_Absolute expiration,
uint64_t uid)
* replication counter is decremented by one IF it was positive before.
* Call 'proc' with all values ZERO or NULL if the datastore is empty.
*
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
* @param proc function to call the value (once only).
* @param proc_cls closure for proc
*/
* Get a random item for expiration.
* Call 'proc' with all values ZERO or NULL if the datastore is empty.
*
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
* @param proc function to call the value (once only).
* @param proc_cls closure for proc
*/
const char *paramValues[] = { (const char *) &btime };
PGresult *ret;
- btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value);
+ btime = GNUNET_htonll (GNUNET_TIME_absolute_get ().abs_value_us);
ret =
PQexecPrepared (plugin->dbh, "select_expiration_order", 1, paramValues,
paramLengths, paramFormats, 1);
PGresult *ret;
int32_t bdelta = (int32_t) htonl ((uint32_t) delta);
uint32_t boid = htonl ((uint32_t) uid);
- uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value__;
+ uint64_t bexpire = GNUNET_TIME_absolute_hton (expire).abs_value_us__;
const char *paramValues[] = {
(const char *) &bdelta,
/**
* Get all of the keys in the datastore.
*
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
* @param proc function to call on each key
* @param proc_cls closure for proc
*/
struct Plugin *plugin = cls;
int ret;
int i;
- GNUNET_HashCode key;
+ struct GNUNET_HashCode key;
PGresult * res;
res = PQexecPrepared (plugin->dbh, "get_keys", 0, NULL, NULL, NULL, 1);
ret = PQntuples (res);
for (i=0;i<ret;i++)
{
- if (sizeof (GNUNET_HashCode) != PQgetlength (res, i, 0))
+ if (sizeof (struct GNUNET_HashCode) != PQgetlength (res, i, 0))
{
- memcpy (&key, PQgetvalue (res, i, 0), sizeof (GNUNET_HashCode));
- proc (proc_cls, &key, 1);
+ memcpy (&key, PQgetvalue (res, i, 0), sizeof (struct GNUNET_HashCode));
+ proc (proc_cls, &key, 1);
}
}
PQclear (res);
/**
* Drop database.
+ *
+ * @param cls closure with the 'struct Plugin'
*/
static void
postgres_plugin_drop (void *cls)
{
struct Plugin *plugin = cls;
- GNUNET_POSTGRES_exec (plugin->dbh, "DROP TABLE gn090");
+ if (GNUNET_OK != GNUNET_POSTGRES_exec (plugin->dbh, "DROP TABLE gn090"))
+ GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "postgres", _("Failed to drop table from database.\n"));
}
struct GNUNET_DATASTORE_PluginFunctions *api;
struct Plugin *plugin;
- plugin = GNUNET_malloc (sizeof (struct Plugin));
+ plugin = GNUNET_new (struct Plugin);
plugin->env = env;
if (GNUNET_OK != init_connection (plugin))
{
GNUNET_free (plugin);
return NULL;
}
- api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
+ api = GNUNET_new (struct GNUNET_DATASTORE_PluginFunctions);
api->cls = plugin;
api->estimate_size = &postgres_plugin_estimate_size;
api->put = &postgres_plugin_put;