Prepare Open vSwitch 1.1.2 release.
[sliver-openvswitch.git] / ovsdb / row.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_ROW_H
17 #define OVSDB_ROW_H 1
18
19 #include <stddef.h>
20 #include <stdint.h>
21 #include "column.h"
22 #include "hmap.h"
23 #include "list.h"
24 #include "ovsdb-data.h"
25
26 struct ovsdb_column_set;
27
28 /* A weak reference.
29  *
30  * When a column in row A contains a weak reference to UUID of a row B this
31  * constitutes a weak reference from A (the source) to B (the destination).
32  *
33  * Rows A and B may be in the same table or different tables.
34  *
35  * Weak references from a row to itself are allowed, but no "struct
36  * ovsdb_weak_ref" structures are created for them.
37  */
38 struct ovsdb_weak_ref {
39     struct list src_node;       /* In src->src_refs list. */
40     struct list dst_node;       /* In destination row's dst_refs list. */
41     struct ovsdb_row *src;      /* Source row. */
42 };
43
44 /* A row in a database table. */
45 struct ovsdb_row {
46     struct ovsdb_table *table;     /* Table to which this belongs. */
47     struct hmap_node hmap_node;    /* Element in ovsdb_table's 'rows' hmap. */
48     struct ovsdb_txn_row *txn_row; /* Transaction that row is in, if any. */
49
50     /* Weak references. */
51     struct list src_refs;       /* Weak references from this row. */
52     struct list dst_refs;       /* Weak references to this row. */
53
54     /* Number of strong refs to this row from other rows, in this table or
55      * other tables, through 'uuid' columns that have a 'refTable' constraint
56      * pointing to this table and a 'refType' of "strong".  A row with nonzero
57      * 'n_refs' cannot be deleted.  Updated and checked only at transaction
58      * commit. */
59     size_t n_refs;
60
61     struct ovsdb_datum fields[];
62 };
63
64 struct ovsdb_row *ovsdb_row_create(const struct ovsdb_table *);
65 struct ovsdb_row *ovsdb_row_clone(const struct ovsdb_row *);
66 void ovsdb_row_destroy(struct ovsdb_row *);
67
68 uint32_t ovsdb_row_hash_columns(const struct ovsdb_row *,
69                                 const struct ovsdb_column_set *,
70                                 uint32_t basis);
71 bool ovsdb_row_equal_columns(const struct ovsdb_row *,
72                              const struct ovsdb_row *,
73                              const struct ovsdb_column_set *);
74 int ovsdb_row_compare_columns_3way(const struct ovsdb_row *,
75                                    const struct ovsdb_row *,
76                                    const struct ovsdb_column_set *);
77 void ovsdb_row_update_columns(struct ovsdb_row *, const struct ovsdb_row *,
78                               const struct ovsdb_column_set *);
79
80 struct ovsdb_error *ovsdb_row_from_json(struct ovsdb_row *,
81                                         const struct json *,
82                                         struct ovsdb_symbol_table *,
83                                         struct ovsdb_column_set *included)
84     WARN_UNUSED_RESULT;
85 struct json *ovsdb_row_to_json(const struct ovsdb_row *,
86                                const struct ovsdb_column_set *include);
87
88 static inline const struct uuid *
89 ovsdb_row_get_uuid(const struct ovsdb_row *row)
90 {
91     return &row->fields[OVSDB_COL_UUID].keys[0].uuid;
92 }
93
94 static inline struct uuid *
95 ovsdb_row_get_uuid_rw(struct ovsdb_row *row)
96 {
97     return &row->fields[OVSDB_COL_UUID].keys[0].uuid;
98 }
99
100 static inline const struct uuid *
101 ovsdb_row_get_version(const struct ovsdb_row *row)
102 {
103     return &row->fields[OVSDB_COL_VERSION].keys[0].uuid;
104 }
105
106 static inline struct uuid *
107 ovsdb_row_get_version_rw(struct ovsdb_row *row)
108 {
109     return &row->fields[OVSDB_COL_VERSION].keys[0].uuid;
110 }
111
112 static inline uint32_t
113 ovsdb_row_hash(const struct ovsdb_row *row)
114 {
115     return uuid_hash(ovsdb_row_get_uuid(row));
116 }
117 \f
118 /* An unordered collection of rows. */
119 struct ovsdb_row_set {
120     const struct ovsdb_row **rows;
121     size_t n_rows, allocated_rows;
122 };
123
124 #define OVSDB_ROW_SET_INITIALIZER { NULL, 0, 0 }
125
126 void ovsdb_row_set_init(struct ovsdb_row_set *);
127 void ovsdb_row_set_destroy(struct ovsdb_row_set *);
128 void ovsdb_row_set_add_row(struct ovsdb_row_set *, const struct ovsdb_row *);
129
130 struct json *ovsdb_row_set_to_json(const struct ovsdb_row_set *,
131                                    const struct ovsdb_column_set *);
132
133 void ovsdb_row_set_sort(struct ovsdb_row_set *,
134                         const struct ovsdb_column_set *);
135 \f
136 /* A hash table of rows.  A specified set of columns is used for hashing and
137  * comparing rows.
138  *
139  * The row hash doesn't necessarily own its rows.  They may be owned by, for
140  * example, an ovsdb_table. */
141 struct ovsdb_row_hash {
142     struct hmap rows;
143     struct ovsdb_column_set columns;
144 };
145
146 #define OVSDB_ROW_HASH_INITIALIZER(RH) \
147     { HMAP_INITIALIZER(&(RH).rows), OVSDB_COLUMN_SET_INITIALIZER }
148
149 struct ovsdb_row_hash_node {
150     struct hmap_node hmap_node;
151     const struct ovsdb_row *row;
152 };
153
154 void ovsdb_row_hash_init(struct ovsdb_row_hash *,
155                          const struct ovsdb_column_set *);
156 void ovsdb_row_hash_destroy(struct ovsdb_row_hash *, bool destroy_rows);
157 size_t ovsdb_row_hash_count(const struct ovsdb_row_hash *);
158 bool ovsdb_row_hash_contains(const struct ovsdb_row_hash *,
159                              const struct ovsdb_row *);
160 bool ovsdb_row_hash_contains_all(const struct ovsdb_row_hash *,
161                                  const struct ovsdb_row_hash *);
162 bool ovsdb_row_hash_insert(struct ovsdb_row_hash *, const struct ovsdb_row *);
163 bool ovsdb_row_hash_contains__(const struct ovsdb_row_hash *,
164                                const struct ovsdb_row *, size_t hash);
165 bool ovsdb_row_hash_insert__(struct ovsdb_row_hash *,
166                              const struct ovsdb_row *, size_t hash);
167
168 #endif /* ovsdb/row.h */