use v2
[oweals/gnunet.git] / src / datastore / datastore_api.c
index 65a519e598c38945162ac04a78579f33598eb452..12ec8bf0535bc2bfe7bf6c76f62a9005965f2e79 100644 (file)
 /**
  * Entry in our priority queue.
  */
-struct QueueEntry
+struct GNUNET_DATASTORE_QueueEntry
 {
 
   /**
    * This is a linked list.
    */
-  struct QueueEntry *next;
+  struct GNUNET_DATASTORE_QueueEntry *next;
 
   /**
    * This is a linked list.
    */
-  struct QueueEntry *prev;
+  struct GNUNET_DATASTORE_QueueEntry *prev;
 
   /**
    * Handle to the master context.
@@ -67,12 +67,12 @@ struct QueueEntry
   /**
    * Function to call after transmission of the request.
    */
-  GNUNET_DATASTORE_ContinuationWithStatus contX;
+  GNUNET_DATASTORE_ContinuationWithStatus cont;
    
   /**
    * Closure for 'cont'.
    */
-  void *cont_clsX;
+  void *cont_cls;
 
   /**
    * Task for timeout signalling.
@@ -141,12 +141,12 @@ struct GNUNET_DATASTORE_Handle
   /**
    * Current head of priority queue.
    */
-  struct QueueEntry *queue_head;
+  struct GNUNET_DATASTORE_QueueEntry *queue_head;
 
   /**
    * Current tail of priority queue.
    */
-  struct QueueEntry *queue_tail;
+  struct GNUNET_DATASTORE_QueueEntry *queue_tail;
 
   /**
    * Task for trying to reconnect.
@@ -240,22 +240,23 @@ transmit_drop (void *cls,
 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
                                  int drop)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
 
   if (h->client != NULL)
-    GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+    {
+      GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
+      h->client = NULL;
+    }
   if (h->reconnect_task != GNUNET_SCHEDULER_NO_TASK)
-    GNUNET_SCHEDULER_cancel (h->sched,
-                            h->reconnect_task);
-  h->client = NULL;
+    {
+      GNUNET_SCHEDULER_cancel (h->sched,
+                              h->reconnect_task);
+      h->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+    }
   while (NULL != (qe = h->queue_head))
     {
-      GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                  h->queue_tail,
-                                  qe);
-      if (NULL != qe->response_proc)
-       qe->response_proc (qe, NULL);
-      GNUNET_free (qe);
+      GNUNET_assert (NULL != qe->response_proc);
+      qe->response_proc (qe, NULL);
     }
   if (GNUNET_YES == drop) 
     {
@@ -281,24 +282,18 @@ void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
 /**
  * A request has timed out (before being transmitted to the service).
  *
- * @param cls the 'struct QueueEntry'
+ * @param cls the 'struct GNUNET_DATASTORE_QueueEntry'
  * @param tc scheduler context
  */
 static void
 timeout_queue_entry (void *cls,
                     const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  struct QueueEntry *qe = cls;
-  struct GNUNET_DATASTORE_Handle *h = qe->h;
+  struct GNUNET_DATASTORE_QueueEntry *qe = cls;
 
   qe->task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_assert (qe->was_transmitted == GNUNET_NO);
-  GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                              h->queue_tail,
-                              qe);
-  if (qe->response_proc != NULL)
-    qe->response_proc (qe, NULL);
-  GNUNET_free (qe);
+  qe->response_proc (qe, NULL);
 }
 
 
