ovsdb: Weak references performance fix
[cascardo/ovs.git] / ovsdb / row.h
1 /* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
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 "openvswitch/hmap.h"
23 #include "openvswitch/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 ovs_list src_node;      /* In src->src_refs list. */
40     struct ovs_list dst_node;      /* In destination row's dst_refs list. */
41     struct ovsdb_row *src;         /* Source row. */
42     struct ovsdb_table *dst_table; /* Destination table. */
43     struct uuid dst;               /* Destination row uuid. */
44 };
45
46 /* A row in a database table. */
47 struct ovsdb_row {
48     struct hmap_node hmap_node;    /* Element in ovsdb_table's 'rows' hmap. */
49     struct ovsdb_table *table;     /* Table to which this belongs. */
50     struct ovsdb_txn_row *txn_row; /* Transaction that row is in, if any. */
51
52     /* Weak references. */
53     struct ovs_list src_refs;   /* Weak references from this row. */
54     struct ovs_list dst_refs;   /* Weak references to this row. */
55
56     /* Number of strong refs to this row from other rows, in this table or
57      * other tables, through 'uuid' columns that have a 'refTable' constraint
58      * pointing to this table and a 'refType' of "strong".  A row with nonzero
59      * 'n_refs' cannot be deleted.  Updated and checked only at transaction
60      * commit. */
61     size_t n_refs;
62
63     /* One datum for each column (shash_count(&table->schema->columns)
64      * elements). */
65     struct ovsdb_datum fields[];
66
67     /* Followed by table->schema->n_indexes "struct hmap_node"s.  In rows that
68      * have have been committed as part of the database, the hmap_node with
69      * index 'i' is contained in hmap table->indexes[i].  */
70 };
71
72 struct ovsdb_row *ovsdb_row_create(const struct ovsdb_table *);
73 struct ovsdb_row *ovsdb_row_clone(const struct ovsdb_row *);
74 void ovsdb_row_destroy(struct ovsdb_row *);
75
76 uint32_t ovsdb_row_hash_columns(const struct ovsdb_row *,
77                                 const struct ovsdb_column_set *,
78                                 uint32_t basis);
79 bool ovsdb_row_equal_columns(const struct ovsdb_row *,
80                              const struct ovsdb_row *,
81                              const struct ovsdb_column_set *);
82 int ovsdb_row_compare_columns_3way(const struct ovsdb_row *,
83                                    const struct ovsdb_row *,
84                                    const struct ovsdb_column_set *);
85 void ovsdb_row_update_columns(struct ovsdb_row *, const struct ovsdb_row *,
86                               const struct ovsdb_column_set *);
87 void ovsdb_row_columns_to_string(const struct ovsdb_row *,
88                                  const struct ovsdb_column_set *, struct ds *);
89 struct ovsdb_error *ovsdb_row_from_json(struct ovsdb_row *,
90                                         const struct json *,
91                                         struct ovsdb_symbol_table *,
92                                         struct ovsdb_column_set *included)
93     OVS_WARN_UNUSED_RESULT;
94 struct json *ovsdb_row_to_json(const struct ovsdb_row *,
95                                const struct ovsdb_column_set *include);
96
97 static inline const struct uuid *
98 ovsdb_row_get_uuid(const struct ovsdb_row *row)
99 {
100     return &row->fields[OVSDB_COL_UUID].keys[0].uuid;
101 }
102
103 static inline struct uuid *
104 ovsdb_row_get_uuid_rw(struct ovsdb_row *row)
105 {
106     return &row->fields[OVSDB_COL_UUID].keys[0].uuid;
107 }
108
109 static inline const struct uuid *
110 ovsdb_row_get_version(const struct ovsdb_row *row)
111 {
112     return &row->fields[OVSDB_COL_VERSION].keys[0].uuid;
113 }
114
115 static inline struct uuid *
116 ovsdb_row_get_version_rw(struct ovsdb_row *row)
117 {
118     return &row->fields[OVSDB_COL_VERSION].keys[0].uuid;
119 }
120
121 static inline uint32_t
122 ovsdb_row_hash(const struct ovsdb_row *row)
123 {
124     return uuid_hash(ovsdb_row_get_uuid(row));
125 }
126 \f
127 /* An unordered collection of rows. */
128 struct ovsdb_row_set {
129     const struct ovsdb_row **rows;
130     size_t n_rows, allocated_rows;
131 };
132
133 #define OVSDB_ROW_SET_INITIALIZER { NULL, 0, 0 }
134
135 void ovsdb_row_set_init(struct ovsdb_row_set *);
136 void ovsdb_row_set_destroy(struct ovsdb_row_set *);
137 void ovsdb_row_set_add_row(struct ovsdb_row_set *, const struct ovsdb_row *);
138
139 struct json *ovsdb_row_set_to_json(const struct ovsdb_row_set *,
140                                    const struct ovsdb_column_set *);
141
142 void ovsdb_row_set_sort(struct ovsdb_row_set *,
143                         const struct ovsdb_column_set *);
144 \f
145 /* A hash table of rows.  A specified set of columns is used for hashing and
146  * comparing rows.
147  *
148  * The row hash doesn't necessarily own its rows.  They may be owned by, for
149  * example, an ovsdb_table. */
150 struct ovsdb_row_hash {
151     struct hmap rows;
152     struct ovsdb_column_set columns;
153 };
154
155 #define OVSDB_ROW_HASH_INITIALIZER(RH) \
156     { HMAP_INITIALIZER(&(RH).rows), OVSDB_COLUMN_SET_INITIALIZER }
157
158 struct ovsdb_row_hash_node {
159     struct hmap_node hmap_node;
160     const struct ovsdb_row *row;
161 };
162
163 void ovsdb_row_hash_init(struct ovsdb_row_hash *,
164                          const struct ovsdb_column_set *);
165 void ovsdb_row_hash_destroy(struct ovsdb_row_hash *, bool destroy_rows);
166 size_t ovsdb_row_hash_count(const struct ovsdb_row_hash *);
167 bool ovsdb_row_hash_contains(const struct ovsdb_row_hash *,
168                              const struct ovsdb_row *);
169 bool ovsdb_row_hash_contains_all(const struct ovsdb_row_hash *,
170                                  const struct ovsdb_row_hash *);
171 bool ovsdb_row_hash_insert(struct ovsdb_row_hash *, const struct ovsdb_row *);
172 bool ovsdb_row_hash_contains__(const struct ovsdb_row_hash *,
173                                const struct ovsdb_row *, size_t hash);
174 bool ovsdb_row_hash_insert__(struct ovsdb_row_hash *,
175                              const struct ovsdb_row *, size_t hash);
176
177 #endif /* ovsdb/row.h */