Mercurial > dovecot > core-2.2
changeset 22528:348e82e819b0
cassandra: sql_transaction_commit*() cleanup - handle multiple query failures earlier
This makes the handling same for the sync and async method. It also
simplifies code for the following commits.
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 24 Aug 2017 11:56:38 +0300 |
parents | db43860e2b12 |
children | 7b17f52b75b6 |
files | src/lib-sql/driver-cassandra.c |
diffstat | 1 files changed, 23 insertions(+), 36 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib-sql/driver-cassandra.c Thu Aug 24 11:13:32 2017 +0300 +++ b/src/lib-sql/driver-cassandra.c Thu Aug 24 11:56:38 2017 +0300 @@ -149,7 +149,7 @@ sql_commit2_callback_t *callback; void *context; - pool_t query_pool; + char *query; char *error; unsigned int begin_succeeded:1; @@ -1406,9 +1406,6 @@ ctx = i_new(struct cassandra_transaction_context, 1); ctx->ctx.db = db; ctx->refcount = 1; - /* we need to be able to handle multiple open transactions, so at least - for now just keep them in memory until commit time. */ - ctx->query_pool = pool_alloconly_create("cassandra transaction", 1024); return &ctx->ctx; } @@ -1422,7 +1419,7 @@ if (--ctx->refcount > 0) return; - pool_unref(&ctx->query_pool); + i_free(ctx->query); i_free(ctx->error); i_free(ctx); } @@ -1468,24 +1465,20 @@ ctx->callback = callback; ctx->context = context; - if (ctx->failed || _ctx->head == NULL) { + if (ctx->failed || ctx->query == NULL) { if (ctx->failed) result.error = ctx->error; callback(&result, context); driver_cassandra_transaction_unref(&ctx); - } else if (_ctx->head->next == NULL) { + } else { /* just a single query, send it */ - if (strncasecmp(_ctx->head->query, "DELETE ", 7) == 0) + if (strncasecmp(ctx->query, "DELETE ", 7) == 0) query_type = CASSANDRA_QUERY_TYPE_DELETE; else query_type = CASSANDRA_QUERY_TYPE_WRITE; - driver_cassandra_query_full(_ctx->db, _ctx->head->query, query_type, + driver_cassandra_query_full(_ctx->db, ctx->query, query_type, transaction_commit_callback, ctx); - } else { - /* multiple queries - we don't actually have a transaction though */ - result.error = "Multiple changes in transaction not supported"; - callback(&result, context); } } @@ -1494,31 +1487,21 @@ { struct sql_transaction_context *_ctx = &ctx->ctx; struct cassandra_db *db = (struct cassandra_db *)_ctx->db; - struct sql_transaction_query *single_query = NULL; struct sql_result *result = NULL; enum cassandra_query_type query_type; - if (_ctx->head->next == NULL) { - /* just a single query, send it */ - single_query = _ctx->head; - if (strncasecmp(_ctx->head->query, "DELETE ", 7) == 0) - query_type = CASSANDRA_QUERY_TYPE_DELETE; - else - query_type = CASSANDRA_QUERY_TYPE_WRITE; - driver_cassandra_sync_init(db); - result = driver_cassandra_sync_query(db, single_query->query, query_type); - driver_cassandra_sync_deinit(db); - } else { - /* multiple queries - we don't actually have a transaction though */ - transaction_set_failed(ctx, "Multiple changes in transaction not supported"); - } + /* just a single query, send it */ + if (strncasecmp(ctx->query, "DELETE ", 7) == 0) + query_type = CASSANDRA_QUERY_TYPE_DELETE; + else + query_type = CASSANDRA_QUERY_TYPE_WRITE; + driver_cassandra_sync_init(db); + result = driver_cassandra_sync_query(db, ctx->query, query_type); + driver_cassandra_sync_deinit(db); - if (!ctx->failed) { - if (sql_result_next_row(result) < 0) - transaction_set_failed(ctx, sql_result_get_error(result)); - } - if (result != NULL) - sql_result_unref(result); + if (sql_result_next_row(result) < 0) + transaction_set_failed(ctx, sql_result_get_error(result)); + sql_result_unref(result); } static int @@ -1528,7 +1511,7 @@ struct cassandra_transaction_context *ctx = (struct cassandra_transaction_context *)_ctx; - if (_ctx->head != NULL) + if (ctx->query != NULL && !ctx->failed) driver_cassandra_try_commit_s(ctx); *error_r = t_strdup(ctx->error); @@ -1557,7 +1540,11 @@ i_assert(affected_rows == NULL); - sql_transaction_add_query(_ctx, ctx->query_pool, query, affected_rows); + if (ctx->query != NULL) { + transaction_set_failed(ctx, "Multiple changes in transaction not supported"); + return; + } + ctx->query = i_strdup(query); } static const char *