1 /* Copyright (c) 2009, 2010 Nicira Networks
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define OVSDB_DATA_H 1
21 #include "ovsdb-types.h"
24 struct ovsdb_symbol_table;
26 /* One value of an atomic type (given by enum ovs_atomic_type). */
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 *);
42 ovsdb_atom_needs_destruction(enum ovsdb_atomic_type type)
44 return type == OVSDB_TYPE_STRING;
48 ovsdb_atom_destroy(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
50 if (type == OVSDB_TYPE_STRING) {
55 uint32_t ovsdb_atom_hash(const union ovsdb_atom *, enum ovsdb_atomic_type,
58 int ovsdb_atom_compare_3way(const union ovsdb_atom *,
59 const union ovsdb_atom *,
60 enum ovsdb_atomic_type);
62 static inline bool ovsdb_atom_equals(const union ovsdb_atom *a,
63 const union ovsdb_atom *b,
64 enum ovsdb_atomic_type type)
66 return !ovsdb_atom_compare_3way(a, b, type);
69 struct ovsdb_error *ovsdb_atom_from_json(union ovsdb_atom *,
70 enum ovsdb_atomic_type,
72 const struct ovsdb_symbol_table *)
74 struct json *ovsdb_atom_to_json(const union ovsdb_atom *,
75 enum ovsdb_atomic_type);
77 void ovsdb_atom_from_string(union ovsdb_atom *, enum ovsdb_atomic_type,
79 void ovsdb_atom_to_string(const union ovsdb_atom *, enum ovsdb_atomic_type,
82 /* An instance of an OVSDB type (given by struct ovsdb_type).
84 * - The 'keys' must be unique and in sorted order. Most functions that modify
85 * an ovsdb_datum maintain these invariants. Functions that don't maintain
86 * the invariants have names that end in "_unsafe". Use ovsdb_datum_sort()
87 * to check and restore these invariants.
89 * - 'n' is constrained by the ovsdb_type's 'n_min' and 'n_max'.
91 * If 'n' is nonzero, then 'keys' points to an array of 'n' atoms of the type
92 * specified by the ovsdb_type's 'key_type'. (Otherwise, 'keys' should be
95 * If 'n' is nonzero and the ovsdb_type's 'value_type' is not
96 * OVSDB_TYPE_VOID, then 'values' points to an array of 'n' atoms of the type
97 * specified by the 'value_type'. (Otherwise, 'values' should be null.)
99 * Thus, for 'n' > 0, 'keys' will always be nonnull and 'values' will be
100 * nonnull only for "map" types.
103 unsigned int n; /* Number of 'keys' and 'values'. */
104 union ovsdb_atom *keys; /* Each of the ovsdb_type's 'key_type'. */
105 union ovsdb_atom *values; /* Each of the ovsdb_type's 'value_type'. */
109 void ovsdb_datum_init_empty(struct ovsdb_datum *);
110 void ovsdb_datum_init_default(struct ovsdb_datum *, const struct ovsdb_type *);
111 bool ovsdb_datum_is_default(const struct ovsdb_datum *,
112 const struct ovsdb_type *);
113 void ovsdb_datum_clone(struct ovsdb_datum *, const struct ovsdb_datum *,
114 const struct ovsdb_type *);
115 void ovsdb_datum_destroy(struct ovsdb_datum *, const struct ovsdb_type *);
116 void ovsdb_datum_swap(struct ovsdb_datum *, struct ovsdb_datum *);
118 /* Checking and maintaining invariants. */
119 struct ovsdb_error *ovsdb_datum_sort(struct ovsdb_datum *,
120 const struct ovsdb_type *);
122 /* Type conversion. */
123 struct ovsdb_error *ovsdb_datum_from_json(struct ovsdb_datum *,
124 const struct ovsdb_type *,
126 const struct ovsdb_symbol_table *)
128 struct json *ovsdb_datum_to_json(const struct ovsdb_datum *,
129 const struct ovsdb_type *);
131 void ovsdb_datum_from_string(struct ovsdb_datum *,
132 const struct ovsdb_type *, const char *);
133 void ovsdb_datum_to_string(const struct ovsdb_datum *,
134 const struct ovsdb_type *, struct ds *);
137 uint32_t ovsdb_datum_hash(const struct ovsdb_datum *,
138 const struct ovsdb_type *, uint32_t basis);
139 int ovsdb_datum_compare_3way(const struct ovsdb_datum *,
140 const struct ovsdb_datum *,
141 const struct ovsdb_type *);
142 bool ovsdb_datum_equals(const struct ovsdb_datum *,
143 const struct ovsdb_datum *,
144 const struct ovsdb_type *);
147 unsigned int ovsdb_datum_find_key(const struct ovsdb_datum *,
148 const union ovsdb_atom *key,
149 enum ovsdb_atomic_type key_type);
150 unsigned int ovsdb_datum_find_key_value(const struct ovsdb_datum *,
151 const union ovsdb_atom *key,
152 enum ovsdb_atomic_type key_type,
153 const union ovsdb_atom *value,
154 enum ovsdb_atomic_type value_type);
156 /* Set operations. */
157 bool ovsdb_datum_includes_all(const struct ovsdb_datum *,
158 const struct ovsdb_datum *,
159 const struct ovsdb_type *);
160 bool ovsdb_datum_excludes_all(const struct ovsdb_datum *,
161 const struct ovsdb_datum *,
162 const struct ovsdb_type *);
163 void ovsdb_datum_union(struct ovsdb_datum *,
164 const struct ovsdb_datum *,
165 const struct ovsdb_type *,
167 void ovsdb_datum_subtract(struct ovsdb_datum *a,
168 const struct ovsdb_type *a_type,
169 const struct ovsdb_datum *b,
170 const struct ovsdb_type *b_type);
172 /* Raw operations that may not maintain the invariants. */
173 void ovsdb_datum_remove_unsafe(struct ovsdb_datum *, size_t idx,
174 const struct ovsdb_type *);
175 void ovsdb_datum_add_unsafe(struct ovsdb_datum *,
176 const union ovsdb_atom *key,
177 const union ovsdb_atom *value,
178 const struct ovsdb_type *);
182 ovsdb_datum_conforms_to_type(const struct ovsdb_datum *datum,
183 const struct ovsdb_type *type)
185 return datum->n >= type->n_min && datum->n <= type->n_max;
188 /* A table mapping from names to data items. Currently the data items are
189 * always UUIDs; perhaps this will be expanded in the future. */
191 struct ovsdb_symbol {
192 struct uuid uuid; /* The UUID that the symbol represents. */
193 bool used; /* Already used as row UUID? */
196 struct ovsdb_symbol_table *ovsdb_symbol_table_create(void);
197 void ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *);
198 struct ovsdb_symbol *ovsdb_symbol_table_get(const struct ovsdb_symbol_table *,
200 void ovsdb_symbol_table_put(struct ovsdb_symbol_table *, const char *name,
201 const struct uuid *, bool used);
205 * Used by ovsdb_atom_from_string() and ovsdb_datum_from_string(). */
207 const char *ovsdb_token_parse(const char *, char **outp);
208 bool ovsdb_token_is_delim(unsigned char);
210 #endif /* ovsdb-data.h */