datacache.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. const struct GNUNET_OS_ProjectData *pd;
  120. if (GNUNET_OK !=
  121. GNUNET_CONFIGURATION_get_value_size (cfg, section, "QUOTA", &quota))
  122. {
  123. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "QUOTA");
  124. return NULL;
  125. }
  126. if (GNUNET_OK !=
  127. GNUNET_CONFIGURATION_get_value_string (cfg, section, "DATABASE", &name))
  128. {
  129. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, section, "DATABASE");
  130. return NULL;
  131. }
  132. bf_size = quota / 32; /* 8 bit per entry, 1 bit per 32 kb in DB */
  133. ret = GNUNET_new (struct GNUNET_DATACACHE_Handle);
  134. if (GNUNET_YES !=
  135. GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF"))
  136. {
  137. if (GNUNET_YES !=
  138. GNUNET_CONFIGURATION_get_value_yesno (cfg, section, "DISABLE_BF_RC"))
  139. {
  140. ret->bloom_name = GNUNET_DISK_mktemp ("gnunet-datacachebloom");
  141. }
  142. if (NULL != ret->bloom_name)
  143. {
  144. ret->filter = GNUNET_CONTAINER_bloomfilter_load (
  145. ret->bloom_name,
  146. quota / 1024, /* 8 bit per entry in DB, expect 1k entries */
  147. 5);
  148. }
  149. if (NULL == ret->filter)
  150. {
  151. ret->filter =
  152. GNUNET_CONTAINER_bloomfilter_init (NULL,
  153. bf_size,
  154. 5); /* approx. 3% false positives at max use */
  155. }
  156. }
  157. ret->stats = GNUNET_STATISTICS_create ("datacache", cfg);
  158. ret->section = GNUNET_strdup (section);
  159. ret->env.cfg = cfg;
  160. ret->env.delete_notify = &env_delete_notify;
  161. ret->env.section = ret->section;
  162. ret->env.cls = ret;
  163. ret->env.delete_notify = &env_delete_notify;
  164. ret->env.quota = quota;
  165. LOG (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' datacache plugin\n"), name);
  166. GNUNET_asprintf (&libname, "libgnunet_plugin_datacache_%s", name);
  167. ret->short_name = name;
  168. ret->lib_name = libname;
  169. /* Load the plugin within GNUnet's default context */
  170. pd = GNUNET_OS_project_data_get ();
  171. GNUNET_OS_init(GNUNET_OS_project_data_default ());
  172. ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
  173. GNUNET_OS_init(pd);
  174. if (NULL == ret->api)
  175. {
  176. /* Try to load the plugin within the application's context
  177. This normally happens when the application is not GNUnet itself but a
  178. third party; inside GNUnet this is effectively a double failure. */
  179. ret->api = GNUNET_PLUGIN_load (libname, &ret->env);
  180. if (NULL == ret->api)
  181. {
  182. LOG (GNUNET_ERROR_TYPE_ERROR,
  183. _ ("Failed to load datacache plugin for `%s'\n"),
  184. name);
  185. GNUNET_DATACACHE_destroy (ret);
  186. return NULL;
  187. }
  188. }
  189. return ret;
  190. }
  191. /**
  192. * Destroy a data cache (and free associated resources).
  193. *
  194. * @param h handle to the datastore
  195. */
  196. void
  197. GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h)
  198. {
  199. if (NULL != h->filter)
  200. GNUNET_CONTAINER_bloomfilter_free (h->filter);
  201. if (NULL != h->api)
  202. GNUNET_break (NULL == GNUNET_PLUGIN_unload (h->lib_name, h->api));
  203. GNUNET_free (h->lib_name);
  204. GNUNET_free (h->short_name);
  205. GNUNET_free (h->section);
  206. if (NULL != h->bloom_name)
  207. {
  208. if (0 != unlink (h->bloom_name))
  209. GNUNET_log_from_strerror_file (GNUNET_ERROR_TYPE_WARNING,
  210. "datacache",
  211. "unlink",
  212. h->bloom_name);
  213. GNUNET_free (h->bloom_name);
  214. }
  215. GNUNET_STATISTICS_destroy (h->stats, GNUNET_NO);
  216. GNUNET_free (h);
  217. }
  218. /**
  219. * Store an item in the datastore.
  220. *
  221. * @param h handle to the datacache
  222. * @param key key to store data under
  223. * @param xor_distance distance of @a key to our PID
  224. * @param data_size number of bytes in @a data
  225. * @param data data to store
  226. * @param type type of the value
  227. * @param discard_time when to discard the value in any case
  228. * @param path_info_len number of entries in @a path_info
  229. * @param path_info a path through the network
  230. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
  231. */
  232. int
  233. GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
  234. const struct GNUNET_HashCode *key,
  235. uint32_t xor_distance,
  236. size_t data_size,
  237. const char *data,
  238. enum GNUNET_BLOCK_Type type,
  239. struct GNUNET_TIME_Absolute discard_time,
  240. unsigned int path_info_len,
  241. const struct GNUNET_PeerIdentity *path_info)
  242. {
  243. ssize_t used;
  244. used = h->api->put (h->api->cls,
  245. key,
  246. xor_distance,
  247. data_size,
  248. data,
  249. type,
  250. discard_time,
  251. path_info_len,
  252. path_info);
  253. if (-1 == used)
  254. {
  255. GNUNET_break (0);
  256. return GNUNET_SYSERR;
  257. }
  258. if (0 == used)
  259. {
  260. /* duplicate */
  261. return GNUNET_NO;
  262. }
  263. LOG (GNUNET_ERROR_TYPE_DEBUG,
  264. "Stored data under key `%s' in cache\n",
  265. GNUNET_h2s (key));
  266. if (NULL != h->filter)
  267. GNUNET_CONTAINER_bloomfilter_add (h->filter, key);
  268. GNUNET_STATISTICS_update (h->stats,
  269. gettext_noop ("# bytes stored"),
  270. used,
  271. GNUNET_NO);
  272. GNUNET_STATISTICS_update (h->stats,
  273. gettext_noop ("# items stored"),
  274. 1,
  275. GNUNET_NO);
  276. while (h->utilization + used > h->env.quota)
  277. GNUNET_assert (GNUNET_OK == h->api->del (h->api->cls));
  278. h->utilization += used;
  279. return GNUNET_OK;
  280. }
  281. /**
  282. * Iterate over the results for a particular key
  283. * in the datacache.
  284. *
  285. * @param h handle to the datacache
  286. * @param key what to look up
  287. * @param type entries of which type are relevant?
  288. * @param iter maybe NULL (to just count)
  289. * @param iter_cls closure for @a iter
  290. * @return the number of results found
  291. */
  292. unsigned int
  293. GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
  294. const struct GNUNET_HashCode *key,
  295. enum GNUNET_BLOCK_Type type,
  296. GNUNET_DATACACHE_Iterator iter,
  297. void *iter_cls)
  298. {
  299. GNUNET_STATISTICS_update (h->stats,
  300. gettext_noop ("# requests received"),
  301. 1,
  302. GNUNET_NO);
  303. LOG (GNUNET_ERROR_TYPE_DEBUG,
  304. "Processing request for key `%s'\n",
  305. GNUNET_h2s (key));
  306. if ((NULL != h->filter) &&
  307. (GNUNET_OK != GNUNET_CONTAINER_bloomfilter_test (h->filter, key)))
  308. {
  309. GNUNET_STATISTICS_update (h->stats,
  310. gettext_noop (
  311. "# requests filtered by bloom filter"),
  312. 1,
  313. GNUNET_NO);
  314. LOG (GNUNET_ERROR_TYPE_DEBUG,
  315. "Bloomfilter filters request for key `%s'\n",
  316. GNUNET_h2s (key));
  317. return 0; /* can not be present */
  318. }
  319. return h->api->get (h->api->cls, key, type, iter, iter_cls);
  320. }
  321. /**
  322. * Obtain a random element from the datacache.
  323. *
  324. * @param h handle to the datacache
  325. * @param iter maybe NULL (to just count)
  326. * @param iter_cls closure for @a iter
  327. * @return the number of results found (zero or 1)
  328. */
  329. unsigned int
  330. GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h,
  331. GNUNET_DATACACHE_Iterator iter,
  332. void *iter_cls)
  333. {
  334. GNUNET_STATISTICS_update (h->stats,
  335. gettext_noop (
  336. "# requests for random value received"),
  337. 1,
  338. GNUNET_NO);
  339. LOG (GNUNET_ERROR_TYPE_DEBUG, "Processing request for random value\n");
  340. return h->api->get_random (h->api->cls, iter, iter_cls);
  341. }
  342. /**
  343. * Iterate over the results that are "close" to a particular key in
  344. * the datacache. "close" is defined as numerically larger than @a
  345. * key (when interpreted as a circular address space), with small
  346. * distance.
  347. *
  348. * @param h handle to the datacache
  349. * @param key area of the keyspace to look into
  350. * @param num_results number of results that should be returned to @a iter
  351. * @param iter maybe NULL (to just count)
  352. * @param iter_cls closure for @a iter
  353. * @return the number of results found
  354. */
  355. unsigned int
  356. GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h,
  357. const struct GNUNET_HashCode *key,
  358. unsigned int num_results,
  359. GNUNET_DATACACHE_Iterator iter,
  360. void *iter_cls)
  361. {
  362. GNUNET_STATISTICS_update (h->stats,
  363. gettext_noop (
  364. "# proximity search requests received"),
  365. 1,
  366. GNUNET_NO);
  367. LOG (GNUNET_ERROR_TYPE_DEBUG,
  368. "Processing proximity search at `%s'\n",
  369. GNUNET_h2s (key));
  370. return h->api->get_closest (h->api->cls, key, num_results, iter, iter_cls);
  371. }
  372. /* end of datacache.c */