]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovsdb-data.h
xenserver: Add license to uuid.py.
[mirror_ovs.git] / lib / ovsdb-data.h
1 /* Copyright (c) 2009, 2010 Nicira Networks
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
23 struct ds;
24 struct ovsdb_symbol_table;
25
26 /* One value of an atomic type (given by enum ovs_atomic_type). */
27 union ovsdb_atom {
28 int64_t integer;
29 double real;
30 bool boolean;
31 char *string;
32 struct uuid uuid;
33 };
34
35 void ovsdb_atom_init_default(union ovsdb_atom *, enum ovsdb_atomic_type);
36 const union ovsdb_atom *ovsdb_atom_default(enum ovsdb_atomic_type);
37 bool ovsdb_atom_is_default(const union ovsdb_atom *, enum ovsdb_atomic_type);
38 void ovsdb_atom_clone(union ovsdb_atom *, const union ovsdb_atom *,
39 enum ovsdb_atomic_type);
40 void ovsdb_atom_swap(union ovsdb_atom *, union ovsdb_atom *);
41
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.) */
48 static inline bool
49 ovsdb_atom_needs_destruction(enum ovsdb_atomic_type type)
50 {
51 return type == OVSDB_TYPE_STRING;
52 }
53
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. */
58 static inline void
59 ovsdb_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
66 uint32_t ovsdb_atom_hash(const union ovsdb_atom *, enum ovsdb_atomic_type,
67 uint32_t basis);
68
69 int ovsdb_atom_compare_3way(const union ovsdb_atom *,
70 const union ovsdb_atom *,
71 enum ovsdb_atomic_type);
72
73 /* Returns true if 'a' and 'b', which are both of type 'type', has the same
74 * contents, false if their contents differ. */
75 static 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
82 struct ovsdb_error *ovsdb_atom_from_json(union ovsdb_atom *,
83 const struct ovsdb_base_type *,
84 const struct json *,
85 struct ovsdb_symbol_table *)
86 WARN_UNUSED_RESULT;
87 struct json *ovsdb_atom_to_json(const union ovsdb_atom *,
88 enum ovsdb_atomic_type);
89
90 char *ovsdb_atom_from_string(union ovsdb_atom *,
91 const struct ovsdb_base_type *, const char *,
92 struct ovsdb_symbol_table *)
93 WARN_UNUSED_RESULT;
94 void ovsdb_atom_to_string(const union ovsdb_atom *, enum ovsdb_atomic_type,
95 struct ds *);
96
97 struct ovsdb_error *ovsdb_atom_check_constraints(
98 const union ovsdb_atom *, const struct ovsdb_base_type *)
99 WARN_UNUSED_RESULT;
100 \f
101 /* An instance of an OVSDB type (given by struct ovsdb_type).
102 *
103 * - The 'keys' must be unique and in sorted order. Most functions that modify
104 * an ovsdb_datum maintain these invariants. Functions that don't maintain
105 * the invariants have names that end in "_unsafe". Use ovsdb_datum_sort()
106 * to check and restore these invariants.
107 *
108 * - 'n' is constrained by the ovsdb_type's 'n_min' and 'n_max'.
109 *
110 * If 'n' is nonzero, then 'keys' points to an array of 'n' atoms of the type
111 * specified by the ovsdb_type's 'key_type'. (Otherwise, 'keys' should be
112 * null.)
113 *
114 * If 'n' is nonzero and the ovsdb_type's 'value_type' is not
115 * OVSDB_TYPE_VOID, then 'values' points to an array of 'n' atoms of the type
116 * specified by the 'value_type'. (Otherwise, 'values' should be null.)
117 *
118 * Thus, for 'n' > 0, 'keys' will always be nonnull and 'values' will be
119 * nonnull only for "map" types.
120 */
121 struct ovsdb_datum {
122 unsigned int n; /* Number of 'keys' and 'values'. */
123 union ovsdb_atom *keys; /* Each of the ovsdb_type's 'key_type'. */
124 union ovsdb_atom *values; /* Each of the ovsdb_type's 'value_type'. */
125 };
126
127 /* Basics. */
128 void ovsdb_datum_init_empty(struct ovsdb_datum *);
129 void ovsdb_datum_init_default(struct ovsdb_datum *, const struct ovsdb_type *);
130 bool ovsdb_datum_is_default(const struct ovsdb_datum *,
131 const struct ovsdb_type *);
132 const struct ovsdb_datum *ovsdb_datum_default(const struct ovsdb_type *);
133 void ovsdb_datum_clone(struct ovsdb_datum *, const struct ovsdb_datum *,
134 const struct ovsdb_type *);
135 void ovsdb_datum_destroy(struct ovsdb_datum *, const struct ovsdb_type *);
136 void ovsdb_datum_swap(struct ovsdb_datum *, struct ovsdb_datum *);
137
138 /* Checking and maintaining invariants. */
139 struct ovsdb_error *ovsdb_datum_sort(struct ovsdb_datum *,
140 enum ovsdb_atomic_type key_type)
141 WARN_UNUSED_RESULT;
142
143 void ovsdb_datum_sort_assert(struct ovsdb_datum *,
144 enum ovsdb_atomic_type key_type);
145
146 size_t ovsdb_datum_sort_unique(struct ovsdb_datum *,
147 enum ovsdb_atomic_type key_type,
148 enum ovsdb_atomic_type value_type);
149
150 struct ovsdb_error *ovsdb_datum_check_constraints(
151 const struct ovsdb_datum *, const struct ovsdb_type *)
152 WARN_UNUSED_RESULT;
153
154 /* Type conversion. */
155 struct ovsdb_error *ovsdb_datum_from_json(struct ovsdb_datum *,
156 const struct ovsdb_type *,
157 const struct json *,
158 struct ovsdb_symbol_table *)
159 WARN_UNUSED_RESULT;
160 struct json *ovsdb_datum_to_json(const struct ovsdb_datum *,
161 const struct ovsdb_type *);
162
163 char *ovsdb_datum_from_string(struct ovsdb_datum *,
164 const struct ovsdb_type *, const char *,
165 struct ovsdb_symbol_table *)
166 WARN_UNUSED_RESULT;
167 void ovsdb_datum_to_string(const struct ovsdb_datum *,
168 const struct ovsdb_type *, struct ds *);
169
170 /* Comparison. */
171 uint32_t ovsdb_datum_hash(const struct ovsdb_datum *,
172 const struct ovsdb_type *, uint32_t basis);
173 int ovsdb_datum_compare_3way(const struct ovsdb_datum *,
174 const struct ovsdb_datum *,
175 const struct ovsdb_type *);
176 bool ovsdb_datum_equals(const struct ovsdb_datum *,
177 const struct ovsdb_datum *,
178 const struct ovsdb_type *);
179
180 /* Search. */
181 unsigned int ovsdb_datum_find_key(const struct ovsdb_datum *,
182 const union ovsdb_atom *key,
183 enum ovsdb_atomic_type key_type);
184 unsigned int ovsdb_datum_find_key_value(const struct ovsdb_datum *,
185 const union ovsdb_atom *key,
186 enum ovsdb_atomic_type key_type,
187 const union ovsdb_atom *value,
188 enum ovsdb_atomic_type value_type);
189
190 /* Set operations. */
191 bool ovsdb_datum_includes_all(const struct ovsdb_datum *,
192 const struct ovsdb_datum *,
193 const struct ovsdb_type *);
194 bool ovsdb_datum_excludes_all(const struct ovsdb_datum *,
195 const struct ovsdb_datum *,
196 const struct ovsdb_type *);
197 void ovsdb_datum_union(struct ovsdb_datum *,
198 const struct ovsdb_datum *,
199 const struct ovsdb_type *,
200 bool replace);
201 void ovsdb_datum_subtract(struct ovsdb_datum *a,
202 const struct ovsdb_type *a_type,
203 const struct ovsdb_datum *b,
204 const struct ovsdb_type *b_type);
205
206 /* Raw operations that may not maintain the invariants. */
207 void ovsdb_datum_remove_unsafe(struct ovsdb_datum *, size_t idx,
208 const struct ovsdb_type *);
209 void ovsdb_datum_add_unsafe(struct ovsdb_datum *,
210 const union ovsdb_atom *key,
211 const union ovsdb_atom *value,
212 const struct ovsdb_type *);
213
214 /* Type checking. */
215 static inline bool
216 ovsdb_datum_conforms_to_type(const struct ovsdb_datum *datum,
217 const struct ovsdb_type *type)
218 {
219 return datum->n >= type->n_min && datum->n <= type->n_max;
220 }
221 \f
222 /* A table mapping from names to data items. Currently the data items are
223 * always UUIDs; perhaps this will be expanded in the future. */
224
225 struct ovsdb_symbol {
226 struct uuid uuid; /* The UUID that the symbol represents. */
227 bool used; /* Already used as row UUID? */
228 };
229
230 struct ovsdb_symbol_table *ovsdb_symbol_table_create(void);
231 void ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *);
232 struct ovsdb_symbol *ovsdb_symbol_table_get(const struct ovsdb_symbol_table *,
233 const char *name);
234 struct ovsdb_symbol *ovsdb_symbol_table_put(struct ovsdb_symbol_table *,
235 const char *name,
236 const struct uuid *, bool used);
237 struct ovsdb_symbol *ovsdb_symbol_table_insert(struct ovsdb_symbol_table *,
238 const char *name);
239 const char *ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table *);
240 \f
241 /* Tokenization
242 *
243 * Used by ovsdb_atom_from_string() and ovsdb_datum_from_string(). */
244
245 char *ovsdb_token_parse(const char **, char **outp) WARN_UNUSED_RESULT;
246 bool ovsdb_token_is_delim(unsigned char);
247
248 #endif /* ovsdb-data.h */