fix
[oweals/gnunet.git] / src / datacache / datacache.c
index c8530055dc1dc9c3a324e69a5f1d4d6a2cd51fdf..10b2721ee53287d79bb1c7ca2583853b66b57f38 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
+     (C) 2004, 2005, 2006, 2007, 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
@@ -26,7 +26,8 @@
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_datacache_lib.h"
-#include "plugin_datacache.h"
+#include "gnunet_statistics_service.h"
+#include "gnunet_datacache_plugin.h"
 
 /**
  * Internal state of the datacache library.
@@ -44,6 +45,11 @@ struct GNUNET_DATACACHE_Handle
    */
   const struct GNUNET_CONFIGURATION_Handle *cfg;
 
+  /**
+   * Opaque handle for the statistics service.
+   */
+  struct GNUNET_STATISTICS_Handle *stats;
+
   /**
    * Configuration section to use.
    */
@@ -94,26 +100,28 @@ struct GNUNET_DATACACHE_Handle
 static void 
 env_delete_notify (void *cls,
                   const GNUNET_HashCode *key,
-                  uint32_t size)
+                  size_t size)
 {
   struct GNUNET_DATACACHE_Handle * h = cls;
   GNUNET_assert (h->utilization >= size);
   h->utilization -= size;
   GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
+  GNUNET_STATISTICS_update (h->stats,
+                           gettext_noop ("# bytes stored"),
+                           -size,
+                           GNUNET_NO);
 }
 
 
 /**
  * Create a data cache.
  *
- * @param sched scheduler to use
  * @param cfg configuration to use
  * @param section section in the configuration that contains our options
  * @return handle to use to access the service
  */
 struct GNUNET_DATACACHE_Handle *
-GNUNET_DATACACHE_create (struct GNUNET_SCHEDULER_Handle *sched,
-                        const struct GNUNET_CONFIGURATION_Handle *cfg,
+GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
                         const char *section)
 {
   unsigned int bf_size;
@@ -157,8 +165,9 @@ GNUNET_DATACACHE_create (struct GNUNET_SCHEDULER_Handle *sched,
     {
       ret->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5);  /* approx. 3% false positives at max use */  
     }
+  ret->stats = GNUNET_STATISTICS_create ("datacache",
+                                        cfg);
   ret->section = GNUNET_strdup (section);
-  ret->env.sched = sched;
   ret->env.cfg = cfg;
   ret->env.delete_notify = &env_delete_notify;  
   ret->env.section = ret->section;
@@ -204,6 +213,8 @@ void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
                                  h->bloom_name);
       GNUNET_free (h->bloom_name);
     }
+  GNUNET_STATISTICS_destroy (h->stats,
+                            GNUNET_NO);
   GNUNET_free (h);
 }
 
@@ -222,9 +233,9 @@ void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
 int 
 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
                      const GNUNET_HashCode * key,
-                     uint32_t size,
+                     size_t size,
                      const char *data,
-                     unsigned int type,
+                     enum GNUNET_BLOCK_Type type,
                      struct GNUNET_TIME_Absolute discard_time)
 {
   uint32_t used;
@@ -236,7 +247,14 @@ GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
                      type,
                      discard_time);
   if (used == 0)
-    return GNUNET_SYSERR;
+    {
+      GNUNET_break (0);
+      return GNUNET_SYSERR;
+    }
+  GNUNET_STATISTICS_update (h->stats,
+                           gettext_noop ("# bytes stored"),
+                           size,
+                           GNUNET_NO);
   GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
   while (h->utilization + used > h->env.quota)
     GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
@@ -259,13 +277,23 @@ GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
 unsigned int 
 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
                      const GNUNET_HashCode * key,
-                     unsigned int type, 
+                     enum GNUNET_BLOCK_Type type, 
                      GNUNET_DATACACHE_Iterator iter,
                      void *iter_cls)
 {
+  GNUNET_STATISTICS_update (h->stats,
+                           gettext_noop ("# requests received"),
+                           1,
+                           GNUNET_NO);
   if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter,
                                                      key))
-    return 0; /* can not be present */
+    {
+      GNUNET_STATISTICS_update (h->stats,
+                               gettext_noop ("# requests filtered by bloom filter"),
+                               1,
+                               GNUNET_NO);
+      return 0; /* can not be present */
+    } 
   return h->api->get (h->api->cls,
                      key,
                      type,