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