Exemple #1
0
        internal Document PutItemHelper(Document doc, PutItemOperationConfig config, bool isAsync)
        {
            var currentConfig = config ?? new PutItemOperationConfig();

            PutItemRequest req = new PutItemRequest
            {
                TableName = TableName,
                Item      = doc.ToAttributeMap(Conversion)
            };

            req.BeforeRequestEvent += isAsync ?
                                      new RequestEventHandler(UserAgentRequestEventHandlerAsync) :
                                      new RequestEventHandler(UserAgentRequestEventHandlerSync);
            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                req.ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues);
            }

            ValidateConditional(currentConfig);


            if (currentConfig.Expected != null)
            {
                req.Expected = currentConfig.Expected.ToExpectedAttributeMap(Conversion);
            }
            else if (currentConfig.ExpectedState != null &&
                     currentConfig.ExpectedState.ExpectedValues != null &&
                     currentConfig.ExpectedState.ExpectedValues.Count > 0)
            {
                req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(Conversion);
                if (req.Expected.Count > 1)
                {
                    req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator);
                }
            }
            else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet)
            {
                currentConfig.ConditionalExpression.ApplyExpression(req, this.Conversion);
            }

            var resp = DDBClient.PutItem(req);

            doc.CommitChanges();

            Document ret = null;

            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                ret = Document.FromAttributeMap(resp.Attributes);
            }
            return(ret);
        }
Exemple #2
0
        internal async Task <Document> PutItemHelperAsync(Document doc, PutItemOperationConfig config, CancellationToken cancellationToken)
        {
            var currentConfig = config ?? new PutItemOperationConfig();

            PutItemRequest req = new PutItemRequest
            {
                TableName = TableName,
                Item      = this.ToAttributeMap(doc)
            };

            this.AddRequestHandler(req, isAsync: true);

            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                req.ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues);
            }

            ValidateConditional(currentConfig);


            if (currentConfig.Expected != null)
            {
                req.Expected = this.ToExpectedAttributeMap(currentConfig.Expected);
            }
            else if (currentConfig.ExpectedState != null &&
                     currentConfig.ExpectedState.ExpectedValues != null &&
                     currentConfig.ExpectedState.ExpectedValues.Count > 0)
            {
                req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(this);
                if (req.Expected.Count > 1)
                {
                    req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator);
                }
            }
            else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet)
            {
                currentConfig.ConditionalExpression.ApplyExpression(req, this);
            }

            var resp = await DDBClient.PutItemAsync(req, cancellationToken).ConfigureAwait(false);

            doc.CommitChanges();

            Document ret = null;

            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                ret = this.FromAttributeMap(resp.Attributes);
            }
            return(ret);
        }
Exemple #3
0
        internal Document DeleteHelper(Key key, DeleteItemOperationConfig config, bool isAsync)
        {
            var currentConfig = config ?? new DeleteItemOperationConfig();

            var req = new DeleteItemRequest
            {
                TableName = TableName,
                Key       = key
            };

            ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)req).AddBeforeRequestHandler(isAsync ?
                                                                                            new RequestEventHandler(UserAgentRequestEventHandlerAsync) :
                                                                                            new RequestEventHandler(UserAgentRequestEventHandlerSync)
                                                                                            );
            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                req.ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues);
            }

            ValidateConditional(currentConfig);

            if (currentConfig.Expected != null)
            {
                req.Expected = currentConfig.Expected.ToExpectedAttributeMap(Conversion);
            }
            else if (currentConfig.ExpectedState != null &&
                     currentConfig.ExpectedState.ExpectedValues != null &&
                     currentConfig.ExpectedState.ExpectedValues.Count > 0)
            {
                req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(Conversion);
                if (req.Expected.Count > 1)
                {
                    req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator);
                }
            }
            else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet)
            {
                currentConfig.ConditionalExpression.ApplyExpression(req, this.Conversion);
            }

            var attributes = DDBClient.DeleteItem(req).Attributes;

            Document ret = null;

            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                ret = Document.FromAttributeMap(attributes);
            }
            return(ret);
        }
Exemple #4
0
        internal async Task <Document> DeleteHelperAsync(Key key, DeleteItemOperationConfig config, CancellationToken cancellationToken)
        {
            var currentConfig = config ?? new DeleteItemOperationConfig();

            var req = new DeleteItemRequest
            {
                TableName = TableName,
                Key       = key
            };

            this.AddRequestHandler(req, isAsync: true);

            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                req.ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues);
            }

            ValidateConditional(currentConfig);

            if (currentConfig.Expected != null)
            {
                req.Expected = this.ToExpectedAttributeMap(currentConfig.Expected);
            }
            else if (currentConfig.ExpectedState != null &&
                     currentConfig.ExpectedState.ExpectedValues != null &&
                     currentConfig.ExpectedState.ExpectedValues.Count > 0)
            {
                req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(this);
                if (req.Expected.Count > 1)
                {
                    req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator);
                }
            }
            else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet)
            {
                currentConfig.ConditionalExpression.ApplyExpression(req, this);
            }

            var attributes = (await DDBClient.DeleteItemAsync(req, cancellationToken).ConfigureAwait(false)).Attributes;

            Document ret = null;

            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                ret = this.FromAttributeMap(attributes);
            }
            return(ret);
        }
