-indent, doxygen
[oweals/gnunet.git] / src / datacache / plugin_datacache_heap.c
index 2f55bcf5337c0b66489b3e11017ff7ee1b579169..9689d4f056605e7bf1964f2a43aa71a862543990 100644 (file)
@@ -32,6 +32,7 @@
 #define LOG_STRERROR_FILE(kind,op,fn) GNUNET_log_from_strerror_file (kind, "datacache-heap", op, fn)
 
 
+
 /**
  * Context for all functions in this plugin.
  */
@@ -75,19 +76,32 @@ struct Value
    */
   struct GNUNET_CONTAINER_HeapNode *hn;
 
+  /**
+   * Path information.
+   */
+  struct GNUNET_PeerIdentity *path_info;
+
   /**
    * Payload (actual payload follows this struct)
    */
   size_t size;
 
+  /**
+   * Number of entries in 'path_info'.
+   */
+  unsigned int path_info_len;
+
   /**
    * Type of the block.
    */
   enum GNUNET_BLOCK_Type type;
-  
+
 };
 
 
+#define OVERHEAD (sizeof (struct Value) + 64)
+
+
 /**
  * Closure for 'put_cb'.
  */
@@ -96,7 +110,7 @@ struct PutContext
   /**
    * Expiration time for the new value.
    */
-  struct GNUNET_TIME_Absolute discard_time; 
+  struct GNUNET_TIME_Absolute discard_time;
 
   /**
    * Data for the new value.
@@ -108,6 +122,11 @@ struct PutContext
    */
   struct GNUNET_CONTAINER_Heap *heap;
 
+  /**
+   * Path information.
+   */
+  const struct GNUNET_PeerIdentity *path_info;
+
   /**
    * Number of bytes in 'data'.
    */
@@ -118,6 +137,11 @@ struct PutContext
    */
   enum GNUNET_BLOCK_Type type;
 
+  /**
+   * Number of entries in 'path_info'.
+   */
+  unsigned int path_info_len;
+
   /**
    * Value to set to GNUNET_YES if an equivalent block was found.
    */
@@ -131,8 +155,8 @@ struct PutContext
  *
  * @param cls the 'struct PutContext'
  * @param key the key for the value(s)
- * @param value an existing value 
- * @return GNUNET_YES if not found (to continue to iterate)
+ * @param value an existing value
+ * @return #GNUNET_YES if not found (to continue to iterate)
  */
 static int
 put_cb (void *cls,
@@ -146,21 +170,27 @@ put_cb (void *cls,
        (val->type == put_ctx->type) &&
        (0 == memcmp (&val[1], put_ctx->data, put_ctx->size)) )
   {
-    put_ctx->found = GNUNET_YES;    
+    put_ctx->found = GNUNET_YES;
     val->discard_time = GNUNET_TIME_absolute_max (val->discard_time,
                                                  put_ctx->discard_time);
+    /* replace old path with new path */
+    GNUNET_array_grow (val->path_info,
+                      val->path_info_len,
+                      put_ctx->path_info_len);
+    memcpy (val->path_info,
+           put_ctx->path_info,
+           put_ctx->path_info_len * sizeof (struct GNUNET_PeerIdentity));
     GNUNET_CONTAINER_heap_update_cost (put_ctx->heap,
                                       val->hn,
-                                      val->discard_time.abs_value);
-    return GNUNET_NO;
-  }
-  if (val->type == put_ctx->type)
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-               "Got another value for key %s and type %d (size %u vs %u)\n",
+                                      val->discard_time.abs_value_us);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+               "Got same value for key %s and type %d (size %u vs %u)\n",
                GNUNET_h2s (key),
                val->type,
                (unsigned int) val->size,
                (unsigned int) put_ctx->size);
+    return GNUNET_NO;
+  }
   return GNUNET_YES;
 }
 
@@ -168,18 +198,22 @@ put_cb (void *cls,
 /**
  * Store an item in the datastore.
  *
- * @param cls closure (our "struct Plugin")
+ * @param cls closure (our `struct Plugin`)
  * @param key key to store data under
  * @param size number of bytes in data
  * @param data data to store
  * @param type type of the value
  * @param discard_time when to discard the value in any case
- * @return 0 on error, number of bytes used otherwise
+ * @param path_info_len number of entries in @a path_info
+ * @param path_info a path through the network
+   * @return 0 if duplicate, -1 on error, number of bytes used otherwise
  */
-static size_t
+static ssize_t
 heap_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size,
-                   const char *data, enum GNUNET_BLOCK_Type type,
-                   struct GNUNET_TIME_Absolute discard_time)
+                const char *data, enum GNUNET_BLOCK_Type type,
+                struct GNUNET_TIME_Absolute discard_time,
+                unsigned int path_info_len,
+                const struct GNUNET_PeerIdentity *path_info)
 {
   struct Plugin *plugin = cls;
   struct Value *val;
@@ -189,6 +223,8 @@ heap_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size,
   put_ctx.heap = plugin->heap;
   put_ctx.data = data;
   put_ctx.size = size;
+  put_ctx.path_info = path_info;
+  put_ctx.path_info_len = path_info_len;
   put_ctx.discard_time = discard_time;
   put_ctx.type = type;
   GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
@@ -203,14 +239,19 @@ heap_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size,
   val->type = type;
   val->discard_time = discard_time;
   val->size = size;
+  GNUNET_array_grow (val->path_info,
+                    val->path_info_len,
+                    path_info_len);
+  memcpy (val->path_info, path_info,
+         path_info_len * sizeof (struct GNUNET_PeerIdentity));
   (void) GNUNET_CONTAINER_multihashmap_put (plugin->map,
                                            &val->key,
                                            val,
                                            GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
   val->hn = GNUNET_CONTAINER_heap_insert (plugin->heap,
                                          val,
-                                         val->discard_time.abs_value);
-  return size;
+                                         val->discard_time.abs_value_us);
+  return size + OVERHEAD;
 }
 
 
