more work on FS URIs
[oweals/gnunet.git] / src / datastore / datastore_api.c
1 /*
2      This file is part of GNUnet
3      (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
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
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file datastore/datastore_api.c
23  * @brief Management for the datastore for files stored on a GNUnet node
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_datastore_service.h"
28 #include "datastore.h"
29
30 /**
31  * Handle to the datastore service.  Followed
32  * by 65536 bytes used for storing messages.
33  */
34 struct GNUNET_DATASTORE_Handle
35 {
36
37   /**
38    * Our configuration.
39    */
40   const struct GNUNET_CONFIGURATION_Handle *cfg;
41
42   /**
43    * Our scheduler.
44    */
45   struct GNUNET_SCHEDULER_Handle *sched;
46
47   /**
48    * Current connection to the datastore service.
49    */
50   struct GNUNET_CLIENT_Connection *client;
51
52   /**
53    * Current response processor (NULL if we are not waiting for a
54    * response).  The specific type depends on the kind of message we
55    * just transmitted.
56    */
57   void *response_proc;
58   
59   /**
60    * Closure for response_proc.
61    */
62   void *response_proc_cls;
63
64   /**
65    * Timeout for the current operation.
66    */
67   struct GNUNET_TIME_Absolute timeout;
68
69   /**
70    * Number of bytes in the message following
71    * this struct, 0 if we have no request pending.
72    */
73   size_t message_size;
74
75 };
76
77
78 /**
79  * Connect to the datastore service.
80  *
81  * @param cfg configuration to use
82  * @param sched scheduler to use
83  * @return handle to use to access the service
84  */
85 struct GNUNET_DATASTORE_Handle *GNUNET_DATASTORE_connect (const struct
86                                                           GNUNET_CONFIGURATION_Handle
87                                                           *cfg,
88                                                           struct
89                                                           GNUNET_SCHEDULER_Handle
90                                                           *sched)
91 {
92   struct GNUNET_CLIENT_Connection *c;
93   struct GNUNET_DATASTORE_Handle *h;
94   
95   c = GNUNET_CLIENT_connect (sched, "datastore", cfg);
96   if (c == NULL)
97     return NULL; /* oops */
98   h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) + 
99                      GNUNET_SERVER_MAX_MESSAGE_SIZE);
100   h->client = c;
101   h->cfg = cfg;
102   h->sched = sched;
103   return h;
104 }
105
106
107 /**
108  * Transmit DROP message to datastore service.
109  */
110 static size_t
111 transmit_drop (void *cls,
112                size_t size, void *buf)
113 {
114   struct GNUNET_DATASTORE_Handle *h = cls;
115   struct GNUNET_MessageHeader *hdr;
116   
117   if (buf == NULL)
118     {
119       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
120                   _("Failed to transmit request to drop database.\n"));
121       GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
122       return 0;
123     }
124   GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
125   hdr = buf;
126   hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
127   hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
128   GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
129   return sizeof(struct GNUNET_MessageHeader);
130 }
131
132
133 /**
134  * Disconnect from the datastore service (and free
135  * associated resources).
136  *
137  * @param h handle to the datastore
138  * @param drop set to GNUNET_YES to delete all data in datastore (!)
139  */
140 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
141                                   int drop)
142 {
143   GNUNET_assert (0 == h->message_size);
144   GNUNET_assert (NULL == h->response_proc);
145   if ( (GNUNET_YES == drop) &&
146        (h->client != NULL) )
147     {
148       if (NULL != 
149           GNUNET_CLIENT_notify_transmit_ready (h->client,
150                                                sizeof(struct GNUNET_MessageHeader),
151                                                GNUNET_TIME_UNIT_MINUTES,
152                                                &transmit_drop,
153                                                h))
154         return;
155       GNUNET_break (0);
156     }
157   if (h->client != NULL)
158     GNUNET_CLIENT_disconnect (h->client);
159   GNUNET_free (h);
160 }
161
162
163 /**
164  * Type of a function to call when we receive a message
165  * from the service.  This specific function is used
166  * to handle messages of type "struct StatusMessage".
167  *
168  * @param cls closure
169  * @param msg message received, NULL on timeout or fatal error
170  */
171 static void 
172 with_status_response_handler (void *cls,
173                               const struct
174                               GNUNET_MessageHeader * msg)
175 {
176   struct GNUNET_DATASTORE_Handle *h = cls;
177   GNUNET_DATASTORE_ContinuationWithStatus cont = h->response_proc;
178   const struct StatusMessage *sm;
179   const char *emsg;
180   int status;
181
182   if (msg == NULL)
183     {
184       h->response_proc = NULL;
185       GNUNET_CLIENT_disconnect (h->client);
186       h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
187       cont (h->response_proc_cls, 
188             GNUNET_SYSERR,
189             _("Timeout trying to read response from datastore service\n"));       
190       return;
191     }
192   if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
193        (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) ) 
194     {
195       GNUNET_break (0);
196       h->response_proc = NULL;
197       GNUNET_CLIENT_disconnect (h->client);
198       h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
199       cont (h->response_proc_cls, 
200             GNUNET_SYSERR,
201             _("Error reading response from datastore service\n"));
202       return;
203     }
204   sm = (const struct StatusMessage*) msg;
205   status = ntohl(sm->status);
206   emsg = NULL;
207   if (ntohs(msg->size) > sizeof(struct StatusMessage))
208     {
209       emsg = (const char*) &sm[1];
210       if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
211         {
212           GNUNET_break (0);
213           emsg = _("Invalid error message received from datastore service");
214         }
215     }  
216   if ( (status == GNUNET_SYSERR) &&
217        (emsg == NULL) )
218     {
219       GNUNET_break (0);
220       emsg = _("Invalid error message received from datastore service");
221     }
222   h->response_proc = NULL;
223 #if DEBUG_DATASTORE
224   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225               "Received status %d/%s\n",
226               status,
227               emsg);
228 #endif
229   cont (h->response_proc_cls, 
230         status,
231         emsg);
232 }
233
234
235 /**
236  * Transmit message to datastore service and then
237  * read a status message.
238  *
239  * @param cls closure with handle to datastore
240  * @param size number of bytes we can transmit at most
241  * @param buf where to write transmission, NULL on
242  *        timeout
243  * @return number of bytes copied to buf
244  */
245 static size_t
246 transmit_get_status (void *cls,
247                      size_t size,
248                      void *buf)
249 {
250   struct GNUNET_DATASTORE_Handle *h = cls;
251   GNUNET_DATASTORE_ContinuationWithStatus cont = h->response_proc;
252   uint16_t msize;
253
254   if (buf == NULL)
255     {
256       h->message_size = 0;
257       h->response_proc = NULL;
258       cont (h->response_proc_cls, 
259             GNUNET_SYSERR,
260             gettext_noop ("Error transmitting message to datastore service.\n"));
261       return 0;
262     }
263   msize = h->message_size;
264   GNUNET_assert (msize <= size);
265   memcpy (buf, &h[1], msize);
266 #if DEBUG_DATASTORE
267   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268               "Transmitted %u byte message to datastore service, now waiting for status.\n",
269               msize);
270 #endif
271   h->message_size = 0;
272   GNUNET_CLIENT_receive (h->client,
273                          &with_status_response_handler,
274                          h,
275                          GNUNET_TIME_absolute_get_remaining (h->timeout));
276   return msize;
277 }
278
279
280 /**
281  * Helper function that will initiate the
282  * transmission of a message to the datastore
283  * service.  The message must already be prepared
284  * and stored in the buffer at the end of the
285  * handle.  The message must be of a type that
286  * expects a "StatusMessage" in response.
287  *
288  * @param h handle to the service with prepared message
289  * @param cont function to call with result
290  * @param cont_cls closure
291  * @param timeout timeout for the operation
292  */
293 static void
294 transmit_for_status (struct GNUNET_DATASTORE_Handle *h,
295                      GNUNET_DATASTORE_ContinuationWithStatus cont,
296                      void *cont_cls,
297                      struct GNUNET_TIME_Relative timeout)
298 {
299   const struct GNUNET_MessageHeader *hdr;
300   uint16_t msize;
301
302   GNUNET_assert (cont != NULL);
303   hdr = (const struct GNUNET_MessageHeader*) &h[1];
304   msize = ntohs(hdr->size);
305 #if DEBUG_DATASTORE
306   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307               "Transmitting %u byte message of type %u to datastore service\n",
308               msize,
309               ntohs(hdr->type));
310 #endif
311   GNUNET_assert (h->response_proc == NULL);
312   h->response_proc = cont;
313   h->response_proc_cls = cont_cls;
314   h->timeout = GNUNET_TIME_relative_to_absolute (timeout);
315   h->message_size = msize;
316   if (NULL == GNUNET_CLIENT_notify_transmit_ready (h->client,
317                                                    msize,
318                                                    timeout,
319                                                    &transmit_get_status,
320                                                    h))
321     {
322       GNUNET_break (0);
323       h->response_proc = NULL;
324       h->message_size = 0;
325       cont (cont_cls,
326             GNUNET_SYSERR,
327             gettext_noop ("Not ready to transmit request to datastore service"));
328     }
329 }
330
331
332 /**
333  * Store an item in the datastore.  If the item is already present,
334  * the priorities are summed up and the higher expiration time and
335  * lower anonymity level is used.
336  *
337  * @param h handle to the datastore
338  * @param key key for the value
339  * @param size number of bytes in data
340  * @param data content stored
341  * @param type type of the content
342  * @param priority priority of the content
343  * @param anonymity anonymity-level for the content
344  * @param expiration expiration time for the content
345  * @param timeout timeout for the operation
346  * @param cont continuation to call when done
347  * @param cont_cls closure for cont
348  */
349 void
350 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
351                       int rid,
352                       const GNUNET_HashCode * key,
353                       uint32_t size,
354                       const void *data,
355                       uint32_t type,
356                       uint32_t priority,
357                       uint32_t anonymity,
358                       struct GNUNET_TIME_Absolute expiration,
359                       struct GNUNET_TIME_Relative timeout,
360                       GNUNET_DATASTORE_ContinuationWithStatus cont,
361                       void *cont_cls)
362 {
363   struct DataMessage *dm;
364   size_t msize;
365
366 #if DEBUG_DATASTORE
367   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368               "Asked to put %u bytes of data\n",
369               size);
370 #endif
371   msize = sizeof(struct DataMessage) + size;
372   GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
373   dm = (struct DataMessage*) &h[1];
374   dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
375   dm->header.size = htons(msize);
376   dm->rid = htonl(rid);
377   dm->size = htonl(size);
378   dm->type = htonl(type);
379   dm->priority = htonl(priority);
380   dm->anonymity = htonl(anonymity);
381   dm->uid = GNUNET_htonll(0);
382   dm->expiration = GNUNET_TIME_absolute_hton(expiration);
383   dm->key = *key;
384   memcpy (&dm[1], data, size);
385   transmit_for_status (h, cont, cont_cls, timeout);
386 }
387
388
389 /**
390  * Reserve space in the datastore.  This function should be used
391  * to avoid "out of space" failures during a longer sequence of "put"
392  * operations (for example, when a file is being inserted).
393  *
394  * @param h handle to the datastore
395  * @param amount how much space (in bytes) should be reserved (for content only)
396  * @param entries how many entries will be created (to calculate per-entry overhead)
397  * @param cont continuation to call when done; "success" will be set to
398  *             a positive reservation value if space could be reserved.
399  * @param cont_cls closure for cont
400  * @param timeout how long to wait at most for a response
401  */
402 void
403 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
404                           uint64_t amount,
405                           uint32_t entries,
406                           GNUNET_DATASTORE_ContinuationWithStatus cont,
407                           void *cont_cls,
408                           struct GNUNET_TIME_Relative timeout)
409 {
410   struct ReserveMessage *rm;
411
412   rm = (struct ReserveMessage*) &h[1];
413   rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
414   rm->header.size = htons(sizeof (struct ReserveMessage));
415   rm->entries = htonl(entries);
416   rm->amount = GNUNET_htonll(amount);
417   transmit_for_status (h, cont, cont_cls, timeout);
418 }
419
420
421 /**
422  * Signal that all of the data for which a reservation was made has
423  * been stored and that whatever excess space might have been reserved
424  * can now be released.
425  *
426  * @param h handle to the datastore
427  * @param rid reservation ID (value of "success" in original continuation
428  *        from the "reserve" function).
429  * @param cont continuation to call when done
430  * @param cont_cls closure for cont
431  * @param timeout how long to wait at most for a response
432  */
433 void
434 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
435                                   int rid,
436                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
437                                   void *cont_cls,
438                                   struct GNUNET_TIME_Relative timeout)
439 {
440   struct ReleaseReserveMessage *rrm;
441
442   rrm = (struct ReleaseReserveMessage*) &h[1];
443   rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
444   rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
445   rrm->rid = htonl(rid);
446   transmit_for_status (h, cont, cont_cls, timeout);
447 }
448
449
450 /**
451  * Update a value in the datastore.
452  *
453  * @param h handle to the datastore
454  * @param uid identifier for the value
455  * @param priority how much to increase the priority of the value
456  * @param expiration new expiration value should be MAX of existing and this argument
457  * @param cont continuation to call when done
458  * @param cont_cls closure for cont
459  * @param timeout how long to wait at most for a response
460  */
461 void
462 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
463                          unsigned long long uid,
464                          uint32_t priority,
465                          struct GNUNET_TIME_Absolute expiration,
466                          GNUNET_DATASTORE_ContinuationWithStatus cont,
467                          void *cont_cls,
468                          struct GNUNET_TIME_Relative timeout)
469 {
470   struct UpdateMessage *um;
471
472   um = (struct UpdateMessage*) &h[1];
473   um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
474   um->header.size = htons(sizeof (struct UpdateMessage));
475   um->priority = htonl(priority);
476   um->expiration = GNUNET_TIME_absolute_hton(expiration);
477   um->uid = GNUNET_htonll(uid);
478   transmit_for_status (h, cont, cont_cls, timeout);
479 }
480
481
482
483
484 /**
485  * Type of a function to call when we receive a message
486  * from the service.  This specific function is used
487  * to handle messages of type "struct DataMessage".
488  *
489  * @param cls closure
490  * @param msg message received, NULL on timeout or fatal error
491  */
492 static void 
493 with_result_response_handler (void *cls,
494                               const struct
495                               GNUNET_MessageHeader * msg)
496 {
497   struct GNUNET_DATASTORE_Handle *h = cls;
498   GNUNET_DATASTORE_Iterator cont = h->response_proc;
499   const struct DataMessage *dm;
500   size_t msize;
501
502   if (msg == NULL)
503     {
504       h->response_proc = NULL;
505       GNUNET_CLIENT_disconnect (h->client);
506       h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
507       cont (h->response_proc_cls, 
508             NULL, 0, NULL, 0, 0, 0, 
509             GNUNET_TIME_UNIT_ZERO_ABS, 0);
510       return;
511     }
512   if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END) 
513     {
514       GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
515       h->response_proc = NULL;
516 #if DEBUG_DATASTORE
517       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518                   "Received end of result set\n");
519 #endif
520       cont (h->response_proc_cls, 
521             NULL, 0, NULL, 0, 0, 0, 
522             GNUNET_TIME_UNIT_ZERO_ABS, 0);
523       return;
524     }
525   if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
526        (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ) 
527     {
528       GNUNET_break (0);
529       GNUNET_CLIENT_disconnect (h->client);
530       h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
531       h->response_proc = NULL;
532       cont (h->response_proc_cls, 
533             NULL, 0, NULL, 0, 0, 0, 
534             GNUNET_TIME_UNIT_ZERO_ABS, 0);
535       return;
536     }
537   dm = (const struct DataMessage*) msg;
538   msize = ntohl(dm->size);
539   if (ntohs(msg->size) != msize + sizeof(struct DataMessage))
540     {
541       GNUNET_break (0);
542       GNUNET_CLIENT_disconnect (h->client);
543       h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
544       h->response_proc = NULL;
545       cont (h->response_proc_cls, 
546             NULL, 0, NULL, 0, 0, 0, 
547             GNUNET_TIME_UNIT_ZERO_ABS, 0);
548       return;
549     }
550 #if DEBUG_DATASTORE
551   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552               "Received result %llu with type %u and size %u with key %s\n",
553               (unsigned long long) GNUNET_ntohll(dm->uid),
554               ntohl(dm->type),
555               msize,
556               GNUNET_h2s(&dm->key));
557 #endif
558   cont (h->response_proc_cls, 
559         &dm->key,
560         msize,
561         &dm[1],
562         ntohl(dm->type),
563         ntohl(dm->priority),
564         ntohl(dm->anonymity),
565         GNUNET_TIME_absolute_ntoh(dm->expiration),      
566         GNUNET_ntohll(dm->uid));
567   GNUNET_CLIENT_receive (h->client,
568                          &with_result_response_handler,
569                          h,
570                          GNUNET_TIME_absolute_get_remaining (h->timeout));
571 }
572
573
574 /**
575  * Transmit message to datastore service and then
576  * read a result message.
577  *
578  * @param cls closure with handle to datastore
579  * @param size number of bytes we can transmit at most
580  * @param buf where to write transmission, NULL on
581  *        timeout
582  * @return number of bytes copied to buf
583  */
584 static size_t
585 transmit_get_result (void *cls,
586                      size_t size,
587                      void *buf)
588 {
589   struct GNUNET_DATASTORE_Handle *h = cls;
590   GNUNET_DATASTORE_ContinuationWithStatus cont = h->response_proc;
591   uint16_t msize;
592
593   if (buf == NULL)
594     {
595       h->response_proc = NULL;
596       h->message_size = 0;
597       cont (h->response_proc_cls, 
598             GNUNET_SYSERR,
599             gettext_noop ("Error transmitting message to datastore service.\n"));
600       return 0;
601     }
602   msize = h->message_size;
603   GNUNET_assert (msize <= size);
604   memcpy (buf, &h[1], msize);
605 #if DEBUG_DATASTORE
606   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607               "Transmitted %u byte message to datastore service, now waiting for result.\n",
608               msize);
609 #endif
610   h->message_size = 0;
611   GNUNET_CLIENT_receive (h->client,
612                          &with_result_response_handler,
613                          h,
614                          GNUNET_TIME_absolute_get_remaining (h->timeout));
615   return msize;
616 }
617
618
619 /**
620  * Helper function that will initiate the
621  * transmission of a message to the datastore
622  * service.  The message must already be prepared
623  * and stored in the buffer at the end of the
624  * handle.  The message must be of a type that
625  * expects a "DataMessage" in response.
626  *
627  * @param h handle to the service with prepared message
628  * @param cont function to call with result
629  * @param cont_cls closure
630  * @param timeout timeout for the operation
631  */
632 static void
633 transmit_for_result (struct GNUNET_DATASTORE_Handle *h,
634                      GNUNET_DATASTORE_Iterator cont,
635                      void *cont_cls,
636                      struct GNUNET_TIME_Relative timeout)
637 {
638   const struct GNUNET_MessageHeader *hdr;
639   uint16_t msize;
640
641   GNUNET_assert (cont != NULL);
642   hdr = (const struct GNUNET_MessageHeader*) &h[1];
643   msize = ntohs(hdr->size);
644 #if DEBUG_DATASTORE
645   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646               "Transmitting %u byte message of type %u to datastore service\n",
647               msize,
648               ntohs(hdr->type));
649 #endif
650   GNUNET_assert (h->response_proc == NULL);
651   h->response_proc = cont;
652   h->response_proc_cls = cont_cls;
653   h->timeout = GNUNET_TIME_relative_to_absolute (timeout);
654   h->message_size = msize;
655   if (NULL == GNUNET_CLIENT_notify_transmit_ready (h->client,
656                                                    msize,
657                                                    timeout,
658                                                    &transmit_get_result,
659                                                    h))
660     {
661       GNUNET_break (0);
662       h->response_proc = NULL;
663       h->message_size = 0;
664       cont (h->response_proc_cls, 
665             NULL, 0, NULL, 0, 0, 0, 
666             GNUNET_TIME_UNIT_ZERO_ABS, 0);
667     }
668 }
669
670
671 /**
672  * Iterate over the results for a particular key
673  * in the datastore.
674  *
675  * @param h handle to the datastore
676  * @param key maybe NULL (to match all entries)
677  * @param type desired type, 0 for any
678  * @param iter function to call on each matching value;
679  *        will be called once with a NULL value at the end
680  * @param iter_cls closure for iter
681  * @param timeout how long to wait at most for a response
682  */
683 void
684 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
685                       const GNUNET_HashCode * key,
686                       uint32_t type,
687                       GNUNET_DATASTORE_Iterator iter, void *iter_cls,
688                       struct GNUNET_TIME_Relative timeout)
689 {
690   struct GetMessage *gm;
691
692   gm = (struct GetMessage*) &h[1];
693   gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
694   gm->type = htonl(type);
695   if (key != NULL)
696     {
697       gm->header.size = htons(sizeof (struct GetMessage));
698       gm->key = *key;
699     }
700   else
701     {
702       gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
703     }
704   transmit_for_result (h, iter, iter_cls, timeout);
705 }
706
707
708 /**
709  * Get a random value from the datastore.
710  *
711  * @param h handle to the datastore
712  * @param iter function to call on a random value; it
713  *        will be called exactly once; if no values
714  *        are available, the value will be NULL.
715  * @param iter_cls closure for iter
716  * @param timeout how long to wait at most for a response
717  */
718 void
719 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
720                              GNUNET_DATASTORE_Iterator iter, void *iter_cls,
721                              struct GNUNET_TIME_Relative timeout)
722 {
723   struct GNUNET_MessageHeader *m;
724
725   m = (struct GNUNET_MessageHeader*) &h[1];
726   m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
727   m->size = htons(sizeof (struct GNUNET_MessageHeader));
728   transmit_for_result (h, iter, iter_cls, timeout);
729 }
730
731
732 /**
733  * Explicitly remove some content from the database.
734  *
735  * @param h handle to the datastore
736  * @param key key for the value
737  * @param size number of bytes in data
738  * @param data content stored
739  * @param cont continuation to call when done
740  * @param cont_cls closure for cont
741  * @param timeout how long to wait at most for a response
742  */
743 void
744 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
745                          const GNUNET_HashCode * key,
746                          uint32_t size, const void *data,
747                          GNUNET_DATASTORE_ContinuationWithStatus cont,
748                          void *cont_cls,
749                          struct GNUNET_TIME_Relative timeout)
750 {
751   struct DataMessage *dm;
752   size_t msize;
753
754   msize = sizeof(struct DataMessage) + size;
755   GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
756   dm = (struct DataMessage*) &h[1];
757   dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
758   dm->header.size = htons(msize);
759   dm->rid = htonl(0);
760   dm->size = htonl(size);
761   dm->type = htonl(0);
762   dm->priority = htonl(0);
763   dm->anonymity = htonl(0);
764   dm->uid = GNUNET_htonll(0);
765   dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
766   dm->key = *key;
767   memcpy (&dm[1], data, size);
768   transmit_for_status (h, cont, cont_cls, timeout);
769 }
770
771
772 /* end of datastore_api.c */