From fbf925e45da7100e5018bd3ad49532e407666f77 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 8 Feb 2010 16:03:21 -0800 Subject: [PATCH] ovsdb: Get rid of "declare" operation. It's more elegant, and just as easy to implement, if we allow a "named-uuid" to be a forward reference to a "uuid-name" in a later "insert" operation. --- lib/ovsdb-data.c | 46 ++++++++++++++++------------- lib/ovsdb-data.h | 11 ++++--- lib/ovsdb-idl.c | 10 ------- ovsdb/SPECS | 64 ++++++++-------------------------------- ovsdb/condition.c | 4 +-- ovsdb/condition.h | 4 +-- ovsdb/execution.c | 53 +++++---------------------------- ovsdb/mutation.c | 4 +-- ovsdb/mutation.h | 4 +-- ovsdb/row.c | 2 +- ovsdb/row.h | 2 +- tests/ovsdb-execution.at | 8 ++--- tests/ovsdb-idl.at | 8 ++--- 13 files changed, 66 insertions(+), 154 deletions(-) diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c index 76d046f1..47d1ea74 100644 --- a/lib/ovsdb-data.c +++ b/lib/ovsdb-data.c @@ -229,14 +229,9 @@ parse_json_pair(const struct json *json, return NULL; } -static struct ovsdb_error * -ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, - const struct ovsdb_symbol_table *symtab) - WARN_UNUSED_RESULT; - -static struct ovsdb_error * +static struct ovsdb_error * WARN_UNUSED_RESULT ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, - const struct ovsdb_symbol_table *symtab) + struct ovsdb_symbol_table *symtab) { struct ovsdb_error *error0; const struct json *value; @@ -254,18 +249,10 @@ ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, error1 = unwrap_json(json, "named-uuid", JSON_STRING, &value); if (!error1) { const char *name = json_string(value); - const struct ovsdb_symbol *symbol; ovsdb_error_destroy(error0); - - symbol = ovsdb_symbol_table_get(symtab, name); - if (symbol) { - *uuid = symbol->uuid; - return NULL; - } else { - return ovsdb_syntax_error(json, NULL, - "unknown named-uuid \"%s\"", name); - } + *uuid = ovsdb_symbol_table_insert(symtab, name)->uuid; + return NULL; } ovsdb_error_destroy(error1); } @@ -276,7 +263,7 @@ ovsdb_atom_parse_uuid(struct uuid *uuid, const struct json *json, static struct ovsdb_error * WARN_UNUSED_RESULT ovsdb_atom_from_json__(union ovsdb_atom *atom, enum ovsdb_atomic_type type, const struct json *json, - const struct ovsdb_symbol_table *symtab) + struct ovsdb_symbol_table *symtab) { switch (type) { case OVSDB_TYPE_VOID: @@ -332,7 +319,7 @@ struct ovsdb_error * ovsdb_atom_from_json(union ovsdb_atom *atom, const struct ovsdb_base_type *base, const struct json *json, - const struct ovsdb_symbol_table *symtab) + struct ovsdb_symbol_table *symtab) { struct ovsdb_error *error; @@ -906,7 +893,7 @@ struct ovsdb_error * ovsdb_datum_from_json(struct ovsdb_datum *datum, const struct ovsdb_type *type, const struct json *json, - const struct ovsdb_symbol_table *symtab) + struct ovsdb_symbol_table *symtab) { struct ovsdb_error *error; @@ -1527,7 +1514,7 @@ ovsdb_symbol_table_get(const struct ovsdb_symbol_table *symtab, return shash_find_data(&symtab->sh, name); } -void +struct ovsdb_symbol * ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name, const struct uuid *uuid, bool used) { @@ -1538,6 +1525,23 @@ ovsdb_symbol_table_put(struct ovsdb_symbol_table *symtab, const char *name, symbol->uuid = *uuid; symbol->used = used; shash_add(&symtab->sh, name, symbol); + return symbol; +} + +struct ovsdb_symbol * +ovsdb_symbol_table_insert(struct ovsdb_symbol_table *symtab, + const char *name) +{ + struct ovsdb_symbol *symbol; + + symbol = ovsdb_symbol_table_get(symtab, name); + if (!symbol) { + struct uuid uuid; + + uuid_generate(&uuid); + symbol = ovsdb_symbol_table_put(symtab, name, &uuid, false); + } + return symbol; } /* Extracts a token from the beginning of 's' and returns a pointer just after diff --git a/lib/ovsdb-data.h b/lib/ovsdb-data.h index c8e146a7..063536b2 100644 --- a/lib/ovsdb-data.h +++ b/lib/ovsdb-data.h @@ -69,7 +69,7 @@ static inline bool ovsdb_atom_equals(const union ovsdb_atom *a, struct ovsdb_error *ovsdb_atom_from_json(union ovsdb_atom *, const struct ovsdb_base_type *, const struct json *, - const struct ovsdb_symbol_table *) + struct ovsdb_symbol_table *) WARN_UNUSED_RESULT; struct json *ovsdb_atom_to_json(const union ovsdb_atom *, enum ovsdb_atomic_type); @@ -131,7 +131,7 @@ struct ovsdb_error *ovsdb_datum_check_constraints( struct ovsdb_error *ovsdb_datum_from_json(struct ovsdb_datum *, const struct ovsdb_type *, const struct json *, - const struct ovsdb_symbol_table *) + struct ovsdb_symbol_table *) WARN_UNUSED_RESULT; struct json *ovsdb_datum_to_json(const struct ovsdb_datum *, const struct ovsdb_type *); @@ -206,8 +206,11 @@ struct ovsdb_symbol_table *ovsdb_symbol_table_create(void); void ovsdb_symbol_table_destroy(struct ovsdb_symbol_table *); struct ovsdb_symbol *ovsdb_symbol_table_get(const struct ovsdb_symbol_table *, const char *name); -void ovsdb_symbol_table_put(struct ovsdb_symbol_table *, const char *name, - const struct uuid *, bool used); +struct ovsdb_symbol *ovsdb_symbol_table_put(struct ovsdb_symbol_table *, + const char *name, + const struct uuid *, bool used); +struct ovsdb_symbol *ovsdb_symbol_table_insert(struct ovsdb_symbol_table *, + const char *name); /* Tokenization * diff --git a/lib/ovsdb-idl.c b/lib/ovsdb-idl.c index 53be4ec0..96b31aab 100644 --- a/lib/ovsdb-idl.c +++ b/lib/ovsdb-idl.c @@ -1103,16 +1103,6 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn) &column->type)); } } - if (row->new && !row->old) { - struct json *op; - - op = json_object_create(); - json_array_add(operations, op); - json_object_put_string(op, "op", "declare"); - json_object_put(op, "uuid-name", - json_string_create_nocopy( - uuid_name_from_uuid(&row->uuid))); - } } /* Add updates. */ diff --git a/ovsdb/SPECS b/ovsdb/SPECS index c1e3eca6..1d55b1b5 100644 --- a/ovsdb/SPECS +++ b/ovsdb/SPECS @@ -582,13 +582,13 @@ Notation for the Wire Protocol A 2-element JSON array that represents the UUID of a row inserted - in a previous "insert" operation within the same transaction. The - first element of the array must be the string "named-uuid" and the - second element must be the string specified on this "insert" - operation's "uuid-name" or on a preceding "insert" within the same - transaction. For example, if this or a previous "insert" - operation specified a "uuid-name" of "myrow", the following - represents the UUID created by that operation: + in an "insert" operation within the same transaction. The first + element of the array must be the string "named-uuid" and the + second element should be the string specified as the "uuid-name" + for an "insert" operation within the same transaction. For + example, if an "insert" operation within this transaction + specifies a "uuid-name" of "myrow", the following + represents the UUID created by that operation: ["named-uuid", "myrow"] @@ -773,21 +773,11 @@ Semantics: - "uuid": 00000000-0000-0000-0000-000000000000 - If "uuid-name" is not supplied, the new row receives a new, - randomly generated UUID. + The new row receives a new, randomly generated UUID. - If "uuid-name" is supplied, then it is an error if has - previously appeared as the "uuid-name" in an "insert" operation. - - If "uuid-name" is supplied and its previously appeared as the - "uuid-name" in a "declare" operation, then the new row receives - the UUID associated with that "uuid-name". - - If "uuid-name" is supplied and its has not previously - appeared as the "uuid-name" in a "declare" operation, then the new - row also receives a new, randomly generated UUID. This UUID is - also made available under that name to this operation and later - operations within the same transaction. + If "uuid-name" is supplied, then it is an error if is not + unique among the "uuid-name"s supplied on all the "insert" + operations within this transaction. The UUID for the new row is returned as the "uuid" member of the result. @@ -796,7 +786,7 @@ Errors: "error": "duplicate uuid-name" - The same "uuid-name" appeared on an earlier "insert" operation + The same "uuid-name" appears on another "insert" operation within this transaction. "error": "constraint violation" @@ -1046,36 +1036,6 @@ Errors: This operation always fails with this error. -declare -....... - -Request object members: - - "op": "declare" required - "uuid-name": required - -Result object members: - - "uuid": - -Semantics: - - Predeclares a UUID named that may be referenced in later - operations as ["named-uuid", ] or (at most once) in an - "insert" operation as "uuid-name". - - It is an error if has appeared as the "uuid-name" in a prior - "insert" or "declare" operation within this transaction. - - The generated UUID is returned as the "uuid" member of the result. - -Errors: - - "error": "duplicate uuid-name" - - The same "uuid-name" appeared on an earlier "insert" or - "declare" operation within this transaction. - comment ....... diff --git a/ovsdb/condition.c b/ovsdb/condition.c index abd79364..59f742c9 100644 --- a/ovsdb/condition.c +++ b/ovsdb/condition.c @@ -55,7 +55,7 @@ ovsdb_function_to_string(enum ovsdb_function function) static WARN_UNUSED_RESULT struct ovsdb_error * ovsdb_clause_from_json(const struct ovsdb_table_schema *ts, const struct json *json, - const struct ovsdb_symbol_table *symtab, + struct ovsdb_symbol_table *symtab, struct ovsdb_clause *clause) { const struct json_array *array; @@ -167,7 +167,7 @@ compare_clauses_3way(const void *a_, const void *b_) struct ovsdb_error * ovsdb_condition_from_json(const struct ovsdb_table_schema *ts, const struct json *json, - const struct ovsdb_symbol_table *symtab, + struct ovsdb_symbol_table *symtab, struct ovsdb_condition *cnd) { const struct json_array *array = json_array(json); diff --git a/ovsdb/condition.h b/ovsdb/condition.h index 8c422b95..4716150e 100644 --- a/ovsdb/condition.h +++ b/ovsdb/condition.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2009 Nicira Networks +/* Copyright (c) 2009, 2010 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,7 +62,7 @@ struct ovsdb_condition { struct ovsdb_error *ovsdb_condition_from_json( const struct ovsdb_table_schema *, - const struct json *, const struct ovsdb_symbol_table *, + const struct json *, struct ovsdb_symbol_table *, struct ovsdb_condition *) WARN_UNUSED_RESULT; struct json *ovsdb_condition_to_json(const struct ovsdb_condition *); void ovsdb_condition_destroy(struct ovsdb_condition *); diff --git a/ovsdb/execution.c b/ovsdb/execution.c index 7cf45f69..0465f038 100644 --- a/ovsdb/execution.c +++ b/ovsdb/execution.c @@ -56,7 +56,6 @@ static ovsdb_operation_executor ovsdb_execute_delete; static ovsdb_operation_executor ovsdb_execute_wait; static ovsdb_operation_executor ovsdb_execute_commit; static ovsdb_operation_executor ovsdb_execute_abort; -static ovsdb_operation_executor ovsdb_execute_declare; static ovsdb_operation_executor ovsdb_execute_comment; static ovsdb_operation_executor * @@ -76,7 +75,6 @@ lookup_executor(const char *name) { "wait", ovsdb_execute_wait }, { "commit", ovsdb_execute_commit }, { "abort", ovsdb_execute_abort }, - { "declare", ovsdb_execute_declare }, { "comment", ovsdb_execute_comment }, }; @@ -240,7 +238,7 @@ parse_table(struct ovsdb_execution *x, static WARN_UNUSED_RESULT struct ovsdb_error * parse_row(struct ovsdb_parser *parser, const char *member, const struct ovsdb_table *table, - const struct ovsdb_symbol_table *symtab, + struct ovsdb_symbol_table *symtab, struct ovsdb_row **rowp, struct ovsdb_column_set *columns) { struct ovsdb_error *error; @@ -285,20 +283,14 @@ ovsdb_execute_insert(struct ovsdb_execution *x, struct ovsdb_parser *parser, if (uuid_name) { struct ovsdb_symbol *symbol; - symbol = ovsdb_symbol_table_get(x->symtab, json_string(uuid_name)); - if (symbol) { - if (symbol->used) { - return ovsdb_syntax_error(uuid_name, "duplicate uuid-name", - "This \"uuid-name\" appeared on an " - "earlier \"insert\" operation."); - } - row_uuid = symbol->uuid; - symbol->used = true; - } else { - uuid_generate(&row_uuid); - ovsdb_symbol_table_put(x->symtab, json_string(uuid_name), - &row_uuid, true); + symbol = ovsdb_symbol_table_insert(x->symtab, json_string(uuid_name)); + if (symbol->used) { + return ovsdb_syntax_error(uuid_name, "duplicate uuid-name", + "This \"uuid-name\" appeared on an " + "earlier \"insert\" operation."); } + row_uuid = symbol->uuid; + symbol->used = true; } else { uuid_generate(&row_uuid); } @@ -689,35 +681,6 @@ ovsdb_execute_wait(struct ovsdb_execution *x, struct ovsdb_parser *parser, return error; } -static struct ovsdb_error * -ovsdb_execute_declare(struct ovsdb_execution *x, struct ovsdb_parser *parser, - struct json *result) -{ - const struct json *uuid_name; - struct uuid uuid; - - uuid_name = ovsdb_parser_member(parser, "uuid-name", OP_ID); - if (!uuid_name) { - return NULL; - } - - if (ovsdb_symbol_table_get(x->symtab, json_string(uuid_name))) { - return ovsdb_syntax_error(uuid_name, "duplicate uuid-name", - "This \"uuid-name\" appeared on an " - "earlier \"declare\" or \"insert\" " - "operation."); - } - - uuid_generate(&uuid); - ovsdb_symbol_table_put(x->symtab, json_string(uuid_name), &uuid, false); - json_object_put(result, "uuid", - json_array_create_2( - json_string_create("uuid"), - json_string_create_nocopy( - xasprintf(UUID_FMT, UUID_ARGS(&uuid))))); - return NULL; -} - static struct ovsdb_error * ovsdb_execute_comment(struct ovsdb_execution *x, struct ovsdb_parser *parser, struct json *result UNUSED) diff --git a/ovsdb/mutation.c b/ovsdb/mutation.c index 1b422806..bd6986da 100644 --- a/ovsdb/mutation.c +++ b/ovsdb/mutation.c @@ -72,7 +72,7 @@ type_mismatch(const struct ovsdb_mutation *m, const struct json *json) static WARN_UNUSED_RESULT struct ovsdb_error * ovsdb_mutation_from_json(const struct ovsdb_table_schema *ts, const struct json *json, - const struct ovsdb_symbol_table *symtab, + struct ovsdb_symbol_table *symtab, struct ovsdb_mutation *m) { const struct json_array *array; @@ -164,7 +164,7 @@ ovsdb_mutation_free(struct ovsdb_mutation *m) struct ovsdb_error * ovsdb_mutation_set_from_json(const struct ovsdb_table_schema *ts, const struct json *json, - const struct ovsdb_symbol_table *symtab, + struct ovsdb_symbol_table *symtab, struct ovsdb_mutation_set *set) { const struct json_array *array = json_array(json); diff --git a/ovsdb/mutation.h b/ovsdb/mutation.h index d466e281..57fd965a 100644 --- a/ovsdb/mutation.h +++ b/ovsdb/mutation.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2009 Nicira Networks +/* Copyright (c) 2009, 2010 Nicira Networks * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,7 +62,7 @@ struct ovsdb_mutation_set { struct ovsdb_error *ovsdb_mutation_set_from_json( const struct ovsdb_table_schema *, - const struct json *, const struct ovsdb_symbol_table *, + const struct json *, struct ovsdb_symbol_table *, struct ovsdb_mutation_set *) WARN_UNUSED_RESULT; struct json *ovsdb_mutation_set_to_json(const struct ovsdb_mutation_set *); void ovsdb_mutation_set_destroy(struct ovsdb_mutation_set *); diff --git a/ovsdb/row.c b/ovsdb/row.c index 52c5ddb2..d088ff98 100644 --- a/ovsdb/row.c +++ b/ovsdb/row.c @@ -160,7 +160,7 @@ ovsdb_row_update_columns(struct ovsdb_row *dst, struct ovsdb_error * ovsdb_row_from_json(struct ovsdb_row *row, const struct json *json, - const struct ovsdb_symbol_table *symtab, + struct ovsdb_symbol_table *symtab, struct ovsdb_column_set *included) { struct ovsdb_table_schema *schema = row->table->schema; diff --git a/ovsdb/row.h b/ovsdb/row.h index d468194a..302f61ab 100644 --- a/ovsdb/row.h +++ b/ovsdb/row.h @@ -57,7 +57,7 @@ void ovsdb_row_update_columns(struct ovsdb_row *, const struct ovsdb_row *, struct ovsdb_error *ovsdb_row_from_json(struct ovsdb_row *, const struct json *, - const struct ovsdb_symbol_table *, + struct ovsdb_symbol_table *, struct ovsdb_column_set *included) WARN_UNUSED_RESULT; struct json *ovsdb_row_to_json(const struct ovsdb_row *, diff --git a/tests/ovsdb-execution.at b/tests/ovsdb-execution.at index 334e2084..06080f71 100644 --- a/tests/ovsdb-execution.at +++ b/tests/ovsdb-execution.at @@ -443,11 +443,7 @@ OVSDB_CHECK_EXECUTION([referential integrity -- simple], OVSDB_CHECK_EXECUTION([referential integrity -- mutual references], [CONSTRAINT_SCHEMA], - [[[[{"op": "declare", - "uuid-name": "row1"}, - {"op": "declare", - "uuid-name": "row2"}, - {"op": "insert", + [[[[{"op": "insert", "table": "a", "row": {"a": 0, "a2b": ["set", [["named-uuid", "row2"]]], @@ -481,7 +477,7 @@ OVSDB_CHECK_EXECUTION([referential integrity -- mutual references], {"op": "delete", "table": "b", "where": [["b", "==", 1]]}]]]], - [[[{"uuid":["uuid","<0>"]},{"uuid":["uuid","<1>"]},{"uuid":["uuid","<0>"]},{"uuid":["uuid","<1>"]}] + [[[{"uuid":["uuid","<0>"]},{"uuid":["uuid","<1>"]}] [{"uuid":["uuid","<2>"]},{"details":"reference to nonexistent row <3>","error":"referential integrity violation"}] [{"count":1},{"details":"cannot delete a row <0> because of 1 remaining reference(s)","error":"referential integrity violation"}] [{"count":1},{"details":"cannot delete b row <1> because of 1 remaining reference(s)","error":"referential integrity violation"}] diff --git a/tests/ovsdb-idl.at b/tests/ovsdb-idl.at index 552f627f..6fc57f37 100644 --- a/tests/ovsdb-idl.at +++ b/tests/ovsdb-idl.at @@ -184,11 +184,7 @@ OVSDB_CHECK_IDL([self-linking idl, consistent ops], "table": "link1", "row": {"i": 0, "k": ["named-uuid", "self"]}, "uuid-name": "self"}]' \ - '[{"op": "declare", - "uuid-name": "row1"}, - {"op": "declare", - "uuid-name": "row2"}, - {"op": "insert", + '[{"op": "insert", "table": "link1", "row": {"i": 1, "k": ["named-uuid", "row2"]}, "uuid-name": "row1"}, @@ -207,7 +203,7 @@ OVSDB_CHECK_IDL([self-linking idl, consistent ops], [[000: empty 001: {"error":null,"result":[{"uuid":["uuid","<0>"]}]} 002: i=0 k=0 ka=[] l2= uuid=<0> -003: {"error":null,"result":[{"uuid":["uuid","<1>"]},{"uuid":["uuid","<2>"]},{"uuid":["uuid","<1>"]},{"uuid":["uuid","<2>"]}]} +003: {"error":null,"result":[{"uuid":["uuid","<1>"]},{"uuid":["uuid","<2>"]}]} 004: i=0 k=0 ka=[] l2= uuid=<0> 004: i=1 k=2 ka=[] l2= uuid=<1> 004: i=2 k=1 ka=[] l2= uuid=<2> -- 2.30.2