d423c039b694edf6ae894bb5d12a4f635c387e00
[oweals/gnunet.git] / src / include / gnunet_datacache_plugin.h
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2006, 2009, 2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20  * @author Christian Grothoff
21  *
22  * @file
23  * API for database backends for the datacache
24  *
25  * @defgroup datacache-plugin  Data Cache plugin API
26  * API for database backends for the datacache
27  * @{
28  */
29 #ifndef PLUGIN_DATACACHE_H
30 #define PLUGIN_DATACACHE_H
31
32 #include "gnunet_datacache_lib.h"
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42
43 /**
44  * Function called by plugins to notify the datacache
45  * about content deletions.
46  *
47  * @param cls closure
48  * @param key key of the content that was deleted
49  * @param size number of bytes that were made available
50  */
51 typedef void
52 (*GNUNET_DATACACHE_DeleteNotifyCallback) (void *cls,
53                                           const struct GNUNET_HashCode *key,
54                                           size_t size);
55
56
57 /**
58  * The datastore service will pass a pointer to a struct
59  * of this type as the first and only argument to the
60  * entry point of each datastore plugin.
61  */
62 struct GNUNET_DATACACHE_PluginEnvironment
63 {
64
65   /**
66    * Configuration to use.
67    */
68   const struct GNUNET_CONFIGURATION_Handle *cfg;
69
70   /**
71    * Configuration section to use.
72    */
73   const char *section;
74
75   /**
76    * Closure to use for callbacks.
77    */
78   void *cls;
79
80   /**
81    * Function to call whenever the plugin needs to
82    * discard content that it was asked to store.
83    */
84   GNUNET_DATACACHE_DeleteNotifyCallback delete_notify;
85
86   /**
87    * How much space are we allowed to use?
88    */
89   unsigned long long quota;
90
91 };
92
93
94 /**
95  * @brief struct returned by the initialization function of the plugin
96  */
97 struct GNUNET_DATACACHE_PluginFunctions
98 {
99
100   /**
101    * Closure to pass to all plugin functions.
102    */
103   void *cls;
104
105   /**
106    * Store an item in the datastore.
107    *
108    * @param cls closure (internal context for the plugin)
109    * @param key key to store the value under
110    * @param xor_distance how close is @a key to our PID?
111    * @param size number of bytes in @a data
112    * @param data data to store
113    * @param type type of the value
114    * @param discard_time when to discard the value in any case
115    * @param path_info_len number of entries in @a path_info
116    * @param path_info a path through the network
117    * @return 0 if duplicate, -1 on error, number of bytes used otherwise
118    */
119   ssize_t (*put) (void *cls,
120                   const struct GNUNET_HashCode *key,
121                   uint32_t xor_distance,
122                   size_t size,
123                   const char *data,
124                   enum GNUNET_BLOCK_Type type,
125                   struct GNUNET_TIME_Absolute discard_time,
126                   unsigned int path_info_len,
127                   const struct GNUNET_PeerIdentity *path_info);
128
129   /**
130    * Iterate over the results for a particular key
131    * in the datastore.
132    *
133    * @param cls closure (internal context for the plugin)
134    * @param key key to look for
135    * @param type entries of which type are relevant?
136    * @param iter maybe NULL (to just count)
137    * @param iter_cls closure for @a iter
138    * @return the number of results found
139    */
140   unsigned int (*get) (void *cls,
141                        const struct GNUNET_HashCode *key,
142                        enum GNUNET_BLOCK_Type type,
143                        GNUNET_DATACACHE_Iterator iter,
144                        void *iter_cls);
145
146   /**
147    * Delete the entry with the lowest expiration value
148    * from the datacache right now.
149    *
150    * @param cls closure (internal context for the plugin)
151    * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
152    */
153   int (*del) (void *cls);
154
155   /**
156    * Return a random value from the datastore.
157    *
158    * @param cls closure (internal context for the plugin)
159    * @param iter maybe NULL (to just count)
160    * @param iter_cls closure for @a iter
161    * @return the number of results found (zero or one)
162    */
163   unsigned int (*get_random) (void *cls,
164                               GNUNET_DATACACHE_Iterator iter,
165                               void *iter_cls);
166
167
168   /**
169    * Iterate over the results that are "close" to a particular key in
170    * the datacache.  "close" is defined as numerically larger than @a
171    * key (when interpreted as a circular address space), with small
172    * distance.
173    *
174    * @param cls closure (internal context for the plugin)
175    * @param key area of the keyspace to look into
176    * @param num_results number of results that should be returned to @a iter
177    * @param iter maybe NULL (to just count)
178    * @param iter_cls closure for @a iter
179    * @return the number of results found
180    */
181   unsigned int (*get_closest) (void *cls,
182                                const struct GNUNET_HashCode *key,
183                                unsigned int num_results,
184                                GNUNET_DATACACHE_Iterator iter,
185                                void *iter_cls);
186
187 };
188
189
190 #if 0                           /* keep Emacsens' auto-indent happy */
191 {
192 #endif
193 #ifdef __cplusplus
194 }
195 #endif
196
197 #endif
198
199 /** @} */  /* end of group */