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