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"
32 * After how many ms "busy" should a DB operation fail for good?
33 * A low value makes sure that we are more responsive to requests
34 * (especially PUTs). A high value guarantees a higher success
35 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
37 * The default value of 1s should ensure that users do not experience
38 * huge latencies while at the same time allowing operations to succeed
39 * with reasonable probability.
41 #define BUSY_TIMEOUT_MS 1000
45 * Log an error message at log-level 'level' that indicates
46 * a failure of the command 'cmd' on file 'filename'
47 * with the message given by strerror(errno).
49 #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)
51 #define LOG(kind,...) GNUNET_log_from (kind, "namestore-sqlite", __VA_ARGS__)
55 * Context for all functions in this plugin.
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * Native SQLite database handle.
73 * Precompiled SQL for put record
75 sqlite3_stmt *put_record;
78 * Precompiled SQL for put node
80 sqlite3_stmt *put_node;
83 * Precompiled SQL for put signature
85 sqlite3_stmt *put_signature;
88 * Precompiled SQL for iterate records
90 sqlite3_stmt *iterate_records;
93 * Precompiled SQL for get node
95 sqlite3_stmt *get_node;
98 * Precompiled SQL for get signature
100 sqlite3_stmt *get_signature;
103 * Precompiled SQL for delete zone
105 sqlite3_stmt *delete_zone_records;
108 * Precompiled SQL for delete zone
110 sqlite3_stmt *delete_zone_nodes;
113 * Precompiled SQL for delete zone
115 sqlite3_stmt *delete_zone_signatures;
121 * @brief Prepare a SQL statement
123 * @param dbh handle to the database
124 * @param zSql SQL statement, UTF-8 encoded
125 * @param ppStmt set to the prepared statement
126 * @return 0 on success
129 sq_prepare (sqlite3 * dbh, const char *zSql, sqlite3_stmt ** ppStmt)
135 sqlite3_prepare_v2 (dbh, zSql, strlen (zSql), ppStmt,
136 (const char **) &dummy);
137 LOG (GNUNET_ERROR_TYPE_DEBUG,
138 "Prepared `%s' / %p: %d\n", zSql, *ppStmt, result);
144 * Create our database indices.
146 * @param dbh handle to the database
149 create_indices (sqlite3 * dbh)
153 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS ir_zone_name_hash ON ns090records (zone_hash,record_name_hash)",
155 LOG (GNUNET_ERROR_TYPE_ERROR,
156 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
160 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS in_zone_location ON ns090nodes (zone_hash,zone_revision,node_location_depth,node_location_offset DESC)",
162 LOG (GNUNET_ERROR_TYPE_ERROR,
163 "Failed to create indices: %s\n", sqlite3_errmsg (dbh));
167 sqlite3_exec (dbh, "CREATE INDEX IF NOT EXISTS is_zone ON ns090signatures (zone_hash)",
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 LOG (GNUNET_ERROR_TYPE_ERROR,
206 _ ("Option `%s' in section `%s' missing in configuration!\n"),
207 "FILENAME", "namestore-sqlite");
208 return GNUNET_SYSERR;
210 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
212 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
215 GNUNET_free (afsdir);
216 return GNUNET_SYSERR;
221 GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), nl_langinfo (CODESET));
223 plugin->fn = GNUNET_STRINGS_to_utf8 (afsdir, strlen (afsdir), "UTF-8"); /* good luck */
225 GNUNET_free (afsdir);
227 /* Open database and precompile statements */
228 if (sqlite3_open (plugin->fn, &plugin->dbh) != SQLITE_OK)
230 LOG (GNUNET_ERROR_TYPE_ERROR,
231 _("Unable to initialize SQLite: %s.\n"),
232 sqlite3_errmsg (plugin->dbh));
233 return GNUNET_SYSERR;
236 sqlite3_exec (plugin->dbh, "PRAGMA temp_store=MEMORY", NULL, NULL,
239 sqlite3_exec (plugin->dbh, "PRAGMA synchronous=NORMAL", NULL, NULL,
242 sqlite3_exec (plugin->dbh, "PRAGMA legacy_file_format=OFF", NULL, NULL,
245 sqlite3_exec (plugin->dbh, "PRAGMA auto_vacuum=INCREMENTAL", NULL,
248 sqlite3_exec (plugin->dbh, "PRAGMA locking_mode=EXCLUSIVE", NULL, NULL,
251 sqlite3_exec (plugin->dbh, "PRAGMA count_changes=OFF", NULL, NULL,
254 sqlite3_exec (plugin->dbh, "PRAGMA page_size=4092", NULL, NULL,
257 CHECK (SQLITE_OK == sqlite3_busy_timeout (plugin->dbh, BUSY_TIMEOUT_MS));
262 sq_prepare (plugin->dbh,
263 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns090records'",
265 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
268 "CREATE TABLE ns090records ("
269 " zone_hash TEXT NOT NULL DEFAULT '',"
270 " zone_revision INT4 NOT NULL DEFAULT 0,"
271 " record_name_hash TEXT NOT NULL DEFAULT '',"
272 " record_name TEXT NOT NULL DEFAULT '',"
273 " record_type INT4 NOT NULL DEFAULT 0,"
274 " node_location_depth INT4 NOT NULL DEFAULT 0,"
275 " node_location_offset INT8 NOT NULL DEFAULT 0,"
276 " record_expiration_time INT8 NOT NULL DEFAULT 0,"
277 " record_flags INT4 NOT NULL DEFAULT 0,"
278 " record_value BLOB 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);
289 sq_prepare (plugin->dbh,
290 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns090nodes'",
292 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
295 "CREATE TABLE ns090nodes ("
296 " zone_hash TEXT NOT NULL DEFAULT '',"
297 " zone_revision INT4 NOT NULL DEFAULT 0,"
298 " node_location_depth INT4 NOT NULL DEFAULT 0,"
299 " node_location_offset INT8 NOT NULL DEFAULT 0,"
300 " node_parent_offset INT8 NOT NULL DEFAULT 0,"
301 " node_hashcodes BLOB NOT NULL DEFAULT ''"
303 NULL, NULL, NULL) != SQLITE_OK))
305 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
306 sqlite3_finalize (stmt);
307 return GNUNET_SYSERR;
309 sqlite3_finalize (stmt);
313 sq_prepare (plugin->dbh,
314 "SELECT 1 FROM sqlite_master WHERE tbl_name = 'ns090signatures'",
316 if ((sqlite3_step (stmt) == SQLITE_DONE) &&
319 "CREATE TABLE ns090signatures ("
320 " zone_hash TEXT NOT NULL DEFAULT '',"
321 " zone_revision INT4 NOT NULL DEFAULT 0,"
322 " zone_time INT8 NOT NULL DEFAULT 0,"
323 " zone_root_hash TEXT NOT NULL DEFAULT 0,"
324 " zone_root_depth INT4 NOT NULL DEFAULT 0,"
325 " zone_public_key BLOB NOT NULL DEFAULT 0,"
326 " zone_signature BLOB NOT NULL DEFAULT 0"
328 NULL, NULL, NULL) != SQLITE_OK))
330 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_exec");
331 sqlite3_finalize (stmt);
332 return GNUNET_SYSERR;
334 sqlite3_finalize (stmt);
337 create_indices (plugin->dbh);
341 "INSERT INTO ns090records (zone_hash, zone_revision, record_name_hash, record_name, "
342 "record_type, node_location_depth, node_location_offset, "
343 "record_expiration_time, record_flags, record_value) VALUES "
344 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
345 &plugin->put_record) != SQLITE_OK) ||
348 "INSERT INTO ns090nodes (zone_hash, zone_revision, "
349 "node_location_depth, node_location_offset, node_parent_offset, node_hashcodes) "
350 "VALUES (?, ?, ?, ?, ?, ?)",
351 &plugin->put_node) != SQLITE_OK) ||
354 "INSERT INTO ns090signatures (zone_hash, zone_revision, zone_time, zone_root_hash, "
355 "zone_root_depth, zone_public_key, zone_signature) "
356 "VALUES (?, ?, ?, ?, ?, ?)",
357 &plugin->put_signature) != SQLITE_OK) ||
360 "SELECT zone_revision,record_name,record_type,node_location_depth,node_location_offset,record_expiration_time,record_flags,record_value "
361 "FROM ns090records WHERE zone_hash=? AND record_name_hash=?",
362 &plugin->iterate_records) != SQLITE_OK) ||
365 "SELECT node_parent_offset,node_hashcodes FROM ns090nodes "
366 "WHERE zone_hash=? AND zone_revision=? AND node_location_depth=? AND node_location_offset<=? ORDER BY node_location_offset DESC LIMIT 1",
367 &plugin->get_node) != SQLITE_OK) ||
370 "SELECT zone_revision,zone_time,zone_root_hash,zone_root_depth,zone_public_key,zone_signature "
371 "FROM ns090signatures WHERE zone_hash=?",
372 &plugin->get_signature) != SQLITE_OK) ||
375 "DELETE FROM gn090records WHERE zone_hash=?",
376 &plugin->delete_zone_records) != SQLITE_OK) ||
379 "DELETE FROM gn090nodes WHERE zone_hash=?",
380 &plugin->delete_zone_nodes) != SQLITE_OK) ||
383 "DELETE FROM gn090signatures WHERE zone_hash=?",
384 &plugin->delete_zone_signatures) != SQLITE_OK) )
386 LOG_SQLITE (plugin,GNUNET_ERROR_TYPE_ERROR, "precompiling");
387 return GNUNET_SYSERR;
394 * Shutdown database connection and associate data
396 * @param plugin the plugin context (state for this module)
399 database_shutdown (struct Plugin *plugin)
404 if (NULL != plugin->put_record)
405 sqlite3_finalize (plugin->put_record);
406 if (NULL != plugin->put_node)
407 sqlite3_finalize (plugin->put_node);
408 if (NULL != plugin->put_signature)
409 sqlite3_finalize (plugin->put_signature);
410 if (NULL != plugin->iterate_records)
411 sqlite3_finalize (plugin->iterate_records);
412 if (NULL != plugin->get_node)
413 sqlite3_finalize (plugin->get_node);
414 if (NULL != plugin->get_signature)
415 sqlite3_finalize (plugin->get_signature);
416 if (NULL != plugin->delete_zone_records)
417 sqlite3_finalize (plugin->delete_zone_records);
418 if (NULL != plugin->delete_zone_nodes)
419 sqlite3_finalize (plugin->delete_zone_nodes);
420 if (NULL != plugin->delete_zone_signatures)
421 sqlite3_finalize (plugin->delete_zone_signatures);
422 result = sqlite3_close (plugin->dbh);
423 if (result == SQLITE_BUSY)
425 LOG (GNUNET_ERROR_TYPE_WARNING,
426 _("Tried to close sqlite without finalizing all prepared statements.\n"));
427 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
430 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
431 "Closing statement %p\n", stmt);
432 result = sqlite3_finalize (stmt);
433 if (result != SQLITE_OK)
434 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite",
435 "Failed to close statement %p: %d\n", stmt, result);
436 stmt = sqlite3_next_stmt (plugin->dbh, NULL);
438 result = sqlite3_close (plugin->dbh);
440 if (SQLITE_OK != result)
441 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite3_close");
443 GNUNET_free_non_null (plugin->fn);
448 * Store a record in the datastore.
450 * @param cls closure (internal context for the plugin)
451 * @param zone hash of the public key of the zone
452 * @param name name that is being mapped (at most 255 characters long)
453 * @param record_type type of the record (A, AAAA, PKEY, etc.)
454 * @param loc location of the signature for the record
455 * @param expiration expiration time for the content
456 * @param flags flags for the content
457 * @param data_size number of bytes in data
458 * @param data value, semantics depend on 'record_type' (see RFCs for DNS and
459 * GNS specification for GNS extensions)
460 * @return GNUNET_OK on success
463 namestore_sqlite_put_record (void *cls,
464 const GNUNET_HashCode *zone,
466 uint32_t record_type,
467 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
468 struct GNUNET_TIME_Absolute expiration,
469 enum GNUNET_NAMESTORE_RecordFlags flags,
473 struct Plugin *plugin = cls;
478 name_len = strlen (name);
479 GNUNET_CRYPTO_hash (name, name_len, &nh);
480 if ((SQLITE_OK != sqlite3_bind_blob (plugin->put_record, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
481 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_record, 2, loc->revision)) ||
482 (SQLITE_OK != sqlite3_bind_blob (plugin->put_record, 3, &nh, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
483 (SQLITE_OK != sqlite3_bind_text (plugin->put_record, 4, name, name_len, SQLITE_STATIC)) ||
484 (SQLITE_OK != sqlite3_bind_int (plugin->put_record, 5, record_type)) ||
485 (SQLITE_OK != sqlite3_bind_int (plugin->put_record, 6, loc->depth)) ||
486 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_record, 7, loc->offset)) ||
487 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_record, 8, expiration.abs_value)) ||
488 (SQLITE_OK != sqlite3_bind_int (plugin->put_record, 9, flags)) ||
489 (SQLITE_OK != sqlite3_bind_blob (plugin->put_record, 10, data, data_size, SQLITE_STATIC)) )
492 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
493 "sqlite3_bind_XXXX");
494 if (SQLITE_OK != sqlite3_reset (plugin->put_record))
496 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
498 return GNUNET_SYSERR;
501 n = sqlite3_step (plugin->put_record);
502 if (SQLITE_OK != sqlite3_reset (plugin->put_record))
503 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
508 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Record stored\n");
511 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
515 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
517 return GNUNET_SYSERR;
523 * Store a Merkle tree node in the datastore.
525 * @param cls closure (internal context for the plugin)
526 * @param zone hash of public key of the zone
527 * @param loc location in the B-tree
528 * @param ploc parent's location in the B-tree (must have depth = loc.depth + 1), NULL for root
529 * @param num_entries number of entries at this node in the B-tree
530 * @param entries the 'num_entries' entries to store (hashes over the
532 * @return GNUNET_OK on success
535 namestore_sqlite_put_node (void *cls,
536 const GNUNET_HashCode *zone,
537 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
538 const struct GNUNET_NAMESTORE_SignatureLocation *ploc,
539 unsigned int num_entries,
540 const GNUNET_HashCode *entries)
542 struct Plugin *plugin = cls;
545 if ( (loc->revision != ploc->revision) ||
546 (loc->depth + 1 != ploc->depth) ||
550 return GNUNET_SYSERR;
552 if ((SQLITE_OK != sqlite3_bind_blob (plugin->put_node, 1, zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
553 (SQLITE_OK != sqlite3_bind_int (plugin->put_node, 2, loc->revision)) ||
554 (SQLITE_OK != sqlite3_bind_int (plugin->put_node, 3, loc->depth)) ||
555 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_node, 4, loc->offset)) ||
556 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_node, 5, ploc->offset)) ||
557 (SQLITE_OK != sqlite3_bind_blob (plugin->put_node, 6, entries, num_entries * sizeof (GNUNET_HashCode), SQLITE_STATIC)) )
559 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
560 "sqlite3_bind_XXXX");
561 if (SQLITE_OK != sqlite3_reset (plugin->put_node))
563 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
565 return GNUNET_SYSERR;
568 n = sqlite3_step (plugin->put_node);
569 if (SQLITE_OK != sqlite3_reset (plugin->put_node))
570 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
575 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Node stored\n");
578 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
582 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
584 return GNUNET_SYSERR;
590 * Store a zone signature in the datastore. If a signature for the zone with a
591 * lower depth exists, the old signature is removed. If a signature for an
592 * older revision of the zone exists, this will delete all records, nodes
593 * and signatures for the older revision of the zone.
595 * @param cls closure (internal context for the plugin)
596 * @param zone_key public key of the zone
597 * @param loc location in the B-tree (top of the tree, offset 0, depth at 'maximum')
598 * @param top_sig signature at the top, NULL if 'loc.depth > 0'
599 * @param root_hash top level hash that is signed
600 * @param zone_time time the zone was signed
601 * @return GNUNET_OK on success
604 namestore_sqlite_put_signature (void *cls,
605 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
606 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
607 const struct GNUNET_CRYPTO_RsaSignature *top_sig,
608 const GNUNET_HashCode *root_hash,
609 struct GNUNET_TIME_Absolute zone_time)
611 struct Plugin *plugin = cls;
613 GNUNET_HashCode zone;
615 GNUNET_CRYPTO_hash (zone_key,
616 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
618 /* FIXME: get "old" signature, if older revision, delete all existing
619 records/nodes for the zone, if same revision, delete only the signature */
622 if ((SQLITE_OK != sqlite3_bind_blob (plugin->put_signature, 1, &zone, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
623 (SQLITE_OK != sqlite3_bind_int (plugin->put_signature, 2, loc->revision)) ||
624 (SQLITE_OK != sqlite3_bind_int64 (plugin->put_signature, 3, zone_time.abs_value)) ||
625 (SQLITE_OK != sqlite3_bind_blob (plugin->put_signature, 4, root_hash, sizeof (GNUNET_HashCode), SQLITE_STATIC)) ||
626 (SQLITE_OK != sqlite3_bind_int (plugin->put_signature, 5, loc->depth)) ||
627 (SQLITE_OK != sqlite3_bind_blob (plugin->put_signature, 6, zone_key, sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), SQLITE_STATIC))||
628 (SQLITE_OK != sqlite3_bind_blob (plugin->put_signature, 7, top_sig, sizeof (struct GNUNET_CRYPTO_RsaSignature), SQLITE_STATIC)) )
630 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
631 "sqlite3_bind_XXXX");
632 if (SQLITE_OK != sqlite3_reset (plugin->put_signature))
634 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
636 return GNUNET_SYSERR;
639 n = sqlite3_step (plugin->put_signature);
640 if (SQLITE_OK != sqlite3_reset (plugin->put_signature))
641 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
646 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite", "Signature stored\n");
649 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
653 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
655 return GNUNET_SYSERR;
661 * Iterate over the results for a particular key and zone in the
662 * datastore. Will only query the latest revision known for the
663 * zone (as adding a new zone revision will cause the plugin to
664 * delete all records from previous revisions).
666 * @param cls closure (internal context for the plugin)
667 * @param zone hash of public key of the zone
668 * @param name_hash hash of name, NULL to iterate over all records of the zone
669 * @param iter maybe NULL (to just count)
670 * @param iter_cls closure for iter
671 * @return the number of results found
674 namestore_sqlite_iterate_records (void *cls,
675 const GNUNET_HashCode *zone,
676 const GNUNET_HashCode *name_hash,
677 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls)
681 struct GNUNET_TIME_Absolute expiration;
682 unsigned long long rowid;
686 n = sqlite3_step (stmt);
690 size = sqlite3_column_bytes (stmt, 5);
691 rowid = sqlite3_column_int64 (stmt, 6);
692 if (sqlite3_column_bytes (stmt, 4) != sizeof (GNUNET_HashCode))
694 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "sqlite",
696 ("Invalid data in database. Trying to fix (by deletion).\n"));
697 if (SQLITE_OK != sqlite3_reset (stmt))
699 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
701 if (GNUNET_OK == delete_by_rowid (plugin, rowid))
702 plugin->env->duc (plugin->env->cls,
703 -(size + GNUNET_NAMESTORE_ENTRY_OVERHEAD));
706 expiration.abs_value = sqlite3_column_int64 (stmt, 3);
708 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "sqlite",
709 "Found reply in database with expiration %llu\n",
710 (unsigned long long) expiration.abs_value);
712 ret = proc (proc_cls, sqlite3_column_blob (stmt, 4) /* key */ ,
713 size, sqlite3_column_blob (stmt, 5) /* data */ ,
714 sqlite3_column_int (stmt, 0) /* type */ ,
715 sqlite3_column_int (stmt, 1) /* priority */ ,
716 sqlite3_column_int (stmt, 2) /* anonymity */ ,
718 if (SQLITE_OK != sqlite3_reset (stmt))
720 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
722 if ((GNUNET_NO == ret) && (GNUNET_OK == delete_by_rowid (plugin, rowid)))
723 plugin->env->duc (plugin->env->cls,
724 -(size + GNUNET_NAMESTORE_ENTRY_OVERHEAD));
727 /* database must be empty */
728 if (SQLITE_OK != sqlite3_reset (stmt))
730 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
737 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
739 if (SQLITE_OK != sqlite3_reset (stmt))
741 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
744 database_shutdown (plugin);
745 database_setup (plugin->env->cfg, plugin);
748 if (SQLITE_OK != sqlite3_reset (stmt))
749 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
751 proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
757 const GNUNET_HashCode *key;
761 GNUNET_assert (proc != NULL);
762 if (sq_prepare (plugin->dbh, "SELECT hash FROM gn090", &stmt) != SQLITE_OK)
764 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
768 while (SQLITE_ROW == (ret = sqlite3_step (stmt)))
770 key = sqlite3_column_blob (stmt, 1);
771 if (sizeof (GNUNET_HashCode) == sqlite3_column_bytes (stmt, 1))
772 proc (proc_cls, key, 1);
774 if (SQLITE_DONE != ret)
775 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR, "sqlite_step");
776 sqlite3_finalize (stmt);
784 * Get a particular node from the signature tree.
786 * @param cls closure (internal context for the plugin)
787 * @param zone hash of public key of the zone
788 * @param loc location of the node in the signature tree
789 * @param cb function to call with the result
790 * @param cb_cls closure for cont
793 namestore_sqlite_get_node (void *cls,
794 const GNUNET_HashCode *zone,
795 const struct GNUNET_NAMESTORE_SignatureLocation *loc,
796 GNUNET_NAMESTORE_NodeCallback cb, void *cb_cls)
803 * Get the current signature for a zone.
805 * @param cls closure (internal context for the plugin)
806 * @param zone hash of public key of the zone
807 * @param cb function to call with the result
808 * @param cb_cls closure for cont
811 namestore_sqlite_get_signature (void *cls,
812 const GNUNET_HashCode *zone,
813 GNUNET_NAMESTORE_SignatureCallback cb, void *cb_cls)
820 * Delete an entire zone (all revisions, all records, all nodes,
821 * all signatures). Not used in normal operation.
823 * @param cls closure (internal context for the plugin)
824 * @param zone zone to delete
827 namestore_sqlite_delete_zone (void *cls,
828 const GNUNET_HashCode *zone)
835 * Entry point for the plugin.
837 * @param cls the "struct GNUNET_NAMESTORE_PluginEnvironment*"
838 * @return NULL on error, othrewise the plugin context
841 libgnunet_plugin_namestore_sqlite_init (void *cls)
843 static struct Plugin plugin;
844 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
845 struct GNUNET_NAMESTORE_PluginFunctions *api;
847 if (NULL != plugin.cfg)
848 return NULL; /* can only initialize once! */
849 memset (&plugin, 0, sizeof (struct Plugin));
851 if (GNUNET_OK != database_setup (&plugin))
853 database_shutdown (&plugin);
856 api = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_PluginFunctions));
858 api->put_record = &namestore_sqlite_put_record;
859 api->put_node = &namestore_sqlite_put_node;
860 api->put_signature = &namestore_sqlite_put_signature;
861 api->iterate_records = &namestore_sqlite_iterate_records;
862 api->get_node = &namestore_sqlite_get_node;
863 api->get_signature = &namestore_sqlite_get_signature;
864 api->delete_zone = &namestore_sqlite_delete_zone;
865 LOG (GNUNET_ERROR_TYPE_INFO,
866 _("Sqlite database running\n"));
872 * Exit point from the plugin.
874 * @param cls the plugin context (as returned by "init")
875 * @return always NULL
878 libgnunet_plugin_namestore_sqlite_done (void *cls)
880 struct GNUNET_NAMESTORE_PluginFunctions *api = cls;
881 struct Plugin *plugin = api->cls;
883 LOG (GNUNET_ERROR_TYPE_DEBUG,
884 "sqlite plugin is done\n");
885 database_shutdown (plugin);
888 LOG (GNUNET_ERROR_TYPE_DEBUG,
889 "sqlite plugin is finished\n");
893 /* end of plugin_namestore_sqlite.c */