confdb.h 5.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*  Copyright (C) 2015 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
/*!
 * \file
 *
 * Configuration database interface.
 *
 * \addtogroup config
 *
 * @{
 */

#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "knot/conf/conf.h"
32
#include "libknot/libknot.h"
33
#include "libknot/yparser/ypschema.h"
34 35

/*! Current version of the configuration database structure. */
36
#define CONF_DB_VERSION		2
37 38 39 40 41 42 43
/*! Minimum length of a database key ([category_id, item_id]. */
#define CONF_MIN_KEY_LEN	(2 * sizeof(uint8_t))
/*! Maximum length of a database key ([category_id, item_id, identifier]. */
#define CONF_MAX_KEY_LEN	(CONF_MIN_KEY_LEN + YP_MAX_ID_LEN)
/*! Maximum size of database data. */
#define CONF_MAX_DATA_LEN	65536

44
/*!
45
 * Initializes the configuration DB if empty.
46
 *
47 48 49
 * \param[in] conf   Configuration.
 * \param[in] txn    Configuration DB transaction.
 * \param[in] purge  Purge the DB indicator.
50 51 52
 *
 * \return Error code, KNOT_EOK if success.
 */
53 54
int conf_db_init(
	conf_t *conf,
55 56
	knot_db_txn_t *txn,
	bool purge
57 58
);

59
/*!
60
 * Checks the configuration DB and returns the number of items.
61 62 63 64
 *
 * \param[in] conf  Configuration.
 * \param[in] txn   Configuration DB transaction.
 *
65
 * \return Error code, KNOT_EOK if ok and empty, > 0 number of records.
66 67 68 69 70 71
 */
int conf_db_check(
	conf_t *conf,
	knot_db_txn_t *txn
);

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
/*!
 * Sets the item with data in the configuration DB.
 *
 * Singlevalued data is rewritten, multivalued data is appended.
 *
 * \note Setting of key0 without key1 has no effect.
 *
 * \param[in] conf      Configuration.
 * \param[in] txn       Configuration DB transaction.
 * \param[in] key0      Section name.
 * \param[in] key1      Item name.
 * \param[in] id        Section identifier.
 * \param[in] id_len    Length of the section identifier.
 * \param[in] data      Item data.
 * \param[in] data_len  Length of the item data.
 *
 * \return Error code, KNOT_EOK if success.
 */
90 91
int conf_db_set(
	conf_t *conf,
92
	knot_db_txn_t *txn,
93 94 95 96 97 98
	const yp_name_t *key0,
	const yp_name_t *key1,
	const uint8_t *id,
	size_t id_len,
	const uint8_t *data,
	size_t data_len
99 100
);

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/*!
 * Unsets the item data in the configuration DB.
 *
 * If no data is provided, the whole item is remove.
 *
 * \param[in] conf         Configuration.
 * \param[in] txn          Configuration DB transaction.
 * \param[in] key0         Section name.
 * \param[in] key1         Item name.
 * \param[in] id           Section identifier.
 * \param[in] id_len       Length of the section identifier.
 * \param[in] data         Item data.
 * \param[in] data_len     Length of the item data.
 * \param[in] delete_key1  Set to unregister the item from the DB.
 *
 * \return Error code, KNOT_EOK if success.
 */
118
int conf_db_unset(
119
	conf_t *conf,
120
	knot_db_txn_t *txn,
121 122 123 124
	const yp_name_t *key0,
	const yp_name_t *key1,
	const uint8_t *id,
	size_t id_len,
125 126 127
	const uint8_t *data,
	size_t data_len,
	bool delete_key1
128 129
);

130 131 132 133 134 135 136 137 138 139 140 141 142
/*!
 * Gets the item data from the configuration DB.
 *
 * \param[in] conf    Configuration.
 * \param[in] txn     Configuration DB transaction.
 * \param[in] key0    Section name.
 * \param[in] key1    Item name.
 * \param[in] id      Section identifier.
 * \param[in] id_len  Length of the section identifier.
 * \param[out] data   Item data.
 *
 * \return Error code, KNOT_EOK if success.
 */
143 144
int conf_db_get(
	conf_t *conf,
145
	knot_db_txn_t *txn,
146 147 148 149 150
	const yp_name_t *key0,
	const yp_name_t *key1,
	const uint8_t *id,
	size_t id_len,
	conf_val_t *data
151 152
);

153 154 155 156 157 158 159 160 161 162
/*!
 * Gets a configuration DB section iterator.
 *
 * \param[in] conf   Configuration.
 * \param[in] txn    Configuration DB transaction.
 * \param[in] key0   Section name.
 * \param[out] iter  Section iterator.
 *
 * \return Error code, KNOT_EOK if success.
 */
163 164
int conf_db_iter_begin(
	conf_t *conf,
165
	knot_db_txn_t *txn,
166 167 168 169
	const yp_name_t *key0,
	conf_iter_t *iter
);

170 171 172 173 174 175 176 177
/*!
 * Moves the section iterator to the next identifier.
 *
 * \param[in] conf      Configuration.
 * \param[in,out] iter  Section iterator.
 *
 * \return Error code, KNOT_EOK if success.
 */
178 179 180 181 182
int conf_db_iter_next(
	conf_t *conf,
	conf_iter_t *iter
);

183 184 185 186 187 188 189 190 191 192
/*!
 * Gets the current section iterator value (identifier).
 *
 * \param[in] conf       Configuration.
 * \param[in] iter       Section iterator.
 * \param[out] data      Identifier.
 * \param[out] data_len  Length of the identifier.
 *
 * \return Error code, KNOT_EOK if success.
 */
193 194 195
int conf_db_iter_id(
	conf_t *conf,
	conf_iter_t *iter,
196
	const uint8_t **data,
197 198 199
	size_t *data_len
);

200 201 202 203 204 205 206 207
/*!
 * Deletes the current section iterator value (identifier).
 *
 * \param[in] conf      Configuration.
 * \param[in,out] iter  Section iterator.
 *
 * \return Error code, KNOT_EOK if success.
 */
208
int conf_db_iter_del(
209 210 211 212
	conf_t *conf,
	conf_iter_t *iter
);

213 214 215 216 217 218
/*!
 * Deletes the section iterator.
 *
 * \param[in] conf      Configuration.
 * \param[in,out] iter  Section iterator.
 */
219
void conf_db_iter_finish(
220
	conf_t *conf,
221
	conf_iter_t *iter
222 223
);

224 225 226 227 228 229 230 231 232 233 234
/*!
 * Dumps the configuration DB in the textual form.
 *
 * \note This function is intended for debugging.
 *
 * \param[in] conf       Configuration.
 * \param[in] txn        Configuration DB transaction.
 * \param[in] file_name  File name to dump to (NULL to dump to stdout).
 *
 * \return Error code, KNOT_EOK if success.
 */
235 236
int conf_db_raw_dump(
	conf_t *conf,
237
	knot_db_txn_t *txn,
238 239
	const char *file_name
);
240 241

/*! @} */