public RiakResult <TResult> PbcWriteRead <TRequest, TResult>(TRequest request)
            where TRequest : class
            where TResult : class, new()
        {
            var writeResult = PbcWrite(request);

            if (writeResult.IsSuccess)
            {
                return(PbcRead <TResult>());
            }
            return(RiakResult <TResult> .Error(writeResult.ResultCode, writeResult.ErrorMessage, writeResult.NodeOffline));
        }
        public RiakResult <IEnumerable <RiakResult <TResult> > > PbcWriteRead <TResult>(MessageCode messageCode,
                                                                                        Func <RiakResult <TResult>, bool> repeatRead)
            where TResult : class, new()
        {
            var writeResult = PbcWrite(messageCode);

            if (writeResult.IsSuccess)
            {
                return(PbcRepeatRead(repeatRead));
            }
            return(RiakResult <IEnumerable <RiakResult <TResult> > > .Error(writeResult.ResultCode, writeResult.ErrorMessage, writeResult.NodeOffline));
        }
Esempio n. 3
0
        public RiakResult <IEnumerable <RiakResult <TResult> > > PbcWriteRead <TRequest, TResult>(TRequest request,
                                                                                                  Func <RiakResult <TResult>, bool> repeatRead)
            where TResult : new()
        {
            var writeResult = PbcWrite(request);

            if (writeResult.IsSuccess)
            {
                return(PbcRepeatRead(repeatRead));
            }
            return(RiakResult <IEnumerable <RiakResult <TResult> > > .Error(writeResult.ResultCode, writeResult.ErrorMessage));
        }
Esempio n. 4
0
        private IEnumerable <RiakResult <TResult> > PbcWriteStreamReadIterator <TRequest, TResult>(TRequest request,
                                                                                                   Func <RiakResult <TResult>, bool> repeatRead, Action onFinish)
            where TResult : new()
        {
            var writeResult = PbcWrite(request);

            if (writeResult.IsSuccess)
            {
                return(PbcStreamReadIterator(repeatRead, onFinish));
            }
            onFinish();
            return(new[] { RiakResult <TResult> .Error(writeResult.ResultCode, writeResult.ErrorMessage) });
        }
Esempio n. 5
0
 public Task <RiakResult <IEnumerable <RiakResult <TResult> > > > PbcWriteStreamRead <TResult>(MessageCode messageCode, Func <RiakResult <TResult>, bool> repeatRead, Action onFinish) where TResult : class, new()
 {
     return(PbcWrite(messageCode)
            .ContinueWith((Task <RiakResult> writeTask) => {
         var writeResult = writeTask.Result;
         if (writeResult.IsSuccess)
         {
             var streamer = PbcStreamReadIterator(repeatRead, onFinish);
             return RiakResult <IEnumerable <RiakResult <TResult> > > .Success(streamer);
         }
         return RiakResult <IEnumerable <RiakResult <TResult> > > .Error(writeResult.ResultCode, writeResult.ErrorMessage, writeResult.NodeOffline);
     }));
 }
        private IEnumerable <RiakResult <TResult> > PbcWriteStreamReadIterator <TResult>(MessageCode messageCode,
                                                                                         Func <RiakResult <TResult>, bool> repeatRead, Action onFinish)
            where TResult : class, new()
        {
            var writeResult = PbcWrite(messageCode);

            if (writeResult.IsSuccess)
            {
                return(PbcStreamReadIterator(repeatRead, onFinish));
            }
            onFinish();
            return(new[] { RiakResult <TResult> .Error(writeResult.ResultCode, writeResult.ErrorMessage, writeResult.NodeOffline) });
        }
Esempio n. 7
0
 public RiakResult PbcWrite <TRequest>(TRequest request)
 {
     try
     {
         _socket.Write(request);
         return(RiakResult.Success());
     }
     catch (Exception ex)
     {
         Disconnect();
         return(RiakResult.Error(ResultCode.CommunicationError, ex.Message));
     }
 }
Esempio n. 8
0
 public RiakResult <TResult> PbcRead <TResult>()
     where TResult : new()
 {
     try
     {
         var result = _socket.Read <TResult>();
         return(RiakResult <TResult> .Success(result));
     }
     catch (Exception ex)
     {
         Disconnect();
         return(RiakResult <TResult> .Error(ResultCode.CommunicationError, ex.Message));
     }
 }
