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