rescheduling sessions for udp
[oweals/gnunet.git] / src / datacache / datacache.c
index f79fae95bb7cf9c6fa8264c55c94b65d12a0dc23..c96432946d12709f8ea31b58a4f90a54147d819b 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -29,7 +29,6 @@
 #include "gnunet_statistics_service.h"
 #include "gnunet_datacache_plugin.h"
 
-#define DEBUG_DATACACHE GNUNET_EXTRA_LOGGING
 
 #define LOG(kind,...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
 
@@ -104,19 +103,18 @@ struct GNUNET_DATACACHE_Handle
  * @param size number of bytes that were made available
  */
 static void
-env_delete_notify (void *cls, const GNUNET_HashCode * key, size_t size)
+env_delete_notify (void *cls, const struct GNUNET_HashCode * key, size_t size)
 {
   struct GNUNET_DATACACHE_Handle *h = cls;
 
-#if DEBUG_DATACACHE
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Content under key `%s' discarded\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Content under key `%s' discarded\n",
        GNUNET_h2s (key));
-#endif
   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_STATISTICS_update (h->stats, gettext_noop ("# bytes stored"), - (long long) size,
+                            GNUNET_NO);
+  GNUNET_STATISTICS_update (h->stats, gettext_noop ("# items stored"), -1,
                             GNUNET_NO);
 }
 
@@ -139,7 +137,7 @@ GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
   char *name;
 
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_number (cfg, section, "QUOTA", &quota))
+      GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", &quota))
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
          _("No `%s' specified for `%s' in configuration!\n"), "QUOTA", section);
@@ -156,15 +154,24 @@ GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
   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
+
+  if (GNUNET_YES !=
+      GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF"))
   {
-    ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
+    if (GNUNET_YES !=
+       GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC"))
+    {
+      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);
+    }
+    if (NULL == ret->filter)
+    {
+       ret->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, bf_size, 5); /* approx. 3% false positives at max use */
+    }
   }
   ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
   ret->section = GNUNET_strdup (section);
@@ -198,7 +205,7 @@ GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
 void
 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
 {
-  if (h->filter != NULL)
+  if (NULL != h->filter)
     GNUNET_CONTAINER_bloomfilter_free (h->filter);
   if (h->api != NULL)
     GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
@@ -226,31 +233,42 @@ GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
  * @param data data to store
  * @param type type of the value
  * @param discard_time when to discard the value in any case
- * @return GNUNET_OK on success, GNUNET_SYSERR on error (full, etc.)
+ * @param path_info_len number of entries in @a path_info
+ * @param path_info a path through the network
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
  */
 int
 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
-                      const GNUNET_HashCode * key, size_t size,
+                      const struct GNUNET_HashCode * key, size_t size,
                       const char *data, enum GNUNET_BLOCK_Type type,
-                      struct GNUNET_TIME_Absolute discard_time)
+                      struct GNUNET_TIME_Absolute discard_time,
+                     unsigned int path_info_len,
+                     const struct GNUNET_PeerIdentity *path_info)
 {
-  uint32_t used;
+  ssize_t used;
 
-
-  used = h->api->put (h->api->cls, key, size, data, type, discard_time);
-  if (used == 0)
+  used = h->api->put (h->api->cls, key,
+                     size, data,
+                     type, discard_time,
+                     path_info_len, path_info);
+  if (-1 == used)
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
-#if DEBUG_DATACACHE
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Stored data under key `%s' in cache\n",
+  if (0 == used)
+  {
+    /* duplicate */
+    return GNUNET_NO;
+  }
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Stored data under key `%s' in cache\n",
        GNUNET_h2s (key));
-#endif
   GNUNET_STATISTICS_update (h->stats, gettext_noop ("# bytes stored"), size,
                             GNUNET_NO);
-  GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
+  GNUNET_STATISTICS_update (h->stats, gettext_noop ("# items stored"), 1,
+                            GNUNET_NO);
+  if (NULL != h->filter)
+    GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
   while (h->utilization + used > h->env.quota)
     GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
   h->utilization += used;
@@ -271,27 +289,22 @@ GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
  */
 unsigned int
 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
-                      const GNUNET_HashCode * key, enum GNUNET_BLOCK_Type type,
+                      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 DEBUG_DATACACHE
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
-       "Processing request for key `%s'\n",
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for key `%s'\n",
        GNUNET_h2s (key));
-#endif
-  if (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key))
+  if ( (NULL != h->filter) &&
+       (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)) )
   {
     GNUNET_STATISTICS_update (h->stats,
                               gettext_noop
                               ("# requests filtered by bloom filter"), 1,
                               GNUNET_NO);
-#if DEBUG_DATACACHE
-    LOG (GNUNET_ERROR_TYPE_DEBUG,
-        "Bloomfilter filters request for key `%s'\n",
-        GNUNET_h2s (key));
-#endif
+    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);