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