@@ -316,7 +311,7 @@ timeout_queue_entry (void *cls,
  * @param client_ctx client context (NOT a closure for response_proc)
  * @return NULL if the queue is full (and this entry was dropped)
  */
-static struct QueueEntry *
+static struct GNUNET_DATASTORE_QueueEntry *
 make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
                  size_t msize,
                  unsigned int queue_priority,
@@ -325,8 +320,8 @@ make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
                  GNUNET_CLIENT_MessageHandler response_proc,            
                  void *client_ctx)
 {
-  struct QueueEntry *ret;
-  struct QueueEntry *pos;
+  struct GNUNET_DATASTORE_QueueEntry *ret;
+  struct GNUNET_DATASTORE_QueueEntry *pos;
   unsigned int c;
 
   c = 0;
@@ -338,8 +333,15 @@ make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
       c++;
       pos = pos->next;
     }
-  if (c >= max_queue_size)
-    return NULL;
+  ret = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_QueueEntry) + msize);
+  ret->h = h;
+  ret->response_proc = response_proc;
+  ret->client_ctx = client_ctx;
+  ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
+  ret->priority = queue_priority;
+  ret->max_queue = max_queue_size;
+  ret->message_size = msize;
+  ret->was_transmitted = GNUNET_NO;
   if (pos == NULL)
     {
       /* append at the tail */
@@ -354,39 +356,29 @@ make_queue_entry (struct GNUNET_DATASTORE_Handle *h,
           (h->queue_head->was_transmitted) )
        pos = h->queue_head;
     }
-  ret = GNUNET_malloc (sizeof (struct QueueEntry) + msize);
+  c++;
   GNUNET_CONTAINER_DLL_insert_after (h->queue_head,
                                     h->queue_tail,
                                     pos,
                                     ret);
-  ret->h = h;
-  ret->response_proc = response_proc;
-  ret->client_ctx = client_ctx;
+  h->queue_size++;
+  if (c > max_queue_size)
+    {
+      response_proc (ret, NULL);
+      GNUNET_free (ret);
+      return NULL;
+    }
   ret->task = GNUNET_SCHEDULER_add_delayed (h->sched,
                                            timeout,
                                            &timeout_queue_entry,
                                            ret);
-  ret->timeout = GNUNET_TIME_relative_to_absolute (timeout);
-  ret->priority = queue_priority;
-  ret->max_queue = max_queue_size;
-  ret->message_size = msize;
-  ret->was_transmitted = GNUNET_NO;
-  h->queue_size++;
-  c++;
   pos = ret->next;
   while (pos != NULL) 
     {
       if (pos->max_queue < h->queue_size)
        {
-         GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                      h->queue_tail,
-                                      pos);
-         GNUNET_SCHEDULER_cancel (h->sched,
-                                  pos->task);
-         if (pos->response_proc != NULL)
-           pos->response_proc (pos, NULL);
-         GNUNET_free (pos);
-         h->queue_size--;
+         GNUNET_assert (pos->response_proc != NULL);
+         pos->response_proc (pos, NULL);
          break;
        }
       pos = pos->next;
