diff options
author | David Barksdale <amatus@amat.us> | 2017-04-16 20:46:21 -0500 |
---|---|---|
committer | David Barksdale <amatus@amat.us> | 2017-04-16 20:49:27 -0500 |
commit | a58d36b8da7afa42410bac54f57d5f3b6b6c4391 (patch) | |
tree | 87b00f07dda6a5c28a9d65ef9c05044cab2336fd /src/datastore/plugin_datastore_postgres.c | |
parent | 4907330f51ffd48af1f7bac6f43c7c7f78c37818 (diff) |
[datastore] Create remove plugin API call
The only use of vhash in the get_key call was for removing, split that out
into its own function. This simplifies the get_key call and removes the
need for some indexes, speeding up insertion into the database.
Diffstat (limited to 'src/datastore/plugin_datastore_postgres.c')
-rw-r--r-- | src/datastore/plugin_datastore_postgres.c | 90 |
1 files changed, 75 insertions, 15 deletions
diff --git a/src/datastore/plugin_datastore_postgres.c b/src/datastore/plugin_datastore_postgres.c index 349848ae67..b6aeb0be60 100644 --- a/src/datastore/plugin_datastore_postgres.c +++ b/src/datastore/plugin_datastore_postgres.c @@ -119,9 +119,6 @@ init_connection (struct Plugin *plugin) "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)")) || (GNUNET_OK != GNUNET_POSTGRES_exec (plugin->dbh, - "CREATE INDEX IF NOT EXISTS idx_hash_vhash ON gn090 (hash,vhash)")) || - (GNUNET_OK != - GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX IF NOT EXISTS idx_prio ON gn090 (prio)")) || (GNUNET_OK != GNUNET_POSTGRES_exec (plugin->dbh, @@ -183,9 +180,8 @@ init_connection (struct Plugin *plugin) "WHERE oid >= $1::bigint AND " "(rvalue >= $2 OR 0 = $3::smallint) AND " "(hash = $4 OR 0 = $5::smallint) AND " - "(vhash = $6 OR 0 = $7::smallint) AND " - "(type = $8 OR 0 = $9::smallint) " - "ORDER BY oid ASC LIMIT 1", 9)) || + "(type = $6 OR 0 = $7::smallint) " + "ORDER BY oid ASC LIMIT 1", 7)) || (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh, "put", "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " @@ -223,6 +219,10 @@ init_connection (struct Plugin *plugin) (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1)) || (GNUNET_OK != + GNUNET_POSTGRES_prepare (plugin->dbh, "remove", "DELETE FROM gn090 " + "WHERE hash = $1 AND " + "value = $2", 2)) || + (GNUNET_OK != GNUNET_POSTGRES_prepare (plugin->dbh, "get_keys", "SELECT hash FROM gn090", 0))) { PQfinish (plugin->dbh); @@ -536,11 +536,6 @@ process_result (struct Plugin *plugin, * @param next_uid return the result with lowest uid >= next_uid * @param random if true, return a random result instead of using next_uid * @param key maybe NULL (to match all entries) - * @param vhash hash of the value, maybe NULL (to - * match all values that have the right key). - * Note that for DBlocks there is no difference - * betwen key and vhash, but for other blocks - * there may be! * @param type entries of which type are relevant? * Use 0 for any type. * @param proc function to call on the matching value; @@ -552,7 +547,6 @@ postgres_plugin_get_key (void *cls, uint64_t next_uid, bool random, const struct GNUNET_HashCode *key, - const struct GNUNET_HashCode *vhash, enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc, void *proc_cls) @@ -561,7 +555,6 @@ postgres_plugin_get_key (void *cls, uint32_t utype = type; uint16_t use_rvalue = random; uint16_t use_key = NULL != key; - uint16_t use_vhash = NULL != vhash; uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type; uint64_t rvalue; struct GNUNET_PQ_QueryParam params[] = { @@ -570,8 +563,6 @@ postgres_plugin_get_key (void *cls, GNUNET_PQ_query_param_uint16 (&use_rvalue), GNUNET_PQ_query_param_auto_from_type (key), GNUNET_PQ_query_param_uint16 (&use_key), - GNUNET_PQ_query_param_auto_from_type (vhash), - GNUNET_PQ_query_param_uint16 (&use_vhash), GNUNET_PQ_query_param_uint32 (&utype), GNUNET_PQ_query_param_uint16 (&use_type), GNUNET_PQ_query_param_end @@ -854,6 +845,74 @@ postgres_plugin_drop (void *cls) /** + * Remove a particular key in the datastore. + * + * @param cls closure + * @param key key for the content + * @param size number of bytes in data + * @param data content stored + * @param cont continuation called with success or failure status + * @param cont_cls continuation closure for @a cont + */ +static void +postgres_plugin_remove_key (void *cls, + const struct GNUNET_HashCode *key, + uint32_t size, + const void *data, + PluginRemoveCont cont, + void *cont_cls) +{ + struct Plugin *plugin = cls; + PGresult *ret; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_auto_from_type (key), + GNUNET_PQ_query_param_fixed_size (data, size), + GNUNET_PQ_query_param_end + }; + ret = GNUNET_PQ_exec_prepared (plugin->dbh, + "remove", + params); + if (GNUNET_OK != + GNUNET_POSTGRES_check_result (plugin->dbh, + ret, + PGRES_COMMAND_OK, + "PQexecPrepared", + "remove")) + { + cont (cont_cls, + key, + size, + GNUNET_SYSERR, + _("Postgress exec failure")); + return; + } + /* What an awful API, this function really does return a string */ + bool affected = 0 != strcmp ("0", PQcmdTuples (ret)); + PQclear (ret); + if (!affected) + { + cont (cont_cls, + key, + size, + GNUNET_NO, + NULL); + return; + } + plugin->env->duc (plugin->env->cls, + - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); + GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, + "datastore-postgres", + "Deleted %u bytes from database\n", + (unsigned int) size); + cont (cont_cls, + key, + size, + GNUNET_OK, + NULL); +} + + +/** * Entry point for the plugin. * * @param cls the `struct GNUNET_DATASTORE_PluginEnvironment*` @@ -883,6 +942,7 @@ libgnunet_plugin_datastore_postgres_init (void *cls) api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity; api->get_keys = &postgres_plugin_get_keys; api->drop = &postgres_plugin_drop; + api->remove_key = &postgres_plugin_remove_key; GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "datastore-postgres", _("Postgres database running\n")); |