ovsdb: Get rid of "declare" operation.
[cascardo/ovs.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 "ovsdb-data.h"
24
25 struct ovsdb_column_set;
26
27 /* A row in a database table. */
28 struct ovsdb_row {
29     struct ovsdb_table *table;  /* Table to which this belongs. */
30     struct hmap_node hmap_node; /* Element in ovsdb_table's 'rows' hmap. */
31     struct ovsdb_txn_row *txn_row; /* Transaction that row is in, if any. */
32
33     /* Number of refs to this row from other rows, in this table or other
34      * tables, through 'uuid' columns that have a 'refTable' constraint
35      * pointing to this table.  A row with nonzero 'n_refs' cannot be deleted.
36      * Updated and checked only at transaction commit. */
37     size_t n_refs;
38
39     struct ovsdb_datum fields[];
40 };
41
42 struct ovsdb_row *ovsdb_row_create(const struct ovsdb_table *);
43 struct ovsdb_row *ovsdb_row_clone(const struct ovsdb_row *);
44 void ovsdb_row_destroy(struct ovsdb_row *);
45
46 uint32_t ovsdb_row_hash_columns(const struct ovsdb_row *,
47                                 const struct ovsdb_column_set *,
48                                 uint32_t basis);
49 bool ovsdb_row_equal_columns(const struct ovsdb_row *,
50                              const struct ovsdb_row *,
51                              const struct ovsdb_column_set *);
52 int ovsdb_row_compare_columns_3way(const struct ovsdb_row *,
53                                    const struct ovsdb_row *,
54                                    const struct ovsdb_column_set *);
55 void ovsdb_row_update_columns(struct ovsdb_row *, const struct ovsdb_row *,
56                               const struct ovsdb_column_set *);
57
58 struct ovsdb_error *ovsdb_row_from_json(struct ovsdb_row *,
59                                         const struct json *,
60                                         struct ovsdb_symbol_table *,
61                                         struct ovsdb_column_set *included)
62     WARN_UNUSED_RESULT;
63 struct json *ovsdb_row_to_json(const struct ovsdb_row *,
64                                const struct ovsdb_column_set *include);
65
66 static inline const struct uuid *
67 ovsdb_row_get_uuid(const struct ovsdb_row *row)
68 {
69     return &row->fields[OVSDB_COL_UUID].keys[0].uuid;
70 }
71
72 static inline struct uuid *
73 ovsdb_row_get_uuid_rw(struct ovsdb_row *row)
74 {
75     return &row->fields[OVSDB_COL_UUID].keys[0].uuid;
76 }
77
78 static inline const struct uuid *
79 ovsdb_row_get_version(const struct ovsdb_row *row)
80 {
81     return &row->fields[OVSDB_COL_VERSION].keys[0].uuid;
82 }
83
84 static inline struct uuid *
85 ovsdb_row_get_version_rw(struct ovsdb_row *row)
86 {
87     return &row->fields[OVSDB_COL_VERSION].keys[0].uuid;
88 }
89
90 static inline uint32_t
91 ovsdb_row_hash(const struct ovsdb_row *row)
92 {
93     return uuid_hash(ovsdb_row_get_uuid(row));
94 }
95 \f
96 /* An unordered collection of rows. */
97 struct ovsdb_row_set {
98     const struct ovsdb_row **rows;
99     size_t n_rows, allocated_rows;
100 };
101
102 #define OVSDB_ROW_SET_INITIALIZER { NULL, 0, 0 }
103
104 void ovsdb_row_set_init(struct ovsdb_row_set *);
105 void ovsdb_row_set_destroy(struct ovsdb_row_set *);
106 void ovsdb_row_set_add_row(struct ovsdb_row_set *, const struct ovsdb_row *);
107
108 struct json *ovsdb_row_set_to_json(const struct ovsdb_row_set *,
109                                    const struct ovsdb_column_set *);
110
111 void ovsdb_row_set_sort(struct ovsdb_row_set *,
112                         const struct ovsdb_column_set *);
113 \f
114 /* A hash table of rows.  A specified set of columns is used for hashing and
115  * comparing rows.
116  *
117  * The row hash doesn't necessarily own its rows.  They may be owned by, for
118  * example, an ovsdb_table. */
119 struct ovsdb_row_hash {
120     struct hmap rows;
121     struct ovsdb_column_set columns;
122 };
123
124 #define OVSDB_ROW_HASH_INITIALIZER(RH) \
125     { HMAP_INITIALIZER(&(RH).rows), OVSDB_COLUMN_SET_INITIALIZER }
126
127 struct ovsdb_row_hash_node {
128     struct hmap_node hmap_node;
129     const struct ovsdb_row *row;
130 };
131
132 void ovsdb_row_hash_init(struct ovsdb_row_hash *,
133                          const struct ovsdb_column_set *);
134 void ovsdb_row_hash_destroy(struct ovsdb_row_hash *, bool destroy_rows);
135 size_t ovsdb_row_hash_count(const struct ovsdb_row_hash *);
136 bool ovsdb_row_hash_contains(const struct ovsdb_row_hash *,
137                              const struct ovsdb_row *);
138 bool ovsdb_row_hash_contains_all(const struct ovsdb_row_hash *,
139                                  const struct ovsdb_row_hash *);
140 bool ovsdb_row_hash_insert(struct ovsdb_row_hash *, const struct ovsdb_row *);
141 bool ovsdb_row_hash_contains__(const struct ovsdb_row_hash *,
142                                const struct ovsdb_row *, size_t hash);
143 bool ovsdb_row_hash_insert__(struct ovsdb_row_hash *,
144                              const struct ovsdb_row *, size_t hash);
145
146 #endif /* ovsdb/row.h */