- added check against statistics
[oweals/gnunet.git] / src / datastore / plugin_datastore_postgres.c
index b9c27c7b1a5bc861bbf19eca68a6a078e553c65d..6dec3146d4df10118dd65870642460428cb046cd 100644 (file)
@@ -29,7 +29,6 @@
 #include "gnunet_postgres_lib.h"
 #include <postgresql/libpq-fe.h>
 
-#define DEBUG_POSTGRES GNUNET_EXTRA_LOGGING
 
 /**
  * After how many ms "busy" should a DB operation fail for good?
@@ -237,13 +236,18 @@ postgres_plugin_estimate_size (void *cls)
   {
     return 0;
   }
-  if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) ||
-      (PQgetlength (ret, 0, 0) != sizeof (unsigned long long)))
+  if ((PQntuples (ret) != 1) || (PQnfields (ret) != 1) )
   {
     GNUNET_break (0);
     PQclear (ret);
     return 0;
   }
+  if (PQgetlength (ret, 0, 0) != sizeof (unsigned long long))
+  {
+    GNUNET_break (0 == PQgetlength (ret, 0, 0));
+    PQclear (ret);
+    return 0;
+  }
   total = GNUNET_ntohll (*(const unsigned long long *) PQgetvalue (ret, 0, 0));
   PQclear (ret);
   return total;
@@ -253,7 +257,7 @@ postgres_plugin_estimate_size (void *cls)
 /**
  * Store an item in the datastore.
  *
- * @param cls closure
+ * @param cls closure with the 'struct Plugin' 
  * @param key key for the item
  * @param size number of bytes in data
  * @param data content stored
@@ -312,10 +316,8 @@ postgres_plugin_put (void *cls, const GNUNET_HashCode * key, uint32_t size,
     return GNUNET_SYSERR;
   PQclear (ret);
   plugin->env->duc (plugin->env->cls, size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
-#if DEBUG_POSTGRES
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                    "Stored %u bytes in database\n", (unsigned int) size);
-#endif
   return GNUNET_OK;
 }
 
@@ -349,10 +351,8 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
       GNUNET_POSTGRES_check_result_ (plugin->dbh, res, PGRES_TUPLES_OK, "PQexecPrepared", "select",
                                     filename, line))
   {
-#if DEBUG_POSTGRES
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                      "Ending iteration (postgres error)\n");
-#endif
     proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
     return;
   }
@@ -360,10 +360,8 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
   if (0 == PQntuples (res))
   {
     /* no result */
-#if DEBUG_POSTGRES
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                      "Ending iteration (no more results)\n");
-#endif
     proc (proc_cls, NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
     PQclear (res);
     return;
@@ -398,11 +396,9 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
       GNUNET_ntohll (*(uint64_t *) PQgetvalue (res, 0, 3));
   memcpy (&key, PQgetvalue (res, 0, 4), sizeof (GNUNET_HashCode));
   size = PQgetlength (res, 0, 5);
-#if DEBUG_POSTGRES
   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                    "Found result of size %u bytes and type %u in database\n",
                    (unsigned int) size, (unsigned int) type);
-#endif
   iret =
       proc (proc_cls, &key, size, PQgetvalue (res, 0, 5),
             (enum GNUNET_BLOCK_Type) type, priority, anonymity, expiration_time,
@@ -410,23 +406,17 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
   PQclear (res);
   if (iret == GNUNET_NO)
   {
-#if DEBUG_POSTGRES
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Processor asked for item %u to be removed.\n", rowid);
-#endif
     if (GNUNET_OK == GNUNET_POSTGRES_delete_by_rowid (plugin->dbh, "delrow", rowid))
     {
-#if DEBUG_POSTGRES
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                        "Deleting %u bytes from database\n",
                        (unsigned int) size);
-#endif
       plugin->env->duc (plugin->env->cls,
                         -(size + GNUNET_DATASTORE_ENTRY_OVERHEAD));
-#if DEBUG_POSTGRES
       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "datastore-postgres",
                        "Deleted %u bytes from database\n", (unsigned int) size);
-#endif
     }
   }
 }
@@ -436,7 +426,7 @@ process_result (struct Plugin *plugin, PluginDatumProcessor proc,
  * Iterate over the results for a particular key
  * in the datastore.
  *
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
  * @param offset offset of the result (modulo num-results);
  *        specific ordering does not matter for the offset
  * @param key maybe NULL (to match all entries)
@@ -622,7 +612,7 @@ struct ReplCtx
  * Decrements the replication counter and calls the original
  * iterator.
  *
- * @param cls closure
+ * @param cls closure with the 'struct ReplCtx*'
  * @param key key for the content
  * @param size number of bytes in data
  * @param data content stored
@@ -681,7 +671,7 @@ repl_proc (void *cls, const GNUNET_HashCode * key, uint32_t size,
  * replication counter is decremented by one IF it was positive before.
  * Call 'proc' with all values ZERO or NULL if the datastore is empty.
  *
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
  * @param proc function to call the value (once only).
  * @param proc_cls closure for proc
  */
@@ -707,7 +697,7 @@ postgres_plugin_get_replication (void *cls, PluginDatumProcessor proc,
  * Get a random item for expiration.
  * Call 'proc' with all values ZERO or NULL if the datastore is empty.
  *
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
  * @param proc function to call the value (once only).
  * @param proc_cls closure for proc
  */
@@ -790,7 +780,7 @@ postgres_plugin_update (void *cls, uint64_t uid, int delta,
 /**
  * Get all of the keys in the datastore.
  *
- * @param cls closure
+ * @param cls closure with the 'struct Plugin'
  * @param proc function to call on each key
  * @param proc_cls closure for proc
  */
@@ -822,13 +812,16 @@ postgres_plugin_get_keys (void *cls,
 
 /**
  * Drop database.
+ *
+ * @param cls closure with the 'struct Plugin'
  */
 static void
 postgres_plugin_drop (void *cls)
 {
   struct Plugin *plugin = cls;
-
-  GNUNET_POSTGRES_exec (plugin->dbh, "DROP TABLE gn090");
+  
+  if (GNUNET_OK != GNUNET_POSTGRES_exec (plugin->dbh, "DROP TABLE gn090"))
+    GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "postgres", _("Failed to drop table from database.\n"));
 }