timeval: Make time_init() static and remove calls to it.
[sliver-openvswitch.git] / utilities / ovs-vsctl.c
index 0291fe8..fc1ba3d 100644 (file)
@@ -125,7 +125,6 @@ main(int argc, char *argv[])
 
     set_program_name(argv[0]);
     signal(SIGPIPE, SIG_IGN);
-    time_init();
     vlog_init();
     vlog_set_levels(VLM_ANY_MODULE, VLF_CONSOLE, VLL_WARN);
     vlog_set_levels(VLM_reconnect, VLF_ANY_FACILITY, VLL_WARN);
@@ -458,13 +457,13 @@ Switch commands:\n\
 \n\
 Database commands:\n\
   list TBL [REC]              list RECord (or all records) in TBL\n\
-  get TBL REC COL[:KEY]       print values of COLumns in RECORD in TBL\n\
+  get TBL REC COL[:KEY]       print values of COLumns in RECord in TBL\n\
   set TBL REC COL[:KEY]=VALUE set COLumn values in RECord in TBL\n\
   add TBL REC COL [KEY=]VALUE add (KEY=)VALUE to COLumn in RECord in TBL\n\
   remove TBL REC COL [KEY=]VALUE  remove (KEY=)VALUE from COLumn\n\
   clear TBL REC COL           clear values from COLumn in RECord in TBL\n\
   create TBL COL[:KEY]=VALUE  create and initialize new record\n\
-  destroy TBL REC             delete REC from TBL\n\
+  destroy TBL REC             delete RECord from TBL\n\
   wait-until TBL REC [COL[:KEY]=VALUE]  wait until condition is true\n\
 Potentially unsafe database commands require --force option.\n\
 \n\
@@ -1968,8 +1967,7 @@ get_table(const char *table_name)
 
 static const struct ovsdb_idl_row *
 get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
