datacache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010, 2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file datacache/datacache.c
  18. * @brief datacache API implementation
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_datacache_lib.h"
  24. #include "gnunet_statistics_service.h"
  25. #include "gnunet_datacache_plugin.h"
  26. #define LOG(kind, ...) GNUNET_log_from (kind, "datacache", __VA_ARGS__)
  27. #define LOG_STRERROR_FILE(kind, op, fn) \
  28. GNUNET_log_from_strerror_file (kind, "datacache", op, fn)
  29. /**
  30. * Internal state of the datacache library.
  31. */
  32. struct GNUNET_DATACACHE_Handle
  33. {
  34. /**
  35. * Bloomfilter to quickly tell if we don't have the content.
  36. */
  37. struct GNUNET_CONTAINER_BloomFilter *filter;
  38. /**
  39. * Our configuration.
  40. */
  41. const struct GNUNET_CONFIGURATION_Handle *cfg;
  42. /**
  43. * Opaque handle for the statistics service.
  44. */
  45. struct GNUNET_STATISTICS_Handle *stats;
  46. /**
  47. * Configuration section to use.
  48. */
  49. char *section;
  50. /**
  51. * API of the transport as returned by the plugin's
  52. * initialization function.
  53. */
  54. struct GNUNET_DATACACHE_PluginFunctions *api;
  55. /**
  56. * Short name for the plugin (i.e. "sqlite").
  57. */
  58. char *short_name;
  59. /**
  60. * Name of the library (i.e. "gnunet_plugin_datacache_sqlite").
  61. */
  62. char *lib_name;
  63. /**
  64. * Name for the bloom filter file.
  65. */
  66. char *bloom_name;
  67. /**
  68. * Environment provided to our plugin.
  69. */
  70. struct GNUNET_DATACACHE_PluginEnvironment env;
  71. /**
  72. * How much space is in use right now?
  73. */
  74. unsigned long long utilization;
  75. };
  76. /**
  77. * Function called by plugins to notify the datacache
  78. * about content deletions.
  79. *
  80. * @param cls closure
  81. * @param key key of the content that was deleted
  82. * @param size number of bytes that were made available
  83. */
  84. static void
  85. env_delete_notify (void *cls, const struct GNUNET_HashCode *key, size_t size)
  86. {
  87. struct GNUNET_DATACACHE_Handle *h = cls;
  88. LOG (GNUNET_ERROR_TYPE_DEBUG,
  89. "Content under key `%s' discarded\n",
  90. GNUNET_h2s (key));
  91. GNUNET_assert (h->utilization >= size);
  92. h->utilization -= size;
  93. GNUNET_CONTAINER_bloomfilter_remove (h->filter, key);
  94. GNUNET_STATISTICS_update (h->stats,
  95. gettext_noop ("# bytes stored"),
  96. -(long long) size,
  97. GNUNET_NO);
  98. GNUNET_STATISTICS_update (h->stats,
  99. gettext_noop ("# items stored"),
  100. -1,
  101. GNUNET_NO);
  102. }
  103. /**
  104. * Create a data cache.
  105. *
  106. * @param cfg configuration to use
  107. * @param section section in the configuration that contains our options
  108. * @return handle to use to access the service
  109. */
  110. struct GNUNET_DATACACHE_Handle *
  111. GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
  112. const char *section)
  113. {
  114. unsigned int bf_size;
  115. unsigned long long quota;
  116. struct GNUNET_DATACACHE_Handle *ret;
  117. char *libname;
  118. char *name;
  119. if (GNUNET_OK !=
  120. GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", &quota))
  121. {
  122. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "QUOTA");
  123. return NULL;
  124. }
  125. if (GNUNET_OK !=
  126. GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
  127. {
  128. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "DATABASE");
  129. return NULL;
  130. }
  131. bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
  132. ret = GNUNET_new (struct GNUNET_DATACACHE_Handle);
  133. if (GNUNET_YES !=
  134. GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF"))
  135. {
  136. if (GNUNET_YES !=
  137. GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC"))
  138. {
  139. ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
  140. }
  141. if (NULL != ret->bloom_name)
  142. {
  143. ret->filter = GNUNET_CONTAINER_bloomfilter_load (
  144. ret->bloom_name,
  145. quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
  146. 5);
  147. }
  148. if (NULL == ret->filter)
  149. {
  150. ret->filter =
  151. GNUNET_CONTAINER_bloomfilter_init (NULL,
  152. bf_size,
  153. 5); /* approx. 3% false positives at max use */
  154. }
  155. }
  156. ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
  157. ret->section = GNUNET_strdup (section);
  158. ret->env.cfg = cfg;
  159. ret->env.delete_notify = &env_delete_notify;
  160. ret->env.section = ret->section;
  161. ret->env.cls = ret;
  162. ret->env.delete_notify = &env_delete_notify;
  163. ret->env.quota = quota;
  164. LOG (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' datacache plugin\n"), name);
  165. GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
  166. ret->short_name = name;
  167. ret->lib_name = libname;
  168. ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
  169. if (ret->api == NULL)
  170. {
  171. LOG (GNUNET_ERROR_TYPE_ERROR,
  172. _ ("Failed to load datacache plugin for `%s'\n"),
  173. name);
  174. GNUNET_DATACACHE_destroy (ret);
  175. return NULL;
  176. }
  177. return ret;
  178. }
  179. /**
  180. * Destroy a data cache (and free associated resources).
  181. *
  182. * @param h handle to the datastore
  183. */
  184. void
  185. GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
  186. {
  187. if (NULL != h->filter)
  188. GNUNET_CONTAINER_bloomfilter_free (h->filter);
  189. if (NULL != h->api)
  190. GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
  191. GNUNET_free (h->lib_name);
  192. GNUNET_free (h->short_name);
  193. GNUNET_free (h->section);
  194. if (NULL != h->bloom_name)
  195. {
  196. if (0 != unlink (h->bloom_name))
  197. GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  198. "datacache",
  199. "unlink",
  200. h->bloom_name);
  201. GNUNET_free (h->bloom_name);
  202. }
  203. GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
  204. GNUNET_free (h);
  205. }
  206. /**
  207. * Store an item in the datastore.
  208. *
  209. * @param h handle to the datacache
  210. * @param key key to store data under
  211. * @param xor_distance distance of @a key to our PID
  212. * @param data_size number of bytes in @a data
  213. * @param data data to store
  214. * @param type type of the value
  215. * @param discard_time when to discard the value in any case
  216. * @param path_info_len number of entries in @a path_info
  217. * @param path_info a path through the network
  218. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
  219. */
  220. int
  221. GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
  222. const struct GNUNET_HashCode *key,
  223. uint32_t xor_distance,
  224. size_t data_size,
  225. const char *data,
  226. enum GNUNET_BLOCK_Type type,
  227. struct GNUNET_TIME_Absolute discard_time,
  228. unsigned int path_info_len,
  229. const struct GNUNET_PeerIdentity *path_info)
  230. {
  231. ssize_t used;
  232. used = h->api->put (h->api->cls,
  233. key,
  234. xor_distance,
  235. data_size,
  236. data,
  237. type,
  238. discard_time,
  239. path_info_len,
  240. path_info);
  241. if (-1 == used)
  242. {
  243. GNUNET_break (0);
  244. return GNUNET_SYSERR;
  245. }
  246. if (0 == used)
  247. {
  248. /* duplicate */
  249. return GNUNET_NO;
  250. }
  251. LOG (GNUNET_ERROR_TYPE_DEBUG,
  252. "Stored data under key `%s' in cache\n",
  253. GNUNET_h2s (key));
  254. if (NULL != h->filter)
  255. GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
  256. GNUNET_STATISTICS_update (h->stats,
  257. gettext_noop ("# bytes stored"),
  258. used,
  259. GNUNET_NO);
  260. GNUNET_STATISTICS_update (h->stats,
  261. gettext_noop ("# items stored"),
  262. 1,
  263. GNUNET_NO);
  264. while (h->utilization + used > h->env.quota)
  265. GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
  266. h->utilization += used;
  267. return GNUNET_OK;
  268. }
  269. /**
  270. * Iterate over the results for a particular key
  271. * in the datacache.
  272. *
  273. * @param h handle to the datacache
  274. * @param key what to look up
  275. * @param type entries of which type are relevant?
  276. * @param iter maybe NULL (to just count)
  277. * @param iter_cls closure for @a iter
  278. * @return the number of results found
  279. */
  280. unsigned int
  281. GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
  282. const struct GNUNET_HashCode *key,
  283. enum GNUNET_BLOCK_Type type,
  284. GNUNET_DATACACHE_Iterator iter,
  285. void *iter_cls)
  286. {
  287. GNUNET_STATISTICS_update (h->stats,
  288. gettext_noop ("# requests received"),
  289. 1,
  290. GNUNET_NO);
  291. LOG (GNUNET_ERROR_TYPE_DEBUG,
  292. "Processing request for key `%s'\n",
  293. GNUNET_h2s (key));
  294. if ((NULL != h->filter) &&
  295. (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)))
  296. {
  297. GNUNET_STATISTICS_update (h->stats,
  298. gettext_noop (
  299. "# requests filtered by bloom filter"),
  300. 1,
  301. GNUNET_NO);
  302. LOG (GNUNET_ERROR_TYPE_DEBUG,
  303. "Bloomfilter filters request for key `%s'\n",
  304. GNUNET_h2s (key));
  305. return 0; /* can not be present */
  306. }
  307. return h->api->get (h->api->cls, key, type, iter, iter_cls);
  308. }
  309. /**
  310. * Obtain a random element from the datacache.
  311. *
  312. * @param h handle to the datacache
  313. * @param iter maybe NULL (to just count)
  314. * @param iter_cls closure for @a iter
  315. * @return the number of results found (zero or 1)
  316. */
  317. unsigned int
  318. GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h,
  319. GNUNET_DATACACHE_Iterator iter,
  320. void *iter_cls)
  321. {
  322. GNUNET_STATISTICS_update (h->stats,
  323. gettext_noop (
  324. "# requests for random value received"),
  325. 1,
  326. GNUNET_NO);
  327. LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n");
  328. return h->api->get_random (h->api->cls, iter, iter_cls);
  329. }
  330. /**
  331. * Iterate over the results that are "close" to a particular key in
  332. * the datacache. "close" is defined as numerically larger than @a
  333. * key (when interpreted as a circular address space), with small
  334. * distance.
  335. *
  336. * @param h handle to the datacache
  337. * @param key area of the keyspace to look into
  338. * @param num_results number of results that should be returned to @a iter
  339. * @param iter maybe NULL (to just count)
  340. * @param iter_cls closure for @a iter
  341. * @return the number of results found
  342. */
  343. unsigned int
  344. GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h,
  345. const struct GNUNET_HashCode *key,
  346. unsigned int num_results,
  347. GNUNET_DATACACHE_Iterator iter,
  348. void *iter_cls)
  349. {
  350. GNUNET_STATISTICS_update (h->stats,
  351. gettext_noop (
  352. "# proximity search requests received"),
  353. 1,
  354. GNUNET_NO);
  355. LOG (GNUNET_ERROR_TYPE_DEBUG,
  356. "Processing proximity search at `%s'\n",
  357. GNUNET_h2s (key));
  358. return h->api->get_closest (h->api->cls, key, num_results, iter, iter_cls);
  359. }
  360. /* end of datacache.c */