aboutsummaryrefslogtreecommitdiff
path: root/src/dht/gnunet-service-dht_datacache.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-09-26 21:24:03 +0000
committerChristian Grothoff <christian@grothoff.org>2016-09-26 21:24:03 +0000
commit7b00dd51218edbd9182caa664cfce185edc2cc45 (patch)
tree2cea8a9d7085aa6e3d16256f18f7c77f3320bb75 /src/dht/gnunet-service-dht_datacache.c
parent60ff113fe4e7bb71d5696063b9a9b81eba60a108 (diff)
downloadgnunet-7b00dd51218edbd9182caa664cfce185edc2cc45.tar.gz
gnunet-7b00dd51218edbd9182caa664cfce185edc2cc45.zip
porting xdht to new service API, major code de-duplication effort
Diffstat (limited to 'src/dht/gnunet-service-dht_datacache.c')
-rw-r--r--src/dht/gnunet-service-dht_datacache.c193
1 files changed, 175 insertions, 18 deletions
diff --git a/src/dht/gnunet-service-dht_datacache.c b/src/dht/gnunet-service-dht_datacache.c
index 12c79764d..9b4dace67 100644
--- a/src/dht/gnunet-service-dht_datacache.c
+++ b/src/dht/gnunet-service-dht_datacache.c
@@ -31,6 +31,11 @@
31 31
32#define LOG(kind,...) GNUNET_log_from (kind, "dht-dhtcache",__VA_ARGS__) 32#define LOG(kind,...) GNUNET_log_from (kind, "dht-dhtcache",__VA_ARGS__)
33 33
34/**
35 * How many "closest" results to we return for migration when
36 * asked (at most)?
37 */
38#define NUM_CLOSEST 42
34 39
35/** 40/**
36 * Handle to the datacache service (for inserting/retrieving data) 41 * Handle to the datacache service (for inserting/retrieving data)
@@ -127,6 +132,17 @@ struct GetRequestContext
127 * Return value to give back. 132 * Return value to give back.
128 */ 133 */
129 enum GNUNET_BLOCK_EvaluationResult eval; 134 enum GNUNET_BLOCK_EvaluationResult eval;
135
136 /**
137 * Function to call on results.
138 */
139 GDS_DATACACHE_GetCallback gc;
140
141 /**
142 * Closure for @e gc.
143 */
144 void *gc_cls;
145
130}; 146};
131 147
132 148
@@ -136,7 +152,7 @@ struct GetRequestContext
136 * @param cls closure for iterator, a `struct GetRequestContext` 152 * @param cls closure for iterator, a `struct GetRequestContext`
137 * @param exp when does this value expire? 153 * @param exp when does this value expire?
138 * @param key the key this data is stored under 154 * @param key the key this data is stored under
139 * @param size the size of the data identified by key 155 * @param data_size the size of the data identified by key
140 * @param data the actual data 156 * @param data the actual data
141 * @param type the type of the @a data 157 * @param type the type of the @a data
142 * @param put_path_length number of peers in @a put_path 158 * @param put_path_length number of peers in @a put_path
@@ -147,7 +163,7 @@ struct GetRequestContext
147static int 163static int
148datacache_get_iterator (void *cls, 164datacache_get_iterator (void *cls,
149 const struct GNUNET_HashCode *key, 165 const struct GNUNET_HashCode *key,
150 size_t size, 166 size_t data_size,
151 const char *data, 167 const char *data,
152 enum GNUNET_BLOCK_Type type, 168 enum GNUNET_BLOCK_Type type,
153 struct GNUNET_TIME_Absolute exp, 169 struct GNUNET_TIME_Absolute exp,
@@ -157,8 +173,8 @@ datacache_get_iterator (void *cls,
157 struct GetRequestContext *ctx = cls; 173 struct GetRequestContext *ctx = cls;
158 enum GNUNET_BLOCK_EvaluationResult eval; 174 enum GNUNET_BLOCK_EvaluationResult eval;
159 175
160 eval = 176 eval
161 GNUNET_BLOCK_evaluate (GDS_block_context, 177 = GNUNET_BLOCK_evaluate (GDS_block_context,
162 type, 178 type,
163 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO, 179 GNUNET_BLOCK_EO_LOCAL_SKIP_CRYPTO,
164 key, 180 key,
@@ -167,7 +183,7 @@ datacache_get_iterator (void *cls,
167 ctx->xquery, 183 ctx->xquery,
168 ctx->xquery_size, 184 ctx->xquery_size,
169 data, 185 data,
170 size); 186 data_size);
171 LOG (GNUNET_ERROR_TYPE_DEBUG, 187 LOG (GNUNET_ERROR_TYPE_DEBUG,
172 "Found reply for query %s in datacache, evaluation result is %d\n", 188 "Found reply for query %s in datacache, evaluation result is %d\n",
173 GNUNET_h2s (key), 189 GNUNET_h2s (key),
@@ -182,18 +198,13 @@ datacache_get_iterator (void *cls,
182 gettext_noop 198 gettext_noop
183 ("# Good RESULTS found in datacache"), 1, 199 ("# Good RESULTS found in datacache"), 1,
184 GNUNET_NO); 200 GNUNET_NO);
185 GDS_CLIENTS_handle_reply (exp, key, 201 ctx->gc (ctx->gc_cls,
186 0, NULL, 202 type,
187 put_path_length, put_path, 203 exp,
188 type, 204 key,
189 size, data); 205 put_path_length, put_path,
190 /* forward to other peers */ 206 0, NULL,
191 GDS_ROUTING_process (type, 207 data, data_size);
192 exp,
193 key,
194 put_path_length, put_path,
195 0, NULL,
196 data, size);
197 break; 208 break;
198 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE: 209 case GNUNET_BLOCK_EVALUATION_OK_DUPLICATE:
199 GNUNET_STATISTICS_update (GDS_stats, 210 GNUNET_STATISTICS_update (GDS_stats,
@@ -242,6 +253,8 @@ datacache_get_iterator (void *cls,
242 * @param xquery_size number of bytes in @a xquery 253 * @param xquery_size number of bytes in @a xquery
243 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL 254 * @param reply_bf where the reply bf is (to be) stored, possibly updated, can be NULL
244 * @param reply_bf_mutator mutation value for @a reply_bf 255 * @param reply_bf_mutator mutation value for @a reply_bf
256 * @param gc function to call on the results
257 * @param gc_cls closure for @a gc
245 * @return evaluation result for the local replies 258 * @return evaluation result for the local replies
246 */ 259 */
247enum GNUNET_BLOCK_EvaluationResult 260enum GNUNET_BLOCK_EvaluationResult
@@ -250,7 +263,9 @@ GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
250 const void *xquery, 263 const void *xquery,
251 size_t xquery_size, 264 size_t xquery_size,
252 struct GNUNET_CONTAINER_BloomFilter **reply_bf, 265 struct GNUNET_CONTAINER_BloomFilter **reply_bf,
253 uint32_t reply_bf_mutator) 266 uint32_t reply_bf_mutator,
267 GDS_DATACACHE_GetCallback gc,
268 void *gc_cls)
254{ 269{
255 struct GetRequestContext ctx; 270 struct GetRequestContext ctx;
256 unsigned int r; 271 unsigned int r;
@@ -267,6 +282,8 @@ GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
267 ctx.xquery_size = xquery_size; 282 ctx.xquery_size = xquery_size;
268 ctx.reply_bf = reply_bf; 283 ctx.reply_bf = reply_bf;
269 ctx.reply_bf_mutator = reply_bf_mutator; 284 ctx.reply_bf_mutator = reply_bf_mutator;
285 ctx.gc = gc;
286 ctx.gc_cls = gc_cls;
270 r = GNUNET_DATACACHE_get (datacache, 287 r = GNUNET_DATACACHE_get (datacache,
271 key, 288 key,
272 type, 289 type,
@@ -282,6 +299,146 @@ GDS_DATACACHE_handle_get (const struct GNUNET_HashCode *key,
282 299
283 300
284/** 301/**
302 * Function called with a random element from the datacache.
303 * Stores the key in the closure.
304 *
305 * @param cls a `struct GNUNET_HashCode *`, where to store the @a key
306 * @param key key for the content
307 * @param data_size number of bytes in @a data
308 * @param data content stored
309 * @param type type of the content
310 * @param exp when will the content expire?
311 * @param path_info_len number of entries in @a path_info
312 * @param path_info a path through the network
313 * @return #GNUNET_OK to continue iterating, #GNUNET_SYSERR to abort
314 */
315static int
316datacache_random_iterator (void *cls,
317 const struct GNUNET_HashCode *key,
318 size_t data_size,
319 const char *data,
320 enum GNUNET_BLOCK_Type type,
321 struct GNUNET_TIME_Absolute exp,
322 unsigned int path_info_len,
323 const struct GNUNET_PeerIdentity *path_info)
324{
325 struct GNUNET_HashCode *dest = cls;
326
327 *dest = *key;
328 return GNUNET_OK; /* should actually not matter which we return */
329}
330
331
332/**
333 * Obtain a random key from the datacache.
334 * Used by Whanau for load-balancing.
335 *
336 * @param[out] key where to store the key of a random element,
337 * randomized by PRNG if datacache is empty
338 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the datacache is empty
339 */
340int
341GDS_DATACACHE_get_random_key (struct GNUNET_HashCode *key)
342{
343 if (0 ==
344 GNUNET_DATACACHE_get_random (datacache,
345 &datacache_random_iterator,
346 key))
347 {
348 /* randomize key in this case */
349 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_NONCE,
350 key);
351 return GNUNET_SYSERR;
352 }
353 return GNUNET_OK;
354}
355
356
357/**
358 * Closure for #datacache_get_successors_iterator().
359 */
360struct SuccContext
361{
362 /**
363 * Function to call on the result
364 */
365 GDS_DATACACHE_SuccessorCallback cb;
366
367 /**
368 * Closure for @e cb.
369 */
370 void *cb_cls;
371};
372
373
374/**
375 * Iterator for local get request results,
376 *
377 * @param cls closure with the `struct GNUNET_HashCode *` with the trail ID
378 * @param key the key this data is stored under
379 * @param size the size of the data identified by key
380 * @param data the actual data
381 * @param type the type of the data
382 * @param exp when does this value expire?
383 * @param put_path_length number of peers in @a put_path
384 * @param put_path path the reply took on put
385 * @return #GNUNET_OK to continue iteration, anything else
386 * to stop iteration.
387 */
388static int
389datacache_get_successors_iterator (void *cls,
390 const struct GNUNET_HashCode *key,
391 size_t size,
392 const char *data,
393 enum GNUNET_BLOCK_Type type,
394 struct GNUNET_TIME_Absolute exp,
395 unsigned int put_path_length,
396 const struct GNUNET_PeerIdentity *put_path)
397{
398 const struct SuccContext *sc = cls;
399
400 /* NOTE: The datacache currently does not store the RO from
401 the original 'put', so we don't know the 'correct' option
402 at this point anymore. Thus, we conservatively assume
403 that recording is desired (for now). */
404 sc->cb (sc->cb_cls,
405 GNUNET_DHT_RO_RECORD_ROUTE,
406 key,
407 type,
408 put_path_length, put_path,
409 exp,
410 data,
411 size);
412 return GNUNET_OK;
413}
414
415
416/**
417 * Handle a request for data close to a key that we have received from
418 * another peer.
419 *
420 * @param key the location at which the peer is looking for data that is close
421 * @param cb function to call with the result
422 * @param cb_cls closure for @a cb
423 */
424void
425GDS_DATACACHE_get_successors (const struct GNUNET_HashCode *key,
426 GDS_DATACACHE_SuccessorCallback cb,
427 void *cb_cls)
428{
429 struct SuccContext sc;
430
431 sc.cb = cb;
432 sc.cb_cls = cb_cls;
433 (void) GNUNET_DATACACHE_get_closest (datacache,
434 key,
435 NUM_CLOSEST,
436 &datacache_get_successors_iterator,
437 &sc);
438}
439
440
441/**
285 * Initialize datacache subsystem. 442 * Initialize datacache subsystem.
286 */ 443 */
287void 444void