[ovs-dev] [PATCH RFC 50/52] ovsdb-client: Add new "query" command.

Ben Pfaff blp at ovn.org
Tue Sep 19 22:01:23 UTC 2017


Signed-off-by: Ben Pfaff <blp at ovn.org>
---
 NEWS                 |  2 +-
 ovsdb/ovsdb-client.c | 42 ++++++++++++++++++++++++++++++++++++------
 2 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/NEWS b/NEWS
index 9782818a1b0d..fc8db4965737 100644
--- a/NEWS
+++ b/NEWS
@@ -7,7 +7,7 @@ Post-v2.8.0
        "ovsdb-client convert".
      * ovsdb-server now always hosts a built-in database named _Server.  See
        ovsdb-server(5) for more details.
-     * ovsdb-client: New "get-schema-cksum" command.
+     * ovsdb-client: New "get-schema-cksum" and "query" commands.
      * ovsdb-client: New "backup" and "restore" commands.
    - ovs-vsctl and other commands that display data in tables now support a
      --max-column-width option to limit column width.
diff --git a/ovsdb/ovsdb-client.c b/ovsdb/ovsdb-client.c
index 056aa25c8983..c153a978385a 100644
--- a/ovsdb/ovsdb-client.c
+++ b/ovsdb/ovsdb-client.c
@@ -294,8 +294,11 @@ usage(void)
            "\n  list-columns [SERVER] [DATABASE] [TABLE]\n"
            "    list columns in TABLE (or all tables) in DATABASE on SERVER\n"
            "\n  transact [SERVER] TRANSACTION\n"
-           "    run TRANSACTION (a JSON array of operations) on SERVER\n"
+           "    run TRANSACTION (params for \"transact\" request) on SERVER\n"
            "    and print the results as JSON on stdout\n"
+           "\n  query [SERVER] TRANSACTION\n"
+           "    run TRANSACTION (params for \"transact\" request) on SERVER,\n"
+           "    as read-only, and print the results as JSON on stdout\n"
            "\n  monitor [SERVER] [DATABASE] TABLE [COLUMN,...]...\n"
            "    monitor contents of COLUMNs in TABLE in DATABASE on SERVER.\n"
            "    COLUMNs may include !initial, !insert, !delete, !modify\n"
@@ -584,13 +587,9 @@ send_db_change_aware(struct jsonrpc *rpc)
 }
 
 static void
-do_transact(struct jsonrpc *rpc, const char *database OVS_UNUSED,
-            int argc OVS_UNUSED, char *argv[])
+do_transact__(struct jsonrpc *rpc, struct json *transaction)
 {
     struct jsonrpc_msg *request, *reply;
-    struct json *transaction;
-
-    transaction = parse_json(argv[0]);
 
     if (db_change_aware == 1) {
         send_db_change_aware(rpc);
@@ -605,6 +604,36 @@ do_transact(struct jsonrpc *rpc, const char *database OVS_UNUSED,
     putchar('\n');
     jsonrpc_msg_destroy(reply);
 }
+
+static void
+do_transact(struct jsonrpc *rpc, const char *database OVS_UNUSED,
+            int argc OVS_UNUSED, char *argv[])
+{
+    do_transact__(rpc, parse_json(argv[0]));
+}
+
+static void
+do_query(struct jsonrpc *rpc, const char *database OVS_UNUSED,
+         int argc OVS_UNUSED, char *argv[])
+{
+    struct json *transaction = parse_json(argv[0]);
+
+    /* Ensure that 'transaction' is a 2-element array, whose second element is
+     * also an array.  The second element is the array of operations in the
+     * query. */
+    if (transaction->type != JSON_ARRAY
+        || transaction->u.array.n != 2
+        || transaction->u.array.elems[1]->type != JSON_ARRAY) {
+        ovs_fatal(0, "not a valid OVSDB query");
+    }
+
+    /* Append an "abort" operation to the query. */
+    struct json *abort_op = json_object_create();
+    json_object_put_string(abort_op, "op", "abort");
+    json_array_add(transaction->u.array.elems[1], abort_op);
+
+    do_transact__(rpc, transaction);
+}
 
 /* "monitor" command. */
 
@@ -1922,6 +1951,7 @@ static const struct ovsdb_client_command all_commands[] = {
     { "list-tables",        NEED_DATABASE, 0, 0,       do_list_tables },
     { "list-columns",       NEED_DATABASE, 0, 1,       do_list_columns },
     { "transact",           NEED_RPC,      1, 1,       do_transact },
+    { "query",              NEED_RPC,      1, 1,       do_query },
     { "monitor",            NEED_DATABASE, 1, INT_MAX, do_monitor },
     { "monitor-cond",       NEED_DATABASE, 2, 3,       do_monitor_cond },
     { "convert",            NEED_RPC,      1, 1,       do_convert },
-- 
2.10.2



More information about the dev mailing list