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