-              const struct vsctl_row_id *id, const char *record_id,
-              bool partial_match_ok)
+              const struct vsctl_row_id *id, const char *record_id)
 {
     const struct ovsdb_idl_row *referrer, *final;
 
@@ -1987,37 +1985,23 @@ get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
         }
     } else {
         const struct ovsdb_idl_row *row;
-        unsigned int best_score = 0;
-
-        /* It might make sense to relax this assertion. */
-        assert(id->name_column->type.key.type == OVSDB_TYPE_STRING);
 
         referrer = NULL;
         for (row = ovsdb_idl_first_row(ctx->idl, id->table);
-             row != NULL && best_score != UINT_MAX;
+             row != NULL;
              row = ovsdb_idl_next_row(row))
         {
-            struct ovsdb_datum name;
-
-            ovsdb_idl_txn_read(row, id->name_column, &name);
-            if (name.n == 1) {
-                unsigned int score;
-
-                score = (partial_match_ok
-                         ? score_partial_match(name.keys[0].string, record_id)
-                         : !strcmp(name.keys[0].string, record_id));
-                if (score > best_score) {
-                    referrer = row;
-                    best_score = score;
-                } else if (score == best_score) {
-                    referrer = NULL;
+            const struct ovsdb_datum *name;
+
+            name = ovsdb_idl_get(row, id->name_column,
+                                 OVSDB_TYPE_STRING, OVSDB_TYPE_VOID);
+            if (name->n == 1 && !strcmp(name->keys[0].string, record_id)) {
+                if (referrer) {
+                    vsctl_fatal("multiple rows in %s match \"%s\"",
+                                table->class->name, record_id);
                 }
+                referrer = row;
             }
-            ovsdb_datum_destroy(&name, &id->name_column->type);
-        }
-        if (best_score && !referrer) {
-            vsctl_fatal("multiple rows in %s match \"%s\"",
-                        table->class->name, record_id);
         }
     }
     if (!referrer) {
@@ -2026,17 +2010,14 @@ get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
 
     final = NULL;
     if (id->uuid_column) {
-        struct ovsdb_datum uuid;
-
-        assert(id->uuid_column->type.key.type == OVSDB_TYPE_UUID);
-        assert(id->uuid_column->type.value.type == OVSDB_TYPE_VOID);
+        const struct ovsdb_datum *uuid;
 
-        ovsdb_idl_txn_read(referrer, id->uuid_column, &uuid);
-        if (uuid.n == 1) {
+        uuid = ovsdb_idl_get(referrer, id->uuid_column,
+                             OVSDB_TYPE_UUID, OVSDB_TYPE_VOID);
+        if (uuid->n == 1) {
             final = ovsdb_idl_get_row_for_uuid(ctx->idl, table->class,
-                                               &uuid.keys[0].uuid);
+                                               &uuid->keys[0].uuid);
         }
-        ovsdb_datum_destroy(&uuid, &id->uuid_column->type);
     } else {
         final = referrer;
     }
@@ -2045,9 +2026,8 @@ get_row_by_id(struct vsctl_context *ctx, const struct vsctl_table_class *table,
 }
 
 static const struct ovsdb_idl_row *
-get_row__(struct vsctl_context *ctx,
-          const struct vsctl_table_class *table, const char *record_id,
-          bool partial_match_ok)
+get_row (struct vsctl_context *ctx,
+         const struct vsctl_table_class *table, const char *record_id)
 {
     const struct ovsdb_idl_row *row;
     struct uuid uuid;
@@ -2058,8 +2038,7 @@ get_row__(struct vsctl_context *ctx,
         int i;
 
         for (i = 0; i < ARRAY_SIZE(table->row_ids); i++) {
-            row = get_row_by_id(ctx, table, &table->row_ids[i], record_id,
-                                partial_match_ok);
+            row = get_row_by_id(ctx, table, &table->row_ids[i], record_id);
             if (row) {
                 break;
             }
@@ -2068,13 +2047,6 @@ get_row__(struct vsctl_context *ctx,
     return row;
 }
 
-static const struct ovsdb_idl_row *
-get_row(struct vsctl_context *ctx,
-        const struct vsctl_table_class *table, const char *record_id)
-{
-    return get_row__(ctx, table, record_id, true);
-}
-
 static const struct ovsdb_idl_row *
 must_get_row(struct vsctl_context *ctx,
              const struct vsctl_table_class *table, const char *record_id)
@@ -2297,7 +2269,7 @@ cmd_get(struct vsctl_context *ctx)
     row = must_get_row(ctx, table, record_id);
     for (i = 3; i < ctx->argc; i++) {
         const struct ovsdb_idl_column *column;
-        struct ovsdb_datum datum;
+        const struct ovsdb_datum *datum;
         char *key_string;
 
         /* Special case for obtaining the UUID of a row.  We can't just do this
@@ -2313,7 +2285,7 @@ cmd_get(struct vsctl_context *ctx)
                                             &column, &key_string,
                                             NULL, NULL, 0, NULL));
 
-        ovsdb_idl_txn_read(row, column, &datum);
+        datum = ovsdb_idl_read(row, column);
         if (key_string) {
             union ovsdb_atom key;
             unsigned int idx;
@@ -2327,7 +2299,7 @@ cmd_get(struct vsctl_context *ctx)
                                                 &column->type.key,
                                                 key_string, ctx->symtab));
 
-            idx = ovsdb_datum_find_key(&datum, &key,
+            idx = ovsdb_datum_find_key(datum, &key,
                                        column->type.key.type);
             if (idx == UINT_MAX) {
                 if (!if_exists) {
@@ -2336,15 +2308,14 @@ cmd_get(struct vsctl_context *ctx)
                                 column->name);
                 }
             } else {
-                ovsdb_atom_to_string(&datum.values[idx],
+                ovsdb_atom_to_string(&datum->values[idx],
                                      column->type.value.type, out);
             }
             ovsdb_atom_destroy(&key, column->type.key.type);
         } else {
-            ovsdb_datum_to_string(&datum, &column->type, out);
+            ovsdb_datum_to_string(datum, &column->type, out);
         }
         ds_put_char(out, '\n');
-        ovsdb_datum_destroy(&datum, &column->type);
 
         free(key_string);
     }
@@ -2360,15 +2331,13 @@ list_record(const struct vsctl_table_class *table,
                   UUID_ARGS(&row->uuid));
     for (i = 0; i < table->class->n_columns; i++) {
         const struct ovsdb_idl_column *column = &table->class->columns[i];
-        struct ovsdb_datum datum;
+        const struct ovsdb_datum *datum;
 
-        ovsdb_idl_txn_read(row, column, &datum);
+        datum = ovsdb_idl_read(row, column);
 
         ds_put_format(out, "%-20s: ", column->name);
-        ovsdb_datum_to_string(&datum, &column->type, out);
+        ovsdb_datum_to_string(datum, &column->type, out);
         ds_put_char(out, '\n');
-
-        ovsdb_datum_destroy(&datum, &column->type);
     }
 }
 
@@ -2421,7 +2390,7 @@ set_column(const struct vsctl_table_class *table,
 
     if (key_string) {
         union ovsdb_atom key, value;
-        struct ovsdb_datum old, new;
+        struct ovsdb_datum datum;
 
         if (column->type.value.type == OVSDB_TYPE_VOID) {
             vsctl_fatal("cannot specify key to set for non-map column %s",
@@ -2433,17 +2402,15 @@ set_column(const struct vsctl_table_class *table,
         die_if_error(ovsdb_atom_from_string(&value, &column->type.value,
                                             value_string, symtab));
 
-        ovsdb_datum_init_empty(&new);
-        ovsdb_datum_add_unsafe(&new, &key, &value, &column->type);
+        ovsdb_datum_init_empty(&datum);
+        ovsdb_datum_add_unsafe(&datum, &key, &value, &column->type);
 
         ovsdb_atom_destroy(&key, column->type.key.type);
         ovsdb_atom_destroy(&value, column->type.value.type);
 
-        ovsdb_idl_txn_read(row, column, &old);
-        ovsdb_datum_union(&old, &new, &column->type, true);
-        ovsdb_idl_txn_write(row, column, &old);
-
-        ovsdb_datum_destroy(&new, &column->type);
+        ovsdb_datum_union(&datum, ovsdb_idl_read(row, column),
+                          &column->type, false);
+        ovsdb_idl_txn_write(row, column, &datum);
     } else {
         struct ovsdb_datum datum;
 
@@ -2490,7 +2457,7 @@ cmd_add(struct vsctl_context *ctx)
     die_if_error(get_column(table, column_name, &column));
 
     type = &column->type;
-    ovsdb_idl_txn_read(row, column, &old);
+    ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
     for (i = 4; i < ctx->argc; i++) {
         struct ovsdb_type add_type;
         struct ovsdb_datum add;
@@ -2531,7 +2498,7 @@ cmd_remove(struct vsctl_context *ctx)
     die_if_error(get_column(table, column_name, &column));
 
     type = &column->type;
-    ovsdb_idl_txn_read(row, column, &old);
+    ovsdb_datum_clone(&old, ovsdb_idl_read(row, column), &column->type);
     for (i = 4; i < ctx->argc; i++) {
         struct ovsdb_type rm_type;
         struct ovsdb_datum rm;
@@ -2681,12 +2648,12 @@ is_condition_satified(const struct vsctl_table_class *table,
     };
 
     const struct ovsdb_idl_column *column;
+    const struct ovsdb_datum *have_datum;
     char *key_string, *value_string;
-    struct ovsdb_datum have_datum;
     const char *operator;
     unsigned int idx;
     char *error;
-    int cmp;
+    int cmp = 0;
 
     error = parse_column_key_value(arg, table, &column, &key_string,
                                    &operator, operators, ARRAY_SIZE(operators),
@@ -2696,7 +2663,7 @@ is_condition_satified(const struct vsctl_table_class *table,
         vsctl_fatal("%s: missing value", arg);
     }
 
-    ovsdb_idl_txn_read(row, column, &have_datum);
+    have_datum = ovsdb_idl_read(row, column);
     if (key_string) {
         union ovsdb_atom want_key, want_value;
 
@@ -2710,10 +2677,10 @@ is_condition_satified(const struct vsctl_table_class *table,
         die_if_error(ovsdb_atom_from_string(&want_value, &column->type.value,
                                             value_string, symtab));
 
-        idx = ovsdb_datum_find_key(&have_datum,
+        idx = ovsdb_datum_find_key(have_datum,
                                    &want_key, column->type.key.type);
         if (idx != UINT_MAX) {
-            cmp = ovsdb_atom_compare_3way(&have_datum.values[idx],
+            cmp = ovsdb_atom_compare_3way(&have_datum->values[idx],
                                           &want_value,
                                           column->type.value.type);
         }
@@ -2726,11 +2693,10 @@ is_condition_satified(const struct vsctl_table_class *table,
         die_if_error(ovsdb_datum_from_string(&want_datum, &column->type,
                                              value_string, symtab));
         idx = 0;
-        cmp = ovsdb_datum_compare_3way(&have_datum, &want_datum,
+        cmp = ovsdb_datum_compare_3way(have_datum, &want_datum,
                                        &column->type);
         ovsdb_datum_destroy(&want_datum, &column->type);
     }
-    ovsdb_datum_destroy(&have_datum, &column->type);
 
     free(key_string);
     free(value_string);
@@ -2756,7 +2722,7 @@ cmd_wait_until(struct vsctl_context *ctx)
 
     table = get_table(table_name);
 
-    row = get_row__(ctx, table, record_id, false);
+    row = get_row(ctx, table, record_id);
     if (!row) {
         ctx->try_again = true;
         return;
@@ -2820,7 +2786,7 @@ do_vsctl(const char *args, struct vsctl_command *commands, size_t n_commands,
     const char *unused;
     struct vsctl_command *c;
     int64_t next_cfg = 0;
-    char *error;
+    char *error = NULL;
 
     txn = the_idl_txn = ovsdb_idl_txn_create(idl);
     if (dry_run) {