fix
[oweals/gnunet.git] / src / datacache / plugin_datacache_postgres.c
index 6bf3c70111914016cff448aa812a28b2f9674ae7..e391469fd12e59d1b1f1ff7c44fa7adfb5746348 100644 (file)
@@ -25,7 +25,7 @@
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
-#include "plugin_datacache.h"
+#include "gnunet_datacache_plugin.h"
 #include <postgresql/libpq-fe.h>
 
 #define DEBUG_POSTGRES GNUNET_NO
@@ -134,11 +134,12 @@ init_connection (struct Plugin *plugin)
   PGresult *ret;
 
   /* Open database and precompile statements */
-  conninfo = NULL;
-  GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
-                                        "datacache-postgres",
-                                        "CONFIG",
-                                        &conninfo);
+  if (GNUNET_OK != 
+      GNUNET_CONFIGURATION_get_value_string (plugin->env->cfg,
+                                            "datacache-postgres",
+                                            "CONFIG",
+                                            &conninfo))
+    conninfo = NULL;
   plugin->dbh = PQconnectdb (conninfo == NULL ? "" : conninfo);
   GNUNET_free_non_null (conninfo);
   if (NULL == plugin->dbh)
@@ -169,8 +170,8 @@ init_connection (struct Plugin *plugin)
                       (ret,
                        PG_DIAG_SQLSTATE)))))
     {
-      check_result (plugin,
-                   ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090dc", __LINE__);
+      (void) check_result (plugin,
+                          ret, PGRES_COMMAND_OK, "CREATE TABLE", "gn090dc", __LINE__);
       PQfinish (plugin->dbh);
       plugin->dbh = NULL;
       return GNUNET_SYSERR;
@@ -267,10 +268,11 @@ init_connection (struct Plugin *plugin)
  */
 static int
 delete_by_rowid (struct Plugin *plugin,
-                unsigned int rowid)
+                uint32_t rowid)
 {
-  const char *paramValues[] = { (const char *) &rowid };
-  int paramLengths[] = { sizeof (rowid) };
+  uint32_t brow = htonl (rowid);
+  const char *paramValues[] = { (const char *) &brow };
+  int paramLengths[] = { sizeof (brow) };
   const int paramFormats[] = { 1 };
   PGresult *ret;
 
@@ -300,10 +302,10 @@ delete_by_rowid (struct Plugin *plugin,
  * @param discard_time when to discard the value in any case
  * @return 0 on error, number of bytes used otherwise
  */
-static uint32_t 
+static size_t 
 postgres_plugin_put (void *cls,
                     const GNUNET_HashCode * key,
-                    uint32_t size,
+                    size_t size,
                     const char *data,
                     enum GNUNET_BLOCK_Type type,
                     struct GNUNET_TIME_Absolute discard_time)
@@ -311,7 +313,7 @@ postgres_plugin_put (void *cls,
   struct Plugin *plugin = cls;
   PGresult *ret;
   uint32_t btype = htonl (type);
-  uint64_t bexpi = GNUNET_TIME_absolute_hton (discard_time).value__;
+  uint64_t bexpi = GNUNET_TIME_absolute_hton (discard_time).abs_value__;
   const char *paramValues[] = {
     (const char *) &btype,
     (const char *) &bexpi,
@@ -406,6 +408,11 @@ postgres_plugin_get (void *cls,
       PQclear (res);
       return 0; 
     }
+  if (iter == NULL)
+    {
+      PQclear (res);
+      return cnt;
+    }
   if ( (3 != PQnfields (res)) ||
        (sizeof (uint64_t) != PQfsize (res, 0)) ||
        (sizeof (uint32_t) != PQfsize (res, 1)))
@@ -416,7 +423,7 @@ postgres_plugin_get (void *cls,
     }
   for (i=0;i<cnt;i++)
     {
-      expiration_time.value = GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, i, 0));
+      expiration_time.abs_value = GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, i, 0));
       type = ntohl (*(uint32_t *) PQgetvalue (res, i, 1));
       size = PQgetlength (res, i, 2);
 #if DEBUG_POSTGRES
@@ -494,9 +501,9 @@ postgres_plugin_del (void *cls)
       return GNUNET_SYSERR; 
     }
   if ( (3 != PQnfields (res)) ||
-       (sizeof (uint32_t) != PQfsize (res, 0)) ||
-       (sizeof (uint32_t) != PQfsize (res, 1)) ||
-       (sizeof (GNUNET_HashCode) != PQfsize (res, 2)) )
+       (sizeof (size) != PQfsize (res, 0)) ||
+       (sizeof (oid) != PQfsize (res, 1)) ||
+       (sizeof (GNUNET_HashCode) != PQgetlength (res, 0, 2)) )
     {
       GNUNET_break (0);
       PQclear (res);
@@ -512,7 +519,7 @@ postgres_plugin_del (void *cls)
     return GNUNET_SYSERR;
   plugin->env->delete_notify (plugin->env->cls,
                              &key,
-                             size);  
+                             size + OVERHEAD);  
   return GNUNET_OK;
 }
 
@@ -564,8 +571,6 @@ libgnunet_plugin_datacache_postgres_done (void *cls)
   struct GNUNET_DATACACHE_PluginFunctions *api = cls;
   struct Plugin *plugin = api->cls;
 
-  fprintf (stderr,
-          "Unloading postgres plugin\n");
   PQfinish (plugin->dbh);
   GNUNET_free (plugin);
   GNUNET_free (api);