diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-02-21 09:15:27 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-02-21 09:15:27 +0000 |
commit | 5c41d90016ad0c48f8069932ecf40ded9af51399 (patch) | |
tree | 77ce31e48472c975db6474f4d8163cfa37d7bdb2 /src/namestore/plugin_namestore_sqlite.c | |
parent | 156e695abdb745dae8b3ee7821b518599452d27a (diff) | |
download | gnunet-5c41d90016ad0c48f8069932ecf40ded9af51399.tar.gz gnunet-5c41d90016ad0c48f8069932ecf40ded9af51399.zip |
-skeleton for sqlite database plugin
Diffstat (limited to 'src/namestore/plugin_namestore_sqlite.c')
-rw-r--r-- | src/namestore/plugin_namestore_sqlite.c | 649 |
1 files changed, 649 insertions, 0 deletions
diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c new file mode 100644 index 000000000..9acece9b9 --- /dev/null +++ b/src/namestore/plugin_namestore_sqlite.c | |||
@@ -0,0 +1,649 @@ | |||
1 | /* | ||
2 | * This file is part of GNUnet | ||
3 | * (C) 2009, 2011, 2012 Christian Grothoff (and other contributing authors) | ||
4 | * | ||
5 | * GNUnet is free software; you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published | ||
7 | * by the Free Software Foundation; either version 3, or (at your | ||
8 | * option) any later version. | ||
9 | * | ||
10 | * GNUnet is distributed in the hope that it will be useful, but | ||
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | * General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with GNUnet; see the file COPYING. If not, write to the | ||
17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | * Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file namestore/plugin_namestore_sqlite.c | ||
23 | * @brief sqlite-based namestore backend | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_namestore_plugin.h" | ||
29 | #include <sqlite3.h> | ||
30 | |||
31 | /** | ||
32 | * Log an error message at log-level 'level' that indicates | ||
33 | * a failure of the command 'cmd' on file 'filename' | ||
34 | * with the message given by strerror(errno). | ||
35 | */ | ||
36 | #define LOG_SQLITE(db, msg, level, cmd) do { GNUNET_log_from (level, "namestore-sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); if (msg != NULL) GNUNET_asprintf(msg, _("`%s' failed at %s:%u with error: %s"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0) | ||
37 | |||
38 | #define LOG(kind,...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__) | ||
39 | |||
40 | |||
41 | /** | ||
42 | * Context for all functions in this plugin. | ||
43 | */ | ||
44 | struct Plugin | ||
45 | { | ||
46 | |||
47 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
48 | |||
49 | /** | ||
50 | * Database filename. | ||
51 | */ | ||
52 | char *fn; | ||
53 | |||
54 | /** | ||
55 | * Native SQLite database handle. | ||
56 | */ | ||
57 | sqlite3 *dbh; | ||
58 | |||
59 | /** | ||
60 | * Precompiled SQL for put record | ||
61 | */ | ||
62 | sqlite3_stmt *put_record; | ||
63 | |||
64 | /** | ||
65 | * Precompiled SQL for put node | ||
66 | */ | ||
67 | sqlite3_stmt *put_node; | ||
68 | |||
69 | /** | ||
70 | * Precompiled SQL for put signature | ||
71 | */ | ||
72 | sqlite3_stmt *put_signature; | ||
73 | |||
74 | /** | ||
75 | * Precompiled SQL for iterate records | ||
76 | */ | ||
77 | sqlite3_stmt *iterate_records; | ||
78 | |||
79 | /** | ||
80 | * Precompiled SQL for get node | ||
81 | */ | ||
82 | sqlite3_stmt *get_node; | ||
83 | |||
84 | /** | ||
85 | * Precompiled SQL for get signature | ||
86 | */ | ||
87 | sqlite3_stmt *get_signature; | ||
88 | |||
89 | /** | ||
90 | * Precompiled SQL for delete zone | ||
91 | */ | ||
92 | sqlite3_stmt *delete_zone; | ||
93 | |||
94 | /** | ||
95 | * Precompiled SQL for deleting old zone in 'put_signature' | ||
96 | */ | ||
97 | sqlite3_stmt *delete_old_zone; | ||
98 | |||
99 | }; | ||
100 | |||
101 | |||
102 | /** | ||
103 | * @brief Prepare a SQL statement | ||
104 | * | ||
105 | * @param dbh handle to the database | ||
106 | * @param zSql SQL statement, UTF-8 encoded | ||
107 | * @param ppStmt set to the prepared statement | ||
108 | * @return 0 on success | ||
109 | */ | ||
110 | static int | ||
111 | sq_prepare (sqlite3 * dbh, const char *zSql, sqlite3_stmt ** ppStmt) | ||
112 | { | ||
113 | char *dummy; | ||
114 | int result; | ||
115 | |||
116 | result = | ||
117 | sqlite3_prepare_v2 (dbh, zSql, strlen (zSql), ppStmt, | ||
118 | (const char **) &dummy); | ||
119 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
120 | "Prepared `%s' / %p: %d\n", zSql, *ppStmt, result); | ||
121 | return result; | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Create our database indices. | ||
127 | * | ||
128 | * @param dbh handle to the database | ||
129 | */ | ||
130 | static void | ||
131 | create_indices (sqlite3 * dbh) | ||
132 | { | ||
133 | /* create indices */ | ||
134 | if (SQLITE_OK != | ||
135 | sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS idx_hash ON ns090 (hash)", | ||
136 | NULL, NULL, NULL)) | ||
137 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
138 | "Failed to create indices: %s\n", sqlite3_errmsg (dbh)); | ||
139 | } | ||
140 | |||
141 | |||
142 | #if 0 | ||
143 | #define CHECK(a) GNUNET_break(a) | ||
144 | #define ENULL NULL | ||
145 | #else | ||
146 | #define ENULL &e | ||
147 | #define ENULL_DEFINED 1 | ||
148 | #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); } | ||
149 | #endif | ||
150 | |||
151 | |||
152 | /** | ||
153 | * Initialize the database connections and associated | ||
154 | * data structures (create tables and indices | ||
155 | * as needed as well). | ||
156 | * | ||
157 | * @param plugin the plugin context (state for this module) | ||
158 | * @return GNUNET_OK on success | ||
159 | */ | ||
160 | static int | ||
161 | database_setup (struct Plugin *plugin) | ||
162 | { | ||
163 | sqlite3_stmt *stmt; | ||
164 | char *afsdir; | ||
165 | #if ENULL_DEFINED | ||
166 | char *e; | ||
167 | #endif | ||
168 | |||
169 | if (GNUNET_OK != | ||
170 | GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "namestore-sqlite", | ||
171 | "FILENAME", &afsdir)) | ||
172 | { | ||
173 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
174 | _ ("Option `%s' in section `%s' missing in configuration!\n"), | ||
175 | "FILENAME", "namestore-sqlite"); | ||
176 | return GNUNET_SYSERR; | ||
177 | } | ||
178 | if (GNUNET_OK != GNUNET_DISK_file_test (afsdir)) | ||
179 | { | ||
180 | if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir)) | ||
181 | { | ||
182 | GNUNET_break (0); | ||
183 | GNUNET_free (afsdir); | ||
184 | return GNUNET_SYSERR; | ||
185 | } | ||
186 | } | ||
187 | #ifdef ENABLE_NLS | ||
188 | plugin->fn = | ||
189 | GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), nl_langinfo (CODESET)); | ||
190 | #else | ||
191 | plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), "UTF-8"); /* good luck */ | ||
192 | #endif | ||
193 | GNUNET_free (afsdir); | ||
194 | |||
195 | /* Open database and precompile statements */ | ||
196 | if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK) | ||
197 | { | ||
198 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
199 | _("Unable to initialize SQLite: %s.\n"), | ||
200 | sqlite3_errmsg (plugin->dbh)); | ||
201 | return GNUNET_SYSERR; | ||
202 | } | ||
203 | CHECK (SQLITE_OK == | ||
204 | sqlite3_exec (plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL, | ||
205 | ENULL)); | ||
206 | CHECK (SQLITE_OK == | ||
207 | sqlite3_exec (plugin->dbh, "PRAGMA synchronous=OFF", NULL, NULL, | ||
208 | ENULL)); | ||
209 | CHECK (SQLITE_OK == | ||
210 | sqlite3_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF", NULL, NULL, | ||
211 | ENULL)); | ||
212 | CHECK (SQLITE_OK == | ||
213 | sqlite3_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL", NULL, | ||
214 | NULL, ENULL)); | ||
215 | CHECK (SQLITE_OK == | ||
216 | sqlite3_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL, | ||
217 | ENULL)); | ||
218 | CHECK (SQLITE_OK == | ||
219 | sqlite3_exec (plugin->dbh, "PRAGMA count_changes=OFF", NULL, NULL, | ||
220 | ENULL)); | ||
221 | CHECK (SQLITE_OK == | ||
222 | sqlite3_exec (plugin->dbh, "PRAGMA page_size=4092", NULL, NULL, | ||
223 | ENULL)); | ||
224 | |||
225 | // CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS)); ? | ||
226 | |||
227 | |||
228 | /* We have to do it here, because otherwise precompiling SQL might fail */ | ||
229 | CHECK (SQLITE_OK == | ||
230 | sq_prepare (plugin->dbh, | ||
231 | "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns090'", | ||
232 | &stmt)); | ||
233 | if ((sqlite3_step (stmt) == SQLITE_DONE) && | ||
234 | (sqlite3_exec | ||
235 | (plugin->dbh, | ||
236 | "CREATE TABLE ns090 (" " repl INT4 NOT NULL DEFAULT 0," | ||
237 | " type INT4 NOT NULL DEFAULT 0," " prio INT4 NOT NULL DEFAULT 0," | ||
238 | " anonLevel INT4 NOT NULL DEFAULT 0," | ||
239 | " expire INT8 NOT NULL DEFAULT 0," " rvalue INT8 NOT NULL," | ||
240 | " hash TEXT NOT NULL DEFAULT ''," " vhash TEXT NOT NULL DEFAULT ''," | ||
241 | " value BLOB NOT NULL DEFAULT '')", NULL, NULL, NULL) != SQLITE_OK)) | ||
242 | { | ||
243 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec"); | ||
244 | sqlite3_finalize (stmt); | ||
245 | return GNUNET_SYSERR; | ||
246 | } | ||
247 | sqlite3_finalize (stmt); | ||
248 | create_indices (plugin->dbh); | ||
249 | |||
250 | if ((sq_prepare | ||
251 | (plugin->dbh, | ||
252 | "UPDATE gn090 " | ||
253 | "SET prio = prio + ?, expire = MAX(expire,?) WHERE _ROWID_ = ?", | ||
254 | &plugin->put_record) != SQLITE_OK) ) | ||
255 | { | ||
256 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "precompiling"); | ||
257 | return GNUNET_SYSERR; | ||
258 | } | ||
259 | return GNUNET_OK; | ||
260 | } | ||
261 | |||
262 | |||
263 | /** | ||
264 | * Shutdown database connection and associate data | ||
265 | * structures. | ||
266 | * @param plugin the plugin context (state for this module) | ||
267 | */ | ||
268 | static void | ||
269 | database_shutdown (struct Plugin *plugin) | ||
270 | { | ||
271 | int result; | ||
272 | sqlite3_stmt *stmt; | ||
273 | |||
274 | if (plugin->put_record != NULL) | ||
275 | sqlite3_finalize (plugin->put_record); | ||
276 | |||
277 | result = sqlite3_close (plugin->dbh); | ||
278 | if (result == SQLITE_BUSY) | ||
279 | { | ||
280 | LOG (GNUNET_ERROR_TYPE_WARNING, | ||
281 | _("Tried to close sqlite without finalizing all prepared statements.\n")); | ||
282 | stmt = sqlite3_next_stmt (plugin->dbh, NULL); | ||
283 | while (stmt != NULL) | ||
284 | { | ||
285 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", | ||
286 | "Closing statement %p\n", stmt); | ||
287 | result = sqlite3_finalize (stmt); | ||
288 | if (result != SQLITE_OK) | ||
289 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite", | ||
290 | "Failed to close statement %p: %d\n", stmt, result); | ||
291 | stmt = sqlite3_next_stmt (plugin->dbh, NULL); | ||
292 | } | ||
293 | result = sqlite3_close (plugin->dbh); | ||
294 | } | ||
295 | if (SQLITE_OK != result) | ||
296 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close"); | ||
297 | |||
298 | GNUNET_free_non_null (plugin->fn); | ||
299 | } | ||
300 | |||
301 | |||
302 | /** | ||
303 | * Store a record in the datastore. | ||
304 | * | ||
305 | * @param cls closure (internal context for the plugin) | ||
306 | * @param zone hash of the public key of the zone | ||
307 | * @param record_hash hash of the record | ||
308 | * @param record_key XOR of zone and hash of name | ||
309 | * @param name name that is being mapped (at most 255 characters long) | ||
310 | * @param record_type type of the record (A, AAAA, PKEY, etc.) | ||
311 | * @param expiration expiration time for the content | ||
312 | * @param flags flags for the content | ||
313 | * @param data_size number of bytes in data | ||
314 | * @param data value, semantics depend on 'record_type' (see RFCs for DNS and | ||
315 | * GNS specification for GNS extensions) | ||
316 | * @return GNUNET_OK on success | ||
317 | */ | ||
318 | static int | ||
319 | namestore_sqlite_put_record (void *cls, | ||
320 | const GNUNET_HashCode *zone, | ||
321 | const GNUNET_HashCode *record_hash, | ||
322 | const GNUNET_HashCode *record_key, | ||
323 | const char *name, | ||
324 | uint32_t record_type, | ||
325 | struct GNUNET_TIME_Absolute expiration, | ||
326 | enum GNUNET_NAMESTORE_RecordFlags flags, | ||
327 | size_t data_size, | ||
328 | const void *data) | ||
329 | { | ||
330 | #if 0 | ||
331 | struct Plugin *plugin = cls; | ||
332 | int n; | ||
333 | |||
334 | if ((SQLITE_OK != sqlite3_bind_int (plugin->updPrio, 1, delta)) || | ||
335 | (SQLITE_OK != sqlite3_bind_int64 (plugin->updPrio, 2, expire.abs_value)) | ||
336 | || (SQLITE_OK != sqlite3_bind_int64 (plugin->updPrio, 3, uid))) | ||
337 | { | ||
338 | LOG_SQLITE (plugin, msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
339 | "sqlite3_bind_XXXX"); | ||
340 | if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) | ||
341 | LOG_SQLITE (plugin, NULL, | ||
342 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
343 | "sqlite3_reset"); | ||
344 | return GNUNET_SYSERR; | ||
345 | |||
346 | } | ||
347 | n = sqlite3_step (plugin->updPrio); | ||
348 | if (SQLITE_OK != sqlite3_reset (plugin->updPrio)) | ||
349 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
350 | "sqlite3_reset"); | ||
351 | switch (n) | ||
352 | { | ||
353 | case SQLITE_DONE: | ||
354 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Block updated\n"); | ||
355 | return GNUNET_OK; | ||
356 | case SQLITE_BUSY: | ||
357 | LOG_SQLITE (plugin, msg, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, | ||
358 | "sqlite3_step"); | ||
359 | return GNUNET_NO; | ||
360 | default: | ||
361 | LOG_SQLITE (plugin, msg, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
362 | "sqlite3_step"); | ||
363 | return GNUNET_SYSERR; | ||
364 | } | ||
365 | #endif | ||
366 | return GNUNET_SYSERR; | ||
367 | } | ||
368 | |||
369 | |||
370 | /** | ||
371 | * Store a Merkle tree node in the datastore. | ||
372 | * | ||
373 | * @param cls closure (internal context for the plugin) | ||
374 | * @param zone hash of public key of the zone | ||
375 | * @param loc location in the B-tree | ||
376 | * @param ploc parent's location in the B-tree (must have depth = loc.depth - 1), NULL for root | ||
377 | * @param num_entries number of entries at this node in the B-tree | ||
378 | * @param entries the 'num_entries' entries to store (hashes over the | ||
379 | * records) | ||
380 | * @return GNUNET_OK on success | ||
381 | */ | ||
382 | static int | ||
383 | namestore_sqlite_put_node (void *cls, | ||
384 | const GNUNET_HashCode *zone, | ||
385 | const struct GNUNET_NAMESTORE_SignatureLocation *loc, | ||
386 | const struct GNUNET_NAMESTORE_SignatureLocation *ploc, | ||
387 | unsigned int num_entries, | ||
388 | const GNUNET_HashCode *entries) | ||
389 | { | ||
390 | return GNUNET_SYSERR; | ||
391 | } | ||
392 | |||
393 | |||
394 | /** | ||
395 | * Store a zone signature in the datastore. If a signature for the zone with a | ||
396 | * lower depth exists, the old signature is removed. If a signature for an | ||
397 | * older revision of the zone exists, this will delete all records, nodes | ||
398 | * and signatures for the older revision of the zone. | ||
399 | * | ||
400 | * @param cls closure (internal context for the plugin) | ||
401 | * @param zone_key public key of the zone | ||
402 | * @param loc location in the B-tree (top of the tree, offset 0, depth at 'maximum') | ||
403 | * @param top_sig signature at the top, NULL if 'loc.depth > 0' | ||
404 | * @param root_hash top level hash that is signed | ||
405 | * @return GNUNET_OK on success | ||
406 | */ | ||
407 | static int | ||
408 | namestore_sqlite_put_signature (void *cls, | ||
409 | const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key, | ||
410 | const struct GNUNET_NAMESTORE_SignatureLocation *loc, | ||
411 | const struct GNUNET_CRYPTO_RsaSignature *top_sig, | ||
412 | const GNUNET_HashCode *root_hash) | ||
413 | { | ||
414 | return GNUNET_SYSERR; | ||
415 | } | ||
416 | |||
417 | |||
418 | /** | ||
419 | * Iterate over the results for a particular key and zone in the | ||
420 | * datastore. Will only query the latest revision known for the | ||
421 | * zone (as adding a new zone revision will cause the plugin to | ||
422 | * delete all records from previous revisions). | ||
423 | * | ||
424 | * @param cls closure (internal context for the plugin) | ||
425 | * @param zone hash of public key of the zone | ||
426 | * @param record_key key for the record (XOR of zone and hash of name); | ||
427 | * NULL to iterate over all records of the zone | ||
428 | * @param iter maybe NULL (to just count) | ||
429 | * @param iter_cls closure for iter | ||
430 | * @return the number of results found | ||
431 | */ | ||
432 | static unsigned int | ||
433 | namestore_sqlite_iterate_records (void *cls, | ||
434 | const GNUNET_HashCode *zone, | ||
435 | const GNUNET_HashCode *record_key, | ||
436 | GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls) | ||
437 | { | ||
438 | #if 0 | ||
439 | int n; | ||
440 | struct GNUNET_TIME_Absolute expiration; | ||
441 | unsigned long long rowid; | ||
442 | unsigned int size; | ||
443 | int ret; | ||
444 | |||
445 | n = sqlite3_step (stmt); | ||
446 | switch (n) | ||
447 | { | ||
448 | case SQLITE_ROW: | ||
449 | size = sqlite3_column_bytes (stmt, 5); | ||
450 | rowid = sqlite3_column_int64 (stmt, 6); | ||
451 | if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode)) | ||
452 | { | ||
453 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite", | ||
454 | _ | ||
455 | ("Invalid data in database. Trying to fix (by deletion).\n")); | ||
456 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
457 | LOG_SQLITE (plugin, NULL, | ||
458 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
459 | "sqlite3_reset"); | ||
460 | if (GNUNET_OK == delete_by_rowid (plugin, rowid)) | ||
461 | plugin->env->duc (plugin->env->cls, | ||
462 | -(size + GNUNET_NAMESTORE_ENTRY_OVERHEAD)); | ||
463 | break; | ||
464 | } | ||
465 | expiration.abs_value = sqlite3_column_int64 (stmt, 3); | ||
466 | #if DEBUG_SQLITE | ||
467 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", | ||
468 | "Found reply in database with expiration %llu\n", | ||
469 | (unsigned long long) expiration.abs_value); | ||
470 | #endif | ||
471 | ret = proc (proc_cls, sqlite3_column_blob (stmt, 4) /* key */ , | ||
472 | size, sqlite3_column_blob (stmt, 5) /* data */ , | ||
473 | sqlite3_column_int (stmt, 0) /* type */ , | ||
474 | sqlite3_column_int (stmt, 1) /* priority */ , | ||
475 | sqlite3_column_int (stmt, 2) /* anonymity */ , | ||
476 | expiration, rowid); | ||
477 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
478 | LOG_SQLITE (plugin, NULL, | ||
479 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
480 | "sqlite3_reset"); | ||
481 | if ((GNUNET_NO == ret) && (GNUNET_OK == delete_by_rowid (plugin, rowid))) | ||
482 | plugin->env->duc (plugin->env->cls, | ||
483 | -(size + GNUNET_NAMESTORE_ENTRY_OVERHEAD)); | ||
484 | return; | ||
485 | case SQLITE_DONE: | ||
486 | /* database must be empty */ | ||
487 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
488 | LOG_SQLITE (plugin, NULL, | ||
489 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
490 | "sqlite3_reset"); | ||
491 | break; | ||
492 | case SQLITE_BUSY: | ||
493 | case SQLITE_ERROR: | ||
494 | case SQLITE_MISUSE: | ||
495 | default: | ||
496 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
497 | "sqlite3_step"); | ||
498 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
499 | LOG_SQLITE (plugin, NULL, | ||
500 | GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
501 | "sqlite3_reset"); | ||
502 | GNUNET_break (0); | ||
503 | database_shutdown (plugin); | ||
504 | database_setup (plugin->env->cfg, plugin); | ||
505 | break; | ||
506 | } | ||
507 | if (SQLITE_OK != sqlite3_reset (stmt)) | ||
508 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
509 | "sqlite3_reset"); | ||
510 | proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0); | ||
511 | |||
512 | |||
513 | |||
514 | |||
515 | |||
516 | const GNUNET_HashCode *key; | ||
517 | sqlite3_stmt *stmt; | ||
518 | int ret; | ||
519 | |||
520 | GNUNET_assert (proc != NULL); | ||
521 | if (sq_prepare (plugin->dbh, "SELECT hash FROM gn090", &stmt) != SQLITE_OK) | ||
522 | { | ||
523 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
524 | "sqlite_prepare"); | ||
525 | return; | ||
526 | } | ||
527 | while (SQLITE_ROW == (ret = sqlite3_step (stmt))) | ||
528 | { | ||
529 | key = sqlite3_column_blob (stmt, 1); | ||
530 | if (sizeof (GNUNET_HashCode) == sqlite3_column_bytes (stmt, 1)) | ||
531 | proc (proc_cls, key, 1); | ||
532 | } | ||
533 | if (SQLITE_DONE != ret) | ||
534 | LOG_SQLITE (plugin, NULL, GNUNET_ERROR_TYPE_ERROR, "sqlite_step"); | ||
535 | sqlite3_finalize (stmt); | ||
536 | |||
537 | #endif | ||
538 | return 0; | ||
539 | } | ||
540 | |||
541 | |||
542 | /** | ||
543 | * Get a particular node from the signature tree. | ||
544 | * | ||
545 | * @param cls closure (internal context for the plugin) | ||
546 | * @param zone hash of public key of the zone | ||
547 | * @param loc location of the node in the signature tree | ||
548 | * @param cb function to call with the result | ||
549 | * @param cb_cls closure for cont | ||
550 | */ | ||
551 | static void | ||
552 | namestore_sqlite_get_node (void *cls, | ||
553 | const GNUNET_HashCode *zone, | ||
554 | const struct GNUNET_NAMESTORE_SignatureLocation *loc, | ||
555 | GNUNET_NAMESTORE_NodeCallback cb, void *cb_cls) | ||
556 | { | ||
557 | } | ||
558 | |||
559 | |||
560 | /** | ||
561 | * Get the current signature for a zone. | ||
562 | * | ||
563 | * @param cls closure (internal context for the plugin) | ||
564 | * @param zone hash of public key of the zone | ||
565 | * @param cb function to call with the result | ||
566 | * @param cb_cls closure for cont | ||
567 | */ | ||
568 | static void | ||
569 | namestore_sqlite_get_signature (void *cls, | ||
570 | const GNUNET_HashCode *zone, | ||
571 | GNUNET_NAMESTORE_SignatureCallback cb, void *cb_cls) | ||
572 | { | ||
573 | } | ||
574 | |||
575 | |||
576 | /** | ||
577 | * Delete an entire zone (all revisions, all records, all nodes, | ||
578 | * all signatures). Not used in normal operation. | ||
579 | * | ||
580 | * @param cls closure (internal context for the plugin) | ||
581 | * @param zone zone to delete | ||
582 | */ | ||
583 | static void | ||
584 | namestore_sqlite_delete_zone (void *cls, | ||
585 | const GNUNET_HashCode *zone) | ||
586 | { | ||
587 | } | ||
588 | |||
589 | |||
590 | /** | ||
591 | * Entry point for the plugin. | ||
592 | * | ||
593 | * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*" | ||
594 | * @return NULL on error, othrewise the plugin context | ||
595 | */ | ||
596 | void * | ||
597 | libgnunet_plugin_namestore_sqlite_init (void *cls) | ||
598 | { | ||
599 | static struct Plugin plugin; | ||
600 | const struct GNUNET_CONFIGURATION_Handle *cfg = cls; | ||
601 | struct GNUNET_NAMESTORE_PluginFunctions *api; | ||
602 | |||
603 | if (NULL != plugin.cfg) | ||
604 | return NULL; /* can only initialize once! */ | ||
605 | memset (&plugin, 0, sizeof (struct Plugin)); | ||
606 | plugin.cfg = cfg; | ||
607 | if (GNUNET_OK != database_setup (&plugin)) | ||
608 | { | ||
609 | database_shutdown (&plugin); | ||
610 | return NULL; | ||
611 | } | ||
612 | api = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_PluginFunctions)); | ||
613 | api->cls = &plugin; | ||
614 | api->put_record = &namestore_sqlite_put_record; | ||
615 | api->put_node = &namestore_sqlite_put_node; | ||
616 | api->put_signature = &namestore_sqlite_put_signature; | ||
617 | api->iterate_records = &namestore_sqlite_iterate_records; | ||
618 | api->get_node = &namestore_sqlite_get_node; | ||
619 | api->get_signature = &namestore_sqlite_get_signature; | ||
620 | api->delete_zone = &namestore_sqlite_delete_zone; | ||
621 | LOG (GNUNET_ERROR_TYPE_INFO, | ||
622 | _("Sqlite database running\n")); | ||
623 | return api; | ||
624 | } | ||
625 | |||
626 | |||
627 | /** | ||
628 | * Exit point from the plugin. | ||
629 | * | ||
630 | * @param cls the plugin context (as returned by "init") | ||
631 | * @return always NULL | ||
632 | */ | ||
633 | void * | ||
634 | libgnunet_plugin_namestore_sqlite_done (void *cls) | ||
635 | { | ||
636 | struct GNUNET_NAMESTORE_PluginFunctions *api = cls; | ||
637 | struct Plugin *plugin = api->cls; | ||
638 | |||
639 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
640 | "sqlite plugin is done\n"); | ||
641 | database_shutdown (plugin); | ||
642 | plugin->cfg = NULL; | ||
643 | GNUNET_free (api); | ||
644 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
645 | "sqlite plugin is finished\n"); | ||
646 | return NULL; | ||
647 | } | ||
648 | |||
649 | /* end of plugin_namestore_sqlite.c */ | ||