Exemple #1
0
        /// <summary>
        /// Sets the <see cref="CorrugatedIron.Models.RiakBucketProperties"/> properties of a <paramref name="bucket"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="CorrugatedIron.RiakResult"/> detailing the success or failure of the operation.
        /// </returns>
        /// <param name='bucket'>
        /// The Bucket.
        /// </param>
        /// <param name='properties'>
        /// The Properties.
        /// </param>
        public RiakResult SetBucketProperties(string bucket, RiakBucketProperties properties)
        {
            if (properties.CanUsePbc)
            {
                var request = new RpbSetBucketReq {
                    Bucket = bucket.ToRiakString(), Props = properties.ToMessage()
                };
                var result = UseConnection(conn => conn.PbcWriteRead <RpbSetBucketReq, RpbSetBucketResp>(request));
                return(result);
            }
            else
            {
                var request = new RiakRestRequest(ToBucketUri(bucket), RiakConstants.Rest.HttpMethod.Put)
                {
                    Body        = properties.ToJsonString().ToRiakString(),
                    ContentType = RiakConstants.ContentTypes.ApplicationJson
                };

                var result = UseConnection(conn => conn.RestRequest(request));
                if (result.IsSuccess && result.Value.StatusCode != HttpStatusCode.NoContent)
                {
                    return(RiakResult.Error(ResultCode.InvalidResponse, "Unexpected Status Code: {0} ({1})".Fmt(result.Value.StatusCode, (int)result.Value.StatusCode)));
                }
                return(result);
            }
        }
        internal static Task <RiakResult> SuccessTask()
        {
            var source = new TaskCompletionSource <RiakResult>();

            source.SetResult(RiakResult.Success());
            return(source.Task);
        }
Exemple #3
0
        /// <summary>
        /// Persist a <see cref="CorrugatedIron.Models.RiakObject"/> to Riak using the specific <see cref="CorrugatedIron.Models.RiakPutOptions" />.
        /// </summary>
        /// <param name='value'>
        /// The <see cref="CorrugatedIron.Models.RiakObject"/> to save.
        /// </param>
        /// <param name='options'>
        /// Put options
        /// </param>
        public RiakResult <RiakObject> Put(RiakObject value, RiakPutOptions options = null)
        {
            options = options ?? new RiakPutOptions();

            var request = value.ToMessage();

            options.Populate(request);

            var result = UseConnection(conn => conn.PbcWriteRead <RpbPutReq, RpbPutResp>(request));

            if (!result.IsSuccess)
            {
                return(RiakResult <RiakObject> .Error(result.ResultCode, result.ErrorMessage));
            }

            var finalResult = options.ReturnBody
                ? new RiakObject(value.Bucket, value.Key, result.Value.Content.First(), result.Value.VectorClock)
                : value;

            if (options.ReturnBody && result.Value.Content.Count > 1)
            {
                finalResult.Siblings = result.Value.Content.Select(c =>
                                                                   new RiakObject(value.Bucket, value.Key, c, result.Value.VectorClock)).ToList();
            }

            value.MarkClean();

            return(RiakResult <RiakObject> .Success(finalResult));
        }
Exemple #4
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);
            }
        }
        public override RiakResult <IEnumerable <TResult> > UseDelayedConnection <TResult>(Func <IRiakConnection, Action, RiakResult <IEnumerable <TResult> > > useFun, int retryAttempts)
        {
            if (retryAttempts < 0)
            {
                return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.NoRetries, "Unable to access a connection on the cluster.", true));
            }
            if (_disposing)
            {
                return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.ShuttingDown, "System currently shutting down", true));
            }

            var node = _node;

            if (node != null)
            {
                var result = node.UseDelayedConnection(useFun);
                if (!result.IsSuccess)
                {
                    Thread.Sleep(RetryWaitTime);
                    return(UseDelayedConnection(useFun, retryAttempts - 1));
                }
                return(result);
            }
            return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.ClusterOffline, "Unable to access functioning Riak node", true));
        }
