2 * This file is part of GNUnet
3 * (C) 2009, 2011, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file namestore/plugin_namestore_sqlite.c
23 * @brief sqlite-based namestore backend
24 * @author Christian Grothoff
28 #include "gnunet_namestore_plugin.h"
29 #include "gnunet_namestore_service.h"
30 #include "namestore.h"
34 * After how many ms "busy" should a DB operation fail for good?
35 * A low value makes sure that we are more responsive to requests
36 * (especially PUTs). A high value guarantees a higher success
37 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
39 * The default value of 1s should ensure that users do not experience
40 * huge latencies while at the same time allowing operations to succeed
41 * with reasonable probability.
43 #define BUSY_TIMEOUT_MS 1000
47 * Log an error message at log-level 'level' that indicates
48 * a failure of the command 'cmd' on file 'filename'
49 * with the message given by strerror(errno).
51 #define LOG_SQLITE(db, 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)); } while(0)
53 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__)
57 * Context for all functions in this plugin.
62 const struct GNUNET_CONFIGURATION_Handle *cfg;
70 * Native SQLite database handle.
75 * Precompiled SQL for put record
77 sqlite3_stmt *put_records;
80 * Precompiled SQL for remove record
82 sqlite3_stmt *remove_records;
85 * Precompiled SQL for iterate over all records.
87 sqlite3_stmt *iterate_all;
90 * Precompiled SQL for iterate records with same name.
92 sqlite3_stmt *iterate_by_name;
95 * Precompiled SQL for iterate records with same zone.
97 sqlite3_stmt *iterate_by_zone;
100 * Precompiled SQL for iterate records with same name and zone.
102 sqlite3_stmt *iterate_records;
105 * Precompiled SQL to get the name for a given zone-value.
107 sqlite3_stmt *zone_to_name;
110 * Precompiled SQL for delete zone
112 sqlite3_stmt *delete_zone;
118 * @brief Prepare a SQL statement
120 * @param dbh handle to the database
121 * @param zSql SQL statement, UTF-8 encoded
122 * @param ppStmt set to the prepared statement
123 * @return 0 on success
126 sq_prepare (sqlite3 * dbh, const char *zSql, sqlite3_stmt ** ppStmt)
132 sqlite3_prepare_v2 (dbh, zSql, strlen (zSql), ppStmt,
133 (const char **) &dummy);
134 LOG (GNUNET_ERROR_TYPE_DEBUG,
135 "Prepared `%s' / %p: %d\n", zSql, *ppStmt, result);
141 * Create our database indices.
143 * @param dbh handle to the database
146 create_indices (sqlite3 * dbh)
150 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_zone_name_rv ON ns091records (zone_hash,record_name_hash,rvalue)",
151 NULL, NULL, NULL)) ||
153 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_zone_delegation ON ns091records (zone_hash,zone_delegation)",
154 NULL, NULL, NULL)) ||
156 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_zone_rv ON ns091records (zone_hash,rvalue)",
157 NULL, NULL, NULL)) ||
159 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_zone ON ns091records (zone_hash)",
160 NULL, NULL, NULL)) ||
162 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_name_rv ON ns091records (record_name_hash,rvalue)",
163 NULL, NULL, NULL)) ||
165 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_rv ON ns091records (rvalue)",
167 LOG (GNUNET_ERROR_TYPE_ERROR,
168 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
173 #define CHECK(a) GNUNET_break(a)
177 #define ENULL_DEFINED 1
178 #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
183 * Initialize the database connections and associated
184 * data structures (create tables and indices
185 * as needed as well).
187 * @param plugin the plugin context (state for this module)
188 * @return GNUNET_OK on success
191 database_setup (struct Plugin *plugin)
200 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "namestore-sqlite",
201 "FILENAME", &afsdir))
203 LOG (GNUNET_ERROR_TYPE_ERROR,
204 _ ("Option `%s' in section `%s' missing in configuration!\n"),
205 "FILENAME", "namestore-sqlite");
206 return GNUNET_SYSERR;
208 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
210 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
213 GNUNET_free (afsdir);
214 return GNUNET_SYSERR;
219 GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), nl_langinfo (CODESET));
221 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), "UTF-8"); /* good luck */
223 GNUNET_free (afsdir);
225 /* Open database and precompile statements */
226 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
228 LOG (GNUNET_ERROR_TYPE_ERROR,
229 _("Unable to initialize SQLite: %s.\n"),
230 sqlite3_errmsg (plugin->dbh));
231 return GNUNET_SYSERR;
234 sqlite3_exec (plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL,
237 sqlite3_exec (plugin->dbh, "PRAGMA synchronous=NORMAL", NULL, NULL,
240 sqlite3_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF", NULL, NULL,
243 sqlite3_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL", NULL,
246 sqlite3_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\"", NULL,
249 sqlite3_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
252 sqlite3_exec (plugin->dbh, "PRAGMA count_changes=OFF", NULL, NULL,
255 sqlite3_exec (plugin->dbh, "PRAGMA page_size=4092", NULL, NULL,
258 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
263 sq_prepare (plugin->dbh,
264 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns091records'",
266 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
269 "CREATE TABLE ns091records ("
270 " zone_key BLOB NOT NULL DEFAULT '',"
271 " zone_delegation BLOB NOT NULL DEFAULT '',"
272 " zone_hash BLOB NOT NULL DEFAULT '',"
273 " record_count INT NOT NULL DEFAULT 0,"
274 " record_data BLOB NOT NULL DEFAULT '',"
275 " block_expiration_time INT8 NOT NULL DEFAULT 0,"
276 " signature BLOB NOT NULL DEFAULT '',"
277 " record_name TEXT NOT NULL DEFAULT '',"
278 " record_name_hash BLOB NOT NULL DEFAULT '',"
279 " rvalue INT8 NOT NULL DEFAULT ''"
281 NULL, NULL, NULL) != SQLITE_OK))
283 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
284 sqlite3_finalize (stmt);
285 return GNUNET_SYSERR;
287 sqlite3_finalize (stmt);
289 create_indices (plugin->dbh);
291 #define ALL "zone_key, record_name, record_count, record_data, block_expiration_time, signature"
294 "INSERT INTO ns091records (" ALL ", zone_delegation, zone_hash, record_name_hash, rvalue) VALUES "
295 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
296 &plugin->put_records) != SQLITE_OK) ||
299 "DELETE FROM ns091records WHERE zone_hash=? AND record_name_hash=?",
300 &plugin->remove_records) != SQLITE_OK) ||
304 " FROM ns091records WHERE zone_hash=? AND record_name_hash=? ORDER BY rvalue LIMIT 1 OFFSET ?",
305 &plugin->iterate_records) != SQLITE_OK) ||
309 " FROM ns091records WHERE zone_hash=? ORDER BY rvalue LIMIT 1 OFFSET ?",
310 &plugin->iterate_by_zone) != SQLITE_OK) ||
314 " FROM ns091records WHERE record_name_hash=? ORDER BY rvalue LIMIT 1 OFFSET ?",
315 &plugin->iterate_by_name) != SQLITE_OK) ||
319 " FROM ns091records ORDER BY rvalue LIMIT 1 OFFSET ?",
320 &plugin->iterate_all) != SQLITE_OK) ||
324 " FROM ns091records WHERE zone_hash=? AND zone_delegation=?",
325 &plugin->zone_to_name) != SQLITE_OK) ||
328 "DELETE FROM ns091records WHERE zone_hash=?",
329 &plugin->delete_zone) != SQLITE_OK) )
331 LOG_SQLITE (plugin,GNUNET_ERROR_TYPE_ERROR, "precompiling");
332 return GNUNET_SYSERR;
340 * Shutdown database connection and associate data
342 * @param plugin the plugin context (state for this module)
345 database_shutdown (struct Plugin *plugin)
350 if (NULL != plugin->put_records)
351 sqlite3_finalize (plugin->put_records);
352 if (NULL != plugin->remove_records)
353 sqlite3_finalize (plugin->remove_records);
354 if (NULL != plugin->iterate_records)
355 sqlite3_finalize (plugin->iterate_records);
356 if (NULL != plugin->iterate_by_zone)
357 sqlite3_finalize (plugin->iterate_by_zone);
358 if (NULL != plugin->iterate_by_name)
359 sqlite3_finalize (plugin->iterate_by_name);
360 if (NULL != plugin->iterate_all)
361 sqlite3_finalize (plugin->iterate_all);
362 if (NULL != plugin->zone_to_name)
363 sqlite3_finalize (plugin->zone_to_name);
364 if (NULL != plugin->delete_zone)
365 sqlite3_finalize (plugin->delete_zone);
366 result = sqlite3_close (plugin->dbh);
367 if (result == SQLITE_BUSY)
369 LOG (GNUNET_ERROR_TYPE_WARNING,
370 _("Tried to close sqlite without finalizing all prepared statements.\n"));
371 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
374 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
375 "Closing statement %p\n", stmt);
376 result = sqlite3_finalize (stmt);
377 if (result != SQLITE_OK)
378 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite",
379 "Failed to close statement %p: %d\n", stmt, result);
380 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
382 result = sqlite3_close (plugin->dbh);
384 if (SQLITE_OK != result)
385 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
387 GNUNET_free_non_null (plugin->fn);
392 * Removes any existing record in the given zone with the same name.
394 * @param cls closure (internal context for the plugin)
395 * @param zone hash of the public key of the zone
396 * @param name name to remove (at most 255 characters long)
397 * @return GNUNET_OK on success
400 namestore_sqlite_remove_records (void *cls,
401 const struct GNUNET_CRYPTO_ShortHashCode *zone,
404 struct Plugin *plugin = cls;
405 struct GNUNET_CRYPTO_ShortHashCode nh;
408 name_len = strlen (name);
409 GNUNET_CRYPTO_short_hash (name, name_len, &nh);
411 if ((SQLITE_OK != sqlite3_bind_blob (plugin->remove_records, 1, zone, sizeof (struct GNUNET_CRYPTO_ShortHashCode), SQLITE_STATIC)) ||
412 (SQLITE_OK != sqlite3_bind_blob (plugin->remove_records, 2, &nh, sizeof (struct GNUNET_CRYPTO_ShortHashCode), SQLITE_STATIC)))
414 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
415 "sqlite3_bind_XXXX");
416 if (SQLITE_OK != sqlite3_reset (plugin->remove_records))
418 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
420 return GNUNET_SYSERR;
422 n = sqlite3_step (plugin->remove_records);
423 if (SQLITE_OK != sqlite3_reset (plugin->remove_records))
424 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
429 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record removed\n");
432 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
436 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
438 return GNUNET_SYSERR;
444 * Store a record in the datastore. Removes any existing record in the
445 * same zone with the same name.
447 * @param cls closure (internal context for the plugin)
448 * @param zone_key public key of the zone
449 * @param expire when does the corresponding block in the DHT expire (until
450 * when should we never do a DHT lookup for the same name again)?
451 * @param name name that is being mapped (at most 255 characters long)
452 * @param rd_count number of entries in 'rd' array
453 * @param rd array of records with data to store
454 * @param signature signature of the record block, NULL if signature is unavailable (i.e.
455 * because the user queried for a particular record type only)
456 * @return GNUNET_OK on success, else GNUNET_SYSERR
459 namestore_sqlite_put_records (void *cls,
460 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
461 struct GNUNET_TIME_Absolute expire,
463 unsigned int rd_count,
464 const struct GNUNET_NAMESTORE_RecordData *rd,
465 const struct GNUNET_CRYPTO_RsaSignature *signature)
467 struct Plugin *plugin = cls;
469 struct GNUNET_CRYPTO_ShortHashCode zone;
470 struct GNUNET_CRYPTO_ShortHashCode zone_delegation;
471 struct GNUNET_CRYPTO_ShortHashCode nh;
477 GNUNET_CRYPTO_short_hash (zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &zone);
478 (void) namestore_sqlite_remove_records (plugin, &zone, name);
479 name_len = strlen (name);
480 GNUNET_CRYPTO_short_hash (name, name_len, &nh);
481 memset (&zone_delegation, 0, sizeof (zone_delegation));
482 for (i=0;i<rd_count;i++)
483 if (rd[i].record_type == GNUNET_NAMESTORE_TYPE_PKEY)
485 GNUNET_assert (sizeof (struct GNUNET_CRYPTO_ShortHashCode) == rd[i].data_size);
486 memcpy (&zone_delegation,
488 sizeof (struct GNUNET_CRYPTO_ShortHashCode));
491 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
492 data_size = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
493 if (data_size > 64 * 65536)
496 return GNUNET_SYSERR;
499 char data[data_size];
501 if (data_size != GNUNET_NAMESTORE_records_serialize (rd_count, rd,
505 return GNUNET_SYSERR;
507 if ((SQLITE_OK != sqlite3_bind_blob (plugin->put_records, 1, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), SQLITE_STATIC)) ||
508 (SQLITE_OK != sqlite3_bind_text (plugin->put_records, 2, name, -1, SQLITE_STATIC)) ||
509 (SQLITE_OK != sqlite3_bind_int (plugin->put_records, 3, rd_count)) ||
510 (SQLITE_OK != sqlite3_bind_blob (plugin->put_records, 4, data, data_size, SQLITE_STATIC)) ||
511 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_records, 5, expire.abs_value)) ||
512 (SQLITE_OK != sqlite3_bind_blob (plugin->put_records, 6, signature, sizeof (struct GNUNET_CRYPTO_RsaSignature), SQLITE_STATIC)) ||
513 (SQLITE_OK != sqlite3_bind_blob (plugin->put_records, 7, &zone_delegation, sizeof (struct GNUNET_CRYPTO_ShortHashCode), SQLITE_STATIC)) ||
514 (SQLITE_OK != sqlite3_bind_blob (plugin->put_records, 8, &zone, sizeof (struct GNUNET_CRYPTO_ShortHashCode), SQLITE_STATIC)) ||
515 (SQLITE_OK != sqlite3_bind_blob (plugin->put_records, 9, &nh, sizeof (struct GNUNET_CRYPTO_ShortHashCode), SQLITE_STATIC)) ||
516 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_records, 10, rvalue)) )
519 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
520 "sqlite3_bind_XXXX");
521 if (SQLITE_OK != sqlite3_reset (plugin->put_records))
523 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
525 return GNUNET_SYSERR;
528 n = sqlite3_step (plugin->put_records);
529 if (SQLITE_OK != sqlite3_reset (plugin->put_records))
530 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
536 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record stored\n");
539 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
543 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
545 return GNUNET_SYSERR;
551 * The given 'sqlite' statement has been prepared to be run.
552 * It will return a record which should be given to the iterator.
553 * Runs the statement and parses the returned record.
555 * @param plugin plugin context
556 * @param stmt to run (and then clean up)
557 * @param iter iterator to call with the result
558 * @param iter_cls closure for 'iter'
559 * @return GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error
562 get_record_and_call_iterator (struct Plugin *plugin,
564 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
568 unsigned int record_count;
570 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key;
571 const struct GNUNET_CRYPTO_RsaSignature *sig;
572 struct GNUNET_TIME_Absolute expiration;
577 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
580 zone_key = sqlite3_column_blob (stmt, 0);
581 name = (const char*) sqlite3_column_text (stmt, 1);
582 record_count = sqlite3_column_int (stmt, 2);
583 data_size = sqlite3_column_bytes (stmt, 3);
584 data = sqlite3_column_blob (stmt, 3);
585 expiration.abs_value = (uint64_t) sqlite3_column_int64 (stmt, 4);
586 sig = sqlite3_column_blob (stmt, 5);
588 if ( (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) != sqlite3_column_bytes (stmt, 0)) ||
589 (sizeof (struct GNUNET_CRYPTO_RsaSignature) != sqlite3_column_bytes (stmt, 5)) )
596 struct GNUNET_NAMESTORE_RecordData rd[record_count];
599 GNUNET_NAMESTORE_records_deserialize (data_size, data,
608 iter (iter_cls, zone_key, expiration, name,
609 record_count, rd, sig);
615 if (SQLITE_DONE != sret)
616 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
617 iter (iter_cls, NULL, GNUNET_TIME_UNIT_ZERO_ABS, NULL, 0, NULL, NULL);
619 if (SQLITE_OK != sqlite3_reset (stmt))
621 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
628 * Iterate over the results for a particular key and zone in the
629 * datastore. Will return at most one result to the iterator.
631 * @param cls closure (internal context for the plugin)
632 * @param zone hash of public key of the zone, NULL to iterate over all zones
633 * @param name name as string, NULL to iterate over all records of the zone
634 * @param offset offset in the list of all matching records
635 * @param iter function to call with the result
636 * @param iter_cls closure for iter
637 * @return GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error
640 namestore_sqlite_iterate_records (void *cls,
641 const struct GNUNET_CRYPTO_ShortHashCode *zone,
644 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
646 struct Plugin *plugin = cls;
648 struct GNUNET_CRYPTO_ShortHashCode name_hase;
653 stmt = plugin->iterate_all;
656 GNUNET_CRYPTO_short_hash (name, strlen(name), &name_hase);
657 stmt = plugin->iterate_by_name;
661 stmt = plugin->iterate_by_zone;
664 GNUNET_CRYPTO_short_hash (name, strlen(name), &name_hase);
665 stmt = plugin->iterate_records;
669 if ( (NULL != zone) &&
670 (SQLITE_OK != sqlite3_bind_blob (stmt, ++boff,
671 zone, sizeof (struct GNUNET_CRYPTO_ShortHashCode),
674 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
675 "sqlite3_bind_XXXX");
676 if (SQLITE_OK != sqlite3_reset (stmt))
678 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
680 return GNUNET_SYSERR;
682 if ( (NULL != name) &&
683 (SQLITE_OK != sqlite3_bind_blob (stmt, ++boff,
684 &name_hase, sizeof (struct GNUNET_CRYPTO_ShortHashCode),
687 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ITERATE NAME HASH: `%8s'", GNUNET_short_h2s(&name_hase));
688 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
689 "sqlite3_bind_XXXX");
690 if (SQLITE_OK != sqlite3_reset (stmt))
692 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
694 return GNUNET_SYSERR;
697 if (SQLITE_OK != sqlite3_bind_int64 (stmt, ++boff,
700 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
701 "sqlite3_bind_XXXX");
702 if (SQLITE_OK != sqlite3_reset (stmt))
704 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
706 return GNUNET_SYSERR;
709 return get_record_and_call_iterator (plugin, stmt, iter, iter_cls);
714 * Look for an existing PKEY delegation record for a given public key.
715 * Returns at most one result to the iterator.
717 * @param cls closure (internal context for the plugin)
718 * @param zone hash of public key of the zone to look up in, never NULL
719 * @param value_zone hash of the public key of the target zone (value), never NULL
720 * @param iter function to call with the result
721 * @param iter_cls closure for iter
722 * @return GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error
725 namestore_sqlite_zone_to_name (void *cls,
726 const struct GNUNET_CRYPTO_ShortHashCode *zone,
727 const struct GNUNET_CRYPTO_ShortHashCode *value_zone,
728 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
730 struct Plugin *plugin = cls;
733 stmt = plugin->zone_to_name;
734 if ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1,
735 zone, sizeof (struct GNUNET_CRYPTO_ShortHashCode),
737 (SQLITE_OK != sqlite3_bind_blob (stmt, 2,
738 value_zone, sizeof (struct GNUNET_CRYPTO_ShortHashCode),
741 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
742 "sqlite3_bind_XXXX");
743 if (SQLITE_OK != sqlite3_reset (stmt))
745 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
747 return GNUNET_SYSERR;
750 return get_record_and_call_iterator (plugin, stmt, iter, iter_cls);
755 * Delete an entire zone (all records). Not used in normal operation.
757 * @param cls closure (internal context for the plugin)
758 * @param zone zone to delete
761 namestore_sqlite_delete_zone (void *cls,
762 const struct GNUNET_CRYPTO_ShortHashCode *zone)
764 struct Plugin *plugin = cls;
765 sqlite3_stmt *stmt = plugin->delete_zone;
768 if (SQLITE_OK != sqlite3_bind_blob (stmt, 1, zone, sizeof (struct GNUNET_CRYPTO_ShortHashCode), SQLITE_STATIC))
770 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
771 "sqlite3_bind_XXXX");
772 if (SQLITE_OK != sqlite3_reset (stmt))
774 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
778 n = sqlite3_step (stmt);
779 if (SQLITE_OK != sqlite3_reset (stmt))
780 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
785 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Values deleted\n");
788 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
792 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
800 * Entry point for the plugin.
802 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
803 * @return NULL on error, othrewise the plugin context
806 libgnunet_plugin_namestore_sqlite_init (void *cls)
808 static struct Plugin plugin;
809 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
810 struct GNUNET_NAMESTORE_PluginFunctions *api;
812 if (NULL != plugin.cfg)
813 return NULL; /* can only initialize once! */
814 memset (&plugin, 0, sizeof (struct Plugin));
816 if (GNUNET_OK != database_setup (&plugin))
818 database_shutdown (&plugin);
821 api = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_PluginFunctions));
823 api->put_records = &namestore_sqlite_put_records;
824 api->remove_records = &namestore_sqlite_remove_records;
825 api->iterate_records = &namestore_sqlite_iterate_records;
826 api->zone_to_name = &namestore_sqlite_zone_to_name;
827 api->delete_zone = &namestore_sqlite_delete_zone;
828 LOG (GNUNET_ERROR_TYPE_INFO,
829 _("Sqlite database running\n"));
835 * Exit point from the plugin.
837 * @param cls the plugin context (as returned by "init")
838 * @return always NULL
841 libgnunet_plugin_namestore_sqlite_done (void *cls)
843 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
844 struct Plugin *plugin = api->cls;
846 LOG (GNUNET_ERROR_TYPE_DEBUG,
847 "sqlite plugin is done\n");
848 database_shutdown (plugin);
851 LOG (GNUNET_ERROR_TYPE_DEBUG,
852 "sqlite plugin is finished\n");
856 /* end of plugin_namestore_sqlite.c */