first hack at implementing new replication select code
[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  * Update the priority for a particular key in the datastore.  If
159  * the expiration time in value is different than the time found in
160  * the datastore, the higher value should be kept.  For the
161  * anonymity level, the lower value is to be used.  The specified
162  * priority should be added to the existing priority, ignoring the
163  * priority in value.
164  *
165  * Note that it is possible for multiple values to match this put.
166  * In that case, all of the respective values are updated.
167  *
168  * @param cls our "struct Plugin*"
169  * @param uid unique identifier of the datum
170  * @param delta by how much should the priority
171  *     change?  If priority + delta < 0 the
172  *     priority should be set to 0 (never go
173  *     negative).
174  * @param expire new expiration time should be the
175  *     MAX of any existing expiration time and
176  *     this value
177  * @param msg set to error message
178  * @return GNUNET_OK on success
179  */
180 static int
181 template_plugin_update (void *cls,
182                         uint64_t uid,
183                         int delta, struct GNUNET_TIME_Absolute expire,
184                         char **msg)
185 {
186   GNUNET_break (0);
187   *msg = GNUNET_strdup ("not implemented");
188   return GNUNET_SYSERR;
189 }
190
191
192 /**
193  * Select a subset of the items in the datastore and call
194  * the given iterator for each of them.
195  *
196  * @param cls our "struct Plugin*"
197  * @param type entries of which type should be considered?
198  *        Use 0 for any type.
199  * @param iter function to call on each matching value;
200  *        will be called once with a NULL value at the end
201  * @param iter_cls closure for iter
202  */
203 static void
204 template_plugin_iter_low_priority (void *cls,
205                                    enum GNUNET_BLOCK_Type type,
206                                    PluginIterator iter,
207                                    void *iter_cls)
208 {
209   GNUNET_break (0);
210 }
211
212
213
214 /**
215  * Select a subset of the items in the datastore and call
216  * the given iterator for each of them.
217  *
218  * @param cls our "struct Plugin*"
219  * @param type entries of which type should be considered?
220  *        Use 0 for any type.
221  * @param iter function to call on each matching value;
222  *        will be called once with a NULL value at the end
223  * @param iter_cls closure for iter
224  */
225 static void
226 template_plugin_iter_zero_anonymity (void *cls,
227                                      enum GNUNET_BLOCK_Type type,
228                                      PluginIterator iter,
229                                      void *iter_cls)
230 {
231   GNUNET_break (0);
232 }
233
234
235
236 /**
237  * Select a subset of the items in the datastore and call
238  * the given iterator for each of them.
239  *
240  * @param cls our "struct Plugin*"
241  * @param type entries of which type should be considered?
242  *        Use 0 for any type.
243  * @param iter function to call on each matching value;
244  *        will be called once with a NULL value at the end
245  * @param iter_cls closure for iter
246  */
247 static void
248 template_plugin_iter_ascending_expiration (void *cls,
249                                            enum GNUNET_BLOCK_Type type,
250                                            PluginIterator iter,
251                                            void *iter_cls)
252 {
253   GNUNET_break (0);
254 }
255
256
257
258 /**
259  * Select a subset of the items in the datastore and call
260  * the given iterator for each of them.
261  *
262  * @param cls our "struct Plugin*"
263  * @param type entries of which type should be considered?
264  *        Use 0 for any type.
265  * @param iter function to call on each matching value;
266  *        will be called once with a NULL value at the end
267  * @param iter_cls closure for iter
268  */
269 static void
270 template_plugin_iter_migration_order (void *cls,
271                                       enum GNUNET_BLOCK_Type type,
272                                       PluginIterator iter,
273                                       void *iter_cls)
274 {
275   GNUNET_break (0);
276 }
277
278
279
280 /**
281  * Select a subset of the items in the datastore and call
282  * the given iterator for each of them.
283  *
284  * @param cls our "struct Plugin*"
285  * @param type entries of which type should be considered?
286  *        Use 0 for any type.
287  * @param iter function to call on each matching value;
288  *        will be called once with a NULL value at the end
289  * @param iter_cls closure for iter
290  */
291 static void
292 template_plugin_iter_all_now (void *cls,
293                               enum GNUNET_BLOCK_Type type,
294                               PluginIterator iter,
295                               void *iter_cls)
296 {
297   GNUNET_break (0);
298 }
299
300
301 /**
302  * Drop database.
303  */
304 static void 
305 template_plugin_drop (void *cls)
306 {
307   GNUNET_break (0);
308 }
309
310
311 /**
312  * Entry point for the plugin.
313  *
314  * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
315  * @return our "struct Plugin*"
316  */
317 void *
318 libgnunet_plugin_datastore_template_init (void *cls)
319 {
320   struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
321   struct GNUNET_DATASTORE_PluginFunctions *api;
322   struct Plugin *plugin;
323
324   plugin = GNUNET_malloc (sizeof (struct Plugin));
325   plugin->env = env;
326   api = GNUNET_malloc (sizeof (struct GNUNET_DATASTORE_PluginFunctions));
327   api->cls = plugin;
328   api->get_size = &template_plugin_get_size;
329   api->put = &template_plugin_put;
330   api->next_request = &template_plugin_next_request;
331   api->get = &template_plugin_get;
332   api->replication_get = &template_plugin_replication_get;
333   api->update = &template_plugin_update;
334   api->iter_low_priority = &template_plugin_iter_low_priority;
335   api->iter_zero_anonymity = &template_plugin_iter_zero_anonymity;
336   api->iter_ascending_expiration = &template_plugin_iter_ascending_expiration;
337   api->iter_migration_order = &template_plugin_iter_migration_order;
338   api->iter_all_now = &template_plugin_iter_all_now;
339   api->drop = &template_plugin_drop;
340   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO,
341                    "template", _("Template database running\n"));
342   return api;
343 }
344
345
346 /**
347  * Exit point from the plugin.
348  * @param cls our "struct Plugin*"
349  * @return always NULL
350  */
351 void *
352 libgnunet_plugin_datastore_template_done (void *cls)
353 {
354   struct GNUNET_DATASTORE_PluginFunctions *api = cls;
355   struct Plugin *plugin = api->cls;
356
357   GNUNET_free (plugin);
358   GNUNET_free (api);
359   return NULL;
360 }
361
362 /* end of plugin_datastore_template.c */