gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnunet] 01/02: -remove unnecessary and badly used extra argument in pos


From: gnunet
Subject: [gnunet] 01/02: -remove unnecessary and badly used extra argument in postgres statement preparation
Date: Wed, 12 Oct 2022 14:48:26 +0200

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository gnunet.

commit 55553e01b8e19d250756538a3d94e48f16ecbb7b
Author: Christian Grothoff <christian@grothoff.org>
AuthorDate: Wed Oct 12 13:02:23 2022 +0200

    -remove unnecessary and badly used extra argument in postgres statement 
preparation
---
 src/datacache/plugin_datacache_postgres.c | 21 +++-----
 src/datastore/plugin_datastore_postgres.c | 33 ++++--------
 src/include/gnunet_pq_lib.h               | 28 ++++++----
 src/namecache/plugin_namecache_postgres.c | 10 ++--
 src/namestore/plugin_namestore_postgres.c | 39 +++++++-------
 src/pq/Makefile.am                        |  2 +-
 src/pq/pq_prepare.c                       | 89 ++++++++++++++-----------------
 7 files changed, 101 insertions(+), 121 deletions(-)

diff --git a/src/datacache/plugin_datacache_postgres.c 
b/src/datacache/plugin_datacache_postgres.c
index b1f9a1b8e..01b2372f1 100644
--- a/src/datacache/plugin_datacache_postgres.c
+++ b/src/datacache/plugin_datacache_postgres.c
@@ -95,21 +95,17 @@ init_connection (struct Plugin *plugin)
   struct GNUNET_PQ_PreparedStatement ps[] = {
     GNUNET_PQ_make_prepare ("getkt",
                             "SELECT expiration_time,type,ro,value,trunc,path 
FROM gn180dc "
-                            "WHERE key=$1 AND type=$2 AND expiration_time >= 
$3",
-                            3),
+                            "WHERE key=$1 AND type=$2 AND expiration_time >= 
$3"),
     GNUNET_PQ_make_prepare ("getk",
                             "SELECT expiration_time,type,ro,value,trunc,path 
FROM gn180dc "
-                            "WHERE key=$1 AND expiration_time >= $2",
-                            2),
+                            "WHERE key=$1 AND expiration_time >= $2"),
     GNUNET_PQ_make_prepare ("getex",
                             "SELECT LENGTH(value) AS len,oid,key FROM gn180dc"
                             " WHERE expiration_time < $1"
-                            " ORDER BY expiration_time ASC LIMIT 1",
-                            1),
+                            " ORDER BY expiration_time ASC LIMIT 1"),
     GNUNET_PQ_make_prepare ("getm",
                             "SELECT LENGTH(value) AS len,oid,key FROM gn180dc"
-                            " ORDER BY prox ASC, expiration_time ASC LIMIT 1",
-                            0),
+                            " ORDER BY prox ASC, expiration_time ASC LIMIT 1"),
     GNUNET_PQ_make_prepare ("get_closest",
                             "(SELECT 
expiration_time,type,ro,value,trunc,path,key FROM gn180dc"
                             " WHERE key >= $1"
@@ -123,16 +119,13 @@ init_connection (struct Plugin *plugin)
                             "   AND expiration_time >= $2"
                             "   AND ( (type = $3) OR ( 0 = $3) )"
                             " ORDER BY key DESC"
-                            " LIMIT $4)",
-                            4),
+                            " LIMIT $4)"),
     GNUNET_PQ_make_prepare ("delrow",
-                            "DELETE FROM gn180dc WHERE oid=$1",
-                            1),
+                            "DELETE FROM gn180dc WHERE oid=$1"),
     GNUNET_PQ_make_prepare ("put",
                             "INSERT INTO gn180dc"
                             " (type, ro, prox, expiration_time, key, value, 
trunc, path) "
-                            "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
-                            8),
+                            "VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"),
     GNUNET_PQ_PREPARED_STATEMENT_END
   };
 
diff --git a/src/datastore/plugin_datastore_postgres.c 
b/src/datastore/plugin_datastore_postgres.c
index 8fb0bf6ee..27e8b7072 100644
--- a/src/datastore/plugin_datastore_postgres.c
+++ b/src/datastore/plugin_datastore_postgres.c
@@ -119,59 +119,48 @@ init_connection (struct Plugin *plugin)
                             " (rvalue >= $2 OR 0 = $3::smallint) AND"
                             " (hash = $4 OR 0 = $5::smallint) AND"
                             " (type = $6 OR 0 = $7::smallint)"
