Ejemplo n.º 1
0
        private DataPage <TResource> ExecuteAdjustedQuery <TResource>(string query, Pagination pagination, SortingInstructions sorting, AttributesToFetch attributes = null) where TResource : RmResource
        {
            attributes = attributes ?? AttributesToFetch.All;

            Initialize();

            var ctx = LogContext.WithConfigFormat();

            _log.Debug(ctx.Format("Executing query {0} with paging: {1}, sorting {2} and attributes {3}"), query, pagination.ToJSON(), sorting.ToJSON(), attributes.GetNames().ToJSON());

            // first request - only to get enumeration context and total rows count
            var enumerationRequest = new EnumerationRequest(query);

            // do not fetch any items - total count only; however this cannot be set to 0 because then total count is not returned (is always set to 0)
            enumerationRequest.MaxElements = 1;
            // get only ObjectID attribute to minimize overhead caused by this operation which returns elements not used for further processing
            enumerationRequest.Selection = new List <string>
            {
                RmResource.AttributeNames.ObjectID.Name
            };
            var enumerateResponse = _pagedQueriesClient.Enumerate(enumerationRequest);

            long totalCount = enumerateResponse.Count ?? 0;

            // second request - to actually get desired data
            var pullRequest = new PullRequest();

            // set enumeration context from previous query
            pullRequest.EnumerationContext = enumerateResponse.EnumerationContext;

            // if attributes names to fetch defined...
            var attributeNames = attributes.GetNames();

            if (attributeNames != null)
            {
                // ... set them to context
                pullRequest.EnumerationContext.Selection         = new Selection();
                pullRequest.EnumerationContext.Selection.@string = attributeNames.ToList();
            }
            else
            {
                pullRequest.EnumerationContext.Selection = null;
            }

            // if paging defined...
            if (pagination.PageSize != Pagination.AllPagesSize)
            {
                // set current page's first row index...
                pullRequest.EnumerationContext.CurrentIndex = pagination.GetFirstRowIndex();
                // ...and page size
                pullRequest.MaxElements = pagination.PageSize;
            }
            // ... if not - get all elements
            else
            {
                // reset current row index...
                pullRequest.EnumerationContext.CurrentIndex = 0;
                // ... page size to max (this may throw if message size exceeds configured maximum, but this situation - getting all items using this method - is not likely to happen)
                pullRequest.MaxElements = int.MaxValue;
            }

            // if sorting defined...
            if (sorting != SortingInstructions.None)
            {
                var sortingAttribute = new SortingAttribute()
                {
                    Ascending = sorting.Order == SortOrder.Ascending,
                    Value     = sorting.AttributeName
                };
                // due to implementation details of these classes, new instances of each of them needs to be created
                pullRequest.EnumerationContext.Sorting = new Sorting
                {
                    SortingAttribute = new List <SortingAttribute>
                    {
                        sortingAttribute
                    }
                };
            }

            var pullResponse = _pagedQueriesClient.Pull(pullRequest);
            var results      = _defaultClient.ResourceFactory.CreateResource(pullResponse)
                               .Cast <TResource>()
                               .ToList();

            log_query_executed(ctx, "Paged query {0} returned {1} results and {2} total count", query, results.Count, totalCount);

            return(new DataPage <TResource>(results, totalCount));
        }
Ejemplo n.º 2
0
        public bool MoveNext()
        {
            lock (this.client) {
                if (resultIndex < results.Count)
                {
                    this.current = results[resultIndex++];
                    return(true);
                }
                else
                {
                    PullResponse response;
                    if (this.context == null)
                    {
                        if (resultIndex > 0)
                        {
                            // case: previous pull returned an invalid context
                            return(false);
                        }
                        EnumerationRequest request = new EnumerationRequest(filter);
                        if (attributes != null)
                        {
                            request.Selection = new List <string>();
                            request.Selection.AddRange(this.attributes);
                        }
                        response           = client.Enumerate(request);
                        this.endOfSequence = response.EndOfSequence != null;
                    }
                    else
                    {
                        if (this.endOfSequence == true)
                        {
                            // case: previous pull returned an end of sequence flag
                            this.current = null;
                            return(false);
                        }
                        PullRequest request = new PullRequest();
                        request.EnumerationContext = this.context;
                        response = client.Pull(request);
                    }

                    if (response == null)
                    {
                        return(false);
                    }
                    resultIndex        = 0;
                    this.results       = resourceFactory.CreateResource(response);
                    this.context       = response.EnumerationContext;
                    this.endOfSequence = response.IsEndOfSequence;
                    if (this.results.Count > 0)
                    {
                        this.current = results[resultIndex++];
                        return(true);
                    }
                    else
                    {
                        this.current = null;
                        return(false);
                    }
                }
            }
        }