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 deleting an older block
82 sqlite3_stmt *delete_block;
85 * Precompiled SQL for looking up a block
87 sqlite3_stmt *lookup_block;
90 * Precompiled SQL for removing expired blocks
92 sqlite3_stmt *expire_blocks;
95 * Precompiled SQL to store records.
97 sqlite3_stmt *store_records;
100 * Precompiled SQL to deltete existing records.
102 sqlite3_stmt *delete_records;
105 * Precompiled SQL for iterate records within a zone.
107 sqlite3_stmt *iterate_zone;
110 * Precompiled SQL for iterate all records within all zones.
112 sqlite3_stmt *iterate_all_zones;
115 * Precompiled SQL to for reverse lookup based on PKEY.
117 sqlite3_stmt *zone_to_name;
123 * @brief Prepare a SQL statement
125 * @param dbh handle to the database
126 * @param zSql SQL statement, UTF-8 encoded
127 * @param ppStmt set to the prepared statement
128 * @return 0 on success
131 sq_prepare (sqlite3 * dbh, const char *zSql, sqlite3_stmt ** ppStmt)
137 sqlite3_prepare_v2 (dbh, zSql, strlen (zSql), ppStmt,
138 (const char **) &dummy);
139 LOG (GNUNET_ERROR_TYPE_DEBUG,
140 "Prepared `%s' / %p: %d\n", zSql, *ppStmt, result);
146 * Create our database indices.
148 * @param dbh handle to the database
151 create_indices (sqlite3 * dbh)
155 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_query_hash ON ns096blocks (query,expiration_time)",
156 NULL, NULL, NULL)) ||
158 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_block_expiration ON ns096blocks (expiration_time)",
159 NULL, NULL, NULL)) ||
161 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_pkey_reverse ON ns096records (zone_private_key,pkey_hash)",
162 NULL, NULL, NULL)) ||
164 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_pkey_iter ON ns096records (zone_private_key,rvalue)",
165 NULL, NULL, NULL)) ||
167 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS it_iter ON ns096records (rvalue)",
169 LOG (GNUNET_ERROR_TYPE_ERROR,
170 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
175 #define CHECK(a) GNUNET_break(a)
179 #define ENULL_DEFINED 1
180 #define CHECK(a) if (! a) { GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "%s\n", e); sqlite3_free(e); }
185 * Initialize the database connections and associated
186 * data structures (create tables and indices
187 * as needed as well).
189 * @param plugin the plugin context (state for this module)
190 * @return #GNUNET_OK on success
193 database_setup (struct Plugin *plugin)
202 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, "namestore-sqlite",
203 "FILENAME", &afsdir))
205 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
206 "namestore-sqlite", "FILENAME");
207 return GNUNET_SYSERR;
209 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
211 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
214 GNUNET_free (afsdir);
215 return GNUNET_SYSERR;
218 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
221 /* Open database and precompile statements */
222 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
224 LOG (GNUNET_ERROR_TYPE_ERROR,
225 _("Unable to initialize SQLite: %s.\n"),
226 sqlite3_errmsg (plugin->dbh));
227 return GNUNET_SYSERR;
230 sqlite3_exec (plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL,
233 sqlite3_exec (plugin->dbh, "PRAGMA synchronous=NORMAL", NULL, NULL,
236 sqlite3_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF", NULL, NULL,
239 sqlite3_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL", NULL,
242 sqlite3_exec (plugin->dbh, "PRAGMA encoding=\"UTF-8\"", NULL,
245 sqlite3_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
248 sqlite3_exec (plugin->dbh, "PRAGMA count_changes=OFF", NULL, NULL,
251 sqlite3_exec (plugin->dbh, "PRAGMA page_size=4092", NULL, NULL,
254 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
259 sq_prepare (plugin->dbh,
260 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns096blocks'",
262 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
265 "CREATE TABLE ns096blocks ("
266 " query BLOB NOT NULL DEFAULT '',"
267 " block BLOB NOT NULL DEFAULT '',"
268 " expiration_time INT8 NOT NULL DEFAULT 0"
270 NULL, NULL, NULL) != SQLITE_OK))
272 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
273 sqlite3_finalize (stmt);
274 return GNUNET_SYSERR;
276 sqlite3_finalize (stmt);
279 sq_prepare (plugin->dbh,
280 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns096records'",
282 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
285 "CREATE TABLE ns096records ("
286 " zone_private_key BLOB NOT NULL DEFAULT '',"
288 " rvalue INT8 NOT NULL DEFAULT '',"
289 " record_count INT NOT NULL DEFAULT 0,"
290 " record_data BLOB NOT NULL DEFAULT '',"
291 " label TEXT NOT NULL DEFAULT ''"
293 NULL, NULL, NULL) != SQLITE_OK))
295 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
296 sqlite3_finalize (stmt);
297 return GNUNET_SYSERR;
299 sqlite3_finalize (stmt);
301 create_indices (plugin->dbh);
305 "INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)",
306 &plugin->cache_block) != SQLITE_OK) ||
309 "DELETE FROM ns096blocks WHERE expiration_time<?",
310 &plugin->expire_blocks) != SQLITE_OK) ||
313 "DELETE FROM ns096blocks WHERE query=? AND expiration_time<=?",
314 &plugin->delete_block) != SQLITE_OK) ||
317 "SELECT block FROM ns096blocks WHERE query=? ORDER BY expiration_time DESC LIMIT 1",
318 &plugin->lookup_block) != SQLITE_OK) ||
321 "INSERT INTO ns096records (zone_private_key, pkey_hash, rvalue, record_count, record_data, label)"
322 " VALUES (?, ?, ?, ?, ?, ?)",
323 &plugin->store_records) != SQLITE_OK) ||
326 "DELETE FROM ns096records WHERE zone_private_key=? AND label=?",
327 &plugin->delete_records) != SQLITE_OK) ||
330 "SELECT record_count,record_data,label"
331 " FROM ns096records WHERE zone_private_key=? AND pkey_hash=?",
332 &plugin->zone_to_name) != SQLITE_OK) ||
335 "SELECT record_count,record_data,label"
336 " FROM ns096records WHERE zone_private_key=? ORDER BY rvalue LIMIT 1 OFFSET ?",
337 &plugin->iterate_zone) != SQLITE_OK) ||
340 "SELECT record_count,record_data,label,zone_private_key"
341 " FROM ns096records ORDER BY rvalue LIMIT 1 OFFSET ?",
342 &plugin->iterate_all_zones) != SQLITE_OK)
345 LOG_SQLITE (plugin,GNUNET_ERROR_TYPE_ERROR, "precompiling");
346 return GNUNET_SYSERR;
353 * Shutdown database connection and associate data
355 * @param plugin the plugin context (state for this module)
358 database_shutdown (struct Plugin *plugin)
363 if (NULL != plugin->cache_block)
364 sqlite3_finalize (plugin->cache_block);
365 if (NULL != plugin->lookup_block)
366 sqlite3_finalize (plugin->lookup_block);
367 if (NULL != plugin->expire_blocks)
368 sqlite3_finalize (plugin->expire_blocks);
369 if (NULL != plugin->delete_block)
370 sqlite3_finalize (plugin->delete_block);
371 if (NULL != plugin->store_records)
372 sqlite3_finalize (plugin->store_records);
373 if (NULL != plugin->delete_records)
374 sqlite3_finalize (plugin->delete_records);
375 if (NULL != plugin->iterate_zone)
376 sqlite3_finalize (plugin->iterate_zone);
377 if (NULL != plugin->iterate_all_zones)
378 sqlite3_finalize (plugin->iterate_all_zones);
379 if (NULL != plugin->zone_to_name)
380 sqlite3_finalize (plugin->zone_to_name);
381 result = sqlite3_close (plugin->dbh);
382 if (result == SQLITE_BUSY)
384 LOG (GNUNET_ERROR_TYPE_WARNING,
385 _("Tried to close sqlite without finalizing all prepared statements.\n"));
386 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
389 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
390 "Closing statement %p\n", stmt);
391 result = sqlite3_finalize (stmt);
392 if (result != SQLITE_OK)
393 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite",
394 "Failed to close statement %p: %d\n", stmt, result);
395 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
397 result = sqlite3_close (plugin->dbh);
399 if (SQLITE_OK != result)
400 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
402 GNUNET_free_non_null (plugin->fn);
407 * Removes any expired block.
409 * @param plugin the plugin
412 namestore_sqlite_expire_blocks (struct Plugin *plugin)
414 struct GNUNET_TIME_Absolute now;
417 now = GNUNET_TIME_absolute_get ();
418 if (SQLITE_OK != sqlite3_bind_int64 (plugin->expire_blocks,
419 1, now.abs_value_us))
421 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
422 "sqlite3_bind_XXXX");
423 if (SQLITE_OK != sqlite3_reset (plugin->expire_blocks))
425 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
429 n = sqlite3_step (plugin->expire_blocks);
430 if (SQLITE_OK != sqlite3_reset (plugin->expire_blocks))
431 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
436 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Records expired\n");
439 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
443 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
451 * Cache a block in the datastore.
453 * @param cls closure (internal context for the plugin)
454 * @param block block to cache
455 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
458 namestore_sqlite_cache_block (void *cls,
459 const struct GNUNET_NAMESTORE_Block *block)
461 struct Plugin *plugin = cls;
462 struct GNUNET_HashCode query;
463 struct GNUNET_TIME_Absolute expiration;
468 namestore_sqlite_expire_blocks (plugin);
469 GNUNET_CRYPTO_hash (&block->derived_key,
470 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
472 expiration = GNUNET_TIME_absolute_ntoh (block->expiration_time);
473 dval = (int64_t) expiration.abs_value_us;
476 block_size = ntohl (block->purpose.size) +
477 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) +
478 sizeof (struct GNUNET_CRYPTO_EccSignature);
479 if (block_size > 64 * 65536)
482 return GNUNET_SYSERR;
485 /* delete old version of the block */
487 sqlite3_bind_blob (plugin->delete_block, 1,
488 &query, sizeof (struct GNUNET_HashCode),
491 sqlite3_bind_int64 (plugin->delete_block,
494 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
495 "sqlite3_bind_XXXX");
496 if (SQLITE_OK != sqlite3_reset (plugin->delete_block))
498 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
500 return GNUNET_SYSERR;
502 n = sqlite3_step (plugin->delete_block);
506 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Old block deleted\n");
509 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
513 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
517 if (SQLITE_OK != sqlite3_reset (plugin->delete_block))
518 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
521 /* insert new version of the block */
523 sqlite3_bind_blob (plugin->cache_block, 1,
524 &query, sizeof (struct GNUNET_HashCode),
527 sqlite3_bind_blob (plugin->cache_block, 2,
531 sqlite3_bind_int64 (plugin->cache_block, 3,
535 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
536 "sqlite3_bind_XXXX");
537 if (SQLITE_OK != sqlite3_reset (plugin->cache_block))
539 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
541 return GNUNET_SYSERR;
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Caching block under derived key `%s'\n",
546 GNUNET_h2s_full (&query));
547 n = sqlite3_step (plugin->cache_block);
548 if (SQLITE_OK != sqlite3_reset (plugin->cache_block))
549 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
554 LOG (GNUNET_ERROR_TYPE_DEBUG,
558 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
562 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
564 return GNUNET_SYSERR;
570 * Get the block for a particular zone and label in the
571 * datastore. Will return at most one result to the iterator.
573 * @param cls closure (internal context for the plugin)
574 * @param query hash of public key derived from the zone and the label
575 * @param iter function to call with the result
576 * @param iter_cls closure for @a iter
577 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
580 namestore_sqlite_lookup_block (void *cls,
581 const struct GNUNET_HashCode *query,
582 GNUNET_NAMESTORE_BlockCallback iter, void *iter_cls)
584 struct Plugin *plugin = cls;
588 const struct GNUNET_NAMESTORE_Block *block;
590 if (SQLITE_OK != sqlite3_bind_blob (plugin->lookup_block, 1,
591 query, sizeof (struct GNUNET_HashCode),
594 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
595 "sqlite3_bind_XXXX");
596 if (SQLITE_OK != sqlite3_reset (plugin->lookup_block))
598 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
600 return GNUNET_SYSERR;
603 if (SQLITE_ROW == (sret = sqlite3_step (plugin->lookup_block)))
605 block = sqlite3_column_blob (plugin->lookup_block, 0);
606 block_size = sqlite3_column_bytes (plugin->lookup_block, 0);
607 if ( (block_size < sizeof (struct GNUNET_NAMESTORE_Block)) ||
608 (ntohl (block->purpose.size) +
609 sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) +
610 sizeof (struct GNUNET_CRYPTO_EccSignature) != block_size) )
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Found block under derived key `%s'\n",
619 GNUNET_h2s_full (query));
620 iter (iter_cls, block);
626 if (SQLITE_DONE != sret)
628 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "No block found under derived key `%s'\n",
635 GNUNET_h2s_full (query));
638 if (SQLITE_OK != sqlite3_reset (plugin->lookup_block))
640 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
647 * Store a record in the datastore. Removes any existing record in the
648 * same zone with the same name.
650 * @param cls closure (internal context for the plugin)
651 * @param zone_key private key of the zone
652 * @param label name that is being mapped (at most 255 characters long)
653 * @param rd_count number of entries in @a rd array
654 * @param rd array of records with data to store
655 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
658 namestore_sqlite_store_records (void *cls,
659 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
661 unsigned int rd_count,
662 const struct GNUNET_NAMESTORE_RecordData *rd)
664 struct Plugin *plugin = cls;
666 struct GNUNET_HashCode pkey_hash;
671 memset (&pkey_hash, 0, sizeof (pkey_hash));
672 for (i=0;i<rd_count;i++)
673 if (GNUNET_NAMESTORE_TYPE_PKEY == rd[i].record_type)
675 GNUNET_break (sizeof (struct GNUNET_CRYPTO_EccPublicSignKey) == rd[i].data_size);
676 GNUNET_CRYPTO_hash (rd[i].data,
681 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX);
682 data_size = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
683 if (data_size > 64 * 65536)
686 return GNUNET_SYSERR;
689 char data[data_size];
691 if (data_size != GNUNET_NAMESTORE_records_serialize (rd_count, rd,
695 return GNUNET_SYSERR;
698 /* First delete 'old' records */
699 if ((SQLITE_OK != sqlite3_bind_blob (plugin->delete_records, 1,
700 zone_key, sizeof (struct GNUNET_CRYPTO_EccPrivateKey), SQLITE_STATIC)) ||
701 (SQLITE_OK != sqlite3_bind_text (plugin->delete_records, 2, label, -1, SQLITE_STATIC)))
704 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
705 "sqlite3_bind_XXXX");
706 if (SQLITE_OK != sqlite3_reset (plugin->delete_records))
708 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
710 return GNUNET_SYSERR;
713 n = sqlite3_step (plugin->delete_records);
714 if (SQLITE_OK != sqlite3_reset (plugin->delete_records))
715 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
720 if ((SQLITE_OK != sqlite3_bind_blob (plugin->store_records, 1,
721 zone_key, sizeof (struct GNUNET_CRYPTO_EccPrivateKey), SQLITE_STATIC)) ||
722 (SQLITE_OK != sqlite3_bind_blob (plugin->store_records, 2,
723 &pkey_hash, sizeof (struct GNUNET_HashCode), SQLITE_STATIC)) ||
724 (SQLITE_OK != sqlite3_bind_int64 (plugin->store_records, 3, rvalue)) ||
725 (SQLITE_OK != sqlite3_bind_int (plugin->store_records, 4, rd_count)) ||
726 (SQLITE_OK != sqlite3_bind_blob (plugin->store_records, 5, data, data_size, SQLITE_STATIC)) ||
727 (SQLITE_OK != sqlite3_bind_text (plugin->store_records, 6, label, -1, SQLITE_STATIC)))
730 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
731 "sqlite3_bind_XXXX");
732 if (SQLITE_OK != sqlite3_reset (plugin->store_records))
734 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
736 return GNUNET_SYSERR;
738 n = sqlite3_step (plugin->store_records);
739 if (SQLITE_OK != sqlite3_reset (plugin->store_records))
740 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
748 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record stored\n");
750 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record deleted\n");
753 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
757 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
759 return GNUNET_SYSERR;
765 * The given 'sqlite' statement has been prepared to be run.
766 * It will return a record which should be given to the iterator.
767 * Runs the statement and parses the returned record.
769 * @param plugin plugin context
770 * @param stmt to run (and then clean up)
771 * @param zone_key private key of the zone
772 * @param iter iterator to call with the result
773 * @param iter_cls closure for @a iter
774 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
777 get_record_and_call_iterator (struct Plugin *plugin,
779 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
780 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
782 unsigned int record_count;
790 if (SQLITE_ROW == (sret = sqlite3_step (stmt)))
792 record_count = sqlite3_column_int (stmt, 0);
793 data_size = sqlite3_column_bytes (stmt, 1);
794 data = sqlite3_column_blob (stmt, 1);
795 label = (const char*) sqlite3_column_text (stmt, 2);
796 if (NULL == zone_key)
798 /* must be "iterate_all_zones", got one extra return value */
799 if (sizeof (struct GNUNET_CRYPTO_EccPrivateKey) !=
800 sqlite3_column_bytes (stmt, 3))
807 zone_key = sqlite3_column_blob (stmt, 3);
810 if (record_count > 64 * 1024)
812 /* sanity check, don't stack allocate far too much just
813 because database might contain a large value here */
819 struct GNUNET_NAMESTORE_RecordData rd[record_count];
822 GNUNET_NAMESTORE_records_deserialize (data_size, data,
828 else if (NULL != zone_key)
831 iter (iter_cls, zone_key, label, record_count, rd);
838 if (SQLITE_DONE != sret)
839 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
841 if (SQLITE_OK != sqlite3_reset (stmt))
843 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
850 * Iterate over the results for a particular key and zone in the
851 * datastore. Will return at most one result to the iterator.
853 * @param cls closure (internal context for the plugin)
854 * @param zone hash of public key of the zone, NULL to iterate over all zones
855 * @param offset offset in the list of all matching records
856 * @param iter function to call with the result
857 * @param iter_cls closure for @a iter
858 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
861 namestore_sqlite_iterate_records (void *cls,
862 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
864 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
866 struct Plugin *plugin = cls;
872 stmt = plugin->iterate_all_zones;
873 err = (SQLITE_OK != sqlite3_bind_int64 (stmt, 1,
878 stmt = plugin->iterate_zone;
879 err = ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1,
880 zone, sizeof (struct GNUNET_CRYPTO_EccPrivateKey),
882 (SQLITE_OK != sqlite3_bind_int64 (stmt, 2,
887 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
888 "sqlite3_bind_XXXX");
889 if (SQLITE_OK != sqlite3_reset (stmt))
891 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
893 return GNUNET_SYSERR;
895 return get_record_and_call_iterator (plugin, stmt, zone, iter, iter_cls);
900 * Look for an existing PKEY delegation record for a given public key.
901 * Returns at most one result to the iterator.
903 * @param cls closure (internal context for the plugin)
904 * @param zone private key of the zone to look up in, never NULL
905 * @param value_zone public key of the target zone (value), never NULL
906 * @param iter function to call with the result
907 * @param iter_cls closure for @a iter
908 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
911 namestore_sqlite_zone_to_name (void *cls,
912 const struct GNUNET_CRYPTO_EccPrivateKey *zone,
913 const struct GNUNET_CRYPTO_EccPublicSignKey *value_zone,
914 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
916 struct Plugin *plugin = cls;
919 stmt = plugin->zone_to_name;
920 if ( (SQLITE_OK != sqlite3_bind_blob (stmt, 1,
921 zone, sizeof (struct GNUNET_CRYPTO_EccPrivateKey),
923 (SQLITE_OK != sqlite3_bind_blob (stmt, 2,
924 value_zone, sizeof (struct GNUNET_CRYPTO_EccPublicSignKey),
927 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
928 "sqlite3_bind_XXXX");
929 if (SQLITE_OK != sqlite3_reset (stmt))
931 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
933 return GNUNET_SYSERR;
935 return get_record_and_call_iterator (plugin, stmt, zone, iter, iter_cls);
940 * Entry point for the plugin.
942 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
943 * @return NULL on error, otherwise the plugin context
946 libgnunet_plugin_namestore_sqlite_init (void *cls)
948 static struct Plugin plugin;
949 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
950 struct GNUNET_NAMESTORE_PluginFunctions *api;
952 if (NULL != plugin.cfg)
953 return NULL; /* can only initialize once! */
954 memset (&plugin, 0, sizeof (struct Plugin));
956 if (GNUNET_OK != database_setup (&plugin))
958 database_shutdown (&plugin);
961 api = GNUNET_new (struct GNUNET_NAMESTORE_PluginFunctions);
963 api->cache_block = &namestore_sqlite_cache_block;
964 api->lookup_block = &namestore_sqlite_lookup_block;
965 api->store_records = &namestore_sqlite_store_records;
966 api->iterate_records = &namestore_sqlite_iterate_records;
967 api->zone_to_name = &namestore_sqlite_zone_to_name;
968 LOG (GNUNET_ERROR_TYPE_INFO,
969 _("Sqlite database running\n"));
975 * Exit point from the plugin.
977 * @param cls the plugin context (as returned by "init")
978 * @return always NULL
981 libgnunet_plugin_namestore_sqlite_done (void *cls)
983 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
984 struct Plugin *plugin = api->cls;
986 database_shutdown (plugin);
989 LOG (GNUNET_ERROR_TYPE_DEBUG,
990 "sqlite plugin is finished\n");
994 /* end of plugin_namestore_sqlite.c */