Esempio n. 9
0
        public RiakResult <IEnumerable <TResult> > UseDelayedConnection <TResult>(Func <IRiakConnection, Action, RiakResult <IEnumerable <TResult> > > useFun)
            where TResult : RiakResult
        {
            if (_disposing)
            {
                return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.ShuttingDown, "Connection is shutting down", true));
            }

            var response = _connections.DelayedConsume(useFun);

            if (response.Item1)
            {
                return(response.Item2);
            }
            return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.NoConnections, "Unable to acquire connection", true));
        }
Esempio n. 10
0
        public Task <RiakResult <IEnumerable <TResult> > > UseDelayedConnection <TResult>(Func <IRiakConnection, Action, Task <RiakResult <IEnumerable <TResult> > > > useFun)
            where TResult : RiakResult
        {
            if (_disposing)
            {
                return(TaskResult(RiakResult <IEnumerable <TResult> > .Error(ResultCode.ShuttingDown, "Connection is shutting down", true)));
            }

            // attempt consume
            return(_connections.DelayedConsume(useFun)
                   .ContinueWith((Task <Tuple <bool, RiakResult <IEnumerable <TResult> > > > finishedTask) => {
                var result = finishedTask.Result;
                if (result.Item1)
                {
                    return result.Item2;
                }
                return RiakResult <IEnumerable <TResult> > .Error(ResultCode.NoConnections, "Unable to acquire connection", true);
            }));
        }
Esempio n. 11
0
        // tidy up messy exceptions
        private RiakResult <T> GetExceptionResult <T>(AggregateException aggregateException)
        {
            var exceptions = aggregateException.Flatten().InnerExceptions;
            var rEx        = exceptions.OfType <RiakException>().FirstOrDefault();
            var fEx        = exceptions.FirstOrDefault();

            if (rEx != null)
            {
                if (rEx.NodeOffline)
                {
                    Disconnect();
                }
                return(RiakResult <T> .Error(ResultCode.CommunicationError, rEx.Message, rEx.NodeOffline));
            }
            else
            {
                Disconnect();
                return(RiakResult <T> .Error(ResultCode.CommunicationError, fEx.Message, true));
            }
        }
 public RiakResult PbcWrite(MessageCode messageCode)
 {
     try
     {
         _socket.Write(messageCode);
         return(RiakResult.Success());
     }
     catch (RiakException ex)
     {
         if (ex.NodeOffline)
         {
             Disconnect();
         }
         return(RiakResult.Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline));
     }
     catch (Exception ex)
     {
         Disconnect();
         return(RiakResult.Error(ResultCode.CommunicationError, ex.Message, true));
     }
 }
 public RiakResult PbcWrite <TRequest>(TRequest request)
     where TRequest : class
 {
     try
     {
         _socket.Write(request);
         return(RiakResult.Success());
     }
     catch (RiakException ex)
     {
         if (ex.NodeOffline)
         {
             Disconnect();
         }
         return(RiakResult.Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline));
     }
     catch (Exception ex)
     {
         Disconnect();
         return(RiakResult.Error(ResultCode.CommunicationError, ex.Message, true));
     }
 }
 public RiakResult <TResult> PbcRead <TResult>()
     where TResult : class, new()
 {
     try
     {
         var result = _socket.Read <TResult>();
         return(RiakResult <TResult> .Success(result));
     }
     catch (RiakException ex)
     {
         if (ex.NodeOffline)
         {
             Disconnect();
         }
         return(RiakResult <TResult> .Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline));
     }
     catch (Exception ex)
     {
         Disconnect();
         return(RiakResult <TResult> .Error(ResultCode.CommunicationError, ex.Message, true));
     }
 }
Esempio n. 15
0
        public RiakResult <IEnumerable <TResult> > UseDelayedConnection <TResult>(byte[] clientId, Func <IRiakConnection, Action, RiakResult <IEnumerable <TResult> > > useFun)
            where TResult : RiakResult
        {
            if (_disposing)
            {
                return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.ShuttingDown, "Connection is shutting down"));
            }

            Func <IRiakConnection, Action, RiakResult <IEnumerable <TResult> > > wrapper = (conn, onFinish) =>
            {
                conn.SetClientId(clientId);
                return(useFun(conn, onFinish));
            };

            var response = _connections.DelayedConsume(wrapper);

            if (response.Item1)
            {
                return(response.Item2);
            }
            return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.NoConnections, "Unable to acquire connection"));
        }
