diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-05-19 12:07:15 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-05-19 12:07:15 +0000 |
commit | aa0f830c863cb8ddb4187f49134d9fbe2de70045 (patch) | |
tree | 569209cd96e18b7c508afea1c1f2f3fa9aa579eb /src/peerstore/plugin_peerstore_sqlite.c | |
parent | 7528bcf5a5af0d90c06aeab964e5c6471900ae06 (diff) | |
download | gnunet-aa0f830c863cb8ddb4187f49134d9fbe2de70045.tar.gz gnunet-aa0f830c863cb8ddb4187f49134d9fbe2de70045.zip |
peerstore: record expiry
Diffstat (limited to 'src/peerstore/plugin_peerstore_sqlite.c')
-rw-r--r-- | src/peerstore/plugin_peerstore_sqlite.c | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c index 9d044aa07..ead2aade6 100644 --- a/src/peerstore/plugin_peerstore_sqlite.c +++ b/src/peerstore/plugin_peerstore_sqlite.c | |||
@@ -97,9 +97,52 @@ struct Plugin | |||
97 | */ | 97 | */ |
98 | sqlite3_stmt *select_peerstoredata_by_all; | 98 | sqlite3_stmt *select_peerstoredata_by_all; |
99 | 99 | ||
100 | /** | ||
101 | * Precompiled SQL for selecting from peerstoredata | ||
102 | */ | ||
103 | sqlite3_stmt *select_peerstoredata_by_all_and_value; | ||
104 | |||
105 | /** | ||
106 | * Precompiled SQL for deleting expired records from peerstoredata | ||
107 | */ | ||
108 | sqlite3_stmt *expire_peerstoredata; | ||
109 | |||
100 | }; | 110 | }; |
101 | 111 | ||
102 | /** | 112 | /** |
113 | * Delete expired records (expiry < now) | ||
114 | * | ||
115 | * @param cls closure (internal context for the plugin) | ||
116 | * @param now time to use as reference | ||
117 | * @return number of records deleted | ||
118 | */ | ||
119 | static int | ||
120 | peerstore_sqlite_expire_records(void *cls, | ||
121 | struct GNUNET_TIME_Absolute now) | ||
122 | { | ||
123 | struct Plugin *plugin = cls; | ||
124 | sqlite3_stmt *stmt = plugin->expire_peerstoredata; | ||
125 | |||
126 | if(SQLITE_OK != sqlite3_bind_int64(stmt, 1, (sqlite3_int64)now.abs_value_us)) | ||
127 | { | ||
128 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind"); | ||
129 | } | ||
130 | else if (SQLITE_DONE != sqlite3_step (stmt)) | ||
131 | { | ||
132 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
133 | "sqlite3_step"); | ||
134 | } | ||
135 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
136 | { | ||
137 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
138 | "sqlite3_reset"); | ||
139 | return 0; | ||
140 | } | ||
141 | return sqlite3_changes(plugin->dbh); | ||
142 | |||
143 | } | ||
144 | |||
145 | /** | ||
103 | * Iterate over the records given an optional peer id | 146 | * Iterate over the records given an optional peer id |
104 | * and/or key. | 147 | * and/or key. |
105 | * | 148 | * |
@@ -194,6 +237,42 @@ peerstore_sqlite_iterate_records (void *cls, | |||
194 | } | 237 | } |
195 | 238 | ||
196 | /** | 239 | /** |
240 | * Checks if a record with the given information | ||
241 | * already exists | ||
242 | * | ||
243 | * @return #GNUNET_YES / #GNUNET_NO | ||
244 | * | ||
245 | static int | ||
246 | check_existing(void *cls, | ||
247 | const char *sub_system, | ||
248 | const struct GNUNET_PeerIdentity *peer, | ||
249 | const char *key, | ||
250 | const void *value, | ||
251 | size_t size) | ||
252 | { | ||
253 | struct Plugin *plugin = cls; | ||
254 | sqlite3_stmt *stmt = plugin->select_peerstoredata_by_all_and_value; | ||
255 | int sret; | ||
256 | |||
257 | if(SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC) | ||
258 | || SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC) | ||
259 | || SQLITE_OK != sqlite3_bind_text(stmt, 3, key, strlen(key) + 1, SQLITE_STATIC) | ||
260 | || SQLITE_OK != sqlite3_bind_blob(stmt, 4, value, size, SQLITE_STATIC)) | ||
261 | { | ||
262 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
263 | "sqlite3_bind"); | ||
264 | sqlite3_reset(stmt); | ||
265 | return GNUNET_NO; | ||
266 | } | ||
267 | sret = sqlite3_step (stmt); | ||
268 | sqlite3_reset(stmt); | ||
269 | if(SQLITE_ROW == sret) | ||
270 | return GNUNET_YES; | ||
271 | return GNUNET_NO; | ||
272 | |||
273 | }*/ | ||
274 | |||
275 | /** | ||
197 | * Store a record in the peerstore. | 276 | * Store a record in the peerstore. |
198 | * Key is the combination of sub system and peer identity. | 277 | * Key is the combination of sub system and peer identity. |
199 | * One key can store multiple values. | 278 | * One key can store multiple values. |
@@ -218,6 +297,15 @@ peerstore_sqlite_store_record(void *cls, | |||
218 | sqlite3_stmt *stmt = plugin->insert_peerstoredata; | 297 | sqlite3_stmt *stmt = plugin->insert_peerstoredata; |
219 | 298 | ||
220 | //FIXME: check if value exists with the same key first | 299 | //FIXME: check if value exists with the same key first |
300 | /*if(GNUNET_YES == check_existing(cls, | ||
301 | sub_system, | ||
302 | peer, | ||
303 | key, | ||
304 | value, | ||
305 | size)) | ||
306 | { | ||
307 | |||
308 | }*/ | ||
221 | 309 | ||
222 | if(SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC) | 310 | if(SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC) |
223 | || SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC) | 311 | || SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC) |
@@ -377,6 +465,17 @@ database_setup (struct Plugin *plugin) | |||
377 | " AND peer_id = ?" | 465 | " AND peer_id = ?" |
378 | " AND key = ?", | 466 | " AND key = ?", |
379 | &plugin->select_peerstoredata_by_all); | 467 | &plugin->select_peerstoredata_by_all); |
468 | sql_prepare(plugin->dbh, | ||
469 | "SELECT * FROM peerstoredata" | ||
470 | " WHERE sub_system = ?" | ||
471 | " AND peer_id = ?" | ||
472 | " AND key = ?" | ||
473 | " AND value = ?", | ||
474 | &plugin->select_peerstoredata_by_all_and_value); | ||
475 | sql_prepare(plugin->dbh, | ||
476 | "DELETE FROM peerstoredata" | ||
477 | " WHERE expiry < ?", | ||
478 | &plugin->expire_peerstoredata); | ||
380 | 479 | ||
381 | return GNUNET_OK; | 480 | return GNUNET_OK; |
382 | } | 481 | } |
@@ -430,6 +529,7 @@ libgnunet_plugin_peerstore_sqlite_init (void *cls) | |||
430 | api->cls = &plugin; | 529 | api->cls = &plugin; |
431 | api->store_record = &peerstore_sqlite_store_record; | 530 | api->store_record = &peerstore_sqlite_store_record; |
432 | api->iterate_records = &peerstore_sqlite_iterate_records; | 531 | api->iterate_records = &peerstore_sqlite_iterate_records; |
532 | api->expire_records = &peerstore_sqlite_expire_records; | ||
433 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is running\n"); | 533 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is running\n"); |
434 | return api; | 534 | return api; |
435 | } | 535 | } |