From 2452d41b2292c2f48de99a519e2cb948709daa76 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Wed, 7 Jun 2006 00:55:48 +0000 Subject: r16071: tdb has nested transactions change the code to exploit that in ldb I still have to reintroduce transactions when you call ldb_request directly, I have some plans I hop to be able to develop in the next weekend (This used to be commit 35111206021d667dfd217b5fd8d82f5c2714cc9e) --- source4/lib/ldb/common/ldb.c | 139 ++++++++++++++++++++++++++----------------- 1 file changed, 86 insertions(+), 53 deletions(-) diff --git a/source4/lib/ldb/common/ldb.c b/source4/lib/ldb/common/ldb.c index b6c22437bd..1fdbeb55d2 100644 --- a/source4/lib/ldb/common/ldb.c +++ b/source4/lib/ldb/common/ldb.c @@ -171,13 +171,11 @@ void ldb_reset_err_string(struct ldb_context *ldb) /* start a transaction */ -int ldb_transaction_start(struct ldb_context *ldb) +static int ldb_transaction_start_internal(struct ldb_context *ldb) { struct ldb_module *module; int status; FIRST_OP(ldb, start_transaction); - - ldb->transaction_active++; ldb_reset_err_string(ldb); @@ -195,18 +193,12 @@ int ldb_transaction_start(struct ldb_context *ldb) /* commit a transaction */ -int ldb_transaction_commit(struct ldb_context *ldb) +static int ldb_transaction_commit_internal(struct ldb_context *ldb) { struct ldb_module *module; int status; FIRST_OP(ldb, end_transaction); - if (ldb->transaction_active > 0) { - ldb->transaction_active--; - } else { - return LDB_ERR_OPERATIONS_ERROR; - } - ldb_reset_err_string(ldb); status = module->ops->end_transaction(module); @@ -223,18 +215,12 @@ int ldb_transaction_commit(struct ldb_context *ldb) /* cancel a transaction */ -int ldb_transaction_cancel(struct ldb_context *ldb) +static int ldb_transaction_cancel_internal(struct ldb_context *ldb) { struct ldb_module *module; int status; FIRST_OP(ldb, del_transaction); - if (ldb->transaction_active > 0) { - ldb->transaction_active--; - } else { - return LDB_ERR_OPERATIONS_ERROR; - } - status = module->ops->del_transaction(module); if (status != LDB_SUCCESS) { if (ldb->err_string == NULL) { @@ -246,6 +232,89 @@ int ldb_transaction_cancel(struct ldb_context *ldb) return status; } +int ldb_transaction_start(struct ldb_context *ldb) +{ + /* disable autotransactions */ + ldb->transaction_active++; + + return ldb_transaction_start_internal(ldb); +} + +int ldb_transaction_commit(struct ldb_context *ldb) +{ + /* renable autotransactions (when we reach 0) */ + if (ldb->transaction_active > 0) + ldb->transaction_active--; + + return ldb_transaction_commit_internal(ldb); +} + +int ldb_transaction_cancel(struct ldb_context *ldb) +{ + /* renable autotransactions (when we reach 0) */ + if (ldb->transaction_active > 0) + ldb->transaction_active--; + + return ldb_transaction_cancel_internal(ldb); +} + +int ldb_autotransaction_start(struct ldb_context *ldb) +{ + /* explicit transaction active, ignore autotransaction request */ + if (ldb->transaction_active) + return LDB_SUCCESS; + + return ldb_transaction_start_internal(ldb); +} + +int ldb_autotransaction_commit(struct ldb_context *ldb) +{ + /* explicit transaction active, ignore autotransaction request */ + if (ldb->transaction_active) + return LDB_SUCCESS; + + return ldb_transaction_commit_internal(ldb); +} + +int ldb_autotransaction_cancel(struct ldb_context *ldb) +{ + /* explicit transaction active, ignore autotransaction request */ + if (ldb->transaction_active) + return LDB_SUCCESS; + + return ldb_transaction_cancel_internal(ldb); +} + +/* autostarts a transacion if none active */ +static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req) +{ + int ret; + + ret = ldb_autotransaction_start(ldb); + if (ret != LDB_SUCCESS) { + return ret; + } + + ret = ldb_request(ldb, req); + if (ret == LDB_SUCCESS) { + ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL); + } + + if (ret == LDB_SUCCESS) { + return ldb_autotransaction_commit(ldb); + } + ldb_autotransaction_cancel(ldb); + + if (ldb->err_string == NULL) { + /* no error string was setup by the backend */ + ldb_set_errstring(ldb, + talloc_asprintf(ldb, "%s (%d)", + ldb_strerror(ret), ret)); + } + + return ret; +} + int ldb_async_wait(struct ldb_async_handle *handle, enum ldb_async_wait_type type) { if (!handle) { @@ -461,42 +530,6 @@ int ldb_search(struct ldb_context *ldb, return ret; } -/* autostarts a transacion if none active */ -static int ldb_autotransaction_request(struct ldb_context *ldb, struct ldb_request *req) -{ - int ret, close_transaction; - - close_transaction = 0; - if (!ldb->transaction_active) { - ret = ldb_transaction_start(ldb); - if (ret != LDB_SUCCESS) { - return ret; - } - close_transaction = 1; - } - - ret = ldb_request(ldb, req); - if (ret == LDB_SUCCESS) { - ret = ldb_async_wait(req->async.handle, LDB_WAIT_ALL); - } - - if (close_transaction) { - if (ret == LDB_SUCCESS) { - return ldb_transaction_commit(ldb); - } - ldb_transaction_cancel(ldb); - } - - if (ldb->err_string == NULL) { - /* no error string was setup by the backend */ - ldb_set_errstring(ldb, - talloc_asprintf(ldb, "%s (%d)", - ldb_strerror(ret), ret)); - } - - return ret; -} - /* add a record to the database. Will fail if a record with the given class and key -- cgit