Ejemplo n.º 1
0
        /// <summary>
        /// increment counter
        /// </summary>
        /// <param name="bucket"></param>
        /// <param name="key"></param>
        /// <param name="amount"></param>
        /// <param name="setOptions"></param>
        /// <param name="millisecondsReceiveTimeout"></param>
        /// <param name="asyncState"></param>
        /// <returns></returns>
        public Task <long?> Increment(string bucket,
                                      byte[] key,
                                      long amount,
                                      Action <CounterUpdateOptions> setOptions = null,
                                      int millisecondsReceiveTimeout           = 3000,
                                      object asyncState = null)
        {
            var source = new TaskCompletionSource <long?>(asyncState);
            var req    = new RpbCounterUpdateReq {
                bucket = bucket.GetBytes(), key = key, amount = amount, returnvalue = true
            };

            if (setOptions != null)
            {
                setOptions(new CounterUpdateOptions(req));
            }

            this._socket.Execute <RpbCounterUpdateReq, RpbCounterUpdateResp>(Codes.CounterUpdateReq, Codes.CounterUpdateResp, req,
                                                                             ex => source.TrySetException(ex),
                                                                             result =>
            {
                if (result == null)
                {
                    source.TrySetResult(null);
                }
                else
                {
                    source.TrySetResult(result.returnvalue);
                }
            }, millisecondsReceiveTimeout);
            return(source.Task);
        }
Ejemplo n.º 2
0
        public async Task <Either <RiakException, RiakCounterResult> > IncrementCounter(string bucket, string counter, long amount, RiakCounterUpdateOptions options = null)
        {
            if (!IsValidBucketOrKey(bucket))
            {
                return(new Either <RiakException, RiakCounterResult>(new RiakException((uint)ResultCode.InvalidRequest, InvalidBucketErrorMessage, false)));
            }

            if (!IsValidBucketOrKey(counter))
            {
                return(new Either <RiakException, RiakCounterResult>(new RiakException((uint)ResultCode.InvalidRequest, InvalidKeyErrorMessage, false)));
            }

            var request = new RpbCounterUpdateReq {
                bucket = bucket.ToRiakString(), key = counter.ToRiakString(), amount = amount
            };

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

            try
            {
                var result = await _connection.PbcWriteRead <RpbCounterUpdateReq, RpbCounterUpdateResp>(_endPoint, request)
                             .ConfigureAwait(false);

                var riakObject  = new RiakObject(bucket, counter, result.returnvalue);
                var cVal        = 0L;
                var parseResult = false;

                if (options.ReturnValue != null && options.ReturnValue.Value)
                {
                    parseResult = long.TryParse(riakObject.Value.FromRiakString(), out cVal);
                }

                return(new Either <RiakException, RiakCounterResult>(new RiakCounterResult(riakObject, parseResult ? (long?)cVal : null)));
            }
            catch (RiakException riakException)
            {
                return(new Either <RiakException, RiakCounterResult>(riakException));
            }
        }
        internal void Populate(RpbCounterUpdateReq request)
        {
            if (W != null)
            {
                request.w = W;
            }

            if (Dw != null)
            {
                request.dw = Dw;
            }

            if (Pw != null)
            {
                request.pw = Pw;
            }

            if (ReturnValue.HasValue)
            {
                request.returnvalue = ReturnValue.Value;
            }
        }