2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009, 2010, 2011 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 3, 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. Implements
24 * a priority queue for requests (with timeouts).
25 * @author Christian Grothoff
28 #include "gnunet_arm_service.h"
29 #include "gnunet_constants.h"
30 #include "gnunet_datastore_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "datastore.h"
35 * If a client stopped asking for more results, how many more do
36 * we receive from the DB before killing the connection? Trade-off
37 * between re-doing TCP handshakes and (needlessly) receiving
40 #define MAX_EXCESS_RESULTS 8
43 * Context for processing status messages.
48 * Continuation to call with the status.
50 GNUNET_DATASTORE_ContinuationWithStatus cont;
61 * Context for processing result messages.
66 * Function to call with the result.
68 GNUNET_DATASTORE_DatumProcessor proc;
79 * Context for a queue operation.
84 struct StatusContext sc;
86 struct ResultContext rc;
93 * Entry in our priority queue.
95 struct GNUNET_DATASTORE_QueueEntry
99 * This is a linked list.
101 struct GNUNET_DATASTORE_QueueEntry *next;
104 * This is a linked list.
106 struct GNUNET_DATASTORE_QueueEntry *prev;
109 * Handle to the master context.
111 struct GNUNET_DATASTORE_Handle *h;
114 * Response processor (NULL if we are not waiting for a response).
115 * This struct should be used for the closure, function-specific
116 * arguments can be passed via 'qc'.
118 GNUNET_CLIENT_MessageHandler response_proc;
121 * Function to call after transmission of the request.
123 GNUNET_DATASTORE_ContinuationWithStatus cont;
126 * Closure for 'cont'.
131 * Context for the operation.
133 union QueueContext qc;
136 * Task for timeout signalling.
138 GNUNET_SCHEDULER_TaskIdentifier task;
141 * Timeout for the current operation.
143 struct GNUNET_TIME_Absolute timeout;
146 * Priority in the queue.
148 unsigned int priority;
151 * Maximum allowed length of queue (otherwise
152 * this request should be discarded).
154 unsigned int max_queue;
157 * Number of bytes in the request message following
158 * this struct. 32-bit value for nicer memory
159 * access (and overall struct alignment).
161 uint32_t message_size;
164 * Has this message been transmitted to the service?
165 * Only ever GNUNET_YES for the head of the queue.
166 * Note that the overall struct should end at a
167 * multiple of 64 bits.
174 * Handle to the datastore service.
176 struct GNUNET_DATASTORE_Handle
182 const struct GNUNET_CONFIGURATION_Handle *cfg;
185 * Current connection to the datastore service.
187 struct GNUNET_CLIENT_Connection *client;
190 * Handle for statistics.
192 struct GNUNET_STATISTICS_Handle *stats;
195 * Current transmit handle.
197 struct GNUNET_CLIENT_TransmitHandle *th;
200 * Current head of priority queue.
202 struct GNUNET_DATASTORE_QueueEntry *queue_head;
205 * Current tail of priority queue.
207 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
210 * Task for trying to reconnect.
212 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
215 * How quickly should we retry? Used for exponential back-off on
218 struct GNUNET_TIME_Relative retry_time;
221 * Number of entries in the queue.
223 unsigned int queue_size;
226 * Number of results we're receiving for the current query
227 * after application stopped to care. Used to determine when
228 * to reset the connection.
230 unsigned int result_count;
233 * Are we currently trying to receive from the service?
238 * We should ignore the next message(s) from the service.
240 unsigned int skip_next_messages;
247 * Connect to the datastore service.
249 * @param cfg configuration to use
250 * @return handle to use to access the service
252 struct GNUNET_DATASTORE_Handle *
253 GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
255 struct GNUNET_CLIENT_Connection *c;
256 struct GNUNET_DATASTORE_Handle *h;
258 c = GNUNET_CLIENT_connect ("datastore", cfg);
260 return NULL; /* oops */
261 h = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_Handle) +
262 GNUNET_SERVER_MAX_MESSAGE_SIZE - 1);
265 h->stats = GNUNET_STATISTICS_create ("datastore-api", cfg);
271 * Transmit DROP message to datastore service.
273 * @param cls the 'struct GNUNET_DATASTORE_Handle'
274 * @param size number of bytes that can be copied to buf
275 * @param buf where to copy the drop message
276 * @return number of bytes written to buf
279 transmit_drop (void *cls, size_t size, void *buf)
281 struct GNUNET_DATASTORE_Handle *h = cls;
282 struct GNUNET_MessageHeader *hdr;
286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287 _("Failed to transmit request to drop database.\n"));
288 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
291 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
293 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
294 hdr->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
295 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
296 return sizeof (struct GNUNET_MessageHeader);
301 * Disconnect from the datastore service (and free
302 * associated resources).
304 * @param h handle to the datastore
305 * @param drop set to GNUNET_YES to delete all data in datastore (!)
308 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h, int drop)
310 struct GNUNET_DATASTORE_QueueEntry *qe;
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Datastore disconnect\n");
317 GNUNET_CLIENT_notify_transmit_ready_cancel (h->th);
320 if (h->client != NULL)
322 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
325 if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
327 GNUNET_SCHEDULER_cancel (h->reconnect_task);
328 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
330 while (NULL != (qe = h->queue_head))
332 GNUNET_assert (NULL != qe->response_proc);
333 qe->response_proc (h, NULL);
335 if (GNUNET_YES == drop)
337 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
338 if (h->client != NULL)
341 GNUNET_CLIENT_notify_transmit_ready (h->client,
343 GNUNET_MessageHeader),
344 GNUNET_TIME_UNIT_MINUTES,
345 GNUNET_YES, &transmit_drop, h))
347 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
352 GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
359 * A request has timed out (before being transmitted to the service).
361 * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
362 * @param tc scheduler context
365 timeout_queue_entry (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
367 struct GNUNET_DATASTORE_QueueEntry *qe = cls;
369 GNUNET_STATISTICS_update (qe->h->stats,
370 gettext_noop ("# queue entry timeouts"),
372 qe->task = GNUNET_SCHEDULER_NO_TASK;
373 GNUNET_assert (qe->was_transmitted == GNUNET_NO);
375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
376 "Timeout of request in datastore queue\n");
378 qe->response_proc (qe->h, NULL);
383 * Create a new entry for our priority queue (and possibly discard other entires if
384 * the queue is getting too long).
386 * @param h handle to the datastore
387 * @param msize size of the message to queue
388 * @param queue_priority priority of the entry
389 * @param max_queue_size at what queue size should this request be dropped
390 * (if other requests of higher priority are in the queue)
391 * @param timeout timeout for the operation
392 * @param response_proc function to call with replies (can be NULL)
393 * @param qc client context (NOT a closure for response_proc)
394 * @return NULL if the queue is full
396 static struct GNUNET_DATASTORE_QueueEntry *
397 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
399 unsigned int queue_priority,
400 unsigned int max_queue_size,
401 struct GNUNET_TIME_Relative timeout,
402 GNUNET_CLIENT_MessageHandler response_proc,
403 const union QueueContext *qc)
405 struct GNUNET_DATASTORE_QueueEntry *ret;
406 struct GNUNET_DATASTORE_QueueEntry *pos;
411 while ((pos != NULL) &&
412 (c < max_queue_size) && (pos->priority >= queue_priority))
417 if (c >= max_queue_size)
419 GNUNET_STATISTICS_update (h->stats,
420 gettext_noop ("# queue overflows"), 1, GNUNET_NO);
423 ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
425 ret->response_proc = response_proc;
427 ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
428 ret->priority = queue_priority;
429 ret->max_queue = max_queue_size;
430 ret->message_size = msize;
431 ret->was_transmitted = GNUNET_NO;
434 /* append at the tail */
440 /* do not insert at HEAD if HEAD query was already
441 * transmitted and we are still receiving replies! */
442 if ((pos == NULL) && (h->queue_head->was_transmitted))
446 GNUNET_STATISTICS_update (h->stats,
447 gettext_noop ("# queue entries created"),
449 GNUNET_CONTAINER_DLL_insert_after (h->queue_head, h->queue_tail, pos, ret);
451 ret->task = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_queue_entry, ret);
455 if ((pos->max_queue < h->queue_size) && (pos->was_transmitted == GNUNET_NO))
457 GNUNET_assert (pos->response_proc != NULL);
458 /* move 'pos' element to head so that it will be
459 * killed on 'NULL' call below */
461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
462 "Dropping request from datastore queue\n");
464 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, pos);
465 GNUNET_CONTAINER_DLL_insert (h->queue_head, h->queue_tail, pos);
466 GNUNET_STATISTICS_update (h->stats,
468 ("# Requests dropped from datastore queue"), 1,
470 GNUNET_assert (h->queue_head == pos);
471 pos->response_proc (h, NULL);
481 * Process entries in the queue (or do nothing if we are already
484 * @param h handle to the datastore
486 static void process_queue (struct GNUNET_DATASTORE_Handle *h);
490 * Try reconnecting to the datastore service.
492 * @param cls the 'struct GNUNET_DATASTORE_Handle'
493 * @param tc scheduler context
496 try_reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
498 struct GNUNET_DATASTORE_Handle *h = cls;
500 if (h->retry_time.rel_value < GNUNET_CONSTANTS_SERVICE_RETRY.rel_value)
501 h->retry_time = GNUNET_CONSTANTS_SERVICE_RETRY;
503 h->retry_time = GNUNET_TIME_relative_multiply (h->retry_time, 2);
504 if (h->retry_time.rel_value > GNUNET_CONSTANTS_SERVICE_TIMEOUT.rel_value)
505 h->retry_time = GNUNET_CONSTANTS_SERVICE_TIMEOUT;
506 h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
507 h->client = GNUNET_CLIENT_connect ("datastore", h->cfg);
508 if (h->client == NULL)
510 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
511 "DATASTORE reconnect failed (fatally)\n");
514 GNUNET_STATISTICS_update (h->stats,
516 ("# datastore connections (re)created"), 1,
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reconnected to DATASTORE\n");
526 * Disconnect from the service and then try reconnecting to the datastore service
529 * @param h handle to datastore to disconnect and reconnect
532 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
534 if (h->client == NULL)
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
538 "client NULL in disconnect, will not try to reconnect\n");
543 GNUNET_STATISTICS_update (stats,
544 gettext_noop ("# reconnected to DATASTORE"),
547 GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
548 h->skip_next_messages = 0;
550 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->retry_time,
556 * Function called whenever we receive a message from
557 * the service. Calls the appropriate handler.
559 * @param cls the 'struct GNUNET_DATASTORE_Handle'
560 * @param msg the received message
563 receive_cb (void *cls, const struct GNUNET_MessageHeader *msg)
565 struct GNUNET_DATASTORE_Handle *h = cls;
566 struct GNUNET_DATASTORE_QueueEntry *qe;
568 h->in_receive = GNUNET_NO;
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving reply from datastore\n");
572 if (h->skip_next_messages > 0)
574 h->skip_next_messages--;
578 if (NULL == (qe = h->queue_head))
584 qe->response_proc (h, msg);
589 * Transmit request from queue to datastore service.
591 * @param cls the 'struct GNUNET_DATASTORE_Handle'
592 * @param size number of bytes that can be copied to buf
593 * @param buf where to copy the drop message
594 * @return number of bytes written to buf
597 transmit_request (void *cls, size_t size, void *buf)
599 struct GNUNET_DATASTORE_Handle *h = cls;
600 struct GNUNET_DATASTORE_QueueEntry *qe;
604 if (NULL == (qe = h->queue_head))
605 return 0; /* no entry in queue */
608 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
609 _("Failed to transmit request to DATASTORE.\n"));
610 GNUNET_STATISTICS_update (h->stats,
611 gettext_noop ("# transmission request failures"),
616 if (size < (msize = qe->message_size))
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
623 "Transmitting %u byte request to DATASTORE\n", msize);
625 memcpy (buf, &qe[1], msize);
626 qe->was_transmitted = GNUNET_YES;
627 GNUNET_SCHEDULER_cancel (qe->task);
628 qe->task = GNUNET_SCHEDULER_NO_TASK;
629 GNUNET_assert (GNUNET_NO == h->in_receive);
630 h->in_receive = GNUNET_YES;
631 GNUNET_CLIENT_receive (h->client,
633 h, GNUNET_TIME_absolute_get_remaining (qe->timeout));
634 GNUNET_STATISTICS_update (h->stats,
635 gettext_noop ("# bytes sent to datastore"),
642 * Process entries in the queue (or do nothing if we are already
645 * @param h handle to the datastore
648 process_queue (struct GNUNET_DATASTORE_Handle *h)
650 struct GNUNET_DATASTORE_QueueEntry *qe;
652 if (NULL == (qe = h->queue_head))
654 #if DEBUG_DATASTORE > 1
655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Queue empty\n");
657 return; /* no entry in queue */
659 if (qe->was_transmitted == GNUNET_YES)
661 #if DEBUG_DATASTORE > 1
662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Head request already transmitted\n");
664 return; /* waiting for replies */
668 #if DEBUG_DATASTORE > 1
669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Pending transmission request\n");
671 return; /* request pending */
673 if (h->client == NULL)
675 #if DEBUG_DATASTORE > 1
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not connected\n");
678 return; /* waiting for reconnect */
680 if (GNUNET_YES == h->in_receive)
682 /* wait for response to previous query */
686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
687 "Queueing %u byte request to DATASTORE\n", qe->message_size);
689 h->th = GNUNET_CLIENT_notify_transmit_ready (h->client,
691 GNUNET_TIME_absolute_get_remaining
692 (qe->timeout), GNUNET_YES,
693 &transmit_request, h);
694 GNUNET_assert (GNUNET_NO == h->in_receive);
695 GNUNET_break (NULL != h->th);
700 * Dummy continuation used to do nothing (but be non-zero).
703 * @param result result
704 * @param emsg error message
707 drop_status_cont (void *cls, int32_t result, const char *emsg)
714 * Free a queue entry. Removes the given entry from the
715 * queue and releases associated resources. Does NOT
718 * @param qe entry to free.
721 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
723 struct GNUNET_DATASTORE_Handle *h = qe->h;
725 GNUNET_CONTAINER_DLL_remove (h->queue_head, h->queue_tail, qe);
726 if (qe->task != GNUNET_SCHEDULER_NO_TASK)
728 GNUNET_SCHEDULER_cancel (qe->task);
729 qe->task = GNUNET_SCHEDULER_NO_TASK;
732 qe->was_transmitted = GNUNET_SYSERR; /* use-after-free warning */
738 * Type of a function to call when we receive a message
742 * @param msg message received, NULL on timeout or fatal error
745 process_status_message (void *cls, const struct GNUNET_MessageHeader *msg)
747 struct GNUNET_DATASTORE_Handle *h = cls;
748 struct GNUNET_DATASTORE_QueueEntry *qe;
749 struct StatusContext rc;
750 const struct StatusMessage *sm;
755 if (NULL == (qe = h->queue_head))
764 was_transmitted = qe->was_transmitted;
765 free_queue_entry (qe);
766 if (was_transmitted == GNUNET_YES)
771 rc.cont (rc.cont_cls,
773 _("Failed to receive status response from database."));
776 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
777 free_queue_entry (qe);
778 if ((ntohs (msg->size) < sizeof (struct StatusMessage)) ||
779 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_STATUS))
782 h->retry_time = GNUNET_TIME_UNIT_ZERO;
785 rc.cont (rc.cont_cls,
787 _("Error reading response from datastore service"));
790 sm = (const struct StatusMessage *) msg;
791 status = ntohl (sm->status);
793 if (ntohs (msg->size) > sizeof (struct StatusMessage))
795 emsg = (const char *) &sm[1];
796 if (emsg[ntohs (msg->size) - sizeof (struct StatusMessage) - 1] != '\0')
799 emsg = _("Invalid error message received from datastore service");
802 if ((status == GNUNET_SYSERR) && (emsg == NULL))
805 emsg = _("Invalid error message received from datastore service");
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Received status %d/%s\n", (int) status, emsg);
811 GNUNET_STATISTICS_update (h->stats,
812 gettext_noop ("# status messages received"),
814 h->retry_time.rel_value = 0;
817 rc.cont (rc.cont_cls, status, emsg);
822 * Store an item in the datastore. If the item is already present,
823 * the priorities are summed up and the higher expiration time and
824 * lower anonymity level is used.
826 * @param h handle to the datastore
827 * @param rid reservation ID to use (from "reserve"); use 0 if no
828 * prior reservation was made
829 * @param key key for the value
830 * @param size number of bytes in data
831 * @param data content stored
832 * @param type type of the content
833 * @param priority priority of the content
834 * @param anonymity anonymity-level for the content
835 * @param replication how often should the content be replicated to other peers?
836 * @param expiration expiration time for the content
837 * @param queue_priority ranking of this request in the priority queue
838 * @param max_queue_size at what queue size should this request be dropped
839 * (if other requests of higher priority are in the queue)
840 * @param timeout timeout for the operation
841 * @param cont continuation to call when done
842 * @param cont_cls closure for cont
843 * @return NULL if the entry was not queued, otherwise a handle that can be used to
844 * cancel; note that even if NULL is returned, the callback will be invoked
845 * (or rather, will already have been invoked)
847 struct GNUNET_DATASTORE_QueueEntry *
848 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
850 const GNUNET_HashCode * key,
853 enum GNUNET_BLOCK_Type type,
856 uint32_t replication,
857 struct GNUNET_TIME_Absolute expiration,
858 unsigned int queue_priority,
859 unsigned int max_queue_size,
860 struct GNUNET_TIME_Relative timeout,
861 GNUNET_DATASTORE_ContinuationWithStatus cont,
864 struct GNUNET_DATASTORE_QueueEntry *qe;
865 struct DataMessage *dm;
867 union QueueContext qc;
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871 "Asked to put %u bytes of data under key `%s' for %llu ms\n",
874 GNUNET_TIME_absolute_get_remaining (expiration).rel_value);
876 msize = sizeof (struct DataMessage) + size;
877 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
879 qc.sc.cont_cls = cont_cls;
880 qe = make_queue_entry (h, msize,
881 queue_priority, max_queue_size, timeout,
882 &process_status_message, &qc);
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
887 "Could not create queue entry for PUT\n");
891 GNUNET_STATISTICS_update (h->stats,
892 gettext_noop ("# PUT requests executed"),
894 dm = (struct DataMessage *) &qe[1];
895 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
896 dm->header.size = htons (msize);
897 dm->rid = htonl (rid);
898 dm->size = htonl ((uint32_t) size);
899 dm->type = htonl (type);
900 dm->priority = htonl (priority);
901 dm->anonymity = htonl (anonymity);
902 dm->replication = htonl (replication);
903 dm->reserved = htonl (0);
904 dm->uid = GNUNET_htonll (0);
905 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
907 memcpy (&dm[1], data, size);
914 * Reserve space in the datastore. This function should be used
915 * to avoid "out of space" failures during a longer sequence of "put"
916 * operations (for example, when a file is being inserted).
918 * @param h handle to the datastore
919 * @param amount how much space (in bytes) should be reserved (for content only)
920 * @param entries how many entries will be created (to calculate per-entry overhead)
921 * @param queue_priority ranking of this request in the priority queue
922 * @param max_queue_size at what queue size should this request be dropped
923 * (if other requests of higher priority are in the queue)
924 * @param timeout how long to wait at most for a response (or before dying in queue)
925 * @param cont continuation to call when done; "success" will be set to
926 * a positive reservation value if space could be reserved.
927 * @param cont_cls closure for cont
928 * @return NULL if the entry was not queued, otherwise a handle that can be used to
929 * cancel; note that even if NULL is returned, the callback will be invoked
930 * (or rather, will already have been invoked)
932 struct GNUNET_DATASTORE_QueueEntry *
933 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
936 unsigned int queue_priority,
937 unsigned int max_queue_size,
938 struct GNUNET_TIME_Relative timeout,
939 GNUNET_DATASTORE_ContinuationWithStatus cont,
942 struct GNUNET_DATASTORE_QueueEntry *qe;
943 struct ReserveMessage *rm;
944 union QueueContext qc;
947 cont = &drop_status_cont;
949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
950 "Asked to reserve %llu bytes of data and %u entries\n",
951 (unsigned long long) amount, (unsigned int) entries);
954 qc.sc.cont_cls = cont_cls;
955 qe = make_queue_entry (h, sizeof (struct ReserveMessage),
956 queue_priority, max_queue_size, timeout,
957 &process_status_message, &qc);
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Could not create queue entry to reserve\n");
966 GNUNET_STATISTICS_update (h->stats,
967 gettext_noop ("# RESERVE requests executed"),
969 rm = (struct ReserveMessage *) &qe[1];
970 rm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
971 rm->header.size = htons (sizeof (struct ReserveMessage));
972 rm->entries = htonl (entries);
973 rm->amount = GNUNET_htonll (amount);
980 * Signal that all of the data for which a reservation was made has
981 * been stored and that whatever excess space might have been reserved
982 * can now be released.
984 * @param h handle to the datastore
985 * @param rid reservation ID (value of "success" in original continuation
986 * from the "reserve" function).
987 * @param queue_priority ranking of this request in the priority queue
988 * @param max_queue_size at what queue size should this request be dropped
989 * (if other requests of higher priority are in the queue)
990 * @param queue_priority ranking of this request in the priority queue
991 * @param max_queue_size at what queue size should this request be dropped
992 * (if other requests of higher priority are in the queue)
993 * @param timeout how long to wait at most for a response
994 * @param cont continuation to call when done
995 * @param cont_cls closure for cont
996 * @return NULL if the entry was not queued, otherwise a handle that can be used to
997 * cancel; note that even if NULL is returned, the callback will be invoked
998 * (or rather, will already have been invoked)
1000 struct GNUNET_DATASTORE_QueueEntry *
1001 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
1003 unsigned int queue_priority,
1004 unsigned int max_queue_size,
1005 struct GNUNET_TIME_Relative timeout,
1006 GNUNET_DATASTORE_ContinuationWithStatus cont,
1009 struct GNUNET_DATASTORE_QueueEntry *qe;
1010 struct ReleaseReserveMessage *rrm;
1011 union QueueContext qc;
1014 cont = &drop_status_cont;
1016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to release reserve %d\n", rid);
1019 qc.sc.cont_cls = cont_cls;
1020 qe = make_queue_entry (h, sizeof (struct ReleaseReserveMessage),
1021 queue_priority, max_queue_size, timeout,
1022 &process_status_message, &qc);
1026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1027 "Could not create queue entry to release reserve\n");
1031 GNUNET_STATISTICS_update (h->stats,
1033 ("# RELEASE RESERVE requests executed"), 1,
1035 rrm = (struct ReleaseReserveMessage *) &qe[1];
1036 rrm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1037 rrm->header.size = htons (sizeof (struct ReleaseReserveMessage));
1038 rrm->rid = htonl (rid);
1045 * Update a value in the datastore.
1047 * @param h handle to the datastore
1048 * @param uid identifier for the value
1049 * @param priority how much to increase the priority of the value
1050 * @param expiration new expiration value should be MAX of existing and this argument
1051 * @param queue_priority ranking of this request in the priority queue
1052 * @param max_queue_size at what queue size should this request be dropped
1053 * (if other requests of higher priority are in the queue)
1054 * @param timeout how long to wait at most for a response
1055 * @param cont continuation to call when done
1056 * @param cont_cls closure for cont
1057 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1058 * cancel; note that even if NULL is returned, the callback will be invoked
1059 * (or rather, will already have been invoked)
1061 struct GNUNET_DATASTORE_QueueEntry *
1062 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
1065 struct GNUNET_TIME_Absolute expiration,
1066 unsigned int queue_priority,
1067 unsigned int max_queue_size,
1068 struct GNUNET_TIME_Relative timeout,
1069 GNUNET_DATASTORE_ContinuationWithStatus cont,
1072 struct GNUNET_DATASTORE_QueueEntry *qe;
1073 struct UpdateMessage *um;
1074 union QueueContext qc;
1077 cont = &drop_status_cont;
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "Asked to update entry %llu raising priority by %u and expiration to %llu\n",
1082 (unsigned int) priority,
1083 (unsigned long long) expiration.abs_value);
1086 qc.sc.cont_cls = cont_cls;
1087 qe = make_queue_entry (h, sizeof (struct UpdateMessage),
1088 queue_priority, max_queue_size, timeout,
1089 &process_status_message, &qc);
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "Could not create queue entry for UPDATE\n");
1098 GNUNET_STATISTICS_update (h->stats,
1099 gettext_noop ("# UPDATE requests executed"),
1101 um = (struct UpdateMessage *) &qe[1];
1102 um->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
1103 um->header.size = htons (sizeof (struct UpdateMessage));
1104 um->priority = htonl (priority);
1105 um->expiration = GNUNET_TIME_absolute_hton (expiration);
1106 um->uid = GNUNET_htonll (uid);
1113 * Explicitly remove some content from the database.
1114 * The "cont"inuation will be called with status
1115 * "GNUNET_OK" if content was removed, "GNUNET_NO"
1116 * if no matching entry was found and "GNUNET_SYSERR"
1117 * on all other types of errors.
1119 * @param h handle to the datastore
1120 * @param key key for the value
1121 * @param size number of bytes in data
1122 * @param data content stored
1123 * @param queue_priority ranking of this request in the priority queue
1124 * @param max_queue_size at what queue size should this request be dropped
1125 * (if other requests of higher priority are in the queue)
1126 * @param timeout how long to wait at most for a response
1127 * @param cont continuation to call when done
1128 * @param cont_cls closure for cont
1129 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1130 * cancel; note that even if NULL is returned, the callback will be invoked
1131 * (or rather, will already have been invoked)
1133 struct GNUNET_DATASTORE_QueueEntry *
1134 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1135 const GNUNET_HashCode * key,
1138 unsigned int queue_priority,
1139 unsigned int max_queue_size,
1140 struct GNUNET_TIME_Relative timeout,
1141 GNUNET_DATASTORE_ContinuationWithStatus cont,
1144 struct GNUNET_DATASTORE_QueueEntry *qe;
1145 struct DataMessage *dm;
1147 union QueueContext qc;
1150 cont = &drop_status_cont;
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Asked to remove %u bytes under key `%s'\n",
1154 size, GNUNET_h2s (key));
1157 qc.sc.cont_cls = cont_cls;
1158 msize = sizeof (struct DataMessage) + size;
1159 GNUNET_assert (msize < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1160 qe = make_queue_entry (h, msize,
1161 queue_priority, max_queue_size, timeout,
1162 &process_status_message, &qc);
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "Could not create queue entry for REMOVE\n");
1171 GNUNET_STATISTICS_update (h->stats,
1172 gettext_noop ("# REMOVE requests executed"),
1174 dm = (struct DataMessage *) &qe[1];
1175 dm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
1176 dm->header.size = htons (msize);
1177 dm->rid = htonl (0);
1178 dm->size = htonl (size);
1179 dm->type = htonl (0);
1180 dm->priority = htonl (0);
1181 dm->anonymity = htonl (0);
1182 dm->uid = GNUNET_htonll (0);
1183 dm->expiration = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_ZERO_ABS);
1185 memcpy (&dm[1], data, size);
1192 * Type of a function to call when we receive a message
1195 * @param cls closure
1196 * @param msg message received, NULL on timeout or fatal error
1199 process_result_message (void *cls, const struct GNUNET_MessageHeader *msg)
1201 struct GNUNET_DATASTORE_Handle *h = cls;
1202 struct GNUNET_DATASTORE_QueueEntry *qe;
1203 struct ResultContext rc;
1204 const struct DataMessage *dm;
1205 int was_transmitted;
1210 GNUNET_assert (NULL != qe);
1212 was_transmitted = qe->was_transmitted;
1213 free_queue_entry (qe);
1214 if (was_transmitted == GNUNET_YES)
1216 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1217 _("Failed to receive response from database.\n"));
1224 if (rc.proc != NULL)
1225 rc.proc (rc.proc_cls,
1226 NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1229 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END)
1231 GNUNET_break (ntohs (msg->size) == sizeof (struct GNUNET_MessageHeader));
1234 GNUNET_assert (GNUNET_YES == qe->was_transmitted);
1235 free_queue_entry (qe);
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "Received end of result set, new queue size is %u\n",
1241 if (rc.proc != NULL)
1242 rc.proc (rc.proc_cls,
1243 NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1244 h->retry_time.rel_value = 0;
1245 h->result_count = 0;
1250 GNUNET_assert (NULL != qe);
1252 if (GNUNET_YES != qe->was_transmitted)
1255 free_queue_entry (qe);
1256 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1258 if (rc.proc != NULL)
1259 rc.proc (rc.proc_cls,
1260 NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1263 if ((ntohs (msg->size) < sizeof (struct DataMessage)) ||
1264 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DATASTORE_DATA) ||
1265 (ntohs (msg->size) !=
1266 sizeof (struct DataMessage) +
1267 ntohl (((const struct DataMessage *) msg)->size)))
1270 free_queue_entry (qe);
1271 h->retry_time = GNUNET_TIME_UNIT_ZERO;
1273 if (rc.proc != NULL)
1274 rc.proc (rc.proc_cls,
1275 NULL, 0, NULL, 0, 0, 0, GNUNET_TIME_UNIT_ZERO_ABS, 0);
1278 GNUNET_STATISTICS_update (h->stats,
1279 gettext_noop ("# Results received"), 1, GNUNET_NO);
1280 dm = (const struct DataMessage *) msg;
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283 "Received result %llu with type %u and size %u with key %s\n",
1284 (unsigned long long) GNUNET_ntohll (dm->uid),
1285 ntohl (dm->type), ntohl (dm->size), GNUNET_h2s (&dm->key));
1287 free_queue_entry (qe);
1288 h->retry_time.rel_value = 0;
1290 if (rc.proc != NULL)
1291 rc.proc (rc.proc_cls,
1296 ntohl (dm->priority),
1297 ntohl (dm->anonymity),
1298 GNUNET_TIME_absolute_ntoh (dm->expiration),
1299 GNUNET_ntohll (dm->uid));
1304 * Get a random value from the datastore for content replication.
1305 * Returns a single, random value among those with the highest
1306 * replication score, lowering positive replication scores by one for
1307 * the chosen value (if only content with a replication score exists,
1308 * a random value is returned and replication scores are not changed).
1310 * @param h handle to the datastore
1311 * @param queue_priority ranking of this request in the priority queue
1312 * @param max_queue_size at what queue size should this request be dropped
1313 * (if other requests of higher priority are in the queue)
1314 * @param timeout how long to wait at most for a response
1315 * @param proc function to call on a random value; it
1316 * will be called once with a value (if available)
1317 * and always once with a value of NULL.
1318 * @param proc_cls closure for proc
1319 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1322 struct GNUNET_DATASTORE_QueueEntry *
1323 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1324 unsigned int queue_priority,
1325 unsigned int max_queue_size,
1326 struct GNUNET_TIME_Relative timeout,
1327 GNUNET_DATASTORE_DatumProcessor proc,
1330 struct GNUNET_DATASTORE_QueueEntry *qe;
1331 struct GNUNET_MessageHeader *m;
1332 union QueueContext qc;
1334 GNUNET_assert (NULL != proc);
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 "Asked to get replication entry in %llu ms\n",
1338 (unsigned long long) timeout.rel_value);
1341 qc.rc.proc_cls = proc_cls;
1342 qe = make_queue_entry (h, sizeof (struct GNUNET_MessageHeader),
1343 queue_priority, max_queue_size, timeout,
1344 &process_result_message, &qc);
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "Could not create queue entry for GET REPLICATION\n");
1353 GNUNET_STATISTICS_update (h->stats,
1355 ("# GET REPLICATION requests executed"), 1,
1357 m = (struct GNUNET_MessageHeader *) &qe[1];
1358 m->type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1359 m->size = htons (sizeof (struct GNUNET_MessageHeader));
1366 * Get a single zero-anonymity value from the datastore.
1368 * @param h handle to the datastore
1369 * @param offset offset of the result (modulo num-results); set to
1370 * a random 64-bit value initially; then increment by
1371 * one each time; detect that all results have been found by uid
1372 * being again the first uid ever returned.
1373 * @param queue_priority ranking of this request in the priority queue
1374 * @param max_queue_size at what queue size should this request be dropped
1375 * (if other requests of higher priority are in the queue)
1376 * @param timeout how long to wait at most for a response
1377 * @param type allowed type for the operation (never zero)
1378 * @param proc function to call on a random value; it
1379 * will be called once with a value (if available)
1380 * or with NULL if none value exists.
1381 * @param proc_cls closure for proc
1382 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1385 struct GNUNET_DATASTORE_QueueEntry *
1386 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1388 unsigned int queue_priority,
1389 unsigned int max_queue_size,
1390 struct GNUNET_TIME_Relative timeout,
1391 enum GNUNET_BLOCK_Type type,
1392 GNUNET_DATASTORE_DatumProcessor proc,
1395 struct GNUNET_DATASTORE_QueueEntry *qe;
1396 struct GetZeroAnonymityMessage *m;
1397 union QueueContext qc;
1399 GNUNET_assert (NULL != proc);
1400 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Asked to get %llu-th zero-anonymity entry of type %d in %llu ms\n",
1404 (unsigned long long) offset,
1405 type, (unsigned long long) timeout.rel_value);
1408 qc.rc.proc_cls = proc_cls;
1409 qe = make_queue_entry (h, sizeof (struct GetZeroAnonymityMessage),
1410 queue_priority, max_queue_size, timeout,
1411 &process_result_message, &qc);
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1416 "Could not create queue entry for zero-anonymity procation\n");
1420 GNUNET_STATISTICS_update (h->stats,
1422 ("# GET ZERO ANONYMITY requests executed"), 1,
1424 m = (struct GetZeroAnonymityMessage *) &qe[1];
1425 m->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1426 m->header.size = htons (sizeof (struct GetZeroAnonymityMessage));
1427 m->type = htonl ((uint32_t) type);
1428 m->offset = GNUNET_htonll (offset);
1435 * Get a result for a particular key from the datastore. The processor
1436 * will only be called once.
1438 * @param h handle to the datastore
1439 * @param offset offset of the result (modulo num-results); set to
1440 * a random 64-bit value initially; then increment by
1441 * one each time; detect that all results have been found by uid
1442 * being again the first uid ever returned.
1443 * @param key maybe NULL (to match all entries)
1444 * @param type desired type, 0 for any
1445 * @param queue_priority ranking of this request in the priority queue
1446 * @param max_queue_size at what queue size should this request be dropped
1447 * (if other requests of higher priority are in the queue)
1448 * @param timeout how long to wait at most for a response
1449 * @param proc function to call on each matching value;
1450 * will be called once with a NULL value at the end
1451 * @param proc_cls closure for proc
1452 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1455 struct GNUNET_DATASTORE_QueueEntry *
1456 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
1458 const GNUNET_HashCode * key,
1459 enum GNUNET_BLOCK_Type type,
1460 unsigned int queue_priority,
1461 unsigned int max_queue_size,
1462 struct GNUNET_TIME_Relative timeout,
1463 GNUNET_DATASTORE_DatumProcessor proc, void *proc_cls)
1465 struct GNUNET_DATASTORE_QueueEntry *qe;
1466 struct GetMessage *gm;
1467 union QueueContext qc;
1469 GNUNET_assert (NULL != proc);
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Asked to look for data of type %u under key `%s'\n",
1473 (unsigned int) type, GNUNET_h2s (key));
1476 qc.rc.proc_cls = proc_cls;
1477 qe = make_queue_entry (h, sizeof (struct GetMessage),
1478 queue_priority, max_queue_size, timeout,
1479 &process_result_message, &qc);
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1484 "Could not queue request for `%s'\n", GNUNET_h2s (key));
1488 GNUNET_STATISTICS_update (h->stats,
1489 gettext_noop ("# GET requests executed"),
1491 gm = (struct GetMessage *) &qe[1];
1492 gm->header.type = htons (GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1493 gm->type = htonl (type);
1494 gm->offset = GNUNET_htonll (offset);
1497 gm->header.size = htons (sizeof (struct GetMessage));
1503 htons (sizeof (struct GetMessage) - sizeof (GNUNET_HashCode));
1511 * Cancel a datastore operation. The final callback from the
1512 * operation must not have been done yet.
1514 * @param qe operation to cancel
1517 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1519 struct GNUNET_DATASTORE_Handle *h;
1521 GNUNET_assert (GNUNET_SYSERR != qe->was_transmitted);
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1526 qe, qe->was_transmitted, h->queue_head == qe);
1528 if (GNUNET_YES == qe->was_transmitted)
1530 free_queue_entry (qe);
1531 h->skip_next_messages++;
1534 free_queue_entry (qe);
1539 /* end of datastore_api.c */