Exemple #6
0
        public override RiakResult <IEnumerable <TResult> > UseDelayedConnection <TResult>(byte[] clientId, Func <IRiakConnection, Action, RiakResult <IEnumerable <TResult> > > useFun, int retryAttempts)
        {
            if (retryAttempts < 0)
            {
                return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.NoRetries, "Unable to access a connection on the cluster."));
            }
            if (_disposing)
            {
                return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.ShuttingDown, "System currently shutting down"));
            }

            var node = _loadBalancer.SelectNode();

            if (node != null)
            {
                var result = node.UseDelayedConnection(clientId, useFun);
                if (!result.IsSuccess)
                {
                    if (result.ResultCode == ResultCode.NoConnections)
                    {
                        Thread.Sleep(RetryWaitTime);
                        return(UseDelayedConnection(clientId, useFun, retryAttempts - 1));
                    }

                    if (result.ResultCode == ResultCode.CommunicationError)
                    {
                        DeactivateNode(node);
                        Thread.Sleep(RetryWaitTime);
                        return(UseDelayedConnection(clientId, useFun, retryAttempts - 1));
                    }
                }
                return(result);
            }
            return(RiakResult <IEnumerable <TResult> > .Error(ResultCode.ClusterOffline, "Unable to access functioning Riak node"));
        }
Exemple #7
0
        private Task <RiakResult <IEnumerable <RiakResult> > > Delete(IRiakConnection conn,
                                                                      IEnumerable <RiakObjectId> objectIds,
                                                                      RiakDeleteOptions options = null)
        {
            options = options ?? new RiakDeleteOptions();
            return(AfterAll(objectIds.Select(id => {
                if (!IsValidBucketOrKey(id.Bucket))
                {
                    return RiakResult.ErrorTask(ResultCode.InvalidRequest, InvalidBucketErrorMessage, false);
                }

                if (!IsValidBucketOrKey(id.Key))
                {
                    return RiakResult.ErrorTask(ResultCode.InvalidRequest, InvalidKeyErrorMessage, false);
                }

                var req = new RpbDelReq {
                    bucket = id.Bucket.ToRiakString(), key = id.Key.ToRiakString()
                };
                options.Populate(req);
                return conn.PbcWriteRead(req, MessageCode.DelResp);
            })).ContinueWith((Task <IEnumerable <RiakResult> > finishedTask) => {
                return RiakResult <IEnumerable <RiakResult> > .Success(finishedTask.Result);
            }));
        }
Exemple #8
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);
        }
        internal static Task <RiakResult> ErrorTask(ResultCode code, string message, bool nodeOffline)
        {
            var source = new TaskCompletionSource <RiakResult>();

            source.SetResult(RiakResult.Error(code, message, nodeOffline));
            return(source.Task);
        }
Exemple #10
0
        private static RiakResult <IEnumerable <RiakResult> > Delete(IRiakConnection conn,
                                                                     IEnumerable <RiakObjectId> objectIds, RiakDeleteOptions options = null)
        {
            options = options ?? new RiakDeleteOptions();

            var responses = objectIds.Select(id =>
            {
                if (!IsValidBucketOrKey(id.Bucket))
                {
                    return(RiakResult.Error(ResultCode.InvalidRequest, InvalidBucketErrorMessage, false));
                }

                if (!IsValidBucketOrKey(id.Key))
                {
                    return(RiakResult.Error(ResultCode.InvalidRequest, InvalidKeyErrorMessage, false));
                }

                var req = new RpbDelReq {
                    bucket = id.Bucket.ToRiakString(), key = id.Key.ToRiakString()
                };
                options.Populate(req);
                return(conn.PbcWriteRead(req, MessageCode.DelResp));
            }).ToList();

            return(RiakResult <IEnumerable <RiakResult> > .Success(responses));
        }
Exemple #11
0
        public Task <RiakResult> SetBucketProperties(string bucket, RiakBucketProperties properties)
        {
            if (!IsValidBucketOrKey(bucket))
            {
                return(RiakResult.ErrorTask(ResultCode.InvalidRequest, InvalidBucketErrorMessage, false));
            }

            if (properties.CanUsePbc)
            {
                var request = new RpbSetBucketReq {
                    bucket = bucket.ToRiakString(), props = properties.ToMessage()
                };
                return(UseConnection(conn => conn.PbcWriteRead(request, MessageCode.SetBucketResp)));
            }
            else
            {
                var request = new RiakRestRequest(ToBucketUri(bucket), RiakConstants.Rest.HttpMethod.Put)
                {
                    Body        = properties.ToJsonString().ToRiakString(),
                    ContentType = RiakConstants.ContentTypes.ApplicationJson
                };

                return(UseConnection(conn => conn.RestRequest(request))
                       .ContinueWith((Task <RiakResult <RiakRestResponse> > finishedTask) => {
                    var result = finishedTask.Result;
                    if (result.IsSuccess && result.Value.StatusCode != HttpStatusCode.NoContent)
                    {
                        return RiakResult.Error(ResultCode.InvalidResponse, "Unexpected Status Code: {0} ({1})".Fmt(result.Value.StatusCode, (int)result.Value.StatusCode), result.NodeOffline);
                    }
                    return result;
                }));
            }
        }
