json: Move from lib to include/openvswitch.
[cascardo/ovs.git] / ovsdb / transaction.c
index 23d5eeb..865e9b6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009, 2010, 2011 Nicira Networks
+/* Copyright (c) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 #include "transaction.h"
 
-#include <assert.h>
-
 #include "bitmap.h"
-#include "dynamic-string.h"
+#include "openvswitch/dynamic-string.h"
 #include "hash.h"
-#include "hmap.h"
-#include "json.h"
-#include "list.h"
+#include "openvswitch/hmap.h"
+#include "openvswitch/json.h"
+#include "openvswitch/list.h"
 #include "ovsdb-error.h"
 #include "ovsdb.h"
 #include "row.h"
 #include "table.h"
+#include "perf-counter.h"
 #include "uuid.h"
 
 struct ovsdb_txn {
     struct ovsdb *db;
-    struct list txn_tables;     /* Contains "struct ovsdb_txn_table"s. */
+    struct ovs_list txn_tables; /* Contains "struct ovsdb_txn_table"s. */
     struct ds comment;
 };
 
 /* A table modified by a transaction. */
 struct ovsdb_txn_table {
-    struct list node;           /* Element in ovsdb_txn's txn_tables list. */
+    struct ovs_list node;       /* Element in ovsdb_txn's txn_tables list. */
     struct ovsdb_table *table;
     struct hmap txn_rows;       /* Contains "struct ovsdb_txn_row"s. */
 
@@ -86,10 +85,10 @@ struct ovsdb_txn_row {
     unsigned long changed[];    /* Bits set to 1 for columns that changed. */
 };
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *r);
 static void ovsdb_txn_row_prefree(struct ovsdb_txn_row *);
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 for_each_txn_row(struct ovsdb_txn *txn,
                       struct ovsdb_error *(*)(struct ovsdb_txn *,
                                               struct ovsdb_txn_row *));
@@ -103,7 +102,7 @@ ovsdb_txn_create(struct ovsdb *db)
 {
     struct ovsdb_txn *txn = xmalloc(sizeof *txn);
     txn->db = db;
-    list_init(&txn->txn_tables);
+    ovs_list_init(&txn->txn_tables);
     ds_init(&txn->comment);
     return txn;
 }
@@ -111,7 +110,7 @@ ovsdb_txn_create(struct ovsdb *db)
 static void
 ovsdb_txn_free(struct ovsdb_txn *txn)
 {
-    assert(list_is_empty(&txn->txn_tables));
+    ovs_assert(ovs_list_is_empty(&txn->txn_tables));
     ds_destroy(&txn->comment);
     free(txn);
 }
