2 This file is part of GNUnet
3 (C) 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file datastore/datastore_api.c
23 * @brief Management for the datastore for files stored on a GNUnet node
24 * @author Christian Grothoff
28 #include "gnunet_datastore_service.h"
29 #include "datastore.h"
38 * This is a linked list.
40 struct MessageQueue *next;
43 * Message we will transmit (allocated at the end
44 * of this struct; do not free!).
46 struct GNUNET_MessageHeader *msg;
49 * Function to call on the response.
51 GNUNET_CLIENT_MessageHandler response_processor;
54 * Closure for response_processor.
56 void *response_processor_cls;
62 * Handle to the datastore service.
64 struct GNUNET_DATASTORE_Handle
68 * Current connection to the datastore service.
70 struct GNUNET_CLIENT_Connection *client;
73 * Linked list of messages waiting to be transmitted.
75 struct MessageQueue *messages;
78 * Current response processor (NULL if we are not waiting
79 * for a response). Largely used only to know if we have
80 * a 'receive' request pending.
82 GNUNET_CLIENT_MessageHandler response_proc;
85 * Closure for response_proc.
87 void *response_proc_cls;
93 * Connect to the datastore service.
95 * @param cfg configuration to use
96 * @param sched scheduler to use
97 * @return handle to use to access the service
99 struct GNUNET_DATASTORE_Handle *GNUNET_DATASTORE_connect (struct
100 GNUNET_CONFIGURATION_Handle
103 GNUNET_SCHEDULER_Handle
106 struct GNUNET_CLIENT_Connection *c;
107 struct GNUNET_DATASTORE_Handle *h;
109 c = GNUNET_CLIENT_connect (sched, "datastore", cfg);
111 return NULL; /* oops */
112 h = GNUNET_malloc (sizeof(struct GNUNET_DATASTORE_Handle));
119 * Transmit DROP message to datastore service.
122 transmit_drop (void *cls,
123 size_t size, void *buf)
125 struct GNUNET_DATASTORE_Handle *h = cls;
126 struct GNUNET_MessageHeader *hdr;
130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
131 _("Failed to transmit request to drop database.\n"));
132 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
135 GNUNET_assert (size >= sizeof(struct GNUNET_MessageHeader));
137 hdr->size = htons(sizeof(struct GNUNET_MessageHeader));
138 hdr->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DROP);
139 GNUNET_DATASTORE_disconnect (h, GNUNET_NO);
140 return sizeof(struct GNUNET_MessageHeader);
145 * Disconnect from the datastore service (and free
146 * associated resources).
148 * @param h handle to the datastore
149 * @param drop set to GNUNET_YES to delete all data in datastore (!)
151 void GNUNET_DATASTORE_disconnect (struct GNUNET_DATASTORE_Handle *h,
154 if (GNUNET_YES == drop)
157 GNUNET_CLIENT_notify_transmit_ready (h->client,
158 sizeof(struct GNUNET_MessageHeader),
159 GNUNET_TIME_UNIT_MINUTES,
165 GNUNET_CLIENT_disconnect (h->client);
171 * The closure is followed by the data message.
175 struct GNUNET_DATASTORE_Handle *h;
176 GNUNET_DATASTORE_ContinuationWithStatus cont;
182 * Transmit PUT message to Database service.
185 transmit_put (void *cls,
186 size_t size, void *buf)
188 struct PutClosure *pc = cls;
189 struct DataMessage *dm;
194 pc->cont (pc->cont_cls, GNUNET_SYSERR,
195 gettext_noop ("Error transmitting `PUT' message to datastore service.\n"));
199 dm = (struct DataMessage*) &pc[1];
200 msize = ntohs(dm->size);
201 GNUNET_assert (msize <= size);
202 memcpy (buf, dm, msize);
203 /* FIXME: wait for response from datastore, then
204 call our continuation! */
210 * Store an item in the datastore. If the item is already present,
211 * the priorities are summed up and the higher expiration time and
212 * lower anonymity level is used.
214 * @param h handle to the datastore
215 * @param key key for the value
216 * @param size number of bytes in data
217 * @param data content stored
218 * @param type type of the content
219 * @param priority priority of the content
220 * @param anonymity anonymity-level for the content
221 * @param expiration expiration time for the content
222 * @param timeout timeout for the operation
223 * @param cont continuation to call when done
224 * @param cont_cls closure for cont
227 GNUNET_DATASTORE_put (struct GNUNET_DATASTORE_Handle *h,
229 const GNUNET_HashCode * key,
235 struct GNUNET_TIME_Absolute expiration,
236 struct GNUNET_TIME_Relative timeout,
237 GNUNET_DATASTORE_ContinuationWithStatus cont,
240 struct PutClosure *pc;
241 struct DataMessage *dm;
243 pc = GNUNET_malloc (sizeof(struct PutClosure) +
244 sizeof(struct DataMessage) +
246 dm = (struct DataMessage*) &pc[1];
249 pc->cont_cls = cont_cls;
250 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_PUT);
251 dm->header.size = htons(sizeof(struct DataMessage) + size);
252 dm->rid = htonl(rid);
253 dm->size = htonl(size);
254 dm->type = htonl(type);
255 dm->priority = htonl(priority);
256 dm->anonymity = htonl(anonymity);
257 dm->uid = GNUNET_htonll(0);
258 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
260 memcpy (&dm[1], data, size);
261 if (NULL == GNUNET_CLIENT_notify_transmit_ready (h->client,
262 sizeof(struct DataMessage) + size,
268 cont (cont_cls, GNUNET_SYSERR,
269 gettext_noop ("Not ready to transmit request to datastore service"));
275 * Reserve space in the datastore. This function should be used
276 * to avoid "out of space" failures during a longer sequence of "put"
277 * operations (for example, when a file is being inserted).
279 * @param h handle to the datastore
280 * @param amount how much space (in bytes) should be reserved (for content only)
281 * @param entries how many entries will be created (to calculate per-entry overhead)
282 * @param cont continuation to call when done; "success" will be set to
283 * a positive reservation value if space could be reserved.
284 * @param cont_cls closure for cont
287 GNUNET_DATASTORE_reserve (struct GNUNET_DATASTORE_Handle *h,
290 GNUNET_DATASTORE_ContinuationWithStatus cont,
293 cont (cont_cls, GNUNET_SYSERR, "not implemented");
298 * Signal that all of the data for which a reservation was made has
299 * been stored and that whatever excess space might have been reserved
300 * can now be released.
302 * @param h handle to the datastore
303 * @param rid reservation ID (value of "success" in original continuation
304 * from the "reserve" function).
305 * @param cont continuation to call when done
306 * @param cont_cls closure for cont
309 GNUNET_DATASTORE_release_reserve (struct GNUNET_DATASTORE_Handle *h,
311 GNUNET_DATASTORE_ContinuationWithStatus cont,
314 cont (cont_cls, GNUNET_OK, NULL);
319 * Update a value in the datastore.
321 * @param h handle to the datastore
322 * @param uid identifier for the value
323 * @param priority how much to increase the priority of the value
324 * @param expiration new expiration value should be MAX of existing and this argument
325 * @param cont continuation to call when done
326 * @param cont_cls closure for cont
329 GNUNET_DATASTORE_update (struct GNUNET_DATASTORE_Handle *h,
330 unsigned long long uid,
332 struct GNUNET_TIME_Absolute expiration,
333 GNUNET_DATASTORE_ContinuationWithStatus cont,
336 cont (cont_cls, GNUNET_SYSERR, "not implemented");
341 * Iterate over the results for a particular key
344 * @param h handle to the datastore
345 * @param key maybe NULL (to match all entries)
346 * @param type desired type, 0 for any
347 * @param iter function to call on each matching value;
348 * will be called once with a NULL value at the end
349 * @param iter_cls closure for iter
352 GNUNET_DATASTORE_get (struct GNUNET_DATASTORE_Handle *h,
353 const GNUNET_HashCode * key,
355 GNUNET_DATASTORE_Iterator iter, void *iter_cls)
357 static struct GNUNET_TIME_Absolute zero;
359 NULL, 0, NULL, 0, 0, 0, zero, 0);
364 * Get a random value from the datastore.
366 * @param h handle to the datastore
367 * @param iter function to call on a random value; it
368 * will be called exactly once; if no values
369 * are available, the value will be NULL.
370 * @param iter_cls closure for iter
373 GNUNET_DATASTORE_get_random (struct GNUNET_DATASTORE_Handle *h,
374 GNUNET_DATASTORE_Iterator iter, void *iter_cls)
376 static struct GNUNET_TIME_Absolute zero;
379 NULL, 0, NULL, 0, 0, 0, zero, 0);
384 * Explicitly remove some content from the database.
386 * @param h handle to the datastore
387 * @param key key for the value
388 * @param size number of bytes in data
389 * @param data content stored
390 * @param cont continuation to call when done
391 * @param cont_cls closure for cont
394 GNUNET_DATASTORE_remove (struct GNUNET_DATASTORE_Handle *h,
395 const GNUNET_HashCode * key,
396 uint32_t size, const void *data,
397 GNUNET_DATASTORE_ContinuationWithStatus cont,
400 cont (cont_cls, GNUNET_SYSERR, "not implemented");
404 /* end of datastore_api.c */