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 for iterate all records within all zones.
107 sqlite3_stmt *iterate_all_zones;
110 * Precompiled SQL to for reverse lookup based on PKEY.
112 sqlite3_stmt *zone_to_name;
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_query_hash ON ns096blocks (query,expiration_time)",
151 NULL, NULL, NULL)) ||
153 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_block_expiration ON ns096blocks (expiration_time)",
154 NULL, NULL, NULL)) ||
156 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_pkey_reverse ON ns096records (zone_private_key,pkey_hash)",
157 NULL, NULL, NULL)) ||
159 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_pkey_iter ON ns096records (zone_private_key,rvalue)",
160 NULL, NULL, NULL)) ||
162 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS it_iter ON ns096records (rvalue)",
164 LOG (GNUNET_ERROR_TYPE_ERROR,
165 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
170 #define CHECK(a) GNUNET_break(a)
174 #define ENULL_DEFINED 1
175 #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
180 * Initialize the database connections and associated
181 * data structures (create tables and indices
182 * as needed as well).
184 * @param plugin the plugin context (state for this module)
185 * @return #GNUNET_OK on success
188 database_setup (struct Plugin *plugin)
197 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "namestore-sqlite",
198 "FILENAME", &afsdir))
200 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
201 "namestore-sqlite", "FILENAME");
202 return GNUNET_SYSERR;
204 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
206 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
209 GNUNET_free (afsdir);
210 return GNUNET_SYSERR;
213 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
216 /* Open database and precompile statements */
217 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
219 LOG (GNUNET_ERROR_TYPE_ERROR,
220 _("Unable to initialize SQLite: %s.\n"),
221 sqlite3_errmsg (plugin->dbh));
222 return GNUNET_SYSERR;
225 sqlite3_exec (plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL,
228 sqlite3_exec (plugin->dbh, "PRAGMA synchronous=NORMAL", NULL, NULL,
231 sqlite3_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF", NULL, NULL,
234 sqlite3_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL", NULL,
237 sqlite3_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\"", NULL,
240 sqlite3_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
243 sqlite3_exec (plugin->dbh, "PRAGMA count_changes=OFF", NULL, NULL,
246 sqlite3_exec (plugin->dbh, "PRAGMA page_size=4092", NULL, NULL,
249 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
254 sq_prepare (plugin->dbh,
255 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns096blocks'",
257 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
260 "CREATE TABLE ns096blocks ("
261 " query BLOB NOT NULL DEFAULT '',"
262 " block BLOB NOT NULL DEFAULT '',"
263 " expiration_time INT8 NOT NULL DEFAULT 0"
265 NULL, NULL, NULL) != SQLITE_OK))
267 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
268 sqlite3_finalize (stmt);
269 return GNUNET_SYSERR;
271 sqlite3_finalize (stmt);
274 sq_prepare (plugin->dbh,
275 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns096records'",
277 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
280 "CREATE TABLE ns096records ("
281 " zone_private_key BLOB NOT NULL DEFAULT '',"
283 " rvalue INT8 NOT NULL DEFAULT '',"
284 " record_count INT NOT NULL DEFAULT 0,"
285 " record_data BLOB NOT NULL DEFAULT '',"
286 " label TEXT NOT NULL DEFAULT ''"
288 NULL, NULL, NULL) != SQLITE_OK))
290 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
291 sqlite3_finalize (stmt);
292 return GNUNET_SYSERR;
294 sqlite3_finalize (stmt);
296 create_indices (plugin->dbh);
300 "INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)",
301 &plugin->cache_block) != SQLITE_OK) ||
304 "DELETE FROM ns096blocks WHERE expiration_time<?",
305 &plugin->expire_blocks) != SQLITE_OK) ||
308 "SELECT block FROM ns096blocks WHERE query=? ORDER BY expiration_time DESC LIMIT 1",
309 &plugin->lookup_block) != SQLITE_OK) ||
312 "INSERT INTO ns096records (zone_private_key, pkey_hash, rvalue, record_count, record_data, label)"
313 " VALUES (?, ?, ?, ?, ?, ?)",
314 &plugin->store_records) != SQLITE_OK) ||
317 "DELETE FROM ns096records WHERE zone_private_key=? AND label=?",
318 &plugin->delete_records) != SQLITE_OK) ||
321 "SELECT record_count,record_data,label"
322 " FROM ns096records WHERE zone_private_key=? AND pkey_hash=?",
323 &plugin->zone_to_name) != SQLITE_OK) ||
326 "SELECT record_count,record_data,label"
327 " FROM ns096records WHERE zone_private_key=? ORDER BY rvalue LIMIT 1 OFFSET ?",
328 &plugin->iterate_zone) != SQLITE_OK) ||
331 "SELECT record_count,record_data,label,zone_private_key"
332 " FROM ns096records ORDER BY rvalue LIMIT 1 OFFSET ?",
333 &plugin->iterate_all_zones) != SQLITE_OK)
336 LOG_SQLITE (plugin,GNUNET_ERROR_TYPE_ERROR, "precompiling");
337 return GNUNET_SYSERR;
344 * Shutdown database connection and associate data
346 * @param plugin the plugin context (state for this module)
349 database_shutdown (struct Plugin *plugin)
354 if (NULL != plugin->cache_block)
355 sqlite3_finalize (plugin->cache_block);
356 if (NULL != plugin->lookup_block)
357 sqlite3_finalize (plugin->lookup_block);
358 if (NULL != plugin->expire_blocks)
359 sqlite3_finalize (plugin->expire_blocks);
360 if (NULL != plugin->store_records)
361 sqlite3_finalize (plugin->store_records);
362 if (NULL != plugin->delete_records)
363 sqlite3_finalize (plugin->delete_records);
364 if (NULL != plugin->iterate_zone)
365 sqlite3_finalize (plugin->iterate_zone);
366 if (NULL != plugin->iterate_all_zones)
367 sqlite3_finalize (plugin->iterate_all_zones);
368 if (NULL != plugin->zone_to_name)
369 sqlite3_finalize (plugin->zone_to_name);
370 result = sqlite3_close (plugin->dbh);
371 if (result == SQLITE_BUSY)
373 LOG (GNUNET_ERROR_TYPE_WARNING,
374 _("Tried to close sqlite without finalizing all prepared statements.\n"));
375 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
378 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
379 "Closing statement %p\n", stmt);
380 result = sqlite3_finalize (stmt);
381 if (result != SQLITE_OK)
382 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite",
383 "Failed to close statement %p: %d\n", stmt, result);
384 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
386 result = sqlite3_close (plugin->dbh);
388 if (SQLITE_OK != result)
389 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
391 GNUNET_free_non_null (plugin->fn);
396 * Removes any expired block.
398 * @param plugin the plugin
401 namestore_sqlite_expire_blocks (struct Plugin *plugin)
403 struct GNUNET_TIME_Absolute now;
406 now = GNUNET_TIME_absolute_get ();
407 if (SQLITE_OK != sqlite3_bind_int64 (plugin->expire_blocks,
408 1, now.abs_value_us))
410 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
411 "sqlite3_bind_XXXX");
412 if (SQLITE_OK != sqlite3_reset (plugin->expire_blocks))
414 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
418 n = sqlite3_step (plugin->expire_blocks);
419 if (SQLITE_OK != sqlite3_reset (plugin->expire_blocks))
420 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
425 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Records expired\n");
428 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
432 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
440 * Cache a block in the datastore.
442 * @param cls closure (internal context for the plugin)
443 * @param block block to cache
444 * @return GNUNET_OK on success, else GNUNET_SYSERR
447 namestore_sqlite_cache_block (void *cls,
448 const struct GNUNET_NAMESTORE_Block *block)
450 struct Plugin *plugin = cls;
451 struct GNUNET_HashCode query;
452 struct GNUNET_TIME_Absolute expiration;
456 namestore_sqlite_expire_blocks (plugin);
457 GNUNET_CRYPTO_hash (&block->derived_key,
458 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
460 expiration = GNUNET_TIME_absolute_ntoh (block->expiration_time);
461 block_size = ntohl (block->purpose.size) +
462 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) +
463 sizeof (struct GNUNET_CRYPTO_EccSignature);
464 if (block_size > 64 * 65536)
467 return GNUNET_SYSERR;
469 if ((SQLITE_OK != sqlite3_bind_blob (plugin->cache_block, 1, &query, sizeof (struct GNUNET_HashCode), SQLITE_STATIC)) ||
470 (SQLITE_OK != sqlite3_bind_blob (plugin->cache_block, 2, block, block_size, SQLITE_STATIC)) ||
471 (SQLITE_OK != sqlite3_bind_int64 (plugin->cache_block, 3, expiration.abs_value_us)))
474 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
475 "sqlite3_bind_XXXX");
476 if (SQLITE_OK != sqlite3_reset (plugin->cache_block))
478 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
480 return GNUNET_SYSERR;
483 n = sqlite3_step (plugin->cache_block);
484 if (SQLITE_OK != sqlite3_reset (plugin->cache_block))
485 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
490 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record stored\n");
493 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
497 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
499 return GNUNET_SYSERR;
505 * Get the block for a particular zone and label in the
506 * datastore. Will return at most one result to the iterator.
508 * @param cls closure (internal context for the plugin)
509 * @param query hash of public key derived from the zone and the label
510 * @param iter function to call with the result
511 * @param iter_cls closure for iter
512 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
515 namestore_sqlite_lookup_block (void *cls,
516 const struct GNUNET_HashCode *query,
517 GNUNET_NAMESTORE_BlockCallback iter, void *iter_cls)
519 struct Plugin *plugin = cls;
523 const struct GNUNET_NAMESTORE_Block *block;
525 if (SQLITE_OK != sqlite3_bind_blob (plugin->lookup_block, 1,
526 query, sizeof (struct GNUNET_HashCode),
529 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
530 "sqlite3_bind_XXXX");
531 if (SQLITE_OK != sqlite3_reset (plugin->lookup_block))
533 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
535 return GNUNET_SYSERR;
539 if (SQLITE_ROW == (sret = sqlite3_step (plugin->lookup_block)))
541 block = sqlite3_column_blob (plugin->lookup_block, 0);
542 block_size = sqlite3_column_bytes (plugin->lookup_block, 0);
543 if ( (block_size < sizeof (struct GNUNET_NAMESTORE_Block)) ||
544 (ntohl (block->purpose.size) +
545 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) +
546 sizeof (struct GNUNET_CRYPTO_EccSignature) != block_size) )
553 iter (iter_cls, block);
559 if (SQLITE_DONE != sret)
561 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
565 if (SQLITE_OK != sqlite3_reset (plugin->lookup_block))
567 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
574 * Store a record in the datastore. Removes any existing record in the
575 * same zone with the same name.
577 * @param cls closure (internal context for the plugin)
578 * @param zone_key private key of the zone
579 * @param label name that is being mapped (at most 255 characters long)
580 * @param rd_count number of entries in 'rd' array
581 * @param rd array of records with data to store
582 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
585 namestore_sqlite_store_records (void *cls,
586 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
588 unsigned int rd_count,
589 const struct GNUNET_NAMESTORE_RecordData *rd)
591 struct Plugin *plugin = cls;
593 struct GNUNET_HashCode pkey_hash;
598 memset (&pkey_hash, 0, sizeof (pkey_hash));
599 for (i=0;i<rd_count;i++)
600 if (GNUNET_NAMESTORE_TYPE_PKEY == rd[i].record_type)
602 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) == rd[i].data_size);
603 GNUNET_CRYPTO_hash (rd[i].data,
608 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
609 data_size = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
610 if (data_size > 64 * 65536)
613 return GNUNET_SYSERR;
616 char data[data_size];
618 if (data_size != GNUNET_NAMESTORE_records_serialize (rd_count, rd,
622 return GNUNET_SYSERR;
625 /* First delete 'old' records */
626 if ((SQLITE_OK != sqlite3_bind_blob (plugin->delete_records, 1,
627 zone_key, sizeof (struct GNUNET_CRYPTO_EccPrivateKey), SQLITE_STATIC)) ||
628 (SQLITE_OK != sqlite3_bind_text (plugin->delete_records, 2, label, -1, SQLITE_STATIC)))
631 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
632 "sqlite3_bind_XXXX");
633 if (SQLITE_OK != sqlite3_reset (plugin->delete_records))
635 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
637 return GNUNET_SYSERR;
640 n = sqlite3_step (plugin->delete_records);
641 if (SQLITE_OK != sqlite3_reset (plugin->delete_records))
642 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
647 if ((SQLITE_OK != sqlite3_bind_blob (plugin->store_records, 1,
648 zone_key, sizeof (struct GNUNET_CRYPTO_EccPrivateKey), SQLITE_STATIC)) ||
649 (SQLITE_OK != sqlite3_bind_blob (plugin->store_records, 2,
650 &pkey_hash, sizeof (struct GNUNET_HashCode), SQLITE_STATIC)) ||
651 (SQLITE_OK != sqlite3_bind_int64 (plugin->store_records, 3, rvalue)) ||
652 (SQLITE_OK != sqlite3_bind_int (plugin->store_records, 4, rd_count)) ||
653 (SQLITE_OK != sqlite3_bind_blob (plugin->store_records, 5, data, data_size, SQLITE_STATIC)) ||
654 (SQLITE_OK != sqlite3_bind_text (plugin->store_records, 6, label, -1, SQLITE_STATIC)))
657 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
658 "sqlite3_bind_XXXX");
659 if (SQLITE_OK != sqlite3_reset (plugin->store_records))
661 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
663 return GNUNET_SYSERR;
665 n = sqlite3_step (plugin->store_records);
666 if (SQLITE_OK != sqlite3_reset (plugin->store_records))
667 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
674 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record stored\n");
677 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
681 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
683 return GNUNET_SYSERR;
689 * The given 'sqlite' statement has been prepared to be run.
690 * It will return a record which should be given to the iterator.
691 * Runs the statement and parses the returned record.
693 * @param plugin plugin context
694 * @param stmt to run (and then clean up)
695 * @param zone_key private key of the zone
696 * @param iter iterator to call with the result
697 * @param iter_cls closure for @a iter
698 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
701 get_record_and_call_iterator (struct Plugin *plugin,
703 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
704 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
706 unsigned int record_count;
714 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
716 record_count = sqlite3_column_int (stmt, 0);
717 data_size = sqlite3_column_bytes (stmt, 1);
718 data = sqlite3_column_blob (stmt, 1);
719 label = (const char*) sqlite3_column_text (stmt, 2);
720 if (NULL == zone_key)
722 /* must be "iterate_all_zones", got one extra return value */
723 if (sizeof (struct GNUNET_CRYPTO_EccPrivateKey) !=
724 sqlite3_column_bytes (stmt, 3))
731 zone_key = sqlite3_column_blob (stmt, 3);
734 if (record_count > 64 * 1024)
736 /* sanity check, don't stack allocate far too much just
737 because database might contain a large value here */
743 struct GNUNET_NAMESTORE_RecordData rd[record_count];
746 GNUNET_NAMESTORE_records_deserialize (data_size, data,
752 else if (NULL != zone_key)
755 iter (iter_cls, zone_key, label, record_count, rd);
762 if (SQLITE_DONE != sret)
763 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
765 if (SQLITE_OK != sqlite3_reset (stmt))
767 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
774 * Iterate over the results for a particular key and zone in the
775 * datastore. Will return at most one result to the iterator.
777 * @param cls closure (internal context for the plugin)
778 * @param zone hash of public key of the zone, NULL to iterate over all zones
779 * @param offset offset in the list of all matching records
780 * @param iter function to call with the result
781 * @param iter_cls closure for @a iter
782 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
785 namestore_sqlite_iterate_records (void *cls,
786 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
788 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
790 struct Plugin *plugin = cls;
796 stmt = plugin->iterate_all_zones;
797 err = (SQLITE_OK != sqlite3_bind_int64 (stmt, 1,
802 stmt = plugin->iterate_zone;
803 err = ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1,
804 zone, sizeof (struct GNUNET_CRYPTO_EccPrivateKey),
806 (SQLITE_OK != sqlite3_bind_int64 (stmt, 2,
811 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
812 "sqlite3_bind_XXXX");
813 if (SQLITE_OK != sqlite3_reset (stmt))
815 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
817 return GNUNET_SYSERR;
819 return get_record_and_call_iterator (plugin, stmt, zone, iter, iter_cls);
824 * Look for an existing PKEY delegation record for a given public key.
825 * Returns at most one result to the iterator.
827 * @param cls closure (internal context for the plugin)
828 * @param zone private key of the zone to look up in, never NULL
829 * @param value_zone public key of the target zone (value), never NULL
830 * @param iter function to call with the result
831 * @param iter_cls closure for @a iter
832 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
835 namestore_sqlite_zone_to_name (void *cls,
836 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
837 const struct GNUNET_CRYPTO_EccPublicSignKey *value_zone,
838 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
840 struct Plugin *plugin = cls;
843 stmt = plugin->zone_to_name;
844 if ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1,
845 zone, sizeof (struct GNUNET_CRYPTO_EccPrivateKey),
847 (SQLITE_OK != sqlite3_bind_blob (stmt, 2,
848 value_zone, sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
851 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
852 "sqlite3_bind_XXXX");
853 if (SQLITE_OK != sqlite3_reset (stmt))
855 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
857 return GNUNET_SYSERR;
859 return get_record_and_call_iterator (plugin, stmt, zone, iter, iter_cls);
864 * Entry point for the plugin.
866 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
867 * @return NULL on error, otherwise the plugin context
870 libgnunet_plugin_namestore_sqlite_init (void *cls)
872 static struct Plugin plugin;
873 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
874 struct GNUNET_NAMESTORE_PluginFunctions *api;
876 if (NULL != plugin.cfg)
877 return NULL; /* can only initialize once! */
878 memset (&plugin, 0, sizeof (struct Plugin));
880 if (GNUNET_OK != database_setup (&plugin))
882 database_shutdown (&plugin);
885 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
887 api->cache_block = &namestore_sqlite_cache_block;
888 api->lookup_block = &namestore_sqlite_lookup_block;
889 api->store_records = &namestore_sqlite_store_records;
890 api->iterate_records = &namestore_sqlite_iterate_records;
891 api->zone_to_name = &namestore_sqlite_zone_to_name;
892 LOG (GNUNET_ERROR_TYPE_INFO,
893 _("Sqlite database running\n"));
899 * Exit point from the plugin.
901 * @param cls the plugin context (as returned by "init")
902 * @return always NULL
905 libgnunet_plugin_namestore_sqlite_done (void *cls)
907 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
908 struct Plugin *plugin = api->cls;
910 database_shutdown (plugin);
913 LOG (GNUNET_ERROR_TYPE_DEBUG,
914 "sqlite plugin is finished\n");
918 /* end of plugin_namestore_sqlite.c */