Initial implementation of OVSDB.
[sliver-openvswitch.git] / ovsdb / row.h
1 /* Copyright (c) 2009 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     struct ovsdb_datum fields[];
33 };
34
35 struct ovsdb_row *ovsdb_row_create(const struct ovsdb_table *);
36 struct ovsdb_row *ovsdb_row_clone(const struct ovsdb_row *);
37 void ovsdb_row_destroy(struct ovsdb_row *);
38
39 uint32_t ovsdb_row_hash_columns(const struct ovsdb_row *,
40                                 const struct ovsdb_column_set *,
41                                 uint32_t basis);
42 bool ovsdb_row_equal_columns(const struct ovsdb_row *,
43                              const struct ovsdb_row *,
44                              const struct ovsdb_column_set *);
45 int ovsdb_row_compare_columns_3way(const struct ovsdb_row *,
46                                    const struct ovsdb_row *,
47                                    const struct ovsdb_column_set *);
48 void ovsdb_row_update_columns(struct ovsdb_row *, const struct ovsdb_row *,
49                               const struct ovsdb_column_set *);
50
51 struct ovsdb_error *ovsdb_row_from_json(struct ovsdb_row *,
52                                         const struct json *,
53                                         const struct ovsdb_symbol_table *,
54                                         struct ovsdb_column_set *included)
55     WARN_UNUSED_RESULT;
56 struct json *ovsdb_row_to_json(const struct ovsdb_row *,
57                                const struct ovsdb_column_set *include);
58
59 static inline const struct uuid *
60 ovsdb_row_get_uuid(const struct ovsdb_row *row)
61 {
62     return &row->fields[OVSDB_COL_UUID].keys[0].uuid;
63 }
64
65 static inline struct uuid *
66 ovsdb_row_get_uuid_rw(struct ovsdb_row *row)
67 {
68     return &row->fields[OVSDB_COL_UUID].keys[0].uuid;
69 }
70
71 static inline const struct uuid *
72 ovsdb_row_get_version(const struct ovsdb_row *row)
73 {
74     return &row->fields[OVSDB_COL_VERSION].keys[0].uuid;
75 }
76
77 static inline struct uuid *
78 ovsdb_row_get_version_rw(struct ovsdb_row *row)
79 {
80     return &row->fields[OVSDB_COL_VERSION].keys[0].uuid;
81 }
82
83 static inline uint32_t
84 ovsdb_row_hash(const struct ovsdb_row *row)
85 {
86     return uuid_hash(ovsdb_row_get_uuid(row));
87 }
88 \f
89 /* An unordered collection of rows. */
90 struct ovsdb_row_set {
91     const struct ovsdb_row **rows;
92     size_t n_rows, allocated_rows;
93 };
94
95 #define OVSDB_ROW_SET_INITIALIZER { NULL, 0, 0 }
96
97 void ovsdb_row_set_init(struct ovsdb_row_set *);
98 void ovsdb_row_set_destroy(struct ovsdb_row_set *);
99 void ovsdb_row_set_add_row(struct ovsdb_row_set *, const struct ovsdb_row *);
100
101 struct json *ovsdb_row_set_to_json(const struct ovsdb_row_set *,
102                                    const struct ovsdb_column_set *);
103
104 void ovsdb_row_set_sort(struct ovsdb_row_set *,
105                         const struct ovsdb_column_set *);
106 \f
107 /* A hash table of rows.  A specified set of columns is used for hashing and
108  * comparing rows.
109  *
110  * The row hash doesn't necessarily own its rows.  They may be owned by, for
111  * example, an ovsdb_table. */
112 struct ovsdb_row_hash {
113     struct hmap rows;
114     struct ovsdb_column_set columns;
115 };
116
117 #define OVSDB_ROW_HASH_INITIALIZER(RH) \
118     { HMAP_INITIALIZER(&(RH).rows), OVSDB_COLUMN_SET_INITIALIZER }
119
120 struct ovsdb_row_hash_node {
121     struct hmap_node hmap_node;
122     const struct ovsdb_row *row;
123 };
124
125 void ovsdb_row_hash_init(struct ovsdb_row_hash *,
126                          const struct ovsdb_column_set *);
127 void ovsdb_row_hash_destroy(struct ovsdb_row_hash *, bool destroy_rows);
128 size_t ovsdb_row_hash_count(const struct ovsdb_row_hash *);
129 bool ovsdb_row_hash_contains(const struct ovsdb_row_hash *,
130                              const struct ovsdb_row *);
131 bool ovsdb_row_hash_contains_all(const struct ovsdb_row_hash *,
132                                  const struct ovsdb_row_hash *);
133 bool ovsdb_row_hash_insert(struct ovsdb_row_hash *, const struct ovsdb_row *);
134 bool ovsdb_row_hash_contains__(const struct ovsdb_row_hash *,
135                                const struct ovsdb_row *, size_t hash);
136 bool ovsdb_row_hash_insert__(struct ovsdb_row_hash *,
137                              const struct ovsdb_row *, size_t hash);
138
139 #endif /* ovsdb/row.h */