-                            " ORDER BY oid ASC LIMIT 1",
-                            7),
+                            " ORDER BY oid ASC LIMIT 1"),
     GNUNET_PQ_make_prepare ("put",
                             "INSERT INTO gn090 (repl, type, prio, anonLevel, 
expire, rvalue, hash, vhash, value) "
-                            "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
-                            9),
+                            "VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)"),
     GNUNET_PQ_make_prepare ("update",
                             "UPDATE gn090"
                             " SET prio = prio + $1,"
                             " repl = repl + $2,"
                             " expire = GREATEST(expire, $3)"
-                            " WHERE hash = $4 AND vhash = $5",
-                            5),
+                            " WHERE hash = $4 AND vhash = $5"),
     GNUNET_PQ_make_prepare ("decrepl",
                             "UPDATE gn090 SET repl = GREATEST (repl - 1, 0) "
-                            "WHERE oid = $1",
-                            1),
+                            "WHERE oid = $1"),
     GNUNET_PQ_make_prepare ("select_non_anonymous",
                             "SELECT " RESULT_COLUMNS " FROM gn090 "
                             "WHERE anonLevel = 0 AND type = $1 AND oid >= 
$2::bigint "
-                            "ORDER BY oid ASC LIMIT 1",
-                            2),
+                            "ORDER BY oid ASC LIMIT 1"),
     GNUNET_PQ_make_prepare ("select_expiration_order",
                             "(SELECT " RESULT_COLUMNS " FROM gn090 "
                             "WHERE expire < $1 ORDER BY prio ASC LIMIT 1) "
                             "UNION "
                             "(SELECT " RESULT_COLUMNS " FROM gn090 "
                             "ORDER BY prio ASC LIMIT 1) "
-                            "ORDER BY expire ASC LIMIT 1",
-                            1),
+                            "ORDER BY expire ASC LIMIT 1"),
     GNUNET_PQ_make_prepare ("select_replication_order",
                             "SELECT " RESULT_COLUMNS " FROM gn090 "
-                            "ORDER BY repl DESC,RANDOM() LIMIT 1",
-                            0),
+                            "ORDER BY repl DESC,RANDOM() LIMIT 1"),
     GNUNET_PQ_make_prepare ("delrow",
                             "DELETE FROM gn090 "
-                            "WHERE oid=$1",
-                            1),
+                            "WHERE oid=$1"),
     GNUNET_PQ_make_prepare ("remove",
                             "DELETE FROM gn090"
                             " WHERE hash = $1 AND"
-                            " value = $2",
-                            2),
+                            " value = $2"),
     GNUNET_PQ_make_prepare ("get_keys",
-                            "SELECT hash FROM gn090",
-                            0),
+                            "SELECT hash FROM gn090"),
     GNUNET_PQ_make_prepare ("estimate_size",
                             "SELECT CASE WHEN NOT EXISTS"
                             "  (SELECT 1 FROM gn090)"
                             "  THEN 0"
                             "  ELSE (SELECT SUM(LENGTH(value))+256*COUNT(*) 
FROM gn090)"
-                            "END AS total",
-                            0),
+                            "END AS total"),
     GNUNET_PQ_PREPARED_STATEMENT_END
   };
 #undef RESULT_COLUMNS
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h
index 2263704aa..7396c6861 100644
--- a/src/include/gnunet_pq_lib.h
+++ b/src/include/gnunet_pq_lib.h
@@ -1,6 +1,6 @@
 /*
    This file is part of GNUnet
-   Copyright (C) 2016, 2017, 2020, 2021 GNUnet e.V.
+   Copyright (C) 2016, 2017, 2020-2022 GNUnet e.V.
 
    GNUnet is free software: you can redistribute it and/or modify it
    under the terms of the GNU Affero General Public License as published
@@ -754,11 +754,6 @@ struct GNUNET_PQ_PreparedStatement
    */
   const char *sql;
 
-  /**
-   * Number of arguments included in @e sql.
-   */
-  unsigned int num_arguments;
-
 };
 
 
@@ -767,7 +762,7 @@ struct GNUNET_PQ_PreparedStatement
  */
 #define GNUNET_PQ_PREPARED_STATEMENT_END \
   {                                      \
-    NULL, NULL, 0                        \
+    NULL, NULL                           \
   }
 
 
