From 3cd17b4bd4464df0ffaf28d785e88dfa5736d7c0 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Sat, 17 Sep 2016 17:35:11 +0000 Subject: Use a guard instead of catch and rethrow. --- .../src/vespa/filedistribution/model/zkfacade.cpp | 81 ++++++++++------------ 1 file changed, 38 insertions(+), 43 deletions(-) (limited to 'filedistribution') diff --git a/filedistribution/src/vespa/filedistribution/model/zkfacade.cpp b/filedistribution/src/vespa/filedistribution/model/zkfacade.cpp index 89ff72e4a36..5d18deed80b 100644 --- a/filedistribution/src/vespa/filedistribution/model/zkfacade.cpp +++ b/filedistribution/src/vespa/filedistribution/model/zkfacade.cpp @@ -422,33 +422,31 @@ ZKFacade::hasNode(const Path& path) { bool ZKFacade::hasNode(const Path& path, const NodeChangedWatcherSP& watcher) { - void* watcherContext = registerWatcher(watcher); - try { - RetryController retryController(this); - do { - Stat stat; - retryController( - zoo_wexists(_zhandle, path.string().c_str(), - &ZKWatcher::watcherFn, watcherContext, - &stat)); - } while(retryController.shouldRetry()); - - switch(retryController._lastStatus) { - case ZNONODE: - return false; - case ZOK: - return true; - default: - retryController.throwIfError(path); - //this should never happen: - assert(false); - return false; - } + RegistrationGuard unregisterGuard(*this, watcher); + void* watcherContext = unregisterGuard.get(); + RetryController retryController(this); + do { + Stat stat; + retryController(zoo_wexists(_zhandle, path.string().c_str(), &ZKWatcher::watcherFn, watcherContext, &stat)); + } while (retryController.shouldRetry()); - } catch (const ZKException &e) { - unregisterWatcher(watcherContext); - throw; + bool retval(false); + switch(retryController._lastStatus) { + case ZNONODE: + retval = false; + break; + case ZOK: + retval = true; + break; + default: + retryController.throwIfError(path); + //this should never happen: + assert(false); + retval = false; + break; } + unregisterGuard.release(); + return retval; } void @@ -529,30 +527,27 @@ ZKFacade::getChildren(const Path& path) { std::vector< std::string > ZKFacade::getChildren(const Path& path, const NodeChangedWatcherSP& watcher) { - void* watcherContext = registerWatcher(watcher); + RegistrationGuard unregisterGuard(*this, watcher); + void* watcherContext = unregisterGuard.get(); - try { - RetryController retryController(this); - String_vector children; - do { - retryController( zoo_wget_children(_zhandle, path.string().c_str(), &ZKWatcher::watcherFn, watcherContext, &children)); - } while(retryController.shouldRetry()); + RetryController retryController(this); + String_vector children; + do { + retryController( zoo_wget_children(_zhandle, path.string().c_str(), &ZKWatcher::watcherFn, watcherContext, &children)); + } while (retryController.shouldRetry()); - retryController.throwIfError(path); + retryController.throwIfError(path); - DeallocateZKStringVectorGuard deallocateGuard(children); + DeallocateZKStringVectorGuard deallocateGuard(children); - typedef std::vector ResultType; - ResultType result; - result.reserve(children.count); + typedef std::vector ResultType; + ResultType result; + result.reserve(children.count); - std::copy(children.data, children.data + children.count, std::back_inserter(result)); + std::copy(children.data, children.data + children.count, std::back_inserter(result)); - return result; - } catch (const ZKException & e) { - unregisterWatcher(watcherContext); - throw; - } + unregisterGuard.release(); + return result; } -- cgit v1.2.3