MXS-1881: Refactor AVRO_TABLE

Moved initialization into the constructor and removed unused member
variables.
This commit is contained in:
Markus Mäkelä
2018-05-29 00:56:32 +03:00
parent f61c56228c
commit 69855f9214
2 changed files with 47 additions and 41 deletions

View File

@ -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, AVRO_TABLE* avro_table_alloc(const char* filepath, const char* json_schema, const char *codec,
size_t block_size) 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), MXS_ERROR("Avro error: %s", avro_strerror());
&table->avro_schema)) MXS_INFO("Avro schema: %s", json_schema);
{ return NULL;
MXS_ERROR("Avro error: %s", avro_strerror()); }
MXS_INFO("Avro schema: %s", json_schema);
MXS_FREE(table);
return NULL;
}
int rc = 0; int rc = 0;
if (access(filepath, F_OK) == 0) if (access(filepath, F_OK) == 0)
{ {
rc = avro_file_writer_open_bs(filepath, &table->avro_file, block_size); rc = avro_file_writer_open_bs(filepath, &avro_file, block_size);
} }
else else
{ {
rc = avro_file_writer_create_with_codec(filepath, table->avro_schema, rc = avro_file_writer_create_with_codec(filepath, avro_schema,
&table->avro_file, codec, block_size); &avro_file, codec, block_size);
} }
if (rc) if (rc)
{ {
MXS_ERROR("Avro error: %s", avro_strerror()); MXS_ERROR("Avro error: %s", avro_strerror());
avro_schema_decref(table->avro_schema); avro_schema_decref(avro_schema);
MXS_FREE(table); return NULL;
return NULL; }
}
if ((table->avro_writer_iface = avro_generic_class_from_schema(table->avro_schema)) == NULL) if ((avro_writer_iface = avro_generic_class_from_schema(avro_schema)) == NULL)
{ {
MXS_ERROR("Avro error: %s", avro_strerror()); MXS_ERROR("Avro error: %s", avro_strerror());
avro_schema_decref(table->avro_schema); avro_schema_decref(avro_schema);
avro_file_writer_close(table->avro_file); avro_file_writer_close(avro_file);
MXS_FREE(table); return NULL;
return NULL; }
}
table->json_schema = MXS_STRDUP_A(json_schema); AVRO_TABLE* table = new (std::nothrow) AVRO_TABLE(avro_file, avro_writer_iface, avro_schema);
table->filename = MXS_STRDUP_A(filepath);
if (!table)
{
avro_file_writer_close(avro_file);
avro_value_iface_decref(avro_writer_iface);
avro_schema_decref(avro_schema);
MXS_OOM();
} }
return table; return table;

View File

@ -192,18 +192,21 @@ struct TABLE_MAP
struct AVRO_TABLE 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_TABLE()
{ {
avro_file_writer_flush(avro_file); avro_file_writer_flush(avro_file);
avro_file_writer_close(avro_file); avro_file_writer_close(avro_file);
avro_value_iface_decref(avro_writer_iface); avro_value_iface_decref(avro_writer_iface);
avro_schema_decref(avro_schema); 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_file_writer_t avro_file; /*< Current Avro data file */
avro_value_iface_t* avro_writer_iface; /*< Avro C API writer interface */ avro_value_iface_t* avro_writer_iface; /*< Avro C API writer interface */
avro_schema_t avro_schema; /*< Native Avro schema of the table */ avro_schema_t avro_schema; /*< Native Avro schema of the table */