Exemple #5
0
        internal Document PutItemHelper(Document doc, PutItemOperationConfig config, bool isAsync)
        {
            var currentConfig = config ?? new PutItemOperationConfig();

            PutItemRequest req = new PutItemRequest
            {
                TableName = TableName,
                Item      = doc.ToAttributeMap()
            };

            req.BeforeRequestEvent += isAsync ?
                                      new RequestEventHandler(UserAgentRequestEventHandlerAsync) :
                                      new RequestEventHandler(UserAgentRequestEventHandlerSync);
            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                req.ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues);
            }
            if (currentConfig.Expected != null && currentConfig.ExpectedState != null)
            {
                throw new InvalidOperationException("Expected and ExpectedState cannot be set at the same time");
            }
            if (currentConfig.Expected != null)
            {
                req.Expected = currentConfig.Expected.ToExpectedAttributeMap();
            }
            if (currentConfig.ExpectedState != null &&
                currentConfig.ExpectedState.ExpectedValues != null &&
                currentConfig.ExpectedState.ExpectedValues.Count > 0)
            {
                req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap();
                if (req.Expected.Count > 1)
                {
                    req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator);
                }
            }

            var resp = DDBClient.PutItem(req);

            doc.CommitChanges();

            Document ret = null;

            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                ret = Document.FromAttributeMap(resp.Attributes);
            }
            return(ret);
        }
Exemple #6
0
        internal Document DeleteHelper(Key key, DeleteItemOperationConfig config, bool isAsync)
        {
            var currentConfig = config ?? new DeleteItemOperationConfig();

            var req = new DeleteItemRequest
            {
                TableName = TableName,
                Key       = key
            };

            req.BeforeRequestEvent += isAsync ?
                                      new RequestEventHandler(UserAgentRequestEventHandlerAsync) :
                                      new RequestEventHandler(UserAgentRequestEventHandlerSync);
            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                req.ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues);
            }
            if (currentConfig.Expected != null && currentConfig.ExpectedState != null)
            {
                throw new InvalidOperationException("Expected and ExpectedState cannot be set at the same time");
            }
            if (currentConfig.Expected != null)
            {
                req.Expected = currentConfig.Expected.ToExpectedAttributeMap();
            }
            if (currentConfig.ExpectedState != null &&
                currentConfig.ExpectedState.ExpectedValues != null &&
                currentConfig.ExpectedState.ExpectedValues.Count > 0)
            {
                req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap();
                if (req.Expected.Count > 1)
                {
                    req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator);
                }
            }

            var attributes = DDBClient.DeleteItem(req).Attributes;

            Document ret = null;

            if (currentConfig.ReturnValues == ReturnValues.AllOldAttributes)
            {
                ret = Document.FromAttributeMap(attributes);
            }
            return(ret);
        }
Exemple #7
0
        /// <summary>
        /// Converts this ExpectedValue instance to Amazon.DynamoDBv2.Model.ExpectedAttributeValue
        /// </summary>
        /// <param name="conversion">Conversion to use for converting .NET values to DynamoDB values.</param>
        /// <returns>Amazon.DynamoDBv2.Model.ExpectedAttributeValue</returns>
        public ExpectedAttributeValue ToExpectedAttributeValue(DynamoDBEntryConversion conversion)
        {
            var eav = new ExpectedAttributeValue();

            if (this.Exists)
            {
                eav.ComparisonOperator = EnumMapper.Convert(this.Comparison);
                foreach (var val in this.Values)
                {
                    eav.AttributeValueList.Add(val.ConvertToAttributeValue(new DynamoDBEntry.AttributeConversionConfig(conversion)));
                }
            }
            else
            {
                eav.Exists = this.Exists;
            }

            return(eav);
        }