@@ -776,13 +771,11 @@ struct GNUNET_PQ_PreparedStatement
  *
  * @param name name of the statement
  * @param sql actual SQL statement
- * @param num_args number of arguments in the statement
  * @return initialized struct
  */
 struct GNUNET_PQ_PreparedStatement
 GNUNET_PQ_make_prepare (const char *name,
-                        const char *sql,
-                        unsigned int num_args);
+                        const char *sql);
 
 
 /**
@@ -799,6 +792,21 @@ GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
                               const struct GNUNET_PQ_PreparedStatement *ps);
 
 
+/**
+ * Request creation of prepared statements @a ps from Postgres, but do not 
automatically re-prepare the statement
+ * if we are disconnected from the database.
+ *
+ * @param db database to prepare the statements for
+ * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
+ *            statements.
+ * @return #GNUNET_OK on success,
+ *         #GNUNET_SYSERR on error
+ */
+enum GNUNET_GenericReturnValue
+GNUNET_PQ_prepare_once (struct GNUNET_PQ_Context *db,
+                        const struct GNUNET_PQ_PreparedStatement *ps);
+
+
 /* ******************** pq_exec.c functions ************** */
 
 
diff --git a/src/namecache/plugin_namecache_postgres.c 
b/src/namecache/plugin_namecache_postgres.c
index ae0f71a1f..d6d1730ce 100644
--- a/src/namecache/plugin_namecache_postgres.c
+++ b/src/namecache/plugin_namecache_postgres.c
@@ -96,16 +96,14 @@ database_setup (struct Plugin *plugin)
     struct GNUNET_PQ_PreparedStatement ps[] = {
       GNUNET_PQ_make_prepare ("cache_block",
                               "INSERT INTO ns096blocks (query, block, 
expiration_time) VALUES "
-                              "($1, $2, $3)", 3),
+                              "($1, $2, $3)"),
       GNUNET_PQ_make_prepare ("expire_blocks",
-                              "DELETE FROM ns096blocks WHERE 
expiration_time<$1",
-                              1),
+                              "DELETE FROM ns096blocks WHERE 
expiration_time<$1"),
       GNUNET_PQ_make_prepare ("delete_block",
-                              "DELETE FROM ns096blocks WHERE query=$1 AND 
expiration_time<=$2",
-                              2),
+                              "DELETE FROM ns096blocks WHERE query=$1 AND 
expiration_time<=$2"),
       GNUNET_PQ_make_prepare ("lookup_block",
                               "SELECT block FROM ns096blocks WHERE query=$1"
-                              " ORDER BY expiration_time DESC LIMIT 1", 1),
+                              " ORDER BY expiration_time DESC LIMIT 1"),
       GNUNET_PQ_PREPARED_STATEMENT_END
     };
 
diff --git a/src/namestore/plugin_namestore_postgres.c 
b/src/namestore/plugin_namestore_postgres.c
index bfe006cba..147dda0e6 100644
--- a/src/namestore/plugin_namestore_postgres.c
+++ b/src/namestore/plugin_namestore_postgres.c
@@ -144,7 +144,8 @@ init_database (struct Plugin *plugin, char **emsg, int drop)
                                                 es,
                                                 NULL);
     }
