2 This file is part of GNUnet
3 (C) 2006, 2009, 2010 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 datacache/plugin_datacache_postgres.c
23 * @brief postgres for an implementation of a database backend for the datacache
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_datacache_plugin.h"
29 #include <postgresql/libpq-fe.h>
31 #define DEBUG_POSTGRES GNUNET_NO
34 * Per-entry overhead estimate
36 #define OVERHEAD (sizeof(GNUNET_HashCode) + 24)
39 * Context for all functions in this plugin.
44 * Our execution environment.
46 struct GNUNET_DATACACHE_PluginEnvironment *env;
49 * Native Postgres database handle.
57 * Check if the result obtained from Postgres has
58 * the desired status code. If not, log an error, clear the
59 * result and return GNUNET_SYSERR.
61 * @return GNUNET_OK if the result is acceptable
64 check_result (struct Plugin *plugin, PGresult * ret, int expected_status,
65 const char *command, const char *args, int line)
69 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
71 "Postgres failed to allocate result for `%s:%s' at %d\n",
75 if (PQresultStatus (ret) != expected_status)
77 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
79 _("`%s:%s' failed at %s:%d with error: %s"), command, args,
80 __FILE__, line, PQerrorMessage (plugin->dbh));
89 * Run simple SQL statement (without results).
92 pq_exec (struct Plugin *plugin, const char *sql, int line)
96 ret = PQexec (plugin->dbh, sql);
98 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexec", sql, line))
106 * Prepare SQL statement.
109 pq_prepare (struct Plugin *plugin, const char *name, const char *sql,
110 int nparms, int line)
114 ret = PQprepare (plugin->dbh, name, sql, nparms, NULL);
116 check_result (plugin, ret, PGRES_COMMAND_OK, "PQprepare", sql, line))
117 return GNUNET_SYSERR;
124 * @brief Get a database handle
125 * @return GNUNET_OK on success, GNUNET_SYSERR on error
128 init_connection (struct Plugin *plugin)
133 /* Open database and precompile statements */
135 GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
136 "datacache-postgres", "CONFIG",
139 plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
140 GNUNET_free_non_null (conninfo);
141 if (NULL == plugin->dbh)
143 /* FIXME: warn about out-of-memory? */
144 return GNUNET_SYSERR;
146 if (PQstatus (plugin->dbh) != CONNECTION_OK)
148 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "datacache-postgres",
149 _("Unable to initialize Postgres: %s"),
150 PQerrorMessage (plugin->dbh));
151 PQfinish (plugin->dbh);
153 return GNUNET_SYSERR;
157 "CREATE TEMPORARY TABLE gn090dc ("
158 " type INTEGER NOT NULL DEFAULT 0,"
159 " discard_time BIGINT NOT NULL DEFAULT 0,"
160 " key BYTEA NOT NULL DEFAULT '',"
161 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
162 if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */
165 PG_DIAG_SQLSTATE)))))
167 (void) check_result (plugin, ret, PGRES_COMMAND_OK, "CREATE TABLE",
168 "gn090dc", __LINE__);
169 PQfinish (plugin->dbh);
171 return GNUNET_SYSERR;
173 if (PQresultStatus (ret) == PGRES_COMMAND_OK)
176 pq_exec (plugin, "CREATE INDEX idx_key ON gn090dc (key)", __LINE__)) ||
178 pq_exec (plugin, "CREATE INDEX idx_dt ON gn090dc (discard_time)",
182 PQfinish (plugin->dbh);
184 return GNUNET_SYSERR;
191 "ALTER TABLE gn090dc ALTER value SET STORAGE EXTERNAL");
193 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc",
196 PQfinish (plugin->dbh);
198 return GNUNET_SYSERR;
201 ret = PQexec (plugin->dbh, "ALTER TABLE gn090dc ALTER key SET STORAGE PLAIN");
203 check_result (plugin, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc",
206 PQfinish (plugin->dbh);
208 return GNUNET_SYSERR;
213 pq_prepare (plugin, "getkt",
214 "SELECT discard_time,type,value FROM gn090dc "
215 "WHERE key=$1 AND type=$2 ", 2, __LINE__)) ||
217 pq_prepare (plugin, "getk",
218 "SELECT discard_time,type,value FROM gn090dc "
219 "WHERE key=$1", 1, __LINE__)) ||
221 pq_prepare (plugin, "getm",
222 "SELECT length(value),oid,key FROM gn090dc "
223 "ORDER BY discard_time ASC LIMIT 1", 0, __LINE__)) ||
225 pq_prepare (plugin, "delrow", "DELETE FROM gn090dc WHERE oid=$1", 1,
228 pq_prepare (plugin, "put",
229 "INSERT INTO gn090dc (type, discard_time, key, value) "
230 "VALUES ($1, $2, $3, $4)", 4, __LINE__)))
232 PQfinish (plugin->dbh);
234 return GNUNET_SYSERR;
241 * Delete the row identified by the given rowid (qid
244 * @return GNUNET_OK on success
247 delete_by_rowid (struct Plugin *plugin, uint32_t rowid)
249 uint32_t brow = htonl (rowid);
250 const char *paramValues[] = { (const char *) &brow };
251 int paramLengths[] = { sizeof (brow) };
252 const int paramFormats[] = { 1 };
256 PQexecPrepared (plugin->dbh, "delrow", 1, paramValues, paramLengths,
259 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "delrow",
262 return GNUNET_SYSERR;
270 * Store an item in the datastore.
272 * @param cls closure (our "struct Plugin")
273 * @param key key to store data under
274 * @param size number of bytes in data
275 * @param data data to store
276 * @param type type of the value
277 * @param discard_time when to discard the value in any case
278 * @return 0 on error, number of bytes used otherwise
281 postgres_plugin_put (void *cls, const GNUNET_HashCode * key, size_t size,
282 const char *data, enum GNUNET_BLOCK_Type type,
283 struct GNUNET_TIME_Absolute discard_time)
285 struct Plugin *plugin = cls;
287 uint32_t btype = htonl (type);
288 uint64_t bexpi = GNUNET_TIME_absolute_hton (discard_time).abs_value__;
290 const char *paramValues[] = {
291 (const char *) &btype,
292 (const char *) &bexpi,
296 int paramLengths[] = {
299 sizeof (GNUNET_HashCode),
302 const int paramFormats[] = { 1, 1, 1, 1 };
305 PQexecPrepared (plugin->dbh, "put", 4, paramValues, paramLengths,
308 check_result (plugin, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put",
310 return GNUNET_SYSERR;
312 return size + OVERHEAD;
317 * Iterate over the results for a particular key
320 * @param cls closure (our "struct Plugin")
322 * @param type entries of which type are relevant?
323 * @param iter maybe NULL (to just count)
324 * @param iter_cls closure for iter
325 * @return the number of results found
328 postgres_plugin_get (void *cls, const GNUNET_HashCode * key,
329 enum GNUNET_BLOCK_Type type,
330 GNUNET_DATACACHE_Iterator iter, void *iter_cls)
332 struct Plugin *plugin = cls;
333 uint32_t btype = htonl (type);
335 const char *paramValues[] = {
337 (const char *) &btype,
339 int paramLengths[] = {
340 sizeof (GNUNET_HashCode),
343 const int paramFormats[] = { 1, 1 };
344 struct GNUNET_TIME_Absolute expiration_time;
352 PQexecPrepared (plugin->dbh, (type == 0) ? "getk" : "getkt",
353 (type == 0) ? 1 : 2, paramValues, paramLengths,
356 check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared",
357 (type == 0) ? "getk" : "getkt", __LINE__))
360 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
361 "Ending iteration (postgres error)\n");
366 if (0 == (cnt = PQntuples (res)))
370 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
371 "Ending iteration (no more results)\n");
381 if ((3 != PQnfields (res)) || (sizeof (uint64_t) != PQfsize (res, 0)) ||
382 (sizeof (uint32_t) != PQfsize (res, 1)))
388 for (i = 0; i < cnt; i++)
390 expiration_time.abs_value =
391 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, i, 0));
392 type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1));
393 size = PQgetlength (res, i, 2);
395 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
396 "Found result of size %u bytes and type %u in database\n",
397 (unsigned int) size, (unsigned int) type);
400 iter (iter_cls, expiration_time, key, size, PQgetvalue (res, i, 2),
401 (enum GNUNET_BLOCK_Type) type))
404 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
405 "Ending iteration (client error)\n");
417 * Delete the entry with the lowest expiration value
418 * from the datacache right now.
420 * @param cls closure (our "struct Plugin")
421 * @return GNUNET_OK on success, GNUNET_SYSERR on error
424 postgres_plugin_del (void *cls)
426 struct Plugin *plugin = cls;
432 res = PQexecPrepared (plugin->dbh, "getm", 0, NULL, NULL, NULL, 1);
434 check_result (plugin, res, PGRES_TUPLES_OK, "PQexecPrepared", "getm",
438 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
439 "Ending iteration (postgres error)\n");
443 if (0 == PQntuples (res))
447 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
448 "Ending iteration (no more results)\n");
451 return GNUNET_SYSERR;
453 if ((3 != PQnfields (res)) || (sizeof (size) != PQfsize (res, 0)) ||
454 (sizeof (oid) != PQfsize (res, 1)) ||
455 (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 2)))
461 size = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
462 oid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
463 memcpy (&key, PQgetvalue (res, 0, 2), sizeof (GNUNET_HashCode));
465 if (GNUNET_OK != delete_by_rowid (plugin, oid))
466 return GNUNET_SYSERR;
467 plugin->env->delete_notify (plugin->env->cls, &key, size + OVERHEAD);
473 * Entry point for the plugin.
475 * @param cls closure (the "struct GNUNET_DATACACHE_PluginEnvironmnet")
476 * @return the plugin's closure (our "struct Plugin")
479 libgnunet_plugin_datacache_postgres_init (void *cls)
481 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
482 struct GNUNET_DATACACHE_PluginFunctions *api;
483 struct Plugin *plugin;
485 plugin = GNUNET_malloc (sizeof (struct Plugin));
488 if (GNUNET_OK != init_connection (plugin))
490 GNUNET_free (plugin);
494 api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions));
496 api->get = &postgres_plugin_get;
497 api->put = &postgres_plugin_put;
498 api->del = &postgres_plugin_del;
499 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "datacache-postgres",
500 _("Postgres datacache running\n"));
506 * Exit point from the plugin.
508 * @param cls closure (our "struct Plugin")
512 libgnunet_plugin_datacache_postgres_done (void *cls)
514 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
515 struct Plugin *plugin = api->cls;
517 PQfinish (plugin->dbh);
518 GNUNET_free (plugin);
525 /* end of plugin_datacache_postgres.c */