Esempio n. 16
0
        public RiakResult <IEnumerable <RiakResult <TResult> > > PbcRepeatRead <TResult>(Func <RiakResult <TResult>, bool> repeatRead)
            where TResult : new()
        {
            var results = new List <RiakResult <TResult> >();

            try
            {
                RiakResult <TResult> result;
                do
                {
                    result = RiakResult <TResult> .Success(_socket.Read <TResult>());

                    results.Add(result);
                } while(repeatRead(result));

                return(RiakResult <IEnumerable <RiakResult <TResult> > > .Success(results));
            }
            catch (Exception ex)
            {
                Disconnect();
                return(RiakResult <IEnumerable <RiakResult <TResult> > > .Error(ResultCode.CommunicationError, ex.Message));
            }
        }
        public RiakResult <RiakRestResponse> RestRequest(RiakRestRequest request)
        {
            var baseUri = new StringBuilder(_restRootUrl).Append(request.Uri);

            if (request.QueryParams.Count > 0)
            {
                baseUri.Append("?");
                var first = request.QueryParams.First();
                baseUri.Append(first.Key.UrlEncoded()).Append("=").Append(first.Value.UrlEncoded());
                request.QueryParams.Skip(1).ForEach(kv => baseUri.Append("&").Append(kv.Key.UrlEncoded()).Append("=").Append(kv.Value.UrlEncoded()));
            }
            var targetUri = new Uri(baseUri.ToString());

            var req = (HttpWebRequest)WebRequest.Create(targetUri);

            req.KeepAlive   = true;
            req.Method      = request.Method;
            req.Credentials = CredentialCache.DefaultCredentials;

            if (!string.IsNullOrWhiteSpace(request.ContentType))
            {
                req.ContentType = request.ContentType;
            }

            if (!request.Cache)
            {
                req.Headers.Set(RiakConstants.Rest.HttpHeaders.DisableCacheKey, RiakConstants.Rest.HttpHeaders.DisableCacheValue);
            }

            request.Headers.ForEach(h => req.Headers.Set(h.Key, h.Value));

            if (request.Body != null && request.Body.Length > 0)
            {
                req.ContentLength = request.Body.Length;
                using (var writer = req.GetRequestStream())
                {
                    writer.Write(request.Body, 0, request.Body.Length);
                }
            }
            else
            {
                req.ContentLength = 0;
            }

            try
            {
                var response = (HttpWebResponse)req.GetResponse();

                var result = new RiakRestResponse
                {
                    ContentLength   = response.ContentLength,
                    ContentType     = response.ContentType,
                    StatusCode      = response.StatusCode,
                    Headers         = response.Headers.AllKeys.ToDictionary(k => k, k => response.Headers[k]),
                    ContentEncoding = !string.IsNullOrWhiteSpace(response.ContentEncoding)
                        ? Encoding.GetEncoding(response.ContentEncoding)
                        : Encoding.Default
                };

                if (response.ContentLength > 0)
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                        {
                            using (var reader = new StreamReader(responseStream, result.ContentEncoding))
                            {
                                result.Body = reader.ReadToEnd();
                            }
                        }
                    }
                }

                return(RiakResult <RiakRestResponse> .Success(result));
            }
            catch (RiakException ex)
            {
                return(RiakResult <RiakRestResponse> .Error(ResultCode.CommunicationError, ex.Message, ex.NodeOffline));
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    return(RiakResult <RiakRestResponse> .Error(ResultCode.HttpError, ex.Message, false));
                }

                return(RiakResult <RiakRestResponse> .Error(ResultCode.HttpError, ex.Message, true));
            }
            catch (Exception ex)
            {
                return(RiakResult <RiakRestResponse> .Error(ResultCode.CommunicationError, ex.Message, true));
            }
        }
Esempio n. 18
0
        public override void SetUp()
        {
            Result = RiakResult <RpbPingResp> .Error(ResultCode.CommunicationError);

            base.SetUp();
        }
