ovsdb-client: Use svec instead of sset for list of database.
[sliver-openvswitch.git] / ovsdb / ovsdb-client.c
index b1c660a..12f34d5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011, 2012 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.
@@ -39,7 +39,7 @@
 #include "ovsdb-data.h"
 #include "ovsdb-error.h"
 #include "sort.h"
-#include "sset.h"
+#include "svec.h"
 #include "stream.h"
 #include "stream-ssl.h"
 #include "table.h"
@@ -75,7 +75,7 @@ static const struct ovsdb_client_command all_commands[];
 static void usage(void) NO_RETURN;
 static void parse_options(int argc, char *argv[]);
 static struct jsonrpc *open_jsonrpc(const char *server);
-static void fetch_dbs(struct jsonrpc *, struct sset *dbs);
+static void fetch_dbs(struct jsonrpc *, struct svec *dbs);
 
 int
 main(int argc, char *argv[])
@@ -118,22 +118,22 @@ main(int argc, char *argv[])
     }
 
     if (command->need == NEED_DATABASE) {
-        struct sset dbs;
+        struct svec dbs;
 
-        sset_init(&dbs);
+        svec_init(&dbs);
         fetch_dbs(rpc, &dbs);
         if (argc - optind > command->min_args
-            && sset_contains(&dbs, argv[optind])) {
+            && svec_contains(&dbs, argv[optind])) {
             database = argv[optind++];
-        } else if (sset_count(&dbs) == 1) {
-            database = xstrdup(SSET_FIRST(&dbs));
-        } else if (sset_contains(&dbs, "Open_vSwitch")) {
+        } else if (dbs.n == 1) {
+            database = xstrdup(dbs.names[0]);
+        } else if (svec_contains(&dbs, "Open_vSwitch")) {
             database = "Open_vSwitch";
         } else {
             ovs_fatal(0, "no default database for `%s' command, please "
                       "specify a database name", command->name);
         }
-        sset_destroy(&dbs);
+        svec_destroy(&dbs);
     } else {
         database = NULL;
     }
@@ -275,6 +275,21 @@ usage(void)
     exit(EXIT_SUCCESS);
 }
 \f
