diff options
author | Christian Grothoff <christian@grothoff.org> | 2013-10-16 21:52:04 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2013-10-16 21:52:04 +0000 |
commit | dbc823a07a03e1085172038125b0edf15b0dc6fe (patch) | |
tree | 4e7c3bf60ce83e801c69a6566ea36d514b571cba /src/namestore/plugin_namestore_postgres.c | |
parent | eec0e5088ec9437f5c0cf9d3ffef87603ad2777a (diff) |
-finishing split of namestore into namestore and namecache (#3065) -- in theory; in practice, somehow something broke badly, so the tests are now failing
Diffstat (limited to 'src/namestore/plugin_namestore_postgres.c')
-rw-r--r-- | src/namestore/plugin_namestore_postgres.c | 265 |
1 files changed, 1 insertions, 264 deletions
diff --git a/src/namestore/plugin_namestore_postgres.c b/src/namestore/plugin_namestore_postgres.c index e5cb75eb40..e844134004 100644 --- a/src/namestore/plugin_namestore_postgres.c +++ b/src/namestore/plugin_namestore_postgres.c @@ -81,12 +81,6 @@ create_indices (PGconn * dbh) /* create indices */ if ( (GNUNET_OK != GNUNET_POSTGRES_exec (dbh, - "CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)")) || - (GNUNET_OK != - GNUNET_POSTGRES_exec (dbh, - "CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)")) || - (GNUNET_OK != - GNUNET_POSTGRES_exec (dbh, "CREATE INDEX ir_pkey_reverse ON ns097records (zone_private_key,pkey)")) || (GNUNET_OK != GNUNET_POSTGRES_exec (dbh, @@ -159,69 +153,10 @@ database_setup (struct Plugin *plugin) plugin->dbh = NULL; return GNUNET_SYSERR; } - - - if (GNUNET_YES == - GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg, - "namestore-postgres", - "TEMPORARY_TABLE")) - { - res = - PQexec (plugin->dbh, - "CREATE TEMPORARY TABLE ns096blocks (" - " query BYTEA NOT NULL DEFAULT ''," - " block BYTEA NOT NULL DEFAULT ''," - " expiration_time BIGINT NOT NULL DEFAULT 0" - ")" "WITH OIDS"); - } - else - { - res = - PQexec (plugin->dbh, - "CREATE TABLE ns096blocks (" - " query BYTEA NOT NULL DEFAULT ''," - " block BYTEA NOT NULL DEFAULT ''," - " expiration_time BIGINT NOT NULL DEFAULT 0" - ")" "WITH OIDS"); - } - if ( (NULL == res) || - ((PQresultStatus (res) != PGRES_COMMAND_OK) && - (0 != strcmp ("42P07", /* duplicate table */ - PQresultErrorField - (res, - PG_DIAG_SQLSTATE))))) - { - (void) GNUNET_POSTGRES_check_result (plugin->dbh, res, - PGRES_COMMAND_OK, "CREATE TABLE", - "ns096blocks"); - PQfinish (plugin->dbh); - plugin->dbh = NULL; - return GNUNET_SYSERR; - } - if (PQresultStatus (res) == PGRES_COMMAND_OK) - create_indices (plugin->dbh); - PQclear (res); + create_indices (plugin->dbh); if ((GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh, - "cache_block", - "INSERT INTO ns096blocks (query, block, expiration_time) VALUES " - "($1, $2, $3)", 3)) || - (GNUNET_OK != - GNUNET_POSTGRES_prepare (plugin->dbh, - "expire_blocks", - "DELETE FROM ns096blocks WHERE expiration_time<$1", 1)) || - (GNUNET_OK != - GNUNET_POSTGRES_prepare (plugin->dbh, - "delete_block", - "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2", 2)) || - (GNUNET_OK != - GNUNET_POSTGRES_prepare (plugin->dbh, - "lookup_block", - "SELECT block FROM ns096blocks WHERE query=$1" - " ORDER BY expiration_time DESC LIMIT 1", 1)) || - (GNUNET_OK != - GNUNET_POSTGRES_prepare (plugin->dbh, "store_records", "INSERT INTO ns097records (zone_private_key, pkey, rvalue, record_count, record_data, label) VALUES " "($1, $2, $3, $4, $5, $6)", 6)) || @@ -254,202 +189,6 @@ database_setup (struct Plugin *plugin) /** - * Removes any expired block. - * - * @param plugin the plugin - */ -static void -namestore_postgres_expire_blocks (struct Plugin *plugin) -{ - struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); - struct GNUNET_TIME_AbsoluteNBO now_be = GNUNET_TIME_absolute_hton (now); - const char *paramValues[] = { - (const char *) &now_be - }; - int paramLengths[] = { - sizeof (now_be) - }; - const int paramFormats[] = { 1 }; - PGresult *res; - - res = - PQexecPrepared (plugin->dbh, "expire_blocks", 1, - paramValues, paramLengths, paramFormats, 1); - if (GNUNET_OK != - GNUNET_POSTGRES_check_result (plugin->dbh, - res, - PGRES_COMMAND_OK, - "PQexecPrepared", - "expire_blocks")) - return; - PQclear (res); -} - - -/** - * Delete older block in the datastore. - * - * @param the plugin - * @param query query for the block - * @param expiration time how old does the block have to be for deletion - * @return #GNUNET_OK on success, else #GNUNET_SYSERR - */ -static void -delete_old_block (struct Plugin *plugin, - const struct GNUNET_HashCode *query, - struct GNUNET_TIME_AbsoluteNBO expiration_time) -{ - const char *paramValues[] = { - (const char *) query, - (const char *) &expiration_time - }; - int paramLengths[] = { - sizeof (*query), - sizeof (expiration_time) - }; - const int paramFormats[] = { 1, 1 }; - PGresult *res; - - res = - PQexecPrepared (plugin->dbh, "delete_block", 2, - paramValues, paramLengths, paramFormats, 1); - if (GNUNET_OK != - GNUNET_POSTGRES_check_result (plugin->dbh, - res, - PGRES_COMMAND_OK, - "PQexecPrepared", - "delete_block")) - return; - PQclear (res); -} - - -/** - * Cache a block in the datastore. - * - * @param cls closure (internal context for the plugin) - * @param block block to cache - * @return #GNUNET_OK on success, else #GNUNET_SYSERR - */ -static int -namestore_postgres_cache_block (void *cls, - const struct GNUNET_GNSRECORD_Block *block) -{ - struct Plugin *plugin = cls; - struct GNUNET_HashCode query; - size_t block_size = ntohl (block->purpose.size) + - sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) + - sizeof (struct GNUNET_CRYPTO_EcdsaSignature); - const char *paramValues[] = { - (const char *) &query, - (const char *) block, - (const char *) &block->expiration_time - }; - int paramLengths[] = { - sizeof (query), - (int) block_size, - sizeof (block->expiration_time) - }; - const int paramFormats[] = { 1, 1, 1 }; - PGresult *res; - - namestore_postgres_expire_blocks (plugin); - GNUNET_CRYPTO_hash (&block->derived_key, - sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey), - &query); - if (block_size > 64 * 65536) - { - GNUNET_break (0); - return GNUNET_SYSERR; - } - delete_old_block (plugin, &query, block->expiration_time); - - res = - PQexecPrepared (plugin->dbh, "cache_block", 3, - paramValues, paramLengths, paramFormats, 1); - if (GNUNET_OK != - GNUNET_POSTGRES_check_result (plugin->dbh, - res, - PGRES_COMMAND_OK, - "PQexecPrepared", - "cache_block")) - return GNUNET_SYSERR; - PQclear (res); - return GNUNET_OK; -} - - -/** - * Get the block for a particular zone and label in the - * datastore. Will return at most one result to the iterator. - * - * @param cls closure (internal context for the plugin) - * @param query hash of public key derived from the zone and the label - * @param iter function to call with the result - * @param iter_cls closure for @a iter - * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error - */ -static int -namestore_postgres_lookup_block (void *cls, - const struct GNUNET_HashCode *query, - GNUNET_GNSRECORD_BlockCallback iter, void *iter_cls) -{ - struct Plugin *plugin = cls; - const char *paramValues[] = { - (const char *) query - }; - int paramLengths[] = { - sizeof (*query) - }; - const int paramFormats[] = { 1 }; - PGresult *res; - unsigned int cnt; - size_t bsize; - const struct GNUNET_GNSRECORD_Block *block; - - res = PQexecPrepared (plugin->dbh, - "lookup_block", 1, - paramValues, paramLengths, paramFormats, - 1); - if (GNUNET_OK != - GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK, - "PQexecPrepared", - "lookup_block")) - { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Failing lookup (postgres error)\n"); - return GNUNET_SYSERR; - } - if (0 == (cnt = PQntuples (res))) - { - /* no result */ - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Ending iteration (no more results)\n"); - PQclear (res); - return GNUNET_NO; - } - GNUNET_assert (1 == cnt); - GNUNET_assert (1 != PQnfields (res)); - bsize = PQgetlength (res, 0, 0); - block = (const struct GNUNET_GNSRECORD_Block *) PQgetvalue (res, 0, 0); - if ( (bsize < sizeof (*block)) || - (bsize != ntohl (block->purpose.size) + - sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) + - sizeof (struct GNUNET_CRYPTO_EcdsaSignature)) ) - { - GNUNET_break (0); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Failing lookup (corrupt block)\n"); - PQclear (res); - return GNUNET_SYSERR; - } - iter (iter_cls, block); - PQclear (res); - return GNUNET_OK; -} - - -/** * Store a record in the datastore. Removes any existing record in the * same zone with the same name. * @@ -773,8 +512,6 @@ libgnunet_plugin_namestore_postgres_init (void *cls) } api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions); api->cls = &plugin; - api->cache_block = &namestore_postgres_cache_block; - api->lookup_block = &namestore_postgres_lookup_block; api->store_records = &namestore_postgres_store_records; api->iterate_records = &namestore_postgres_iterate_records; api->zone_to_name = &namestore_postgres_zone_to_name; |