Exemple #8
0
        /// <summary>
        /// Converts this ExpectedValue instance to Amazon.DynamoDBv2.Model.ExpectedAttributeValue
        /// </summary>
        /// <returns>Amazon.DynamoDBv2.Model.ExpectedAttributeValue</returns>
        public ExpectedAttributeValue ToExpectedAttributeValue()
        {
            var eav = new ExpectedAttributeValue();

            if (this.Exists)
            {
                eav.ComparisonOperator = EnumMapper.Convert(this.Comparison);
                foreach (var val in this.Values)
                {
                    eav.AttributeValueList.Add(val.ConvertToAttributeValue());
                }
            }
            else
            {
                eav.Exists = this.Exists;
            }

            return(eav);
        }
        internal static ExpectedAttributeValue ToExpectedAttributeValue(bool exists, IEnumerable <DynamoDBEntry> values, ScanOperator comparison,
                                                                        DynamoDBEntryConversion conversion)
        {
            var eav = new ExpectedAttributeValue();

            if (exists)
            {
                eav.ComparisonOperator = EnumMapper.Convert(comparison);
                foreach (var val in values)
                {
                    eav.AttributeValueList.Add(val.ConvertToAttributeValue(new DynamoDBEntry.AttributeConversionConfig(conversion)));
                }
            }
            else
            {
                eav.Exists = exists;
            }

            return(eav);
        }
Exemple #10
0
        internal async Task <Document> UpdateHelperAsync(Document doc, Key key, UpdateItemOperationConfig config, CancellationToken cancellationToken)
        {
            var currentConfig = config ?? new UpdateItemOperationConfig();

            // If the keys have been changed, treat entire document as having changed
            bool haveKeysChanged             = HaveKeysChanged(doc);
            bool updateChangedAttributesOnly = !haveKeysChanged;

            var attributeUpdates = this.ToAttributeUpdateMap(doc, updateChangedAttributesOnly);

            foreach (var keyName in this.KeyNames)
            {
                attributeUpdates.Remove(keyName);
            }

            UpdateItemRequest req = new UpdateItemRequest
            {
                TableName        = TableName,
                Key              = key,
                AttributeUpdates = attributeUpdates.Count == 0 ? null : attributeUpdates, // pass null if keys-only update
                ReturnValues     = EnumMapper.Convert(currentConfig.ReturnValues)
            };

            this.AddRequestHandler(req, isAsync: true);

            ValidateConditional(currentConfig);

            if (currentConfig.Expected != null)
            {
                req.Expected = this.ToExpectedAttributeMap(currentConfig.Expected);
            }
            else if (currentConfig.ExpectedState != null &&
                     currentConfig.ExpectedState.ExpectedValues != null &&
                     currentConfig.ExpectedState.ExpectedValues.Count > 0)
            {
                req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(this);
                if (req.Expected.Count > 1)
                {
                    req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator);
                }
            }
            else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet)
            {
                currentConfig.ConditionalExpression.ApplyExpression(req, this);

                string statement;
                Dictionary <string, AttributeValue> expressionAttributeValues;
                Dictionary <string, string>         expressionAttributeNames;
                Common.ConvertAttributeUpdatesToUpdateExpression(attributeUpdates, out statement, out expressionAttributeValues, out expressionAttributeNames);

                req.AttributeUpdates = null;
                req.UpdateExpression = statement;

                if (req.ExpressionAttributeValues == null)
                {
                    req.ExpressionAttributeValues = expressionAttributeValues;
                }
                else
                {
                    foreach (var kvp in expressionAttributeValues)
                    {
                        req.ExpressionAttributeValues.Add(kvp.Key, kvp.Value);
                    }
                }

                if (req.ExpressionAttributeNames == null)
                {
                    req.ExpressionAttributeNames = expressionAttributeNames;
                }
                else
                {
                    foreach (var kvp in expressionAttributeNames)
                    {
                        req.ExpressionAttributeNames.Add(kvp.Key, kvp.Value);
                    }
                }
            }

            var resp = await DDBClient.UpdateItemAsync(req, cancellationToken).ConfigureAwait(false);

            var returnedAttributes = resp.Attributes;

            doc.CommitChanges();

            Document ret = null;

            if (currentConfig.ReturnValues != ReturnValues.None)
            {
                ret = this.FromAttributeMap(returnedAttributes);
            }
            return(ret);
        }
