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