-    else {
+    else
+    {
       plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg,
                                                 "namestore-postgres",
                                                 NULL,
@@ -162,6 +163,7 @@ init_database (struct Plugin *plugin, char **emsg, int drop)
   return GNUNET_OK;
 }
 
+
 static int
 database_prepare (struct Plugin *plugin)
 {
@@ -177,32 +179,25 @@ database_prepare (struct Plugin *plugin)
                             " DO UPDATE"
                             "    SET 
pkey=$2,rvalue=$3,record_count=$4,record_data=$5"
                             "    WHERE ns098records.zone_private_key = $1"
-                            "          AND ns098records.label = $6",
-                            6),
+                            "          AND ns098records.label = $6"),
     GNUNET_PQ_make_prepare ("delete_records",
                             "DELETE FROM ns098records "
-                            "WHERE zone_private_key=$1 AND label=$2",
-                            2),
+                            "WHERE zone_private_key=$1 AND label=$2"),
     GNUNET_PQ_make_prepare ("zone_to_name",
                             "SELECT seq,record_count,record_data,label FROM 
ns098records"
-                            " WHERE zone_private_key=$1 AND pkey=$2",
-                            2),
+                            " WHERE zone_private_key=$1 AND pkey=$2"),
     GNUNET_PQ_make_prepare ("iterate_zone",
                             "SELECT seq,record_count,record_data,label FROM 
ns098records "
-                            "WHERE zone_private_key=$1 AND seq > $2 ORDER BY 
seq ASC LIMIT $3",
-                            3),
+                            "WHERE zone_private_key=$1 AND seq > $2 ORDER BY 
seq ASC LIMIT $3"),
     GNUNET_PQ_make_prepare ("iterate_all_zones",
                             "SELECT 
seq,record_count,record_data,label,zone_private_key"
-                            " FROM ns098records WHERE seq > $1 ORDER BY seq 
ASC LIMIT $2",
-                            2),
+                            " FROM ns098records WHERE seq > $1 ORDER BY seq 
ASC LIMIT $2"),
     GNUNET_PQ_make_prepare ("lookup_label",
                             "SELECT seq,record_count,record_data,label "
-                            "FROM ns098records WHERE zone_private_key=$1 AND 
label=$2",
-                            2),
+                            "FROM ns098records WHERE zone_private_key=$1 AND 
label=$2"),
     GNUNET_PQ_make_prepare ("edit_set",
                             "SELECT seq,record_count,record_data,label "
-                            "FROM ns098records WHERE zone_private_key=$1 AND 
label=$2 FOR UPDATE NOWAIT",
-                            2),
+                            "FROM ns098records WHERE zone_private_key=$1 AND 
label=$2 FOR UPDATE NOWAIT"),
     GNUNET_PQ_PREPARED_STATEMENT_END
   };
   ret = GNUNET_PQ_prepare_statements (plugin->dbh, ps);
@@ -221,7 +216,7 @@ database_prepare (struct Plugin *plugin)
  * @param plugin the plugin context (state for this module)
  * @return #GNUNET_OK on success
  */
