-consistently use struct GNUNET_HashCode
[oweals/gnunet.git] / src / datacache / datacache.c
index 0ad9b9df4071a9eaed85f03028b28af1813d9743..9087ae0dd21b0352cf8b563046dd8d9316a24569 100644 (file)
 #include "gnunet_util_lib.h"
 #include "gnunet_datacache_lib.h"
 #include "gnunet_statistics_service.h"
-#include "plugin_datacache.h"
+#include "gnunet_datacache_plugin.h"
+
+
+#define LOG(kind,...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
+
+#define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
 
 /**
  * Internal state of the datacache library.
@@ -70,7 +75,7 @@ struct GNUNET_DATACACHE_Handle
    * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
    */
   char *lib_name;
-  
+
   /**
    * Name for the bloom filter file.
    */
@@ -92,39 +97,36 @@ struct GNUNET_DATACACHE_Handle
 /**
  * Function called by plugins to notify the datacache
  * about content deletions.
- * 
+ *
  * @param cls closure
  * @param key key of the content that was deleted
  * @param size number of bytes that were made available
  */
-static void 
-env_delete_notify (void *cls,
-                  const GNUNET_HashCode *key,
-                  uint32_t size)
+static void
+env_delete_notify (void *cls, const struct GNUNET_HashCode * key, size_t size)
 {
-  struct GNUNET_DATACACHE_Handle * h = cls;
+  struct GNUNET_DATACACHE_Handle *h = cls;
+
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Content under key `%s' discarded\n",
+       GNUNET_h2s (key));
   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);
+  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,
-                        const char *section)
+GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                         const char *section)
 {
   unsigned int bf_size;
   unsigned long long quota;
@@ -133,64 +135,60 @@ GNUNET_DATACACHE_create (struct GNUNET_SCHEDULER_Handle *sched,
   char *name;
 
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (cfg,
-                                             section, "QUOTA", &quota))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("No `%s' specified for `%s' in configuration!\n"),
-                 "QUOTA",
-                 section);
-      return NULL;
-    }
+      GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", &quota))
+  {
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _("No `%s' specified for `%s' in configuration!\n"), "QUOTA", section);
+    return NULL;
+  }
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_string (cfg,
-                                             section,
-                                            "DATABASE", &name))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                 _("No `%s' specified for `%s' in configuration!\n"),
-                 "DATABASE",
-                 section);
-      return NULL;
-    }
-  bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
-
-  ret = GNUNET_malloc (sizeof(struct GNUNET_DATACACHE_Handle));
+      GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
+  {
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
+         section);
+    return NULL;
+  }
+  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_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);
-    }
-  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->filter = GNUNET_CONTAINER_bloomfilter_load (ret->bloom_name, quota / 1024,     /* 8 bit per entry in DB, expect 1k entries */
+                                                     5);
+  }
+  if (NULL == ret->filter)
+  {
+    ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
+  }
+  if (NULL == ret->filter)
+  {
+    GNUNET_free (name);
+    GNUNET_free (ret->bloom_name);
+    GNUNET_free (ret);
+    return NULL;
+  }
+  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.delete_notify = &env_delete_notify;
   ret->env.section = ret->section;
   ret->env.cls = ret;
   ret->env.delete_notify = &env_delete_notify;
   ret->env.quota = quota;
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              _("Loading `%s' datacache plugin\n"), name);
+  LOG (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datacache plugin\n"), name);
   GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
   ret->short_name = name;
   ret->lib_name = libname;
   ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
   if (ret->api == NULL)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  _("Failed to load datacache plugin for `%s'\n"), name);
-      GNUNET_DATACACHE_destroy (ret);
-      return NULL;
-    }
+  {
+    LOG (GNUNET_ERROR_TYPE_ERROR,
+         _("Failed to load datacache plugin for `%s'\n"), name);
+    GNUNET_DATACACHE_destroy (ret);
+    return NULL;
+  }
   return ret;
 }
 
@@ -200,7 +198,8 @@ GNUNET_DATACACHE_create (struct GNUNET_SCHEDULER_Handle *sched,
  *
  * @param h handle to the datastore
  */
-void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
+void
+GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
 {
   if (h->filter != NULL)
     GNUNET_CONTAINER_bloomfilter_free (h->filter);
@@ -210,15 +209,13 @@ void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
   GNUNET_free (h->short_name);
   GNUNET_free (h->section);
   if (h->bloom_name != NULL)
-    {
-      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);
+  {
+    if (0 != UNLINK (h->bloom_name))
+      GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING, "datacache",
+                                     "unlink", h->bloom_name);
+    GNUNET_free (h->bloom_name);
+  }
+  GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
   GNUNET_free (h);
 }
 
@@ -234,28 +231,24 @@ void GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
  * @param discard_time when to discard the value in any case
  * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.)
  */
-int 
+int
 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
-                     const GNUNET_HashCode * key,
-                     uint32_t size,
-                     const char *data,
-                     enum GNUNET_BLOCK_Type type,
-                     struct GNUNET_TIME_Absolute discard_time)
+                      const struct GNUNET_HashCode * key, size_t size,
+                      const char *data, enum GNUNET_BLOCK_Type type,
+                      struct GNUNET_TIME_Absolute discard_time)
 {
   uint32_t used;
 
-  used = h->api->put (h->api->cls,
-                     key,
-                     size,
-                     data,
-                     type,
-                     discard_time);
+  used = h->api->put (h->api->cls, key, size, data, type, discard_time);
   if (used == 0)
+  {
+    GNUNET_break (0);
     return GNUNET_SYSERR;
-  GNUNET_STATISTICS_update (h->stats,
-                           gettext_noop ("# bytes stored"),
-                           size,
-                           GNUNET_NO);
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Stored data under key `%s' in cache\n",
+       GNUNET_h2s (key));
+  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));
@@ -275,31 +268,26 @@ GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
  * @param iter_cls closure for iter
  * @return the number of results found
  */
-unsigned int 
+unsigned int
 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
-                     const GNUNET_HashCode * key,
-                     enum GNUNET_BLOCK_Type type, 
-                     GNUNET_DATACACHE_Iterator iter,
-                     void *iter_cls)
+                      const struct GNUNET_HashCode * key, 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))
-    {
-      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,
-                     iter,
-                     iter_cls);
+  GNUNET_STATISTICS_update (h->stats, gettext_noop ("# requests received"), 1,
+                            GNUNET_NO);
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for key `%s'\n",
+       GNUNET_h2s (key));
+  if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key))
+  {
+    GNUNET_STATISTICS_update (h->stats,
+                              gettext_noop
+                              ("# requests filtered by bloom filter"), 1,
+                              GNUNET_NO);
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "Bloomfilter filters request for key `%s'\n",
+         GNUNET_h2s (key));
+    return 0;                   /* can not be present */
+  }
+  return h->api->get (h->api->cls, key, type, iter, iter_cls);
 }