From 69855f9214c1a93daba216035f18b665a2368f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Tue, 29 May 2018 00:56:32 +0300 Subject: [PATCH] MXS-1881: Refactor AVRO_TABLE Moved initialization into the constructor and removed unused member variables. --- .../modules/routing/avrorouter/avro_file.cc | 77 ++++++++++--------- .../modules/routing/avrorouter/avrorouter.hh | 11 ++- 2 files changed, 47 insertions(+), 41 deletions(-) diff --git a/server/modules/routing/avrorouter/avro_file.cc b/server/modules/routing/avrorouter/avro_file.cc index 89bb1393b..0f7404e47 100644 --- a/server/modules/routing/avrorouter/avro_file.cc +++ b/server/modules/routing/avrorouter/avro_file.cc @@ -102,50 +102,53 @@ void avro_close_binlog(int fd) AVRO_TABLE* avro_table_alloc(const char* filepath, const char* json_schema, const char *codec, size_t block_size) { - AVRO_TABLE *table = new (std::nothrow)AVRO_TABLE; + avro_file_writer_t avro_file; + avro_value_iface_t* avro_writer_iface; + avro_schema_t avro_schema; - if (table) + if (avro_schema_from_json_length(json_schema, strlen(json_schema), + &avro_schema)) { - if (avro_schema_from_json_length(json_schema, strlen(json_schema), - &table->avro_schema)) - { - MXS_ERROR("Avro error: %s", avro_strerror()); - MXS_INFO("Avro schema: %s", json_schema); - MXS_FREE(table); - return NULL; - } + MXS_ERROR("Avro error: %s", avro_strerror()); + MXS_INFO("Avro schema: %s", json_schema); + return NULL; + } - int rc = 0; + int rc = 0; - if (access(filepath, F_OK) == 0) - { - rc = avro_file_writer_open_bs(filepath, &table->avro_file, block_size); - } - else - { - rc = avro_file_writer_create_with_codec(filepath, table->avro_schema, - &table->avro_file, codec, block_size); - } + if (access(filepath, F_OK) == 0) + { + rc = avro_file_writer_open_bs(filepath, &avro_file, block_size); + } + else + { + rc = avro_file_writer_create_with_codec(filepath, avro_schema, + &avro_file, codec, block_size); + } - if (rc) - { - MXS_ERROR("Avro error: %s", avro_strerror()); - avro_schema_decref(table->avro_schema); - MXS_FREE(table); - return NULL; - } + if (rc) + { + MXS_ERROR("Avro error: %s", avro_strerror()); + avro_schema_decref(avro_schema); + return NULL; + } - if ((table->avro_writer_iface = avro_generic_class_from_schema(table->avro_schema)) == NULL) - { - MXS_ERROR("Avro error: %s", avro_strerror()); - avro_schema_decref(table->avro_schema); - avro_file_writer_close(table->avro_file); - MXS_FREE(table); - return NULL; - } + if ((avro_writer_iface = avro_generic_class_from_schema(avro_schema)) == NULL) + { + MXS_ERROR("Avro error: %s", avro_strerror()); + avro_schema_decref(avro_schema); + avro_file_writer_close(avro_file); + return NULL; + } - table->json_schema = MXS_STRDUP_A(json_schema); - table->filename = MXS_STRDUP_A(filepath); + AVRO_TABLE* table = new (std::nothrow) AVRO_TABLE(avro_file, avro_writer_iface, avro_schema); + + if (!table) + { + avro_file_writer_close(avro_file); + avro_value_iface_decref(avro_writer_iface); + avro_schema_decref(avro_schema); + MXS_OOM(); } return table; diff --git a/server/modules/routing/avrorouter/avrorouter.hh b/server/modules/routing/avrorouter/avrorouter.hh index 26ab94041..6dcb0b3af 100644 --- a/server/modules/routing/avrorouter/avrorouter.hh +++ b/server/modules/routing/avrorouter/avrorouter.hh @@ -192,18 +192,21 @@ struct TABLE_MAP struct AVRO_TABLE { + AVRO_TABLE(avro_file_writer_t file, avro_value_iface_t* iface, avro_schema_t schema): + avro_file(file), + avro_writer_iface(iface), + avro_schema(schema) + { + } + ~AVRO_TABLE() { avro_file_writer_flush(avro_file); avro_file_writer_close(avro_file); avro_value_iface_decref(avro_writer_iface); avro_schema_decref(avro_schema); - MXS_FREE(json_schema); - MXS_FREE(filename); } - char* filename; /*< Absolute filename */ - char* json_schema; /*< JSON representation of the schema */ avro_file_writer_t avro_file; /*< Current Avro data file */ avro_value_iface_t* avro_writer_iface; /*< Avro C API writer interface */ avro_schema_t avro_schema; /*< Native Avro schema of the table */