2 This file is part of GNUnet
3 Copyright (C) 2004-2013, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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
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"
34 #define LOG(kind,...) GNUNET_log_from (kind, "datastore-api",__VA_ARGS__)
37 * Collect an instane number of statistics? May cause excessive IPC.
39 #define INSANE_STATISTICS GNUNET_NO
42 * If a client stopped asking for more results, how many more do
43 * we receive from the DB before killing the connection? Trade-off
44 * between re-doing TCP handshakes and (needlessly) receiving
47 #define MAX_EXCESS_RESULTS 8
50 * Context for processing status messages.
55 * Continuation to call with the status.
57 GNUNET_DATASTORE_ContinuationWithStatus cont;
60 * Closure for @e cont.
68 * Context for processing result messages.
73 * Function to call with the result.
75 GNUNET_DATASTORE_DatumProcessor proc;
78 * Closure for @e proc.
86 * Context for a queue operation.
91 struct StatusContext sc;
93 struct ResultContext rc;
99 * Entry in our priority queue.
101 struct GNUNET_DATASTORE_QueueEntry
105 * This is a linked list.
107 struct GNUNET_DATASTORE_QueueEntry *next;
110 * This is a linked list.
112 struct GNUNET_DATASTORE_QueueEntry *prev;
115 * Handle to the master context.
117 struct GNUNET_DATASTORE_Handle *h;
120 * Function to call after transmission of the request.
122 GNUNET_DATASTORE_ContinuationWithStatus cont;
125 * Closure for @e cont.
130 * Context for the operation.
132 union QueueContext qc;
135 * Envelope of the request to transmit, NULL after
138 struct GNUNET_MQ_Envelope *env;
141 * Priority in the queue.
143 unsigned int priority;
146 * Maximum allowed length of queue (otherwise
147 * this request should be discarded).
149 unsigned int max_queue;
152 * Expected response type.
154 uint16_t response_type;
160 * Handle to the datastore service.
162 struct GNUNET_DATASTORE_Handle
168 const struct GNUNET_CONFIGURATION_Handle *cfg;
171 * Current connection to the datastore service.
173 struct GNUNET_MQ_Handle *mq;
176 * Handle for statistics.
178 struct GNUNET_STATISTICS_Handle *stats;
181 * Current head of priority queue.
183 struct GNUNET_DATASTORE_QueueEntry *queue_head;
186 * Current tail of priority queue.
188 struct GNUNET_DATASTORE_QueueEntry *queue_tail;
191 * Task for trying to reconnect.
193 struct GNUNET_SCHEDULER_Task *reconnect_task;
196 * How quickly should we retry? Used for exponential back-off on
199 struct GNUNET_TIME_Relative retry_time;
202 * Number of entries in the queue.
204 unsigned int queue_size;
207 * Number of results we're receiving for the current query
208 * after application stopped to care. Used to determine when
209 * to reset the connection.
211 unsigned int result_count;
214 * We should ignore the next message(s) from the service.
216 unsigned int skip_next_messages;
222 * Try reconnecting to the datastore service.
224 * @param cls the `struct GNUNET_DATASTORE_Handle`
227 try_reconnect (void *cls);
231 * Disconnect from the service and then try reconnecting to the datastore service
234 * @param h handle to datastore to disconnect and reconnect
237 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
244 GNUNET_MQ_destroy (h->mq);
246 h->skip_next_messages = 0;
248 = GNUNET_SCHEDULER_add_delayed (h->retry_time,
255 * Free a queue entry. Removes the given entry from the
256 * queue and releases associated resources. Does NOT
259 * @param qe entry to free.
262 free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
264 struct GNUNET_DATASTORE_Handle *h = qe->h;
266 GNUNET_CONTAINER_DLL_remove (h->queue_head,
271 GNUNET_MQ_discard (qe->env);
277 * Handle error in sending drop request to datastore.
279 * @param cls closure with the datastore handle
280 * @param error error code
283 mq_error_handler (void *cls,
284 enum GNUNET_MQ_Error error)
286 struct GNUNET_DATASTORE_Handle *h = cls;
287 struct GNUNET_DATASTORE_QueueEntry *qe;
289 LOG (GNUNET_ERROR_TYPE_DEBUG,
290 "MQ error, reconnecting to DATASTORE\n");
296 union QueueContext qc = qe->qc;
297 uint16_t rt = qe->response_type;
299 LOG (GNUNET_ERROR_TYPE_DEBUG,
300 "Failed to receive response from database.\n");
301 free_queue_entry (qe);
304 case GNUNET_MESSAGE_TYPE_DATASTORE_STATUS:
305 if (NULL != qc.sc.cont)
306 qc.sc.cont (qc.sc.cont_cls,
308 GNUNET_TIME_UNIT_ZERO_ABS,
309 _("DATASTORE disconnected"));
311 case GNUNET_MESSAGE_TYPE_DATASTORE_DATA:
312 if (NULL != qc.rc.proc)
313 qc.rc.proc (qc.rc.proc_cls,
317 GNUNET_TIME_UNIT_ZERO_ABS,
328 * Connect to the datastore service.
330 * @param cfg configuration to use
331 * @return handle to use to access the service
333 struct GNUNET_DATASTORE_Handle *
334 GNUNET_DATASTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
336 struct GNUNET_DATASTORE_Handle *h;
338 LOG (GNUNET_ERROR_TYPE_DEBUG,
339 "Establishing DATASTORE connection!\n");
340 h = GNUNET_new (struct GNUNET_DATASTORE_Handle);
348 h->stats = GNUNET_STATISTICS_create ("datastore-api",
355 * Task used by to disconnect from the datastore after
356 * we send the #GNUNET_MESSAGE_TYPE_DATASTORE_DROP message.
358 * @param cls the datastore handle
361 disconnect_after_drop (void *cls)
363 struct GNUNET_DATASTORE_Handle *h = cls;
365 LOG (GNUNET_ERROR_TYPE_DEBUG,
366 "Drop sent, disconnecting\n");
367 GNUNET_DATASTORE_disconnect (h,
373 * Handle error in sending drop request to datastore.
375 * @param cls closure with the datastore handle
376 * @param error error code
379 disconnect_on_mq_error (void *cls,
380 enum GNUNET_MQ_Error error)
382 struct GNUNET_DATASTORE_Handle *h = cls;
384 LOG (GNUNET_ERROR_TYPE_ERROR,
385 "Failed to ask datastore to drop tables\n");
386 GNUNET_DATASTORE_disconnect (h,
392 * Disconnect from the datastore service (and free
393 * associated resources).
395 * @param h handle to the datastore
396 * @param drop set to #GNUNET_YES to delete all data in datastore (!)
399 GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
402 struct GNUNET_DATASTORE_QueueEntry *qe;
404 LOG (GNUNET_ERROR_TYPE_DEBUG,
405 "Datastore disconnect\n");
408 GNUNET_MQ_destroy (h->mq);
411 if (NULL != h->reconnect_task)
413 GNUNET_SCHEDULER_cancel (h->reconnect_task);
414 h->reconnect_task = NULL;
416 while (NULL != (qe = h->queue_head))
418 switch (qe->response_type)
420 case GNUNET_MESSAGE_TYPE_DATASTORE_STATUS:
421 if (NULL != qe->qc.sc.cont)
422 qe->qc.sc.cont (qe->qc.sc.cont_cls,
424 GNUNET_TIME_UNIT_ZERO_ABS,
425 _("Disconnected from DATASTORE"));
427 case GNUNET_MESSAGE_TYPE_DATASTORE_DATA:
428 if (NULL != qe->qc.rc.proc)
429 qe->qc.rc.proc (qe->qc.rc.proc_cls,
433 GNUNET_TIME_UNIT_ZERO_ABS,
439 free_queue_entry (qe);
441 if (GNUNET_YES == drop)
443 LOG (GNUNET_ERROR_TYPE_DEBUG,
444 "Re-connecting to issue DROP!\n");
445 GNUNET_assert (NULL == h->mq);
446 h->mq = GNUNET_CLIENT_connect (h->cfg,
449 &disconnect_on_mq_error,
453 struct GNUNET_MessageHeader *hdr;
454 struct GNUNET_MQ_Envelope *env;
456 env = GNUNET_MQ_msg (hdr,
457 GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
458 GNUNET_MQ_notify_sent (env,
459 &disconnect_after_drop,
461 GNUNET_MQ_send (h->mq,
467 GNUNET_STATISTICS_destroy (h->stats,
475 * Create a new entry for our priority queue (and possibly discard other entires if
476 * the queue is getting too long).
478 * @param h handle to the datastore
479 * @param env envelope with the message to queue
480 * @param queue_priority priority of the entry
481 * @param max_queue_size at what queue size should this request be dropped
482 * (if other requests of higher priority are in the queue)
483 * @param expected_type which type of response do we expect,
484 * #GNUNET_MESSAGE_TYPE_DATASTORE_STATUS or
485 * #GNUNET_MESSAGE_TYPE_DATASTORE_DATA
486 * @param qc client context (NOT a closure for @a response_proc)
487 * @return NULL if the queue is full
489 static struct GNUNET_DATASTORE_QueueEntry *
490 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
491 struct GNUNET_MQ_Envelope *env,
492 unsigned int queue_priority,
493 unsigned int max_queue_size,
494 uint16_t expected_type,
495 const union QueueContext *qc)
497 struct GNUNET_DATASTORE_QueueEntry *qe;
498 struct GNUNET_DATASTORE_QueueEntry *pos;
503 while ( (NULL != pos) &&
504 (c < max_queue_size) &&
505 (pos->priority >= queue_priority) )
510 if (c >= max_queue_size)
512 GNUNET_STATISTICS_update (h->stats,
513 gettext_noop ("# queue overflows"),
516 GNUNET_MQ_discard (env);
519 qe = GNUNET_new (struct GNUNET_DATASTORE_QueueEntry);
522 qe->response_type = expected_type;
524 qe->priority = queue_priority;
525 qe->max_queue = max_queue_size;
528 /* append at the tail */
534 /* do not insert at HEAD if HEAD query was already
535 * transmitted and we are still receiving replies! */
536 if ( (NULL == pos) &&
537 (NULL == h->queue_head->env) )
541 #if INSANE_STATISTICS
542 GNUNET_STATISTICS_update (h->stats,
543 gettext_noop ("# queue entries created"),
547 GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
557 * Process entries in the queue (or do nothing if we are already
560 * @param h handle to the datastore
563 process_queue (struct GNUNET_DATASTORE_Handle *h)
565 struct GNUNET_DATASTORE_QueueEntry *qe;
567 if (NULL == (qe = h->queue_head))
569 /* no entry in queue */
570 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 /* waiting for replies */
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "Head request already transmitted\n");
583 /* waiting for reconnect */
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
588 GNUNET_MQ_send (h->mq,
597 * Function called to check status message from the service.
600 * @param sm status message received
601 * @return #GNUNET_OK if the message is well-formed
604 check_status (void *cls,
605 const struct StatusMessage *sm)
607 uint16_t msize = ntohs (sm->header.size) - sizeof (*sm);
608 int32_t status = ntohl (sm->status);
612 const char *emsg = (const char *) &sm[1];
614 if ('\0' != emsg[msize - 1])
617 return GNUNET_SYSERR;
620 else if (GNUNET_SYSERR == status)
623 return GNUNET_SYSERR;
630 * Function called to handle status message from the service.
633 * @param sm status message received
636 handle_status (void *cls,
637 const struct StatusMessage *sm)
639 struct GNUNET_DATASTORE_Handle *h = cls;
640 struct GNUNET_DATASTORE_QueueEntry *qe;
641 struct StatusContext rc;
643 int32_t status = ntohl (sm->status);
645 if (h->skip_next_messages > 0)
647 h->skip_next_messages--;
651 if (NULL == (qe = h->queue_head))
663 if (GNUNET_MESSAGE_TYPE_DATASTORE_STATUS != qe->response_type)
670 free_queue_entry (qe);
671 if (ntohs (sm->header.size) > sizeof (struct StatusMessage))
672 emsg = (const char *) &sm[1];
675 LOG (GNUNET_ERROR_TYPE_DEBUG,
676 "Received status %d/%s\n",
679 GNUNET_STATISTICS_update (h->stats,
680 gettext_noop ("# status messages received"),
683 h->retry_time = GNUNET_TIME_UNIT_ZERO;
686 rc.cont (rc.cont_cls,
688 GNUNET_TIME_absolute_ntoh (sm->min_expiration),
694 * Check data message we received from the service.
696 * @param cls closure with the `struct GNUNET_DATASTORE_Handle *`
697 * @param dm message received
700 check_data (void *cls,
701 const struct DataMessage *dm)
703 uint16_t msize = ntohs (dm->header.size) - sizeof (*dm);
705 if (msize != ntohl (dm->size))
708 return GNUNET_SYSERR;
715 * Handle data message we got from the service.
717 * @param cls closure with the `struct GNUNET_DATASTORE_Handle *`
718 * @param dm message received
721 handle_data (void *cls,
722 const struct DataMessage *dm)
724 struct GNUNET_DATASTORE_Handle *h = cls;
725 struct GNUNET_DATASTORE_QueueEntry *qe;
726 struct ResultContext rc;
728 if (h->skip_next_messages > 0)
746 if (GNUNET_MESSAGE_TYPE_DATASTORE_DATA != qe->response_type)
752 #if INSANE_STATISTICS
753 GNUNET_STATISTICS_update (h->stats,
754 gettext_noop ("# Results received"),
758 LOG (GNUNET_ERROR_TYPE_DEBUG,
759 "Received result %llu with type %u and size %u with key %s\n",
760 (unsigned long long) GNUNET_ntohll (dm->uid),
763 GNUNET_h2s (&dm->key));
765 free_queue_entry (qe);
766 h->retry_time = GNUNET_TIME_UNIT_ZERO;
769 rc.proc (rc.proc_cls,
774 ntohl (dm->priority),
775 ntohl (dm->anonymity),
776 GNUNET_TIME_absolute_ntoh (dm->expiration),
777 GNUNET_ntohll (dm->uid));
782 * Type of a function to call when we receive a
783 * #GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END message from the service.
785 * @param cls closure with the `struct GNUNET_DATASTORE_Handle *`
786 * @param msg message received
789 handle_data_end (void *cls,
790 const struct GNUNET_MessageHeader *msg)
792 struct GNUNET_DATASTORE_Handle *h = cls;
793 struct GNUNET_DATASTORE_QueueEntry *qe;
794 struct ResultContext rc;
796 if (h->skip_next_messages > 0)
798 h->skip_next_messages--;
815 if (GNUNET_MESSAGE_TYPE_DATASTORE_DATA != qe->response_type)
822 free_queue_entry (qe);
823 LOG (GNUNET_ERROR_TYPE_DEBUG,
824 "Received end of result set, new queue size is %u\n",
826 h->retry_time = GNUNET_TIME_UNIT_ZERO;
829 /* signal end of iteration */
831 rc.proc (rc.proc_cls,
838 GNUNET_TIME_UNIT_ZERO_ABS,
844 * Try reconnecting to the datastore service.
846 * @param cls the `struct GNUNET_DATASTORE_Handle`
849 try_reconnect (void *cls)
851 struct GNUNET_DATASTORE_Handle *h = cls;
852 struct GNUNET_MQ_MessageHandler handlers[] = {
853 GNUNET_MQ_hd_var_size (status,
854 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
855 struct StatusMessage,
857 GNUNET_MQ_hd_var_size (data,
858 GNUNET_MESSAGE_TYPE_DATASTORE_DATA,
861 GNUNET_MQ_hd_fixed_size (data_end,
862 GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END,
863 struct GNUNET_MessageHeader,
865 GNUNET_MQ_handler_end ()
868 h->retry_time = GNUNET_TIME_STD_BACKOFF (h->retry_time);
869 h->reconnect_task = NULL;
870 GNUNET_assert (NULL == h->mq);
871 h->mq = GNUNET_CLIENT_connect (h->cfg,
878 GNUNET_STATISTICS_update (h->stats,
879 gettext_noop ("# datastore connections (re)created"),
882 LOG (GNUNET_ERROR_TYPE_DEBUG,
883 "Reconnected to DATASTORE\n");
889 * Dummy continuation used to do nothing (but be non-zero).
892 * @param result result
893 * @param min_expiration expiration time
894 * @param emsg error message
897 drop_status_cont (void *cls,
899 struct GNUNET_TIME_Absolute min_expiration,
907 * Store an item in the datastore. If the item is already present,
908 * the priorities are summed up and the higher expiration time and
909 * lower anonymity level is used.
911 * @param h handle to the datastore
912 * @param rid reservation ID to use (from "reserve"); use 0 if no
913 * prior reservation was made
914 * @param key key for the value
915 * @param size number of bytes in data
916 * @param data content stored
917 * @param type type of the content
918 * @param priority priority of the content
919 * @param anonymity anonymity-level for the content
920 * @param replication how often should the content be replicated to other peers?
921 * @param expiration expiration time for the content
922 * @param queue_priority ranking of this request in the priority queue
923 * @param max_queue_size at what queue size should this request be dropped
924 * (if other requests of higher priority are in the queue)
925 * @param cont continuation to call when done
926 * @param cont_cls closure for @a cont
927 * @return NULL if the entry was not queued, otherwise a handle that can be used to
928 * cancel; note that even if NULL is returned, the callback will be invoked
929 * (or rather, will already have been invoked)
931 struct GNUNET_DATASTORE_QueueEntry *
932 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
934 const struct GNUNET_HashCode *key,
937 enum GNUNET_BLOCK_Type type,
940 uint32_t replication,
941 struct GNUNET_TIME_Absolute expiration,
942 unsigned int queue_priority,
943 unsigned int max_queue_size,
944 GNUNET_DATASTORE_ContinuationWithStatus cont,
947 struct GNUNET_DATASTORE_QueueEntry *qe;
948 struct GNUNET_MQ_Envelope *env;
949 struct DataMessage *dm;
950 union QueueContext qc;
952 if (size + sizeof (*dm) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
958 LOG (GNUNET_ERROR_TYPE_DEBUG,
959 "Asked to put %u bytes of data under key `%s' for %s\n",
962 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (expiration),
964 env = GNUNET_MQ_msg_extra (dm,
966 GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
967 dm->rid = htonl (rid);
968 dm->size = htonl ((uint32_t) size);
969 dm->type = htonl (type);
970 dm->priority = htonl (priority);
971 dm->anonymity = htonl (anonymity);
972 dm->replication = htonl (replication);
973 dm->reserved = htonl (0);
974 dm->uid = GNUNET_htonll (0);
975 dm->expiration = GNUNET_TIME_absolute_hton (expiration);
977 GNUNET_memcpy (&dm[1],
981 qc.sc.cont_cls = cont_cls;
982 qe = make_queue_entry (h,
986 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
990 LOG (GNUNET_ERROR_TYPE_DEBUG,
991 "Could not create queue entry for PUT\n");
994 GNUNET_STATISTICS_update (h->stats,
995 gettext_noop ("# PUT requests executed"),
1004 * Reserve space in the datastore. This function should be used
1005 * to avoid "out of space" failures during a longer sequence of "put"
1006 * operations (for example, when a file is being inserted).
1008 * @param h handle to the datastore
1009 * @param amount how much space (in bytes) should be reserved (for content only)
1010 * @param entries how many entries will be created (to calculate per-entry overhead)
1011 * @param cont continuation to call when done; "success" will be set to
1012 * a positive reservation value if space could be reserved.
1013 * @param cont_cls closure for @a cont
1014 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1015 * cancel; note that even if NULL is returned, the callback will be invoked
1016 * (or rather, will already have been invoked)
1018 struct GNUNET_DATASTORE_QueueEntry *
1019 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
1022 GNUNET_DATASTORE_ContinuationWithStatus cont,
1025 struct GNUNET_DATASTORE_QueueEntry *qe;
1026 struct GNUNET_MQ_Envelope *env;
1027 struct ReserveMessage *rm;
1028 union QueueContext qc;
1031 cont = &drop_status_cont;
1032 LOG (GNUNET_ERROR_TYPE_DEBUG,
1033 "Asked to reserve %llu bytes of data and %u entries\n",
1034 (unsigned long long) amount,
1035 (unsigned int) entries);
1036 env = GNUNET_MQ_msg (rm,
1037 GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
1038 rm->entries = htonl (entries);
1039 rm->amount = GNUNET_htonll (amount);
1042 qc.sc.cont_cls = cont_cls;
1043 qe = make_queue_entry (h,
1047 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
1051 LOG (GNUNET_ERROR_TYPE_DEBUG,
1052 "Could not create queue entry to reserve\n");
1055 GNUNET_STATISTICS_update (h->stats,
1056 gettext_noop ("# RESERVE requests executed"),
1065 * Signal that all of the data for which a reservation was made has
1066 * been stored and that whatever excess space might have been reserved
1067 * can now be released.
1069 * @param h handle to the datastore
1070 * @param rid reservation ID (value of "success" in original continuation
1071 * from the "reserve" function).
1072 * @param queue_priority ranking of this request in the priority queue
1073 * @param max_queue_size at what queue size should this request be dropped
1074 * (if other requests of higher priority are in the queue)
1075 * @param queue_priority ranking of this request in the priority queue
1076 * @param max_queue_size at what queue size should this request be dropped
1077 * (if other requests of higher priority are in the queue)
1078 * @param cont continuation to call when done
1079 * @param cont_cls closure for @a cont
1080 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1081 * cancel; note that even if NULL is returned, the callback will be invoked
1082 * (or rather, will already have been invoked)
1084 struct GNUNET_DATASTORE_QueueEntry *
1085 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
1087 unsigned int queue_priority,
1088 unsigned int max_queue_size,
1089 GNUNET_DATASTORE_ContinuationWithStatus cont,
1092 struct GNUNET_DATASTORE_QueueEntry *qe;
1093 struct GNUNET_MQ_Envelope *env;
1094 struct ReleaseReserveMessage *rrm;
1095 union QueueContext qc;
1098 cont = &drop_status_cont;
1099 LOG (GNUNET_ERROR_TYPE_DEBUG,
1100 "Asked to release reserve %d\n",
1102 env = GNUNET_MQ_msg (rrm,
1103 GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
1104 rrm->rid = htonl (rid);
1106 qc.sc.cont_cls = cont_cls;
1107 qe = make_queue_entry (h,
1111 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
1115 LOG (GNUNET_ERROR_TYPE_DEBUG,
1116 "Could not create queue entry to release reserve\n");
1119 GNUNET_STATISTICS_update (h->stats,
1121 ("# RELEASE RESERVE requests executed"), 1,
1129 * Explicitly remove some content from the database.
1130 * The @a cont continuation will be called with `status`
1131 * #GNUNET_OK" if content was removed, #GNUNET_NO
1132 * if no matching entry was found and #GNUNET_SYSERR
1133 * on all other types of errors.
1135 * @param h handle to the datastore
1136 * @param key key for the value
1137 * @param size number of bytes in data
1138 * @param data content stored
1139 * @param queue_priority ranking of this request in the priority queue
1140 * @param max_queue_size at what queue size should this request be dropped
1141 * (if other requests of higher priority are in the queue)
1142 * @param cont continuation to call when done
1143 * @param cont_cls closure for @a cont
1144 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1145 * cancel; note that even if NULL is returned, the callback will be invoked
1146 * (or rather, will already have been invoked)
1148 struct GNUNET_DATASTORE_QueueEntry *
1149 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
1150 const struct GNUNET_HashCode *key,
1153 unsigned int queue_priority,
1154 unsigned int max_queue_size,
1155 GNUNET_DATASTORE_ContinuationWithStatus cont,
1158 struct GNUNET_DATASTORE_QueueEntry *qe;
1159 struct DataMessage *dm;
1160 struct GNUNET_MQ_Envelope *env;
1161 union QueueContext qc;
1163 if (sizeof (*dm) + size >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
1169 cont = &drop_status_cont;
1170 LOG (GNUNET_ERROR_TYPE_DEBUG,
1171 "Asked to remove %u bytes under key `%s'\n",
1174 env = GNUNET_MQ_msg_extra (dm,
1176 GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
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 GNUNET_memcpy (&dm[1],
1190 qc.sc.cont_cls = cont_cls;
1192 qe = make_queue_entry (h,
1196 GNUNET_MESSAGE_TYPE_DATASTORE_STATUS,
1200 LOG (GNUNET_ERROR_TYPE_DEBUG,
1201 "Could not create queue entry for REMOVE\n");
1204 GNUNET_STATISTICS_update (h->stats,
1205 gettext_noop ("# REMOVE requests executed"),
1215 * Get a random value from the datastore for content replication.
1216 * Returns a single, random value among those with the highest
1217 * replication score, lowering positive replication scores by one for
1218 * the chosen value (if only content with a replication score exists,
1219 * a random value is returned and replication scores are not changed).
1221 * @param h handle to the datastore
1222 * @param queue_priority ranking of this request in the priority queue
1223 * @param max_queue_size at what queue size should this request be dropped
1224 * (if other requests of higher priority are in the queue)
1225 * @param proc function to call on a random value; it
1226 * will be called once with a value (if available)
1227 * and always once with a value of NULL.
1228 * @param proc_cls closure for @a proc
1229 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1232 struct GNUNET_DATASTORE_QueueEntry *
1233 GNUNET_DATASTORE_get_for_replication (struct GNUNET_DATASTORE_Handle *h,
1234 unsigned int queue_priority,
1235 unsigned int max_queue_size,
1236 GNUNET_DATASTORE_DatumProcessor proc,
1239 struct GNUNET_DATASTORE_QueueEntry *qe;
1240 struct GNUNET_MQ_Envelope *env;
1241 struct GNUNET_MessageHeader *m;
1242 union QueueContext qc;
1244 GNUNET_assert (NULL != proc);
1245 LOG (GNUNET_ERROR_TYPE_DEBUG,
1246 "Asked to get replication entry\n");
1247 env = GNUNET_MQ_msg (m,
1248 GNUNET_MESSAGE_TYPE_DATASTORE_GET_REPLICATION);
1250 qc.rc.proc_cls = proc_cls;
1251 qe = make_queue_entry (h,
1255 GNUNET_MESSAGE_TYPE_DATASTORE_DATA,
1259 LOG (GNUNET_ERROR_TYPE_DEBUG,
1260 "Could not create queue entry for GET REPLICATION\n");
1263 GNUNET_STATISTICS_update (h->stats,
1265 ("# GET REPLICATION requests executed"), 1,
1273 * Get a single zero-anonymity value from the datastore.
1275 * @param h handle to the datastore
1276 * @param offset offset of the result (modulo num-results); set to
1277 * a random 64-bit value initially; then increment by
1278 * one each time; detect that all results have been found by uid
1279 * being again the first uid ever returned.
1280 * @param queue_priority ranking of this request in the priority queue
1281 * @param max_queue_size at what queue size should this request be dropped
1282 * (if other requests of higher priority are in the queue)
1283 * @param type allowed type for the operation (never zero)
1284 * @param proc function to call on a random value; it
1285 * will be called once with a value (if available)
1286 * or with NULL if none value exists.
1287 * @param proc_cls closure for @a proc
1288 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1291 struct GNUNET_DATASTORE_QueueEntry *
1292 GNUNET_DATASTORE_get_zero_anonymity (struct GNUNET_DATASTORE_Handle *h,
1294 unsigned int queue_priority,
1295 unsigned int max_queue_size,
1296 enum GNUNET_BLOCK_Type type,
1297 GNUNET_DATASTORE_DatumProcessor proc,
1300 struct GNUNET_DATASTORE_QueueEntry *qe;
1301 struct GNUNET_MQ_Envelope *env;
1302 struct GetZeroAnonymityMessage *m;
1303 union QueueContext qc;
1305 GNUNET_assert (NULL != proc);
1306 GNUNET_assert (type != GNUNET_BLOCK_TYPE_ANY);
1307 LOG (GNUNET_ERROR_TYPE_DEBUG,
1308 "Asked to get %llu-th zero-anonymity entry of type %d\n",
1309 (unsigned long long) offset,
1311 env = GNUNET_MQ_msg (m,
1312 GNUNET_MESSAGE_TYPE_DATASTORE_GET_ZERO_ANONYMITY);
1313 m->type = htonl ((uint32_t) type);
1314 m->offset = GNUNET_htonll (offset);
1316 qc.rc.proc_cls = proc_cls;
1317 qe = make_queue_entry (h,
1321 GNUNET_MESSAGE_TYPE_DATASTORE_DATA,
1325 LOG (GNUNET_ERROR_TYPE_DEBUG,
1326 "Could not create queue entry for zero-anonymity procation\n");
1329 GNUNET_STATISTICS_update (h->stats,
1331 ("# GET ZERO ANONYMITY requests executed"), 1,
1339 * Get a result for a particular key from the datastore. The processor
1340 * will only be called once.
1342 * @param h handle to the datastore
1343 * @param offset offset of the result (modulo num-results); set to
1344 * a random 64-bit value initially; then increment by
1345 * one each time; detect that all results have been found by uid
1346 * being again the first uid ever returned.
1347 * @param key maybe NULL (to match all entries)
1348 * @param type desired type, 0 for any
1349 * @param queue_priority ranking of this request in the priority queue
1350 * @param max_queue_size at what queue size should this request be dropped
1351 * (if other requests of higher priority are in the queue)
1352 * @param proc function to call on each matching value;
1353 * will be called once with a NULL value at the end
1354 * @param proc_cls closure for @a proc
1355 * @return NULL if the entry was not queued, otherwise a handle that can be used to
1358 struct GNUNET_DATASTORE_QueueEntry *
1359 GNUNET_DATASTORE_get_key (struct GNUNET_DATASTORE_Handle *h,
1361 const struct GNUNET_HashCode *key,
1362 enum GNUNET_BLOCK_Type type,
1363 unsigned int queue_priority,
1364 unsigned int max_queue_size,
1365 GNUNET_DATASTORE_DatumProcessor proc,
1368 struct GNUNET_DATASTORE_QueueEntry *qe;
1369 struct GNUNET_MQ_Envelope *env;
1370 struct GetKeyMessage *gkm;
1371 struct GetMessage *gm;
1372 union QueueContext qc;
1374 GNUNET_assert (NULL != proc);
1375 LOG (GNUNET_ERROR_TYPE_DEBUG,
1376 "Asked to look for data of type %u under key `%s'\n",
1377 (unsigned int) type,
1381 env = GNUNET_MQ_msg (gm,
1382 GNUNET_MESSAGE_TYPE_DATASTORE_GET);
1383 gm->type = htonl (type);
1384 gm->offset = GNUNET_htonll (offset);
1388 env = GNUNET_MQ_msg (gkm,
1389 GNUNET_MESSAGE_TYPE_DATASTORE_GET_KEY);
1390 gkm->type = htonl (type);
1391 gkm->offset = GNUNET_htonll (offset);
1395 qc.rc.proc_cls = proc_cls;
1396 qe = make_queue_entry (h,
1400 GNUNET_MESSAGE_TYPE_DATASTORE_DATA,
1404 LOG (GNUNET_ERROR_TYPE_DEBUG,
1405 "Could not queue request for `%s'\n",
1409 #if INSANE_STATISTICS
1410 GNUNET_STATISTICS_update (h->stats,
1411 gettext_noop ("# GET requests executed"),
1421 * Cancel a datastore operation. The final callback from the
1422 * operation must not have been done yet.
1424 * @param qe operation to cancel
1427 GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
1429 struct GNUNET_DATASTORE_Handle *h = qe->h;
1431 LOG (GNUNET_ERROR_TYPE_DEBUG,
1432 "Pending DATASTORE request %p cancelled (%d, %d)\n",
1435 h->queue_head == qe);
1436 if (NULL == qe->env)
1438 free_queue_entry (qe);
1439 h->skip_next_messages++;
1442 free_queue_entry (qe);
1447 /* end of datastore_api.c */