wscript: separate embedded_heimdal from system_heimdal
[Samba.git] / ctdb / common / db_hash.h
blob67e2b85796198a6f918ccca2584c8f7dd7a19e43
1 /*
2 Using tdb as a hash table
4 Copyright (C) Amitay Isaacs 2015
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #ifndef __CTDB_DB_HASH_H__
21 #define __CTDB_DB_HASH_H__
23 #include <talloc.h>
24 #include <tdb.h>
26 /**
27 * @file db_hash.h
29 * @brief Use tdb database as a hash table
31 * This uses in-memory tdb databases to create a fixed sized hash table.
34 /**
35 * @brief Hash type to indicate the hashing function to use.
37 * DB_HASH_SIMPLE uses default hashing function
38 * DB_HASH_COMPLEX uses jenkins hashing function
40 enum db_hash_type {
41 DB_HASH_SIMPLE,
42 DB_HASH_COMPLEX,
45 /**
46 * @brief Parser callback function called when fetching a record
48 * This function is called when fetching a record. This function should
49 * not modify key and data arguments.
51 * The function should return 0 on success and errno on error.
53 typedef int (*db_hash_record_parser_fn)(uint8_t *keybuf, size_t keylen,
54 uint8_t *databuf, size_t datalen,
55 void *private_data);
57 /**
58 * @brief Abstract structure representing tdb hash table
60 struct db_hash_context;
62 /**
63 * @brief Initialize tdb hash table
65 * This returns a new tdb hash table context which is a talloc context. Freeing
66 * this context will free all the memory associated with the hash table.
68 * @param[in] mem_ctx Talloc memory context
69 * @param[in] name The name for the hash table
70 * @param[in] hash_size The size of the hash table
71 * @param[in] type The type of hashing function to use
72 * @param[out] result The new db_hash_context structure
73 * @return 0 on success, errno on failure
75 int db_hash_init(TALLOC_CTX *mem_ctx, const char *name, int hash_size,
76 enum db_hash_type type, struct db_hash_context **result);
78 /**
79 * @brief Insert a record into the hash table
81 * The key and data can be any binary data. Insert only if the record does not
82 * exist. If the record already exists, return error.
84 * @param[in] dh The tdb hash table context
85 * @param[in] keybuf The key buffer
86 * @param[in] keylen The key length
87 * @param[in] databuf The data buffer
88 * @param[in] datalen The data length
89 * @return 0 on success, errno on failure
91 int db_hash_insert(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen,
92 uint8_t *databuf, size_t datalen);
94 /**
95 * @brief Add a record into the hash table
97 * The key and data can be any binary data. If the record does not exist,
98 * insert the record. If the record already exists, replace the record.
100 * @param[in] dh The tdb hash table context
101 * @param[in] keybuf The key buffer
102 * @param[in] keylen The key length
103 * @param[in] databuf The data buffer
104 * @param[in] datalen The data length
105 * @return 0 on success, errno on failure
107 int db_hash_add(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen,
108 uint8_t *databuf, size_t datalen);
110 * @brief Delete a record from the hash table
112 * @param[in] dh The tdb hash table context
113 * @param[in] keybuf The key buffer
114 * @param[in] keylen The key length
115 * @return 0 on success, errno on failure
117 int db_hash_delete(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen);
120 * @brief Fetch a record from the hash table
122 * The key and data can be any binary data.
124 * @param[in] dh The tdb hash table context
125 * @param[in] keybuf The key buffer
126 * @param[in] keylen The key length
127 * @param[in] parser Function called when the matching record is found
128 * @param[in] private_data Private data to parser function
129 * @return 0 on success, errno on failure
131 int db_hash_fetch(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen,
132 db_hash_record_parser_fn parser, void *private_data);
135 * @brief Check if a record exists in the hash table
137 * @param[in] dh The tdb hash table context
138 * @param[in] keybuf The key buffer
139 * @param[in] keylen The key length
140 * @return 0 if the record exists, errno on failure
142 int db_hash_exists(struct db_hash_context *dh, uint8_t *keybuf, size_t keylen);
145 * @brief Traverse the database without modification
147 * The parser function should return non-zero value to stop traverse.
149 * @param[in] dh The tdb hash table context
150 * @param[in] parser Function called for each record
151 * @param[in] private_data Private data to parser function
152 * @param[out] count Number of records traversed
153 * @return 0 on success, errno on failure
155 int db_hash_traverse(struct db_hash_context *dh,
156 db_hash_record_parser_fn parser, void *private_data,
157 int *count);
160 * @brief Traverse the database for modifications
162 * The parser function should return non-zero value to stop traverse.
164 * @param[in] dh The tdb hash table context
165 * @param[in] parser Function called for each record
166 * @param[in] private_data Private data to parser function
167 * @param[out] count Number of records traversed
168 * @return 0 on success, errno on failure
170 int db_hash_traverse_update(struct db_hash_context *dh,
171 db_hash_record_parser_fn parser,
172 void *private_data, int *count);
174 #endif /* __CTDB_DB_HASH_H__ */