ovsdb: Document some ovsdb-data.[ch] functions.
[sliver-openvswitch.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 bool ovsdb_atom_is_default(const union ovsdb_atom *, enum ovsdb_atomic_type);
37 void ovsdb_atom_clone(union ovsdb_atom *, const union ovsdb_atom *,
38                       enum ovsdb_atomic_type);
39 void ovsdb_atom_swap(union ovsdb_atom *, union ovsdb_atom *);
40
41 /* Returns false if ovsdb_atom_destroy() is a no-op when it is applied to an
42  * initialized atom of the given 'type', true if ovsdb_atom_destroy() actually
43  * does something.
44  *
45  * This can be used to avoid calling ovsdb_atom_destroy() for each element in
46  * an array of homogeneous atoms.  (It's not worthwhile for a single atom.) */
47 static inline bool
48 ovsdb_atom_needs_destruction(enum ovsdb_atomic_type type)
49 {
50     return type == OVSDB_TYPE_STRING;
51 }
52
53 /* Frees the contents of 'atom', which must have the specified 'type'.
54  *
55  * This does not actually call free(atom).  If necessary, the caller must be
56  * responsible for that. */
57 static inline void
58 ovsdb_atom_destroy(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
59 {
60     if (type == OVSDB_TYPE_STRING) {
61         free(atom->string);
62     }
63 }
64
65 uint32_t ovsdb_atom_hash(const union ovsdb_atom *, enum ovsdb_atomic_type,
66                          uint32_t basis);
67
68 int ovsdb_atom_compare_3way(const union ovsdb_atom *,
69                             const union ovsdb_atom *,
70                             enum ovsdb_atomic_type);
71
72 /* Returns true if 'a' and 'b', which are both of type 'type', has the same
73  * contents, false if their contents differ.  */
74 static inline bool ovsdb_atom_equals(const union ovsdb_atom *a,
75                                      const union ovsdb_atom *b,
76                                      enum ovsdb_atomic_type type)
77 {
78     return !ovsdb_atom_compare_3way(a, b, type);
79 }
80
81 struct ovsdb_error *ovsdb_atom_from_json(union ovsdb_atom *,
82                                          const struct ovsdb_base_type *,
83                                          const struct json *,
84                                          struct ovsdb_symbol_table *)
85     WARN_UNUSED_RESULT;
86 struct json *ovsdb_atom_to_json(const union ovsdb_atom *,
87                                 enum ovsdb_atomic_type);
88
89 char *ovsdb_atom_from_string(union ovsdb_atom *,
90                              const struct ovsdb_base_type *, const char *,
91                              struct ovsdb_symbol_table *)
92     WARN_UNUSED_RESULT;
93 void ovsdb_atom_to_string(const union ovsdb_atom *, enum ovsdb_atomic_type,
94                           struct ds *);
95
96 struct ovsdb_error *ovsdb_atom_check_constraints(
97     const union ovsdb_atom *, const struct ovsdb_base_type *)
98     WARN_UNUSED_RESULT;
99 \f
100 /* An instance of an OVSDB type (given by struct ovsdb_type).
101  *
102  * - The 'keys' must be unique and in sorted order.  Most functions that modify
103  *   an ovsdb_datum maintain these invariants.  Functions that don't maintain
104  *   the invariants have names that end in "_unsafe".  Use ovsdb_datum_sort()
105  *   to check and restore these invariants.
106  *
107  * - 'n' is constrained by the ovsdb_type's 'n_min' and 'n_max'.
108  *
109  *   If 'n' is nonzero, then 'keys' points to an array of 'n' atoms of the type
110  *   specified by the ovsdb_type's 'key_type'.  (Otherwise, 'keys' should be
111  *   null.)
112  *
113  *   If 'n' is nonzero and the ovsdb_type's 'value_type' is not
114  *   OVSDB_TYPE_VOID, then 'values' points to an array of 'n' atoms of the type
115  *   specified by the 'value_type'.  (Otherwise, 'values' should be null.)
116  *
117  *   Thus, for 'n' > 0, 'keys' will always be nonnull and 'values' will be
118  *   nonnull only for "map" types.
119  */
120 struct ovsdb_datum {
121     unsigned int n;             /* Number of 'keys' and 'values'. */
122     union ovsdb_atom *keys;     /* Each of the ovsdb_type's 'key_type'. */
123     union ovsdb_atom *values;   /* Each of the ovsdb_type's 'value_type'. */
124 };
125
126 /* Basics. */
127 void ovsdb_datum_init_empty(struct ovsdb_datum *);
128 void ovsdb_datum_init_default(struct ovsdb_datum *, const struct ovsdb_type *);
129 bool ovsdb_datum_is_default(const struct ovsdb_datum *,
130                             const struct ovsdb_type *);
131 void ovsdb_datum_clone(struct ovsdb_datum *, const struct ovsdb_datum *,
132                        const struct ovsdb_type *);
133 void ovsdb_datum_destroy(struct ovsdb_datum *, const struct ovsdb_type *);
134 void ovsdb_datum_swap(struct ovsdb_datum *, struct ovsdb_datum *);
135
136 /* Checking and maintaining invariants. */
137 struct ovsdb_error *ovsdb_datum_sort(struct ovsdb_datum *,
138                                      enum ovsdb_atomic_type key_type)
139     WARN_UNUSED_RESULT;
140
141 void ovsdb_datum_sort_assert(struct ovsdb_datum *,
142                              enum ovsdb_atomic_type key_type);
143
144 struct ovsdb_error *ovsdb_datum_check_constraints(
145     const struct ovsdb_datum *, const struct ovsdb_type *)
146     WARN_UNUSED_RESULT;
147
148 /* Type conversion. */
149 struct ovsdb_error *ovsdb_datum_from_json(struct ovsdb_datum *,
150                                           const struct ovsdb_type *,
151                                           const struct json *,
152                                           struct ovsdb_symbol_table *)
153     WARN_UNUSED_RESULT;
154 struct json *ovsdb_datum_to_json(const struct ovsdb_datum *,
155                                  const struct ovsdb_type *);
156
157 char *ovsdb_datum_from_string(struct ovsdb_datum *,
158                               const struct ovsdb_type *, const char *,
159                               struct ovsdb_symbol_table *)
160     WARN_UNUSED_RESULT;
161 void ovsdb_datum_to_string(const struct ovsdb_datum *,
162                            const struct ovsdb_type *, struct ds *);
163
164 /* Comparison. */
165 uint32_t ovsdb_datum_hash(const struct ovsdb_datum *,
166                           const struct ovsdb_type *, uint32_t basis);
167 int ovsdb_datum_compare_3way(const struct ovsdb_datum *,
168                              const struct ovsdb_datum *,
169                              const struct ovsdb_type *);
170 bool ovsdb_datum_equals(const struct ovsdb_datum *,
171                         const struct ovsdb_datum *,
172                         const struct ovsdb_type *);
173
174 /* Search. */
175 unsigned int ovsdb_datum_find_key(const struct ovsdb_datum *,
176                                   const union ovsdb_atom *key,
177                                   enum ovsdb_atomic_type key_type);
178 unsigned int ovsdb_datum_find_key_value(const struct ovsdb_datum *,
179                                         const union ovsdb_atom *key,
180                                         enum ovsdb_atomic_type key_type,
181                                         const union ovsdb_atom *value,
182                                         enum ovsdb_atomic_type value_type);
183
184 /* Set operations. */
185 bool ovsdb_datum_includes_all(const struct ovsdb_datum *,
186                               const struct ovsdb_datum *,
187                               const struct ovsdb_type *);
188 bool ovsdb_datum_excludes_all(const struct ovsdb_datum *,
189                               const struct ovsdb_datum *,
190                               const struct ovsdb_type *);
191 void ovsdb_datum_union(struct ovsdb_datum *,
192                        const struct ovsdb_datum *,
193                        const struct ovsdb_type *,
194                        bool replace);
195 void ovsdb_datum_subtract(struct ovsdb_datum *a,
196                           const struct ovsdb_type *a_type,
197                           const struct ovsdb_datum *b,
198                           const struct ovsdb_type *b_type);
199
200 /* Raw operations that may not maintain the invariants. */
201 void ovsdb_datum_remove_unsafe(struct ovsdb_datum *, size_t idx,
202                                const struct ovsdb_type *);
203 void ovsdb_datum_add_unsafe(struct ovsdb_datum *,
204                             const union ovsdb_atom *key,
205                             const union ovsdb_atom *value,
206                             const struct ovsdb_type *);
207
208 /* Type checking. */
209 static inline bool
210 ovsdb_datum_conforms_to_type(const struct ovsdb_datum *datum,
211                              const struct ovsdb_type *type)
212 {
213     return datum->n >= type->n_min && datum->n <= type->n_max;
214 }
215 \f
216 /* A table mapping from names to data items.  Currently the data items are
217  * always UUIDs; perhaps this will be expanded in the future. */
218
219 struct ovsdb_symbol {
220     struct uuid uuid;           /* The UUID that the symbol represents. */
221     bool used;                  /* Already used as row UUID? */
222 };
223
224 struct ovsdb_symbol_table *ovsdb_symbol_table_create(void);
225 void ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *);
226 struct ovsdb_symbol *ovsdb_symbol_table_get(const struct ovsdb_symbol_table *,
227                                             const char *name);
228 struct ovsdb_symbol *ovsdb_symbol_table_put(struct ovsdb_symbol_table *,
229                                             const char *name,
230                                             const struct uuid *, bool used);
231 struct ovsdb_symbol *ovsdb_symbol_table_insert(struct ovsdb_symbol_table *,
232                                                const char *name);
233 const char *ovsdb_symbol_table_find_unused(const struct ovsdb_symbol_table *);
234 \f
235 /* Tokenization
236  *
237  * Used by ovsdb_atom_from_string() and ovsdb_datum_from_string(). */
238
239 char *ovsdb_token_parse(const char **, char **outp) WARN_UNUSED_RESULT;
240 bool ovsdb_token_is_delim(unsigned char);
241
242 #endif /* ovsdb-data.h */