aboutsummaryrefslogtreecommitdiff
path: root/src/datacache/plugin_datacache_sqlite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/datacache/plugin_datacache_sqlite.c')
-rw-r--r--src/datacache/plugin_datacache_sqlite.c94
1 files changed, 69 insertions, 25 deletions
diff --git a/src/datacache/plugin_datacache_sqlite.c b/src/datacache/plugin_datacache_sqlite.c
index 15438b29b..ef24199ca 100644
--- a/src/datacache/plugin_datacache_sqlite.c
+++ b/src/datacache/plugin_datacache_sqlite.c
@@ -38,7 +38,7 @@
38 * How much overhead do we assume per entry in the 38 * How much overhead do we assume per entry in the
39 * datacache? 39 * datacache?
40 */ 40 */
41#define OVERHEAD (sizeof(struct GNUNET_HashCode) + 32) 41#define OVERHEAD (sizeof(struct GNUNET_HashCode) + 36)
42 42
43/** 43/**
44 * Context for all functions in this plugin. 44 * Context for all functions in this plugin.
@@ -83,6 +83,11 @@ struct Plugin
83 /** 83 /**
84 * Prepared statement for #sqlite_plugin_del. 84 * Prepared statement for #sqlite_plugin_del.
85 */ 85 */
86 sqlite3_stmt *del_expired_stmt;
87
88 /**
89 * Prepared statement for #sqlite_plugin_del.
90 */
86 sqlite3_stmt *del_stmt; 91 sqlite3_stmt *del_stmt;
87 92
88 /** 93 /**
@@ -150,6 +155,7 @@ sq_prepare (sqlite3 *dbh,
150 * 155 *
151 * @param cls closure (our `struct Plugin`) 156 * @param cls closure (our `struct Plugin`)
152 * @param key key to store @a data under 157 * @param key key to store @a data under
158 * @param xor_distance how close is @a key to our PID?
153 * @param size number of bytes in @a data 159 * @param size number of bytes in @a data
154 * @param data data to store 160 * @param data data to store
155 * @param type type of the value 161 * @param type type of the value
@@ -161,6 +167,7 @@ sq_prepare (sqlite3 *dbh,
161static ssize_t 167static ssize_t
162sqlite_plugin_put (void *cls, 168sqlite_plugin_put (void *cls,
163 const struct GNUNET_HashCode *key, 169 const struct GNUNET_HashCode *key,
170 uint32_t xor_distance,
164 size_t size, 171 size_t size,
165 const char *data, 172 const char *data,
166 enum GNUNET_BLOCK_Type type, 173 enum GNUNET_BLOCK_Type type,
@@ -174,6 +181,7 @@ sqlite_plugin_put (void *cls,
174 GNUNET_SQ_query_param_uint32 (&type32), 181 GNUNET_SQ_query_param_uint32 (&type32),
175 GNUNET_SQ_query_param_absolute_time (&discard_time), 182 GNUNET_SQ_query_param_absolute_time (&discard_time),
176 GNUNET_SQ_query_param_auto_from_type (key), 183 GNUNET_SQ_query_param_auto_from_type (key),
184 GNUNET_SQ_query_param_uint32 (&xor_distance),
177 GNUNET_SQ_query_param_fixed_size (data, size), 185 GNUNET_SQ_query_param_fixed_size (data, size),
178 GNUNET_SQ_query_param_fixed_size (path_info, 186 GNUNET_SQ_query_param_fixed_size (path_info,
179 path_info_len * sizeof (struct GNUNET_PeerIdentity)), 187 path_info_len * sizeof (struct GNUNET_PeerIdentity)),
@@ -387,6 +395,7 @@ sqlite_plugin_del (void *cls)
387 void *data; 395 void *data;
388 size_t dsize; 396 size_t dsize;
389 struct GNUNET_HashCode hc; 397 struct GNUNET_HashCode hc;
398 struct GNUNET_TIME_Absolute now;
390 struct GNUNET_SQ_ResultSpec rs[] = { 399 struct GNUNET_SQ_ResultSpec rs[] = {
391 GNUNET_SQ_result_spec_uint64 (&rowid), 400 GNUNET_SQ_result_spec_uint64 (&rowid),
392 GNUNET_SQ_result_spec_auto_from_type (&hc), 401 GNUNET_SQ_result_spec_auto_from_type (&hc),
@@ -398,27 +407,52 @@ sqlite_plugin_del (void *cls)
398 GNUNET_SQ_query_param_uint64 (&rowid), 407 GNUNET_SQ_query_param_uint64 (&rowid),
399 GNUNET_SQ_query_param_end 408 GNUNET_SQ_query_param_end
400 }; 409 };
410 struct GNUNET_SQ_QueryParam time_params[] = {
411 GNUNET_SQ_query_param_absolute_time (&now),
412 GNUNET_SQ_query_param_end
413 };
401 414
402 LOG (GNUNET_ERROR_TYPE_DEBUG, 415 LOG (GNUNET_ERROR_TYPE_DEBUG,
403 "Processing DEL\n"); 416 "Processing DEL\n");
404 if (SQLITE_ROW != 417 now = GNUNET_TIME_absolute_get ();
405 sqlite3_step (plugin->del_select_stmt)) 418 if (GNUNET_OK !=
419 GNUNET_SQ_bind (plugin->del_expired_stmt,
420 time_params))
406 { 421 {
407 LOG_SQLITE (plugin->dbh, 422 LOG_SQLITE (plugin->dbh,
408 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 423 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
409 "sqlite3_step"); 424 "sqlite3_bind");
410 GNUNET_SQ_reset (plugin->dbh, 425 GNUNET_SQ_reset (plugin->dbh,
411 plugin->del_select_stmt); 426 plugin->del_expired_stmt);
412 return GNUNET_SYSERR; 427 return GNUNET_SYSERR;
413 } 428 }
414 if (GNUNET_OK != 429 if ( (SQLITE_ROW !=
415 GNUNET_SQ_extract_result (plugin->del_select_stmt, 430 sqlite3_step (plugin->del_expired_stmt)) ||
416 rs)) 431 (GNUNET_OK !=
432 GNUNET_SQ_extract_result (plugin->del_expired_stmt,
433 rs)) )
417 { 434 {
418 GNUNET_break (0);
419 GNUNET_SQ_reset (plugin->dbh, 435 GNUNET_SQ_reset (plugin->dbh,
420 plugin->del_select_stmt); 436 plugin->del_expired_stmt);
421 return GNUNET_SYSERR; 437 if (SQLITE_ROW !=
438 sqlite3_step (plugin->del_select_stmt))
439 {
440 LOG_SQLITE (plugin->dbh,
441 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
442 "sqlite3_step");
443 GNUNET_SQ_reset (plugin->dbh,
444 plugin->del_select_stmt);
445 return GNUNET_SYSERR;
446 }
447 if (GNUNET_OK !=
448 GNUNET_SQ_extract_result (plugin->del_select_stmt,
449 rs))
450 {
451 GNUNET_SQ_reset (plugin->dbh,
452 plugin->del_select_stmt);
453 GNUNET_break (0);
454 return GNUNET_SYSERR;
455 }
422 } 456 }
423 GNUNET_SQ_cleanup_result (rs); 457 GNUNET_SQ_cleanup_result (rs);
424 GNUNET_SQ_reset (plugin->dbh, 458 GNUNET_SQ_reset (plugin->dbh,
@@ -709,13 +743,15 @@ libgnunet_plugin_datacache_sqlite_init (void *cls)
709 SQLITE3_EXEC (dbh, "PRAGMA sqlite_temp_store=3"); 743 SQLITE3_EXEC (dbh, "PRAGMA sqlite_temp_store=3");
710 744
711 SQLITE3_EXEC (dbh, 745 SQLITE3_EXEC (dbh,
712 "CREATE TABLE ds090 (" " type INTEGER NOT NULL DEFAULT 0," 746 "CREATE TABLE ds091 ("
713 " expire INTEGER NOT NULL DEFAULT 0," 747 " type INTEGER NOT NULL DEFAULT 0,"
748 " expire INTEGER NOT NULL,"
714 " key BLOB NOT NULL DEFAULT ''," 749 " key BLOB NOT NULL DEFAULT '',"
715 " value BLOB NOT NULL DEFAULT ''," 750 " prox INTEGER NOT NULL,"
751 " value BLOB NOT NULL,"
716 " path BLOB DEFAULT '')"); 752 " path BLOB DEFAULT '')");
717 SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds090 (key,type,expire)"); 753 SQLITE3_EXEC (dbh, "CREATE INDEX idx_hashidx ON ds091 (key,type,expire)");
718 SQLITE3_EXEC (dbh, "CREATE INDEX idx_expire ON ds090 (expire)"); 754 SQLITE3_EXEC (dbh, "CREATE INDEX idx_expire ON ds091 (prox,expire)");
719 plugin = GNUNET_new (struct Plugin); 755 plugin = GNUNET_new (struct Plugin);
720 plugin->env = env; 756 plugin->env = env;
721 plugin->dbh = dbh; 757 plugin->dbh = dbh;
@@ -723,35 +759,42 @@ libgnunet_plugin_datacache_sqlite_init (void *cls)
723 759
724 if ( (SQLITE_OK != 760 if ( (SQLITE_OK !=
725 sq_prepare (plugin->dbh, 761 sq_prepare (plugin->dbh,
726 "INSERT INTO ds090 (type, expire, key, value, path) " 762 "INSERT INTO ds091 (type, expire, key, prox, value, path) "
727 "VALUES (?, ?, ?, ?, ?)", 763 "VALUES (?, ?, ?, ?, ?, ?)",
728 &plugin->insert_stmt)) || 764 &plugin->insert_stmt)) ||
729 (SQLITE_OK != 765 (SQLITE_OK !=
730 sq_prepare (plugin->dbh, 766 sq_prepare (plugin->dbh,
731 "SELECT count(*) FROM ds090 " 767 "SELECT count(*) FROM ds091 "
732 "WHERE key=? AND type=? AND expire >= ?", 768 "WHERE key=? AND type=? AND expire >= ?",
733 &plugin->get_count_stmt)) || 769 &plugin->get_count_stmt)) ||
734 (SQLITE_OK != 770 (SQLITE_OK !=
735 sq_prepare (plugin->dbh, 771 sq_prepare (plugin->dbh,
736 "SELECT value,expire,path FROM ds090 " 772 "SELECT value,expire,path FROM ds091"
737 "WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET ?", 773 " WHERE key=? AND type=? AND expire >= ? LIMIT 1 OFFSET ?",
738 &plugin->get_stmt)) || 774 &plugin->get_stmt)) ||
739 (SQLITE_OK != 775 (SQLITE_OK !=
740 sq_prepare (plugin->dbh, 776 sq_prepare (plugin->dbh,
741 "SELECT _ROWID_,key,value FROM ds090 ORDER BY expire ASC LIMIT 1", 777 "SELECT _ROWID_,key,value FROM ds091"
778 " WHERE expire < ?"
779 " ORDER BY expire ASC LIMIT 1",
780 &plugin->del_expired_stmt)) ||
781 (SQLITE_OK !=
782 sq_prepare (plugin->dbh,
783 "SELECT _ROWID_,key,value FROM ds091"
784 " ORDER BY prox ASC, expire ASC LIMIT 1",
742 &plugin->del_select_stmt)) || 785 &plugin->del_select_stmt)) ||
743 (SQLITE_OK != 786 (SQLITE_OK !=
744 sq_prepare (plugin->dbh, 787 sq_prepare (plugin->dbh,
745 "DELETE FROM ds090 WHERE _ROWID_=?", 788 "DELETE FROM ds091 WHERE _ROWID_=?",
746 &plugin->del_stmt)) || 789 &plugin->del_stmt)) ||
747 (SQLITE_OK != 790 (SQLITE_OK !=
748 sq_prepare (plugin->dbh, 791 sq_prepare (plugin->dbh,
749 "SELECT value,expire,path,key,type FROM ds090 " 792 "SELECT value,expire,path,key,type FROM ds091 "
750 "ORDER BY key LIMIT 1 OFFSET ?", 793 "ORDER BY key LIMIT 1 OFFSET ?",
751 &plugin->get_random_stmt)) || 794 &plugin->get_random_stmt)) ||
752 (SQLITE_OK != 795 (SQLITE_OK !=
753 sq_prepare (plugin->dbh, 796 sq_prepare (plugin->dbh,
754 "SELECT value,expire,path,type,key FROM ds090 " 797 "SELECT value,expire,path,type,key FROM ds091 "
755 "WHERE key>=? AND expire >= ? ORDER BY KEY ASC LIMIT ?", 798 "WHERE key>=? AND expire >= ? ORDER BY KEY ASC LIMIT ?",
756 &plugin->get_closest_stmt)) 799 &plugin->get_closest_stmt))
757 ) 800 )
@@ -807,6 +850,7 @@ libgnunet_plugin_datacache_sqlite_done (void *cls)
807 sqlite3_finalize (plugin->get_count_stmt); 850 sqlite3_finalize (plugin->get_count_stmt);
808 sqlite3_finalize (plugin->get_stmt); 851 sqlite3_finalize (plugin->get_stmt);
809 sqlite3_finalize (plugin->del_select_stmt); 852 sqlite3_finalize (plugin->del_select_stmt);
853 sqlite3_finalize (plugin->del_expired_stmt);
810 sqlite3_finalize (plugin->del_stmt); 854 sqlite3_finalize (plugin->del_stmt);
811 sqlite3_finalize (plugin->get_random_stmt); 855 sqlite3_finalize (plugin->get_random_stmt);
812 sqlite3_finalize (plugin->get_closest_stmt); 856 sqlite3_finalize (plugin->get_closest_stmt);