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/gnunet-service-datastore.c
23 * @brief Management for the datastore for files stored on a GNUnet node
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_protocols.h"
30 #include "plugin_datastore.h"
31 #include "datastore.h"
34 * How many messages do we queue at most per client?
36 #define MAX_PENDING 1024
39 * How long are we at most keeping "expired" content
40 * past the expiration date in the database?
42 #define MAX_EXPIRE_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
47 * Our datastore plugin.
49 struct DatastorePlugin
53 * API of the transport as returned by the plugin's
54 * initialization function.
56 struct GNUNET_DATASTORE_PluginFunctions *api;
59 * Short name for the plugin (i.e. "sqlite").
64 * Name of the library (i.e. "gnunet_plugin_datastore_sqlite").
69 * Environment this transport service is using
72 struct GNUNET_DATASTORE_PluginEnvironment env;
78 * Linked list of active reservations.
80 struct ReservationList
84 * This is a linked list.
86 struct ReservationList *next;
89 * Client that made the reservation.
91 struct GNUNET_SERVER_Client *client;
94 * Number of bytes (still) reserved.
99 * Number of items (still) reserved.
104 * Reservation identifier.
112 * Our datastore plugin (NULL if not available).
114 static struct DatastorePlugin *plugin;
117 * Linked list of space reservations made by clients.
119 static struct ReservationList *reservations;
122 * Bloomfilter to quickly tell if we don't have the content.
124 static struct GNUNET_CONTAINER_BloomFilter *filter;
127 * Static counter to produce reservation identifiers.
129 static int reservation_gen;
132 * How much space are we allowed to use?
134 static unsigned long long quota;
137 * How much space are we using for the cache?
138 * (space available for insertions that will be
139 * instantly reclaimed by discarding less
140 * important content --- or possibly whatever
141 * we just inserted into the "cache").
143 static unsigned long long cache_size;
146 * How much space have we currently reserved?
148 static unsigned long long reserved;
151 * Identity of the task that is used to delete
154 static GNUNET_SCHEDULER_TaskIdentifier expired_kill_task;
159 struct GNUNET_CONFIGURATION_Handle *cfg;
164 struct GNUNET_SCHEDULER_Handle *sched;
167 * Function called once the transmit operation has
168 * either failed or succeeded.
171 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
173 typedef void (*TransmitContinuation)(void *cls,
177 struct TransmitCallbackContext
180 * The message that we're asked to transmit.
182 struct GNUNET_MessageHeader *msg;
185 * Client that we are transmitting to.
187 struct GNUNET_SERVER_Client *client;
190 * Function to call once msg has been transmitted
191 * (or at least added to the buffer).
193 TransmitContinuation tc;
201 * GNUNET_YES if we are supposed to signal the server
202 * completion of the client's request.
209 * Task that is used to remove expired entries from
210 * the datastore. This task will schedule itself
211 * again automatically to always delete all expired
214 * @param cls not used
215 * @param tc task context
218 delete_expired (void *cls,
219 const struct GNUNET_SCHEDULER_TaskContext *tc);
223 * Iterate over the expired items stored in the datastore.
224 * Delete all expired items; once we have processed all
225 * expired items, re-schedule the "delete_expired" task.
227 * @param cls not used
228 * @param next_cls closure to pass to the "next" function.
229 * @param key key for the content
230 * @param size number of bytes in data
231 * @param data content stored
232 * @param type type of the content
233 * @param priority priority of the content
234 * @param anonymity anonymity-level for the content
235 * @param expiration expiration time for the content
236 * @param uid unique identifier for the datum;
237 * maybe 0 if no unique identifier is available
239 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
240 * (continue on call to "next", of course),
241 * GNUNET_NO to delete the item and continue (if supported)
244 expired_processor (void *cls,
246 const GNUNET_HashCode * key,
252 struct GNUNET_TIME_Absolute
256 struct GNUNET_TIME_Absolute now;
258 expired_kill_task = GNUNET_SCHEDULER_NO_TASK;
262 = GNUNET_SCHEDULER_add_delayed (sched,
264 GNUNET_SCHEDULER_PRIORITY_IDLE,
265 GNUNET_SCHEDULER_NO_TASK,
269 return GNUNET_SYSERR;
271 now = GNUNET_TIME_absolute_get ();
272 if (expiration.value > now.value)
274 /* finished processing */
275 plugin->api->next_request (next_cls, GNUNET_YES);
276 return GNUNET_SYSERR;
278 plugin->api->next_request (next_cls, GNUNET_NO);
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Deleting content that expired %llu ms ago\n",
282 (unsigned long long) (now.value - expiration.value));
284 return GNUNET_NO; /* delete */
289 * Task that is used to remove expired entries from
290 * the datastore. This task will schedule itself
291 * again automatically to always delete all expired
294 * @param cls not used
295 * @param tc task context
298 delete_expired (void *cls,
299 const struct GNUNET_SCHEDULER_TaskContext *tc)
301 plugin->api->iter_ascending_expiration (plugin->api->cls,
309 * An iterator over a set of items stored in the datastore.
312 * @param next_cls closure to pass to the "next" function.
313 * @param key key for the content
314 * @param size number of bytes in data
315 * @param data content stored
316 * @param type type of the content
317 * @param priority priority of the content
318 * @param anonymity anonymity-level for the content
319 * @param expiration expiration time for the content
320 * @param uid unique identifier for the datum;
321 * maybe 0 if no unique identifier is available
323 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue
324 * (continue on call to "next", of course),
325 * GNUNET_NO to delete the item and continue (if supported)
330 const GNUNET_HashCode * key,
336 struct GNUNET_TIME_Absolute
340 unsigned long long *need = cls;
345 return GNUNET_SYSERR;
347 if (size + GNUNET_DATASTORE_ENTRY_OVERHEAD > *need)
350 *need -= size + GNUNET_DATASTORE_ENTRY_OVERHEAD;
351 plugin->api->next_request (next_cls,
352 (0 == *need) ? GNUNET_YES : GNUNET_NO);
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Deleting %llu bytes of low-priority content (still trying to recover %llu bytes)\n",
356 size + GNUNET_DATASTORE_ENTRY_OVERHEAD,
364 * Manage available disk space by running tasks
365 * that will discard content if necessary. This
366 * function will be run whenever a request for
367 * "need" bytes of storage could only be satisfied
368 * by eating into the "cache" (and we want our cache
371 * @param need number of bytes of content that were
372 * placed into the "cache" (and hence the
373 * number of bytes that should be removed).
376 manage_space (unsigned long long need)
378 unsigned long long *n;
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Asked to recover %llu bytes of cache space\n",
385 n = GNUNET_malloc (sizeof(unsigned long long));
387 plugin->api->iter_low_priority (plugin->api->cls,
395 * Function called to notify a client about the socket
396 * begin ready to queue more data. "buf" will be
397 * NULL and "size" zero if the socket was closed for
398 * writing in the meantime.
401 * @param size number of bytes available in buf
402 * @param buf where the callee should write the message
403 * @return number of bytes written to buf
406 transmit_callback (void *cls,
407 size_t size, void *buf)
409 struct TransmitCallbackContext *tcc = cls;
412 msize = ntohs(tcc->msg->size);
416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
417 "Transmission failed.\n");
420 tcc->tc (tcc->tc_cls, GNUNET_SYSERR);
421 if (GNUNET_YES == tcc->end)
423 GNUNET_SERVER_receive_done (tcc->client, GNUNET_SYSERR);
425 GNUNET_free (tcc->msg);
429 GNUNET_assert (size >= msize);
430 memcpy (buf, tcc->msg, msize);
432 tcc->tc (tcc->tc_cls, GNUNET_OK);
433 if (GNUNET_YES == tcc->end)
435 GNUNET_SERVER_receive_done (tcc->client, GNUNET_OK);
440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441 "Response transmitted, more pending!\n");
444 GNUNET_free (tcc->msg);
451 * Transmit the given message to the client.
453 * @param client target of the message
454 * @param msg message to transmit, will be freed!
455 * @param end is this the last response (and we should
456 * signal the server completion accodingly after
457 * transmitting this message)?
460 transmit (struct GNUNET_SERVER_Client *client,
461 struct GNUNET_MessageHeader *msg,
462 TransmitContinuation tc,
466 struct TransmitCallbackContext *tcc;
468 tcc = GNUNET_malloc (sizeof(struct TransmitCallbackContext));
470 tcc->client = client;
472 tcc->tc_cls = tc_cls;
476 GNUNET_SERVER_notify_transmit_ready (client,
478 GNUNET_TIME_UNIT_FOREVER_REL,
483 if (GNUNET_YES == end)
486 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
487 "Disconnecting client.\n");
489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
492 tc (tc_cls, GNUNET_SYSERR);
500 * Transmit a status code to the client.
502 * @param client receiver of the response
503 * @param code status code
504 * @param msg optional error message (can be NULL)
507 transmit_status (struct GNUNET_SERVER_Client *client,
511 struct StatusMessage *sm;
515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
516 "Transmitting `%s' message with value %d and message `%s'\n",
519 msg != NULL ? msg : "(none)");
521 slen = (msg == NULL) ? 0 : strlen(msg) + 1;
522 sm = GNUNET_malloc (sizeof(struct StatusMessage) + slen);
523 sm->header.size = htons(sizeof(struct StatusMessage) + slen);
524 sm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_STATUS);
525 sm->status = htonl(code);
526 memcpy (&sm[1], msg, slen);
527 transmit (client, &sm->header, NULL, NULL, GNUNET_YES);
532 * Function called once the transmit operation has
533 * either failed or succeeded.
536 * @param status GNUNET_OK on success, GNUNET_SYSERR on error
539 get_next(void *next_cls,
542 if (status != GNUNET_OK)
544 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
545 _("Failed to transmit an item to the client; aborting iteration.\n"));
546 plugin->api->next_request (next_cls, GNUNET_YES);
549 plugin->api->next_request (next_cls, GNUNET_NO);
554 * Function that will transmit the given datastore entry
557 * @param cls closure, pointer to the client (of type GNUNET_SERVER_Client).
558 * @param next_cls closure to use to ask for the next item
559 * @param key key for the content
560 * @param size number of bytes in data
561 * @param data content stored
562 * @param type type of the content
563 * @param priority priority of the content
564 * @param anonymity anonymity-level for the content
565 * @param expiration expiration time for the content
566 * @param uid unique identifier for the datum;
567 * maybe 0 if no unique identifier is available
569 * @return GNUNET_SYSERR to abort the iteration, GNUNET_OK to continue,
570 * GNUNET_NO to delete the item and continue (if supported)
573 transmit_item (void *cls,
575 const GNUNET_HashCode * key,
581 struct GNUNET_TIME_Absolute
582 expiration, uint64_t uid)
584 struct GNUNET_SERVER_Client *client = cls;
585 struct GNUNET_MessageHeader *end;
586 struct DataMessage *dm;
590 /* transmit 'DATA_END' */
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Transmitting `%s' message\n",
596 end = GNUNET_malloc (sizeof(struct GNUNET_MessageHeader));
597 end->size = htons(sizeof(struct GNUNET_MessageHeader));
598 end->type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA_END);
599 transmit (client, end, NULL, NULL, GNUNET_YES);
600 GNUNET_SERVER_client_drop (client);
603 dm = GNUNET_malloc (sizeof(struct DataMessage) + size);
604 dm->header.size = htons(sizeof(struct DataMessage) + size);
605 dm->header.type = htons(GNUNET_MESSAGE_TYPE_DATASTORE_DATA);
607 dm->size = htonl(size);
608 dm->type = htonl(type);
609 dm->priority = htonl(priority);
610 dm->anonymity = htonl(anonymity);
611 dm->expiration = GNUNET_TIME_absolute_hton(expiration);
612 dm->uid = GNUNET_htonll(uid);
614 memcpy (&dm[1], data, size);
616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
617 "Transmitting `%s' message\n",
620 transmit (client, &dm->header, &get_next, next_cls, GNUNET_NO);
626 * Handle RESERVE-message.
629 * @param client identification of the client
630 * @param message the actual message
633 handle_reserve (void *cls,
634 struct GNUNET_SERVER_Client *client,
635 const struct GNUNET_MessageHeader *message)
637 const struct ReserveMessage *msg = (const struct ReserveMessage*) message;
638 struct ReservationList *e;
639 unsigned long long used;
640 unsigned long long req;
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Processing `%s' request\n",
649 amount = GNUNET_ntohll(msg->amount);
650 entries = ntohl(msg->entries);
651 used = plugin->api->get_size (plugin->api->cls) + reserved;
652 req = amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * entries;
653 if (used + req > quota)
656 used = quota; /* cheat a bit for error message (to avoid negative numbers) */
657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
658 _("Insufficient space (%llu bytes are available) to satisfy `%s' request for %llu bytes\n"),
662 if (cache_size < req)
664 /* TODO: document this in the FAQ; essentially, if this
665 message happens, the insertion request could be blocked
666 by less-important content from migration because it is
667 larger than 1/8th of the overall available space, and
668 we only reserve 1/8th for "fresh" insertions */
669 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
670 _("The requested amount (%llu bytes) is larger than the cache size (%llu bytes)\n"),
673 transmit_status (client, 0,
674 gettext_noop ("Insufficient space to satisfy request and "
675 "requested amount is larger than cache size"));
679 transmit_status (client, 0,
680 gettext_noop ("Insufficient space to satisfy request"));
685 e = GNUNET_malloc (sizeof(struct ReservationList));
686 e->next = reservations;
690 e->entries = entries;
691 e->rid = ++reservation_gen;
692 if (reservation_gen < 0)
693 reservation_gen = 0; /* wrap around */
694 transmit_status (client, e->rid, NULL);
699 * Handle RELEASE_RESERVE-message.
702 * @param client identification of the client
703 * @param message the actual message
706 handle_release_reserve (void *cls,
707 struct GNUNET_SERVER_Client *client,
708 const struct GNUNET_MessageHeader *message)
710 const struct ReleaseReserveMessage *msg = (const struct ReleaseReserveMessage*) message;
711 struct ReservationList *pos;
712 struct ReservationList *prev;
713 struct ReservationList *next;
714 int rid = ntohl(msg->rid);
715 unsigned long long rem;
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
719 "Processing `%s' request\n",
724 while (NULL != (pos = next))
733 rem = pos->amount + ((unsigned long long) GNUNET_DATASTORE_ENTRY_OVERHEAD) * pos->entries;
734 GNUNET_assert (reserved >= rem);
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Returning %llu remaining reserved bytes to storage pool\n",
742 transmit_status (client, GNUNET_OK, NULL);
749 transmit_status (client, GNUNET_SYSERR, gettext_noop ("Could not find matching reservation"));
754 * Check that the given message is a valid data message.
756 * @return NULL if the message is not well-formed, otherwise the message
758 static const struct DataMessage *
759 check_data (const struct GNUNET_MessageHeader *message)
763 const struct DataMessage *dm;
765 size = ntohs(message->size);
766 if (size < sizeof(struct DataMessage))
771 dm = (const struct DataMessage *) message;
772 dsize = ntohl(dm->size);
773 if (size != dsize + sizeof(struct DataMessage))
783 * Handle PUT-message.
786 * @param client identification of the client
787 * @param message the actual message
790 handle_put (void *cls,
791 struct GNUNET_SERVER_Client *client,
792 const struct GNUNET_MessageHeader *message)
794 const struct DataMessage *dm = check_data (message);
798 struct ReservationList *pos;
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Processing `%s' request\n",
806 if (ntohl(dm->type) == 0)
814 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
817 rid = ntohl(dm->rid);
818 size = ntohl(dm->size);
822 while ( (NULL != pos) &&
825 GNUNET_break (pos != NULL);
828 GNUNET_break (pos->entries > 0);
829 GNUNET_break (pos->amount > size);
832 reserved -= (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
836 ret = plugin->api->put (plugin->api->cls,
842 ntohl(dm->anonymity),
843 GNUNET_TIME_absolute_ntoh(dm->expiration),
845 if (GNUNET_OK == ret)
846 GNUNET_CONTAINER_bloomfilter_add (filter,
848 transmit_status (client,
849 (GNUNET_SYSERR == ret) ? GNUNET_SYSERR : GNUNET_OK,
851 GNUNET_free_non_null (msg);
852 if (quota - reserved - cache_size < plugin->api->get_size (plugin->api->cls))
853 manage_space (size + GNUNET_DATASTORE_ENTRY_OVERHEAD);
858 * Handle GET-message.
861 * @param client identification of the client
862 * @param message the actual message
865 handle_get (void *cls,
866 struct GNUNET_SERVER_Client *client,
867 const struct GNUNET_MessageHeader *message)
869 static struct GNUNET_TIME_Absolute zero;
870 const struct GetMessage *msg;
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
875 "Processing `%s' request\n",
878 size = ntohs(message->size);
879 if ( (size != sizeof(struct GetMessage)) &&
880 (size != sizeof(struct GetMessage) - sizeof(GNUNET_HashCode)) )
883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
886 msg = (const struct GetMessage*) message;
887 if ( (size == sizeof(struct GetMessage)) &&
888 (GNUNET_YES != GNUNET_CONTAINER_bloomfilter_test (filter,
891 /* don't bother database... */
893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
894 "Empty result set for `%s' request.\n",
897 transmit_item (client,
898 NULL, NULL, 0, NULL, 0, 0, 0, zero, 0);
901 GNUNET_SERVER_client_keep (client);
902 plugin->api->get (plugin->api->cls,
903 ((size == sizeof(struct GetMessage)) ? &msg->key : NULL),
912 * Handle UPDATE-message.
915 * @param client identification of the client
916 * @param message the actual message
919 handle_update (void *cls,
920 struct GNUNET_SERVER_Client *client,
921 const struct GNUNET_MessageHeader *message)
923 const struct UpdateMessage *msg;
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "Processing `%s' request\n",
932 msg = (const struct UpdateMessage*) message;
934 ret = plugin->api->update (plugin->api->cls,
935 GNUNET_ntohll(msg->uid),
936 (int32_t) ntohl(msg->priority),
937 GNUNET_TIME_absolute_ntoh(msg->expiration),
939 transmit_status (client, ret, emsg);
940 GNUNET_free_non_null (emsg);
945 * Handle GET_RANDOM-message.
948 * @param client identification of the client
949 * @param message the actual message
952 handle_get_random (void *cls,
953 struct GNUNET_SERVER_Client *client,
954 const struct GNUNET_MessageHeader *message)
957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
958 "Processing `%s' request\n",
961 GNUNET_SERVER_client_keep (client);
962 plugin->api->iter_migration_order (plugin->api->cls,
970 * Context for the 'remove_callback'.
975 * Client for whom we're doing the remvoing.
977 struct GNUNET_SERVER_Client *client;
980 * GNUNET_YES if we managed to remove something.
987 * Callback function that will cause the item that is passed
988 * in to be deleted (by returning GNUNET_NO).
991 remove_callback (void *cls,
993 const GNUNET_HashCode * key,
999 struct GNUNET_TIME_Absolute
1000 expiration, uint64_t uid)
1002 struct RemoveContext *rc = cls;
1007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1008 "No further matches for `%s' request.\n",
1011 if (GNUNET_YES == rc->found)
1012 transmit_status (rc->client, GNUNET_OK, NULL);
1014 transmit_status (rc->client, GNUNET_SYSERR, _("Content not found"));
1015 GNUNET_SERVER_client_drop (rc->client);
1017 return GNUNET_OK; /* last item */
1019 rc->found = GNUNET_YES;
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "Item %llu matches `%s' request.\n",
1023 (unsigned long long) uid,
1026 GNUNET_CONTAINER_bloomfilter_remove (filter,
1028 plugin->api->next_request (next_cls, GNUNET_YES);
1034 * Handle REMOVE-message.
1036 * @param cls closure
1037 * @param client identification of the client
1038 * @param message the actual message
1041 handle_remove (void *cls,
1042 struct GNUNET_SERVER_Client *client,
1043 const struct GNUNET_MessageHeader *message)
1045 const struct DataMessage *dm = check_data (message);
1046 GNUNET_HashCode vhash;
1047 struct RemoveContext *rc;
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "Processing `%s' request\n",
1057 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
1060 rc = GNUNET_malloc (sizeof(struct RemoveContext));
1061 GNUNET_SERVER_client_keep (client);
1062 rc->client = client;
1063 GNUNET_CRYPTO_hash (&dm[1],
1066 GNUNET_SERVER_client_keep (client);
1067 plugin->api->get (plugin->api->cls,
1077 * Handle DROP-message.
1079 * @param cls closure
1080 * @param client identification of the client
1081 * @param message the actual message
1084 handle_drop (void *cls,
1085 struct GNUNET_SERVER_Client *client,
1086 const struct GNUNET_MessageHeader *message)
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Processing `%s' request\n",
1093 plugin->api->drop (plugin->api->cls);
1094 GNUNET_SERVER_receive_done (client, GNUNET_OK);
1099 * List of handlers for the messages understood by this
1102 static struct GNUNET_SERVER_MessageHandler handlers[] = {
1103 {&handle_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RESERVE,
1104 sizeof(struct ReserveMessage) },
1105 {&handle_release_reserve, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_RELEASE_RESERVE,
1106 sizeof(struct ReleaseReserveMessage) },
1107 {&handle_put, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_PUT, 0 },
1108 {&handle_update, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_UPDATE,
1109 sizeof (struct UpdateMessage) },
1110 {&handle_get, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET, 0 },
1111 {&handle_get_random, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_GET_RANDOM,
1112 sizeof(struct GNUNET_MessageHeader) },
1113 {&handle_remove, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_REMOVE, 0 },
1114 {&handle_drop, NULL, GNUNET_MESSAGE_TYPE_DATASTORE_DROP,
1115 sizeof(struct GNUNET_MessageHeader) },
1122 * Load the datastore plugin.
1124 static struct DatastorePlugin *
1127 struct DatastorePlugin *ret;
1132 GNUNET_CONFIGURATION_get_value_string (cfg,
1133 "DATASTORE", "DATABASE", &name))
1135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1136 _("No `%s' specified for `%s' in configuration!\n"),
1141 ret = GNUNET_malloc (sizeof(struct DatastorePlugin));
1143 ret->env.sched = sched;
1144 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1145 _("Loading `%s' datastore plugin\n"), name);
1146 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
1147 ret->short_name = name;
1148 ret->lib_name = libname;
1149 ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
1150 if (ret->api == NULL)
1152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1153 _("Failed to load datastore plugin for `%s'\n"), name);
1154 GNUNET_free (ret->short_name);
1155 GNUNET_free (libname);
1164 * Function called when the service shuts
1165 * down. Unloads our datastore plugin.
1167 * @param plug plugin to unload
1170 unload_plugin (struct DatastorePlugin *plug)
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "Datastore service is unloading plugin...\n");
1176 GNUNET_break (NULL == GNUNET_PLUGIN_unload (plug->lib_name, plug->api));
1177 GNUNET_free (plug->lib_name);
1178 GNUNET_free (plug->short_name);
1184 * Last task run during shutdown. Disconnects us from
1185 * the transport and core.
1188 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1190 unload_plugin (plugin);
1196 * Function that removes all active reservations made
1197 * by the given client and releases the space for other
1200 * @param cls closure
1201 * @param client identification of the client
1204 cleanup_reservations (void *cls,
1205 struct GNUNET_SERVER_Client
1208 struct ReservationList *pos;
1209 struct ReservationList *prev;
1210 struct ReservationList *next;
1217 if (pos->client == client)
1220 reservations = next;
1223 reserved -= pos->amount + pos->entries * GNUNET_DATASTORE_ENTRY_OVERHEAD;
1236 * Process datastore requests.
1238 * @param cls closure
1239 * @param s scheduler to use
1240 * @param server the initialized server
1241 * @param c configuration to use
1245 struct GNUNET_SCHEDULER_Handle *s,
1246 struct GNUNET_SERVER_Handle *server,
1247 struct GNUNET_CONFIGURATION_Handle *c)
1250 unsigned int bf_size;
1255 GNUNET_CONFIGURATION_get_value_number (cfg,
1256 "DATASTORE", "QUOTA", "a))
1258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1259 _("No `%s' specified for `%s' in configuration!\n"),
1264 cache_size = quota / 8; /* Or should we make this an option? */
1265 bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
1268 GNUNET_CONFIGURATION_get_value_filename (cfg,
1273 GNUNET_DISK_directory_create_for_file (fn)) )
1275 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1276 _("Could not use specified filename `%s' for bloomfilter.\n"),
1277 fn != NULL ? fn : "");
1278 GNUNET_free_non_null (fn);
1281 filter = GNUNET_CONTAINER_bloomfilter_load (fn, bf_size, 5); /* approx. 3% false positives at max use */
1282 GNUNET_free_non_null (fn);
1285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1286 _("Failed to initialize bloomfilter.\n"));
1289 plugin = load_plugin ();
1292 GNUNET_CONTAINER_bloomfilter_free (filter);
1295 GNUNET_SERVER_disconnect_notify (server, &cleanup_reservations, NULL);
1296 GNUNET_SERVER_add_handlers (server, handlers);
1298 = GNUNET_SCHEDULER_add_delayed (sched,
1300 GNUNET_SCHEDULER_PRIORITY_IDLE,
1301 GNUNET_SCHEDULER_NO_TASK,
1302 GNUNET_TIME_UNIT_ZERO,
1303 &delete_expired, NULL);
1304 GNUNET_SCHEDULER_add_delayed (sched,
1306 GNUNET_SCHEDULER_PRIORITY_IDLE,
1307 GNUNET_SCHEDULER_NO_TASK,
1308 GNUNET_TIME_UNIT_FOREVER_REL,
1309 &cleaning_task, NULL);
1315 * The main function for the datastore service.
1317 * @param argc number of arguments from the command line
1318 * @param argv command line arguments
1319 * @return 0 ok, 1 on error
1322 main (int argc, char *const *argv)
1327 GNUNET_SERVICE_run (argc,
1329 "datastore", &run, NULL, NULL, NULL)) ? 0 : 1;
1334 /* end of gnunet-service-datastore.c */