aboutsummaryrefslogtreecommitdiff
path: root/src/datacache/plugin_datacache_heap.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-04-28 15:05:12 +0000
committerChristian Grothoff <christian@grothoff.org>2015-04-28 15:05:12 +0000
commit7581e114280c0c33f375b5d3ba49508b82680755 (patch)
treed24afeeda69f94b70d1dc325db06810a4b0b1be0 /src/datacache/plugin_datacache_heap.c
parent553909348c19833330ad94d7a1064eaf48f97e3f (diff)
downloadgnunet-7581e114280c0c33f375b5d3ba49508b82680755.tar.gz
gnunet-7581e114280c0c33f375b5d3ba49508b82680755.zip
extending datacache API with function to return random element, implemented (only) in heap plugin right now
Diffstat (limited to 'src/datacache/plugin_datacache_heap.c')
-rw-r--r--src/datacache/plugin_datacache_heap.c84
1 files changed, 59 insertions, 25 deletions
diff --git a/src/datacache/plugin_datacache_heap.c b/src/datacache/plugin_datacache_heap.c
index e1cabd746..32e762e23 100644
--- a/src/datacache/plugin_datacache_heap.c
+++ b/src/datacache/plugin_datacache_heap.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet 2 This file is part of GNUnet
3 Copyright (C) 2012 Christian Grothoff (and other contributing authors) 3 Copyright (C) 2012, 2015 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -87,7 +87,7 @@ struct Value
87 size_t size; 87 size_t size;
88 88
89 /** 89 /**
90 * Number of entries in 'path_info'. 90 * Number of entries in @e path_info.
91 */ 91 */
92 unsigned int path_info_len; 92 unsigned int path_info_len;
93 93
@@ -103,7 +103,7 @@ struct Value
103 103
104 104
105/** 105/**
106 * Closure for 'put_cb'. 106 * Closure for #put_cb().
107 */ 107 */
108struct PutContext 108struct PutContext
109{ 109{
@@ -128,7 +128,7 @@ struct PutContext
128 const struct GNUNET_PeerIdentity *path_info; 128 const struct GNUNET_PeerIdentity *path_info;
129 129
130 /** 130 /**
131 * Number of bytes in 'data'. 131 * Number of bytes in @e data.
132 */ 132 */
133 size_t size; 133 size_t size;
134 134
@@ -138,12 +138,12 @@ struct PutContext
138 enum GNUNET_BLOCK_Type type; 138 enum GNUNET_BLOCK_Type type;
139 139
140 /** 140 /**
141 * Number of entries in 'path_info'. 141 * Number of entries in @e path_info.
142 */ 142 */
143 unsigned int path_info_len; 143 unsigned int path_info_len;
144 144
145 /** 145 /**
146 * Value to set to GNUNET_YES if an equivalent block was found. 146 * Value to set to #GNUNET_YES if an equivalent block was found.
147 */ 147 */
148 int found; 148 int found;
149}; 149};
@@ -153,7 +153,7 @@ struct PutContext
153 * Function called during PUT to detect if an equivalent block 153 * Function called during PUT to detect if an equivalent block
154 * already exists. 154 * already exists.
155 * 155 *
156 * @param cls the 'struct PutContext' 156 * @param cls the `struct PutContext`
157 * @param key the key for the value(s) 157 * @param key the key for the value(s)
158 * @param value an existing value 158 * @param value an existing value
159 * @return #GNUNET_YES if not found (to continue to iterate) 159 * @return #GNUNET_YES if not found (to continue to iterate)
@@ -200,17 +200,20 @@ put_cb (void *cls,
200 * 200 *
201 * @param cls closure (our `struct Plugin`) 201 * @param cls closure (our `struct Plugin`)
202 * @param key key to store data under 202 * @param key key to store data under
203 * @param size number of bytes in data 203 * @param size number of bytes in @a data
204 * @param data data to store 204 * @param data data to store
205 * @param type type of the value 205 * @param type type of the value
206 * @param discard_time when to discard the value in any case 206 * @param discard_time when to discard the value in any case
207 * @param path_info_len number of entries in @a path_info 207 * @param path_info_len number of entries in @a path_info
208 * @param path_info a path through the network 208 * @param path_info a path through the network
209 * @return 0 if duplicate, -1 on error, number of bytes used otherwise 209 * @return 0 if duplicate, -1 on error, number of bytes used otherwise
210 */ 210 */
211static ssize_t 211static ssize_t
212heap_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size, 212heap_plugin_put (void *cls,
213 const char *data, enum GNUNET_BLOCK_Type type, 213 const struct GNUNET_HashCode *key,
214 size_t size,
215 const char *data,
216 enum GNUNET_BLOCK_Type type,
214 struct GNUNET_TIME_Absolute discard_time, 217 struct GNUNET_TIME_Absolute discard_time,
215 unsigned int path_info_len, 218 unsigned int path_info_len,
216 const struct GNUNET_PeerIdentity *path_info) 219 const struct GNUNET_PeerIdentity *path_info)
@@ -229,7 +232,7 @@ heap_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size,
229 put_ctx.type = type; 232 put_ctx.type = type;
230 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map, 233 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
231 key, 234 key,
232 put_cb, 235 &put_cb,
233 &put_ctx); 236 &put_ctx);
234 if (GNUNET_YES == put_ctx.found) 237 if (GNUNET_YES == put_ctx.found)
235 return 0; 238 return 0;
@@ -256,7 +259,7 @@ heap_plugin_put (void *cls, const struct GNUNET_HashCode * key, size_t size,
256 259
257 260
258/** 261/**
259 * Closure for 'get_cb'. 262 * Closure for #get_cb().
260 */ 263 */
261struct GetContext 264struct GetContext
262{ 265{
@@ -266,7 +269,7 @@ struct GetContext
266 GNUNET_DATACACHE_Iterator iter; 269 GNUNET_DATACACHE_Iterator iter;
267 270
268 /** 271 /**
269 * Closure for 'iter'. 272 * Closure for @e iter.
270 */ 273 */
271 void *iter_cls; 274 void *iter_cls;
272 275
@@ -287,10 +290,10 @@ struct GetContext
287 * Function called during GET to find matching blocks. 290 * Function called during GET to find matching blocks.
288 * Only matches by type. 291 * Only matches by type.
289 * 292 *
290 * @param cls the 'struct GetContext' 293 * @param cls the `struct GetContext`
291 * @param key the key for the value(s) 294 * @param key the key for the value(s)
292 * @param value an existing value 295 * @param value an existing value
293 * @return GNUNET_YES to continue to iterate 296 * @return #GNUNET_YES to continue to iterate
294 */ 297 */
295static int 298static int
296get_cb (void *cls, 299get_cb (void *cls,
@@ -310,7 +313,7 @@ get_cb (void *cls,
310 val->size, 313 val->size,
311 (const char *) &val[1], 314 (const char *) &val[1],
312 val->type, 315 val->type,
313 val->discard_time, 316 val->discard_time,
314 val->path_info_len, 317 val->path_info_len,
315 val->path_info); 318 val->path_info);
316 else 319 else
@@ -324,17 +327,19 @@ get_cb (void *cls,
324 * Iterate over the results for a particular key 327 * Iterate over the results for a particular key
325 * in the datastore. 328 * in the datastore.
326 * 329 *
327 * @param cls closure (our "struct Plugin") 330 * @param cls closure (our `struct Plugin`)
328 * @param key 331 * @param key
329 * @param type entries of which type are relevant? 332 * @param type entries of which type are relevant?
330 * @param iter maybe NULL (to just count) 333 * @param iter maybe NULL (to just count)
331 * @param iter_cls closure for iter 334 * @param iter_cls closure for @a iter
332 * @return the number of results found 335 * @return the number of results found
333 */ 336 */
334static unsigned int 337static unsigned int
335heap_plugin_get (void *cls, const struct GNUNET_HashCode * key, 338heap_plugin_get (void *cls,
336 enum GNUNET_BLOCK_Type type, GNUNET_DATACACHE_Iterator iter, 339 const struct GNUNET_HashCode *key,
337 void *iter_cls) 340 enum GNUNET_BLOCK_Type type,
341 GNUNET_DATACACHE_Iterator iter,
342 void *iter_cls)
338{ 343{
339 struct Plugin *plugin = cls; 344 struct Plugin *plugin = cls;
340 struct GetContext get_ctx; 345 struct GetContext get_ctx;
@@ -345,7 +350,7 @@ heap_plugin_get (void *cls, const struct GNUNET_HashCode * key,
345 get_ctx.cnt = 0; 350 get_ctx.cnt = 0;
346 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map, 351 GNUNET_CONTAINER_multihashmap_get_multiple (plugin->map,
347 key, 352 key,
348 get_cb, 353 &get_cb,
349 &get_ctx); 354 &get_ctx);
350 return get_ctx.cnt; 355 return get_ctx.cnt;
351} 356}
@@ -355,7 +360,7 @@ heap_plugin_get (void *cls, const struct GNUNET_HashCode * key,
355 * Delete the entry with the lowest expiration value 360 * Delete the entry with the lowest expiration value
356 * from the datacache right now. 361 * from the datacache right now.
357 * 362 *
358 * @param cls closure (our "struct Plugin") 363 * @param cls closure (our `struct Plugin`)
359 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 364 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
360 */ 365 */
361static int 366static int
@@ -381,6 +386,33 @@ heap_plugin_del (void *cls)
381 386
382 387
383/** 388/**
389 * Return a random value from the datastore.
390 *
391 * @param cls closure (our `struct Plugin`)
392 * @param iter maybe NULL (to just count)
393 * @param iter_cls closure for @a iter
394 * @return the number of results found
395 */
396static unsigned int
397heap_plugin_get_random (void *cls,
398 GNUNET_DATACACHE_Iterator iter,
399 void *iter_cls)
400{
401 struct Plugin *plugin = cls;
402 struct GetContext get_ctx;
403
404 get_ctx.type = GNUNET_BLOCK_TYPE_ANY;
405 get_ctx.iter = iter;
406 get_ctx.iter_cls = iter_cls;
407 get_ctx.cnt = 0;
408 GNUNET_CONTAINER_multihashmap_get_random (plugin->map,
409 &get_cb,
410 &get_ctx);
411 return get_ctx.cnt;
412}
413
414
415/**
384 * Entry point for the plugin. 416 * Entry point for the plugin.
385 * 417 *
386 * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironmnet`) 418 * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironmnet`)
@@ -403,7 +435,9 @@ libgnunet_plugin_datacache_heap_init (void *cls)
403 api->get = &heap_plugin_get; 435 api->get = &heap_plugin_get;
404 api->put = &heap_plugin_put; 436 api->put = &heap_plugin_put;
405 api->del = &heap_plugin_del; 437 api->del = &heap_plugin_del;
406 LOG (GNUNET_ERROR_TYPE_INFO, _("Heap datacache running\n")); 438 api->get_random = &heap_plugin_get_random;
439 LOG (GNUNET_ERROR_TYPE_INFO,
440 _("Heap datacache running\n"));
407 return api; 441 return api;
408} 442}
409 443