Exemple #12
0
        /// <summary>
        /// Get the specified <paramref name="key"/> from the <paramref name="bucket"/>.
        /// Optionally can be read from rVal instances. By default, the server's
        /// r-value will be used, but can be overridden by rVal.
        /// </summary>
        /// <param name='bucket'>
        /// The name of the bucket containing the <paramref name="key"/>
        /// </param>
        /// <param name='key'>
        /// The key.
        /// </param>
        /// <param name='options'>The <see cref="CorrugatedIron.Models.RiakGetOptions" /> responsible for
        /// configuring the semantics of this single get request. These options will override any previously
        /// defined bucket configuration properties.</param>
        /// <remarks>If a node does not respond, that does not necessarily mean that the
        /// <paramref name="bucket"/>/<paramref name="key"/> combination is not available. It simply means
        /// that fewer than R/PR nodes responded to the read request. See <see cref="CorrugatedIron.Models.RiakGetOptions" />
        /// for information on how different options change Riak's default behavior.
        /// </remarks>
        public RiakResult <RiakObject> Get(string bucket, string key, RiakGetOptions options = null)
        {
            if (!IsValidBucketOrKey(bucket))
            {
                return(RiakResult <RiakObject> .Error(ResultCode.InvalidRequest, InvalidBucketErrorMessage, false));
            }

            if (!IsValidBucketOrKey(key))
            {
                return(RiakResult <RiakObject> .Error(ResultCode.InvalidRequest, InvalidKeyErrorMessage, false));
            }

            var request = new RpbGetReq {
                bucket = bucket.ToRiakString(), key = key.ToRiakString()
            };

            options = options ?? new RiakGetOptions();
            options.Populate(request);

            var result = UseConnection(conn => conn.PbcWriteRead <RpbGetReq, RpbGetResp>(request));

            if (!result.IsSuccess)
            {
                return(RiakResult <RiakObject> .Error(result.ResultCode, result.ErrorMessage, result.NodeOffline));
            }

            if (result.Value.vclock == null)
            {
                return(RiakResult <RiakObject> .Error(ResultCode.NotFound, "Unable to find value in Riak", false));
            }

            var o = new RiakObject(bucket, key, result.Value.content, result.Value.vclock);

            return(RiakResult <RiakObject> .Success(o));
        }
        public override Task <RiakResult <IEnumerable <TResult> > > UseDelayedConnection <TResult>(Func <IRiakConnection, Action, Task <RiakResult <IEnumerable <TResult> > > > useFun, int retryAttempts)
        {
            if (retryAttempts < 0)
            {
                return(RiakResult <IEnumerable <TResult> > .ErrorTask(ResultCode.NoRetries, "Unable to access a connection on the cluster.", true));
            }
            if (_disposing)
            {
                return(RiakResult <IEnumerable <TResult> > .ErrorTask(ResultCode.ShuttingDown, "System currently shutting down", true));
            }

            var node = _node;

            if (node != null)
            {
                return(node.UseDelayedConnection(useFun)
                       .ContinueWith((Task <RiakResult <IEnumerable <TResult> > > finishedTask) => {
                    var result = finishedTask.Result;
                    if (!result.IsSuccess)
                    {
                        return TaskDelay(RetryWaitTime).ContinueWith(finishedDelayTask => {
                            return UseDelayedConnection(useFun, retryAttempts - 1);
                        }).Unwrap();
                    }
                    return TaskResult(result);
                }).Unwrap());
            }
            return(RiakResult <IEnumerable <TResult> > .ErrorTask(ResultCode.ClusterOffline, "Unable to access functioning Riak node", true));
        }
