aboutsummaryrefslogtreecommitdiff
path: root/src/datastore/plugin_datastore_sqlite.c
diff options
context:
space:
mode:
authorDavid Barksdale <amatus@amat.us>2017-04-16 20:46:21 -0500
committerDavid Barksdale <amatus@amat.us>2017-04-16 20:49:27 -0500
commita58d36b8da7afa42410bac54f57d5f3b6b6c4391 (patch)
tree87b00f07dda6a5c28a9d65ef9c05044cab2336fd /src/datastore/plugin_datastore_sqlite.c
parent4907330f51ffd48af1f7bac6f43c7c7f78c37818 (diff)
downloadgnunet-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_sqlite.c')
-rw-r--r--src/datastore/plugin_datastore_sqlite.c110
1 files changed, 92 insertions, 18 deletions
diff --git a/src/datastore/plugin_datastore_sqlite.c b/src/datastore/plugin_datastore_sqlite.c
index 469dd7717..bcaf27d99 100644
--- a/src/datastore/plugin_datastore_sqlite.c
+++ b/src/datastore/plugin_datastore_sqlite.c
@@ -88,6 +88,11 @@ struct Plugin
88 sqlite3 *dbh; 88 sqlite3 *dbh;
89 89
90 /** 90 /**
91 * Precompiled SQL for remove_key.
92 */
93 sqlite3_stmt *remove;
94
95 /**
91 * Precompiled SQL for deletion. 96 * Precompiled SQL for deletion.
92 */ 97 */
93 sqlite3_stmt *delRow; 98 sqlite3_stmt *delRow;
@@ -185,10 +190,6 @@ create_indices (sqlite3 * dbh)
185 NULL, NULL, NULL)) || 190 NULL, NULL, NULL)) ||
186 (SQLITE_OK != 191 (SQLITE_OK !=
187 sqlite3_exec (dbh, 192 sqlite3_exec (dbh,
188 "CREATE INDEX IF NOT EXISTS idx_hash_vhash ON gn090 (hash,vhash)",
189 NULL, NULL, NULL)) ||
190 (SQLITE_OK !=
191 sqlite3_exec (dbh,
192 "CREATE INDEX IF NOT EXISTS idx_expire_repl ON gn090 (expire ASC,repl DESC)", 193 "CREATE INDEX IF NOT EXISTS idx_expire_repl ON gn090 (expire ASC,repl DESC)",
193 NULL, NULL, NULL)) || 194 NULL, NULL, NULL)) ||
194 (SQLITE_OK != 195 (SQLITE_OK !=
@@ -415,15 +416,21 @@ database_setup (const struct GNUNET_CONFIGURATION_Handle *cfg,
415 "WHERE _ROWID_ >= ? AND " 416 "WHERE _ROWID_ >= ? AND "
416 "(rvalue >= ? OR 0 = ?) AND " 417 "(rvalue >= ? OR 0 = ?) AND "
417 "(hash = ? OR 0 = ?) AND " 418 "(hash = ? OR 0 = ?) AND "
418 "(vhash = ? OR 0 = ?) AND "
419 "(type = ? OR 0 = ?) " 419 "(type = ? OR 0 = ?) "
420 "ORDER BY _ROWID_ ASC LIMIT 1", 420 "ORDER BY _ROWID_ ASC LIMIT 1",
421 &plugin->get)) || 421 &plugin->get)) ||
422 (SQLITE_OK != 422 (SQLITE_OK !=
423 sq_prepare (plugin->dbh, 423 sq_prepare (plugin->dbh,
424 "DELETE FROM gn090 WHERE _ROWID_ = ?", 424 "DELETE FROM gn090 WHERE _ROWID_ = ?",
425 &plugin->delRow)) 425 &plugin->delRow)) ||
426 ) 426 (SQLITE_OK !=
427 sq_prepare (plugin->dbh,
428 "DELETE FROM gn090 "
429 "WHERE hash = ? AND "
430 "value = ? "
431 "LIMIT 1",
432 &plugin->remove)) ||
433 false)
427 { 434 {
428 LOG_SQLITE (plugin, 435 LOG_SQLITE (plugin,
429 GNUNET_ERROR_TYPE_ERROR, 436 GNUNET_ERROR_TYPE_ERROR,
@@ -448,6 +455,8 @@ database_shutdown (struct Plugin *plugin)
448 sqlite3_stmt *stmt; 455 sqlite3_stmt *stmt;
449#endif 456#endif
450 457
458 if (NULL != plugin->remove)
459 sqlite3_finalize (plugin->remove);
451 if (NULL != plugin->delRow) 460 if (NULL != plugin->delRow)
452 sqlite3_finalize (plugin->delRow); 461 sqlite3_finalize (plugin->delRow);
453 if (NULL != plugin->update) 462 if (NULL != plugin->update)
@@ -845,15 +854,10 @@ sqlite_plugin_get_zero_anonymity (void *cls,
845 * @param next_uid return the result with lowest uid >= next_uid 854 * @param next_uid return the result with lowest uid >= next_uid
846 * @param random if true, return a random result instead of using next_uid 855 * @param random if true, return a random result instead of using next_uid
847 * @param key maybe NULL (to match all entries) 856 * @param key maybe NULL (to match all entries)
848 * @param vhash hash of the value, maybe NULL (to
849 * match all values that have the right key).
850 * Note that for DBlocks there is no difference
851 * betwen key and vhash, but for other blocks
852 * there may be!
853 * @param type entries of which type are relevant? 857 * @param type entries of which type are relevant?
854 * Use 0 for any type. 858 * Use 0 for any type.
855 * @param proc function to call on each matching value; 859 * @param proc function to call on the matching value;
856 * will be called once with a NULL value at the end 860 * will be called with NULL if nothing matches
857 * @param proc_cls closure for @a proc 861 * @param proc_cls closure for @a proc
858 */ 862 */
859static void 863static void
@@ -861,7 +865,6 @@ sqlite_plugin_get_key (void *cls,
861 uint64_t next_uid, 865 uint64_t next_uid,
862 bool random, 866 bool random,
863 const struct GNUNET_HashCode *key, 867 const struct GNUNET_HashCode *key,
864 const struct GNUNET_HashCode *vhash,
865 enum GNUNET_BLOCK_Type type, 868 enum GNUNET_BLOCK_Type type,
866 PluginDatumProcessor proc, 869 PluginDatumProcessor proc,
867 void *proc_cls) 870 void *proc_cls)
@@ -872,15 +875,12 @@ sqlite_plugin_get_key (void *cls,
872 uint32_t type32 = (uint32_t) type; 875 uint32_t type32 = (uint32_t) type;
873 uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type; 876 uint16_t use_type = GNUNET_BLOCK_TYPE_ANY != type;
874 uint16_t use_key = NULL != key; 877 uint16_t use_key = NULL != key;
875 uint16_t use_vhash = NULL != vhash;
876 struct GNUNET_SQ_QueryParam params[] = { 878 struct GNUNET_SQ_QueryParam params[] = {
877 GNUNET_SQ_query_param_uint64 (&next_uid), 879 GNUNET_SQ_query_param_uint64 (&next_uid),
878 GNUNET_SQ_query_param_uint64 (&rvalue), 880 GNUNET_SQ_query_param_uint64 (&rvalue),
879 GNUNET_SQ_query_param_uint16 (&use_rvalue), 881 GNUNET_SQ_query_param_uint16 (&use_rvalue),
880 GNUNET_SQ_query_param_auto_from_type (key), 882 GNUNET_SQ_query_param_auto_from_type (key),
881 GNUNET_SQ_query_param_uint16 (&use_key), 883 GNUNET_SQ_query_param_uint16 (&use_key),
882 GNUNET_SQ_query_param_auto_from_type (vhash),
883 GNUNET_SQ_query_param_uint16 (&use_vhash),
884 GNUNET_SQ_query_param_uint32 (&type32), 884 GNUNET_SQ_query_param_uint32 (&type32),
885 GNUNET_SQ_query_param_uint16 (&use_type), 885 GNUNET_SQ_query_param_uint16 (&use_type),
886 GNUNET_SQ_query_param_end 886 GNUNET_SQ_query_param_end
@@ -1186,6 +1186,79 @@ sqlite_plugin_drop (void *cls)
1186 1186
1187 1187
1188/** 1188/**
1189 * Remove a particular key in the datastore.
1190 *
1191 * @param cls closure
1192 * @param key key for the content
1193 * @param size number of bytes in data
1194 * @param data content stored
1195 * @param cont continuation called with success or failure status
1196 * @param cont_cls continuation closure for @a cont
1197 */
1198static void
1199sqlite_plugin_remove_key (void *cls,
1200 const struct GNUNET_HashCode *key,
1201 uint32_t size,
1202 const void *data,
1203 PluginRemoveCont cont,
1204 void *cont_cls)
1205{
1206 struct Plugin *plugin = cls;
1207 struct GNUNET_SQ_QueryParam params[] = {
1208 GNUNET_SQ_query_param_auto_from_type (key),
1209 GNUNET_SQ_query_param_fixed_size (data, size),
1210 GNUNET_SQ_query_param_end
1211 };
1212
1213 if (GNUNET_OK !=
1214 GNUNET_SQ_bind (plugin->remove,
1215 params))
1216 {
1217 cont (cont_cls,
1218 key,
1219 size,
1220 GNUNET_SYSERR,
1221 "bind failed");
1222 return;
1223 }
1224 if (SQLITE_DONE != sqlite3_step (plugin->remove))
1225 {
1226 LOG_SQLITE (plugin,
1227 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
1228 "sqlite3_step");
1229 GNUNET_SQ_reset (plugin->dbh,
1230 plugin->remove);
1231 cont (cont_cls,
1232 key,
1233 size,
1234 GNUNET_SYSERR,
1235 "sqlite3_step failed");
1236 return;
1237 }
1238 int changes = sqlite3_changes (plugin->dbh);
1239 GNUNET_SQ_reset (plugin->dbh,
1240 plugin->remove);
1241 if (0 == changes)
1242 {
1243 cont (cont_cls,
1244 key,
1245 size,
1246 GNUNET_NO,
1247 NULL);
1248 return;
1249 }
1250 if (NULL != plugin->env->duc)
1251 plugin->env->duc (plugin->env->cls,
1252 -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
1253 cont (cont_cls,
1254 key,
1255 size,
1256 GNUNET_OK,
1257 NULL);
1258}
1259
1260
1261/**
1189 * Get an estimate of how much space the database is 1262 * Get an estimate of how much space the database is
1190 * currently using. 1263 * currently using.
1191 * 1264 *
@@ -1286,6 +1359,7 @@ libgnunet_plugin_datastore_sqlite_init (void *cls)
1286 api->get_zero_anonymity = &sqlite_plugin_get_zero_anonymity; 1359 api->get_zero_anonymity = &sqlite_plugin_get_zero_anonymity;
1287 api->get_keys = &sqlite_plugin_get_keys; 1360 api->get_keys = &sqlite_plugin_get_keys;
1288 api->drop = &sqlite_plugin_drop; 1361 api->drop = &sqlite_plugin_drop;
1362 api->remove_key = &sqlite_plugin_remove_key;
1289 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, 1363 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
1290 "sqlite", 1364 "sqlite",
1291 _("Sqlite database running\n")); 1365 _("Sqlite database running\n"));