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"
23 struct ovsdb_symbol_table;
25 /* One value of an atomic type (given by enum ovs_atomic_type). */
34 void ovsdb_atom_init_default(union ovsdb_atom *, enum ovsdb_atomic_type);
35 bool ovsdb_atom_is_default(const union ovsdb_atom *, enum ovsdb_atomic_type);
36 void ovsdb_atom_clone(union ovsdb_atom *, const union ovsdb_atom *,
37 enum ovsdb_atomic_type);
38 void ovsdb_atom_swap(union ovsdb_atom *, union ovsdb_atom *);
41 ovsdb_atom_needs_destruction(enum ovsdb_atomic_type type)
43 return type == OVSDB_TYPE_STRING;
47 ovsdb_atom_destroy(union ovsdb_atom *atom, enum ovsdb_atomic_type type)
49 if (type == OVSDB_TYPE_STRING) {
54 uint32_t ovsdb_atom_hash(const union ovsdb_atom *, enum ovsdb_atomic_type,
57 int ovsdb_atom_compare_3way(const union ovsdb_atom *,
58 const union ovsdb_atom *,
59 enum ovsdb_atomic_type);
61 static inline bool ovsdb_atom_equals(const union ovsdb_atom *a,
62 const union ovsdb_atom *b,
63 enum ovsdb_atomic_type type)
65 return !ovsdb_atom_compare_3way(a, b, type);
68 struct ovsdb_error *ovsdb_atom_from_json(union ovsdb_atom *,
69 enum ovsdb_atomic_type,
71 const struct ovsdb_symbol_table *)
73 struct json *ovsdb_atom_to_json(const union ovsdb_atom *,
74 enum ovsdb_atomic_type);
76 /* An instance of an OVSDB type (given by struct ovsdb_type).
78 * - The 'keys' must be unique and in sorted order. Most functions that modify
79 * an ovsdb_datum maintain these invariants. Functions that don't maintain
80 * the invariants have names that end in "_unsafe". Use ovsdb_datum_sort()
81 * to check and restore these invariants.
83 * - 'n' is constrained by the ovsdb_type's 'n_min' and 'n_max'.
85 * If 'n' is nonzero, then 'keys' points to an array of 'n' atoms of the type
86 * specified by the ovsdb_type's 'key_type'. (Otherwise, 'keys' should be
89 * If 'n' is nonzero and the ovsdb_type's 'value_type' is not
90 * OVSDB_TYPE_VOID, then 'values' points to an array of 'n' atoms of the type
91 * specified by the 'value_type'. (Otherwise, 'values' should be null.)
93 * Thus, for 'n' > 0, 'keys' will always be nonnull and 'values' will be
94 * nonnull only for "map" types.
97 unsigned int n; /* Number of 'keys' and 'values'. */
98 union ovsdb_atom *keys; /* Each of the ovsdb_type's 'key_type'. */
99 union ovsdb_atom *values; /* Each of the ovsdb_type's 'value_type'. */
103 void ovsdb_datum_init_empty(struct ovsdb_datum *);
104 void ovsdb_datum_init_default(struct ovsdb_datum *, const struct ovsdb_type *);
105 bool ovsdb_datum_is_default(const struct ovsdb_datum *,
106 const struct ovsdb_type *);
107 void ovsdb_datum_clone(struct ovsdb_datum *, const struct ovsdb_datum *,
108 const struct ovsdb_type *);
109 void ovsdb_datum_destroy(struct ovsdb_datum *, const struct ovsdb_type *);
110 void ovsdb_datum_swap(struct ovsdb_datum *, struct ovsdb_datum *);
112 /* Checking and maintaining invariants. */
113 struct ovsdb_error *ovsdb_datum_sort(struct ovsdb_datum *,
114 const struct ovsdb_type *);
116 /* Type conversion. */
117 struct ovsdb_error *ovsdb_datum_from_json(struct ovsdb_datum *,
118 const struct ovsdb_type *,
120 const struct ovsdb_symbol_table *)
122 struct json *ovsdb_datum_to_json(const struct ovsdb_datum *,
123 const struct ovsdb_type *);
125 uint32_t ovsdb_datum_hash(const struct ovsdb_datum *,
126 const struct ovsdb_type *, uint32_t basis);
127 int ovsdb_datum_compare_3way(const struct ovsdb_datum *,
128 const struct ovsdb_datum *,
129 const struct ovsdb_type *);
130 bool ovsdb_datum_equals(const struct ovsdb_datum *,
131 const struct ovsdb_datum *,
132 const struct ovsdb_type *);
135 unsigned int ovsdb_datum_find_key(const struct ovsdb_datum *,
136 const union ovsdb_atom *key,
137 enum ovsdb_atomic_type key_type);
138 unsigned int ovsdb_datum_find_key_value(const struct ovsdb_datum *,
139 const union ovsdb_atom *key,
140 enum ovsdb_atomic_type key_type,
141 const union ovsdb_atom *value,
142 enum ovsdb_atomic_type value_type);
144 /* Set operations. */
145 bool ovsdb_datum_includes_all(const struct ovsdb_datum *,
146 const struct ovsdb_datum *,
147 const struct ovsdb_type *);
148 bool ovsdb_datum_excludes_all(const struct ovsdb_datum *,
149 const struct ovsdb_datum *,
150 const struct ovsdb_type *);
151 void ovsdb_datum_union(struct ovsdb_datum *,
152 const struct ovsdb_datum *,
153 const struct ovsdb_type *,
155 void ovsdb_datum_subtract(struct ovsdb_datum *a,
156 const struct ovsdb_type *a_type,
157 const struct ovsdb_datum *b,
158 const struct ovsdb_type *b_type);
160 /* Raw operations that may not maintain the invariants. */
161 void ovsdb_datum_remove_unsafe(struct ovsdb_datum *, size_t idx,
162 const struct ovsdb_type *);
163 void ovsdb_datum_add_unsafe(struct ovsdb_datum *,
164 const union ovsdb_atom *key,
165 const union ovsdb_atom *value,
166 const struct ovsdb_type *);
170 ovsdb_datum_conforms_to_type(const struct ovsdb_datum *datum,
171 const struct ovsdb_type *type)
173 return datum->n >= type->n_min && datum->n <= type->n_max;
176 /* A table mapping from names to data items. Currently the data items are
177 * always UUIDs; perhaps this will be expanded in the future. */
179 struct ovsdb_symbol {
180 struct uuid uuid; /* The UUID that the symbol represents. */
181 bool used; /* Already used as row UUID? */
184 struct ovsdb_symbol_table *ovsdb_symbol_table_create(void);
185 void ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *);
186 struct ovsdb_symbol *ovsdb_symbol_table_get(const struct ovsdb_symbol_table *,
188 void ovsdb_symbol_table_put(struct ovsdb_symbol_table *, const char *name,
189 const struct uuid *, bool used);
191 #endif /* ovsdb-data.h */