public HttpResponseMessage GetRange(HttpRequestMessage request, RangeRequest <Customer> data)
        {
            try
            {
                IEnumerable <Customer> customers = null;
                string filterKey = GetCookieValue(request, AppConstants.LOGIN);

                if (FilteredCustomers.ContainsKey(filterKey))
                {
                    customers = FilteredCustomers[filterKey];
                }
                else
                {
                    customers = _unitOfWork.Customers.GetAll();
                }

                int total = customers.Count();
                customers = SortCustomers(customers, data);
                customers = customers
                            .Skip((data.PageNum > 0 ? (data.PageNum - 1) : data.PageNum) * data.PageSize)
                            .Take(data.PageSize);

                Paginator <Customer> paginator = new Paginator <Customer>()
                {
                    Items         = customers,
                    PageNum       = data.PageNum,
                    AllItemsCount = total,
                    AllPagesCount = (int)Math.Ceiling((decimal)total / (decimal)data.PageSize)
                };

                return(request.CreateResponse <Paginator <Customer> >(HttpStatusCode.OK, paginator));
            }
            catch (Exception ex) { return(ProcessError(ex, request)); }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public async Task <RangeResponse> GetAsync(RangeRequest request, Metadata headers = null)
        {
            RangeResponse rangeResponse = new RangeResponse();
            bool          success       = false;
            int           retryCount    = 0;

            while (!success)
            {
                try
                {
                    rangeResponse = await _balancer.GetConnection().kvClient.RangeAsync(request, headers);

                    success = true;
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable)
                {
                    retryCount++;
                    if (retryCount >= _balancer._numNodes)
                    {
                        throw ex;
                    }
                }
            }

            return(rangeResponse);
        }
Ejemplo n.º 3
0
        public void Constructor_CompleteRange_Valid()
        {
            int max   = 10;
            var range = new RangeRequest(0, max - 1, max);

            Assert.True(range.Start == 0 && range.End == max - 1 && range.Maximum == max);
        }
Ejemplo n.º 4
0
 public override async Task Range(RangeRequest request, IServerStreamWriter <Int32Value> responseStream, ServerCallContext context)
 {
     foreach (var i in Enumerable.Range(request.Start, request.Count))
     {
         await responseStream.WriteAsync(new Int32Value { Value = i });
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <param name="request">The request to send to the server.</param>
        /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param>
        /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param>
        /// <param name="cancellationToken">An optional token for canceling the call.</param>
        /// <returns>The etcd response for the specified request</returns>
        public async Task <RangeResponse> GetAsync(RangeRequest request, Grpc.Core.Metadata headers = null,
                                                   DateTime?deadline = null,
                                                   CancellationToken cancellationToken = default)
        {
            RangeResponse rangeResponse = new RangeResponse();
            bool          success       = false;
            int           retryCount    = 0;

            while (!success)
            {
                try
                {
                    rangeResponse = await _balancer.GetConnection().kvClient
                                    .RangeAsync(request, headers, deadline, cancellationToken);

                    success = true;
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable)
                {
                    retryCount++;
                    if (retryCount >= _balancer._numNodes)
                    {
                        throw;
                    }
                }
            }

            return(rangeResponse);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Get the etcd response for a specified key in async
 /// </summary>
 /// <param name="request">The request to send to the server.</param>
 /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param>
 /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param>
 /// <param name="cancellationToken">An optional token for canceling the call.</param>
 /// <returns>The etcd response for the specified request</returns>
 public async Task <RangeResponse> GetAsync(RangeRequest request, Grpc.Core.Metadata headers = null,
                                            DateTime?deadline = null,
                                            CancellationToken cancellationToken = default)
 {
     return(await CallEtcdAsync(async (connection) => await connection.kvClient
                                .RangeAsync(request, headers, deadline, cancellationToken)));
 }
Ejemplo n.º 7
0
        public ConvertedRange ConvertRange(RangeRequest range)
        {
            for (var i = range.Start; i <= range.End; i++)
            {
                _convertedRangeBuilder.AddElement(i);
            }

            return(_convertedRangeBuilder.Build());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Get the etcd response for a specified RangeRequest
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public RangeResponse Get(RangeRequest request, Metadata headers = null)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = _balancer.GetConnection().kvClient.Range(request, headers);


            return(rangeResponse);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public async Task <RangeResponse> GetAsync(RangeRequest request, Metadata headers = null)
        {
            RangeResponse rangeResponse = new RangeResponse();

            rangeResponse = await _balancer.GetConnection().kvClient.RangeAsync(request, headers);


            return(rangeResponse);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Get the value for a specified key in async
        /// </summary>
        /// <returns>The value for the specified key</returns>
        /// <param name="key">Key for which value need to be fetched</param>
        public async Task <string> GetValAsync(string key, Metadata headers = null)
        {
            var request = new RangeRequest
            {
                Key = ByteString.CopyFromUtf8(key)
            };
            var rangeResponse = await GetAsync(request, headers);

            return(rangeResponse.Count != 0 ? rangeResponse.Kvs[0].Value.ToStringUtf8().Trim() : string.Empty);
        }
Ejemplo n.º 11
0
        public void GetMultipartIntermediateHeader_Valid()
        {
            var rangeRequest = new RangeRequest(0, 511, 512);

            var expected = @"--0000_1111_Multipart_Boundary_1111_0000
            Content-Type: Test
            Content-Range: bytes 0-511/512

            ";
            Assert.Equal(expected, rangeRequest.GetMultipartIntermediateHeader("Test"));
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Gets the range of keys with the specified prefix in async
        /// </summary>
        /// <returns>Dictionary containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public async Task <IDictionary <string, string> > GetRangeValAsync(string prefixKey, Metadata headers = null)
        {
            string rangeEnd = EtcdUtil.GetRangeEnd(prefixKey);
            var    request  = new RangeRequest
            {
                Key      = ByteString.CopyFromUtf8(prefixKey),
                RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
            };
            var rangeResponse = await GetAsync(request, headers);

            return(EtcdUtil.RangeRespondToDictionary(rangeResponse));
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Gets the range of keys with the specified prefix in async
        /// </summary>
        /// <returns>RangeResponse containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public async Task <RangeResponse> GetRangeAsync(string prefixKey, Metadata headers = null)
        {
            string rangeEnd = EtcdUtil.GetRangeEnd(prefixKey);
            var    request  = new RangeRequest
            {
                Key      = ByteString.CopyFromUtf8(prefixKey),
                RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
            };
            var rangeResponse = await GetAsync(request, headers);

            return(rangeResponse);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Gets the range of keys with the specified prefix
        /// </summary>
        /// <returns>RangeResponse containing range of key-values</returns>
        /// <param name="prefixKey">Prefix key</param>
        public RangeResponse GetRange(string prefixKey, Metadata headers = null)
        {
            string rangeEnd = EtcdUtil.GetRangeEnd(prefixKey);
            var    request  = new RangeRequest
            {
                Key      = ByteString.CopyFromUtf8(prefixKey),
                RangeEnd = ByteString.CopyFromUtf8(rangeEnd)
            };
            var rangeResponse = Get(request, headers);

            return(rangeResponse);
        }
Ejemplo n.º 15
0
        public void GetMultipartIntermediateHeader_Valid()
        {
            var rangeRequest = new RangeRequest(0, 511, 512);

            var expected = @"--0000_1111_Multipart_Boundary_1111_0000
Content-Type: Test
Content-Range: bytes 0-511/512

";

            Assert.Equal(expected, rangeRequest.GetMultipartIntermediateHeader("Test"));
        }
Ejemplo n.º 16
0
            RequestOp toRequestOp()
            {
                RangeRequest range = new RangeRequest()
                {
                    Key        = this.key, CountOnly = this.option.IsCountOnly(),
                    Limit      = this.option.GetLimit(), Revision = this.option.GetRevision(),
                    KeysOnly   = this.option.IsKeysOnly(), Serializable = this.option.isSerializable(),
                    SortOrder  = OptionsUtil.ToRangeRequestSortOrder(this.option.GetSortOrder()),
                    SortTarget = OptionsUtil.ToRangeRequestSortTarget(this.option.GetSortField())
                };

                range.RangeEnd = ByteString.CopyFrom(this.option.GetEndKey().getBytes());
                RequestOp op = new RequestOp();

                op.RequestRange = range;

                return(op);
            }
Ejemplo n.º 17
0
        /// <summary>
        /// Get the etcd response for a specified key in async
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public async Task <RangeResponse> GetAsync(RangeRequest request)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                rangeResponse = await _kvClient.RangeAsync(request, _headers);
            }
            catch (RpcException ex)
            {
                ResetConnection(ex);
                throw;
            }
            catch
            {
                throw;
            }

            return(rangeResponse);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Get the etcd response for a specified RangeRequest
        /// </summary>
        /// <param name="request"></param>
        /// <returns>The etcd response for the specified request</returns>
        public RangeResponse Get(RangeRequest request)
        {
            RangeResponse rangeResponse = new RangeResponse();

            try
            {
                rangeResponse = _kvClient.Range(request, _headers);
            }
            catch (RpcException ex)
            {
                ResetConnection(ex);
                throw;
            }
            catch
            {
                throw;
            }

            return(rangeResponse);
        }
        public HttpResponseMessage ClearFilter(HttpRequestMessage request, RangeRequest <Customer> data)
        {
            try
            {
                string filterKey = GetCookieValue(request, AppConstants.LOGIN);

                if (FilteredCustomers.ContainsKey(filterKey))
                {
                    FilteredCustomers.Remove(filterKey);
                }

                return(GetRange(request, new RangeRequest <Customer>()
                {
                    PageNum = data.PageNum,
                    PageSize = data.PageSize,
                    FieldName = data.FieldName,
                    Direction = data.Direction
                }));
            }
            catch (Exception ex) { return(ProcessError(ex, request)); }
        }
Ejemplo n.º 20
0
 public void GetMultipartIntermediateHeader_ThrowsOnNullContentType()
 {
     var rangeRequest = new RangeRequest(0, 1, 2);
     Assert.Throws<ArgumentNullException>(() => rangeRequest.GetMultipartIntermediateHeader(null));
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Get the etcd response for a specified RangeRequest
 /// </summary>
 /// <param name="request">The request to send to the server.</param>
 /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param>
 /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param>
 /// <param name="cancellationToken">An optional token for canceling the call.</param>
 /// <returns>The etcd response for the specified request</returns>
 public RangeResponse Get(RangeRequest request, Grpc.Core.Metadata headers = null, DateTime?deadline = null,
                          CancellationToken cancellationToken = default)
 {
     return(CallEtcd((connection) => connection.kvClient
                     .Range(request, headers, deadline, cancellationToken)));
 }
Ejemplo n.º 22
0
        private void EnqueueRequest(int aIndex, ERequestPriority aPriority)
        {
            if (!iDisposed)
            {
                Assert.Check(iOpened);
                Assert.Check(aIndex >= 0 && aIndex < iCount);
                T item = default(T);
                if (!iCache.TryGet(aIndex, out item))
                {
                    lock (iQueueLock)
                    {
                        // get the correct start index for this request's chunk
                        int startIndex = iRangeSize * (aIndex / iRangeSize);

                        // first ensure that the request is not currently being executed
                        if (iExecutingRequests.ContainsKey(startIndex))
                        {
                            return;
                        }

                        // enqueue the request according to its priority (readahead requests are treated as slightly lower priority).
                        int          insertIndex     = 0;
                        int          existingIndex   = -1;
                        RangeRequest existingRequest = null;

                        for (int i = 0; i < iQueuedRequests.Count; i++)
                        {
                            RangeRequest current = iQueuedRequests[i];
                            if (current.Priority <= aPriority)
                            {
                                insertIndex++;
                            }
                            if (iQueuedRequests[i].StartIndex == startIndex)
                            {
                                existingIndex   = i;
                                existingRequest = current;
                            }
                            if (existingRequest != null && current.Priority > aPriority)
                            {
                                break;
                            }
                        }
                        if (existingRequest != null)
                        {
                            // if it is already enqueued, but has been re-enqueued at a higher priority, move it up the queue
                            if (insertIndex - 1 > existingIndex)
                            {
                                existingRequest.Priority = aPriority;
                                iQueuedRequests.RemoveAt(existingIndex);
                                iQueuedRequests.Insert(insertIndex - 1, existingRequest);
                            }
                        }
                        else
                        {
                            Assert.Check(insertIndex <= iQueuedRequests.Count);
                            iQueuedRequests.Insert(insertIndex, new RangeRequest()
                            {
                                Priority = aPriority, StartIndex = startIndex
                            });
                            iDequeueScheduler.Schedule(new Scheduler.DCallback(DoDequeueRequest));
                        }
                    }
                }
            }
        }
Ejemplo n.º 23
0
        public List <T> Range <T>(RangeRequest request)
        {
            var list = Range(request);

            return(ToGenericList <T>(list));
        }
Ejemplo n.º 24
0
        public List <Document> Range(RangeRequest request)
        {
            var list = _protocol.Range(request);

            return(list.Cast <Document>().ToList());
        }
Ejemplo n.º 25
0
        public void Constructor_PartialRange_Valid()
        {
            var range = new RangeRequest(0, 4, 20);

            Assert.True(range.Start == 0 && range.End == 4 && range.Maximum == 20);
        }
Ejemplo n.º 26
0
        public void GetMultipartIntermediateHeader_ThrowsOnNullContentType()
        {
            var rangeRequest = new RangeRequest(0, 1, 2);

            Assert.Throws <ArgumentNullException>(() => rangeRequest.GetMultipartIntermediateHeader(null));
        }
Ejemplo n.º 27
0
        public void GetMultipartIntermediateHeader_ThrowsOnWhiteSpaceContentType()
        {
            var rangeRequest = new RangeRequest(0, 1, 2);

            Assert.Throws <ArgumentException>(() => rangeRequest.GetMultipartIntermediateHeader(string.Empty));
        }
Ejemplo n.º 28
0
        public ActionResult <ConvertedRange> Get(RangeRequest range)
        {
            var convertedRange = _convertRangeService.ConvertRange(range);

            return(convertedRange);
        }
Ejemplo n.º 29
0
 public void Constructor_CompleteRange_Valid()
 {
     int max = 10;
     var range = new RangeRequest(0, max - 1, max);
     Assert.True(range.Start == 0 && range.End == max - 1 && range.Maximum == max);
 }
Ejemplo n.º 30
0
 public void Constructor_PartialRange_Valid()
 {
     var range = new RangeRequest(0, 4, 20);
     Assert.True(range.Start == 0 && range.End == 4 && range.Maximum == 20);
 }
Ejemplo n.º 31
0
 public void GetMultipartIntermediateHeader_ThrowsOnWhiteSpaceContentType()
 {
     var rangeRequest = new RangeRequest(0, 1, 2);
     Assert.Throws<ArgumentException>(() => rangeRequest.GetMultipartIntermediateHeader(string.Empty));
 }