maybe
[oweals/gnunet.git] / src / datastore / plugin_datastore_template.c
1 /*
2      This file is part of GNUnet
3      (C) 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file datastore/plugin_datastore_template.c
23  * @brief template-based datastore backend
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_datastore_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_DATASTORE_PluginEnvironment *env;
40 };
41
42
43 /**
44  * Get an estimate of how much space the database is
45  * currently using.
46  *
47  * @param cls our "struct Plugin*"
48  * @return number of bytes used on disk
49  */
50 static unsigned long long template_plugin_get_size (void *cls)
51 {
52   GNUNET_break (0);
53   return 0;
54 }
55
56
57 /**
58  * Store an item in the datastore.
59  *
60  * @param cls closure
61  * @param key key for the item
62  * @param size number of bytes in data
63  * @param data content stored
64  * @param type type of the content
65  * @param priority priority of the content
66  * @param anonymity anonymity-level for the content
67  * @param replication replication-level for the content
68  * @param expiration expiration time for the content
69  * @param msg set to error message
70  * @return GNUNET_OK on success
71  */
72 static int
73 template_plugin_put (void *cls,
74                      const GNUNET_HashCode * key,
75                      uint32_t size,
76                      const void *data,
77                      enum GNUNET_BLOCK_Type type,
78                      uint32_t priority,
79                      uint32_t anonymity,
80                      uint32_t replication,
81                      struct GNUNET_TIME_Absolute expiration,
82                      char **msg)
83 {
84   GNUNET_break (0);
85   *msg = GNUNET_strdup ("not implemented");
86   return GNUNET_SYSERR;
87 }
88
89
90 /**
91  * Function invoked on behalf of a "PluginIterator"
92  * asking the database plugin to call the iterator
93  * with the next item.
94  *
95  * @param next_cls whatever argument was given
96  *        to the PluginIterator as "next_cls".
97  * @param end_it set to GNUNET_YES if we
98  *        should terminate the iteration early
99  *        (iterator should be still called once more
100  *         to signal the end of the iteration).
101  */
102 static void 
103 template_plugin_next_request (void *next_cls,
104                        int end_it)
105 {
106   GNUNET_break (0);
107 }
108
109
110 /**
111  * Iterate over the results for a particular key
112  * in the datastore.
113  *
114  * @param cls closure
115  * @param key maybe NULL (to match all entries)
116  * @param vhash hash of the value, maybe NULL (to
117  *        match all values that have the right key).
118  *        Note that for DBlocks there is no difference
119  *        betwen key and vhash, but for other blocks
120  *        there may be!
121  * @param type entries of which type are relevant?
122  *     Use 0 for any type.
123  * @param iter function to call on each matching value;
124  *        will be called once with a NULL value at the end
125  * @param iter_cls closure for iter
126  */
127 static void
128 template_plugin_get (void *cls,
129                      const GNUNET_HashCode * key,
130                      const GNUNET_HashCode * vhash,
131                      enum GNUNET_BLOCK_Type type,
132                      PluginIterator iter, void *iter_cls)
133 {
134   GNUNET_break (0);
135 }
136
137
138
139 /**
140  * Get a random item for replication.  Returns a single, not expired, random item
141  * from those with the highest replication counters.  The item's 
142  * replication counter is decremented by one IF it was positive before.
143  * Call 'iter' with all values ZERO or NULL if the datastore is empty.
144  *
145  * @param cls closure
146  * @param iter function to call the value (once only).
147  * @param iter_cls closure for iter
148  */
149 static void
150 template_plugin_replication_get (void *cls,
151                                  PluginIterator iter, void *iter_cls)
152 {
153   GNUNET_break (0);
154 }
155
156
157 /**
158  * Get a random item for expiration.
159  * Call 'iter' with all values ZERO or NULL if the datastore is empty.
160  *
161  * @param cls closure
162  * @param iter function to call the value (once only).
163  * @param iter_cls closure for iter
164  */
165 static void
166 template_plugin_expiration_get (void *cls,
167                                 PluginIterator iter, void *iter_cls)
168 {
169   GNUNET_break (0);
170 }
171
172
173 /**
174  * Update the priority for a particular key in the datastore.  If
175  * the expiration time in value is different than the time found in
176  * the datastore, the higher value should be kept.  For the
177  * anonymity level, the lower value is to be used.  The specified
178  * priority should be added to the existing priority, ignoring the
179  * priority in value.
180  *
181  * Note that it is possible for multiple values to match this put.
182  * In that case, all of the respective values are updated.
183  *
184  * @param cls our "struct Plugin*"
185  * @param uid unique identifier of the datum
186  * @param delta by how much should the priority
187  *     change?  If priority + delta < 0 the
188  *     priority should be set to 0 (never go
189  *     negative).
190  * @param expire new expiration time should be the
191  *     MAX of any existing expiration time and
192  *     this value
193  * @param msg set to error message
194  * @return GNUNET_OK on success
195  */
196 static int
197 template_plugin_update (void *cls,
198                         uint64_t uid,
199                         int delta, struct GNUNET_TIME_Absolute expire,
200                         char **msg)
201 {
202   GNUNET_break (0);
203   *msg = GNUNET_strdup ("not implemented");
204   return GNUNET_SYSERR;
205 }
206
207
208 /**
209  * Select a subset of the items in the datastore and call
210  * the given iterator for each of them.
211  *
212  * @param cls our "struct Plugin*"
213  * @param type entries of which type should be considered?
214  *        Use 0 for any type.
215  * @param iter function to call on each matching value;
216  *        will be called once with a NULL value at the end
217  * @param iter_cls closure for iter
218  */
219 static void
220 template_plugin_iter_zero_anonymity (void *cls,
221                                      enum GNUNET_BLOCK_Type type,
222                                      PluginIterator iter,
223                                      void *iter_cls)
224 {
225   GNUNET_break (0);
226 }
227
228
229 /**
230  * Drop database.
231  */
232 static void 
233 template_plugin_drop (void *cls)
234 {
235   GNUNET_break (0);
236 }
237
238
239 /**
240  * Entry point for the plugin.
241  *
242  * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
243  * @return our "struct Plugin*"
244  */
245 void *
246 libgnunet_plugin_datastore_template_init (void *cls)
247 {
248   struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
249   struct GNUNET_DATASTORE_PluginFunctions *api;
250   struct Plugin *plugin;
251
252   plugin = GNUNET_malloc (sizeof (struct Plugin));
253   plugin->env = env;
254   api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
255   api->cls = plugin;
256   api->get_size = &template_plugin_get_size;
257   api->put = &template_plugin_put;
258   api->next_request = &template_plugin_next_request;
259   api->get = &template_plugin_get;
260   api->replication_get = &template_plugin_replication_get;
261   api->expiration_get = &template_plugin_expiration_get;
262   api->update = &template_plugin_update;
263   api->iter_zero_anonymity = &template_plugin_iter_zero_anonymity;
264   api->drop = &template_plugin_drop;
265   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
266                    "template", _("Template database running\n"));
267   return api;
268 }
269
270
271 /**
272  * Exit point from the plugin.
273  * @param cls our "struct Plugin*"
274  * @return always NULL
275  */
276 void *
277 libgnunet_plugin_datastore_template_done (void *cls)
278 {
279   struct GNUNET_DATASTORE_PluginFunctions *api = cls;
280   struct Plugin *plugin = api->cls;
281
282   GNUNET_free (plugin);
283   GNUNET_free (api);
284   return NULL;
285 }
286
287 /* end of plugin_datastore_template.c */