@@ -435,12 +427,13 @@ try_reconnect (void *cls,
  * Disconnect from the service and then try reconnecting to the datastore service
  * after some delay.
  *
- * @param cls the 'struct GNUNET_DATASTORE_Handle'
- * @param tc scheduler context
+ * @param h handle to datastore to disconnect and reconnect
  */
 static void
 do_disconnect (struct GNUNET_DATASTORE_Handle *h)
 {
+  if (h->client == NULL)
+    return;
   GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
   h->client = NULL;
   h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->sched,
@@ -464,7 +457,7 @@ transmit_request (void *cls,
                  void *buf)
 {
   struct GNUNET_DATASTORE_Handle *h = cls;
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   size_t msize;
 
   h->th = NULL;
@@ -504,7 +497,7 @@ transmit_request (void *cls,
 static void
 process_queue (struct GNUNET_DATASTORE_Handle *h)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
 
   if (NULL == (qe = h->queue_head))
     return; /* no entry in queue */
@@ -557,6 +550,24 @@ drop_status_cont (void *cls, int result, const char *emsg)
 }
 
 
+static void
+free_queue_entry (struct GNUNET_DATASTORE_QueueEntry *qe)
+{
+  struct GNUNET_DATASTORE_Handle *h = qe->h;
+
+  GNUNET_CONTAINER_DLL_remove (h->queue_head,
+                              h->queue_tail,
+                              qe);
+  if (qe->task != GNUNET_SCHEDULER_NO_TASK)
+    {
+      GNUNET_SCHEDULER_cancel (h->sched,
+                              qe->task);
+      qe->task = GNUNET_SCHEDULER_NO_TASK;
+    }
+  h->queue_size--;
+  GNUNET_free (qe);
+}
+
 /**
  * Type of a function to call when we receive a message
  * from the service.
@@ -569,19 +580,18 @@ process_status_message (void *cls,
                        const struct
                        GNUNET_MessageHeader * msg)
 {
-  struct QueueEntry *qe = cls;
+  struct GNUNET_DATASTORE_QueueEntry *qe = cls;
   struct GNUNET_DATASTORE_Handle *h = qe->h;
   struct StatusContext *rc = qe->client_ctx;
   const struct StatusMessage *sm;
   const char *emsg;
   int32_t status;
 
-  GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                              h->queue_tail,
-                              qe);
-  GNUNET_free (qe);
+  free_queue_entry (qe);
   if (msg == NULL)
-    {
+    {      
+      if (NULL == h->client)
+       return; /* forced disconnect */
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Failed to receive response from database.\n"));
       do_disconnect (h);
@@ -653,8 +663,11 @@ process_status_message (void *cls,
  * @param timeout timeout for the operation
  * @param cont continuation to call when done
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
                      int rid,
                       const GNUNET_HashCode * key,
@@ -671,7 +684,7 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
                      void *cont_cls)
 {
   struct StatusContext *scont;
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct DataMessage *dm;
   size_t msize;
 
@@ -690,7 +703,7 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
                         queue_priority, max_queue_size, timeout,
                         &process_status_message, scont);
   if (qe == NULL)
-    return;
+    return NULL;
   dm = (struct DataMessage* ) &qe[1];
   dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
   dm->header.size = htons(msize);
@@ -704,6 +717,7 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
   dm->key = *key;
   memcpy (&dm[1], data, size);
   process_queue (h);
+  return qe;
 }
 
 
@@ -722,8 +736,11 @@ GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
  * @param cont continuation to call when done; "success" will be set to
  *             a positive reservation value if space could be reserved.
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
                          uint64_t amount,
                          uint32_t entries,
@@ -733,7 +750,7 @@ GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
                          GNUNET_DATASTORE_ContinuationWithStatus cont,
                          void *cont_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct ReserveMessage *rm;
   struct StatusContext *scont;
 
@@ -752,13 +769,14 @@ GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
                         queue_priority, max_queue_size, timeout,
                         &process_status_message, scont);
   if (qe == NULL)
-    return;
+    return NULL;
   rm = (struct ReserveMessage*) &qe[1];
   rm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE);
   rm->header.size = htons(sizeof (struct ReserveMessage));
   rm->entries = htonl(entries);
   rm->amount = GNUNET_htonll(amount);
   process_queue (h);
+  return qe;
 }
 
 
@@ -779,8 +797,11 @@ GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
  * @param timeout how long to wait at most for a response
  * @param cont continuation to call when done
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
                                  int rid,
                                  unsigned int queue_priority,
@@ -789,7 +810,7 @@ GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
                                  GNUNET_DATASTORE_ContinuationWithStatus cont,
                                  void *cont_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct ReleaseReserveMessage *rrm;
   struct StatusContext *scont;
 
@@ -807,12 +828,13 @@ GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
                         queue_priority, max_queue_size, timeout,
                         &process_status_message, scont);
   if (qe == NULL)
-    return;
+    return NULL;
   rrm = (struct ReleaseReserveMessage*) &qe[1];
   rrm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE);
   rrm->header.size = htons(sizeof (struct ReleaseReserveMessage));
   rrm->rid = htonl(rid);
   process_queue (h);