Exemple #14
0
        /// <summary>
        /// Retrieve multiple objects from Riak.
        /// </summary>
        /// <param name='bucketKeyPairs'>
        /// An <see href="System.Collections.Generic.IEnumerable&lt;T&gt;"/> of <see cref="CorrugatedIron.Models.RiakObjectId"/> to be retrieved
        /// </param>
        /// <param name='options'>The <see cref="CorrugatedIron.Models.RiakGetOptions" /> responsible for
        /// configuring the semantics of this single get request. These options will override any previously
        /// defined bucket configuration properties.</param>
        /// <returns>An <see cref="System.Collections.Generic.IEnumerable{T}"/> of <see cref="RiakResult{T}"/>
        /// is returned. You should verify the success or failure of each result separately.</returns>
        /// <remarks>Riak does not support multi get behavior. CorrugatedIron's multi get functionality wraps multiple
        /// get requests and returns results as an IEnumerable{RiakResult{RiakObject}}. Callers should be aware that
        /// this may result in partial success - all results should be evaluated individually in the calling application.
        /// In addition, applications should plan for multiple failures or multiple cases of siblings being present.</remarks>
        public IEnumerable <RiakResult <RiakObject> > Get(IEnumerable <RiakObjectId> bucketKeyPairs,
                                                          RiakGetOptions options = null)
        {
            bucketKeyPairs = bucketKeyPairs.ToList();

            options = options ?? new RiakGetOptions();

            var results = UseConnection(conn =>
            {
                var responses = bucketKeyPairs.Select(bkp =>
                {
                    // modified closure FTW
                    var bk = bkp;
                    if (!IsValidBucketOrKey(bk.Bucket))
                    {
                        return(RiakResult <RpbGetResp> .Error(ResultCode.InvalidRequest, InvalidBucketErrorMessage, false));
                    }

                    if (!IsValidBucketOrKey(bk.Key))
                    {
                        return(RiakResult <RpbGetResp> .Error(ResultCode.InvalidRequest, InvalidKeyErrorMessage, false));
                    }

                    var req = new RpbGetReq {
                        bucket = bk.Bucket.ToRiakString(), key = bk.Key.ToRiakString()
                    };
                    options.Populate(req);

                    return(conn.PbcWriteRead <RpbGetReq, RpbGetResp>(req));
                }).ToList();
                return(RiakResult <IEnumerable <RiakResult <RpbGetResp> > > .Success(responses));
            });

            return(results.Value.Zip(bucketKeyPairs, Tuple.Create).Select(result =>
            {
                if (!result.Item1.IsSuccess)
                {
                    return RiakResult <RiakObject> .Error(result.Item1.ResultCode, result.Item1.ErrorMessage, result.Item1.NodeOffline);
                }

                if (result.Item1.Value.vclock == null)
                {
                    return RiakResult <RiakObject> .Error(ResultCode.NotFound, "Unable to find value in Riak", false);
                }

                var o = new RiakObject(result.Item2.Bucket, result.Item2.Key, result.Item1.Value.content.First(), result.Item1.Value.vclock);

                if (result.Item1.Value.content.Count > 1)
                {
                    o.Siblings = result.Item1.Value.content.Select(c =>
                                                                   new RiakObject(result.Item2.Bucket, result.Item2.Key, c, result.Item1.Value.vclock)).ToList();
                }

                return RiakResult <RiakObject> .Success(o);
            }));
        }
Exemple #15
0
        /// <summary>
        /// Get the server information from the connected cluster.
        /// </summary>
        /// <returns>Model containing information gathered from a node in the cluster.</returns>
        /// <remarks>This function will assume that all of the nodes in the cluster are running
        /// the same version of Riak. It will only get executed on a single node, and the content
        /// that is returned technically only relates to that node. All nodes in a cluster should
        /// run on the same version of Riak.</remarks>
        public RiakResult <RiakServerInfo> GetServerInfo()
        {
            var result = UseConnection(conn => conn.PbcWriteRead <RpbGetServerInfoReq, RpbGetServerInfoResp>(new RpbGetServerInfoReq()));

            if (result.IsSuccess)
            {
                return(RiakResult <RiakServerInfo> .Success(new RiakServerInfo(result.Value)));
            }
            return(RiakResult <RiakServerInfo> .Error(result.ResultCode, result.ErrorMessage));
        }
