gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r8044 - GNUnet/src/applications/sqstore_postgres


From: gnunet
Subject: [GNUnet-SVN] r8044 - GNUnet/src/applications/sqstore_postgres
Date: Mon, 29 Dec 2008 03:06:14 -0700 (MST)

Author: grothoff
Date: 2008-12-29 03:06:14 -0700 (Mon, 29 Dec 2008)
New Revision: 8044

Modified:
   GNUnet/src/applications/sqstore_postgres/Makefile.am
   GNUnet/src/applications/sqstore_postgres/postgres.c
Log:
stuff

Modified: GNUnet/src/applications/sqstore_postgres/Makefile.am
===================================================================
--- GNUnet/src/applications/sqstore_postgres/Makefile.am        2008-12-28 
20:47:27 UTC (rev 8043)
+++ GNUnet/src/applications/sqstore_postgres/Makefile.am        2008-12-29 
10:06:14 UTC (rev 8044)
@@ -27,7 +27,7 @@
   $(POSTGRES_LDFLAGS)
 libgnunetmodule_sqstore_postgres_la_LIBADD = \
   $(top_builddir)/src/util/libgnunetutil.la \
-  -lpg \
+  -lpq \
   $(LTLIBINTL)
 
 EXTRA_DIST = check.conf

Modified: GNUnet/src/applications/sqstore_postgres/postgres.c
===================================================================
--- GNUnet/src/applications/sqstore_postgres/postgres.c 2008-12-28 20:47:27 UTC 
(rev 8043)
+++ GNUnet/src/applications/sqstore_postgres/postgres.c 2008-12-29 10:06:14 UTC 
(rev 8044)
@@ -105,47 +105,38 @@
 
 static unsigned int stat_size;
 
-#if DEBUG_POSTGRES
-static unsigned int stat_mem;
-#endif
-
 static struct GNUNET_GE_Context *ectx;
 
 static struct GNUNET_Mutex *lock;
 
-static char *fn;
-
 static unsigned long long payload;
 
 static unsigned int lastSync;
 
 /**
- * @brief Prepare a SQL statement
+ * Check if the result obtained from Postgres has
+ * the desired status code.  If not, log an error, clear the
+ * result and return GNUNET_SYSERR.
+ * 
+ * @return GNUNET_OK if the result is acceptable
  */
 static int
-pq_prepare (const char *zSql, int nParams,
-           const Oid * paramTypes)
+check_result (PGresult * ret,
+             int expected_status,
+             const char * command)
 {
-  PGresult * ret;
-  ret = PQprepare (dbh,
-                  zSql,
-                  zSql,
-                  nParams,
-                  paramTypes);
   if (ret == NULL)
-    {
-      GNUNET_GE_LOG (ectx, GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                   _("PQprepare failed (returning NULL)\n"));
+    {      
+      /* FIXME: report error! */
       return GNUNET_SYSERR;
     }
-  if (PQresultStatus(res) != PGRES_COMMAND_OK)
+  if (PQresultStatus (ret) != expected_status)       
     {
       LOG_POSTGRES (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                   "PQprepare");
-      PQclear (ret);
+                   command);
+      PQclear(ret);
       return GNUNET_SYSERR;
     }
-  PQclear (ret);
   return GNUNET_OK;
 }
 
@@ -157,18 +148,11 @@
 {
   PGresult * ret;
   ret = PQexec (dbh, sql);
-  if (ret == NULL)
-    {
-      /* FIXME: report error! */
+  if (GNUNET_OK != check_result (ret, PGRES_COMMAND_OK, "PQexec"))
+    {      
+      GNUNET_mutex_unlock (lock);
       return GNUNET_SYSERR;
     }
-  if (PQresultStatus (res) != PGRES_COMMAND_OK)
-    {
-      LOG_POSTGRES (GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_BULK,
-                   "PQexec");
-      PQclear(ret);
-      return GNUNET_SYSERR;
-    }
   PQclear(ret);
   return GNUNET_OK;
 }
@@ -277,43 +261,41 @@
   GNUNET_mutex_lock (lock);
   ret = payload;
   if (stats)
-    {
-      stats->set (stat_size, ret);
-#if DEBUG_POSTGRES
-      stats->set (stat_mem, postgres3_memory_used ());
-#endif
-    }
+    stats->set (stat_size, ret);    
   GNUNET_mutex_unlock (lock);
   return (unsigned long long) (ret * 1.00);
   /* benchmarking shows XX% overhead */
 }
 
-///////////////////////////////////////////////
+/**
+ * @brief write all statistics to the db
+ */
+static void
+syncStats ()
+{
+  // setStat (handle, "PAYLOAD", payload);
+  lastSync = 0;
+}
 
-static int
-delete_by_rowid (postgresHandle * handle, unsigned long long rid)
+/**
+ * @brief Get database statistics
+ * @param key kind of stat to retrieve
+ * @return GNUNET_SYSERR on error, the value otherwise
+ */
+static unsigned long long
+getStat (const char *key)
 {
-  postgres3_stmt *stmt;
+  return 0;
+}
 