Exemple #11
0
        internal Document UpdateHelper(Document doc, Key key, UpdateItemOperationConfig config, bool isAsync)
        {
            var currentConfig = config ?? new UpdateItemOperationConfig();

            // If the keys have been changed, treat entire document as having changed
            bool haveKeysChanged             = HaveKeysChanged(doc);
            bool updateChangedAttributesOnly = !haveKeysChanged;

            var attributeUpdates = doc.ToAttributeUpdateMap(Conversion, updateChangedAttributesOnly);

            foreach (var keyName in this.KeyNames)
            {
                attributeUpdates.Remove(keyName);
            }

            UpdateItemRequest req = new UpdateItemRequest
            {
                TableName        = TableName,
                Key              = key,
                AttributeUpdates = attributeUpdates.Count == 0 ? null : attributeUpdates, // pass null if keys-only update
                ReturnValues     = EnumMapper.Convert(currentConfig.ReturnValues)
            };


            ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)req).AddBeforeRequestHandler(isAsync ?
                                                                                            new RequestEventHandler(UserAgentRequestEventHandlerAsync) :
                                                                                            new RequestEventHandler(UserAgentRequestEventHandlerSync)
                                                                                            );

            ValidateConditional(currentConfig);

            if (currentConfig.Expected != null)
            {
                req.Expected = currentConfig.Expected.ToExpectedAttributeMap(Conversion);
            }
            else if (currentConfig.ExpectedState != null &&
                     currentConfig.ExpectedState.ExpectedValues != null &&
                     currentConfig.ExpectedState.ExpectedValues.Count > 0)
            {
                req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(Conversion);
                if (req.Expected.Count > 1)
                {
                    req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator);
                }
            }
            else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet)
            {
                currentConfig.ConditionalExpression.ApplyExpression(req, this.Conversion);

                string statement;
                Dictionary <string, AttributeValue> expressionAttributeValues;
                Dictionary <string, string>         expressionAttributeNames;
                Common.ConvertAttributeUpdatesToUpdateExpression(attributeUpdates, out statement, out expressionAttributeValues, out expressionAttributeNames);

                req.AttributeUpdates = null;
                req.UpdateExpression = statement;

                if (req.ExpressionAttributeValues == null)
                {
                    req.ExpressionAttributeValues = expressionAttributeValues;
                }
                else
                {
                    foreach (var kvp in expressionAttributeValues)
                    {
                        req.ExpressionAttributeValues.Add(kvp.Key, kvp.Value);
                    }
                }

                if (req.ExpressionAttributeNames == null)
                {
                    req.ExpressionAttributeNames = expressionAttributeNames;
                }
                else
                {
                    foreach (var kvp in expressionAttributeNames)
                    {
                        req.ExpressionAttributeNames.Add(kvp.Key, kvp.Value);
                    }
                }
            }

            var resp = DDBClient.UpdateItem(req);
            var returnedAttributes = resp.Attributes;

            doc.CommitChanges();

            Document ret = null;

            if (currentConfig.ReturnValues != ReturnValues.None)
            {
                ret = Document.FromAttributeMap(returnedAttributes);
            }
            return(ret);
        }
Exemple #12
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");
                    }
                }
            }
        }
Exemple #13
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);
        }
        /// <summary>
        /// Adds a condition for a specified non-key attribute that consists
        /// of an operator and any number of values
        /// </summary>
        /// <param name="nonKeyAttributeName">Target non-key attribute name</param>
        /// <param name="op">Comparison operator</param>
        /// <param name="values">Values to compare to</param>
        public void AddCondition(string nonKeyAttributeName, ScanOperator op, params DynamoDBEntry[] values)
        {
            ComparisonOperator comparisonOperator = EnumMapper.Convert(op);

            AddCondition(nonKeyAttributeName, comparisonOperator, values.ToList());
        }
        internal List <Document> GetNextSetHelper(bool isAsync)
        {
            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.Conversion),
                        Select            = EnumMapper.Convert(Select),
                    };
                    if (!string.IsNullOrEmpty(this.IndexName))
                    {
                        scanReq.IndexName = this.IndexName;
                    }
                    if (this.FilterExpression != null && this.FilterExpression.IsSet)
                    {
                        this.FilterExpression.ApplyExpression(scanReq, SourceTable.Conversion);
                    }
                    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;
                    }

                    ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)scanReq).AddBeforeRequestHandler(isAsync ?
                                                                                                        new RequestEventHandler(SourceTable.UserAgentRequestEventHandlerAsync) :
                                                                                                        new RequestEventHandler(SourceTable.UserAgentRequestEventHandlerSync)
                                                                                                        );

                    var scanResult = SourceTable.DDBClient.Scan(scanReq);
                    foreach (var item in scanResult.Items)
                    {
                        Document doc = Document.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,
                    };

                    if (this.FilterExpression != null && this.FilterExpression.IsSet)
                    {
                        this.FilterExpression.ApplyExpression(queryReq, SourceTable.Conversion);
                    }

                    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);
                    }

                    ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)queryReq).AddBeforeRequestHandler(isAsync ?
                                                                                                         new RequestEventHandler(SourceTable.UserAgentRequestEventHandlerAsync) :
                                                                                                         new RequestEventHandler(SourceTable.UserAgentRequestEventHandlerSync)
                                                                                                         );
                    var queryResult = SourceTable.DDBClient.Query(queryReq);
                    foreach (var item in queryResult.Items)
                    {
                        Document doc = Document.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);
        }
Exemple #16
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,
                        };
                        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;
                        }
                        scanReq.BeforeRequestEvent += SourceTable.UserAgentRequestEventHandlerSync;

                        ScanResult 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
                        };

                        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);
                        }

                        queryReq.BeforeRequestEvent += SourceTable.UserAgentRequestEventHandlerSync;

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

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