Exemplo n.º 1
0
        private void CloneGlobalSecondaryIndexSchema(IEnumerable <GlobalSecondaryIndex> sourceSchema)
        {
            foreach (var sourceIndex in sourceSchema)
            {
                var clonedIndex = new GlobalSecondaryIndex
                {
                    IndexName  = new string(sourceIndex.IndexName.ToCharArray()),
                    Projection = null,
                    KeySchema  = new List <KeySchemaElement>()
                };

                foreach (var sourceKey in sourceIndex.KeySchema)
                {
                    var clonedKey = new KeySchemaElement
                    {
                        AttributeName = new string(sourceKey.AttributeName.ToCharArray()),
                        KeyType       = new DynamoDBv2.KeyType(sourceKey.KeyType)
                    };
                    clonedIndex.KeySchema.Add(clonedKey);
                }

                if (sourceIndex.Projection != null)
                {
                    clonedIndex.Projection = new Projection
                    {
                        ProjectionType   = new DynamoDBv2.ProjectionType(sourceIndex.Projection.ProjectionType),
                        NonKeyAttributes = new List <string>()
                    };
                    foreach (var nonKeyAttr in sourceIndex.Projection.NonKeyAttributes)
                    {
                        clonedIndex.Projection.NonKeyAttributes.Add(nonKeyAttr);
                    }
                }

                clonedIndex.ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = sourceIndex.ProvisionedThroughput.ReadCapacityUnits,
                    WriteCapacityUnits = sourceIndex.ProvisionedThroughput.WriteCapacityUnits
                };

                GlobalSecondaryIndexSchema.Add(clonedIndex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Adds a new global secondary index or updates an index if it has been defined already.
        /// </summary>
        /// <param name="indexName"></param>
        /// <param name="hashKeyName"></param>
        /// <param name="hashKeyDataType"></param>
        /// <param name="rangeKeyName"></param>
        /// <param name="rangeKeyDataType"></param>
        /// <param name="readCapacityUnits"></param>
        /// <param name="writeCapacityUnits"></param>
        /// <param name="projectionType"></param>
        /// <param name="nonKeyAttributes"></param>
        internal void SetGlobalSecondaryIndex(string indexName,
                                              string hashKeyName,
                                              string hashKeyDataType,
                                              string rangeKeyName,
                                              string rangeKeyDataType,
                                              Int64 readCapacityUnits,
                                              Int64 writeCapacityUnits,
                                              string projectionType     = null,
                                              string[] nonKeyAttributes = null)
        {
            // to add additional range keys, the user invokes this cmdlet multiple times in the
            // pipeline
            bool creatingNewIndex = false;
            var  gsi = GlobalSecondaryIndexSchema.FirstOrDefault(g => g.IndexName.Equals(indexName, StringComparison.OrdinalIgnoreCase));

            if (gsi == null)
            {
                creatingNewIndex = true;
                gsi = new GlobalSecondaryIndex
                {
                    IndexName = indexName,
                    KeySchema = new List <KeySchemaElement>()
                };
            }

            // for a GSI, an additional hash key can be defined that does not have to match that used by the table
            if (!string.IsNullOrEmpty(hashKeyName))
            {
                if (AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(hashKeyName, StringComparison.Ordinal)) == null)
                {
                    // could validate that data type matches here
                    AttributeSchema.Add(new AttributeDefinition {
                        AttributeName = hashKeyName, AttributeType = hashKeyDataType
                    });
                }

                gsi.KeySchema.Add(new KeySchemaElement {
                    AttributeName = hashKeyName, KeyType = Amazon.DynamoDBv2.KeyType.HASH
                });
            }

            // for global indexes, range key is optional (assuming a hash key has been specified); for local indexes the range key is
            // mandatory
            if (!string.IsNullOrEmpty(rangeKeyName))
            {
                if (AttributeSchema.FirstOrDefault(a => a.AttributeName.Equals(rangeKeyName, StringComparison.Ordinal)) == null)
                {
                    // could validate that data type matches here
                    AttributeSchema.Add(new AttributeDefinition {
                        AttributeName = rangeKeyName, AttributeType = rangeKeyDataType
                    });
                }

                gsi.KeySchema.Add(new KeySchemaElement {
                    AttributeName = rangeKeyName, KeyType = Amazon.DynamoDBv2.KeyType.RANGE
                });
            }

            gsi.ProvisionedThroughput = new ProvisionedThroughput
            {
                ReadCapacityUnits  = readCapacityUnits,
                WriteCapacityUnits = writeCapacityUnits
            };

            if (!string.IsNullOrEmpty(projectionType))
            {
                gsi.Projection = new Projection();

                if (_projectionTypes.Contains(projectionType, StringComparer.OrdinalIgnoreCase))
                {
                    gsi.Projection.ProjectionType = projectionType.ToUpper();
                    if (nonKeyAttributes != null && nonKeyAttributes.Length > 0)
                    {
                        gsi.Projection.NonKeyAttributes.AddRange(nonKeyAttributes);
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format("Invalid ProjectionType '{0}'; allowed values: {1}.",
                                                              projectionType,
                                                              string.Join(", ", _projectionTypes)),
                                                "ProjectionType");
                }
            }

            if (creatingNewIndex)
            {
                GlobalSecondaryIndexSchema.Add(gsi);
            }
        }