fix bad free
[oweals/gnunet.git] / src / include / gnunet_datacache_lib.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  * datacache API
24  *
25  * @defgroup datacache  Data Cache library
26  * Simple, transient hash table of bounded size with content expiration.
27  *
28  * In contrast to the sqstore there is
29  * no prioritization, deletion or iteration.
30  * All of the data is discarded when the peer shuts down!
31  *
32  * @{
33  */
34
35 #ifndef GNUNET_DATACACHE_LIB_H
36 #define GNUNET_DATACACHE_LIB_H
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_block_lib.h"
40
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #if 0                           /* keep Emacsens' auto-indent happy */
45 }
46 #endif
47 #endif
48
49
50 /**
51  * Handle to the cache.
52  */
53 struct GNUNET_DATACACHE_Handle;
54
55
56 /**
57  * Create a data cache.
58  *
59  * @param cfg configuration to use
60  * @param section section in the configuration that contains our options
61  * @return handle to use to access the service
62  */
63 struct GNUNET_DATACACHE_Handle *
64 GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg,
65                          const char *section);
66
67
68 /**
69  * Destroy a data cache (and free associated resources).
70  *
71  * @param h handle to the datastore
72  */
73 void
74 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h);
75
76
77 /**
78  * An iterator over a set of items stored in the datacache.
79  *
80  * @param cls closure
81  * @param key key for the content
82  * @param data_size number of bytes in @a data
83  * @param data content stored
84  * @param type type of the content
85  * @param exp when will the content expire?
86  * @param path_info_len number of entries in @a path_info
87  * @param path_info a path through the network
88  * @return #GNUNET_OK to continue iterating, #GNUNET_SYSERR to abort
89  */
90 typedef int
91 (*GNUNET_DATACACHE_Iterator) (void *cls,
92                               const struct GNUNET_HashCode *key,
93                               size_t data_size,
94                               const char *data,
95                               enum GNUNET_BLOCK_Type type,
96                               struct GNUNET_TIME_Absolute exp,
97                               unsigned int path_info_len,
98                               const struct GNUNET_PeerIdentity *path_info);
99
100
101 /**
102  * Store an item in the datacache.
103  *
104  * @param h handle to the datacache
105  * @param key key to store data under
106  * @param how close is @a key to our pid?
107  * @param data_size number of bytes in @a data
108  * @param data data to store
109  * @param type type of the value
110  * @param discard_time when to discard the value in any case
111  * @param path_info_len number of entries in @a path_info
112  * @param path_info a path through the network
113  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate
114  */
115 int
116 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h,
117                       const struct GNUNET_HashCode *key,
118                       uint32_t xor_distance,
119                       size_t data_size,
120                       const char *data,
121                       enum GNUNET_BLOCK_Type type,
122                       struct GNUNET_TIME_Absolute discard_time,
123                       unsigned int path_info_len,
124                       const struct GNUNET_PeerIdentity *path_info);
125
126
127 /**
128  * Iterate over the results for a particular key
129  * in the datacache.
130  *
131  * @param h handle to the datacache
132  * @param key what to look up
133  * @param type entries of which type are relevant?
134  * @param iter maybe NULL (to just count)
135  * @param iter_cls closure for @a iter
136  * @return the number of results found
137  */
138 unsigned int
139 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h,
140                       const struct GNUNET_HashCode *key,
141                       enum GNUNET_BLOCK_Type type,
142                       GNUNET_DATACACHE_Iterator iter,
143                       void *iter_cls);
144
145
146 /**
147  * Obtain a random element from the datacache.
148  *
149  * @param h handle to the datacache
150  * @param iter maybe NULL (to just count)
151  * @param iter_cls closure for @a iter
152  * @return the number of results found (zero or 1)
153  */
154 unsigned int
155 GNUNET_DATACACHE_get_random (struct GNUNET_DATACACHE_Handle *h,
156                              GNUNET_DATACACHE_Iterator iter,
157                              void *iter_cls);
158
159
160 /**
161  * Iterate over the results that are "close" to a particular key in
162  * the datacache.  "close" is defined as numerically larger than @a
163  * key (when interpreted as a circular address space), with small
164  * distance.
165  *
166  * @param h handle to the datacache
167  * @param key area of the keyspace to look into
168  * @param num_results number of results that should be returned to @a iter
169  * @param iter maybe NULL (to just count)
170  * @param iter_cls closure for @a iter
171  * @return the number of results found
172  */
173 unsigned int
174 GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h,
175                               const struct GNUNET_HashCode *key,
176                               unsigned int num_results,
177                               GNUNET_DATACACHE_Iterator iter,
178                               void *iter_cls);
179
180
181 #if 0                           /* keep Emacsens' auto-indent happy */
182 {
183 #endif
184 #ifdef __cplusplus
185 }
186 #endif
187
188 #endif
189
190 /** @} */  /* end of group */