2 * This file is part of GNUnet
3 * (C) 2009-2013 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? A
35 * low value makes sure that we are more responsive to requests
36 * (especially PUTs). A high value guarantees a higher success rate
37 * (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
41 * succeed 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 caching a block
77 sqlite3_stmt *cache_block;
80 * Precompiled SQL for looking up a block
82 sqlite3_stmt *lookup_block;
85 * Precompiled SQL for removing expired blocks
87 sqlite3_stmt *expire_blocks;
90 * Precompiled SQL to store records.
92 sqlite3_stmt *store_records;
95 * Precompiled SQL to deltete existing records.
97 sqlite3_stmt *delete_records;
100 * Precompiled SQL for iterate records within a zone.
102 sqlite3_stmt *iterate_zone;
105 * Precompiled SQL to for reverse lookup based on PKEY.
107 sqlite3_stmt *zone_to_name;
113 * @brief Prepare a SQL statement
115 * @param dbh handle to the database
116 * @param zSql SQL statement, UTF-8 encoded
117 * @param ppStmt set to the prepared statement
118 * @return 0 on success
121 sq_prepare (sqlite3 * dbh, const char *zSql, sqlite3_stmt ** ppStmt)
127 sqlite3_prepare_v2 (dbh, zSql, strlen (zSql), ppStmt,
128 (const char **) &dummy);
129 LOG (GNUNET_ERROR_TYPE_DEBUG,
130 "Prepared `%s' / %p: %d\n", zSql, *ppStmt, result);
136 * Create our database indices.
138 * @param dbh handle to the database
141 create_indices (sqlite3 * dbh)
145 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_query_hash ON ns096blocks (query,expiration_time)",
146 NULL, NULL, NULL)) ||
148 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_block_expiration ON ns096blocks (expiration_time)",
149 NULL, NULL, NULL)) ||
151 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_pkey_reverse ON ns096records (zone_private_key,pkey_hash)",
152 NULL, NULL, NULL)) ||
154 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_pkey_iter ON ns096records (zone_private_key,rvalue)",
156 LOG (GNUNET_ERROR_TYPE_ERROR,
157 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
162 #define CHECK(a) GNUNET_break(a)
166 #define ENULL_DEFINED 1
167 #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
172 * Initialize the database connections and associated
173 * data structures (create tables and indices
174 * as needed as well).
176 * @param plugin the plugin context (state for this module)
177 * @return GNUNET_OK on success
180 database_setup (struct Plugin *plugin)
189 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "namestore-sqlite",
190 "FILENAME", &afsdir))
192 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
193 "namestore-sqlite", "FILENAME");
194 return GNUNET_SYSERR;
196 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
198 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
201 GNUNET_free (afsdir);
202 return GNUNET_SYSERR;
205 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
208 /* Open database and precompile statements */
209 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
211 LOG (GNUNET_ERROR_TYPE_ERROR,
212 _("Unable to initialize SQLite: %s.\n"),
213 sqlite3_errmsg (plugin->dbh));
214 return GNUNET_SYSERR;
217 sqlite3_exec (plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL,
220 sqlite3_exec (plugin->dbh, "PRAGMA synchronous=NORMAL", NULL, NULL,
223 sqlite3_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF", NULL, NULL,
226 sqlite3_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL", NULL,
229 sqlite3_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\"", NULL,
232 sqlite3_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
235 sqlite3_exec (plugin->dbh, "PRAGMA count_changes=OFF", NULL, NULL,
238 sqlite3_exec (plugin->dbh, "PRAGMA page_size=4092", NULL, NULL,
241 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
246 sq_prepare (plugin->dbh,
247 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns096blocks'",
249 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
252 "CREATE TABLE ns096blocks ("
253 " query BLOB NOT NULL DEFAULT '',"
254 " block BLOB NOT NULL DEFAULT '',"
255 " expiration_time INT8 NOT NULL DEFAULT 0"
257 NULL, NULL, NULL) != SQLITE_OK))
259 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
260 sqlite3_finalize (stmt);
261 return GNUNET_SYSERR;
263 sqlite3_finalize (stmt);
266 sq_prepare (plugin->dbh,
267 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns096records'",
269 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
272 "CREATE TABLE ns096records ("
273 " zone_private_key BLOB NOT NULL DEFAULT '',"
275 " rvalue INT8 NOT NULL DEFAULT '',"
276 " record_count INT NOT NULL DEFAULT 0,"
277 " record_data BLOB NOT NULL DEFAULT '',"
278 " label TEXT NOT NULL DEFAULT ''"
280 NULL, NULL, NULL) != SQLITE_OK))
282 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
283 sqlite3_finalize (stmt);
284 return GNUNET_SYSERR;
286 sqlite3_finalize (stmt);
288 create_indices (plugin->dbh);
292 "INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)",
293 &plugin->cache_block) != SQLITE_OK) ||
296 "DELETE FROM ns096blocks WHERE expiration_time<?",
297 &plugin->expire_blocks) != SQLITE_OK) ||
300 "SELECT block FROM ns096blocks WHERE query=? ORDER BY expiration_time DESC LIMIT 1",
301 &plugin->lookup_block) != SQLITE_OK) ||
304 "INSERT INTO ns096records (zone_private_key, pkey_hash, rvalue, record_count, record_data, label)"
305 " VALUES (?, ?, ?, ?, ?, ?)",
306 &plugin->store_records) != SQLITE_OK) ||
309 "DELETE FROM ns096records WHERE zone_private_key=? AND label=?",
310 &plugin->delete_records) != SQLITE_OK) ||
313 "SELECT record_count,record_data,label"
314 " FROM ns096records WHERE zone_private_key=? AND pkey_hash=?",
315 &plugin->zone_to_name) != SQLITE_OK) ||
318 "SELECT record_count,record_data,label"
319 " FROM ns096records WHERE zone_private_key=? ORDER BY rvalue LIMIT 1 OFFSET ?",
320 &plugin->iterate_zone) != SQLITE_OK) )
322 LOG_SQLITE (plugin,GNUNET_ERROR_TYPE_ERROR, "precompiling");
323 return GNUNET_SYSERR;
330 * Shutdown database connection and associate data
332 * @param plugin the plugin context (state for this module)
335 database_shutdown (struct Plugin *plugin)
340 if (NULL != plugin->cache_block)
341 sqlite3_finalize (plugin->cache_block);
342 if (NULL != plugin->expire_blocks)
343 sqlite3_finalize (plugin->expire_blocks);
344 if (NULL != plugin->lookup_block)
345 sqlite3_finalize (plugin->lookup_block);
346 if (NULL != plugin->store_records)
347 sqlite3_finalize (plugin->store_records);
348 if (NULL != plugin->delete_records)
349 sqlite3_finalize (plugin->delete_records);
350 if (NULL != plugin->iterate_zone)
351 sqlite3_finalize (plugin->iterate_zone);
352 if (NULL != plugin->zone_to_name)
353 sqlite3_finalize (plugin->zone_to_name);
354 result = sqlite3_close (plugin->dbh);
355 if (result == SQLITE_BUSY)
357 LOG (GNUNET_ERROR_TYPE_WARNING,
358 _("Tried to close sqlite without finalizing all prepared statements.\n"));
359 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
362 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
363 "Closing statement %p\n", stmt);
364 result = sqlite3_finalize (stmt);
365 if (result != SQLITE_OK)
366 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite",
367 "Failed to close statement %p: %d\n", stmt, result);
368 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
370 result = sqlite3_close (plugin->dbh);
372 if (SQLITE_OK != result)
373 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
375 GNUNET_free_non_null (plugin->fn);
380 * Removes any expired block.
382 * @param plugin the plugin
385 namestore_sqlite_expire_blocks (struct Plugin *plugin)
387 struct GNUNET_TIME_Absolute now;
390 now = GNUNET_TIME_absolute_get ();
391 if (SQLITE_OK != sqlite3_bind_int64 (plugin->expire_blocks,
392 1, now.abs_value_us))
394 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
395 "sqlite3_bind_XXXX");
396 if (SQLITE_OK != sqlite3_reset (plugin->expire_blocks))
398 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
402 n = sqlite3_step (plugin->expire_blocks);
403 if (SQLITE_OK != sqlite3_reset (plugin->expire_blocks))
404 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
409 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Records expired\n");
412 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
416 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
424 * Cache a block in the datastore.
426 * @param cls closure (internal context for the plugin)
427 * @param block block to cache
428 * @return GNUNET_OK on success, else GNUNET_SYSERR
431 namestore_sqlite_cache_block (void *cls,
432 const struct GNUNET_NAMESTORE_Block *block)
434 struct Plugin *plugin = cls;
435 struct GNUNET_HashCode query;
436 struct GNUNET_TIME_Absolute expiration;
440 namestore_sqlite_expire_blocks (plugin);
441 GNUNET_CRYPTO_hash (&block->derived_key,
442 sizeof (struct GNUNET_CRYPTO_EccPublicKey),
444 expiration = GNUNET_TIME_absolute_ntoh (block->expiration_time);
445 block_size = ntohl (block->purpose.size) +
446 sizeof (struct GNUNET_CRYPTO_EccPublicKey) +
447 sizeof (struct GNUNET_CRYPTO_EccSignature);
448 if (block_size > 64 * 65536)
451 return GNUNET_SYSERR;
453 if ((SQLITE_OK != sqlite3_bind_blob (plugin->cache_block, 1, &query, sizeof (struct GNUNET_HashCode), SQLITE_STATIC)) ||
454 (SQLITE_OK != sqlite3_bind_blob (plugin->cache_block, 2, block, block_size, SQLITE_STATIC)) ||
455 (SQLITE_OK != sqlite3_bind_int64 (plugin->cache_block, 3, expiration.abs_value_us)))
458 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
459 "sqlite3_bind_XXXX");
460 if (SQLITE_OK != sqlite3_reset (plugin->cache_block))
462 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
464 return GNUNET_SYSERR;
467 n = sqlite3_step (plugin->cache_block);
468 if (SQLITE_OK != sqlite3_reset (plugin->cache_block))
469 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
474 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record stored\n");
477 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
481 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
483 return GNUNET_SYSERR;
489 * Get the block for a particular zone and label in the
490 * datastore. Will return at most one result to the iterator.
492 * @param cls closure (internal context for the plugin)
493 * @param query hash of public key derived from the zone and the label
494 * @param iter function to call with the result
495 * @param iter_cls closure for iter
496 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
499 namestore_sqlite_lookup_block (void *cls,
500 const struct GNUNET_HashCode *query,
501 GNUNET_NAMESTORE_BlockCallback iter, void *iter_cls)
503 struct Plugin *plugin = cls;
507 const struct GNUNET_NAMESTORE_Block *block;
509 if (SQLITE_OK != sqlite3_bind_blob (plugin->lookup_block, 1,
510 query, sizeof (struct GNUNET_HashCode),
513 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
514 "sqlite3_bind_XXXX");
515 if (SQLITE_OK != sqlite3_reset (plugin->lookup_block))
517 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
519 return GNUNET_SYSERR;
523 if (SQLITE_ROW == (sret = sqlite3_step (plugin->lookup_block)))
525 block = sqlite3_column_blob (plugin->lookup_block, 0);
526 block_size = sqlite3_column_bytes (plugin->lookup_block, 0);
527 if ( (block_size < sizeof (struct GNUNET_NAMESTORE_Block)) ||
528 (ntohl (block->purpose.size) +
529 sizeof (struct GNUNET_CRYPTO_EccPublicKey) +
530 sizeof (struct GNUNET_CRYPTO_EccSignature) != block_size) )
537 iter (iter_cls, block);
543 if (SQLITE_DONE != sret)
545 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
549 if (SQLITE_OK != sqlite3_reset (plugin->lookup_block))
551 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
558 * Store a record in the datastore. Removes any existing record in the
559 * same zone with the same name.
561 * @param cls closure (internal context for the plugin)
562 * @param zone_key private key of the zone
563 * @param label name that is being mapped (at most 255 characters long)
564 * @param rd_count number of entries in 'rd' array
565 * @param rd array of records with data to store
566 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
569 namestore_sqlite_store_records (void *cls,
570 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
572 unsigned int rd_count,
573 const struct GNUNET_NAMESTORE_RecordData *rd)
575 struct Plugin *plugin = cls;
577 struct GNUNET_HashCode pkey_hash;
582 memset (&pkey_hash, 0, sizeof (pkey_hash));
583 for (i=0;i<rd_count;i++)
584 if (GNUNET_NAMESTORE_TYPE_PKEY == rd[i].record_type)
586 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EccPublicKey) == rd[i].data_size);
587 GNUNET_CRYPTO_hash (rd[i].data,
592 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
593 data_size = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
594 if (data_size > 64 * 65536)
597 return GNUNET_SYSERR;
600 char data[data_size];
602 if (data_size != GNUNET_NAMESTORE_records_serialize (rd_count, rd,
606 return GNUNET_SYSERR;
609 /* First delete 'old' records */
610 if ((SQLITE_OK != sqlite3_bind_blob (plugin->delete_records, 1,
611 zone_key, sizeof (struct GNUNET_CRYPTO_EccPrivateKey), SQLITE_STATIC)) ||
612 (SQLITE_OK != sqlite3_bind_text (plugin->delete_records, 2, label, -1, SQLITE_STATIC)))
615 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
616 "sqlite3_bind_XXXX");
617 if (SQLITE_OK != sqlite3_reset (plugin->delete_records))
619 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
621 return GNUNET_SYSERR;
624 n = sqlite3_step (plugin->delete_records);
625 if (SQLITE_OK != sqlite3_reset (plugin->delete_records))
626 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
631 if ((SQLITE_OK != sqlite3_bind_blob (plugin->store_records, 1,
632 zone_key, sizeof (struct GNUNET_CRYPTO_EccPrivateKey), SQLITE_STATIC)) ||
633 (SQLITE_OK != sqlite3_bind_blob (plugin->store_records, 2,
634 &pkey_hash, sizeof (struct GNUNET_HashCode), SQLITE_STATIC)) ||
635 (SQLITE_OK != sqlite3_bind_int64 (plugin->store_records, 3, rvalue)) ||
636 (SQLITE_OK != sqlite3_bind_int (plugin->store_records, 4, rd_count)) ||
637 (SQLITE_OK != sqlite3_bind_blob (plugin->store_records, 5, data, data_size, SQLITE_STATIC)) ||
638 (SQLITE_OK != sqlite3_bind_text (plugin->store_records, 6, label, -1, SQLITE_STATIC)))
641 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
642 "sqlite3_bind_XXXX");
643 if (SQLITE_OK != sqlite3_reset (plugin->store_records))
645 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
647 return GNUNET_SYSERR;
649 n = sqlite3_step (plugin->store_records);
650 if (SQLITE_OK != sqlite3_reset (plugin->store_records))
651 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
658 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record stored\n");
661 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
665 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
667 return GNUNET_SYSERR;
673 * The given 'sqlite' statement has been prepared to be run.
674 * It will return a record which should be given to the iterator.
675 * Runs the statement and parses the returned record.
677 * @param plugin plugin context
678 * @param stmt to run (and then clean up)
679 * @param zone_key private key of the zone
680 * @param iter iterator to call with the result
681 * @param iter_cls closure for @a iter
682 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
685 get_record_and_call_iterator (struct Plugin *plugin,
687 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
688 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
690 unsigned int record_count;
698 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
700 record_count = sqlite3_column_int (stmt, 0);
701 data_size = sqlite3_column_bytes (stmt, 1);
702 data = sqlite3_column_blob (stmt, 1);
703 label = (const char*) sqlite3_column_text (stmt, 2);
705 if (record_count > 64 * 1024)
707 /* sanity check, don't stack allocate far too much just
708 because database might contain a large value here */
714 struct GNUNET_NAMESTORE_RecordData rd[record_count];
717 GNUNET_NAMESTORE_records_deserialize (data_size, data,
725 iter (iter_cls, zone_key, label,
733 if (SQLITE_DONE != sret)
734 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
736 if (SQLITE_OK != sqlite3_reset (stmt))
738 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
745 * Iterate over the results for a particular key and zone in the
746 * datastore. Will return at most one result to the iterator.
748 * @param cls closure (internal context for the plugin)
749 * @param zone hash of public key of the zone, NULL to iterate over all zones
750 * @param offset offset in the list of all matching records
751 * @param iter function to call with the result
752 * @param iter_cls closure for @a iter
753 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
756 namestore_sqlite_iterate_records (void *cls,
757 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
759 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
761 struct Plugin *plugin = cls;
764 stmt = plugin->iterate_zone;
766 if ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1,
767 zone, sizeof (struct GNUNET_CRYPTO_EccPrivateKey),
769 (SQLITE_OK != sqlite3_bind_int64 (stmt, 2,
772 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
773 "sqlite3_bind_XXXX");
774 if (SQLITE_OK != sqlite3_reset (stmt))
776 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
778 return GNUNET_SYSERR;
780 return get_record_and_call_iterator (plugin, stmt, zone, iter, iter_cls);
785 * Look for an existing PKEY delegation record for a given public key.
786 * Returns at most one result to the iterator.
788 * @param cls closure (internal context for the plugin)
789 * @param zone private key of the zone to look up in, never NULL
790 * @param value_zone public key of the target zone (value), never NULL
791 * @param iter function to call with the result
792 * @param iter_cls closure for @a iter
793 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
796 namestore_sqlite_zone_to_name (void *cls,
797 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
798 const struct GNUNET_CRYPTO_EccPublicKey *value_zone,
799 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
801 struct Plugin *plugin = cls;
804 stmt = plugin->zone_to_name;
805 if ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1,
806 zone, sizeof (struct GNUNET_CRYPTO_EccPrivateKey),
808 (SQLITE_OK != sqlite3_bind_blob (stmt, 2,
809 value_zone, sizeof (struct GNUNET_CRYPTO_EccPublicKey),
812 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
813 "sqlite3_bind_XXXX");
814 if (SQLITE_OK != sqlite3_reset (stmt))
816 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
818 return GNUNET_SYSERR;
820 return get_record_and_call_iterator (plugin, stmt, zone, iter, iter_cls);
825 * Entry point for the plugin.
827 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
828 * @return NULL on error, otherwise the plugin context
831 libgnunet_plugin_namestore_sqlite_init (void *cls)
833 static struct Plugin plugin;
834 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
835 struct GNUNET_NAMESTORE_PluginFunctions *api;
837 if (NULL != plugin.cfg)
838 return NULL; /* can only initialize once! */
839 memset (&plugin, 0, sizeof (struct Plugin));
841 if (GNUNET_OK != database_setup (&plugin))
843 database_shutdown (&plugin);
846 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
848 api->cache_block = &namestore_sqlite_cache_block;
849 api->lookup_block = &namestore_sqlite_lookup_block;
850 api->store_records = &namestore_sqlite_store_records;
851 api->iterate_records = &namestore_sqlite_iterate_records;
852 api->zone_to_name = &namestore_sqlite_zone_to_name;
853 LOG (GNUNET_ERROR_TYPE_INFO,
854 _("Sqlite database running\n"));
860 * Exit point from the plugin.
862 * @param cls the plugin context (as returned by "init")
863 * @return always NULL
866 libgnunet_plugin_namestore_sqlite_done (void *cls)
868 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
869 struct Plugin *plugin = api->cls;
871 database_shutdown (plugin);
874 LOG (GNUNET_ERROR_TYPE_DEBUG,
875 "sqlite plugin is finished\n");
879 /* end of plugin_namestore_sqlite.c */