-  if (sq_prepare (handle->dbh,
-                  "DELETE FROM gn080 WHERE _ROWID_ = ?", &stmt) != POSTGRES_OK)
-    {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "sq_prepare");
-      return GNUNET_SYSERR;
-    }
-  postgres3_bind_int64 (stmt, 1, rid);
-  if (POSTGRES_DONE != postgres3_step (stmt))
-    {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres3_step");
-      postgres3_finalize (stmt);
-      return GNUNET_SYSERR;
-    }
-  postgres3_finalize (stmt);
-  return GNUNET_OK;
+
+///////////////////////////////////////////////
+
+
+static void
+delete_by_rowid (unsigned int rowid)
+{
+  /* FIXME... */
 }
 
 /**
@@ -321,14 +303,15 @@
  * assemble it into a GNUNET_DatastoreValue representation.
  */
 static GNUNET_DatastoreValue *
-assembleDatum (postgresHandle * handle, postgres3_stmt * stmt,
-               GNUNET_HashCode * key, unsigned long long *rowid)
+assembleDatum (PGresult * handle,
+               GNUNET_HashCode * key,
+              unsigned int * rowid)
 {
+  return NULL;
+#if 0
   GNUNET_DatastoreValue *value;
   int contentSize;
-  postgres3 *dbh;
   unsigned int type;
-  postgres3_stmt *stmtd;
 
   *rowid = postgres3_column_int64 (stmt, 7);
   type = postgres3_column_int (stmt, 1);
@@ -420,114 +403,10 @@
   memcpy (key, postgres3_column_blob (stmt, 5), sizeof (GNUNET_HashCode));
   memcpy (&value[1], postgres3_column_blob (stmt, 6), contentSize);
   return value;
+#endif
 }
 
