gnunet_datacache_lib.h (5496B)
1 /* 2 This file is part of GNUnet 3 Copyright (C) 2006, 2009, 2015, 2022 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 * @addtogroup dht_libs DHT and support libraries 23 * @{ 24 * 25 * @author Christian Grothoff 26 * 27 * @file 28 * datacache API 29 * 30 * @defgroup datacache Data Cache library 31 * Simple, transient hash table of bounded size with content expiration. 32 * 33 * In contrast to the sqstore there is 34 * no prioritization, deletion or iteration. 35 * All of the data is discarded when the peer shuts down! 36 * 37 * @{ 38 */ 39 40 #ifndef GNUNET_DATACACHE_LIB_H 41 #define GNUNET_DATACACHE_LIB_H 42 43 44 #include "gnunet_util_lib.h" 45 #include "gnunet_block_lib.h" 46 #include "gnunet_dht_service.h" 47 48 #ifdef __cplusplus 49 extern "C" 50 { 51 #if 0 /* keep Emacsens' auto-indent happy */ 52 } 53 #endif 54 #endif 55 56 57 /** 58 * Information about a block stored in the datacache. 59 */ 60 struct GNUNET_DATACACHE_Block 61 { 62 /** 63 * Key of the block. 64 */ 65 struct GNUNET_HashCode key; 66 67 /** 68 * When does the block expire? 69 */ 70 struct GNUNET_TIME_Absolute expiration_time; 71 72 /** 73 * If the path was truncated, this is the peer 74 * ID at which the path was truncated. 75 */ 76 struct GNUNET_PeerIdentity trunc_peer; 77 78 /** 79 * PUT path taken by the block, array of peer identities. 80 */ 81 const struct GNUNET_DHT_PathElement *put_path; 82 83 /** 84 * Actual block data. 85 */ 86 const void *data; 87 88 /** 89 * Number of bytes in @a data. 90 */ 91 size_t data_size; 92 93 /** 94 * Length of the @e put_path array. 95 */ 96 unsigned int put_path_length; 97 98 /** 99 * Type of the block. 100 */ 101 enum GNUNET_BLOCK_Type type; 102 103 /** 104 * Options for routing for the block. 105 */ 106 enum GNUNET_DHT_RouteOption ro; 107 }; 108 109 110 /** 111 * Handle to the cache. 112 */ 113 struct GNUNET_DATACACHE_Handle; 114 115 116 /** 117 * Create a data cache. 118 * 119 * @param cfg configuration to use 120 * @param section section in the configuration that contains our options 121 * @return handle to use to access the service 122 */ 123 struct GNUNET_DATACACHE_Handle * 124 GNUNET_DATACACHE_create (const struct GNUNET_CONFIGURATION_Handle *cfg, 125 const char *section); 126 127 128 /** 129 * Destroy a data cache (and free associated resources). 130 * 131 * @param h handle to the datastore 132 */ 133 void 134 GNUNET_DATACACHE_destroy (struct GNUNET_DATACACHE_Handle *h); 135 136 137 /** 138 * An iterator over a set of items stored in the datacache. 139 * 140 * @param cls closure 141 * @param block a block from the datacache 142 * @return #GNUNET_OK to continue iterating, #GNUNET_SYSERR to abort 143 */ 144 typedef enum GNUNET_GenericReturnValue 145 (*GNUNET_DATACACHE_Iterator)(void *cls, 146 const struct GNUNET_DATACACHE_Block *block); 147 148 149 /** 150 * Store an item in the datacache. 151 * 152 * @param h handle to the datacache 153 * @param xor_distance how close is the block's key to our pid? 154 * @param block actual block data to store 155 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error, #GNUNET_NO if duplicate 156 */ 157 enum GNUNET_GenericReturnValue 158 GNUNET_DATACACHE_put (struct GNUNET_DATACACHE_Handle *h, 159 uint32_t xor_distance, 160 const struct GNUNET_DATACACHE_Block *block); 161 162 163 /** 164 * Iterate over the results for a particular key 165 * in the datacache. 166 * 167 * @param h handle to the datacache 168 * @param key what to look up 169 * @param type entries of which type are relevant? 170 * @param iter maybe NULL (to just count) 171 * @param iter_cls closure for @a iter 172 * @return the number of results found 173 */ 174 unsigned int 175 GNUNET_DATACACHE_get (struct GNUNET_DATACACHE_Handle *h, 176 const struct GNUNET_HashCode *key, 177 enum GNUNET_BLOCK_Type type, 178 GNUNET_DATACACHE_Iterator iter, 179 void *iter_cls); 180 181 182 /** 183 * Iterate over the results that are "close" to a particular key in 184 * the datacache. "close" is defined as numerically larger than @a 185 * key (when interpreted as a circular address space), with small 186 * distance. 187 * 188 * @param h handle to the datacache 189 * @param key area of the keyspace to look into 190 * @param type entries of which type are relevant? 191 * @param num_results number of results that should be returned to @a iter 192 * @param iter maybe NULL (to just count) 193 * @param iter_cls closure for @a iter 194 * @return the number of results found 195 */ 196 unsigned int 197 GNUNET_DATACACHE_get_closest (struct GNUNET_DATACACHE_Handle *h, 198 const struct GNUNET_HashCode *key, 199 enum GNUNET_BLOCK_Type type, 200 unsigned int num_results, 201 GNUNET_DATACACHE_Iterator iter, 202 void *iter_cls); 203 204 205 #if 0 /* keep Emacsens' auto-indent happy */ 206 { 207 #endif 208 #ifdef __cplusplus 209 } 210 #endif 211 212 #endif 213 214 /** @} */ /* end of group */ 215 216 /** @} */ /* end of group addition */