+  return qe;
 }
 
 
@@ -829,8 +851,11 @@ GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
  * @param timeout how long to wait at most for a response
  * @param cont continuation to call when done
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
                         unsigned long long uid,
                         uint32_t priority,
@@ -841,7 +866,7 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
                         GNUNET_DATASTORE_ContinuationWithStatus cont,
                         void *cont_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct UpdateMessage *um;
   struct StatusContext *scont;
 
@@ -861,7 +886,7 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
                         queue_priority, max_queue_size, timeout,
                         &process_status_message, scont);
   if (qe == NULL)
-    return;
+    return NULL;
   um = (struct UpdateMessage*) &qe[1];
   um->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE);
   um->header.size = htons(sizeof (struct UpdateMessage));
@@ -869,6 +894,7 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
   um->expiration = GNUNET_TIME_absolute_hton(expiration);
   um->uid = GNUNET_htonll(uid);
   process_queue (h);
+  return qe;
 }
 
 
@@ -889,8 +915,11 @@ GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
  * @param timeout how long to wait at most for a response
  * @param cont continuation to call when done
  * @param cont_cls closure for cont
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
                          const GNUNET_HashCode *key,
                          uint32_t size, 
@@ -901,7 +930,7 @@ GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
                         GNUNET_DATASTORE_ContinuationWithStatus cont,
                         void *cont_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct DataMessage *dm;
   size_t msize;
   struct StatusContext *scont;
@@ -923,7 +952,7 @@ GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
                         queue_priority, max_queue_size, timeout,
                         &process_status_message, scont);
   if (qe == NULL)
-    return;
+    return NULL;
   dm = (struct DataMessage*) &qe[1];
   dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE);
   dm->header.size = htons(msize);
@@ -937,6 +966,7 @@ GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
   dm->key = *key;
   memcpy (&dm[1], data, size);
   process_queue (h);
+  return qe;
 }
 
 
@@ -970,21 +1000,19 @@ static void
 process_result_message (void *cls,
                        const struct GNUNET_MessageHeader * msg)
 {
-  struct QueueEntry *qe = cls;
+  struct GNUNET_DATASTORE_QueueEntry *qe = cls;
   struct GNUNET_DATASTORE_Handle *h = qe->h;
   struct ResultContext *rc = qe->client_ctx;
   const struct DataMessage *dm;
 
+  GNUNET_assert (h->queue_head == qe);
   if (msg == NULL)
     {
 #if DEBUG_DATASTORE
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Failed to receive response from datastore\n"));
 #endif
-      GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                  h->queue_tail,
-                                  qe);
-      GNUNET_free (qe);
+      free_queue_entry (qe);
       do_disconnect (h);
       rc->iter (rc->iter_cls,
                NULL, 0, NULL, 0, 0, 0, 
@@ -999,10 +1027,7 @@ process_result_message (void *cls,
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                  "Received end of result set\n");
 #endif
-      GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                  h->queue_tail,
-                                  qe);
-      GNUNET_free (qe);
+      free_queue_entry (qe);
       rc->iter (rc->iter_cls,
                NULL, 0, NULL, 0, 0, 0, 
                GNUNET_TIME_UNIT_ZERO_ABS, 0);  
@@ -1015,10 +1040,7 @@ process_result_message (void *cls,
        (ntohs(msg->size) != sizeof(struct DataMessage) + ntohl (((const struct DataMessage*)msg)->size)) )
     {
       GNUNET_break (0);
-      GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                                  h->queue_tail,
-                                  qe);
-      GNUNET_free (qe);
+      free_queue_entry (qe);
       h->retry_time = GNUNET_TIME_UNIT_ZERO;
       do_disconnect (h);
       rc->iter (rc->iter_cls,
@@ -1060,8 +1082,11 @@ process_result_message (void *cls,
  *        will be called once with a value (if available)
  *        and always once with a value of NULL.
  * @param iter_cls closure for iter
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
                             unsigned int queue_priority,
                             unsigned int max_queue_size,
@@ -1069,7 +1094,7 @@ GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
                              GNUNET_DATASTORE_Iterator iter, 
                             void *iter_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct GNUNET_MessageHeader *m;
   struct ResultContext *rcont;
 
@@ -1085,11 +1110,12 @@ GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
                         queue_priority, max_queue_size, timeout,
                         &process_result_message, rcont);
   if (qe == NULL)
-    return;
+    return NULL;    
   m = (struct GNUNET_MessageHeader*) &qe[1];
   m->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM);
   m->size = htons(sizeof (struct GNUNET_MessageHeader));
   process_queue (h);
+  return qe;
 }
 
 
