]> git.proxmox.com Git - mirror_ovs.git/blame - lib/ovsdb-data.h
ovsdb: New functions ovsdb_datum_sort_unique(), ovsdb_datum_from_json_unique().
[mirror_ovs.git] / lib / ovsdb-data.h
CommitLineData
c532bf9d 1/* Copyright (c) 2009, 2010 Nicira Networks
f85f8ebb
BP
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef OVSDB_DATA_H
17#define OVSDB_DATA_H 1
18
19#include <stdlib.h>
20#include "compiler.h"
21#include "ovsdb-types.h"
22
0194f33a 23struct ds;
f85f8ebb
BP
24struct ovsdb_symbol_table;
25
26/* One value of an atomic type (given by enum ovs_atomic_type). */
27union ovsdb_atom {
28 int64_t integer;
29 double real;
30 bool boolean;
31 char *string;
32 struct uuid uuid;
33};
34
35void ovsdb_atom_init_default(union ovsdb_atom *, enum ovsdb_atomic_type);
958ac03a 36const union ovsdb_atom *ovsdb_atom_default(enum ovsdb_atomic_type);
c532bf9d 37bool ovsdb_atom_is_default(const union ovsdb_atom *, enum ovsdb_atomic_type);
f85f8ebb
BP
38void ovsdb_atom_clone(union ovsdb_atom *, const union ovsdb_atom *,
39 enum ovsdb_atomic_type);
40void ovsdb_atom_swap(union ovsdb_atom *, union ovsdb_atom *);
41
5413de95
BP
42/* Returns false if ovsdb_atom_destroy() is a no-op when it is applied to an
43 * initialized atom of the given 'type', true if ovsdb_atom_destroy() actually
44 * does something.
45 *
46 * This can be used to avoid calling ovsdb_atom_destroy() for each element in
47 * an array of homogeneous atoms. (It's not worthwhile for a single atom.) */
f85f8ebb
BP
48static inline bool
49ovsdb_atom_needs_destruction(enum ovsdb_atomic_type type)
50{
51 return type == OVSDB_TYPE_STRING;
52}
53
5413de95
BP
54/* Frees the contents of 'atom', which must have the specified 'type'.
55 *
56 * This does not actually call free(atom). If necessary, the caller must be
57 * responsible for that. */
f85f8ebb
BP
58static inline void
59ovsdb_atom_destroy(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
60{
61 if (type == OVSDB_TYPE_STRING) {
62 free(atom->string);
63 }
64}
65
66uint32_t ovsdb_atom_hash(const union ovsdb_atom *, enum ovsdb_atomic_type,
67 uint32_t basis);
68
69int ovsdb_atom_compare_3way(const union ovsdb_atom *,
70 const union ovsdb_atom *,
71 enum ovsdb_atomic_type);
72
5413de95
BP
73/* Returns true if 'a' and 'b', which are both of type 'type', has the same
74 * contents, false if their contents differ. */
f85f8ebb
BP
75static inline bool ovsdb_atom_equals(const union ovsdb_atom *a,
76 const union ovsdb_atom *b,
77 enum ovsdb_atomic_type type)
78{
79 return !ovsdb_atom_compare_3way(a, b, type);
80}
81
82struct ovsdb_error *ovsdb_atom_from_json(union ovsdb_atom *,
bd76d25d 83 const struct ovsdb_base_type *,
f85f8ebb 84 const struct json *,
fbf925e4 85 struct ovsdb_symbol_table *)
f85f8ebb 86 WARN_UNUSED_RESULT;
2b66469b
BP
87struct ovsdb_error *ovsdb_datum_from_json_unique(struct ovsdb_datum *,
88 const struct ovsdb_type *,
89 const struct json *,
90 struct ovsdb_symbol_table *)
91 WARN_UNUSED_RESULT;
f85f8ebb
BP
92struct json *ovsdb_atom_to_json(const union ovsdb_atom *,
93 enum ovsdb_atomic_type);
0194f33a 94
bd76d25d 95char *ovsdb_atom_from_string(union ovsdb_atom *,
ce5a3e38
BP
96 const struct ovsdb_base_type *, const char *,
97 struct ovsdb_symbol_table *)
1bc6ff29 98 WARN_UNUSED_RESULT;
0194f33a
BP
99void ovsdb_atom_to_string(const union ovsdb_atom *, enum ovsdb_atomic_type,
100 struct ds *);
bd76d25d
BP
101
102struct ovsdb_error *ovsdb_atom_check_constraints(
103 const union ovsdb_atom *, const struct ovsdb_base_type *)
104 WARN_UNUSED_RESULT;
f85f8ebb 105\f
c7239c2f
BP
106/* An instance of an OVSDB type (given by struct ovsdb_type).
107 *
2f47998b
BP
108 * - The 'keys' must be unique and in sorted order. Most functions that modify
109 * an ovsdb_datum maintain these invariants. Functions that don't maintain
110 * the invariants have names that end in "_unsafe". Use ovsdb_datum_sort()
111 * to check and restore these invariants.
c7239c2f 112 *
2f47998b 113 * - 'n' is constrained by the ovsdb_type's 'n_min' and 'n_max'.
c7239c2f 114 *
2f47998b
BP
115 * If 'n' is nonzero, then 'keys' points to an array of 'n' atoms of the type
116 * specified by the ovsdb_type's 'key_type'. (Otherwise, 'keys' should be
117 * null.)
c7239c2f 118 *
2f47998b
BP
119 * If 'n' is nonzero and the ovsdb_type's 'value_type' is not
120 * OVSDB_TYPE_VOID, then 'values' points to an array of 'n' atoms of the type
121 * specified by the 'value_type'. (Otherwise, 'values' should be null.)
122 *
123 * Thus, for 'n' > 0, 'keys' will always be nonnull and 'values' will be
124 * nonnull only for "map" types.
c7239c2f 125 */
f85f8ebb
BP
126struct ovsdb_datum {
127 unsigned int n; /* Number of 'keys' and 'values'. */
128 union ovsdb_atom *keys; /* Each of the ovsdb_type's 'key_type'. */
129 union ovsdb_atom *values; /* Each of the ovsdb_type's 'value_type'. */
130};
131
2f47998b
BP
132/* Basics. */
133void ovsdb_datum_init_empty(struct ovsdb_datum *);
f85f8ebb 134void ovsdb_datum_init_default(struct ovsdb_datum *, const struct ovsdb_type *);
c532bf9d
BP
135bool ovsdb_datum_is_default(const struct ovsdb_datum *,
136 const struct ovsdb_type *);
958ac03a 137const struct ovsdb_datum *ovsdb_datum_default(const struct ovsdb_type *);
f85f8ebb
BP
138void ovsdb_datum_clone(struct ovsdb_datum *, const struct ovsdb_datum *,
139 const struct ovsdb_type *);
140void ovsdb_datum_destroy(struct ovsdb_datum *, const struct ovsdb_type *);
141void ovsdb_datum_swap(struct ovsdb_datum *, struct ovsdb_datum *);
2f47998b
BP
142
143/* Checking and maintaining invariants. */
e9f8f936 144struct ovsdb_error *ovsdb_datum_sort(struct ovsdb_datum *,
bfc96d9b
BP
145 enum ovsdb_atomic_type key_type)
146 WARN_UNUSED_RESULT;
147
148void ovsdb_datum_sort_assert(struct ovsdb_datum *,
149 enum ovsdb_atomic_type key_type);
150
2b66469b
BP
151size_t ovsdb_datum_sort_unique(struct ovsdb_datum *,
152 enum ovsdb_atomic_type key_type,
153 enum ovsdb_atomic_type value_type);
154
bd76d25d
BP
155struct ovsdb_error *ovsdb_datum_check_constraints(
156 const struct ovsdb_datum *, const struct ovsdb_type *)
157 WARN_UNUSED_RESULT;
f85f8ebb 158
2f47998b 159/* Type conversion. */
f85f8ebb
BP
160struct ovsdb_error *ovsdb_datum_from_json(struct ovsdb_datum *,
161 const struct ovsdb_type *,
162 const struct json *,
fbf925e4 163 struct ovsdb_symbol_table *)
f85f8ebb
BP
164 WARN_UNUSED_RESULT;
165struct json *ovsdb_datum_to_json(const struct ovsdb_datum *,
166 const struct ovsdb_type *);
167
1bc6ff29 168char *ovsdb_datum_from_string(struct ovsdb_datum *,
ce5a3e38
BP
169 const struct ovsdb_type *, const char *,
170 struct ovsdb_symbol_table *)
1bc6ff29 171 WARN_UNUSED_RESULT;
0194f33a
BP
172void ovsdb_datum_to_string(const struct ovsdb_datum *,
173 const struct ovsdb_type *, struct ds *);
174
175/* Comparison. */
f85f8ebb
BP
176uint32_t ovsdb_datum_hash(const struct ovsdb_datum *,
177 const struct ovsdb_type *, uint32_t basis);
178int ovsdb_datum_compare_3way(const struct ovsdb_datum *,
179 const struct ovsdb_datum *,
180 const struct ovsdb_type *);
181bool ovsdb_datum_equals(const struct ovsdb_datum *,
182 const struct ovsdb_datum *,
183 const struct ovsdb_type *);
2f47998b
BP
184
185/* Search. */
186unsigned int ovsdb_datum_find_key(const struct ovsdb_datum *,
187 const union ovsdb_atom *key,
188 enum ovsdb_atomic_type key_type);
189unsigned int ovsdb_datum_find_key_value(const struct ovsdb_datum *,
190 const union ovsdb_atom *key,
191 enum ovsdb_atomic_type key_type,
192 const union ovsdb_atom *value,
193 enum ovsdb_atomic_type value_type);
194
195/* Set operations. */
f85f8ebb
BP
196bool ovsdb_datum_includes_all(const struct ovsdb_datum *,
197 const struct ovsdb_datum *,
198 const struct ovsdb_type *);
199bool ovsdb_datum_excludes_all(const struct ovsdb_datum *,
200 const struct ovsdb_datum *,
201 const struct ovsdb_type *);
e9f8f936
BP
202void ovsdb_datum_union(struct ovsdb_datum *,
203 const struct ovsdb_datum *,
2f47998b
BP
204 const struct ovsdb_type *,
205 bool replace);
e9f8f936
BP
206void ovsdb_datum_subtract(struct ovsdb_datum *a,
207 const struct ovsdb_type *a_type,
208 const struct ovsdb_datum *b,
209 const struct ovsdb_type *b_type);
210
2f47998b
BP
211/* Raw operations that may not maintain the invariants. */
212void ovsdb_datum_remove_unsafe(struct ovsdb_datum *, size_t idx,
213 const struct ovsdb_type *);
214void ovsdb_datum_add_unsafe(struct ovsdb_datum *,
215 const union ovsdb_atom *key,
216 const union ovsdb_atom *value,
217 const struct ovsdb_type *);
218
219/* Type checking. */
f85f8ebb
BP
220static inline bool
221ovsdb_datum_conforms_to_type(const struct ovsdb_datum *datum,
222 const struct ovsdb_type *type)
223{
224 return datum->n >= type->n_min && datum->n <= type->n_max;
225}
226\f
227/* A table mapping from names to data items. Currently the data items are
228 * always UUIDs; perhaps this will be expanded in the future. */
229
2d2d6d4a
BP
230struct ovsdb_symbol {
231 struct uuid uuid; /* The UUID that the symbol represents. */
232 bool used; /* Already used as row UUID? */
233};
234
f85f8ebb
BP
235struct ovsdb_symbol_table *ovsdb_symbol_table_create(void);
236void ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *);
2d2d6d4a
BP
237struct ovsdb_symbol *ovsdb_symbol_table_get(const struct ovsdb_symbol_table *,
238 const char *name);
fbf925e4
BP
239struct ovsdb_symbol *ovsdb_symbol_table_put(struct ovsdb_symbol_table *,
240 const char *name,
241 const struct uuid *, bool used);
242struct ovsdb_symbol *ovsdb_symbol_table_insert(struct ovsdb_symbol_table *,
243 const char *name);
ce5a3e38 244const char *ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table *);
0194f33a
BP
245\f
246/* Tokenization
247 *
248 * Used by ovsdb_atom_from_string() and ovsdb_datum_from_string(). */
249
1bc6ff29 250char *ovsdb_token_parse(const char **, char **outp) WARN_UNUSED_RESULT;
0194f33a 251bool ovsdb_token_is_delim(unsigned char);
f85f8ebb
BP
252
253#endif /* ovsdb-data.h */