2 This file is part of GNUnet
3 (C) 2006, 2009, 2010, 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 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_postgres_lib.h"
29 #include "gnunet_datacache_plugin.h"
30 #include <postgresql/libpq-fe.h>
32 #define DEBUG_POSTGRES GNUNET_EXTRA_LOGGING
35 * Per-entry overhead estimate
37 #define OVERHEAD (sizeof(GNUNET_HashCode) + 24)
40 * Context for all functions in this plugin.
45 * Our execution environment.
47 struct GNUNET_DATACACHE_PluginEnvironment *env;
50 * Native Postgres database handle.
58 * @brief Get a database handle
60 * @param plugin global context
61 * @return GNUNET_OK on success, GNUNET_SYSERR on error
64 init_connection (struct Plugin *plugin)
68 plugin->dbh = GNUNET_POSTGRES_connect (plugin->env->cfg,
69 "datacache-postgres");
70 if (NULL == plugin->dbh)
74 "CREATE TEMPORARY TABLE gn090dc ("
75 " type INTEGER NOT NULL DEFAULT 0,"
76 " discard_time BIGINT NOT NULL DEFAULT 0,"
77 " key BYTEA NOT NULL DEFAULT '',"
78 " value BYTEA NOT NULL DEFAULT '')" "WITH OIDS");
79 if ((ret == NULL) || ((PQresultStatus (ret) != PGRES_COMMAND_OK) && (0 != strcmp ("42P07", /* duplicate table */
84 (void) GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "CREATE TABLE",
86 PQfinish (plugin->dbh);
90 if (PQresultStatus (ret) == PGRES_COMMAND_OK)
93 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_key ON gn090dc (key)")) ||
95 GNUNET_POSTGRES_exec (plugin->dbh, "CREATE INDEX idx_dt ON gn090dc (discard_time)")))
98 PQfinish (plugin->dbh);
100 return GNUNET_SYSERR;
106 "ALTER TABLE gn090dc ALTER value SET STORAGE EXTERNAL");
108 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc"))
110 PQfinish (plugin->dbh);
112 return GNUNET_SYSERR;
115 ret = PQexec (plugin->dbh, "ALTER TABLE gn090dc ALTER key SET STORAGE PLAIN");
117 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "ALTER TABLE", "gn090dc"))
119 PQfinish (plugin->dbh);
121 return GNUNET_SYSERR;
125 GNUNET_POSTGRES_prepare (plugin->dbh, "getkt",
126 "SELECT discard_time,type,value FROM gn090dc "
127 "WHERE key=$1 AND type=$2 ", 2)) ||
129 GNUNET_POSTGRES_prepare (plugin->dbh, "getk",
130 "SELECT discard_time,type,value FROM gn090dc "
131 "WHERE key=$1", 1)) ||
133 GNUNET_POSTGRES_prepare (plugin->dbh, "getm",
134 "SELECT length(value),oid,key FROM gn090dc "
135 "ORDER BY discard_time ASC LIMIT 1", 0)) ||
137 GNUNET_POSTGRES_prepare (plugin->dbh, "delrow", "DELETE FROM gn090dc WHERE oid=$1", 1)) ||
139 GNUNET_POSTGRES_prepare (plugin->dbh, "put",
140 "INSERT INTO gn090dc (type, discard_time, key, value) "
141 "VALUES ($1, $2, $3, $4)", 4)))
143 PQfinish (plugin->dbh);
145 return GNUNET_SYSERR;
152 * Store an item in the datastore.
154 * @param cls closure (our "struct Plugin")
155 * @param key key to store data under
156 * @param size number of bytes in data
157 * @param data data to store
158 * @param type type of the value
159 * @param discard_time when to discard the value in any case
160 * @return 0 on error, number of bytes used otherwise
163 postgres_plugin_put (void *cls, const GNUNET_HashCode * key, size_t size,
164 const char *data, enum GNUNET_BLOCK_Type type,
165 struct GNUNET_TIME_Absolute discard_time)
167 struct Plugin *plugin = cls;
169 uint32_t btype = htonl (type);
170 uint64_t bexpi = GNUNET_TIME_absolute_hton (discard_time).abs_value__;
172 const char *paramValues[] = {
173 (const char *) &btype,
174 (const char *) &bexpi,
178 int paramLengths[] = {
181 sizeof (GNUNET_HashCode),
184 const int paramFormats[] = { 1, 1, 1, 1 };
187 PQexecPrepared (plugin->dbh, "put", 4, paramValues, paramLengths,
190 GNUNET_POSTGRES_check_result (plugin->dbh, ret, PGRES_COMMAND_OK, "PQexecPrepared", "put"))
191 return GNUNET_SYSERR;
193 return size + OVERHEAD;
198 * Iterate over the results for a particular key
201 * @param cls closure (our "struct Plugin")
203 * @param type entries of which type are relevant?
204 * @param iter maybe NULL (to just count)
205 * @param iter_cls closure for iter
206 * @return the number of results found
209 postgres_plugin_get (void *cls, const GNUNET_HashCode * key,
210 enum GNUNET_BLOCK_Type type,
211 GNUNET_DATACACHE_Iterator iter, void *iter_cls)
213 struct Plugin *plugin = cls;
214 uint32_t btype = htonl (type);
216 const char *paramValues[] = {
218 (const char *) &btype,
220 int paramLengths[] = {
221 sizeof (GNUNET_HashCode),
224 const int paramFormats[] = { 1, 1 };
225 struct GNUNET_TIME_Absolute expiration_time;
232 PQexecPrepared (plugin->dbh, (type == 0) ? "getk" : "getkt",
233 (type == 0) ? 1 : 2, paramValues, paramLengths,
236 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared",
237 (type == 0) ? "getk" : "getkt"))
240 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
241 "Ending iteration (postgres error)\n");
246 if (0 == (cnt = PQntuples (res)))
250 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
251 "Ending iteration (no more results)\n");
261 if ((3 != PQnfields (res)) || (sizeof (uint64_t) != PQfsize (res, 0)) ||
262 (sizeof (uint32_t) != PQfsize (res, 1)))
268 for (i = 0; i < cnt; i++)
270 expiration_time.abs_value =
271 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, i, 0));
272 type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1));
273 size = PQgetlength (res, i, 2);
275 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
276 "Found result of size %u bytes and type %u in database\n",
277 (unsigned int) size, (unsigned int) type);
280 iter (iter_cls, expiration_time, key, size, PQgetvalue (res, i, 2),
281 (enum GNUNET_BLOCK_Type) type))
284 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
285 "Ending iteration (client error)\n");
297 * Delete the entry with the lowest expiration value
298 * from the datacache right now.
300 * @param cls closure (our "struct Plugin")
301 * @return GNUNET_OK on success, GNUNET_SYSERR on error
304 postgres_plugin_del (void *cls)
306 struct Plugin *plugin = cls;
312 res = PQexecPrepared (plugin->dbh, "getm", 0, NULL, NULL, NULL, 1);
314 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "getm"))
317 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
318 "Ending iteration (postgres error)\n");
322 if (0 == PQntuples (res))
326 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datacache-postgres",
327 "Ending iteration (no more results)\n");
330 return GNUNET_SYSERR;
332 if ((3 != PQnfields (res)) || (sizeof (size) != PQfsize (res, 0)) ||
333 (sizeof (oid) != PQfsize (res, 1)) ||
334 (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 2)))
340 size = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
341 oid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
342 memcpy (&key, PQgetvalue (res, 0, 2), sizeof (GNUNET_HashCode));
344 if (GNUNET_OK != GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", oid))
345 return GNUNET_SYSERR;
346 plugin->env->delete_notify (plugin->env->cls, &key, size + OVERHEAD);
352 * Entry point for the plugin.
354 * @param cls closure (the "struct GNUNET_DATACACHE_PluginEnvironmnet")
355 * @return the plugin's closure (our "struct Plugin")
358 libgnunet_plugin_datacache_postgres_init (void *cls)
360 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
361 struct GNUNET_DATACACHE_PluginFunctions *api;
362 struct Plugin *plugin;
364 plugin = GNUNET_malloc (sizeof (struct Plugin));
367 if (GNUNET_OK != init_connection (plugin))
369 GNUNET_free (plugin);
373 api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions));
375 api->get = &postgres_plugin_get;
376 api->put = &postgres_plugin_put;
377 api->del = &postgres_plugin_del;
378 GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "datacache-postgres",
379 _("Postgres datacache running\n"));
385 * Exit point from the plugin.
387 * @param cls closure (our "struct Plugin")
391 libgnunet_plugin_datacache_postgres_done (void *cls)
393 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
394 struct Plugin *plugin = api->cls;
396 PQfinish (plugin->dbh);
397 GNUNET_free (plugin);
404 /* end of plugin_datacache_postgres.c */