@@ -1111,8 +1137,11 @@ GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
  * @param iter function to call on each matching value;
  *        will be called once with a NULL value at the end
  * @param iter_cls closure for iter
+ * @return NULL if the entry was not queued, otherwise a handle that can be used to
+ *         cancel; note that even if NULL is returned, the callback will be invoked
+ *         (or rather, will already have been invoked)
  */
-void
+struct GNUNET_DATASTORE_QueueEntry *
 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
                       const GNUNET_HashCode * key,
                      enum GNUNET_BLOCK_Type type,
@@ -1122,7 +1151,7 @@ GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
                       GNUNET_DATASTORE_Iterator iter, 
                      void *iter_cls)
 {
-  struct QueueEntry *qe;
+  struct GNUNET_DATASTORE_QueueEntry *qe;
   struct GetMessage *gm;
   struct ResultContext *rcont;
 
@@ -1139,7 +1168,7 @@ GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
                         queue_priority, max_queue_size, timeout,
                         &process_result_message, rcont);
   if (qe == NULL)
-    return;
+    return NULL;
   gm = (struct GetMessage*) &qe[1];
   gm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_GET);
   gm->type = htonl(type);
@@ -1153,6 +1182,7 @@ GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
       gm->header.size = htons(sizeof (struct GetMessage) - sizeof(GNUNET_HashCode));
     }
   process_queue (h);
+  return qe;
 }
 
 
@@ -1168,7 +1198,7 @@ void
 GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
                           int more)
 {
-  struct QueueEntry *qe = h->queue_head;
+  struct GNUNET_DATASTORE_QueueEntry *qe = h->queue_head;
   struct ResultContext *rc = qe->client_ctx;
 
   GNUNET_assert (NULL != qe);
@@ -1181,10 +1211,7 @@ GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
                             GNUNET_TIME_absolute_get_remaining (qe->timeout));
       return;
     }
-  GNUNET_CONTAINER_DLL_remove (h->queue_head,
-                              h->queue_tail,
-                              qe);
-  GNUNET_free (qe);
+  free_queue_entry (qe);
   h->retry_time = GNUNET_TIME_UNIT_ZERO;
   do_disconnect (h);
   rc->iter (rc->iter_cls,
@@ -1194,4 +1221,28 @@ GNUNET_DATASTORE_get_next (struct GNUNET_DATASTORE_Handle *h,
 }
 
 
+/**
+ * Cancel a datastore operation.  The final callback from the
+ * operation must not have been done yet.
+ * 
+ * @param qe operation to cancel
+ */
+void
+GNUNET_DATASTORE_cancel (struct GNUNET_DATASTORE_QueueEntry *qe)
+{
+  struct GNUNET_DATASTORE_Handle *h;
+  int reconnect;
+
+  h = qe->h;
+  reconnect = qe->was_transmitted;
+  free_queue_entry (qe);
+  h->queue_size--;
+  if (reconnect)
+    {
+      h->retry_time = GNUNET_TIME_UNIT_ZERO;
+      do_disconnect (h);
+    }
+}
+
+
 /* end of datastore_api.c */