2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file datastore/datastore_api.c
23 * @brief Management for the datastore for files stored on a GNUnet node
24 * @author Christian Grothoff
27 #include "gnunet_datastore_service.h"
28 #include "datastore.h"
31 * Handle to the datastore service. Followed
32 * by 65536 bytes used for storing messages.
34 struct GNUNET_DATASTORE_Handle
40 const struct GNUNET_CONFIGURATION_Handle *cfg;
45 struct GNUNET_SCHEDULER_Handle *sched;
48 * Current connection to the datastore service.
50 struct GNUNET_CLIENT_Connection *client;
53 * Current response processor (NULL if we are not waiting for a
54 * response). The specific type depends on the kind of message we
60 * Closure for response_proc.
62 void *response_proc_cls;
65 * Timeout for the current operation.
67 struct GNUNET_TIME_Absolute timeout;
70 * Number of bytes in the message following
71 * this struct, 0 if we have no request pending.
79 * Connect to the datastore service.
81 * @param cfg configuration to use
82 * @param sched scheduler to use
83 * @return handle to use to access the service
85 struct GNUNET_DATASTORE_Handle *GNUNET_DATASTORE_connect (const struct
86 GNUNET_CONFIGURATION_Handle
89 GNUNET_SCHEDULER_Handle
92 struct GNUNET_CLIENT_Connection *c;
93 struct GNUNET_DATASTORE_Handle *h;
95 c = GNUNET_CLIENT_connect (sched, "datastore", cfg);
97 return NULL; /* oops */
98 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle) +
99 GNUNET_SERVER_MAX_MESSAGE_SIZE);
108 * Transmit DROP message to datastore service.
111 transmit_drop (void *cls,
112 size_t size, void *buf)
114 struct GNUNET_DATASTORE_Handle *h = cls;
115 struct GNUNET_MessageHeader *hdr;
119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
120 _("Failed to transmit request to drop database.\n"));
121 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
124 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
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);
134 * Disconnect from the datastore service (and free
135 * associated resources).
137 * @param h handle to the datastore
138 * @param drop set to GNUNET_YES to delete all data in datastore (!)
140 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
143 GNUNET_assert (0 == h->message_size);
144 GNUNET_assert (NULL == h->response_proc);
145 if ( (GNUNET_YES == drop) &&
146 (h->client != NULL) )
149 GNUNET_CLIENT_notify_transmit_ready (h->client,
150 sizeof(struct GNUNET_MessageHeader),
151 GNUNET_TIME_UNIT_MINUTES,
157 if (h->client != NULL)
158 GNUNET_CLIENT_disconnect (h->client);
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".
169 * @param msg message received, NULL on timeout or fatal error
172 with_status_response_handler (void *cls,
174 GNUNET_MessageHeader * msg)
176 struct GNUNET_DATASTORE_Handle *h = cls;
177 GNUNET_DATASTORE_ContinuationWithStatus cont = h->response_proc;
178 const struct StatusMessage *sm;
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,
189 _("Timeout trying to read response from datastore service\n"));
192 if ( (ntohs(msg->size) < sizeof(struct StatusMessage)) ||
193 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS) )
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,
201 _("Error reading response from datastore service\n"));
204 sm = (const struct StatusMessage*) msg;
205 status = ntohl(sm->status);
207 if (ntohs(msg->size) > sizeof(struct StatusMessage))
209 emsg = (const char*) &sm[1];
210 if (emsg[ntohs(msg->size) - sizeof(struct StatusMessage) - 1] != '\0')
213 emsg = _("Invalid error message received from datastore service");
216 if ( (status == GNUNET_SYSERR) &&
220 emsg = _("Invalid error message received from datastore service");
222 h->response_proc = NULL;
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Received status %d/%s\n",
229 cont (h->response_proc_cls,
236 * Transmit message to datastore service and then
237 * read a status message.
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
243 * @return number of bytes copied to buf
246 transmit_get_status (void *cls,
250 struct GNUNET_DATASTORE_Handle *h = cls;
251 GNUNET_DATASTORE_ContinuationWithStatus cont = h->response_proc;
257 h->response_proc = NULL;
258 cont (h->response_proc_cls,
260 gettext_noop ("Error transmitting message to datastore service.\n"));
263 msize = h->message_size;
264 GNUNET_assert (msize <= size);
265 memcpy (buf, &h[1], msize);
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Transmitted %u byte message to datastore service, now waiting for status.\n",
272 GNUNET_CLIENT_receive (h->client,
273 &with_status_response_handler,
275 GNUNET_TIME_absolute_get_remaining (h->timeout));
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.
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
294 transmit_for_status (struct GNUNET_DATASTORE_Handle *h,
295 GNUNET_DATASTORE_ContinuationWithStatus cont,
297 struct GNUNET_TIME_Relative timeout)
299 const struct GNUNET_MessageHeader *hdr;
302 GNUNET_assert (cont != NULL);
303 hdr = (const struct GNUNET_MessageHeader*) &h[1];
304 msize = ntohs(hdr->size);
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Transmitting %u byte message of type %u to datastore service\n",
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,
319 &transmit_get_status,
323 h->response_proc = NULL;
327 gettext_noop ("Not ready to transmit request to datastore service"));
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.
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
352 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
354 const GNUNET_HashCode * key,
360 struct GNUNET_TIME_Absolute expiration,
361 struct GNUNET_TIME_Relative timeout,
362 GNUNET_DATASTORE_ContinuationWithStatus cont,
365 struct DataMessage *dm;
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370 "Asked to put %u bytes of data\n",
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);
386 memcpy (&dm[1], data, size);
387 transmit_for_status (h, cont, cont_cls, timeout);
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).
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
405 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
408 GNUNET_DATASTORE_ContinuationWithStatus cont,
410 struct GNUNET_TIME_Relative timeout)
412 struct ReserveMessage *rm;
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);
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.
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
436 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
438 GNUNET_DATASTORE_ContinuationWithStatus cont,
440 struct GNUNET_TIME_Relative timeout)
442 struct ReleaseReserveMessage *rrm;
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);
453 * Update a value in the datastore.
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
464 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
465 unsigned long long uid,
467 struct GNUNET_TIME_Absolute expiration,
468 GNUNET_DATASTORE_ContinuationWithStatus cont,
470 struct GNUNET_TIME_Relative timeout)
472 struct UpdateMessage *um;
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);
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".
492 * @param msg message received, NULL on timeout or fatal error
495 with_result_response_handler (void *cls,
497 GNUNET_MessageHeader * msg)
499 struct GNUNET_DATASTORE_Handle *h = cls;
500 GNUNET_DATASTORE_Iterator cont = h->response_proc;
501 const struct DataMessage *dm;
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);
514 if (ntohs(msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
516 GNUNET_break (ntohs(msg->size) == sizeof(struct GNUNET_MessageHeader));
517 h->response_proc = NULL;
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "Received end of result set\n");
522 cont (h->response_proc_cls,
523 NULL, 0, NULL, 0, 0, 0,
524 GNUNET_TIME_UNIT_ZERO_ABS, 0);
527 if ( (ntohs(msg->size) < sizeof(struct DataMessage)) ||
528 (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) )
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);
539 dm = (const struct DataMessage*) msg;
540 msize = ntohl(dm->size);
541 if (ntohs(msg->size) != msize + sizeof(struct DataMessage))
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);
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),
558 GNUNET_h2s(&dm->key));
560 cont (h->response_proc_cls,
566 ntohl(dm->anonymity),
567 GNUNET_TIME_absolute_ntoh(dm->expiration),
568 GNUNET_ntohll(dm->uid));
573 * Transmit message to datastore service and then
574 * read a result message.
576 * @param cls closure with handle to datastore
577 * @param size number of bytes we can transmit at most
578 * @param buf where to write transmission, NULL on
580 * @return number of bytes copied to buf
583 transmit_get_result (void *cls,
587 struct GNUNET_DATASTORE_Handle *h = cls;
588 GNUNET_DATASTORE_Iterator cont = h->response_proc;
593 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
594 _("Error transmitting message to datastore service.\n"));
595 h->response_proc = NULL;
597 cont (h->response_proc_cls,
598 NULL, 0, NULL, 0, 0, 0,
599 GNUNET_TIME_UNIT_ZERO_ABS, 0);
602 msize = h->message_size;
603 GNUNET_assert (msize <= size);
604 memcpy (buf, &h[1], msize);
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Transmitted %u byte message to datastore service, now waiting for result.\n",
611 GNUNET_CLIENT_receive (h->client,
612 &with_result_response_handler,
614 GNUNET_TIME_absolute_get_remaining (h->timeout));
621 * Function called to trigger obtaining the next result
622 * from the datastore.
624 * @param h handle to the datastore
625 * @param more GNUNET_YES to get moxre results, GNUNET_NO to abort
626 * iteration (with a final call to "iter" with key/data == NULL).
629 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
632 GNUNET_DATASTORE_Iterator cont;
634 if (GNUNET_YES == more)
636 GNUNET_CLIENT_receive (h->client,
637 &with_result_response_handler,
639 GNUNET_TIME_absolute_get_remaining (h->timeout));
642 cont = h->response_proc;
643 h->response_proc = NULL;
644 GNUNET_CLIENT_disconnect (h->client);
645 h->client = GNUNET_CLIENT_connect (h->sched, "datastore", h->cfg);
646 cont (h->response_proc_cls,
647 NULL, 0, NULL, 0, 0, 0,
648 GNUNET_TIME_UNIT_ZERO_ABS, 0);
653 * Helper function that will initiate the
654 * transmission of a message to the datastore
655 * service. The message must already be prepared
656 * and stored in the buffer at the end of the
657 * handle. The message must be of a type that
658 * expects a "DataMessage" in response.
660 * @param h handle to the service with prepared message
661 * @param cont function to call with result
662 * @param cont_cls closure
663 * @param timeout timeout for the operation
666 transmit_for_result (struct GNUNET_DATASTORE_Handle *h,
667 GNUNET_DATASTORE_Iterator cont,
669 struct GNUNET_TIME_Relative timeout)
671 const struct GNUNET_MessageHeader *hdr;
674 GNUNET_assert (cont != NULL);
675 hdr = (const struct GNUNET_MessageHeader*) &h[1];
676 msize = ntohs(hdr->size);
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
679 "Transmitting %u byte message of type %u to datastore service\n",
683 GNUNET_assert (h->response_proc == NULL);
684 h->response_proc = cont;
685 h->response_proc_cls = cont_cls;
686 h->timeout = GNUNET_TIME_relative_to_absolute (timeout);
687 h->message_size = msize;
688 if (NULL == GNUNET_CLIENT_notify_transmit_ready (h->client,
691 &transmit_get_result,
695 h->response_proc = NULL;
697 cont (h->response_proc_cls,
698 NULL, 0, NULL, 0, 0, 0,
699 GNUNET_TIME_UNIT_ZERO_ABS, 0);
705 * Iterate over the results for a particular key
708 * @param h handle to the datastore
709 * @param key maybe NULL (to match all entries)
710 * @param type desired type, 0 for any
711 * @param iter function to call on each matching value;
712 * will be called once with a NULL value at the end
713 * @param iter_cls closure for iter
714 * @param timeout how long to wait at most for a response
717 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
718 const GNUNET_HashCode * key,
720 GNUNET_DATASTORE_Iterator iter, void *iter_cls,
721 struct GNUNET_TIME_Relative timeout)
723 struct GetMessage *gm;
725 gm = (struct GetMessage*) &h[1];
726 gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
727 gm->type = htonl(type);
730 gm->header.size = htons(sizeof (struct GetMessage));
735 gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
737 transmit_for_result (h, iter, iter_cls, timeout);
742 * Get a random value from the datastore.
744 * @param h handle to the datastore
745 * @param iter function to call on a random value; it
746 * will be called exactly once; if no values
747 * are available, the value will be NULL.
748 * @param iter_cls closure for iter
749 * @param timeout how long to wait at most for a response
752 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
753 GNUNET_DATASTORE_Iterator iter, void *iter_cls,
754 struct GNUNET_TIME_Relative timeout)
756 struct GNUNET_MessageHeader *m;
758 m = (struct GNUNET_MessageHeader*) &h[1];
759 m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
760 m->size = htons(sizeof (struct GNUNET_MessageHeader));
761 transmit_for_result (h, iter, iter_cls, timeout);
766 * Explicitly remove some content from the database.
768 * @param h handle to the datastore
769 * @param key key for the value
770 * @param size number of bytes in data
771 * @param data content stored
772 * @param cont continuation to call when done
773 * @param cont_cls closure for cont
774 * @param timeout how long to wait at most for a response
777 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
778 const GNUNET_HashCode * key,
779 uint32_t size, const void *data,
780 GNUNET_DATASTORE_ContinuationWithStatus cont,
782 struct GNUNET_TIME_Relative timeout)
784 struct DataMessage *dm;
787 msize = sizeof(struct DataMessage) + size;
788 GNUNET_assert (msize <= GNUNET_SERVER_MAX_MESSAGE_SIZE);
789 dm = (struct DataMessage*) &h[1];
790 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
791 dm->header.size = htons(msize);
793 dm->size = htonl(size);
795 dm->priority = htonl(0);
796 dm->anonymity = htonl(0);
797 dm->uid = GNUNET_htonll(0);
798 dm->expiration = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_ZERO_ABS);
800 memcpy (&dm[1], data, size);
801 transmit_for_status (h, cont, cont_cls, timeout);
805 /* end of datastore_api.c */