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) | |
download | gnunet-a58d36b8da7afa42410bac54f57d5f3b6b6c4391.tar.gz gnunet-a58d36b8da7afa42410bac54f57d5f3b6b6c4391.zip |
[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 349848ae6..b6aeb0be6 100644 --- a/src/datastore/plugin_datastore_postgres.c +++ b/src/datastore/plugin_datastore_postgres.c | |||
@@ -119,9 +119,6 @@ init_connection (struct Plugin *plugin) | |||
119 | "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)")) || | 119 | "CREATE INDEX IF NOT EXISTS idx_hash ON gn090 (hash)")) || |
120 | (GNUNET_OK != | 120 | (GNUNET_OK != |
121 | GNUNET_POSTGRES_exec (plugin->dbh, | 121 | GNUNET_POSTGRES_exec (plugin->dbh, |
122 | "CREATE INDEX IF NOT EXISTS idx_hash_vhash ON gn090 (hash,vhash)")) || | ||
123 | (GNUNET_OK != | ||
124 | GNUNET_POSTGRES_exec (plugin->dbh, | ||
125 | "CREATE INDEX IF NOT EXISTS idx_prio ON gn090 (prio)")) || | 122 | "CREATE INDEX IF NOT EXISTS idx_prio ON gn090 (prio)")) || |
126 | (GNUNET_OK != | 123 | (GNUNET_OK != |
127 | GNUNET_POSTGRES_exec (plugin->dbh, | 124 | GNUNET_POSTGRES_exec (plugin->dbh, |
@@ -183,9 +180,8 @@ init_connection (struct Plugin *plugin) | |||
183 | "WHERE oid >= $1::bigint AND " | 180 | "WHERE oid >= $1::bigint AND " |
184 | "(rvalue >= $2 OR 0 = $3::smallint) AND " | 181 | "(rvalue >= $2 OR 0 = $3::smallint) AND " |
185 | "(hash = $4 OR 0 = $5::smallint) AND " | 182 | "(hash = $4 OR 0 = $5::smallint) AND " |
186 | "(vhash = $6 OR 0 = $7::smallint) AND " | 183 | "(type = $6 OR 0 = $7::smallint) " |
187 | "(type = $8 OR 0 = $9::smallint) " | 184 | "ORDER BY oid ASC LIMIT 1", 7)) || |
188 | "ORDER BY oid ASC LIMIT 1", 9)) || | ||
189 | (GNUNET_OK != | 185 | (GNUNET_OK != |
190 | GNUNET_POSTGRES_prepare (plugin->dbh, "put", | 186 | GNUNET_POSTGRES_prepare (plugin->dbh, "put", |
191 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " | 187 | "INSERT INTO gn090 (repl, type, prio, anonLevel, expire, rvalue, hash, vhash, value) " |
@@ -223,6 +219,10 @@ init_connection (struct Plugin *plugin) | |||
223 | (GNUNET_OK != | 219 | (GNUNET_OK != |
224 | GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1)) || | 220 | GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090 " "WHERE oid=$1", 1)) || |
225 | (GNUNET_OK != | 221 | (GNUNET_OK != |
222 | GNUNET_POSTGRES_prepare (plugin->dbh, "remove", "DELETE FROM gn090 " | ||
223 | "WHERE hash = $1 AND " | ||
224 | "value = $2", 2)) || | ||
225 | (GNUNET_OK != | ||
226 | GNUNET_POSTGRES_prepare (plugin->dbh, "get_keys", "SELECT hash FROM gn090", 0))) | 226 | GNUNET_POSTGRES_prepare (plugin->dbh, "get_keys", "SELECT hash FROM gn090", 0))) |
227 | { | 227 | { |
228 | PQfinish (plugin->dbh); | 228 | PQfinish (plugin->dbh); |
@@ -536,11 +536,6 @@ process_result (struct Plugin *plugin, | |||
536 | * @param next_uid return the result with lowest uid >= next_uid | 536 | * @param next_uid return the result with lowest uid >= next_uid |
537 | * @param random if true, return a random result instead of using next_uid | 537 | * @param random if true, return a random result instead of using next_uid |
538 | * @param key maybe NULL (to match all entries) | 538 | * @param key maybe NULL (to match all entries) |
539 | * @param vhash hash of the value, maybe NULL (to | ||
540 | * match all values that have the right key). | ||
541 | * Note that for DBlocks there is no difference | ||
542 | * betwen key and vhash, but for other blocks | ||
543 | * there may be! | ||
544 | * @param type entries of which type are relevant? | 539 | * @param type entries of which type are relevant? |
545 | * Use 0 for any type. | 540 | * Use 0 for any type. |
546 | * @param proc function to call on the matching value; | 541 | * @param proc function to call on the matching value; |
@@ -552,7 +547,6 @@ postgres_plugin_get_key (void *cls, | |||
552 | uint64_t next_uid, | 547 | uint64_t next_uid, |
553 | bool random, | 548 | bool random, |
554 | const struct GNUNET_HashCode *key, | 549 | const struct GNUNET_HashCode *key, |
555 | const struct GNUNET_HashCode *vhash, | ||
556 | enum GNUNET_BLOCK_Type type, | 550 | enum GNUNET_BLOCK_Type type, |
557 | PluginDatumProcessor proc, | 551 | PluginDatumProcessor proc, |
558 | void *proc_cls) | 552 | void *proc_cls) |
@@ -561,7 +555,6 @@ postgres_plugin_get_key (void *cls, | |||
561 | uint32_t utype = type; | 555 | uint32_t utype = type; |
562 | uint16_t use_rvalue = random; | 556 | uint16_t use_rvalue = random; |
563 | uint16_t use_key = NULL != key; | 557 | uint16_t use_key = NULL != key; |
564 | uint16_t use_vhash = NULL != vhash; | ||
565 | uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type; | 558 | uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type; |
566 | uint64_t rvalue; | 559 | uint64_t rvalue; |
567 | struct GNUNET_PQ_QueryParam params[] = { | 560 | struct GNUNET_PQ_QueryParam params[] = { |
@@ -570,8 +563,6 @@ postgres_plugin_get_key (void *cls, | |||
570 | GNUNET_PQ_query_param_uint16 (&use_rvalue), | 563 | GNUNET_PQ_query_param_uint16 (&use_rvalue), |
571 | GNUNET_PQ_query_param_auto_from_type (key), | 564 | GNUNET_PQ_query_param_auto_from_type (key), |
572 | GNUNET_PQ_query_param_uint16 (&use_key), | 565 | GNUNET_PQ_query_param_uint16 (&use_key), |
573 | GNUNET_PQ_query_param_auto_from_type (vhash), | ||
574 | GNUNET_PQ_query_param_uint16 (&use_vhash), | ||
575 | GNUNET_PQ_query_param_uint32 (&utype), | 566 | GNUNET_PQ_query_param_uint32 (&utype), |
576 | GNUNET_PQ_query_param_uint16 (&use_type), | 567 | GNUNET_PQ_query_param_uint16 (&use_type), |
577 | GNUNET_PQ_query_param_end | 568 | GNUNET_PQ_query_param_end |
@@ -854,6 +845,74 @@ postgres_plugin_drop (void *cls) | |||
854 | 845 | ||
855 | 846 | ||
856 | /** | 847 | /** |
848 | * Remove a particular key in the datastore. | ||
849 | * | ||
850 | * @param cls closure | ||
851 | * @param key key for the content | ||
852 | * @param size number of bytes in data | ||
853 | * @param data content stored | ||
854 | * @param cont continuation called with success or failure status | ||
855 | * @param cont_cls continuation closure for @a cont | ||
856 | */ | ||
857 | static void | ||
858 | postgres_plugin_remove_key (void *cls, | ||
859 | const struct GNUNET_HashCode *key, | ||
860 | uint32_t size, | ||
861 | const void *data, | ||
862 | PluginRemoveCont cont, | ||
863 | void *cont_cls) | ||
864 | { | ||
865 | struct Plugin *plugin = cls; | ||
866 | PGresult *ret; | ||
867 | struct GNUNET_PQ_QueryParam params[] = { | ||
868 | GNUNET_PQ_query_param_auto_from_type (key), | ||
869 | GNUNET_PQ_query_param_fixed_size (data, size), | ||
870 | GNUNET_PQ_query_param_end | ||
871 | }; | ||
872 | ret = GNUNET_PQ_exec_prepared (plugin->dbh, | ||
873 | "remove", | ||
874 | params); | ||
875 | if (GNUNET_OK != | ||
876 | GNUNET_POSTGRES_check_result (plugin->dbh, | ||
877 | ret, | ||
878 | PGRES_COMMAND_OK, | ||
879 | "PQexecPrepared", | ||
880 | "remove")) | ||
881 | { | ||
882 | cont (cont_cls, | ||
883 | key, | ||
884 | size, | ||
885 | GNUNET_SYSERR, | ||
886 | _("Postgress exec failure")); | ||
887 | return; | ||
888 | } | ||
889 | /* What an awful API, this function really does return a string */ | ||
890 | bool affected = 0 != strcmp ("0", PQcmdTuples (ret)); | ||
891 | PQclear (ret); | ||
892 | if (!affected) | ||
893 | { | ||
894 | cont (cont_cls, | ||
895 | key, | ||
896 | size, | ||
897 | GNUNET_NO, | ||
898 | NULL); | ||
899 | return; | ||
900 | } | ||
901 | plugin->env->duc (plugin->env->cls, | ||
902 | - (size + GNUNET_DATASTORE_ENTRY_OVERHEAD)); | ||
903 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
904 | "datastore-postgres", | ||
905 | "Deleted %u bytes from database\n", | ||
906 | (unsigned int) size); | ||
907 | cont (cont_cls, | ||
908 | key, | ||
909 | size, | ||
910 | GNUNET_OK, | ||
911 | NULL); | ||
912 | } | ||
913 | |||
914 | |||
915 | /** | ||
857 | * Entry point for the plugin. | 916 | * Entry point for the plugin. |
858 | * | 917 | * |
859 | * @param cls the `struct GNUNET_DATASTORE_PluginEnvironment*` | 918 | * @param cls the `struct GNUNET_DATASTORE_PluginEnvironment*` |
@@ -883,6 +942,7 @@ libgnunet_plugin_datastore_postgres_init (void *cls) | |||
883 | api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity; | 942 | api->get_zero_anonymity = &postgres_plugin_get_zero_anonymity; |
884 | api->get_keys = &postgres_plugin_get_keys; | 943 | api->get_keys = &postgres_plugin_get_keys; |
885 | api->drop = &postgres_plugin_drop; | 944 | api->drop = &postgres_plugin_drop; |
945 | api->remove_key = &postgres_plugin_remove_key; | ||
886 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, | 946 | GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, |
887 | "datastore-postgres", | 947 | "datastore-postgres", |
888 | _("Postgres database running\n")); | 948 | _("Postgres database running\n")); |