-
 /**
- * @brief Get database statistics
- * @param key kind of stat to retrieve
- * @return GNUNET_SYSERR on error, the value otherwise
- */
-static unsigned long long
-getStat (const char *key)
-{
-  int i;
-  postgres3_stmt *stmt;
-  unsigned long long ret = GNUNET_SYSERR;
-
-  i = sq_prepare (handle->dbh,
-                  "SELECT value FROM gn071 WHERE key = ?", &stmt);
-  if (i == POSTGRES_OK)
-    {
-      postgres3_bind_text (stmt, 1, key, strlen (key), POSTGRES_STATIC);
-      i = postgres3_step (stmt);
-
-      if (i == POSTGRES_DONE)
-        {
-          ret = 0;
-          i = POSTGRES_OK;
-        }
-      else if (i == POSTGRES_ROW)
-        {
-          ret = postgres3_column_int64 (stmt, 0);
-          i = POSTGRES_OK;
-        }
-      postgres3_finalize (stmt);
-    }
-  if (i == POSTGRES_BUSY)
-    return GNUNET_SYSERR;
-  if (i != POSTGRES_OK)
-    {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres_getStat");
-      return GNUNET_SYSERR;
-    }
-  return ret;
-}
-
-/**
- * @brief set database statistics
- * @param key statistic to set
- * @param val value to set
- * @return GNUNET_SYSERR on error, GNUNET_OK otherwise
- */
-static int
-setStat (postgresHandle * handle, const char *key, unsigned long long val)
-{
-  postgres3_stmt *stmt;
-  postgres3 *dbh;
-
-  dbh = handle->dbh;
-  if (sq_prepare (dbh, "DELETE FROM gn071 where key = ?", &stmt) == 
POSTGRES_OK)
-    {
-      postgres3_bind_text (stmt, 1, key, strlen (key), POSTGRES_STATIC);
-      if (POSTGRES_DONE != postgres3_step (stmt))
-        LOG_POSTGRES (handle,
-                    GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                    GNUNET_GE_BULK, "postgres3_step");
-
-      postgres3_finalize (stmt);
-    }
-
-  if (sq_prepare (dbh,
-                  "INSERT INTO gn071(key, value) VALUES (?, ?)",
-                  &stmt) != POSTGRES_OK)
-    return GNUNET_SYSERR;
-  if ((POSTGRES_OK !=
-       postgres3_bind_text (stmt, 1, key, strlen (key), POSTGRES_STATIC))
-      || (POSTGRES_OK != postgres3_bind_int64 (stmt, 2, val)))
-    {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres3_bind_xxx");
-      postgres3_finalize (stmt);
-      return GNUNET_SYSERR;
-    }
-  if (postgres3_step (stmt) != POSTGRES_DONE)
-    {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres3_step");
-      postgres3_finalize (stmt);
-      return GNUNET_SYSERR;
-    }
-  postgres3_finalize (stmt);
-
-  return GNUNET_OK;
-}
-
-/**
- * @brief write all statistics to the db
- */
-static void
-syncStats (postgresHandle * handle)
-{
-  setStat (handle, "PAYLOAD", payload);
-  lastSync = 0;
-}
-
-/**
  * Call a method for each key in the database and
  * call the callback method on it.
  *
@@ -541,231 +420,171 @@
  */
 static int
 postgres_iterate (unsigned int type,
-                int is_asc,
-                int is_prio,
-                int is_migr,
-                int limit_nonanonymous,
-                const char *stmt_str_1,
-                const char *stmt_str_2,
-                GNUNET_DatastoreValueIterator iter, void *closure)
+                 int is_asc,
+                 unsigned int iter_select,
+                 GNUNET_DatastoreValueIterator dviter, 
+                 void *closure)
 {
-  postgres3_stmt *stmt_1;
-  postgres3_stmt *stmt_2;
-  int count;
-  GNUNET_DatastoreValue *datum_1;
-  GNUNET_DatastoreValue *datum_2;
-  GNUNET_DatastoreValue *last_datum_2;
+#if 0
   GNUNET_DatastoreValue *datum;
-  unsigned int lastPrio;
-  unsigned long long lastExp;
-  GNUNET_HashCode key_1;
-  GNUNET_HashCode key_2;
-  GNUNET_HashCode key;
+  int count;
   int ret;
+  unsigned int last_prio;
+  unsigned long long last_expire;
+  unsigned long long last_vkey;
+  unsigned int size;
+  unsigned int rtype;
+  unsigned int prio;
+  unsigned int level;
+  unsigned long long expiration;
+  unsigned long long vkey;
+  unsigned long hashSize;
+  GNUNET_HashCode key;
   GNUNET_CronTime now;
-  unsigned long long rowid;
-  unsigned long long rowid_1;
-  unsigned long long rowid_2;
+  MYSQL_BIND rbind[7];
 
-  GNUNET_mutex_lock (lock);
-  if (sq_prepare (dbh, stmt_str_1, &stmt_1) != POSTGRES_OK)
-    {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres3_prepare");
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  if (sq_prepare (dbh, stmt_str_2, &stmt_2) != POSTGRES_OK)
-    {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres3_prepare");
-      postgres3_finalize (stmt_1);
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  count = 0;
   if (is_asc)
     {
-      lastPrio = 0;
-      lastExp = 0;
-      memset (&key, 0, sizeof (GNUNET_HashCode));
+      last_prio = 0;
+      last_vkey = 0;
+      last_expire = 0;
     }
   else
     {
-      lastPrio = 0x7FFFFFFF;
-      lastExp = 0x7FFFFFFFFFFFFFFFLL;
-      memset (&key, 255, sizeof (GNUNET_HashCode));
+      last_prio = 0x7FFFFFFFL;
+      last_vkey = 0x7FFFFFFFFFFFFFFFLL; /* MySQL only supports 63 bits */
+      last_expire = 0x7FFFFFFFFFFFFFFFLL;       /* MySQL only supports 63 bits 
*/
     }
-  last_datum_2 = NULL;
+  hashSize = sizeof (GNUNET_HashCode);
+  memset (rbind, 0, sizeof (rbind));
+  rbind[0].buffer_type = MYSQL_TYPE_LONG;
+  rbind[0].buffer = &size;
+  rbind[0].is_unsigned = 1;
+  rbind[1].buffer_type = MYSQL_TYPE_LONG;
+  rbind[1].buffer = &rtype;
+  rbind[1].is_unsigned = 1;
+  rbind[2].buffer_type = MYSQL_TYPE_LONG;
+  rbind[2].buffer = &prio;
+  rbind[2].is_unsigned = 1;
+  rbind[3].buffer_type = MYSQL_TYPE_LONG;
+  rbind[3].buffer = &level;
+  rbind[3].is_unsigned = 1;
+  rbind[4].buffer_type = MYSQL_TYPE_LONGLONG;
+  rbind[4].buffer = &expiration;
+  rbind[4].is_unsigned = 1;
+  rbind[5].buffer_type = MYSQL_TYPE_BLOB;
+  rbind[5].buffer = &key;
+  rbind[5].buffer_length = hashSize;
+  rbind[5].length = &hashSize;
+  rbind[6].buffer_type = MYSQL_TYPE_LONGLONG;
+  rbind[6].buffer = &vkey;
+  rbind[6].is_unsigned = GNUNET_YES;
+
+  now = GNUNET_get_time ();
+  count = 0;
   while (1)
     {
-      if (is_prio)
+      switch (iter_select)
         {
-          postgres3_bind_int (stmt_1, 1, lastPrio);
-          postgres3_bind_int (stmt_2, 1, lastPrio);
+        case 0:
+        case 1:
+          ret = GNUNET_MYSQL_prepared_statement_run_select (iter[iter_select],
+                                                            7,
+                                                            rbind,
+                                                            &return_ok,
+                                                            NULL,
+                                                            MYSQL_TYPE_LONG,
+                                                            &last_prio,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_vkey,
+                                                            GNUNET_YES,
+                                                            MYSQL_TYPE_LONG,
+                                                            &last_prio,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_vkey,
+                                                            GNUNET_YES, -1);
+          break;
+        case 2:
+          ret = GNUNET_MYSQL_prepared_statement_run_select (iter[iter_select],
+                                                            7,
+                                                            rbind,
+                                                            &return_ok,
+                                                            NULL,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_expire,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_vkey,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_expire,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_vkey,
+                                                            GNUNET_YES, -1);
+          break;
+        case 3:
+          ret = GNUNET_MYSQL_prepared_statement_run_select (iter[iter_select],
+                                                            7,
+                                                            rbind,
+                                                            &return_ok,
+                                                            NULL,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_expire,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_vkey,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &now,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_expire,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &last_vkey,
+                                                            GNUNET_YES,
+                                                            
MYSQL_TYPE_LONGLONG,
+                                                            &now,
+                                                            GNUNET_YES, -1);
+          break;
+        default:
+          GNUNET_GE_BREAK (NULL, 0);
+          return GNUNET_SYSERR;
         }
-      else
+      if (ret != GNUNET_OK)
+        break;
+      last_vkey = vkey;
+      last_prio = prio;
+      last_expire = expiration;
+      count++;
+      if (dviter != NULL)
         {
-          postgres3_bind_int64 (stmt_1, 1, lastExp);
-          postgres3_bind_int64 (stmt_2, 1, lastExp);
-        }
-      postgres3_bind_blob (stmt_1, 2, &key, sizeof (GNUNET_HashCode),
-                         POSTGRES_TRANSIENT);
-      now = GNUNET_get_time ();
-      datum_1 = NULL;
-      datum_2 = last_datum_2;
-      last_datum_2 = NULL;
-      if ((ret = postgres3_step (stmt_1)) == POSTGRES_ROW)
-        {
-          if (is_migr && postgres3_column_int64 (stmt_1, 4) < now)
-            datum_1 = NULL;
-          else
-            datum_1 = assembleDatum (handle, stmt_1, &key_1, &rowid_1);
-          if (POSTGRES_OK != postgres3_reset (stmt_1))
-            LOG_POSTGRES (handle,
-                        GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                        GNUNET_GE_BULK, "postgres3_reset");
-        }
-      else
-        {
-          if (ret != POSTGRES_DONE)
+          datum = assembleDatum (rbind);
+          if (datum == NULL)
+            continue;
+          ret = dviter (&key, datum, closure, vkey);
+          if (ret == GNUNET_SYSERR)
             {
-              LOG_POSTGRES (handle,
-                          GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                          GNUNET_GE_BULK, "postgres3_step");
-              postgres3_finalize (stmt_1);
-              postgres3_finalize (stmt_2);
-              GNUNET_mutex_unlock (lock);
-              return GNUNET_SYSERR;
+              GNUNET_free (datum);
+              break;
             }
-          postgres3_reset (stmt_1);
-        }
-
-      if (datum_2 == NULL)
-        {
-          if ((ret = postgres3_step (stmt_2)) == POSTGRES_ROW)
+          if (ret == GNUNET_NO)
             {
-              if (is_migr && postgres3_column_int64 (stmt_2, 4) < now)
-                datum_2 = NULL;
-              else
-                datum_2 = assembleDatum (handle, stmt_2, &key_2, &rowid_2);
-              if (POSTGRES_OK != postgres3_reset (stmt_2))
-                LOG_POSTGRES (handle,
-                            GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER
-                            | GNUNET_GE_BULK, "postgres3_reset");
-            }
-          else
-            {
-              if (ret != POSTGRES_DONE)
-                {
-                  LOG_POSTGRES (handle,
-                              GNUNET_GE_ERROR | GNUNET_GE_ADMIN |
-                              GNUNET_GE_USER | GNUNET_GE_BULK,
-                              "postgres3_step");
-                  postgres3_finalize (stmt_1);
-                  postgres3_finalize (stmt_2);
-                  GNUNET_mutex_unlock (lock);
-                  GNUNET_free_non_null (datum_1);
-                  return GNUNET_SYSERR;
-                }
-              postgres3_reset (stmt_2);
-            }
-        }
-      datum = NULL;
-      if (datum_1 == NULL)
-        {
-          datum = datum_2;
-          rowid = rowid_2;
-          key = key_2;
-        }
-      else if (datum_2 == NULL)
-        {
-          datum = datum_1;
-          rowid = rowid_1;
-          key = key_1;
-        }
-      else
-        {
-          /* have to pick between 1 and 2 */
-          if (is_prio)
-            {
-              if ((ntohl (datum_1->priority) < ntohl (datum_2->priority)) ==
-                  is_asc)
-                {
-                  datum = datum_1;
-                  rowid = rowid_1;
-                  key = key_1;
-                  last_datum_2 = datum_2;
-                }
-              else
-                {
-                  datum = datum_2;
-                  rowid = rowid_2;
-                  key = key_2;
-                  GNUNET_free (datum_1);
-                }
-            }
-          else
-            {
-              if ((GNUNET_ntohll (datum_1->expiration_time) <
-                   GNUNET_ntohll (datum_2->expiration_time)) == is_asc)
-                {
-                  datum = datum_1;
-                  rowid = rowid_1;
-                  key = key_1;
-                  last_datum_2 = datum_2;
-                }
-              else
-                {
-                  datum = datum_2;
-                  rowid = rowid_2;
-                  key = key_2;
-                  GNUNET_free (datum_1);
-                }
-            }
-        }
-      if (datum == NULL)
-        break;
-#if 0
-      printf ("FOUND %4u prio %4u exp %20llu old: %4u, %20llu\n",
-              (ntohl (datum->size) - sizeof (GNUNET_DatastoreValue)),
-              ntohl (datum->priority),
-              GNUNET_ntohll (datum->expiration_time), lastPrio, lastExp);
-#endif
-      if (((GNUNET_NO == limit_nonanonymous) ||
-           (ntohl (datum->anonymity_level) == 0)) &&
-          ((type == GNUNET_ECRS_BLOCKTYPE_ANY) ||
-           (type == ntohl (datum->type))))
-        {
-          count++;
-          if (iter != NULL)
-            {
-              GNUNET_mutex_unlock (lock);
-              ret = iter (&key, datum, closure, rowid);
+              do_delete_value (vkey);
+              do_delete_entry_by_vkey (vkey);
               GNUNET_mutex_lock (lock);
-              if (ret == GNUNET_SYSERR)
-                {
-                  GNUNET_free (datum);
-                  break;
-                }
-              if (ret == GNUNET_NO)
-                {
-                  payload -= getContentDatastoreSize (datum);
-                  delete_by_rowid (handle, rowid);
-                }
+              content_size -= ntohl (datum->size);
+              GNUNET_mutex_unlock (lock);
             }
+          GNUNET_free (datum);
         }
-      lastPrio = ntohl (datum->priority);
-      lastExp = GNUNET_ntohll (datum->expiration_time);
-      GNUNET_free (datum);
     }
-  postgres3_finalize (stmt_1);
-  postgres3_finalize (stmt_2);
-  GNUNET_free_non_null (last_datum_2);
-  GNUNET_mutex_unlock (lock);
   return count;
+#endif
+  return 0;
 }
 
 /**
@@ -782,9 +601,7 @@
 iterateLowPriority (unsigned int type, GNUNET_DatastoreValueIterator iter,
                     void *closure)
 {
-  return postgres_iterate (type, GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_NO,
-                         SELECT_IT_LOW_PRIORITY_1,
-                         SELECT_IT_LOW_PRIORITY_2, iter, closure);
+  return postgres_iterate (type, GNUNET_YES, 0, iter, closure);
 }
 
 /**
@@ -800,9 +617,7 @@
 iterateNonAnonymous (unsigned int type, GNUNET_DatastoreValueIterator iter,
                      void *closure)
 {
-  return postgres_iterate (type, GNUNET_NO, GNUNET_YES, GNUNET_NO, GNUNET_YES,
-                         SELECT_IT_NON_ANONYMOUS_1,
-                         SELECT_IT_NON_ANONYMOUS_2, iter, closure);
+  return postgres_iterate (type, GNUNET_NO, 1, iter, closure);
 }
 
 /**
@@ -818,9 +633,7 @@
 iterateExpirationTime (unsigned int type, GNUNET_DatastoreValueIterator iter,
                        void *closure)
 {
-  return postgres_iterate (type, GNUNET_YES, GNUNET_NO, GNUNET_NO, GNUNET_NO,
-                         SELECT_IT_EXPIRATION_TIME_1,
-                         SELECT_IT_EXPIRATION_TIME_2, iter, closure);
+  return postgres_iterate (type, GNUNET_YES, 2, iter, closure);
 }
 
 /**
@@ -833,9 +646,7 @@
 static int
 iterateMigrationOrder (GNUNET_DatastoreValueIterator iter, void *closure)
 {
-  return postgres_iterate (0, GNUNET_NO, GNUNET_NO, GNUNET_YES, GNUNET_NO,
-                         SELECT_IT_MIGRATION_ORDER_1,
-                         SELECT_IT_MIGRATION_ORDER_2, iter, closure);
+  return postgres_iterate (0, GNUNET_NO, 3, iter, closure);
 }
 
 /**
@@ -850,87 +661,7 @@
 static int
 iterateAllNow (GNUNET_DatastoreValueIterator iter, void *closure)
 {
-  postgres3_stmt *stmt;
-  int count;
-  GNUNET_DatastoreValue *datum;
-  int ret;
-  unsigned long long newpayload;
-  unsigned long long rowid;
-  unsigned long long last_rowid;
-  GNUNET_HashCode key;
-
-  newpayload = 0;
-  GNUNET_mutex_lock (lock);
-  /* For the rowid trick see
-     http://permalink.gmane.org/gmane.network.gnunet.devel/1363 */
-  if (sq_prepare (dbh,
-                  "SELECT size,type,prio,anonLevel,expire,hash,value,_ROWID_"
-                  " FROM gn080 WHERE _ROWID_ > :1 ORDER BY _ROWID_ ASC LIMIT 
1",
-                  &stmt) != POSTGRES_OK)
-    {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres3_prepare");
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
-    }
-  count = 0;
-  last_rowid = 0;
-  while (1)
-    {
-      ret = postgres3_bind_int64 (stmt, 1, last_rowid);
-      if (ret != POSTGRES_OK)
-        break;
-      if (postgres3_step (stmt) != POSTGRES_ROW)
-        break;
-      datum = assembleDatum (handle, stmt, &key, &rowid);
-#if 0
-      printf ("IA-FOUND %4u prio %4u exp %20llu RID %llu old-RID: %llu\n",
-              (ntohl (datum->size) - sizeof (GNUNET_DatastoreValue)),
-              ntohl (datum->priority),
-              GNUNET_ntohll (datum->expiration_time), rowid, last_rowid);
-#endif
-      last_rowid = rowid;
-      postgres3_reset (stmt);
-      if (datum == NULL)
-        continue;
-      newpayload += getContentDatastoreSize (datum);
-      count++;
-      if (iter != NULL)
-        {
-          GNUNET_mutex_unlock (lock);
-          ret = iter (&key, datum, closure, rowid);
-          GNUNET_mutex_lock (lock);
-        }
-      else
-        ret = GNUNET_OK;
-      if (ret == GNUNET_SYSERR)
-        {
-          GNUNET_free (datum);
-          break;
-        }
-      if (ret == GNUNET_NO)
-        {
-          newpayload -= getContentDatastoreSize (datum);
-          delete_by_rowid (handle, rowid);
-        }
-      GNUNET_free (datum);
-    }
-  postgres3_reset (stmt);
-  postgres3_finalize (stmt);
-  if (count != GNUNET_SYSERR)
-    {
-      /* re-computed payload! */
-      GNUNET_GE_LOG (ectx,
-                     GNUNET_GE_INFO | GNUNET_GE_IMMEDIATE | GNUNET_GE_USER |
-                     GNUNET_GE_ADMIN,
-                     "Postgres database size recomputed.  New estimate is 
%llu, old estimate was %llu\n",
-                     newpayload, payload);
-      payload = newpayload;
-      syncStats (handle);
-    }
-  GNUNET_mutex_unlock (lock);
-  return count;
+  return postgres_iterate (0, GNUNET_YES, 0, iter, closure);
 }
 
 
@@ -957,96 +688,182 @@
      const GNUNET_HashCode * vhash,
      unsigned int type, GNUNET_DatastoreValueIterator iter, void *closure)
 {
-  int ret;
+  unsigned int total;
+  const char * paramValues[5];
+  int paramLengths[5];
+  const int paramFormats[] = {1,1,1,1,1};
+  unsigned int last_rowid;
+  unsigned int rowid;
+  int nparams;
+  int iret;
+
+
   int count;
-  int total;
   int off;
   int limit_off;
-  postgres3_stmt *stmt;
-  char scratch[256];
+  PGresult * ret;
   GNUNET_DatastoreValue *datum;
-  postgres3 *dbh;
   GNUNET_HashCode rkey;
-  unsigned long long last_rowid;
-  unsigned long long rowid;
-  int sqoff;
 
   if (key == NULL)
     return iterateLowPriority (type, iter, closure);
   GNUNET_mutex_lock (lock);
-
-  GNUNET_snprintf (scratch, 256,
-                   "SELECT count(*) FROM gn080 WHERE hash=:1%s%s",
-                   vhash == NULL ? "" : " AND vhash=:2",
-                   type == 0 ? "" : (vhash ==
-                                     NULL) ? " AND type=:2" : " AND type=:3");
-  if (sq_prepare (dbh, scratch, &stmt) != POSTGRES_OK)
-    {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres_prepare");
-      GNUNET_mutex_unlock (lock);
-      return GNUNET_SYSERR;
+  paramValues[0] = (const char*) key;
+  paramLengths[0] = sizeof(GNUNET_HashCode);
+  if (type != 0)
+    {      
+      if (vhash != NULL)
+       {
+         paramValues[1] = (const char*) vhash;
+         paramLengths[1] = sizeof(GNUNET_HashCode);
+         paramValues[2] = (const char*) &type;
+         paramLengths[2] = sizeof(unsigned int);
+         ret = PQexecParams(dbh,
+                            "SELECT count(*) FROM gn080 WHERE hash=$1 AND 
vhash=$2 AND type=$3",
+                            3,
+                            NULL,
+                            paramValues,
+                            paramLengths,
+                            paramFormats,
+                            1);
+       }
+      else
+       {
+         paramValues[1] = (const char*) &type;
+         paramLengths[1] = sizeof(unsigned int);
+         ret = PQexecParams(dbh,
+                            "SELECT count(*) FROM gn080 WHERE hash=$1 AND 
type=$2",
+                            2,
+                            NULL,
+                            paramValues,
+                            paramLengths,
+                            paramFormats,
+                            1);
+       }
     }
-  sqoff = 1;
-  ret = postgres3_bind_blob (stmt,
-                           sqoff++,
-                           key, sizeof (GNUNET_HashCode), POSTGRES_TRANSIENT);
-  if ((vhash != NULL) && (ret == POSTGRES_OK))
-    ret = postgres3_bind_blob (stmt,
-                             sqoff++,
-                             vhash,
-                             sizeof (GNUNET_HashCode), POSTGRES_TRANSIENT);
-  if ((type != 0) && (ret == POSTGRES_OK))
-    ret = postgres3_bind_int (stmt, sqoff++, type);
-  if (ret != POSTGRES_OK)
+  else
     {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres_bind");
-      postgres3_reset (stmt);
-      postgres3_finalize (stmt);
+       if (vhash != NULL)
+       {
+         paramValues[1] = (const char*) vhash;
+         paramLengths[1] = sizeof(GNUNET_HashCode);
+         const int paramLengths[] = { sizeof(GNUNET_HashCode), 
sizeof(GNUNET_HashCode) };
+         const int paramFormats[] = { 1, 1 };
+         ret = PQexecParams(dbh,
+                            "SELECT count(*) FROM gn080 WHERE hash=$1 AND 
vhash=$2",
+                            2,
+                            NULL,
+                            paramValues,
+                            paramLengths,
+                            paramFormats,
+                            1);
+       }
+      else
+       {
+         ret = PQexecParams(dbh,
+                            "SELECT count(*) FROM gn080 WHERE hash=$1",
+                            1,
+                            NULL,
+                            paramValues,
+                            paramLengths,
+                            paramFormats,
+                            1);
+       }   
+    }
+   if (GNUNET_OK != check_result (ret, PGRES_TUPLES_OK, "PQexecParams"))
+    {      
       GNUNET_mutex_unlock (lock);
       return GNUNET_SYSERR;
-
     }
-  ret = postgres3_step (stmt);
-  if (ret != POSTGRES_ROW)
+  if ( (PQntuples (ret) != 1) ||
+       (PQnfields (ret) != 1) ||
+       (PQgetlength (ret, 0, 0) != sizeof(unsigned int) ) )
     {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres_step");
-      postgres3_reset (stmt);
-      postgres3_finalize (stmt);
+      GNUNET_GE_BREAK (NULL, 0);
+      PQclear(ret);
       GNUNET_mutex_unlock (lock);
       return GNUNET_SYSERR;
-
     }
-  total = postgres3_column_int (stmt, 0);
-  postgres3_reset (stmt);
-  postgres3_finalize (stmt);
-  if ((iter == NULL) || (total == 0))
+  total = *(const unsigned int*) PQgetvalue(ret, 0, 0);
+  PQclear(ret);
+  if ( (iter == NULL) || (total == 0) )
     {
       GNUNET_mutex_unlock (lock);
       return total;
     }
-
-  GNUNET_snprintf (scratch, 256,
-                   "SELECT size, type, prio, anonLevel, expire, hash, value, 
_ROWID_ "
-                   "FROM gn080 WHERE hash=:1%s%s AND _ROWID_ >= :%d "
-                   "ORDER BY _ROWID_ ASC LIMIT 1 OFFSET :d",
-                   vhash == NULL ? "" : " AND vhash=:2",
-                   type == 0 ? "" : (vhash ==
-                                     NULL) ? " AND type=:2" : " AND type=:3",
-                   sqoff, sqoff + 1);
-  if (sq_prepare (dbh, scratch, &stmt) != POSTGRES_OK)
+  if (type != 0)
     {
-      LOG_POSTGRES (handle,
-                  GNUNET_GE_ERROR | GNUNET_GE_ADMIN | GNUNET_GE_USER |
-                  GNUNET_GE_BULK, "postgres_prepare");
+      if (vhash != NULL)
+       {
+         paramValues[3] = (const char*) &last_rowid;
+         paramLengths[3] = sizeof(last_rowid);
+         paramValues[4] = (const char*) &limit_off;
+         paramLengths[4] = sizeof(limit_off);
+         nparams = 5;
+         ret = PQprepare(dbh,
+                          "",
+                          "SELECT size, type, prio, anonLevel, expire, hash, 
value, oid FROM gn080 "
+                          "WHERE hash=$1 AND vhash=$2 AND type=$3 "
+                          "AND oid >= $4 ORDER BY oid ASC LIMIT 1 OFFSET $5",
+                          5,
+                          NULL);
+       }
+      else
+       {
+         paramValues[2] = (const char*) &last_rowid;
+         paramLengths[2] = sizeof(last_rowid);
+         paramValues[3] = (const char*) &limit_off;
+         paramLengths[3] = sizeof(limit_off);
+         nparams = 4;
+         ret = PQprepare(dbh,
+                         "",
+                         "SELECT size, type, prio, anonLevel, expire, hash, 
value, oid FROM gn080 "
+                         "WHERE hash=$1 AND type=$2"
+                         "AND oid >= $3 ORDER BY oid ASC LIMIT 1 OFFSET $4",
+                         4,
+                         NULL);
+       }
+    }
+  else
+    {
+       if (vhash != NULL)
+       {
+         paramValues[2] = (const char*) &last_rowid;
+         paramLengths[2] = sizeof(last_rowid);
+         paramValues[3] = (const char*) &limit_off;
+         paramLengths[3] = sizeof(limit_off);
+         nparams = 4;
+         ret = PQprepare(dbh,
+                         "",
+                         "SELECT size, type, prio, anonLevel, expire, hash, 
value, oid FROM gn080 "
+                         "WHERE hash=$1 AND vhash=$2"
+                         "AND oid >= $3 ORDER BY oid ASC LIMIT 1 OFFSET $4",
+                         4,
+                         NULL);
+       }
+      else
+       {
+         paramValues[1] = (const char*) &last_rowid;
+         paramLengths[1] = sizeof(last_rowid);
+         paramValues[2] = (const char*) &limit_off;
+         paramLengths[2] = sizeof(limit_off);
+         nparams = 3;
+         ret = PQprepare(dbh,
+                         "",
+                         "SELECT size, type, prio, anonLevel, expire, hash, 
value, oid FROM gn080 "
+                         "WHERE hash=$1"
+                         "AND oid >= $2 ORDER BY oid ASC LIMIT 1 OFFSET $3",
+                         3,
+                         NULL);
+       }   
+    }
+  if (GNUNET_OK != check_result (ret, PGRES_COMMAND_OK, "PQprepare"))
+    {      
       GNUNET_mutex_unlock (lock);
       return GNUNET_SYSERR;
     }
+  PQclear(ret);
+
   count = 0;
   last_rowid = 0;
   off = GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, total);
@@ -1056,63 +873,51 @@
         limit_off = off;
       else
         limit_off = 0;
-      sqoff = 1;
-      ret = postgres3_bind_blob (stmt,
-                               sqoff++,
-                               key, sizeof (GNUNET_HashCode),
-                               POSTGRES_TRANSIENT);
-      if ((vhash != NULL) && (ret == POSTGRES_OK))
-        ret = postgres3_bind_blob (stmt,
-                                 sqoff++,
-                                 vhash,
-                                 sizeof (GNUNET_HashCode), POSTGRES_TRANSIENT);
-      if ((type != 0) && (ret == POSTGRES_OK))
-        ret = postgres3_bind_int (stmt, sqoff++, type);
-      if (ret == POSTGRES_OK)
-        ret = postgres3_bind_int64 (stmt, sqoff++, last_rowid);
-      if (ret == POSTGRES_OK)
-        ret = postgres3_bind_int (stmt, sqoff++, limit_off);
-      if (ret == POSTGRES_OK)
-        {
-          ret = postgres3_step (stmt);
-          if (ret != POSTGRES_ROW)
-            break;
-          datum = assembleDatum (handle, stmt, &rkey, &rowid);
-          last_rowid = rowid + 1;
-          postgres3_reset (stmt);
-          if (datum == NULL)
-            continue;
-          if ((key != NULL) &&
-              (0 != memcmp (&rkey, key, sizeof (GNUNET_HashCode))))
-            {
-              GNUNET_GE_BREAK (NULL, 0);
-              GNUNET_free (datum);
-              continue;
-            }
-          GNUNET_mutex_unlock (lock);
-          count++;
-          ret = iter (&rkey, datum, closure, rowid);
-          GNUNET_mutex_lock (lock);
-          if (ret == GNUNET_SYSERR)
-            {
-              GNUNET_free (datum);
-              ret = POSTGRES_DONE;
-              break;
-            }
-          if (ret == GNUNET_NO)
-            {
-              payload -= getContentDatastoreSize (datum);
-              delete_by_rowid (handle, rowid);
-            }
-          GNUNET_free (datum);
-        }
+
+      ret = PQexecPrepared (dbh,
+                           "",
+                           nparams,
+                           paramValues,
+                           paramLengths,
+                           paramFormats,
+                           1);
+      if (GNUNET_OK != check_result (ret, PGRES_TUPLES_OK, "PQexecPrepared"))
+       {      
+         GNUNET_mutex_unlock (lock);
+         return GNUNET_SYSERR;
+       }
+      datum = assembleDatum (ret, &rkey, &rowid);
+      last_rowid = rowid + 1;
+      PQclear (ret);
+      if (datum == NULL)
+       continue;
+      if ( (key != NULL) &&
+          (0 != memcmp (&rkey, key, sizeof (GNUNET_HashCode))))
+       {
+         GNUNET_GE_BREAK (NULL, 0);
+         GNUNET_free (datum);
+         continue;
+       }
+      GNUNET_mutex_unlock (lock);
+      count++;
+      iret = iter (&rkey, datum, closure, rowid);
+      GNUNET_mutex_lock (lock);
+      if (iret == GNUNET_SYSERR)
+       {
+         GNUNET_free (datum);
+         break;
+       }
+      if (iret == GNUNET_NO)
+       {
+         payload -= getContentDatastoreSize (datum);
+         delete_by_rowid (rowid);
+       }
+      GNUNET_free (datum);    
       if (count + off == total)
         last_rowid = 0;         /* back to start */
       if (count == total)
         break;
     }
