Example #1
0
        /// <summary>
        /// Used to create a batched set of actions to be sent to a Riak cluster. This guarantees some level of serialized activity.
        /// </summary>
        /// <param name='batchAction'>
        /// Batch action.
        /// </param>
        /// <exception cref='Exception'>
        /// Represents errors that occur during application execution.
        /// </exception>
        public void Batch(Action <IRiakBatchClient> batchAction)
        {
            Func <IRiakConnection, Action, RiakResult <IEnumerable <RiakResult <object> > > > batchFun = (conn, onFinish) =>
            {
                try
                {
                    batchAction(new RiakClient(conn, _clientId));
                    return(RiakResult <IEnumerable <RiakResult <object> > > .Success(null));
                }
                catch (Exception ex)
                {
                    return(RiakResult <IEnumerable <RiakResult <object> > > .Error(ResultCode.BatchException, "{0}\n{1}".Fmt(ex.Message, ex.StackTrace)));
                }
                finally
                {
                    onFinish();
                }
            };

            var result = _endPoint.UseDelayedConnection(_clientId, batchFun, RetryCount);

            if (!result.IsSuccess && result.ResultCode == ResultCode.BatchException)
            {
                throw new Exception(result.ErrorMessage);
            }
        }
Example #2
0
        public T Batch <T>(Func <IRiakBatchClient, T> batchFun)
        {
            var funResult = default(T);

            Func <IRiakConnection, Action, RiakResult <IEnumerable <RiakResult <object> > > > helperBatchFun = (conn, onFinish) =>
            {
                try
                {
                    funResult = batchFun(new RiakClient(conn));
                    return(RiakResult <IEnumerable <RiakResult <object> > > .Success(null));
                }
                catch (Exception ex)
                {
                    return(RiakResult <IEnumerable <RiakResult <object> > > .Error(ResultCode.BatchException, "{0}\n{1}".Fmt(ex.Message, ex.StackTrace), true));
                }
                finally
                {
                    onFinish();
                }
            };

            var result = _endPoint.UseDelayedConnection(helperBatchFun, RetryCount);

            if (!result.IsSuccess && result.ResultCode == ResultCode.BatchException)
            {
                throw new Exception(result.ErrorMessage);
            }

            return(funResult);
        }
Example #3
0
        public Task <T> Batch <T>(Func <IRiakBatchAsyncClient, Task <T> > batchFun)
        {
            var taskResult = default(T);

            // no idea what this is
            Func <IRiakConnection, Action, Task <RiakResult <IEnumerable <RiakResult <object> > > > > helperBatchFun = ((conn, onFinish) =>
            {
                return(Task.Factory.StartNew(() => batchFun(new RiakAsyncClient(conn))).Unwrap()
                       .ContinueWith((Task <T> finishedTask) => {
                    onFinish();

                    // exception or success
                    if (!finishedTask.IsFaulted)
                    {
                        taskResult = finishedTask.Result;
                        return RiakResult <IEnumerable <RiakResult <object> > > .Success(null);
                    }
                    else
                    {
                        var ex = finishedTask.Exception.Flatten().InnerExceptions.First();
                        return RiakResult <IEnumerable <RiakResult <object> > > .Error(
                            ResultCode.BatchException, "{0}\n{1}".Fmt(ex.Message, ex.StackTrace), true);
                    }
                }));
            });

            // apply task to connection
            return(_endPoint.UseDelayedConnection(helperBatchFun, RetryCount)
                   .ContinueWith((Task <RiakResult <IEnumerable <RiakResult <object> > > > finishedTask) => {
                var result = finishedTask.Result;
                if (!result.IsSuccess && result.ResultCode == ResultCode.BatchException)
                {
                    throw new Exception(result.ErrorMessage);
                }
                return taskResult;
            }));
        }