Exemple #16
0
        /// <summary>
        /// Lists all buckets available on the Riak cluster.
        /// </summary>
        /// <returns>
        /// An <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> of <see cref="string"/> bucket names.
        /// </returns>
        /// <remarks>Buckets provide a logical namespace for keys. Listing buckets requires folding over all keys in a cluster and
        /// reading a list of buckets from disk. This operation, while non-blocking in Riak 1.0 and newer, still produces considerable
        /// physical I/O and can take a long time.</remarks>
        public RiakResult <IEnumerable <string> > ListBuckets()
        {
            var result = UseConnection(conn => conn.PbcWriteRead <RpbListBucketsResp>(MessageCode.ListBucketsReq));

            if (result.IsSuccess)
            {
                var buckets = result.Value.buckets.Select(b => b.FromRiakString());
                return(RiakResult <IEnumerable <string> > .Success(buckets.ToList()));
            }
            return(RiakResult <IEnumerable <string> > .Error(result.ResultCode, result.ErrorMessage, result.NodeOffline));
        }
Exemple #17
0
        public override Task <RiakResult <IEnumerable <TResult> > > UseDelayedConnection <TResult>(Func <IRiakConnection, Action, Task <RiakResult <IEnumerable <TResult> > > > useFun, int retryAttempts)
        {
            if (retryAttempts < 0)
            {
                return(RiakResult <IEnumerable <TResult> > .ErrorTask(ResultCode.NoRetries, "Unable to access a connection on the cluster.", false));
            }
            if (_disposing)
            {
                return(RiakResult <IEnumerable <TResult> > .ErrorTask(ResultCode.ShuttingDown, "System currently shutting down", true));
            }

            // select a node
            var node = _loadBalancer.SelectNode();

            if (node != null)
            {
                return(node.UseDelayedConnection(useFun)
                       .ContinueWith((Task <RiakResult <IEnumerable <TResult> > > finishedTask) => {
                    var result = finishedTask.Result;
                    if (result.IsSuccess)
                    {
                        return TaskResult(result);
                    }
                    else
                    {
                        if (result.ResultCode == ResultCode.NoConnections)
                        {
                            return DelayTask(RetryWaitTime)
                            .ContinueWith(delayTask => {
                                return UseDelayedConnection(useFun, retryAttempts - 1);
                            }).Unwrap();
                        }
                        if (result.ResultCode == ResultCode.CommunicationError)
                        {
                            if (result.NodeOffline)
                            {
                                DeactivateNode(node);
                            }

                            return DelayTask(RetryWaitTime)
                            .ContinueWith(delayTask => {
                                return UseDelayedConnection(useFun, retryAttempts - 1);
                            }).Unwrap();
                        }
                    }

                    // out of options
                    return RiakResult <IEnumerable <TResult> > .ErrorTask(ResultCode.ClusterOffline, "Unable to access functioning Riak node", true);
                }).Unwrap());
            }

            // no functioning node
            return(RiakResult <IEnumerable <TResult> > .ErrorTask(ResultCode.ClusterOffline, "Unable to access functioning Riak node", true));
        }
Exemple #18
0
        private IEnumerable <RiakResult <RpbMapRedResp> > CondenseResponse(IEnumerable <RiakResult <RpbMapRedResp> > originalResponse)
        {
            var resultList = new List <RiakResult <RpbMapRedResp> >(originalResponse);

            if (resultList.Count() == 1)
            {
                return(resultList);
            }

            var newResponse = new List <RiakResult <RpbMapRedResp> >();
            RiakResult <RpbMapRedResp> previous = null;

            foreach (var current in resultList)
            {
                if (previous == null)
                {
                    newResponse.Add(RiakResult <RpbMapRedResp> .Success(current.Value));
                    previous = current;
                }
                else if (previous.Value.Phase == current.Value.Phase)
                {
                    var mrResp = new RpbMapRedResp {
                        Done = current.Value.Done
                    };


                    if (current.Value.Response != null)
                    {
                        var newLength = previous.Value.Response.Length + current.Value.Response.Length;

                        var newValue = new List <byte>(newLength);
                        newValue.AddRange(previous.Value.Response);
                        newValue.AddRange(current.Value.Response);

                        var index = newResponse.IndexOf(previous);

                        mrResp.Phase    = current.Value.Phase;
                        mrResp.Response = newValue.ToArray();

                        newResponse.Remove(previous);
                        newResponse.Insert(index, RiakResult <RpbMapRedResp> .Success(mrResp));

                        previous = newResponse.ElementAt(index);
                    }
                }
                else
                {
                    newResponse.Add(RiakResult <RpbMapRedResp> .Success(current.Value));
                    previous = current;
                }
            }

            return(newResponse);
        }
