fixing issue pointed out by amatus
[oweals/gnunet.git] / src / datacache / datacache.c
index 421acac0ff1bc9093f6c903cc259658d40403ea3..d2aa2616a826f3ad0645dd67d4248387e1d8ab99 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
 */
 
 /**
- * @file datacache/datacache_api.c
+ * @file datacache/datacache.c
  * @brief datacache API implementation
  * @author Christian Grothoff
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_datacache_lib.h"
+#include "gnunet_statistics_service.h"
 #include "plugin_datacache.h"
 
 /**
 struct GNUNET_DATACACHE_Handle
 {
 
-  /**
-   * Our datastore plugin (NULL if not available).
-   */
-  struct DatastorePlugin *plugin;
-  
   /**
    * Bloomfilter to quickly tell if we don't have the content.
    */
@@ -49,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.
    */
@@ -99,12 +100,16 @@ 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);
 }
 
 
@@ -121,7 +126,6 @@ GNUNET_DATACACHE_create (struct GNUNET_SCHEDULER_Handle *sched,
                         const struct GNUNET_CONFIGURATION_Handle *cfg,
                         const char *section)
 {
-  int fd;
   unsigned int bf_size;
   unsigned long long quota;
   struct GNUNET_DATACACHE_Handle *ret;
@@ -152,19 +156,20 @@ GNUNET_DATACACHE_create (struct GNUNET_SCHEDULER_Handle *sched,
   bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
 
   ret = GNUNET_malloc (sizeof(struct GNUNET_DATACACHE_Handle));
-  ret->bloom_name = GNUNET_strdup ("/tmp/datacachebloomXXXXXX");
-  fd = mkstemp (ret->bloom_name);
-  if (fd != -1)
+  ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
+  if (NULL != ret->bloom_name)
     {
       ret->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, 
                                                       quota / 1024,    /* 8 bit per entry in DB, expect 1k entries */
                                                       5);
-      CLOSE (fd);
     }
   else
     {
       ret->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, bf_size, 5);  /* approx. 3% false positives at max use */  
     }
+  ret->stats = GNUNET_STATISTICS_create (sched,
+                                        "datacache",
+                                        cfg);
   ret->section = GNUNET_strdup (section);
   ret->env.sched = sched;
   ret->env.cfg = cfg;
@@ -206,9 +211,14 @@ void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
   GNUNET_free (h->section);
   if (h->bloom_name != NULL)
     {
-      UNLINK (h->bloom_name);
+      if (0 != UNLINK (h->bloom_name))
+       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                                 "unlink",
+                                 h->bloom_name);
       GNUNET_free (h->bloom_name);
     }
+  GNUNET_STATISTICS_destroy (h->stats,
+                            GNUNET_NO);
   GNUNET_free (h);
 }
 
@@ -227,9 +237,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;
@@ -241,7 +251,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));
@@ -264,13 +281,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,