Setting tag sliver-openvswitch-2.2.90-1
[sliver-openvswitch.git] / ovsdb / table.c
index 2ea73bf..e7545ad 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2009, 2010, 2011 Nicira Networks
+/* Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
 
 #include "table.h"
 
-#include <assert.h>
 #include <limits.h>
 
 #include "json.h"
@@ -30,7 +29,7 @@
 static void
 add_column(struct ovsdb_table_schema *ts, struct ovsdb_column *column)
 {
-    assert(!shash_find(&ts->columns, column->name));
+    ovs_assert(!shash_find(&ts->columns, column->name));
     column->index = shash_count(&ts->columns);
     shash_add(&ts->columns, column->name, column);
 }
@@ -51,11 +50,14 @@ ovsdb_table_schema_create(const char *name, bool mutable,
 
     uuid = ovsdb_column_create("_uuid", false, true, &ovsdb_type_uuid);
     add_column(ts, uuid);
-    assert(uuid->index == OVSDB_COL_UUID);
+    ovs_assert(uuid->index == OVSDB_COL_UUID);
 
     version = ovsdb_column_create("_version", false, false, &ovsdb_type_uuid);
     add_column(ts, version);
-    assert(version->index == OVSDB_COL_VERSION);
+    ovs_assert(version->index == OVSDB_COL_VERSION);
+
+    ts->n_indexes = 0;
+    ts->indexes = NULL;
 
     return ts;
 }
@@ -65,6 +67,7 @@ ovsdb_table_schema_clone(const struct ovsdb_table_schema *old)
 {
     struct ovsdb_table_schema *new;
     struct shash_node *node;
+    size_t i;
 
     new = ovsdb_table_schema_create(old->name, old->mutable,
                                     old->max_rows, old->is_root);
@@ -78,6 +81,24 @@ ovsdb_table_schema_clone(const struct ovsdb_table_schema *old)
 
         add_column(new, ovsdb_column_clone(column));
     }
+
+    new->n_indexes = old->n_indexes;
+    new->indexes = xmalloc(new->n_indexes * sizeof *new->indexes);
+    for (i = 0; i < new->n_indexes; i++) {
+        const struct ovsdb_column_set *old_index = &old->indexes[i];
+        struct ovsdb_column_set *new_index = &new->indexes[i];
+        size_t j;
+
+        ovsdb_column_set_init(new_index);
+        for (j = 0; j < old_index->n_columns; j++) {
+            const struct ovsdb_column *old_column = old_index->columns[j];
+            const struct ovsdb_column *new_column;
+
+            new_column = ovsdb_table_schema_get_column(new, old_column->name);
+            ovsdb_column_set_add(new_index, new_column);
+        }
+    }
+
     return new;
 }
 
@@ -85,6 +106,12 @@ void
 ovsdb_table_schema_destroy(struct ovsdb_table_schema *ts)
 {
     struct shash_node *node;
+    size_t i;
+
+    for (i = 0; i < ts->n_indexes; i++) {
+        ovsdb_column_set_destroy(&ts->indexes[i]);
+    }
+    free(ts->indexes);
 
     SHASH_FOR_EACH (node, &ts->columns) {
         ovsdb_column_destroy(node->data);
@@ -99,7 +126,7 @@ ovsdb_table_schema_from_json(const struct json *json, const char *name,
                              struct ovsdb_table_schema **tsp)
 {
     struct ovsdb_table_schema *ts;
-    const struct json *columns, *mutable, *max_rows, *is_root;
+    const struct json *columns, *mutable, *max_rows, *is_root, *indexes;
     struct shash_node *node;
     struct ovsdb_parser parser;
     struct ovsdb_error *error;
@@ -114,6 +141,7 @@ ovsdb_table_schema_from_json(const struct json *json, const char *name,
     max_rows = ovsdb_parser_member(&parser, "maxRows",
                                    OP_INTEGER | OP_OPTIONAL);
     is_root = ovsdb_parser_member(&parser, "isRoot", OP_BOOLEAN | OP_OPTIONAL);
+    indexes = ovsdb_parser_member(&parser, "indexes", OP_ARRAY | OP_OPTIONAL);
     error = ovsdb_parser_finish(&parser);
     if (error) {
         return error;
@@ -150,14 +178,51 @@ ovsdb_table_schema_from_json(const struct json *json, const char *name,
             error = ovsdb_column_from_json(node->data, node->name, &column);
         }
         if (error) {
-            ovsdb_table_schema_destroy(ts);
-            return error;
+            goto error;
         }
 
         add_column(ts, column);
     }
+
+    if (indexes) {
+        size_t i;
+
+        ts->indexes = xmalloc(indexes->u.array.n * sizeof *ts->indexes);
+        for (i = 0; i < indexes->u.array.n; i++) {
+            struct ovsdb_column_set *index = &ts->indexes[i];
+            size_t j;
+
+            error = ovsdb_column_set_from_json(indexes->u.array.elems[i],
+                                               ts, index);
+            if (error) {
+                goto error;
+            }
+            if (index->n_columns == 0) {
+                error = ovsdb_syntax_error(json, NULL, "index must have "
+                                           "at least one column");
+                goto error;
+            }
+            ts->n_indexes++;
+
+            for (j = 0; j < index->n_columns; j++) {
+                const struct ovsdb_column *column = index->columns[j];
+
+                if (!column->persistent) {
+                    error = ovsdb_syntax_error(json, NULL, "ephemeral columns "
+                                               "(such as %s) may not be "
+                                               "indexed", column->name);
+                    goto error;
+                }
+            }
+        }
+    }
+
     *tsp = ts;
     return NULL;
+
+error:
+    ovsdb_table_schema_destroy(ts);
+    return error;
 }
 
 /* Returns table schema 'ts' serialized into JSON.
@@ -165,7 +230,7 @@ ovsdb_table_schema_from_json(const struct json *json, const char *name,
  * The "isRoot" member is included in the JSON only if its value would differ
  * from 'default_is_root'.  Ordinarily 'default_is_root' should be false,
  * because ordinarily a table would be not be part of the root set if its
- * "isRoot" member is omitted.  However, garbage collection was not orginally
+ * "isRoot" member is omitted.  However, garbage collection was not originally
  * included in OVSDB, so in older schemas that do not include any "isRoot"
  * members, every table is implicitly part of the root set.  To serialize such
  * a schema in a way that can be read by older OVSDB tools, specify
@@ -199,6 +264,18 @@ ovsdb_table_schema_to_json(const struct ovsdb_table_schema *ts,
         json_object_put(json, "maxRows", json_integer_create(ts->max_rows));
     }
 
+    if (ts->n_indexes) {
+        struct json **indexes;
+        size_t i;
+
+        indexes = xmalloc(ts->n_indexes * sizeof *indexes);
+        for (i = 0; i < ts->n_indexes; i++) {
+            indexes[i] = ovsdb_column_set_to_json(&ts->indexes[i]);
+        }
+        json_object_put(json, "indexes",
+                        json_array_create(indexes, ts->n_indexes));
+    }
+
     return json;
 }
 
@@ -213,10 +290,15 @@ struct ovsdb_table *
 ovsdb_table_create(struct ovsdb_table_schema *ts)
 {
     struct ovsdb_table *table;
+    size_t i;
 
     table = xmalloc(sizeof *table);
     table->schema = ts;
     table->txn_table = NULL;
+    table->indexes = xmalloc(ts->n_indexes * sizeof *table->indexes);
+    for (i = 0; i < ts->n_indexes; i++) {
+        hmap_init(&table->indexes[i]);
+    }
     hmap_init(&table->rows);
 
     return table;
@@ -227,12 +309,18 @@ ovsdb_table_destroy(struct ovsdb_table *table)
 {
     if (table) {
         struct ovsdb_row *row, *next;
+        size_t i;
 
         HMAP_FOR_EACH_SAFE (row, next, hmap_node, &table->rows) {
             ovsdb_row_destroy(row);
         }
         hmap_destroy(&table->rows);
 
+        for (i = 0; i < table->schema->n_indexes; i++) {
+            hmap_destroy(&table->indexes[i]);
+        }
+        free(table->indexes);
+
         ovsdb_table_schema_destroy(table->schema);
         free(table);
     }
@@ -251,17 +339,3 @@ ovsdb_table_get_row(const struct ovsdb_table *table, const struct uuid *uuid)
 
     return NULL;
 }
-
-/* This is probably not the function you want.  Use ovsdb_txn_row_modify()
- * instead. */
-bool
-ovsdb_table_put_row(struct ovsdb_table *table, struct ovsdb_row *row)
-{
-    const struct uuid *uuid = ovsdb_row_get_uuid(row);
-    if (!ovsdb_table_get_row(table, uuid)) {
-        hmap_insert(&table->rows, &row->hmap_node, uuid_hash(uuid));
-        return true;
-    } else {
-        return false;
-    }
-}