-static int
+static enum GNUNET_GenericReturnValue
 database_connect (struct Plugin *plugin)
 {
   char *emsg;
@@ -235,7 +230,10 @@ database_connect (struct Plugin *plugin)
      * Gracefully fail as this should not be a critical error if the
      * database is already created
      */
-    if (GNUNET_OK != init_database (plugin, &emsg, GNUNET_NO))
+    if (GNUNET_OK !=
+        init_database (plugin,
+                       &emsg,
+                       GNUNET_NO))
     {
       LOG (GNUNET_ERROR_TYPE_WARNING,
            "Failed to initialize database on connect: `%s'\n",
@@ -485,6 +483,7 @@ parse_result_call_iterator (void *cls,
   pc->limit -= num_results;
 }
 
+
 /**
  * Lookup records in the datastore for which we are the authority.
  *
@@ -535,6 +534,7 @@ lookup_records (void *cls,
   return GNUNET_OK;
 }
 
+
 /**
  * Lookup records in the datastore for which we are the authority.
  *
@@ -556,6 +556,7 @@ namestore_postgres_lookup_records (void *cls,
   return lookup_records (cls, zone, label, iter, iter_cls, "lookup_label");
 }
 
+
 /**
  * Edit records in the datastore for which we are the authority.
  *
@@ -690,6 +691,7 @@ namestore_postgres_zone_to_name (void *cls,
   return GNUNET_OK;
 }
 
+
 /**
  * Begin a transaction for a client.
  *
@@ -711,6 +713,7 @@ namestore_postgres_transaction_begin (void *cls,
   return GNUNET_PQ_exec_statements (plugin->dbh, es);
 }
 
+
 /**
  * Commit a transaction for a client.
  * This releases the lock on the database.
@@ -733,6 +736,7 @@ namestore_postgres_transaction_rollback (void *cls,
   return GNUNET_PQ_exec_statements (plugin->dbh, es);
 }
 
+
 /**
  * Roll back a transaction for a client.
  * This releases the lock on the database.
@@ -780,7 +784,6 @@ namestore_postgres_reset_database (void *cls,
 }
 
 
-
 /**
  * Shutdown database connection and associate data
  * structures.
diff --git a/src/pq/Makefile.am b/src/pq/Makefile.am
index d3a2ce9ac..013b20588 100644
--- a/src/pq/Makefile.am
+++ b/src/pq/Makefile.am
@@ -24,7 +24,7 @@ libgnunetpq_la_LIBADD = -lpq \
 libgnunetpq_la_LDFLAGS = \
  $(POSTGRESQL_LDFLAGS) \
  $(GN_LIB_LDFLAGS) \
-  -version-info 2:0:0
+  -version-info 3:0:0
 
 if ENABLE_TEST_RUN
 TESTS = \
diff --git a/src/pq/pq_prepare.c b/src/pq/pq_prepare.c
index fcf1cc793..e5970144d 100644
--- a/src/pq/pq_prepare.c
+++ b/src/pq/pq_prepare.c
@@ -26,68 +26,23 @@
 #include "pq.h"
 
 
-/**
- * Create a `struct GNUNET_PQ_PreparedStatement`.
- *
- * @param name name of the statement
- * @param sql actual SQL statement
- * @param num_args number of arguments in the statement
- * @return initialized struct
- */
 struct GNUNET_PQ_PreparedStatement
 GNUNET_PQ_make_prepare (const char *name,
-                        const char *sql,
-                        unsigned int num_args)
+                        const char *sql)
 {
   struct GNUNET_PQ_PreparedStatement ps = {
     .name = name,
-    .sql = sql,
-    .num_arguments = num_args
+    .sql = sql
   };
 
   return ps;
 }
 
 
-/**
- * Request creation of prepared statements @a ps from Postgres.
- *
- * @param db database to prepare the statements for
- * @param ps #GNUNET_PQ_PREPARED_STATEMENT_END-terminated array of prepared
- *            statements.
- * @return #GNUNET_OK on success,
- *         #GNUNET_SYSERR on error
- */
 enum GNUNET_GenericReturnValue
-GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
-                              const struct GNUNET_PQ_PreparedStatement *ps)
+GNUNET_PQ_prepare_once (struct GNUNET_PQ_Context *db,
+                        const struct GNUNET_PQ_PreparedStatement *ps)
 {
-  if (db->ps != ps)
-  {
-    /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
-    unsigned int olen = 0; /* length of existing 'db->ps' array */
-    unsigned int nlen = 0; /* length of 'ps' array */
-    struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
-
-    if (NULL != db->ps)
-      while (NULL != db->ps[olen].name)
-        olen++;
-    while (NULL != ps[nlen].name)
-      nlen++;
-    rps = GNUNET_new_array (olen + nlen + 1,
-                            struct GNUNET_PQ_PreparedStatement);
-    if (NULL != db->ps)
-      memcpy (rps,
-              db->ps,
-              olen * sizeof (struct GNUNET_PQ_PreparedStatement));
-    memcpy (&rps[olen],
-            ps,
-            nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
-    GNUNET_free (db->ps);
-    db->ps = rps;
-  }
-
-  /* actually prepare statements */
   for (unsigned int i = 0; NULL != ps[i].name; i++)
   {
     PGresult *ret;
@@ -100,7 +55,7 @@ GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
     ret = PQprepare (db->conn,
                      ps[i].name,
                      ps[i].sql,
-                     ps[i].num_arguments,
+                     0,
                      NULL);
     if (PGRES_COMMAND_OK != PQresultStatus (ret))
     {
@@ -129,4 +84,38 @@ GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
 }
 
 
+enum GNUNET_GenericReturnValue
+GNUNET_PQ_prepare_statements (struct GNUNET_PQ_Context *db,
+                              const struct GNUNET_PQ_PreparedStatement *ps)
+{
+  if (db->ps != ps)
+  {
+    /* add 'ps' to list db->ps of prepared statements to run on reconnect! */
+    unsigned int olen = 0; /* length of existing 'db->ps' array */
+    unsigned int nlen = 0; /* length of 'ps' array */
+    struct GNUNET_PQ_PreparedStatement *rps; /* combined array */
+
+    if (NULL != db->ps)
+      while (NULL != db->ps[olen].name)
+        olen++;
+    while (NULL != ps[nlen].name)
+      nlen++;
+    rps = GNUNET_new_array (olen + nlen + 1,
+                            struct GNUNET_PQ_PreparedStatement);
+    if (NULL != db->ps)
+      memcpy (rps,
+              db->ps,
+              olen * sizeof (struct GNUNET_PQ_PreparedStatement));
+    memcpy (&rps[olen],
+            ps,
+            nlen * sizeof (struct GNUNET_PQ_PreparedStatement));
+    GNUNET_free (db->ps);
+    db->ps = rps;
+  }
+
+  return GNUNET_PQ_prepare_once (db,
+                                 ps);
+}
+
+
 /* end of pq/pq_prepare.c */

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]