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 LOG(kind,...) GNUNET_log_from (kind, "datacache-postgres", __VA_ARGS__)
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"))
239 LOG (GNUNET_ERROR_TYPE_DEBUG,
240 "Ending iteration (postgres error)\n");
244 if (0 == (cnt = PQntuples (res)))
247 LOG (GNUNET_ERROR_TYPE_DEBUG,
248 "Ending iteration (no more results)\n");
257 if ((3 != PQnfields (res)) || (sizeof (uint64_t) != PQfsize (res, 0)) ||
258 (sizeof (uint32_t) != PQfsize (res, 1)))
264 for (i = 0; i < cnt; i++)
266 expiration_time.abs_value =
267 GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, i, 0));
268 type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1));
269 size = PQgetlength (res, i, 2);
270 LOG (GNUNET_ERROR_TYPE_DEBUG,
271 "Found result of size %u bytes and type %u in database\n",
272 (unsigned int) size, (unsigned int) type);
274 iter (iter_cls, expiration_time, key, size, PQgetvalue (res, i, 2),
275 (enum GNUNET_BLOCK_Type) type))
277 LOG (GNUNET_ERROR_TYPE_DEBUG,
278 "Ending iteration (client error)\n");
289 * Delete the entry with the lowest expiration value
290 * from the datacache right now.
292 * @param cls closure (our "struct Plugin")
293 * @return GNUNET_OK on success, GNUNET_SYSERR on error
296 postgres_plugin_del (void *cls)
298 struct Plugin *plugin = cls;
304 res = PQexecPrepared (plugin->dbh, "getm", 0, NULL, NULL, NULL, 1);
306 GNUNET_POSTGRES_check_result (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "getm"))
308 LOG (GNUNET_ERROR_TYPE_DEBUG,
309 "Ending iteration (postgres error)\n");
312 if (0 == PQntuples (res))
315 LOG (GNUNET_ERROR_TYPE_DEBUG,
316 "Ending iteration (no more results)\n");
318 return GNUNET_SYSERR;
320 if ((3 != PQnfields (res)) || (sizeof (size) != PQfsize (res, 0)) ||
321 (sizeof (oid) != PQfsize (res, 1)) ||
322 (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 2)))
328 size = ntohl (*(uint32_t *) PQgetvalue (res, 0, 0));
329 oid = ntohl (*(uint32_t *) PQgetvalue (res, 0, 1));
330 memcpy (&key, PQgetvalue (res, 0, 2), sizeof (GNUNET_HashCode));
332 if (GNUNET_OK != GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", oid))
333 return GNUNET_SYSERR;
334 plugin->env->delete_notify (plugin->env->cls, &key, size + OVERHEAD);
340 * Entry point for the plugin.
342 * @param cls closure (the "struct GNUNET_DATACACHE_PluginEnvironmnet")
343 * @return the plugin's closure (our "struct Plugin")
346 libgnunet_plugin_datacache_postgres_init (void *cls)
348 struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
349 struct GNUNET_DATACACHE_PluginFunctions *api;
350 struct Plugin *plugin;
352 plugin = GNUNET_malloc (sizeof (struct Plugin));
355 if (GNUNET_OK != init_connection (plugin))
357 GNUNET_free (plugin);
361 api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions));
363 api->get = &postgres_plugin_get;
364 api->put = &postgres_plugin_put;
365 api->del = &postgres_plugin_del;
366 LOG (GNUNET_ERROR_TYPE_INFO,
367 _("Postgres datacache running\n"));
373 * Exit point from the plugin.
375 * @param cls closure (our "struct Plugin")
379 libgnunet_plugin_datacache_postgres_done (void *cls)
381 struct GNUNET_DATACACHE_PluginFunctions *api = cls;
382 struct Plugin *plugin = api->cls;
384 PQfinish (plugin->dbh);
385 GNUNET_free (plugin);
392 /* end of plugin_datacache_postgres.c */