7b674087ecc60a50f76c33b15aff30ae923e7c03
[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 rid reservation ID to use (from "reserve"); use 0 if no
339  *            prior reservation was made
340  * @param key key for the value
341  * @param size number of bytes in data
342  * @param data content stored
343  * @param type type of the content
344  * @param priority priority of the content
345  * @param anonymity anonymity-level for the content
346  * @param expiration expiration time for the content
347  * @param timeout timeout for the operation
348  * @param cont continuation to call when done
349  * @param cont_cls closure for cont
350  */
351 void
352 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
353                       int rid,
354                       const GNUNET_HashCode * key,
355                       uint32_t size,
356                       const void *data,
357                       uint32_t type,
358                       uint32_t priority,
359                       uint32_t anonymity,
360                       struct GNUNET_TIME_Absolute expiration,
361                       struct GNUNET_TIME_Relative timeout,
362                       GNUNET_DATASTORE_ContinuationWithStatus cont,
363                       void *cont_cls)
364 {
365   struct DataMessage *dm;
366   size_t msize;
367
368 #if DEBUG_DATASTORE
369   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370               "Asked to put %u bytes of data\n",
371               size);
372 #endif
373   msize = sizeof(struct DataMessage) + size;
374   GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
375   dm = (struct DataMessage*) &h[1];
376   dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
377   dm->header.size = htons(msize);
378   dm->rid = htonl(rid);
379   dm->size = htonl(size);
380   dm->type = htonl(type);
381   dm->priority = htonl(priority);
382   dm->anonymity = htonl(anonymity);
383   dm->uid = GNUNET_htonll(0);
384   dm->expiration = GNUNET_TIME_absolute_hton(expiration);
385   dm->key = *key;
386   memcpy (&dm[1], data, size);
387   transmit_for_status (h, cont, cont_cls, timeout);
388 }
389
390
391 /**
392  * Reserve space in the datastore.  This function should be used
393  * to avoid "out of space" failures during a longer sequence of "put"
394  * operations (for example, when a file is being inserted).
395  *
396  * @param h handle to the datastore
397  * @param amount how much space (in bytes) should be reserved (for content only)
398  * @param entries how many entries will be created (to calculate per-entry overhead)
399  * @param cont continuation to call when done; "success" will be set to
400  *             a positive reservation value if space could be reserved.
401  * @param cont_cls closure for cont
402  * @param timeout how long to wait at most for a response
403  */
404 void
405 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
406                           uint64_t amount,
407                           uint32_t entries,
408                           GNUNET_DATASTORE_ContinuationWithStatus cont,
409                           void *cont_cls,
410                           struct GNUNET_TIME_Relative timeout)
411 {
412   struct ReserveMessage *rm;
413
414   rm = (struct ReserveMessage*) &h[1];
415   rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
416   rm->header.size = htons(sizeof (struct ReserveMessage));
417   rm->entries = htonl(entries);
418   rm->amount = GNUNET_htonll(amount);
419   transmit_for_status (h, cont, cont_cls, timeout);
420 }
421
422
423 /**
424  * Signal that all of the data for which a reservation was made has
425  * been stored and that whatever excess space might have been reserved
426  * can now be released.
427  *
428  * @param h handle to the datastore
429  * @param rid reservation ID (value of "success" in original continuation
430  *        from the "reserve" function).
431  * @param cont continuation to call when done
432  * @param cont_cls closure for cont
433  * @param timeout how long to wait at most for a response
434  */
435 void
436 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
437                                   int rid,
438                                   GNUNET_DATASTORE_ContinuationWithStatus cont,
439                                   void *cont_cls,
440                                   struct GNUNET_TIME_Relative timeout)
441 {
442   struct ReleaseReserveMessage *rrm;
443
444   rrm = (struct ReleaseReserveMessage*) &h[1];
445   rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
446   rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
447   rrm->rid = htonl(rid);
448   transmit_for_status (h, cont, cont_cls, timeout);
449 }
450
451
452 /**
453  * Update a value in the datastore.
454  *
455  * @param h handle to the datastore
456  * @param uid identifier for the value
457  * @param priority how much to increase the priority of the value
458  * @param expiration new expiration value should be MAX of existing and this argument
459  * @param cont continuation to call when done
460  * @param cont_cls closure for cont
461  * @param timeout how long to wait at most for a response
462  */
463 void
464 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
465                          unsigned long long uid,
466                          uint32_t priority,
467                          struct GNUNET_TIME_Absolute expiration,
468                          GNUNET_DATASTORE_ContinuationWithStatus cont,
469                          void *cont_cls,
470                          struct GNUNET_TIME_Relative timeout)
471 {
472   struct UpdateMessage *um;
473
474   um = (struct UpdateMessage*) &h[1];
475   um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
476   um->header.size = htons(sizeof (struct UpdateMessage));
477   um->priority = htonl(priority);
478   um->expiration = GNUNET_TIME_absolute_hton(expiration);
479   um->uid = GNUNET_htonll(uid);
480   transmit_for_status (h, cont, cont_cls, timeout);
481 }
482
483
484
485
486 /**
487  * Type of a function to call when we receive a message
488  * from the service.  This specific function is used
489  * to handle messages of type "struct DataMessage".
490  *
491  * @param cls closure
492  * @param msg message received, NULL on timeout or fatal error
493  */
494 static void 
495 with_result_response_handler (void *cls,
496                               const struct
497                               GNUNET_MessageHeader * msg)
498 {
499   struct GNUNET_DATASTORE_Handle *h = cls;
500   GNUNET_DATASTORE_Iterator cont = h->response_proc;
501   const struct DataMessage *dm;
502   size_t msize;
503
504   if (msg == NULL)
505     {
506       h->response_proc = NULL;
507       GNUNET_CLIENT_disconnect (h->client);
508       h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
509       cont (h->response_proc_cls, 
510             NULL, 0, NULL, 0, 0, 0, 
511             GNUNET_TIME_UNIT_ZERO_ABS, 0);
512       return;
513     }
514   if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END) 
515     {
516       GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
517       h->response_proc = NULL;
518 #if DEBUG_DATASTORE
519       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520                   "Received end of result set\n");
521 #endif
522       cont (h->response_proc_cls, 
523             NULL, 0, NULL, 0, 0, 0, 
524             GNUNET_TIME_UNIT_ZERO_ABS, 0);
525       return;
526     }
527   if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
528        (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ) 
529     {
530       GNUNET_break (0);
531       GNUNET_CLIENT_disconnect (h->client);
532       h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
533       h->response_proc = NULL;
534       cont (h->response_proc_cls, 
535             NULL, 0, NULL, 0, 0, 0, 
536             GNUNET_TIME_UNIT_ZERO_ABS, 0);
537       return;
538     }
539   dm = (const struct DataMessage*) msg;
540   msize = ntohl(dm->size);
541   if (ntohs(msg->size) != msize + sizeof(struct DataMessage))
542     {
543       GNUNET_break (0);
544       GNUNET_CLIENT_disconnect (h->client);
545       h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
546       h->response_proc = NULL;
547       cont (h->response_proc_cls, 
548             NULL, 0, NULL, 0, 0, 0, 
549             GNUNET_TIME_UNIT_ZERO_ABS, 0);
550       return;
551     }
552 #if DEBUG_DATASTORE
553   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
554               "Received result %llu with type %u and size %u with key %s\n",
555               (unsigned long long) GNUNET_ntohll(dm->uid),
556               ntohl(dm->type),
557               msize,
558               GNUNET_h2s(&dm->key));
559 #endif
560   cont (h->response_proc_cls, 
561         &dm->key,
562         msize,
563         &dm[1],
564         ntohl(dm->type),
565         ntohl(dm->priority),
566         ntohl(dm->anonymity),
567         GNUNET_TIME_absolute_ntoh(dm->expiration),      
568         GNUNET_ntohll(dm->uid));
569   GNUNET_CLIENT_receive (h->client,
570                          &with_result_response_handler,
571                          h,
572                          GNUNET_TIME_absolute_get_remaining (h->timeout));
573 }
574
575
576 /**
577  * Transmit message to datastore service and then
578  * read a result message.
579  *
580  * @param cls closure with handle to datastore
581  * @param size number of bytes we can transmit at most
582  * @param buf where to write transmission, NULL on
583  *        timeout
584  * @return number of bytes copied to buf
585  */
586 static size_t
587 transmit_get_result (void *cls,
588                      size_t size,
589                      void *buf)
590 {
591   struct GNUNET_DATASTORE_Handle *h = cls;
592   GNUNET_DATASTORE_ContinuationWithStatus cont = h->response_proc;
593   uint16_t msize;
594
595   if (buf == NULL)
596     {
597       h->response_proc = NULL;
598       h->message_size = 0;
599       cont (h->response_proc_cls, 
600             GNUNET_SYSERR,
601             gettext_noop ("Error transmitting message to datastore service.\n"));
602       return 0;
603     }
604   msize = h->message_size;
605   GNUNET_assert (msize <= size);
606   memcpy (buf, &h[1], msize);
607 #if DEBUG_DATASTORE
608   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
609               "Transmitted %u byte message to datastore service, now waiting for result.\n",
610               msize);
611 #endif
612   h->message_size = 0;
613   GNUNET_CLIENT_receive (h->client,
614                          &with_result_response_handler,
615                          h,
616                          GNUNET_TIME_absolute_get_remaining (h->timeout));
617   return msize;
618 }
619
620
621 /**
622  * Helper function that will initiate the
623  * transmission of a message to the datastore
624  * service.  The message must already be prepared
625  * and stored in the buffer at the end of the
626  * handle.  The message must be of a type that
627  * expects a "DataMessage" in response.
628  *
629  * @param h handle to the service with prepared message
630  * @param cont function to call with result
631  * @param cont_cls closure
632  * @param timeout timeout for the operation
633  */
634 static void
635 transmit_for_result (struct GNUNET_DATASTORE_Handle *h,
636                      GNUNET_DATASTORE_Iterator cont,
637                      void *cont_cls,
638                      struct GNUNET_TIME_Relative timeout)
639 {
640   const struct GNUNET_MessageHeader *hdr;
641   uint16_t msize;
642
643   GNUNET_assert (cont != NULL);
644   hdr = (const struct GNUNET_MessageHeader*) &h[1];
645   msize = ntohs(hdr->size);
646 #if DEBUG_DATASTORE
647   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
648               "Transmitting %u byte message of type %u to datastore service\n",
649               msize,
650               ntohs(hdr->type));
651 #endif
652   GNUNET_assert (h->response_proc == NULL);
653   h->response_proc = cont;
654   h->response_proc_cls = cont_cls;
655   h->timeout = GNUNET_TIME_relative_to_absolute (timeout);
656   h->message_size = msize;
657   if (NULL == GNUNET_CLIENT_notify_transmit_ready (h->client,
658                                                    msize,
659                                                    timeout,
660                                                    &transmit_get_result,
661                                                    h))
662     {
663       GNUNET_break (0);
664       h->response_proc = NULL;
665       h->message_size = 0;
666       cont (h->response_proc_cls, 
667             NULL, 0, NULL, 0, 0, 0, 
668             GNUNET_TIME_UNIT_ZERO_ABS, 0);
669     }
670 }
671
672
673 /**
674  * Iterate over the results for a particular key
675  * in the datastore.
676  *
677  * @param h handle to the datastore
678  * @param key maybe NULL (to match all entries)
679  * @param type desired type, 0 for any
680  * @param iter function to call on each matching value;
681  *        will be called once with a NULL value at the end
682  * @param iter_cls closure for iter
683  * @param timeout how long to wait at most for a response
684  */
685 void
686 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
687                       const GNUNET_HashCode * key,
688                       uint32_t type,
689                       GNUNET_DATASTORE_Iterator iter, void *iter_cls,
690                       struct GNUNET_TIME_Relative timeout)
691 {
692   struct GetMessage *gm;
693
694   gm = (struct GetMessage*) &h[1];
695   gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
696   gm->type = htonl(type);
697   if (key != NULL)
698     {
699       gm->header.size = htons(sizeof (struct GetMessage));
700       gm->key = *key;
701     }
702   else
703     {
704       gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
705     }
706   transmit_for_result (h, iter, iter_cls, timeout);
707 }
708
709
710 /**
711  * Get a random value from the datastore.
712  *
713  * @param h handle to the datastore
714  * @param iter function to call on a random value; it
715  *        will be called exactly once; if no values
716  *        are available, the value will be NULL.
717  * @param iter_cls closure for iter
718  * @param timeout how long to wait at most for a response
719  */
720 void
721 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
722                              GNUNET_DATASTORE_Iterator iter, void *iter_cls,
723                              struct GNUNET_TIME_Relative timeout)
724 {
725   struct GNUNET_MessageHeader *m;
726
727   m = (struct GNUNET_MessageHeader*) &h[1];
728   m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
729   m->size = htons(sizeof (struct GNUNET_MessageHeader));
730   transmit_for_result (h, iter, iter_cls, timeout);
731 }
732
733
734 /**
735  * Explicitly remove some content from the database.
736  *
737  * @param h handle to the datastore
738  * @param key key for the value
739  * @param size number of bytes in data
740  * @param data content stored
741  * @param cont continuation to call when done
742  * @param cont_cls closure for cont
743  * @param timeout how long to wait at most for a response
744  */
745 void
746 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
747                          const GNUNET_HashCode * key,
748                          uint32_t size, const void *data,
749                          GNUNET_DATASTORE_ContinuationWithStatus cont,
750                          void *cont_cls,
751                          struct GNUNET_TIME_Relative timeout)
752 {
753   struct DataMessage *dm;
754   size_t msize;
755
756   msize = sizeof(struct DataMessage) + size;
757   GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
758   dm = (struct DataMessage*) &h[1];
759   dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
760   dm->header.size = htons(msize);
761   dm->rid = htonl(0);
762   dm->size = htonl(size);
763   dm->type = htonl(0);
764   dm->priority = htonl(0);
765   dm->anonymity = htonl(0);
766   dm->uid = GNUNET_htonll(0);
767   dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
768   dm->key = *key;
769   memcpy (&dm[1], data, size);
770   transmit_for_status (h, cont, cont_cls, timeout);
771 }
772
773
774 /* end of datastore_api.c */