@@ -206,7 +205,7 @@ find_or_make_txn_row(struct ovsdb_txn *txn, const struct ovsdb_table *table,
     return txn_row;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
                            const struct ovsdb_column *c,
                            const struct ovsdb_base_type *base,
@@ -246,7 +245,7 @@ ovsdb_txn_adjust_atom_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
                           const struct ovsdb_column *column, int delta)
 {
@@ -262,7 +261,7 @@ ovsdb_txn_adjust_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *r,
     return error;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
 {
     struct ovsdb_table *table = r->table;
@@ -272,16 +271,18 @@ update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
         const struct ovsdb_column *column = node->data;
         struct ovsdb_error *error;
 
-        if (r->old) {
-            error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
-            if (error) {
-                return OVSDB_WRAP_BUG("error decreasing refcount", error);
+        if (bitmap_is_set(r->changed, column->index)) {
+            if (r->old) {
+                error = ovsdb_txn_adjust_row_refs(txn, r->old, column, -1);
+                if (error) {
+                    return OVSDB_WRAP_BUG("error decreasing refcount", error);
+                }
             }
-        }
-        if (r->new) {
-            error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
-            if (error) {
-                return error;
+            if (r->new) {
+                error = ovsdb_txn_adjust_row_refs(txn, r->new, column, 1);
+                if (error) {
+                    return error;
+                }
             }
         }
     }
@@ -289,7 +290,7 @@ update_row_ref_count(struct ovsdb_txn *txn, struct ovsdb_txn_row *r)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
 {
     if (r->new || !r->n_refs) {
@@ -297,13 +298,13 @@ check_ref_count(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *r)
     } else {
         return ovsdb_error("referential integrity violation",
                            "cannot delete %s row "UUID_FMT" because "
-                           "of %zu remaining reference(s)",
+                           "of %"PRIuSIZE" remaining reference(s)",
                            r->table->schema->name, UUID_ARGS(&r->uuid),
                            r->n_refs);
     }
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 delete_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *row,
                 const struct ovsdb_base_type *base,
                 const union ovsdb_atom *atoms, unsigned int n)
@@ -345,7 +346,7 @@ delete_row_refs(struct ovsdb_txn *txn, const struct ovsdb_row *row,
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
 {
     struct shash_node *node;
@@ -380,7 +381,7 @@ delete_garbage_row(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 collect_garbage(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
 {
     if (txn_row->new && !txn_row->n_refs) {
@@ -389,7 +390,7 @@ collect_garbage(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 update_ref_counts(struct ovsdb_txn *txn)
 {
     struct ovsdb_error *error;
@@ -439,8 +440,8 @@ static void
 add_weak_ref(struct ovsdb_txn *txn,
              const struct ovsdb_row *src_, const struct ovsdb_row *dst_)
 {
-    struct ovsdb_row *src = (struct ovsdb_row *) src_;
-    struct ovsdb_row *dst = (struct ovsdb_row *) dst_;
+    struct ovsdb_row *src = CONST_CAST(struct ovsdb_row *, src_);
+    struct ovsdb_row *dst = CONST_CAST(struct ovsdb_row *, dst_);
     struct ovsdb_weak_ref *weak;
 
     if (src == dst) {
@@ -449,9 +450,9 @@ add_weak_ref(struct ovsdb_txn *txn,
 
     dst = ovsdb_txn_row_modify(txn, dst);
 
-    if (!list_is_empty(&dst->dst_refs)) {
+    if (!ovs_list_is_empty(&dst->dst_refs)) {
         /* Omit duplicates. */
-        weak = CONTAINER_OF(list_back(&dst->dst_refs),
+        weak = CONTAINER_OF(ovs_list_back(&dst->dst_refs),
                             struct ovsdb_weak_ref, dst_node);
         if (weak->src == src) {
             return;
@@ -460,11 +461,11 @@ add_weak_ref(struct ovsdb_txn *txn,
 
     weak = xmalloc(sizeof *weak);
     weak->src = src;
-    list_push_back(&dst->dst_refs, &weak->dst_node);
-    list_push_back(&src->src_refs, &weak->src_node);
+    ovs_list_push_back(&dst->dst_refs, &weak->dst_node);
+    ovs_list_push_back(&src->src_refs, &weak->src_node);
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
 {
     struct ovsdb_table *table;
@@ -570,7 +571,7 @@ assess_weak_refs(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
 {
     struct ovsdb_table *table = txn_row->table;
@@ -604,7 +605,7 @@ determine_changes(struct ovsdb_txn *txn, struct ovsdb_txn_row *txn_row)
     return NULL;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 check_max_rows(struct ovsdb_txn *txn)
 {
     struct ovsdb_txn_table *t;
@@ -616,7 +617,7 @@ check_max_rows(struct ovsdb_txn *txn)
         if (n_rows > max_rows) {
             return ovsdb_error("constraint violation",
                                "transaction causes \"%s\" table to contain "
-                               "%zu rows, greater than the schema-defined "
+                               "%"PRIuSIZE" rows, greater than the schema-defined "
                                "limit of %u row(s)",
                                t->table->schema->name, n_rows, max_rows);
         }
@@ -655,7 +656,7 @@ duplicate_index_row__(const struct ovsdb_column_set *index,
     ds_put_format(out, "%s row, with UUID "UUID_FMT", ",
                   title, UUID_ARGS(ovsdb_row_get_uuid(row)));
     if (!row->txn_row
-        || bitmap_scan(row->txn_row->changed, 0, n_columns) == n_columns) {
+        || bitmap_scan(row->txn_row->changed, 1, 0, n_columns) == n_columns) {
         ds_put_cstr(out, "existed in the database before this "
                     "transaction and was not modified by the transaction.");
     } else if (!row->txn_row->old) {
@@ -673,7 +674,7 @@ duplicate_index_row__(const struct ovsdb_column_set *index,
     }
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 duplicate_index_row(const struct ovsdb_column_set *index,
                     const struct ovsdb_row *a,
                     const struct ovsdb_row *b)
@@ -712,7 +713,7 @@ duplicate_index_row(const struct ovsdb_column_set *index,
     return error;
 }
 
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 check_index_uniqueness(struct ovsdb_txn *txn OVS_UNUSED,
                        struct ovsdb_txn_row *txn_row)
 {
@@ -748,8 +749,23 @@ check_index_uniqueness(struct ovsdb_txn *txn OVS_UNUSED,
     return NULL;
 }
 
-struct ovsdb_error *
-ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
+update_version(struct ovsdb_txn *txn OVS_UNUSED, struct ovsdb_txn_row *txn_row)
+{
+    struct ovsdb_table *table = txn_row->table;
+    size_t n_columns = shash_count(&table->schema->columns);
+
+    if (txn_row->old && txn_row->new
+        && !bitmap_is_all_zeros(txn_row->changed, n_columns)) {
+        bitmap_set1(txn_row->changed, OVSDB_COL_VERSION);
+        uuid_generate(ovsdb_row_get_version_rw(txn_row->new));
+    }
+
+    return NULL;
+}
+
+static struct ovsdb_error *
+ovsdb_txn_commit_(struct ovsdb_txn *txn, bool durable)
 {
     struct ovsdb_replica *replica;
     struct ovsdb_error *error;
@@ -760,7 +776,7 @@ ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
     if (error) {
         return OVSDB_WRAP_BUG("can't happen", error);
     }
-    if (list_is_empty(&txn->txn_tables)) {
+    if (ovs_list_is_empty(&txn->txn_tables)) {
         ovsdb_txn_abort(txn);
         return NULL;
     }
@@ -801,13 +817,19 @@ ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
         return error;
     }
 
+    /* Update _version for rows that changed.  */
+    error = for_each_txn_row(txn, update_version);
+    if (error) {
+        return OVSDB_WRAP_BUG("can't happen", error);
+    }
+
     /* Send the commit to each replica. */
     LIST_FOR_EACH (replica, node, &txn->db->replicas) {
         error = (replica->class->commit)(replica, txn, durable);
         if (error) {
             /* We don't support two-phase commit so only the first replica is
              * allowed to report an error. */
-            assert(&replica->node == txn->db->replicas.next);
+            ovs_assert(&replica->node == txn->db->replicas.next);
 
             ovsdb_txn_abort(txn);
             return error;
@@ -822,6 +844,15 @@ ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
     return NULL;
 }
 
+struct ovsdb_error *
+ovsdb_txn_commit(struct ovsdb_txn *txn, bool durable)
+{
+   struct ovsdb_error *err;
+
+   PERF(__func__, err = ovsdb_txn_commit_(txn, durable));
+   return err;
+}
+
 void
 ovsdb_txn_for_each_change(const struct ovsdb_txn *txn,
                           ovsdb_txn_row_cb_func *cb, void *aux)
@@ -854,7 +885,7 @@ ovsdb_txn_create_txn_table(struct ovsdb_txn *txn, struct ovsdb_table *table)
         for (i = 0; i < table->schema->n_indexes; i++) {
             hmap_init(&txn_table->txn_indexes[i]);
         }
-        list_push_back(&txn->txn_tables, &txn_table->node);
+        ovs_list_push_back(&txn->txn_tables, &txn_table->node);
     }
     return table->txn_table;
 }
@@ -864,7 +895,7 @@ ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
                      const struct ovsdb_row *old_, struct ovsdb_row *new)
 {
     const struct ovsdb_row *row = old_ ? old_ : new;
-    struct ovsdb_row *old = (struct ovsdb_row *) old_;
+    struct ovsdb_row *old = CONST_CAST(struct ovsdb_row *, old_);
     size_t n_columns = shash_count(&table->schema->columns);
     struct ovsdb_txn_table *txn_table;
     struct ovsdb_txn_row *txn_row;
@@ -895,10 +926,10 @@ ovsdb_txn_row_create(struct ovsdb_txn *txn, struct ovsdb_table *table,
 struct ovsdb_row *
 ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
 {
-    struct ovsdb_row *ro_row = (struct ovsdb_row *) ro_row_;
+    struct ovsdb_row *ro_row = CONST_CAST(struct ovsdb_row *, ro_row_);
 
     if (ro_row->txn_row) {
-        assert(ro_row == ro_row->txn_row->new);
+        ovs_assert(ro_row == ro_row->txn_row->new);
         return ro_row;
     } else {
         struct ovsdb_table *table = ro_row->table;
@@ -906,7 +937,6 @@ ovsdb_txn_row_modify(struct ovsdb_txn *txn, const struct ovsdb_row *ro_row_)
 
         rw_row = ovsdb_row_clone(ro_row);
         rw_row->n_refs = ro_row->n_refs;
-        uuid_generate(ovsdb_row_get_version_rw(rw_row));
         ovsdb_txn_row_create(txn, table, ro_row, rw_row);
         hmap_replace(&table->rows, &ro_row->hmap_node, &rw_row->hmap_node);
 
@@ -931,7 +961,7 @@ ovsdb_txn_row_insert(struct ovsdb_txn *txn, struct ovsdb_row *row)
 void
 ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
 {
-    struct ovsdb_row *row = (struct ovsdb_row *) row_;
+    struct ovsdb_row *row = CONST_CAST(struct ovsdb_row *, row_);
     struct ovsdb_table *table = row->table;
     struct ovsdb_txn_row *txn_row = row->txn_row;
 
@@ -940,7 +970,7 @@ ovsdb_txn_row_delete(struct ovsdb_txn *txn, const struct ovsdb_row *row_)
     if (!txn_row) {
         ovsdb_txn_row_create(txn, table, row, NULL);
     } else {
-        assert(txn_row->new == row);
+        ovs_assert(txn_row->new == row);
         if (txn_row->old) {
             txn_row->new = NULL;
         } else {
@@ -987,15 +1017,16 @@ ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
 {
     size_t i;
 
-    assert(hmap_is_empty(&txn_table->txn_rows));
+    ovs_assert(hmap_is_empty(&txn_table->txn_rows));
 
     for (i = 0; i < txn_table->table->schema->n_indexes; i++) {
         hmap_destroy(&txn_table->txn_indexes[i]);
     }
+    free(txn_table->txn_indexes);
 
     txn_table->table->txn_table = NULL;
     hmap_destroy(&txn_table->txn_rows);
-    list_remove(&txn_table->node);
+    ovs_list_remove(&txn_table->node);
     free(txn_table);
 }
 
@@ -1013,7 +1044,7 @@ ovsdb_txn_table_destroy(struct ovsdb_txn_table *txn_table)
  * (Even though 'cb' is not allowed to delete some txn_rows, it can still
  * delete any actual row by clearing a txn_row's 'new' member.)
  */
-static struct ovsdb_error * WARN_UNUSED_RESULT
+static struct ovsdb_error * OVS_WARN_UNUSED_RESULT
 for_each_txn_row(struct ovsdb_txn *txn,
                  struct ovsdb_error *(*cb)(struct ovsdb_txn *,
                                            struct ovsdb_txn_row *))