Exemple #19
0
        public RiakResult <RiakSearchResult> Search(RiakSearchRequest search)
        {
            var request  = search.ToMessage();
            var response = UseConnection(conn => conn.PbcWriteRead <RpbSearchQueryReq, RpbSearchQueryResp>(request));

            if (response.IsSuccess)
            {
                return(RiakResult <RiakSearchResult> .Success(new RiakSearchResult(response.Value)));
            }

            return(RiakResult <RiakSearchResult> .Error(response.ResultCode, response.ErrorMessage, response.NodeOffline));
        }
Exemple #20
0
 public Task <RiakResult <RiakServerInfo> > GetServerInfo()
 {
     return(UseConnection(conn => conn.PbcWriteRead <RpbGetServerInfoResp>(MessageCode.GetServerInfoReq))
            .ContinueWith((Task <RiakResult <RpbGetServerInfoResp> > finishedTask) => {
         var result = finishedTask.Result;
         if (result.IsSuccess)
         {
             return RiakResult <RiakServerInfo> .Success(new RiakServerInfo(result.Value));
         }
         return RiakResult <RiakServerInfo> .Error(result.ResultCode, result.ErrorMessage, result.NodeOffline);
     }));
 }
Exemple #21
0
        public RiakResult <RiakMapReduceResult> MapReduce(RiakMapReduceQuery query)
        {
            var request  = query.ToMessage();
            var response = UseConnection(conn => conn.PbcWriteRead <RpbMapRedReq, RpbMapRedResp>(request, r => r.IsSuccess && !r.Value.done));

            if (response.IsSuccess)
            {
                return(RiakResult <RiakMapReduceResult> .Success(new RiakMapReduceResult(response.Value)));
            }

            return(RiakResult <RiakMapReduceResult> .Error(response.ResultCode, response.ErrorMessage, response.NodeOffline));
        }
Exemple #22
0
        public RiakResult <RiakStreamedMapReduceResult> StreamMapReduce(RiakMapReduceQuery query)
        {
            var request  = query.ToMessage();
            var response = UseDelayedConnection((conn, onFinish) =>
                                                conn.PbcWriteStreamRead <RpbMapRedReq, RpbMapRedResp>(request, r => r.IsSuccess && !r.Value.Done, onFinish));

            if (response.IsSuccess)
            {
                return(RiakResult <RiakStreamedMapReduceResult> .Success(new RiakStreamedMapReduceResult(response.Value)));
            }
            return(RiakResult <RiakStreamedMapReduceResult> .Error(response.ResultCode, response.ErrorMessage));
        }
Exemple #23
0
 public Task <RiakResult <IEnumerable <string> > > ListBuckets()
 {
     return(UseConnection(conn => conn.PbcWriteRead <RpbListBucketsResp>(MessageCode.ListBucketsReq))
            .ContinueWith((Task <RiakResult <RpbListBucketsResp> > finishedTask) => {
         var result = finishedTask.Result;
         if (result.IsSuccess)
         {
             var buckets = result.Value.buckets.Select(b => b.FromRiakString());
             return RiakResult <IEnumerable <string> > .Success(buckets.ToList());
         }
         return RiakResult <IEnumerable <string> > .Error(result.ResultCode, result.ErrorMessage, result.NodeOffline);
     }));
 }
Exemple #24
0
        private static RiakResult <IEnumerable <RiakResult> > Delete(IRiakConnection conn,
                                                                     IEnumerable <RiakObjectId> objectIds, RiakDeleteOptions options = null)
        {
            options = options ?? new RiakDeleteOptions();
            var requests = objectIds.Select(id => new RpbDelReq {
                Bucket = id.Bucket.ToRiakString(), Key = id.Key.ToRiakString()
            }).ToList();

            requests.ForEach(r => options.Populate(r));

            var responses = requests.Select(conn.PbcWriteRead <RpbDelReq, RpbDelResp>).ToList();

            return(RiakResult <IEnumerable <RiakResult> > .Success(responses));
        }
