diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-04-27 18:40:04 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-04-27 18:40:04 +0000 |
commit | a8504a84ee0161e36180811b1d44fd1dc3ab9b17 (patch) | |
tree | ee2fe1ae4fddd83759d906deeaae8d70eae98fd7 /src | |
parent | 4637bb9965f3b945426d888f353e566da75ee32a (diff) | |
download | gnunet-a8504a84ee0161e36180811b1d44fd1dc3ab9b17.tar.gz gnunet-a8504a84ee0161e36180811b1d44fd1dc3ab9b17.zip |
PEERSTORE sqlite plugin store function
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_peerstore_plugin.h | 19 | ||||
-rw-r--r-- | src/peerstore/gnunet-service-peerstore.c | 12 | ||||
-rw-r--r-- | src/peerstore/plugin_peerstore_sqlite.c | 50 |
3 files changed, 78 insertions, 3 deletions
diff --git a/src/include/gnunet_peerstore_plugin.h b/src/include/gnunet_peerstore_plugin.h index dc2322459..8dcc1ec2a 100644 --- a/src/include/gnunet_peerstore_plugin.h +++ b/src/include/gnunet_peerstore_plugin.h | |||
@@ -49,6 +49,25 @@ struct GNUNET_PEERSTORE_PluginFunctions | |||
49 | */ | 49 | */ |
50 | void *cls; | 50 | void *cls; |
51 | 51 | ||
52 | /** | ||
53 | * Store a record in the peerstore. | ||
54 | * Key is the combination of sub system and peer identity. | ||
55 | * One key can store multiple values. | ||
56 | * | ||
57 | * @param cls closure (internal context for the plugin) | ||
58 | * @param sub_system name of the GNUnet sub system responsible | ||
59 | * @param peer peer identity | ||
60 | * @param value value to be stored | ||
61 | * @param size size of value to be stored | ||
62 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR | ||
63 | */ | ||
64 | int | ||
65 | (*store_record) (void *cls, | ||
66 | const char *sub_system, | ||
67 | const struct GNUNET_PeerIdentity *peer, | ||
68 | const void *value, | ||
69 | size_t size); | ||
70 | |||
52 | }; | 71 | }; |
53 | 72 | ||
54 | 73 | ||
diff --git a/src/peerstore/gnunet-service-peerstore.c b/src/peerstore/gnunet-service-peerstore.c index 418e33703..d0359ad50 100644 --- a/src/peerstore/gnunet-service-peerstore.c +++ b/src/peerstore/gnunet-service-peerstore.c | |||
@@ -33,6 +33,11 @@ | |||
33 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | 33 | static const struct GNUNET_CONFIGURATION_Handle *cfg; |
34 | 34 | ||
35 | /** | 35 | /** |
36 | * Database plugin library name | ||
37 | */ | ||
38 | char *db_lib_name; | ||
39 | |||
40 | /** | ||
36 | * Database handle | 41 | * Database handle |
37 | */ | 42 | */ |
38 | static struct GNUNET_PEERSTORE_PluginFunctions *db; | 43 | static struct GNUNET_PEERSTORE_PluginFunctions *db; |
@@ -47,6 +52,12 @@ static void | |||
47 | shutdown_task (void *cls, | 52 | shutdown_task (void *cls, |
48 | const struct GNUNET_SCHEDULER_TaskContext *tc) | 53 | const struct GNUNET_SCHEDULER_TaskContext *tc) |
49 | { | 54 | { |
55 | if(NULL != db_lib_name) | ||
56 | { | ||
57 | GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, db)); | ||
58 | GNUNET_free (db_lib_name); | ||
59 | db_lib_name = NULL; | ||
60 | } | ||
50 | } | 61 | } |
51 | 62 | ||
52 | 63 | ||
@@ -79,7 +90,6 @@ run (void *cls, | |||
79 | {NULL, NULL, 0, 0} | 90 | {NULL, NULL, 0, 0} |
80 | }; | 91 | }; |
81 | char *database; | 92 | char *database; |
82 | char *db_lib_name; | ||
83 | 93 | ||
84 | cfg = c; | 94 | cfg = c; |
85 | if (GNUNET_OK != | 95 | if (GNUNET_OK != |
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c index ff6ae9f67..41b6044b3 100644 --- a/src/peerstore/plugin_peerstore_sqlite.c +++ b/src/peerstore/plugin_peerstore_sqlite.c | |||
@@ -80,6 +80,51 @@ struct Plugin | |||
80 | }; | 80 | }; |
81 | 81 | ||
82 | /** | 82 | /** |
83 | * Store a record in the peerstore. | ||
84 | * Key is the combination of sub system and peer identity. | ||
85 | * One key can store multiple values. | ||
86 | * | ||
87 | * @param cls closure (internal context for the plugin) | ||
88 | * @param sub_system name of the GNUnet sub system responsible | ||
89 | * @param peer peer identity | ||
90 | * @param value value to be stored | ||
91 | * @param size size of value to be stored | ||
92 | * @return #GNUNET_OK on success, else #GNUNET_SYSERR | ||
93 | */ | ||
94 | static int | ||
95 | peerstore_sqlite_store_record (void *cls, | ||
96 | const char *sub_system, | ||
97 | const struct GNUNET_PeerIdentity *peer, | ||
98 | const void *value, | ||
99 | size_t size) | ||
100 | { | ||
101 | struct Plugin *plugin = cls; | ||
102 | sqlite3_stmt *stmt = plugin->insert_peerstoredata; | ||
103 | |||
104 | //FIXME: check if value exists with the same key first | ||
105 | |||
106 | if(SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, sizeof(sub_system), SQLITE_STATIC) | ||
107 | || SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC) | ||
108 | || SQLITE_OK != sqlite3_bind_blob(stmt, 3, value, size, SQLITE_STATIC)) | ||
109 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
110 | "sqlite3_bind"); | ||
111 | else if (SQLITE_DONE != sqlite3_step (stmt)) | ||
112 | { | ||
113 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
114 | "sqlite3_step"); | ||
115 | } | ||
116 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
117 | { | ||
118 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
119 | "sqlite3_reset"); | ||
120 | return GNUNET_SYSERR; | ||
121 | } | ||
122 | |||
123 | return GNUNET_OK; | ||
124 | } | ||
125 | |||
126 | |||
127 | /** | ||
83 | * @brief Prepare a SQL statement | 128 | * @brief Prepare a SQL statement |
84 | * | 129 | * |
85 | * @param dbh handle to the database | 130 | * @param dbh handle to the database |
@@ -243,7 +288,8 @@ libgnunet_plugin_peerstore_sqlite_init (void *cls) | |||
243 | } | 288 | } |
244 | api = GNUNET_new (struct GNUNET_PEERSTORE_PluginFunctions); | 289 | api = GNUNET_new (struct GNUNET_PEERSTORE_PluginFunctions); |
245 | api->cls = &plugin; | 290 | api->cls = &plugin; |
246 | LOG(GNUNET_ERROR_TYPE_INFO, "Sqlite plugin is running\n"); | 291 | api->store_record = &peerstore_sqlite_store_record; |
292 | LOG(GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is running\n"); | ||
247 | return api; | 293 | return api; |
248 | } | 294 | } |
249 | 295 | ||
@@ -262,7 +308,7 @@ libgnunet_plugin_peerstore_sqlite_done (void *cls) | |||
262 | database_shutdown (plugin); | 308 | database_shutdown (plugin); |
263 | plugin->cfg = NULL; | 309 | plugin->cfg = NULL; |
264 | GNUNET_free (api); | 310 | GNUNET_free (api); |
265 | LOG (GNUNET_ERROR_TYPE_INFO, "Sqlite plugin is finished\n"); | 311 | LOG (GNUNET_ERROR_TYPE_DEBUG, "Sqlite plugin is finished\n"); |
266 | return NULL; | 312 | return NULL; |
267 | 313 | ||
268 | } | 314 | } |