diff options
author | Omar Tarabai <tarabai@devegypt.com> | 2014-05-14 16:40:45 +0000 |
---|---|---|
committer | Omar Tarabai <tarabai@devegypt.com> | 2014-05-14 16:40:45 +0000 |
commit | a760c1b877f3a9ac17f2577cdc298f793c574407 (patch) | |
tree | 7411c99a1865aa1b1a6e1141172aa9a1378d4db7 /src/peerstore/plugin_peerstore_sqlite.c | |
parent | cb67c0bb1e6f2a5bae0295a75bc1f4b1b9f9f765 (diff) | |
download | gnunet-a760c1b877f3a9ac17f2577cdc298f793c574407.tar.gz gnunet-a760c1b877f3a9ac17f2577cdc298f793c574407.zip |
peerstore helper file
Diffstat (limited to 'src/peerstore/plugin_peerstore_sqlite.c')
-rw-r--r-- | src/peerstore/plugin_peerstore_sqlite.c | 136 |
1 files changed, 67 insertions, 69 deletions
diff --git a/src/peerstore/plugin_peerstore_sqlite.c b/src/peerstore/plugin_peerstore_sqlite.c index ac6d30931..10ab17650 100644 --- a/src/peerstore/plugin_peerstore_sqlite.c +++ b/src/peerstore/plugin_peerstore_sqlite.c | |||
@@ -90,101 +90,67 @@ struct Plugin | |||
90 | /** | 90 | /** |
91 | * Precompiled SQL for selecting from peerstoredata | 91 | * Precompiled SQL for selecting from peerstoredata |
92 | */ | 92 | */ |
93 | sqlite3_stmt *select_peerstoredata_by_ss; | 93 | sqlite3_stmt *select_peerstoredata_by_key; |
94 | 94 | ||
95 | /** | 95 | /** |
96 | * Precompiled SQL for selecting from peerstoredata | 96 | * Precompiled SQL for selecting from peerstoredata |
97 | */ | 97 | */ |
98 | sqlite3_stmt *select_peerstoredata_by_both; | 98 | sqlite3_stmt *select_peerstoredata_by_all; |
99 | 99 | ||
100 | }; | 100 | }; |
101 | 101 | ||
102 | /** | 102 | /** |
103 | * The given 'sqlite' statement has been prepared to be run. | ||
104 | * It will return a record which should be given to the iterator. | ||
105 | * Runs the statement and parses the returned record. | ||
106 | * | ||
107 | * @param plugin plugin context | ||
108 | * @param stmt to run (and then clean up) | ||
109 | * @param iter iterator to call with the result | ||
110 | * @param iter_cls closure for @a iter | ||
111 | * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error | ||
112 | */ | ||
113 | static int | ||
114 | get_record_and_call_iterator (struct Plugin *plugin, | ||
115 | sqlite3_stmt *stmt, | ||
116 | GNUNET_PEERSTORE_RecordIterator iter, void *iter_cls) | ||
117 | { | ||
118 | int ret; | ||
119 | int sret; | ||
120 | const struct GNUNET_PeerIdentity *pid; | ||
121 | const char *sub_system; | ||
122 | const void *value; | ||
123 | size_t value_size; | ||
124 | |||
125 | ret = GNUNET_NO; | ||
126 | if (SQLITE_ROW == (sret = sqlite3_step (stmt))) | ||
127 | { | ||
128 | pid = sqlite3_column_blob(stmt, 0); | ||
129 | sub_system = (const char*) sqlite3_column_text(stmt, 1); | ||
130 | value = sqlite3_column_blob(stmt, 2); | ||
131 | value_size = sqlite3_column_bytes(stmt, 2); | ||
132 | if (NULL != iter) | ||
133 | iter (iter_cls, pid, sub_system, value, value_size); | ||
134 | ret = GNUNET_YES; | ||
135 | } | ||
136 | else | ||
137 | { | ||
138 | if (SQLITE_DONE != sret) | ||
139 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); | ||
140 | } | ||
141 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
142 | LOG_SQLITE (plugin, | ||
143 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
144 | "sqlite3_reset"); | ||
145 | return ret; | ||
146 | |||
147 | } | ||
148 | |||
149 | /** | ||
150 | * Iterate over the records given an optional peer id | 103 | * Iterate over the records given an optional peer id |
151 | * and/or sub system. | 104 | * and/or key. |
152 | * | 105 | * |
153 | * @param cls closure (internal context for the plugin) | 106 | * @param cls closure (internal context for the plugin) |
107 | * @param sub_system name of sub system | ||
154 | * @param peer Peer identity (can be NULL) | 108 | * @param peer Peer identity (can be NULL) |
155 | * @param sub_system name of sub system (can be NULL) | 109 | * @param key entry key string (can be NULL) |
156 | * @param iter function to call with the result | 110 | * @param iter function to call with the result |
157 | * @param iter_cls closure for @a iter | 111 | * @param iter_cls closure for @a iter |
158 | * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error | 112 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error |
159 | */ | 113 | */ |
160 | static int | 114 | static int |
161 | peerstore_sqlite_iterate_records (void *cls, | 115 | peerstore_sqlite_iterate_records (void *cls, |
162 | const struct GNUNET_PeerIdentity *peer, | ||
163 | const char *sub_system, | 116 | const char *sub_system, |
117 | const struct GNUNET_PeerIdentity *peer, | ||
118 | const char *key, | ||
164 | GNUNET_PEERSTORE_RecordIterator iter, void *iter_cls) | 119 | GNUNET_PEERSTORE_RecordIterator iter, void *iter_cls) |
165 | { | 120 | { |
166 | struct Plugin *plugin = cls; | 121 | struct Plugin *plugin = cls; |
167 | sqlite3_stmt *stmt; | 122 | sqlite3_stmt *stmt; |
168 | int err = 0; | 123 | int err = 0; |
124 | int sret; | ||
125 | const char *ret_sub_system; | ||
126 | const struct GNUNET_PeerIdentity *ret_peer; | ||
127 | const char *ret_key; | ||
128 | const void *ret_value; | ||
129 | size_t ret_value_size; | ||
169 | 130 | ||
170 | if(NULL == sub_system && NULL == peer) | 131 | if(NULL == peer && NULL == key) |
132 | { | ||
171 | stmt = plugin->select_peerstoredata; | 133 | stmt = plugin->select_peerstoredata; |
172 | else if(NULL == sub_system) | 134 | err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)); |
135 | } | ||
136 | else if(NULL == key) | ||
173 | { | 137 | { |
174 | stmt = plugin->select_peerstoredata_by_pid; | 138 | stmt = plugin->select_peerstoredata_by_pid; |
175 | err = (SQLITE_OK != sqlite3_bind_blob(stmt, 1, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)); | 139 | err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)) |
140 | || (SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)); | ||
176 | } | 141 | } |
177 | else if(NULL == peer) | 142 | else if(NULL == peer) |
178 | { | 143 | { |
179 | stmt = plugin->select_peerstoredata_by_ss; | 144 | stmt = plugin->select_peerstoredata_by_key; |
180 | err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)); | 145 | err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)) |
146 | || (SQLITE_OK != sqlite3_bind_text(stmt, 3, key, strlen(key) + 1, SQLITE_STATIC)); | ||
181 | } | 147 | } |
182 | else | 148 | else |
183 | { | 149 | { |
184 | stmt = plugin->select_peerstoredata_by_both; | 150 | stmt = plugin->select_peerstoredata_by_all; |
185 | err = | 151 | err = (SQLITE_OK != sqlite3_bind_text(stmt, 1, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)) |
186 | (SQLITE_OK != sqlite3_bind_blob(stmt, 1, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)) | 152 | || (SQLITE_OK != sqlite3_bind_blob(stmt, 2, peer, sizeof(struct GNUNET_PeerIdentity), SQLITE_STATIC)) |
187 | || (SQLITE_OK != sqlite3_bind_text(stmt, 2, sub_system, strlen(sub_system) + 1, SQLITE_STATIC)); | 153 | || (SQLITE_OK != sqlite3_bind_text(stmt, 3, key, strlen(key) + 1, SQLITE_STATIC)); |
188 | } | 154 | } |
189 | 155 | ||
190 | if (err) | 156 | if (err) |
@@ -197,7 +163,31 @@ peerstore_sqlite_iterate_records (void *cls, | |||
197 | "sqlite3_reset"); | 163 | "sqlite3_reset"); |
198 | return GNUNET_SYSERR; | 164 | return GNUNET_SYSERR; |
199 | } | 165 | } |
200 | return get_record_and_call_iterator (plugin, stmt, iter, iter_cls); | 166 | while (SQLITE_ROW == (sret = sqlite3_step (stmt))) |
167 | { | ||
168 | ret_sub_system = (const char *)sqlite3_column_text(stmt, 0); | ||
169 | ret_peer = sqlite3_column_blob(stmt, 1); | ||
170 | ret_key = (const char *)sqlite3_column_text(stmt, 2); | ||
171 | ret_value = sqlite3_column_blob(stmt, 3); | ||
172 | ret_value_size = sqlite3_column_bytes(stmt, 3); | ||
173 | if (NULL != iter) | ||
174 | iter (iter_cls, ret_sub_system, ret_peer, ret_key, ret_value, ret_value_size); | ||
175 | } | ||
176 | if (SQLITE_DONE != sret) | ||
177 | { | ||
178 | LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); | ||
179 | err = 1; | ||
180 | } | ||
181 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
182 | { | ||
183 | LOG_SQLITE (plugin, | ||
184 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
185 | "sqlite3_reset"); | ||
186 | err = 1; | ||
187 | } | ||
188 | if(err) | ||
189 | return GNUNET_SYSERR; | ||
190 | return GNUNET_OK; | ||
201 | } | 191 | } |
202 | 192 | ||
203 | /** | 193 | /** |
@@ -362,17 +352,25 @@ database_setup (struct Plugin *plugin) | |||
362 | "INSERT INTO peerstoredata (sub_system, peer_id, key, value) VALUES (?,?,?,?);", | 352 | "INSERT INTO peerstoredata (sub_system, peer_id, key, value) VALUES (?,?,?,?);", |
363 | &plugin->insert_peerstoredata); | 353 | &plugin->insert_peerstoredata); |
364 | sql_prepare(plugin->dbh, | 354 | sql_prepare(plugin->dbh, |
365 | "SELECT peer_id, sub_system, value FROM peerstoredata", | 355 | "SELECT peer_id, sub_system, value FROM peerstoredata" |
356 | " WHERE sub_system = ?", | ||
366 | &plugin->select_peerstoredata); | 357 | &plugin->select_peerstoredata); |
367 | sql_prepare(plugin->dbh, | 358 | sql_prepare(plugin->dbh, |
368 | "SELECT peer_id, sub_system, value FROM peerstoredata WHERE peer_id = ?", | 359 | "SELECT peer_id, sub_system, value FROM peerstoredata" |
360 | " WHERE sub_system = ?" | ||
361 | " AND peer_id = ?", | ||
369 | &plugin->select_peerstoredata_by_pid); | 362 | &plugin->select_peerstoredata_by_pid); |
370 | sql_prepare(plugin->dbh, | 363 | sql_prepare(plugin->dbh, |
371 | "SELECT peer_id, sub_system, value FROM peerstoredata WHERE sub_system = ?", | 364 | "SELECT peer_id, sub_system, value FROM peerstoredata" |
372 | &plugin->select_peerstoredata_by_ss); | 365 | " WHERE sub_system = ?" |
366 | " AND key = ?", | ||
367 | &plugin->select_peerstoredata_by_key); | ||
373 | sql_prepare(plugin->dbh, | 368 | sql_prepare(plugin->dbh, |
374 | "SELECT peer_id, sub_system, value FROM peerstoredata WHERE peer_id = ? AND sub_system = ?", | 369 | "SELECT peer_id, sub_system, value FROM peerstoredata" |
375 | &plugin->select_peerstoredata_by_both); | 370 | " WHERE sub_system = ?" |
371 | " AND peer_id = ?" | ||
372 | " AND key = ?", | ||
373 | &plugin->select_peerstoredata_by_all); | ||
376 | 374 | ||
377 | return GNUNET_OK; | 375 | return GNUNET_OK; |
378 | } | 376 | } |