Esempio n. 19
0
        public Task <RiakResult <RiakRestResponse> > RestRequest(RiakRestRequest request)
        {
            var baseUri = new StringBuilder(_restRootUrl).Append(request.Uri);

            if (request.QueryParams.Count > 0)
            {
                baseUri.Append("?");
                var first = request.QueryParams.First();
                baseUri.Append(first.Key.UrlEncoded()).Append("=").Append(first.Value.UrlEncoded());
                request.QueryParams.Skip(1).ForEach(kv => baseUri.Append("&").Append(kv.Key.UrlEncoded()).Append("=").Append(kv.Value.UrlEncoded()));
            }
            var targetUri = new Uri(baseUri.ToString());

            var req = (HttpWebRequest)WebRequest.Create(targetUri);

            req.KeepAlive   = true;
            req.Method      = request.Method;
            req.Credentials = CredentialCache.DefaultCredentials;

            if (!string.IsNullOrWhiteSpace(request.ContentType))
            {
                req.ContentType = request.ContentType;
            }

            if (!request.Cache)
            {
                req.Headers.Set(RiakConstants.Rest.HttpHeaders.DisableCacheKey, RiakConstants.Rest.HttpHeaders.DisableCacheValue);
            }

            request.Headers.ForEach(h => req.Headers.Set(h.Key, h.Value));

            if (request.Body != null && request.Body.Length > 0)
            {
                req.ContentLength = request.Body.Length;
                using (var writer = req.GetRequestStream())
                {
                    writer.Write(request.Body, 0, request.Body.Length);
                }
            }
            else
            {
                req.ContentLength = 0;
            }

            // process response
            var asyncResult = req.BeginGetResponse(null, null);

            return(Task <WebResponse> .Factory.FromAsync(asyncResult, req.EndGetResponse)
                   .ContinueWith((Task <WebResponse> responseTask) => {
                // rethrow exception on fault
                if (responseTask.IsFaulted)
                {
                    throw responseTask.Exception;
                }

                // pull out response
                var response = (HttpWebResponse)responseTask.Result;
                var result = new RiakRestResponse()
                {
                    ContentLength = response.ContentLength,
                    ContentType = response.ContentType,
                    StatusCode = response.StatusCode,
                    Headers = response.Headers.AllKeys.ToDictionary(k => k, k => response.Headers[k]),
                    ContentEncoding = !string.IsNullOrWhiteSpace(response.ContentEncoding)
                            ? Encoding.GetEncoding(response.ContentEncoding)
                            : Encoding.Default
                };

                if (response.ContentLength > 0)
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                        {
                            return ReadStreamAsync(responseStream, (int)response.ContentLength)
                            .ContinueWith((Task <byte[]> readTask) => {
                                result.Body = result.ContentEncoding.GetString(readTask.Result);
                                return result;
                            });
                        }
                    }
                }

                // end of the line
                throw new RiakException("Couldn't read HTTP response", false);
            }).Unwrap()
                   .ContinueWith((Task <RiakRestResponse> responseTask) => {
                if (!responseTask.IsFaulted)
                {
                    return RiakResult <RiakRestResponse> .Success(responseTask.Result);
                }
                else
                {
                    var exceptions = responseTask.Exception.Flatten().InnerExceptions;
                    var rEx = exceptions.OfType <RiakException>().FirstOrDefault();
                    var wEx = exceptions.OfType <WebException>().FirstOrDefault();

                    // process exceptions
                    if (rEx != null)
                    {
                        return RiakResult <RiakRestResponse> .Error(ResultCode.CommunicationError, rEx.Message, rEx.NodeOffline);
                    }
                    else if (wEx != null)
                    {
                        if (wEx.Status == WebExceptionStatus.ProtocolError)
                        {
                            return RiakResult <RiakRestResponse> .Error(ResultCode.HttpError, wEx.Message, false);
                        }

                        return RiakResult <RiakRestResponse> .Error(ResultCode.HttpError, wEx.Message, true);
                    }
                    else
                    {
                        var ex = exceptions.FirstOrDefault();
                        return RiakResult <RiakRestResponse> .Error(ResultCode.CommunicationError, ex.Message, true);
                    }
                }
            }));
        }