#include "peerstore_common.h"
//TODO: GNUNET_SERVER_receive_done() ?
-//TODO: implement value lifetime
+
+/**
+ * Interval for expired records cleanup (in seconds)
+ */
+#define CLEANUP_INTERVAL 300 /* 5mins */
/**
* Our configuration.
}
}
+/**
+ * Deletes any expired records from storage
+ */
+static void
+cleanup_expired_records(void *cls,
+ const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+ int deleted;
+
+ GNUNET_assert(NULL != db);
+ deleted = db->expire_records(db->cls, GNUNET_TIME_absolute_get());
+ GNUNET_log(GNUNET_ERROR_TYPE_INFO, "%d records expired.\n", deleted);
+ GNUNET_SCHEDULER_add_delayed(
+ GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, CLEANUP_INTERVAL),
+ &cleanup_expired_records, NULL);
+}
+
/**
* A client disconnected. Remove all of its data structure entries.
GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Could not load database backend `%s'\n", db_lib_name);
else
{
+ cleanup_expired_records(NULL, NULL);
GNUNET_SERVER_add_handlers (server, handlers);
GNUNET_SERVER_disconnect_notify (server,
&handle_client_disconnect,
*/
sqlite3_stmt *select_peerstoredata_by_all;
+ /**
+ * Precompiled SQL for selecting from peerstoredata
+ */
+ sqlite3_stmt *select_peerstoredata_by_all_and_value;
+
+ /**
+ * Precompiled SQL for deleting expired records from peerstoredata
+ */
+ sqlite3_stmt *expire_peerstoredata;
+
};
+/**
+ * Delete expired records (expiry < now)
+ *
+ * @param cls closure (internal context for the plugin)
+ * @param now time to use as reference
+ * @return number of records deleted
+ */
+static int
+peerstore_sqlite_expire_records(void *cls,
+ struct GNUNET_TIME_Absolute now)
+{
+ struct Plugin *plugin = cls;
+ sqlite3_stmt *stmt = plugin->expire_peerstoredata;
+
+ if(SQLITE_OK != sqlite3_bind_int64(stmt, 1, (sqlite3_int64)now.abs_value_us))
+ {
+ LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, "sqlite3_bind");
+ }
+ else if (SQLITE_DONE != sqlite3_step (stmt))
+ {
+ LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
+ "sqlite3_step");
+ }
+ if (SQLITE_OK != sqlite3_reset (stmt))
+ {
+ LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
+ "sqlite3_reset");
+ return 0;
+ }
+ return sqlite3_changes(plugin->dbh);
+
+}
+
/**
* Iterate over the records given an optional peer id
* and/or key.
return GNUNET_OK;
}
+/**
+ * Checks if a record with the given information
+ * already exists
+ *
+ * @return #GNUNET_YES / #GNUNET_NO
+ *
+static int
+check_existing(void *cls,
+ const char *sub_system,
+ const struct GNUNET_PeerIdentity *peer,
+ const char *key,
+ const void *value,
+ size_t size)
+{
+ struct Plugin *plugin = cls;
+ sqlite3_stmt *stmt = plugin->select_peerstoredata_by_all_and_value;
+ int sret;
+
+ if(SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)
+ || SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)
+ || SQLITE_OK != sqlite3_bind_text(stmt, 3, key, strlen(key) + 1, SQLITE_STATIC)
+ || SQLITE_OK != sqlite3_bind_blob(stmt, 4, value, size, SQLITE_STATIC))
+ {
+ LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
+ "sqlite3_bind");
+ sqlite3_reset(stmt);
+ return GNUNET_NO;
+ }
+ sret = sqlite3_step (stmt);
+ sqlite3_reset(stmt);
+ if(SQLITE_ROW == sret)
+ return GNUNET_YES;
+ return GNUNET_NO;
+
+}*/
+
/**
* Store a record in the peerstore.
* Key is the combination of sub system and peer identity.
sqlite3_stmt *stmt = plugin->insert_peerstoredata;
//FIXME: check if value exists with the same key first
+ /*if(GNUNET_YES == check_existing(cls,
+ sub_system,
+ peer,
+ key,
+ value,
+ size))
+ {
+
+ }*/
if(SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)
|| SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)
" AND peer_id = ?"
" AND key = ?",
&plugin->select_peerstoredata_by_all);
+ sql_prepare(plugin->dbh,
+ "SELECT * FROM peerstoredata"
+ " WHERE sub_system = ?"
+ " AND peer_id = ?"
+ " AND key = ?"
+ " AND value = ?",
+ &plugin->select_peerstoredata_by_all_and_value);
+ sql_prepare(plugin->dbh,
+ "DELETE FROM peerstoredata"
+ " WHERE expiry < ?",
+ &plugin->expire_peerstoredata);
return GNUNET_OK;
}
api->cls = &plugin;
api->store_record = &peerstore_sqlite_store_record;
api->iterate_records = &peerstore_sqlite_iterate_records;
+ api->expire_records = &peerstore_sqlite_expire_records;
LOG(GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is running\n");
return api;
}