Exemple #25
0
        public Task <RiakResult <RiakMapReduceResult> > MapReduce(RiakMapReduceQuery query)
        {
            var request = query.ToMessage();

            return(UseConnection(conn => conn.PbcWriteRead <RpbMapRedReq, RpbMapRedResp>(request, r => r.IsSuccess && !r.Value.done))
                   .ContinueWith((Task <RiakResult <IEnumerable <RiakResult <RpbMapRedResp> > > > finishedTask) => {
                var result = finishedTask.Result;
                if (result.IsSuccess)
                {
                    return RiakResult <RiakMapReduceResult> .Success(new RiakMapReduceResult(result.Value));
                }
                return RiakResult <RiakMapReduceResult> .Error(result.ResultCode, result.ErrorMessage, result.NodeOffline);
            }));
        }
Exemple #26
0
        public Task <RiakResult <RiakSearchResult> > Search(RiakSearchRequest search)
        {
            var request = search.ToMessage();

            return(UseConnection(conn => conn.PbcWriteRead <RpbSearchQueryReq, RpbSearchQueryResp>(request))
                   .ContinueWith((Task <RiakResult <RpbSearchQueryResp> > finishedTask) => {
                var result = finishedTask.Result;
                if (result.IsSuccess)
                {
                    return RiakResult <RiakSearchResult> .Success(new RiakSearchResult(result.Value));
                }

                return RiakResult <RiakSearchResult> .Error(result.ResultCode, result.ErrorMessage, result.NodeOffline);
            }));
        }
Exemple #27
0
        /// <summary>
        /// Deletes the contents of the specified <paramref name="bucket"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="System.Collections.Generic.IEnumerable&lt;T&gt;"/> of <see cref="CorrugatedIron.RiakResult"/> listing the success of all deletes
        /// </returns>
        /// <param name='bucket'>
        /// The bucket to be deleted.
        /// </param>
        /// <param name='deleteOptions'>
        /// Options for Riak delete operation <see cref="CorrugatedIron.Models.RiakDeleteOptions"/>
        /// </param>
        /// <remarks>
        /// <para>
        /// A delete bucket operation actually deletes all keys in the bucket individually.
        /// A <see cref="CorrugatedIron.RiakClient.ListKeys"/> operation is performed to retrieve a list of keys
        /// The keys retrieved from the <see cref="CorrugatedIron.RiakClient.ListKeys"/> are then deleted through
        /// <see cref="CorrugatedIron.RiakClient.Delete"/>.
        /// </para>
        /// <para>
        /// Because of the <see cref="CorrugatedIron.RiakClient.ListKeys"/> operation, this may be a time consuming operation on
        /// production systems and may cause memory problems for the client. This should be used either in testing or on small buckets with
        /// known amounts of data.
        /// </para>
        /// </remarks>
        public IEnumerable <RiakResult> DeleteBucket(string bucket, RiakDeleteOptions deleteOptions)
        {
            var results = UseConnection(conn =>
            {
                var keyResults = ListKeys(conn, bucket);
                if (keyResults.IsSuccess)
                {
                    var objectIds = keyResults.Value.Select(key => new RiakObjectId(bucket, key)).ToList();
                    return(Delete(conn, objectIds, deleteOptions));
                }
                return(RiakResult <IEnumerable <RiakResult> > .Error(keyResults.ResultCode, keyResults.ErrorMessage, keyResults.NodeOffline));
            });

            return(results.Value);
        }
Exemple #28
0
        public RiakResult <IEnumerable <string> > StreamListKeys(string bucket)
        {
            var lkReq = new RpbListKeysReq {
                Bucket = bucket.ToRiakString()
            };
            var result = UseDelayedConnection((conn, onFinish) =>
                                              conn.PbcWriteStreamRead <RpbListKeysReq, RpbListKeysResp>(lkReq, lkr => lkr.IsSuccess && !lkr.Value.Done, onFinish));

            if (result.IsSuccess)
            {
                var keys = result.Value.Where(r => r.IsSuccess).SelectMany(r => r.Value.KeyNames);
                return(RiakResult <IEnumerable <string> > .Success(keys));
            }
            return(RiakResult <IEnumerable <string> > .Error(result.ResultCode, result.ErrorMessage));
        }
Exemple #29
0
        private static RiakResult <IEnumerable <string> > ListKeys(IRiakConnection conn, string bucket)
        {
            var lkReq = new RpbListKeysReq {
                Bucket = bucket.ToRiakString()
            };
            var result = conn.PbcWriteRead <RpbListKeysReq, RpbListKeysResp>(lkReq,
                                                                             lkr => lkr.IsSuccess && !lkr.Value.Done);

            if (result.IsSuccess)
            {
                var keys = result.Value.Where(r => r.IsSuccess).SelectMany(r => r.Value.KeyNames).Distinct().ToList();
                return(RiakResult <IEnumerable <string> > .Success(keys));
            }
            return(RiakResult <IEnumerable <string> > .Error(result.ResultCode, result.ErrorMessage));
        }