+static void
+check_txn(int error, struct jsonrpc_msg **reply_)
+{
+    struct jsonrpc_msg *reply = *reply_;
+
+    if (error) {
+        ovs_fatal(error, "transaction failed");
+    }
+
+    if (reply->error) {
+        ovs_fatal(error, "transaction returned error: %s",
+                  json_to_string(reply->error, table_style.json_flags));
+    }
+}
+
 static struct json *
 parse_json(const char *s)
 {
@@ -291,11 +306,12 @@ open_jsonrpc(const char *server)
     struct stream *stream;
     int error;
 
-    error = stream_open_block(jsonrpc_stream_open(server, &stream), &stream);
+    error = stream_open_block(jsonrpc_stream_open(server, &stream,
+                              DSCP_DEFAULT), &stream);
     if (error == EAFNOSUPPORT) {
         struct pstream *pstream;
 
-        error = jsonrpc_pstream_open(server, &pstream);
+        error = jsonrpc_pstream_open(server, &pstream, DSCP_DEFAULT);
         if (error) {
             ovs_fatal(error, "failed to connect or listen to \"%s\"", server);
         }
@@ -342,16 +358,12 @@ fetch_schema(struct jsonrpc *rpc, const char *database)
 {
     struct jsonrpc_msg *request, *reply;
     struct ovsdb_schema *schema;
-    int error;
 
     request = jsonrpc_create_request("get_schema",
                                      json_array_create_1(
                                          json_string_create(database)),
                                      NULL);
-    error = jsonrpc_transact_block(rpc, request, &reply);
-    if (error) {
-        ovs_fatal(error, "transaction failed");
-    }
+    check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
     check_ovsdb_error(ovsdb_schema_from_json(reply->result, &schema));
     jsonrpc_msg_destroy(reply);
 
@@ -359,19 +371,15 @@ fetch_schema(struct jsonrpc *rpc, const char *database)
 }
 
 static void
-fetch_dbs(struct jsonrpc *rpc, struct sset *dbs)
+fetch_dbs(struct jsonrpc *rpc, struct svec *dbs)
 {
     struct jsonrpc_msg *request, *reply;
-    int error;
     size_t i;
 
     request = jsonrpc_create_request("list_dbs", json_array_create_empty(),
                                      NULL);
-    error = jsonrpc_transact_block(rpc, request, &reply);
-    if (error) {
-        ovs_fatal(error, "transaction failed");
-    }
 
+    check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
     if (reply->result->type != JSON_ARRAY) {
         ovs_fatal(0, "list_dbs response is not array");
     }
@@ -382,7 +390,7 @@ fetch_dbs(struct jsonrpc *rpc, struct sset *dbs)
         if (name->type != JSON_STRING) {
             ovs_fatal(0, "list_dbs response %zu is not string", i);
         }
-        sset_add(dbs, name->u.string);
+        svec_add(dbs, name->u.string);
     }
     jsonrpc_msg_destroy(reply);
 }
@@ -392,14 +400,15 @@ do_list_dbs(struct jsonrpc *rpc, const char *database OVS_UNUSED,
             int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     const char *db_name;
-    struct sset dbs;
+    struct svec dbs;
+    size_t i;
 
-    sset_init(&dbs);
+    svec_init(&dbs);
     fetch_dbs(rpc, &dbs);
-    SSET_FOR_EACH (db_name, &dbs) {
+    SVEC_FOR_EACH (i, db_name, &dbs) {
         puts(db_name);
     }
-    sset_destroy(&dbs);
+    svec_destroy(&dbs);
 }
 
 static void
@@ -485,19 +494,11 @@ do_transact(struct jsonrpc *rpc, const char *database OVS_UNUSED,
 {
     struct jsonrpc_msg *request, *reply;
     struct json *transaction;
-    int error;
 
     transaction = parse_json(argv[0]);
 
     request = jsonrpc_create_request("transact", transaction, NULL);
-    error = jsonrpc_transact_block(rpc, request, &reply);
-    if (error) {
-        ovs_fatal(error, "transaction failed");
-    }
-    if (reply->error) {
-        ovs_fatal(error, "transaction returned error: %s",
-                  json_to_string(reply->error, table_style.json_flags));
-    }
+    check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
     print_json(reply->result);
     putchar('\n');
     jsonrpc_msg_destroy(reply);
@@ -885,10 +886,15 @@ dump_table(const struct ovsdb_table_schema *ts, struct json_array *rows)
             struct cell *cell = table_add_cell(&t);
             cell->json = ovsdb_datum_to_json(&data[y][x], &columns[x]->type);
             cell->type = &columns[x]->type;
+            ovsdb_datum_destroy(&data[y][x], &columns[x]->type);
         }
+        free(data[y]);
     }
     table_print(&t, &table_style);
     table_destroy(&t);
+
+    free(data);
+    free(columns);
 }
 
 static void
@@ -898,7 +904,6 @@ do_dump(struct jsonrpc *rpc, const char *database,
     struct jsonrpc_msg *request, *reply;
     struct ovsdb_schema *schema;
     struct json *transaction;
-    int error;
 
     const struct shash_node **tables;
     size_t n_tables;
@@ -935,10 +940,7 @@ do_dump(struct jsonrpc *rpc, const char *database,
 
     /* Send request, get reply. */
     request = jsonrpc_create_request("transact", transaction, NULL);
-    error = jsonrpc_transact_block(rpc, request, &reply);
-    if (error) {
-        ovs_fatal(error, "transaction failed");
-    }
+    check_txn(jsonrpc_transact_block(rpc, request, &reply), &reply);
 
     /* Print database contents. */
     if (reply->result->type != JSON_ARRAY
@@ -961,6 +963,10 @@ do_dump(struct jsonrpc *rpc, const char *database,
 
         dump_table(ts, &rows->u.array);
     }
+
+    jsonrpc_msg_destroy(reply);
+    free(tables);
+    ovsdb_schema_destroy(schema);
 }
 
 static void