@@ -248,7 +289,7 @@ struct GetContext
  *
  * @param cls the 'struct GetContext'
  * @param key the key for the value(s)
- * @param value an existing value 
+ * @param value an existing value
  * @return GNUNET_YES to continue to iterate
  */
 static int
@@ -263,13 +304,17 @@ get_cb (void *cls,
   if ( (get_ctx->type != val->type) &&
        (GNUNET_BLOCK_TYPE_ANY != get_ctx->type) )
     return GNUNET_OK;
-  ret = get_ctx->iter (get_ctx->iter_cls,
+  if (NULL != get_ctx->iter)
+    ret = get_ctx->iter (get_ctx->iter_cls,
+                        key,
+                        val->size,
+                        (const char *) &val[1],
+                        val->type,
                       val->discard_time,
-                      key,
-                      val->size,
-                      (const char *) &val[1],
-                      val->type);
-                
+                        val->path_info_len,
+                        val->path_info);
+  else
+    ret = GNUNET_YES;
   get_ctx->cnt++;
   return ret;
 }
@@ -296,7 +341,7 @@ heap_plugin_get (void *cls, const struct GNUNET_HashCode * key,
 
   get_ctx.type = type;
   get_ctx.iter = iter;
-  get_ctx.iter_cls = iter_cls;  
+  get_ctx.iter_cls = iter_cls;
   get_ctx.cnt = 0;
   GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
                                              key,
@@ -311,14 +356,14 @@ heap_plugin_get (void *cls, const struct GNUNET_HashCode * key,
  * from the datacache right now.
  *
  * @param cls closure (our "struct Plugin")
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 static int
 heap_plugin_del (void *cls)
 {
   struct Plugin *plugin = cls;
   struct Value *val;
-  
+
   val = GNUNET_CONTAINER_heap_remove_root (plugin->heap);
   if (NULL == val)
     return GNUNET_SYSERR;
@@ -328,7 +373,8 @@ heap_plugin_del (void *cls)
                                                       val));
   plugin->env->delete_notify (plugin->env->cls,
                              &val->key,
-                             val->size);
+                             val->size + OVERHEAD);
+  GNUNET_free_non_null (val->path_info);
   GNUNET_free (val);
   return GNUNET_OK;
 }
@@ -337,8 +383,8 @@ heap_plugin_del (void *cls)
 /**
  * Entry point for the plugin.
  *
- * @param cls closure (the "struct GNUNET_DATACACHE_PluginEnvironmnet")
- * @return the plugin's closure (our "struct Plugin")
+ * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironmnet`)
+ * @return the plugin's closure (our `struct Plugin`)
  */
 void *
 libgnunet_plugin_datacache_heap_init (void *cls)
@@ -347,12 +393,12 @@ libgnunet_plugin_datacache_heap_init (void *cls)
   struct GNUNET_DATACACHE_PluginFunctions *api;
   struct Plugin *plugin;
 
-  plugin = GNUNET_malloc (sizeof (struct Plugin));
+  plugin = GNUNET_new (struct Plugin);
   plugin->map = GNUNET_CONTAINER_multihashmap_create (1024,  /* FIXME: base on quota! */
                                                      GNUNET_YES);
   plugin->heap = GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
   plugin->env = env;
-  api = GNUNET_malloc (sizeof (struct GNUNET_DATACACHE_PluginFunctions));
+  api = GNUNET_new (struct GNUNET_DATACACHE_PluginFunctions);
   api->cls = plugin;
   api->get = &heap_plugin_get;
   api->put = &heap_plugin_put;
@@ -381,6 +427,7 @@ libgnunet_plugin_datacache_heap_done (void *cls)
                   GNUNET_CONTAINER_multihashmap_remove (plugin->map,
                                                         &val->key,
                                                         val));
+    GNUNET_free_non_null (val->path_info);
     GNUNET_free (val);
   }
   GNUNET_CONTAINER_heap_destroy (plugin->heap);