/*
This file is part of GNUnet
(C) 2009, 2010 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file datastore/plugin_datastore_postgres.c
* @brief postgres-based datastore backend
* @author Christian Grothoff
*/
#include "platform.h"
#include "plugin_datastore.h"
#include <postgresql/libpq-fe.h>
#define DEBUG_POSTGRES GNUNET_NO
#define SELECT_IT_LOW_PRIORITY "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "\
"WHERE (prio = $1 AND oid > $2) " \
"ORDER BY prio ASC,oid ASC LIMIT 1) "\
"UNION "\
"(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "\
"WHERE (prio > $1 AND oid != $2)"\
"ORDER BY prio ASC,oid ASC LIMIT 1)"\
"ORDER BY prio ASC,oid ASC LIMIT 1"
#define SELECT_IT_NON_ANONYMOUS "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "\
"WHERE (prio = $1 AND oid < $2)"\
" AND anonLevel=0 ORDER BY prio DESC,oid DESC LIMIT 1) "\
"UNION "\
"(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "\
"WHERE (prio < $1 AND oid != $2)"\
" AND anonLevel=0 ORDER BY prio DESC,oid DESC LIMIT 1) "\
"ORDER BY prio DESC,oid DESC LIMIT 1"
#define SELECT_IT_EXPIRATION_TIME "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "\
"WHERE (expire = $1 AND oid > $2) "\
"ORDER BY expire ASC,oid ASC LIMIT 1) "\
"UNION "\
"(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "\
"WHERE (expire > $1 AND oid != $2) " \
"ORDER BY expire ASC,oid ASC LIMIT 1)"\
"ORDER BY expire ASC,oid ASC LIMIT 1"
#define SELECT_IT_MIGRATION_ORDER "(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "\
"WHERE (expire = $1 AND oid < $2)"\
" AND expire > $3 AND type!=3"\
" ORDER BY expire DESC,oid DESC LIMIT 1) "\
"UNION "\
"(SELECT type, prio, anonLevel, expire, hash, value, oid FROM gn090 "\
"WHERE (expire < $1 AND oid != $2)" \
" AND expire > $3 AND type!=3"\
" ORDER BY expire DESC,oid DESC LIMIT 1)"\
"ORDER BY expire DESC,oid DESC LIMIT 1"
/**
* After how many ms "busy" should a DB operation fail for good?
* A low value makes sure that we are more responsive to requests
* (especially PUTs). A high value guarantees a higher success
* rate (SELECTs in iterate can take several seconds despite LIMIT=1).
*
* The default value of 1s should ensure that users do not experience
* huge latencies while at the same time allowing operations to succeed
* with reasonable probability.
*/
#define BUSY_TIMEOUT GNUNET_TIME_UNIT_SECONDS
/**
* Closure for 'postgres_next_request_cont'.
*/
struct NextRequestClosure
{
/**
* Global plugin data.
*/
struct Plugin *plugin;
/**
* Function to call for each matching entry.
*/
PluginIterator iter;
/**
* Closure for 'iter'.
*/
void *iter_cls;
/**
* Parameters for the prepared statement.
*/
const char *paramValues[5];
/**
* Name of the prepared statement to run.
*/
const char *pname;
/**
* Size of values pointed to by paramValues.
*/
int paramLengths[5];
/**
* Number of paramters in paramValues/paramLengths.
*/
int nparams;
/**
* Current time (possible parameter), big-endian.
*/
uint64_t