1 /* Copyright (C) 2016 Hewlett Packard Enterprise Development LP
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may
5 * not use this file except in compliance with the License. You may obtain
6 * a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
18 #include "ovsdb-map-op.h"
23 /* Map Operation: a Partial Map Update */
25 struct hmap_node node;
26 struct ovsdb_datum *datum;
27 enum map_op_type type;
30 /* List of Map Operations */
35 static void map_op_destroy_datum(struct map_op *, const struct ovsdb_type *);
36 static struct map_op *map_op_list_find(struct map_op_list *, struct map_op *,
37 const struct ovsdb_type *, size_t);
40 map_op_create(struct ovsdb_datum *datum, enum map_op_type type)
42 struct map_op *map_op = xmalloc(sizeof *map_op);
43 map_op->node.hash = 0;
44 map_op->node.next = HMAP_NODE_NULL;
45 map_op->datum = datum;
51 map_op_destroy_datum(struct map_op *map_op, const struct ovsdb_type *type)
53 if (map_op->type == MAP_OP_DELETE){
54 struct ovsdb_type type_ = *type;
55 type_.value.type = OVSDB_TYPE_VOID;
56 ovsdb_datum_destroy(map_op->datum, &type_);
58 ovsdb_datum_destroy(map_op->datum, type);
65 map_op_destroy(struct map_op *map_op, const struct ovsdb_type *type)
67 map_op_destroy_datum(map_op, type);
72 map_op_datum(const struct map_op *map_op)
78 map_op_type(const struct map_op *map_op)
84 map_op_list_create(void)
86 struct map_op_list *list = xmalloc(sizeof *list);
87 hmap_init(&list->hmap);
92 map_op_list_destroy(struct map_op_list *list, const struct ovsdb_type *type)
94 struct map_op *map_op, *next;
95 HMAP_FOR_EACH_SAFE (map_op, next, node, &list->hmap) {
96 map_op_destroy(map_op, type);
98 hmap_destroy(&list->hmap);
102 static struct map_op*
103 map_op_list_find(struct map_op_list *list, struct map_op *map_op,
104 const struct ovsdb_type *type, size_t hash)
106 struct map_op *found = NULL;
108 HMAP_FOR_EACH_WITH_HASH(old, node, hash, &list->hmap) {
109 if (ovsdb_atom_equals(&old->datum->keys[0], &map_op->datum->keys[0],
118 /* Inserts 'map_op' into 'list'. Makes sure that any conflict with a previous
119 * map operation is resolved, so only one map operation is possible on each key
120 * per transactions. 'type' must be the type of the column over which the map
121 * operation will be applied. */
123 map_op_list_add(struct map_op_list *list, struct map_op *map_op,
124 const struct ovsdb_type *type)
126 /* Check if there is a previous update with the same key. */
128 struct map_op *prev_map_op;
130 hash = ovsdb_atom_hash(&map_op->datum->keys[0], type->key.type, 0);
131 prev_map_op = map_op_list_find(list, map_op, type, hash);
132 if (prev_map_op == NULL){
133 hmap_insert(&list->hmap, &map_op->node, hash);
135 if (prev_map_op->type == MAP_OP_INSERT &&
136 map_op->type == MAP_OP_DELETE) {
137 /* These operations cancel each other out. */
138 hmap_remove(&list->hmap, &prev_map_op->node);
139 map_op_destroy(prev_map_op, type);
140 map_op_destroy(map_op, type);
142 /* For any other case, the new update operation replaces
143 * the previous update operation. */
144 map_op_destroy_datum(prev_map_op, type);
145 prev_map_op->type = map_op->type;
146 prev_map_op->datum = map_op->datum;
153 map_op_list_first(struct map_op_list *list)
155 struct hmap_node *node = hmap_first(&list->hmap);
159 struct map_op *map_op = CONTAINER_OF(node, struct map_op, node);
164 map_op_list_next(struct map_op_list *list, struct map_op *map_op)
166 struct hmap_node *node = hmap_next(&list->hmap, &map_op->node);
170 struct map_op *next = CONTAINER_OF(node, struct map_op, node);