plugin_datacache_template.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2006, 2009, 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/plugin_datacache_template.c
  18. * @brief template for an implementation of a database backend for the datacache
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_datacache_plugin.h"
  24. /**
  25. * Context for all functions in this plugin.
  26. */
  27. struct Plugin
  28. {
  29. /**
  30. * Our execution environment.
  31. */
  32. struct GNUNET_DATACACHE_PluginEnvironment *env;
  33. };
  34. /**
  35. * Store an item in the datastore.
  36. *
  37. * @param cls closure (our `struct Plugin`)
  38. * @param key key to store @a data under
  39. * @param xor_distance distance of @a key to our PID
  40. * @param size number of bytes in @a data
  41. * @param data data to store
  42. * @param type type of the value
  43. * @param discard_time when to discard the value in any case
  44. * @param path_info_len number of entries in @a path_info
  45. * @param path_info a path through the network
  46. * @return 0 if duplicate, -1 on error, number of bytes used otherwise
  47. */
  48. static ssize_t
  49. template_plugin_put (void *cls,
  50. const struct GNUNET_HashCode *key,
  51. uint32_t xor_distance,
  52. size_t size,
  53. const char *data,
  54. enum GNUNET_BLOCK_Type type,
  55. struct GNUNET_TIME_Absolute discard_time,
  56. unsigned int path_info_len,
  57. const struct GNUNET_PeerIdentity *path_info)
  58. {
  59. GNUNET_break (0);
  60. return -1;
  61. }
  62. /**
  63. * Iterate over the results for a particular key
  64. * in the datastore.
  65. *
  66. * @param cls closure (our `struct Plugin`)
  67. * @param key
  68. * @param type entries of which type are relevant?
  69. * @param iter maybe NULL (to just count)
  70. * @param iter_cls closure for @a iter
  71. * @return the number of results found
  72. */
  73. static unsigned int
  74. template_plugin_get (void *cls,
  75. const struct GNUNET_HashCode *key,
  76. enum GNUNET_BLOCK_Type type,
  77. GNUNET_DATACACHE_Iterator iter,
  78. void *iter_cls)
  79. {
  80. GNUNET_break (0);
  81. return 0;
  82. }
  83. /**
  84. * Delete the entry with the lowest expiration value
  85. * from the datacache right now.
  86. *
  87. * @param cls closure (our `struct Plugin`)
  88. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  89. */
  90. static int
  91. template_plugin_del (void *cls)
  92. {
  93. GNUNET_break (0);
  94. return GNUNET_SYSERR;
  95. }
  96. /**
  97. * Return a random value from the datastore.
  98. *
  99. * @param cls closure (internal context for the plugin)
  100. * @param iter maybe NULL (to just count)
  101. * @param iter_cls closure for @a iter
  102. * @return the number of results found (zero or one)
  103. */
  104. static unsigned int
  105. template_plugin_get_random (void *cls,
  106. GNUNET_DATACACHE_Iterator iter,
  107. void *iter_cls)
  108. {
  109. GNUNET_break (0);
  110. return 0;
  111. }
  112. /**
  113. * Iterate over the results that are "close" to a particular key in
  114. * the datacache. "close" is defined as numerically larger than @a
  115. * key (when interpreted as a circular address space), with small
  116. * distance.
  117. *
  118. * @param cls closure (internal context for the plugin)
  119. * @param key area of the keyspace to look into
  120. * @param num_results number of results that should be returned to @a iter
  121. * @param iter maybe NULL (to just count)
  122. * @param iter_cls closure for @a iter
  123. * @return the number of results found
  124. */
  125. static unsigned int
  126. template_plugin_get_closest (void *cls,
  127. const struct GNUNET_HashCode *key,
  128. unsigned int num_results,
  129. GNUNET_DATACACHE_Iterator iter,
  130. void *iter_cls)
  131. {
  132. GNUNET_break (0);
  133. return 0;
  134. }
  135. /**
  136. * Entry point for the plugin.
  137. *
  138. * @param cls closure (the `struct GNUNET_DATACACHE_PluginEnvironmnet`)
  139. * @return the plugin's closure (our `struct Plugin`)
  140. */
  141. void *
  142. libgnunet_plugin_datacache_template_init (void *cls)
  143. {
  144. struct GNUNET_DATACACHE_PluginEnvironment *env = cls;
  145. struct GNUNET_DATACACHE_PluginFunctions *api;
  146. struct Plugin *plugin;
  147. plugin = GNUNET_new (struct Plugin);
  148. plugin->env = env;
  149. api = GNUNET_new (struct GNUNET_DATACACHE_PluginFunctions);
  150. api->cls = plugin;
  151. api->get = &template_plugin_get;
  152. api->put = &template_plugin_put;
  153. api->del = &template_plugin_del;
  154. api->get_random = &template_plugin_get_random;
  155. api->get_closest = &template_plugin_get_closest;
  156. GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
  157. "template",
  158. "Template datacache running\n");
  159. return api;
  160. }
  161. /**
  162. * Exit point from the plugin.
  163. *
  164. * @param cls closure (our `struct Plugin`)
  165. * @return NULL
  166. */
  167. void *
  168. libgnunet_plugin_datacache_template_done (void *cls)
  169. {
  170. struct GNUNET_DATACACHE_PluginFunctions *api = cls;
  171. struct Plugin *plugin = api->cls;
  172. GNUNET_free (plugin);
  173. GNUNET_free (api);
  174. return NULL;
  175. }
  176. /* end of plugin_datacache_template.c */