[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] r33322 - in gnunet/src: include peerstore
From: |
gnunet |
Subject: |
[GNUnet-SVN] r33322 - in gnunet/src: include peerstore |
Date: |
Mon, 19 May 2014 14:07:15 +0200 |
Author: otarabai
Date: 2014-05-19 14:07:15 +0200 (Mon, 19 May 2014)
New Revision: 33322
Modified:
gnunet/src/include/gnunet_peerstore_plugin.h
gnunet/src/peerstore/gnunet-service-peerstore.c
gnunet/src/peerstore/plugin_peerstore_sqlite.c
gnunet/src/peerstore/test_peerstore_api.c
Log:
peerstore: record expiry
Modified: gnunet/src/include/gnunet_peerstore_plugin.h
===================================================================
--- gnunet/src/include/gnunet_peerstore_plugin.h 2014-05-19 12:03:26 UTC
(rev 33321)
+++ gnunet/src/include/gnunet_peerstore_plugin.h 2014-05-19 12:07:15 UTC
(rev 33322)
@@ -89,6 +89,17 @@
const char *key,
GNUNET_PEERSTORE_Processor iter, void *iter_cls);
+ /**
+ * 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
+ */
+ int
+ (*expire_records) (void *cls,
+ struct GNUNET_TIME_Absolute now);
+
};
Modified: gnunet/src/peerstore/gnunet-service-peerstore.c
===================================================================
--- gnunet/src/peerstore/gnunet-service-peerstore.c 2014-05-19 12:03:26 UTC
(rev 33321)
+++ gnunet/src/peerstore/gnunet-service-peerstore.c 2014-05-19 12:07:15 UTC
(rev 33322)
@@ -30,9 +30,13 @@
#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.
*/
static const struct GNUNET_CONFIGURATION_Handle *cfg;
@@ -65,7 +69,24 @@
}
}
+/**
+ * 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.
*
@@ -245,6 +266,7 @@
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,
Modified: gnunet/src/peerstore/plugin_peerstore_sqlite.c
===================================================================
--- gnunet/src/peerstore/plugin_peerstore_sqlite.c 2014-05-19 12:03:26 UTC
(rev 33321)
+++ gnunet/src/peerstore/plugin_peerstore_sqlite.c 2014-05-19 12:07:15 UTC
(rev 33322)
@@ -97,9 +97,52 @@
*/
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.
*
@@ -194,6 +237,42 @@
}
/**
+ * 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.
* One key can store multiple values.
@@ -218,7 +297,16 @@
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)
|| SQLITE_OK != sqlite3_bind_text(stmt, 3, key, strlen(key) + 1,
SQLITE_STATIC)
@@ -377,6 +465,17 @@
" 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;
}
@@ -430,6 +529,7 @@
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;
}
Modified: gnunet/src/peerstore/test_peerstore_api.c
===================================================================
--- gnunet/src/peerstore/test_peerstore_api.c 2014-05-19 12:03:26 UTC (rev
33321)
+++ gnunet/src/peerstore/test_peerstore_api.c 2014-05-19 12:07:15 UTC (rev
33322)
@@ -84,9 +84,11 @@
struct GNUNET_PeerIdentity pid;
char *val = "peerstore-test-value";
size_t val_size = strlen(val);
+ struct GNUNET_TIME_Absolute expiry;
ok = 0;
memset (&pid, 32, sizeof (pid));
+ expiry = GNUNET_TIME_absolute_get();
h = GNUNET_PEERSTORE_connect(cfg);
GNUNET_assert(NULL != h);
GNUNET_PEERSTORE_store(h,
@@ -95,7 +97,7 @@
"peerstore-test-key",
val,
val_size,
- GNUNET_TIME_UNIT_FOREVER_ABS,
+ expiry,
&store_cont,
NULL);
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [GNUnet-SVN] r33322 - in gnunet/src: include peerstore,
gnunet <=