-  postgres3_reset (stmt);
-  postgres3_finalize (stmt);
   GNUNET_mutex_unlock (lock);
   return count;
 }
@@ -1126,6 +931,7 @@
 static int
 put (const GNUNET_HashCode * key, const GNUNET_DatastoreValue * value)
 {
+#if 0
   int n;
   postgres3_stmt *stmt;
   unsigned int contentSize;
@@ -1216,6 +1022,8 @@
 #endif
   GNUNET_mutex_unlock (lock);
   return GNUNET_OK;
+#endif
+  return GNUNET_SYSERR;
 }
 
 /**
@@ -1225,6 +1033,7 @@
 static int
 update (unsigned long long uid, int delta, GNUNET_CronTime expire)
 {
+#if 0
   int n;
 
   GNUNET_mutex_lock (lock);
@@ -1247,9 +1056,10 @@
   if (n == POSTGRES_BUSY)
     return GNUNET_NO;
   return n == POSTGRES_OK ? GNUNET_OK : GNUNET_SYSERR;
+#endif
+  return GNUNET_SYSERR;
 }
 
-///////////////////////////////////////
 
 
 static void
@@ -1368,8 +1178,6 @@
   if (dbh == NULL)
     {
       GNUNET_mutex_destroy (lock);
-      GNUNET_free (fn);
-      fn = NULL;
       return;
     }
   create_indices ();





reply via email to

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