Ejemplo n.º 1
0
        private QueryFilter ComposeQueryFilter(DynamoDBFlatConfig currentConfig, object hashKeyValue, IEnumerable <QueryCondition> conditions, ItemStorageConfig storageConfig, out List <string> indexNames)
        {
            if (hashKeyValue == null)
            {
                throw new ArgumentNullException("hashKeyValue");
            }

            // Set hash key property name
            // In case of index queries, if GSI, different key could be used
            string hashKeyProperty = storageConfig.HashKeyPropertyNames[0];

            hashKeyProperty = storageConfig.GetCorrectHashKeyProperty(currentConfig, hashKeyProperty);

            PropertyStorage propertyStorage   = storageConfig.GetPropertyStorage(hashKeyProperty);
            string          hashAttributeName = propertyStorage.AttributeName;

            DynamoDBEntry hashKeyEntry = ValueToDynamoDBEntry(propertyStorage, hashKeyValue, currentConfig);

            if (hashKeyEntry == null)
            {
                throw new InvalidOperationException("Unable to convert hash key value for property " + hashKeyProperty);
            }

            Document hashKey = new Document();

            hashKey[hashAttributeName] = hashKeyEntry;

            return(ComposeQueryFilterHelper(currentConfig, hashKey, conditions, storageConfig, out indexNames));
        }
Ejemplo n.º 2
0
        private static QueryFilter ComposeQueryFilterHelper(Document hashKey, IEnumerable <QueryCondition> conditions, ItemStorageConfig storageConfig, DynamoDBFlatConfig currentConfig, out List <string> indexNames)
        {
            if (hashKey == null)
            {
                throw new ArgumentNullException("hashKey");
            }

            if (storageConfig.HashKeyPropertyNames.Count != 1)
            {
                throw new InvalidOperationException("Must have one hash key defined for the table " + storageConfig.TableName);
            }
            if (storageConfig.RangeKeyPropertyNames.Count != 1 && storageConfig.IndexNameToGSIMapping.Count == 0)
            {
                throw new InvalidOperationException("Must have one range key or a GSI index defined for the table " + storageConfig.TableName);
            }

            QueryFilter filter = new QueryFilter();

            // Configure hash-key equality condition
            string hashKeyProperty = storageConfig.HashKeyPropertyNames[0];

            hashKeyProperty = storageConfig.GetCorrectHashKeyProperty(currentConfig, hashKeyProperty);
            PropertyStorage propertyStorage = storageConfig.GetPropertyStorage(hashKeyProperty);
            string          attributeName   = propertyStorage.AttributeName;
            DynamoDBEntry   hashValue       = hashKey[attributeName];

            filter.AddCondition(attributeName, QueryOperator.Equal, hashValue);

            indexNames = new List <string>();
            if (conditions != null)
            {
                foreach (QueryCondition condition in conditions)
                {
                    object[]        conditionValues   = condition.Values;
                    PropertyStorage conditionProperty = storageConfig.GetPropertyStorage(condition.PropertyName);
                    if (conditionProperty.IsLSIRangeKey || conditionProperty.IsGSIKey)
                    {
                        indexNames.AddRange(conditionProperty.Indexes);
                    }
                    List <AttributeValue> attributeValues = new List <AttributeValue>();
                    foreach (var conditionValue in conditionValues)
                    {
                        DynamoDBEntry  entry          = ToDynamoDBEntry(conditionProperty, conditionValue);
                        AttributeValue attributeValue = entry.ConvertToAttributeValue();
                        attributeValues.Add(attributeValue);
                    }
                    filter.AddCondition(conditionProperty.AttributeName, condition.Operator, attributeValues);
                }
            }
            return(filter);
        }