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 namecache/plugin_namecache_postgres.c
23 * @brief postgres-based namecache backend
24 * @author Christian Grothoff
27 #include "gnunet_namecache_plugin.h"
28 #include "gnunet_namecache_service.h"
29 #include "gnunet_gnsrecord_lib.h"
30 #include "gnunet_postgres_lib.h"
31 #include "namecache.h"
35 * After how many ms "busy" should a DB operation fail for good?
36 * A low value makes sure that we are more responsive to requests
37 * (especially PUTs). A high value guarantees a higher success
38 * rate (SELECTs in iterate can take several seconds despite LIMIT=1).
40 * The default value of 1s should ensure that users do not experience
41 * huge latencies while at the same time allowing operations to succeed
42 * with reasonable probability.
44 #define BUSY_TIMEOUT_MS 1000
48 * Log an error message at log-level 'level' that indicates
49 * a failure of the command 'cmd' on file 'filename'
50 * with the message given by strerror(errno).
52 #define LOG_POSTGRES(db, level, cmd) do { GNUNET_log_from (level, "namecache-postgres", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while(0)
54 #define LOG(kind,...) GNUNET_log_from (kind, "namecache-postgres", __VA_ARGS__)
58 * Context for all functions in this plugin.
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
66 * Native Postgres database handle.
74 * Create our database indices.
76 * @param dbh handle to the database
79 create_indices (PGconn * dbh)
83 GNUNET_POSTGRES_exec (dbh,
84 "CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)")) ||
86 GNUNET_POSTGRES_exec (dbh,
87 "CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)")) )
88 LOG (GNUNET_ERROR_TYPE_ERROR,
89 _("Failed to create indices\n"));
94 * Initialize the database connections and associated
95 * data structures (create tables and indices
98 * @param plugin the plugin context (state for this module)
99 * @return GNUNET_OK on success
102 database_setup (struct Plugin *plugin)
106 plugin->dbh = GNUNET_POSTGRES_connect (plugin->cfg,
107 "namecache-postgres");
108 if (NULL == plugin->dbh)
109 return GNUNET_SYSERR;
111 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
112 "namecache-postgres",
117 "CREATE TEMPORARY TABLE ns096blocks ("
118 " query BYTEA NOT NULL DEFAULT '',"
119 " block BYTEA NOT NULL DEFAULT '',"
120 " expiration_time BIGINT NOT NULL DEFAULT 0"
127 "CREATE TABLE ns096blocks ("
128 " query BYTEA NOT NULL DEFAULT '',"
129 " block BYTEA NOT NULL DEFAULT '',"
130 " expiration_time BIGINT NOT NULL DEFAULT 0"
133 if ( (NULL == res) ||
134 ((PQresultStatus (res) != PGRES_COMMAND_OK) &&
135 (0 != strcmp ("42P07", /* duplicate table */
138 PG_DIAG_SQLSTATE)))))
140 (void) GNUNET_POSTGRES_check_result (plugin->dbh, res,
141 PGRES_COMMAND_OK, "CREATE TABLE",
143 PQfinish (plugin->dbh);
145 return GNUNET_SYSERR;
147 if (PQresultStatus (res) == PGRES_COMMAND_OK)
148 create_indices (plugin->dbh);
152 GNUNET_POSTGRES_prepare (plugin->dbh,
154 "INSERT INTO ns096blocks (query, block, expiration_time) VALUES "
155 "($1, $2, $3)", 3)) ||
157 GNUNET_POSTGRES_prepare (plugin->dbh,
159 "DELETE FROM ns096blocks WHERE expiration_time<$1", 1)) ||
161 GNUNET_POSTGRES_prepare (plugin->dbh,
163 "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2", 2)) ||
165 GNUNET_POSTGRES_prepare (plugin->dbh,
167 "SELECT block FROM ns096blocks WHERE query=$1"
168 " ORDER BY expiration_time DESC LIMIT 1", 1)))
170 PQfinish (plugin->dbh);
172 return GNUNET_SYSERR;
179 * Removes any expired block.
181 * @param plugin the plugin
184 namecache_postgres_expire_blocks (struct Plugin *plugin)
186 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
187 struct GNUNET_TIME_AbsoluteNBO now_be = GNUNET_TIME_absolute_hton (now);
188 const char *paramValues[] = {
189 (const char *) &now_be
191 int paramLengths[] = {
194 const int paramFormats[] = { 1 };
198 PQexecPrepared (plugin->dbh, "expire_blocks", 1,
199 paramValues, paramLengths, paramFormats, 1);
201 GNUNET_POSTGRES_check_result (plugin->dbh,
212 * Delete older block in the datastore.
215 * @param query query for the block
216 * @param expiration time how old does the block have to be for deletion
217 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
220 delete_old_block (struct Plugin *plugin,
221 const struct GNUNET_HashCode *query,
222 struct GNUNET_TIME_AbsoluteNBO expiration_time)
224 const char *paramValues[] = {
225 (const char *) query,
226 (const char *) &expiration_time
228 int paramLengths[] = {
230 sizeof (expiration_time)
232 const int paramFormats[] = { 1, 1 };
236 PQexecPrepared (plugin->dbh, "delete_block", 2,
237 paramValues, paramLengths, paramFormats, 1);
239 GNUNET_POSTGRES_check_result (plugin->dbh,
250 * Cache a block in the datastore.
252 * @param cls closure (internal context for the plugin)
253 * @param block block to cache
254 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
257 namecache_postgres_cache_block (void *cls,
258 const struct GNUNET_GNSRECORD_Block *block)
260 struct Plugin *plugin = cls;
261 struct GNUNET_HashCode query;
262 size_t block_size = ntohl (block->purpose.size) +
263 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
264 sizeof (struct GNUNET_CRYPTO_EcdsaSignature);
265 const char *paramValues[] = {
266 (const char *) &query,
267 (const char *) block,
268 (const char *) &block->expiration_time
270 int paramLengths[] = {
273 sizeof (block->expiration_time)
275 const int paramFormats[] = { 1, 1, 1 };
278 namecache_postgres_expire_blocks (plugin);
279 GNUNET_CRYPTO_hash (&block->derived_key,
280 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
282 if (block_size > 64 * 65536)
285 return GNUNET_SYSERR;
287 delete_old_block (plugin, &query, block->expiration_time);
290 PQexecPrepared (plugin->dbh, "cache_block", 3,
291 paramValues, paramLengths, paramFormats, 1);
293 GNUNET_POSTGRES_check_result (plugin->dbh,
298 return GNUNET_SYSERR;
305 * Get the block for a particular zone and label in the
306 * datastore. Will return at most one result to the iterator.
308 * @param cls closure (internal context for the plugin)
309 * @param query hash of public key derived from the zone and the label
310 * @param iter function to call with the result
311 * @param iter_cls closure for @a iter
312 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
315 namecache_postgres_lookup_block (void *cls,
316 const struct GNUNET_HashCode *query,
317 GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
319 struct Plugin *plugin = cls;
320 const char *paramValues[] = {
323 int paramLengths[] = {
326 const int paramFormats[] = { 1 };
330 const struct GNUNET_GNSRECORD_Block *block;
332 res = PQexecPrepared (plugin->dbh,
334 paramValues, paramLengths, paramFormats,
337 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK,
341 LOG (GNUNET_ERROR_TYPE_DEBUG,
342 "Failing lookup (postgres error)\n");
343 return GNUNET_SYSERR;
345 if (0 == (cnt = PQntuples (res)))
348 LOG (GNUNET_ERROR_TYPE_DEBUG,
349 "Ending iteration (no more results)\n");
353 GNUNET_assert (1 == cnt);
354 GNUNET_assert (1 != PQnfields (res));
355 bsize = PQgetlength (res, 0, 0);
356 block = (const struct GNUNET_GNSRECORD_Block *) PQgetvalue (res, 0, 0);
357 if ( (bsize < sizeof (*block)) ||
358 (bsize != ntohl (block->purpose.size) +
359 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
360 sizeof (struct GNUNET_CRYPTO_EcdsaSignature)) )
363 LOG (GNUNET_ERROR_TYPE_DEBUG,
364 "Failing lookup (corrupt block)\n");
366 return GNUNET_SYSERR;
368 iter (iter_cls, block);
375 * Shutdown database connection and associate data
378 * @param plugin the plugin context (state for this module)
381 database_shutdown (struct Plugin *plugin)
383 PQfinish (plugin->dbh);
389 * Entry point for the plugin.
391 * @param cls the "struct GNUNET_NAMECACHE_PluginEnvironment*"
392 * @return NULL on error, othrewise the plugin context
395 libgnunet_plugin_namecache_postgres_init (void *cls)
397 static struct Plugin plugin;
398 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
399 struct GNUNET_NAMECACHE_PluginFunctions *api;
401 if (NULL != plugin.cfg)
402 return NULL; /* can only initialize once! */
403 memset (&plugin, 0, sizeof (struct Plugin));
405 if (GNUNET_OK != database_setup (&plugin))
407 database_shutdown (&plugin);
410 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
412 api->cache_block = &namecache_postgres_cache_block;
413 api->lookup_block = &namecache_postgres_lookup_block;
414 LOG (GNUNET_ERROR_TYPE_INFO,
415 _("Postgres database running\n"));
421 * Exit point from the plugin.
423 * @param cls the plugin context (as returned by "init")
424 * @return always NULL
427 libgnunet_plugin_namecache_postgres_done (void *cls)
429 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
430 struct Plugin *plugin = api->cls;
432 database_shutdown (plugin);
435 LOG (GNUNET_ERROR_TYPE_DEBUG,
436 "postgres plugin is finished\n");
440 /* end of plugin_namecache_postgres.c */