Ejemplo n.º 1
0
        private int GetCount()
        {
            if (IsDone && CollectResults)
            {
                return(Matches.Count);
            }
            else
            {
                if (count != -1)
                {
                    return(count);
                }
                else
                {
                    switch (SearchMethod)
                    {
                    case SearchType.Scan:
                        ScanRequest scanReq = new ScanRequest
                        {
                            TableName         = TableName,
                            Select            = EnumMapper.Convert(SelectValues.Count),
                            ExclusiveStartKey = NextKey,
                            ScanFilter        = Filter.ToConditions(SourceTable.Conversion, SourceTable.IsEmptyStringValueEnabled),
                            ConsistentRead    = IsConsistentRead
                        };
                        if (!string.IsNullOrEmpty(this.IndexName))
                        {
                            scanReq.IndexName = this.IndexName;
                        }
                        if (this.FilterExpression != null && this.FilterExpression.IsSet)
                        {
                            this.FilterExpression.ApplyExpression(scanReq, SourceTable);
                        }
                        if (scanReq.ScanFilter != null && scanReq.ScanFilter.Count > 1)
                        {
                            scanReq.ConditionalOperator = EnumMapper.Convert(ConditionalOperator);
                        }

                        if (this.TotalSegments != 0)
                        {
                            scanReq.TotalSegments = this.TotalSegments;
                            scanReq.Segment       = this.Segment;
                        }

                        SourceTable.AddRequestHandler(scanReq, isAsync: false);

                        var scanResult = SourceTable.DDBClient.Scan(scanReq);
                        count = Matches.Count + scanResult.Count;
                        return(count);

                    case SearchType.Query:
                        QueryRequest queryReq = new QueryRequest
                        {
                            TableName         = TableName,
                            ConsistentRead    = IsConsistentRead,
                            Select            = EnumMapper.Convert(SelectValues.Count),
                            ExclusiveStartKey = NextKey,
                            ScanIndexForward  = !IsBackwardSearch,
                            IndexName         = IndexName
                        };

                        Expression.ApplyExpression(queryReq, SourceTable, KeyExpression, FilterExpression);
                        Dictionary <string, Condition> keyConditions, filterConditions;
                        SplitQueryFilter(Filter, SourceTable, queryReq.IndexName, out keyConditions, out filterConditions);
                        queryReq.KeyConditions = keyConditions;
                        queryReq.QueryFilter   = filterConditions;

                        if (queryReq.QueryFilter != null && queryReq.QueryFilter.Count > 1)
                        {
                            queryReq.ConditionalOperator = EnumMapper.Convert(ConditionalOperator);
                        }

                        SourceTable.AddRequestHandler(queryReq, isAsync: false);

                        var queryResult = SourceTable.DDBClient.Query(queryReq);
                        count = Matches.Count + queryResult.Count;
                        return(count);

                    default:
                        throw new InvalidOperationException("Unknown Search Method");
                    }
                }
            }
        }
Ejemplo n.º 2
0
        internal async Task <List <Document> > GetNextSetHelperAsync(CancellationToken cancellationToken)
        {
            List <Document> ret = new List <Document>();

            if (!IsDone)
            {
                switch (SearchMethod)
                {
                case SearchType.Scan:
                    ScanRequest scanReq = new ScanRequest
                    {
                        ExclusiveStartKey = NextKey,
                        Limit             = Limit,
                        TableName         = TableName,
                        AttributesToGet   = AttributesToGet,
                        ScanFilter        = Filter.ToConditions(SourceTable),
                        Select            = EnumMapper.Convert(Select),
                        ConsistentRead    = IsConsistentRead
                    };
                    if (!string.IsNullOrEmpty(this.IndexName))
                    {
                        scanReq.IndexName = this.IndexName;
                    }
                    if (this.FilterExpression != null && this.FilterExpression.IsSet)
                    {
                        this.FilterExpression.ApplyExpression(scanReq, SourceTable);
                    }
                    if (scanReq.ScanFilter != null && scanReq.ScanFilter.Count > 1)
                    {
                        scanReq.ConditionalOperator = EnumMapper.Convert(ConditionalOperator);
                    }
                    Common.ConvertAttributesToGetToProjectionExpression(scanReq);

                    if (this.TotalSegments != 0)
                    {
                        scanReq.TotalSegments = this.TotalSegments;
                        scanReq.Segment       = this.Segment;
                    }

                    SourceTable.AddRequestHandler(scanReq, isAsync: true);

                    var scanResult = await SourceTable.DDBClient.ScanAsync(scanReq, cancellationToken).ConfigureAwait(false);

                    foreach (var item in scanResult.Items)
                    {
                        Document doc = SourceTable.FromAttributeMap(item);
                        ret.Add(doc);
                        if (CollectResults)
                        {
                            Matches.Add(doc);
                        }
                    }
                    NextKey = scanResult.LastEvaluatedKey;
                    if (NextKey == null || NextKey.Count == 0)
                    {
                        IsDone = true;
                    }
                    return(ret);

                case SearchType.Query:
                    QueryRequest queryReq = new QueryRequest
                    {
                        TableName         = TableName,
                        ConsistentRead    = IsConsistentRead,
                        Select            = EnumMapper.Convert(Select),
                        ExclusiveStartKey = NextKey,
                        Limit             = Limit,
                        ScanIndexForward  = !IsBackwardSearch,
                        AttributesToGet   = AttributesToGet,
                        IndexName         = IndexName,
                    };

                    Expression.ApplyExpression(queryReq, SourceTable, KeyExpression, FilterExpression);

                    Dictionary <string, Condition> keyConditions, filterConditions;
                    SplitQueryFilter(Filter, SourceTable, queryReq.IndexName, out keyConditions, out filterConditions);
                    queryReq.KeyConditions = keyConditions;
                    queryReq.QueryFilter   = filterConditions;
                    Common.ConvertAttributesToGetToProjectionExpression(queryReq);

                    if (queryReq.QueryFilter != null && queryReq.QueryFilter.Count > 1)
                    {
                        queryReq.ConditionalOperator = EnumMapper.Convert(ConditionalOperator);
                    }

                    SourceTable.AddRequestHandler(queryReq, isAsync: true);

                    var queryResult = await SourceTable.DDBClient.QueryAsync(queryReq, cancellationToken).ConfigureAwait(false);

                    foreach (var item in queryResult.Items)
                    {
                        Document doc = SourceTable.FromAttributeMap(item);
                        ret.Add(doc);
                        if (CollectResults)
                        {
                            Matches.Add(doc);
                        }
                    }
                    NextKey = queryResult.LastEvaluatedKey;
                    if (NextKey == null || NextKey.Count == 0)
                    {
                        IsDone = true;
                    }
                    return(ret);

                default:
                    throw new InvalidOperationException("Unknown Search Method");
                }
            }

            return(ret);
        }