Exemple #30
0
        /// <summary>
        /// Persist an <see href="System.Collections.Generic.IEnumerable&lt;T&gt;"/> of <see cref="CorrugatedIron.Models.RiakObjectId"/> to Riak.
        /// </summary>
        /// <param name='values'>
        /// The <see href="System.Collections.Generic.IEnumerable&lt;T&gt;"/> of <see cref="CorrugatedIron.Models.RiakObjectId"/> to save.
        /// </param>
        /// <param name='options'>
        /// Put options.
        /// </param>
        public IEnumerable <RiakResult <RiakObject> > Put(IEnumerable <RiakObject> values, RiakPutOptions options = null)
        {
            options = options ?? new RiakPutOptions();

            values = values.ToList();
            var messages = values.Select(v =>
            {
                var m = v.ToMessage();
                options.Populate(m);
                return(m);
            }).ToList();

            var results = UseConnection(conn =>
            {
                var responses = messages.Select(conn.PbcWriteRead <RpbPutReq, RpbPutResp>).ToList();
                return(RiakResult <IEnumerable <RiakResult <RpbPutResp> > > .Success(responses));
            });

            var resultsArray = results.Value.ToArray();

            for (var i = 0; i < resultsArray.Length; i++)
            {
                if (resultsArray[i].IsSuccess)
                {
                    values.ElementAt(i).MarkClean();
                }
            }

            return(results.Value.Zip(values, Tuple.Create).Select(t =>
            {
                if (t.Item1.IsSuccess)
                {
                    var finalResult = options.ReturnBody
                        ? new RiakObject(t.Item2.Bucket, t.Item2.Key, t.Item1.Value.Content.First(), t.Item1.Value.VectorClock)
                        : t.Item2;

                    if (options.ReturnBody && t.Item1.Value.Content.Count > 1)
                    {
                        finalResult.Siblings = t.Item1.Value.Content.Select(c =>
                                                                            new RiakObject(t.Item2.Bucket, t.Item2.Key, c, t.Item1.Value.VectorClock)).ToList();
                    }

                    return RiakResult <RiakObject> .Success(finalResult);
                }

                return RiakResult <RiakObject> .Error(t.Item1.ResultCode, t.Item1.ErrorMessage);
            }));
        }
        private void Poll(RiakResult<RiakStreamedMapReduceResult> result)
        {
            if (result.IsSuccess)
            {
                foreach (var phase in result.Value.PhaseResults)
                {
                    // make sure we get hold of the phase result which has data
                    if (phase.Value != null)
                    {
                        // deserialize into an array of messages
                        var messages = phase.GetObject<YakMessage[]>();

                        // throw them on screen
                        foreach (var m in messages)
                        {
                            DisplayMessage(m);
                            _since = m.timestamp + 1;
                        }
                    }
                }
            }

            // create the next map reduce job
            var pollQuery = new RiakMapReduceQuery()
                .Inputs("messages")
                .MapJs(m => m.BucketKey("yakmr", "mapMessageSince").Argument(_since))
                .ReduceJs(r => r.BucketKey("yakmr", "reduceSortTimestamp").Keep(true));

            // do the usual wait
            WaitRandom();

            // and off we go again.
            _riakClient.Async.StreamMapReduce(pollQuery, Poll);
        }
 public RiakPointerPersistenceException(string message, RiakResult<RiakObject> result)
     : base(message)
 {
     Result = result;
 }
 public RiakHeadPointerPersistenceException(string message, RiakResult<RiakObject> result)
     : base(message, result)
 {
 }
        static void HandleStreamingMapReduce(RiakResult<RiakStreamedMapReduceResult> streamingMRResult, EventWaitHandle handle)
        {
            System.Diagnostics.Debug.Assert(streamingMRResult.IsSuccess);
            foreach (var result in streamingMRResult.Value.PhaseResults)
            {
                Console.WriteLine("Handling async result ...");
                if (result.Phase == 1)
                {
                    System.Diagnostics.Debug.Assert(result.GetObjects<int[]>().First()[0] == 12);
                }
            }

            handle.Set();
        }