malloc -> new
[oweals/gnunet.git] / src / datastore / plugin_datastore_template.c
1 /*
2      This file is part of GNUnet
3      (C) 2009, 2011 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
51 template_plugin_estimate_size (void *cls)
52 {
53   GNUNET_break (0);
54   return 0;
55 }
56
57
58 /**
59  * Store an item in the datastore.
60  *
61  * @param cls closure
62  * @param key key for the item
63  * @param size number of bytes in data
64  * @param data content stored
65  * @param type type of the content
66  * @param priority priority of the content
67  * @param anonymity anonymity-level for the content
68  * @param replication replication-level for the content
69  * @param expiration expiration time for the content
70  * @param msg set to error message
71  * @return GNUNET_OK on success
72  */
73 static int
74 template_plugin_put (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
75                      const void *data, enum GNUNET_BLOCK_Type type,
76                      uint32_t priority, uint32_t anonymity,
77                      uint32_t replication,
78                      struct GNUNET_TIME_Absolute expiration, char **msg)
79 {
80   GNUNET_break (0);
81   *msg = GNUNET_strdup ("not implemented");
82   return GNUNET_SYSERR;
83 }
84
85
86 /**
87  * Get one of the results for a particular key in the datastore.
88  *
89  * @param cls closure
90  * @param offset offset of the result (modulo num-results);
91  *               specific ordering does not matter for the offset
92  * @param key maybe NULL (to match all entries)
93  * @param vhash hash of the value, maybe NULL (to
94  *        match all values that have the right key).
95  *        Note that for DBlocks there is no difference
96  *        betwen key and vhash, but for other blocks
97  *        there may be!
98  * @param type entries of which type are relevant?
99  *     Use 0 for any type.
100  * @param proc function to call on each matching value;
101  *        will be called with NULL if nothing matches
102  * @param proc_cls closure for proc
103  */
104 static void
105 template_plugin_get_key (void *cls, uint64_t offset,
106                          const struct GNUNET_HashCode * key,
107                          const struct GNUNET_HashCode * vhash,
108                          enum GNUNET_BLOCK_Type type, PluginDatumProcessor proc,
109                          void *proc_cls)
110 {
111   GNUNET_break (0);
112 }
113
114
115
116 /**
117  * Get a random item for replication.  Returns a single, not expired,
118  * random item from those with the highest replication counters.  The
119  * item's replication counter is decremented by one IF it was positive
120  * before.  Call 'proc' with all values ZERO or NULL if the datastore
121  * is empty.
122  *
123  * @param cls closure
124  * @param proc function to call the value (once only).
125  * @param proc_cls closure for proc
126  */
127 static void
128 template_plugin_get_replication (void *cls, PluginDatumProcessor proc,
129                                  void *proc_cls)
130 {
131   GNUNET_break (0);
132 }
133
134
135 /**
136  * Get a random item for expiration.  Call 'proc' with all values ZERO
137  * or NULL if the datastore is empty.
138  *
139  * @param cls closure
140  * @param proc function to call the value (once only).
141  * @param proc_cls closure for proc
142  */
143 static void
144 template_plugin_get_expiration (void *cls, PluginDatumProcessor proc,
145                                 void *proc_cls)
146 {
147   GNUNET_break (0);
148 }
149
150
151 /**
152  * Update the priority for a particular key in the datastore.  If
153  * the expiration time in value is different than the time found in
154  * the datastore, the higher value should be kept.  For the
155  * anonymity level, the lower value is to be used.  The specified
156  * priority should be added to the existing priority, ignoring the
157  * priority in value.
158  *
159  * Note that it is possible for multiple values to match this put.
160  * In that case, all of the respective values are updated.
161  *
162  * @param cls our "struct Plugin*"
163  * @param uid unique identifier of the datum
164  * @param delta by how much should the priority
165  *     change?  If priority + delta < 0 the
166  *     priority should be set to 0 (never go
167  *     negative).
168  * @param expire new expiration time should be the
169  *     MAX of any existing expiration time and
170  *     this value
171  * @param msg set to error message
172  * @return GNUNET_OK on success
173  */
174 static int
175 template_plugin_update (void *cls, uint64_t uid, int delta,
176                         struct GNUNET_TIME_Absolute expire, char **msg)
177 {
178   GNUNET_break (0);
179   *msg = GNUNET_strdup ("not implemented");
180   return GNUNET_SYSERR;
181 }
182
183
184 /**
185  * Call the given processor on an item with zero anonymity.
186  *
187  * @param cls our "struct Plugin*"
188  * @param offset offset of the result (modulo num-results);
189  *               specific ordering does not matter for the offset
190  * @param type entries of which type should be considered?
191  *        Use 0 for any type.
192  * @param proc function to call on each matching value;
193  *        will be called  with NULL if no value matches
194  * @param proc_cls closure for proc
195  */
196 static void
197 template_plugin_get_zero_anonymity (void *cls, uint64_t offset,
198                                     enum GNUNET_BLOCK_Type type,
199                                     PluginDatumProcessor proc, void *proc_cls)
200 {
201   GNUNET_break (0);
202 }
203
204
205 /**
206  * Drop database.
207  */
208 static void
209 template_plugin_drop (void *cls)
210 {
211   GNUNET_break (0);
212 }
213
214
215 /**
216  * Get all of the keys in the datastore.
217  *
218  * @param cls closure
219  * @param proc function to call on each key
220  * @param proc_cls closure for proc
221  */
222 static void
223 template_get_keys (void *cls,
224                    PluginKeyProcessor proc,
225                    void *proc_cls)
226 {
227 }
228
229
230 /**
231  * Entry point for the plugin.
232  *
233  * @param cls the "struct GNUNET_DATASTORE_PluginEnvironment*"
234  * @return our "struct Plugin*"
235  */
236 void *
237 libgnunet_plugin_datastore_template_init (void *cls)
238 {
239   struct GNUNET_DATASTORE_PluginEnvironment *env = cls;
240   struct GNUNET_DATASTORE_PluginFunctions *api;
241   struct Plugin *plugin;
242
243   plugin = GNUNET_new (struct Plugin);
244   plugin->env = env;
245   api = GNUNET_new (struct GNUNET_DATASTORE_PluginFunctions);
246   api->cls = plugin;
247   api->estimate_size = &template_plugin_estimate_size;
248   api->put = &template_plugin_put;
249   api->update = &template_plugin_update;
250   api->get_key = &template_plugin_get_key;
251   api->get_replication = &template_plugin_get_replication;
252   api->get_expiration = &template_plugin_get_expiration;
253   api->get_zero_anonymity = &template_plugin_get_zero_anonymity;
254   api->drop = &template_plugin_drop;
255   api->get_keys = &template_get_keys;
256   GNUNET_log_from (GNUNET_ERROR_TYPE_INFO, "template",
257                    _("Template database running\n"));
258   return api;
259 }
260
261
262 /**
263  * Exit point from the plugin.
264  * @param cls our "struct Plugin*"
265  * @return always NULL
266  */
267 void *
268 libgnunet_plugin_datastore_template_done (void *cls)
269 {
270   struct GNUNET_DATASTORE_PluginFunctions *api = cls;
271   struct Plugin *plugin = api->cls;
272
273   GNUNET_free (plugin);
274   GNUNET_free (api);
275   return NULL;
276 }
277
278 /* end of plugin_datastore_template.c */