GlobalSecondaryIndex SummonGlobalSecondaryIndex(Type type, PropertyInfo[] properties, CreateTableRequest createTableRequest, string indexName)
        {
            var index = createTableRequest.GlobalSecondaryIndexes.FirstOrDefault(x => x.IndexName == indexName);

            if (index != null)
            {
                return(index);
            }

            var indexAttribute = GetDynamoIndexAttributeForIndexConstant(type, properties, createTableRequest, indexName);

            index = new GlobalSecondaryIndex {
                IndexName  = indexName,
                KeySchema  = new List <KeySchemaElement>(),
                Projection = new Projection {
                    ProjectionType   = indexAttribute.AmazonProjectionType,
                    NonKeyAttributes = indexAttribute.NonKeyAttributes.EmptyIfNull().ToList()
                },
                ProvisionedThroughput = new ProvisionedThroughput {
                    ReadCapacityUnits  = ReadCapacityUnits,
                    WriteCapacityUnits = WriteCapacityUnits
                }
            };

            createTableRequest.GlobalSecondaryIndexes.Add(index);

            return(index);
        }
        public async Task CreateTableAsync(int readCapacity = 1, int writecapacity = 1)
        {
            string tableName = TABLENAME;
            var    client    = Provider.DynamoDBClient;
            var    ptIndex   = new ProvisionedThroughput
            {
                ReadCapacityUnits  = readCapacity,
                WriteCapacityUnits = writecapacity
            };
            var subscriberIndex = new GlobalSecondaryIndex()
            {
                IndexName             = SelfModel.SCITagName,
                ProvisionedThroughput = ptIndex,
                KeySchema             =
                {
                    new KeySchemaElement {
                        AttributeName = "Subscriber", KeyType = "HASH" //Partition key
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = "ALL"
                }
            };
            var response = await client.CreateTableAsync(new CreateTableRequest
            {
                TableName            = tableName,
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Tag",               // Hash
                        AttributeType = "S"
                    },
                    new AttributeDefinition
                    {
                        AttributeName = "Subscriber",               // Range
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>()
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Tag",
                        KeyType       = "HASH"
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "Subscriber",
                        KeyType       = "RANGE"
                    }
                },
                ProvisionedThroughput  = ptIndex,
                GlobalSecondaryIndexes = { subscriberIndex }
            });

            WaitTillTableCreated(response).Wait();
        }
Exemplo n.º 3
0
        private async Task CreateWorkflowTable()
        {
            var runnableIndex = new GlobalSecondaryIndex
            {
                IndexName = "ix_runnable",
                KeySchema = new List <KeySchemaElement>
                {
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "runnable",
                            KeyType       = "HASH" //Partition key
                        }
                    },
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "next_execution",
                            KeyType       = "RANGE" //Sort key
                        }
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = ProjectionType.KEYS_ONLY
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            var createRequest = new CreateTableRequest($"{_tablePrefix}-{DynamoPersistenceProvider.WORKFLOW_TABLE}", new List <KeySchemaElement>
            {
                new KeySchemaElement("id", KeyType.HASH)
            })
            {
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition("id", ScalarAttributeType.S),
                    new AttributeDefinition("runnable", ScalarAttributeType.N),
                    new AttributeDefinition("next_execution", ScalarAttributeType.N),
                },
                GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
                {
                    runnableIndex
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            await CreateTable(createRequest);
        }
Exemplo n.º 4
0
        private async Task CreateSubscriptionTable()
        {
            var slugIndex = new GlobalSecondaryIndex
            {
                IndexName = "ix_slug",
                KeySchema = new List <KeySchemaElement>
                {
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "event_slug",
                            KeyType       = "HASH" //Partition key
                        }
                    },
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "subscribe_as_of",
                            KeyType       = "RANGE" //Sort key
                        }
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = ProjectionType.ALL
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            var createRequest = new CreateTableRequest($"{_tablePrefix}-{DynamoPersistenceProvider.SUBCRIPTION_TABLE}", new List <KeySchemaElement>
            {
                new KeySchemaElement("id", KeyType.HASH)
            })
            {
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition("id", ScalarAttributeType.S),
                    new AttributeDefinition("event_slug", ScalarAttributeType.S),
                    new AttributeDefinition("subscribe_as_of", ScalarAttributeType.N)
                },
                GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
                {
                    slugIndex
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            await CreateTable(createRequest);
        }
Exemplo n.º 5
0
        private static void AddGSIToMap(string indexName, GlobalSecondaryIndexDetails gsiHashKeyResult, Dictionary <string, GlobalSecondaryIndex> gsiMap)
        {
            var gsi = new GlobalSecondaryIndex();

            gsi.IndexName = indexName;
            var gsiHashKey = new KeySchemaElement(gsiHashKeyResult.Prop.Name, KeyType.HASH);

            gsi.KeySchema.Add(gsiHashKey);
            gsiMap.Add(gsi.IndexName, gsi);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initialize current instance with specific global configuration and logger
        /// </summary>
        /// <param name="config"> Global configuration to initialize with </param>
        /// <param name="logger"> Specific logger to use in current instance </param>
        /// <returns></returns>
        public Task Init(GlobalConfiguration config, Logger logger)
        {
            deploymentId = config.DeploymentId;
            serviceId    = config.ServiceId;

            this.logger = logger;

            storage = new DynamoDBStorage(config.DataConnectionStringForReminders, logger);
            logger.Info(ErrorCode.ReminderServiceBase, "Initializing AWS DynamoDB Reminders Table");

            var secondaryIndex = new GlobalSecondaryIndex
            {
                IndexName  = SERVICE_ID_INDEX,
                Projection = new Projection {
                    ProjectionType = ProjectionType.ALL
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement {
                        AttributeName = SERVICE_ID_PROPERTY_NAME, KeyType = KeyType.HASH
                    },
                    new KeySchemaElement {
                        AttributeName = GRAIN_HASH_PROPERTY_NAME, KeyType = KeyType.RANGE
                    }
                }
            };

            return(storage.InitializeTable(TABLE_NAME_DEFAULT_VALUE,
                                           new List <KeySchemaElement>
            {
                new KeySchemaElement {
                    AttributeName = REMINDER_ID_PROPERTY_NAME, KeyType = KeyType.HASH
                },
                new KeySchemaElement {
                    AttributeName = GRAIN_HASH_PROPERTY_NAME, KeyType = KeyType.RANGE
                }
            },
                                           new List <AttributeDefinition>
            {
                new AttributeDefinition {
                    AttributeName = REMINDER_ID_PROPERTY_NAME, AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition {
                    AttributeName = GRAIN_HASH_PROPERTY_NAME, AttributeType = ScalarAttributeType.N
                },
                new AttributeDefinition {
                    AttributeName = SERVICE_ID_PROPERTY_NAME, AttributeType = ScalarAttributeType.S
                }
            },
                                           new List <GlobalSecondaryIndex> {
                secondaryIndex
            }));
        }
        /// <summary>Initialize current instance with specific global configuration and logger</summary>
        public Task Init()
        {
            this.storage = new DynamoDBStorage(this.logger, this.options.Service, this.options.AccessKey, this.options.SecretKey,
                                               this.options.ReadCapacityUnits, this.options.WriteCapacityUnits);

            this.logger.Info(ErrorCode.ReminderServiceBase, "Initializing AWS DynamoDB Reminders Table");

            var secondaryIndex = new GlobalSecondaryIndex
            {
                IndexName  = SERVICE_ID_INDEX,
                Projection = new Projection {
                    ProjectionType = ProjectionType.ALL
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement {
                        AttributeName = SERVICE_ID_PROPERTY_NAME, KeyType = KeyType.HASH
                    },
                    new KeySchemaElement {
                        AttributeName = GRAIN_HASH_PROPERTY_NAME, KeyType = KeyType.RANGE
                    }
                }
            };

            return(this.storage.InitializeTable(this.options.TableName,
                                                new List <KeySchemaElement>
            {
                new KeySchemaElement {
                    AttributeName = REMINDER_ID_PROPERTY_NAME, KeyType = KeyType.HASH
                },
                new KeySchemaElement {
                    AttributeName = GRAIN_HASH_PROPERTY_NAME, KeyType = KeyType.RANGE
                }
            },
                                                new List <AttributeDefinition>
            {
                new AttributeDefinition {
                    AttributeName = REMINDER_ID_PROPERTY_NAME, AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition {
                    AttributeName = GRAIN_HASH_PROPERTY_NAME, AttributeType = ScalarAttributeType.N
                },
                new AttributeDefinition {
                    AttributeName = SERVICE_ID_PROPERTY_NAME, AttributeType = ScalarAttributeType.S
                }
            },
                                                new List <GlobalSecondaryIndex> {
                secondaryIndex
            }));
        }
Exemplo n.º 8
0
        // [OneTimeSetUp]
        public async Task CreateTableAndIndexes()
        {
            await DynamoDBHelper.DeleteTable("GameScores");

            var gsi = new GlobalSecondaryIndex()
            {
                IndexName = "GameTitleIndex",
                KeySchema = new List <KeySchemaElement>()
                {
                    new KeySchemaElement("GameTitle", KeyType.HASH),
                    new KeySchemaElement("TopScore", KeyType.RANGE)
                },
                Projection = new Projection()
                {
                    ProjectionType = ProjectionType.ALL,
                },
                ProvisionedThroughput = new ProvisionedThroughput(2, 2)
            };


            var request = new CreateTableRequest()
            {
                TableName            = "GameScores",
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition("UserId", ScalarAttributeType.S),
                    new AttributeDefinition("GameTitle", ScalarAttributeType.S),
                    new AttributeDefinition("TopScore", ScalarAttributeType.N)
                },
                KeySchema = new List <KeySchemaElement>()
                {
                    new KeySchemaElement("UserId", KeyType.HASH),
                    new KeySchemaElement("GameTitle", KeyType.RANGE)
                },
                ProvisionedThroughput  = new ProvisionedThroughput(2, 2),
                GlobalSecondaryIndexes = { gsi },
                BillingMode            = BillingMode.PROVISIONED
            };

            var result = await DynamoDBHelper.Client.CreateTableAsync(request);

            await DynamoDBHelper.WaitUntilTableReady("GameScores");

            var batchResult = await PutItemsBatch(CreateTableItems());

            var results = await PutItems(CreateTableItems());

            var results1 = await PutItems(CreateTableItems());
        }
Exemplo n.º 9
0
        /// <summary>
        /// Initialize current instance with specific global configuration and logger
        /// </summary>
        /// <param name="config"> Global configuration to initialize with </param>
        /// <returns></returns>
        public Task Init()
        {
            this.storage = new DynamoDBStorage(this.storageOptions.ConnectionString, this.loggerFactory);
            this.logger.Info(ErrorCode.ReminderServiceBase, "Initializing AWS DynamoDB Reminders Table");

            var secondaryIndex = new GlobalSecondaryIndex
            {
                IndexName  = SERVICE_ID_INDEX,
                Projection = new Projection {
                    ProjectionType = ProjectionType.ALL
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement {
                        AttributeName = SERVICE_ID_PROPERTY_NAME, KeyType = KeyType.HASH
                    },
                    new KeySchemaElement {
                        AttributeName = GRAIN_HASH_PROPERTY_NAME, KeyType = KeyType.RANGE
                    }
                }
            };

            return(this.storage.InitializeTable(TABLE_NAME_DEFAULT_VALUE,
                                                new List <KeySchemaElement>
            {
                new KeySchemaElement {
                    AttributeName = REMINDER_ID_PROPERTY_NAME, KeyType = KeyType.HASH
                },
                new KeySchemaElement {
                    AttributeName = GRAIN_HASH_PROPERTY_NAME, KeyType = KeyType.RANGE
                }
            },
                                                new List <AttributeDefinition>
            {
                new AttributeDefinition {
                    AttributeName = REMINDER_ID_PROPERTY_NAME, AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition {
                    AttributeName = GRAIN_HASH_PROPERTY_NAME, AttributeType = ScalarAttributeType.N
                },
                new AttributeDefinition {
                    AttributeName = SERVICE_ID_PROPERTY_NAME, AttributeType = ScalarAttributeType.S
                }
            },
                                                new List <GlobalSecondaryIndex> {
                secondaryIndex
            }));
        }
Exemplo n.º 10
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.º 11
0
        public IEnumerable <GlobalSecondaryIndex> CreateRequestIndexes(
            IEnumerable <GlobalSecondaryIndexConfiguration> indexes)
        {
            indexes = indexes ?? Enumerable.Empty <GlobalSecondaryIndexConfiguration>();
            var gsis = new List <GlobalSecondaryIndex>();

            foreach (var index in indexes)
            {
                if (string.IsNullOrWhiteSpace(index.IndexName) ||
                    string.IsNullOrWhiteSpace(index.HashKeyMemberName))
                {
                    continue;
                }

                var gsi = new GlobalSecondaryIndex
                {
                    IndexName  = index.IndexName,
                    Projection = new Projection
                    {
                        ProjectionType = ProjectionType.ALL
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = index.ReadCapacityUnits,
                        WriteCapacityUnits = index.WriteCapacityUnits
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement(index.HashKeyMemberName, KeyType.HASH)
                    }
                };

                if (!string.IsNullOrWhiteSpace(index.RangeKeyMemberName))
                {
                    gsi.KeySchema.Add(
                        new KeySchemaElement(index.RangeKeyMemberName, KeyType.RANGE));
                }

                gsis.Add(gsi);
            }

            return(gsis);
        }
Exemplo n.º 12
0
        private async Task CreateEventTable()
        {
            var slugIndex = new GlobalSecondaryIndex
            {
                IndexName = "ix_slug",
                KeySchema = new List <KeySchemaElement>
                {
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "event_slug",
                            KeyType       = "HASH" //Partition key
                        }
                    },
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "event_time",
                            KeyType       = "RANGE" //Sort key
                        }
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = ProjectionType.KEYS_ONLY
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            var processedIndex = new GlobalSecondaryIndex
            {
                IndexName = "ix_not_processed",
                KeySchema = new List <KeySchemaElement>
                {
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "not_processed",
                            KeyType       = "HASH" //Partition key
                        }
                    },
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "event_time",
                            KeyType       = "RANGE" //Sort key
                        }
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = ProjectionType.KEYS_ONLY
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            var createRequest = new CreateTableRequest($"{_tablePrefix}-{DynamoPersistenceProvider.EVENT_TABLE}", new List <KeySchemaElement>
            {
                new KeySchemaElement("id", KeyType.HASH)
            })
            {
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition("id", ScalarAttributeType.S),
                    new AttributeDefinition("not_processed", ScalarAttributeType.N),
                    new AttributeDefinition("event_slug", ScalarAttributeType.S),
                    new AttributeDefinition("event_time", ScalarAttributeType.N)
                },
                GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
                {
                    slugIndex,
                    processedIndex
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            await CreateTable(createRequest);
        }
        private void PopulateRequestFromSchemaObject(TableSchema schemaObject, CreateTableRequest request)
        {
            if (!schemaObject.HasDefinedKeys)
            {
                ThrowArgumentError("Key schema not defined on input object.", Schema);
            }
            if (!schemaObject.HasDefinedAttributes)
            {
                ThrowArgumentError("Attribute schema containing keys not defined on input object.", Schema);
            }

            request.KeySchema            = schemaObject.KeySchema;
            request.AttributeDefinitions = schemaObject.AttributeSchema;

            // optional
            if (schemaObject.HasDefinedLocalSecondaryIndices)
            {
                // reconstruct each lsi so that the schema builder cmdlets can opt to not
                // set the primary hash key element, thus enabling re-use on multiple
                // table creation pipelines
                var requestIndices = new List <LocalSecondaryIndex>();
                foreach (var definedLSI in schemaObject.LocalSecondaryIndexSchema)
                {
                    var lsi = new LocalSecondaryIndex
                    {
                        IndexName = definedLSI.IndexName,
                        KeySchema = new List <KeySchemaElement>()
                    };
                    var hashKey = definedLSI.KeySchema.FirstOrDefault(k => k.KeyType.Equals(KeyType.HASH));
                    if (hashKey == null)
                    {
                        hashKey = schemaObject.KeySchema.FirstOrDefault(k => k.KeyType.Equals(KeyType.HASH));
                        if (hashKey == null)
                        {
                            throw new ArgumentException(string.Format("Unable to determine primary hash key from supplied schema to use in local secondary index {0}", definedLSI.IndexName));
                        }

                        // DynamoDB requires the hash key be first in the collection
                        lsi.KeySchema.Add(hashKey);
                        lsi.KeySchema.AddRange(definedLSI.KeySchema);
                    }
                    else
                    {
                        // primary hash already set, possibly from earlier setup so just re-use
                        lsi.KeySchema.AddRange(definedLSI.KeySchema);
                    }

                    if (definedLSI.Projection != null)
                    {
                        lsi.Projection = new Projection
                        {
                            ProjectionType   = definedLSI.Projection.ProjectionType,
                            NonKeyAttributes = definedLSI.Projection.NonKeyAttributes
                        };
                    }

                    requestIndices.Add(lsi);
                }

                request.LocalSecondaryIndexes = requestIndices;
            }

            // optional
            if (schemaObject.HasDefinedGlobalSecondaryIndices)
            {
                // reconstruct each lsi so that the schema builder cmdlets can opt to not
                // set the primary hash key element, thus enabling re-use on multiple
                // table creation pipelines
                var requestIndices = new List <GlobalSecondaryIndex>();
                foreach (var definedGSI in schemaObject.GlobalSecondaryIndexSchema)
                {
                    var gsi = new GlobalSecondaryIndex
                    {
                        IndexName             = definedGSI.IndexName,
                        KeySchema             = new List <KeySchemaElement>(),
                        ProvisionedThroughput = new ProvisionedThroughput
                        {
                            ReadCapacityUnits  = definedGSI.ProvisionedThroughput.ReadCapacityUnits,
                            WriteCapacityUnits = definedGSI.ProvisionedThroughput.WriteCapacityUnits
                        }
                    };

                    var hashKey = definedGSI.KeySchema.FirstOrDefault(k => k.KeyType.Equals(KeyType.HASH));
                    if (hashKey == null)
                    {
                        hashKey = schemaObject.KeySchema.FirstOrDefault(k => k.KeyType.Equals(KeyType.HASH));
                        if (hashKey == null)
                        {
                            throw new ArgumentException(string.Format("Unable to determine primary hash key from supplied schema to use in global secondary index {0}", definedGSI.IndexName));
                        }

                        // DynamoDB requires the hash key be first in the collection
                        gsi.KeySchema.Add(hashKey);
                        gsi.KeySchema.AddRange(definedGSI.KeySchema);
                    }
                    else
                    {
                        // primary hash already set, possibly from earlier setup so just re-use
                        gsi.KeySchema.AddRange(definedGSI.KeySchema);
                    }

                    if (definedGSI.Projection != null)
                    {
                        gsi.Projection = new Projection
                        {
                            ProjectionType   = definedGSI.Projection.ProjectionType,
                            NonKeyAttributes = definedGSI.Projection.NonKeyAttributes
                        };
                    }

                    requestIndices.Add(gsi);
                }

                request.GlobalSecondaryIndexes = requestIndices;
            }
        }
Exemplo n.º 14
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);
            }
        }
        private static void CreateTable()
        {
            // Attribute definitions
            var attributeDefinitions = new List <AttributeDefinition>()
            {
                { new AttributeDefinition {
                      AttributeName = "IssueId", AttributeType = "S"
                  } },
                { new AttributeDefinition {
                      AttributeName = "Title", AttributeType = "S"
                  } },
                { new AttributeDefinition {
                      AttributeName = "CreateDate", AttributeType = "S"
                  } },
                { new AttributeDefinition {
                      AttributeName = "DueDate", AttributeType = "S"
                  } }
            };

            // Key schema for table
            var tableKeySchema = new List <KeySchemaElement>()
            {
                {
                    new KeySchemaElement {
                        AttributeName = "IssueId",
                        KeyType       = "HASH" //Partition key
                    }
                },
                {
                    new KeySchemaElement {
                        AttributeName = "Title",
                        KeyType       = "RANGE" //Sort key
                    }
                }
            };

            // Initial provisioned throughput settings for the indexes
            var ptIndex = new ProvisionedThroughput
            {
                ReadCapacityUnits  = 1L,
                WriteCapacityUnits = 1L
            };

            // CreateDateIndex
            var createDateIndex = new GlobalSecondaryIndex()
            {
                IndexName             = "CreateDateIndex",
                ProvisionedThroughput = ptIndex,
                KeySchema             =
                {
                    new KeySchemaElement {
                        AttributeName = "CreateDate", KeyType = "HASH" //Partition key
                    },
                    new KeySchemaElement {
                        AttributeName = "IssueId", KeyType = "RANGE" //Sort key
                    }
                },
                Projection = new Projection
                {
                    ProjectionType   = "INCLUDE",
                    NonKeyAttributes =
                    {
                        "Description", "Status"
                    }
                }
            };

            // TitleIndex
            var titleIndex = new GlobalSecondaryIndex()
            {
                IndexName             = "TitleIndex",
                ProvisionedThroughput = ptIndex,
                KeySchema             =
                {
                    new KeySchemaElement {
                        AttributeName = "Title", KeyType = "HASH" //Partition key
                    },
                    new KeySchemaElement {
                        AttributeName = "IssueId", KeyType = "RANGE" //Sort key
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = "KEYS_ONLY"
                }
            };

            // DueDateIndex
            var dueDateIndex = new GlobalSecondaryIndex()
            {
                IndexName             = "DueDateIndex",
                ProvisionedThroughput = ptIndex,
                KeySchema             =
                {
                    new KeySchemaElement {
                        AttributeName = "DueDate",
                        KeyType       = "HASH" //Partition key
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = "ALL"
                }
            };



            var createTableRequest = new CreateTableRequest
            {
                TableName             = tableName,
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = (long)1,
                    WriteCapacityUnits = (long)1
                },
                AttributeDefinitions   = attributeDefinitions,
                KeySchema              = tableKeySchema,
                GlobalSecondaryIndexes =
                {
                    createDateIndex, titleIndex, dueDateIndex
                }
            };

            Console.WriteLine("Creating table " + tableName + "...");
            client.CreateTable(createTableRequest);

            WaitUntilTableReady(tableName);
        }
Exemplo n.º 16
0
 /**
  * Create an instance of CreateTableRequest class
  * Create table using the request defined
  *
  * @param attributeDefinitions    Attribute definitions for the table
  * @param tableKeySchema          Table's key schema
  * @param gsi                     Global secondary index
  */
 private static void CreateInfectionsTableWithIndex(List <AttributeDefinition> attributeDefinitions, List <KeySchemaElement> tableKeySchema, GlobalSecondaryIndex gsi)
 {
     // STUDENT TODO 2: Replace the solution with your own code
     Solution.CreateInfectionsTableWithIndex(dynamoDBClient, InfectionsTableName, attributeDefinitions, tableKeySchema, gsi);
 }
Exemplo n.º 17
0
        /**
         * Create an instance of CreateTableRequest class
         * Create table using the request defined
         *
         * @param attributeDefinitions    Attribute definitions for the table
         * @param tableKeySchema          Table's key schema
         * @param gsi                     Global secondary index
         */
        private static void CreateInfectionsTableWithIndex(List <AttributeDefinition> attributeDefinitions, List <KeySchemaElement> tableKeySchema, GlobalSecondaryIndex gsi)
        {
            CreateTableRequest request = new CreateTableRequest
            {
                TableName             = InfectionsTableName,
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 5L,
                    WriteCapacityUnits = 10L
                },
                AttributeDefinitions   = attributeDefinitions,
                KeySchema              = tableKeySchema,
                GlobalSecondaryIndexes = { gsi },
            };

            dynamoDBClient.CreateTable(request);
        }
Exemplo n.º 18
0
        /// <summary>Initialize current instance with specific global configuration and logger</summary>
        public Task Init()
        {
            this.storage = new DynamoDBStorage(
                this.logger,
                this.options.Service,
                this.options.AccessKey,
                this.options.SecretKey,
                this.options.Token,
                this.options.ProfileName,
                this.options.ReadCapacityUnits,
                this.options.WriteCapacityUnits,
                this.options.UseProvisionedThroughput,
                this.options.CreateIfNotExists,
                this.options.UpdateIfExists);

            this.logger.LogInformation((int)ErrorCode.ReminderServiceBase, "Initializing AWS DynamoDB Reminders Table");

            var serviceIdGrainHashGlobalSecondaryIndex = new GlobalSecondaryIndex
            {
                IndexName  = SERVICE_ID_GRAIN_HASH_INDEX,
                Projection = new Projection {
                    ProjectionType = ProjectionType.ALL
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement {
                        AttributeName = SERVICE_ID_PROPERTY_NAME, KeyType = KeyType.HASH
                    },
                    new KeySchemaElement {
                        AttributeName = GRAIN_HASH_PROPERTY_NAME, KeyType = KeyType.RANGE
                    }
                }
            };

            var serviceIdGrainReferenceGlobalSecondaryIndex = new GlobalSecondaryIndex
            {
                IndexName  = SERVICE_ID_GRAIN_REFERENCE_INDEX,
                Projection = new Projection {
                    ProjectionType = ProjectionType.ALL
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement {
                        AttributeName = SERVICE_ID_PROPERTY_NAME, KeyType = KeyType.HASH
                    },
                    new KeySchemaElement {
                        AttributeName = GRAIN_REFERENCE_PROPERTY_NAME, KeyType = KeyType.RANGE
                    }
                }
            };

            return(this.storage.InitializeTable(this.options.TableName,
                                                new List <KeySchemaElement>
            {
                new KeySchemaElement {
                    AttributeName = REMINDER_ID_PROPERTY_NAME, KeyType = KeyType.HASH
                },
                new KeySchemaElement {
                    AttributeName = GRAIN_HASH_PROPERTY_NAME, KeyType = KeyType.RANGE
                }
            },
                                                new List <AttributeDefinition>
            {
                new AttributeDefinition {
                    AttributeName = REMINDER_ID_PROPERTY_NAME, AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition {
                    AttributeName = GRAIN_HASH_PROPERTY_NAME, AttributeType = ScalarAttributeType.N
                },
                new AttributeDefinition {
                    AttributeName = SERVICE_ID_PROPERTY_NAME, AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition {
                    AttributeName = GRAIN_REFERENCE_PROPERTY_NAME, AttributeType = ScalarAttributeType.S
                }
            },
                                                new List <GlobalSecondaryIndex> {
                serviceIdGrainHashGlobalSecondaryIndex, serviceIdGrainReferenceGlobalSecondaryIndex
            }));
        }
Exemplo n.º 19
0
    /// <summary>
    /// Creates tables that don't exist, with LocalIndexes and GlobalIndexes when found
    /// </summary>
    /// <param name="assembly">The assembly that contains DynamoDb entities</param>
    /// <returns></returns>
    public async Task <StringBuilder> CreateTablesAsync(Assembly assembly)
    {
        StringBuilder oSb = new StringBuilder();

        var currentTables = await _oDynamoDBClient.ListTablesAsync();

        var tableNames = currentTables.TableNames;

        var types = GetTypesWithDynamoTableAttribute(assembly);

        var tablePrefix = _dynamoConfig.TablePrefix;

        foreach (var tbl in types)
        {
            var tblName = tablePrefix + tbl.GetCustomAttribute <DynamoDBTableAttribute>().TableName;
            if (!tableNames.Contains(tblName))
            {
                Projection projection = new Projection()
                {
                    ProjectionType = "INCLUDE"
                };
                List <string> nonKeyAttributes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBPropertyAttribute>() != null).Select(s => s.Name).ToList();
                projection.NonKeyAttributes = nonKeyAttributes;

                CreateTableRequest reqCreateTable = new CreateTableRequest();
                reqCreateTable.TableName             = tblName;
                reqCreateTable.ProvisionedThroughput = new ProvisionedThroughput();
                reqCreateTable.ProvisionedThroughput.ReadCapacityUnits  = 1;
                reqCreateTable.ProvisionedThroughput.WriteCapacityUnits = 1;

                var hashKeyProperty = tbl.GetProperties().FirstOrDefault(p => p.GetCustomAttribute <DynamoDBHashKeyAttribute>() != null);

                if (hashKeyProperty != null)
                {
                    string hashKeyName = hashKeyProperty.Name;
                    if (hashKeyProperty.GetCustomAttribute <DynamoDBHashKeyAttribute>().AttributeName != null)
                    {
                        hashKeyName = hashKeyProperty.GetCustomAttribute <DynamoDBHashKeyAttribute>().AttributeName;
                    }

                    var hashKeySchema = new KeySchemaElement()
                    {
                        AttributeName = hashKeyName, KeyType = KeyType.HASH
                    };
                    reqCreateTable.KeySchema.Add(hashKeySchema);

                    reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(hashKeyProperty, hashKeyName));

                    // adding range property if exists
                    var rangeyProperty = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBRangeKeyAttribute>() != null).FirstOrDefault();

                    if (rangeyProperty != null)
                    {
                        string rangeKeyName = rangeyProperty.Name;
                        if (rangeyProperty.GetCustomAttribute <DynamoDBRangeKeyAttribute>().AttributeName != null)
                        {
                            rangeKeyName = rangeyProperty.GetCustomAttribute <DynamoDBRangeKeyAttribute>().AttributeName;
                        }

                        reqCreateTable.KeySchema.Add(new KeySchemaElement()
                        {
                            AttributeName = rangeKeyName, KeyType = KeyType.RANGE
                        });
                        reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(rangeyProperty, rangeKeyName));

                        // adding local secondary indexes
                        var localIndexes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBLocalSecondaryIndexRangeKeyAttribute>() != null);
                        foreach (var indexProperty in localIndexes)
                        {
                            // usually only one index is assigned
                            string indexName = indexProperty.GetCustomAttribute <DynamoDBLocalSecondaryIndexRangeKeyAttribute>().IndexNames.First();
                            List <KeySchemaElement> indexKeySchema = new List <KeySchemaElement>();
                            indexKeySchema.Add(hashKeySchema);
                            indexKeySchema.Add(new KeySchemaElement()
                            {
                                AttributeName = indexProperty.Name, KeyType = KeyType.RANGE
                            });

                            reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(indexProperty, indexProperty.Name));

                            LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex()
                            {
                                IndexName  = indexName,
                                KeySchema  = indexKeySchema,
                                Projection = projection
                            };

                            reqCreateTable.LocalSecondaryIndexes.Add(localSecondaryIndex);
                        }
                    }

                    // adding local secondary indexes
                    var globalIndexes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>() != null);
                    if (globalIndexes.Count() > 0)
                    {
                        foreach (var globalIndexProperty in globalIndexes)
                        {
                            var globalKeySchema = new List <KeySchemaElement>();

                            string globalHashKeyName = globalIndexProperty.Name;
                            if (globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().AttributeName != null)
                            {
                                globalHashKeyName = globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().AttributeName;
                            }

                            var globalHashKeySchema = new KeySchemaElement()
                            {
                                AttributeName = globalHashKeyName, KeyType = KeyType.HASH
                            };
                            globalKeySchema.Add(globalHashKeySchema);
                            reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(globalIndexProperty, globalHashKeyName));

                            string thisGlobalIndexName = globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().IndexNames.First();

                            // find the range keys related to this index
                            var globalRangeProperty = tbl.GetProperties().SingleOrDefault(p => p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>() != null && p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().IndexNames.Contains(thisGlobalIndexName));
                            if (globalRangeProperty != null)
                            {
                                string globalRangeKeyName = globalRangeProperty.Name;
                                if (globalRangeProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().AttributeName != null)
                                {
                                    globalRangeKeyName = globalRangeProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().AttributeName;
                                }

                                var globalRangeKeySchema = new KeySchemaElement()
                                {
                                    AttributeName = globalRangeKeyName, KeyType = KeyType.RANGE
                                };
                                globalKeySchema.Add(globalRangeKeySchema);
                                reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(globalRangeProperty, globalRangeKeyName));
                            }

                            GlobalSecondaryIndex globalSecondaryIndex = new GlobalSecondaryIndex()
                            {
                                IndexName             = thisGlobalIndexName,
                                KeySchema             = globalKeySchema,
                                Projection            = projection,
                                ProvisionedThroughput = new ProvisionedThroughput(1, 1)
                            };

                            reqCreateTable.GlobalSecondaryIndexes.Add(globalSecondaryIndex);
                        }
                    }

                    var response = await _oDynamoDBClient.CreateTableAsync(reqCreateTable);

                    // tables aren't created instantly
                    await WaitUntilTableReadyAsync(reqCreateTable.TableName);

                    foreach (var kv in response.ResponseMetadata.Metadata)
                    {
                        oSb.AppendLine($"{kv.Key} {kv.Value}");
                    }
                }
                else
                {
                    oSb.AppendLine($"{tblName} table has no hash key property");
                }
            }
        }
        return(oSb);
    }
        public void Run()
        {
            var dbClient = new AmazonDynamoDBClientProvider().Get();

            var typeDataIndex = new GlobalSecondaryIndex
            {
                IndexName             = "Type-Data-index",
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                },
                Projection = new Projection
                {
                    ProjectionType = ProjectionType.ALL
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Type",
                        KeyType       = KeyType.HASH
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "Data",
                        KeyType       = KeyType.RANGE
                    }
                }
            };

            var rangeHashIndex = new GlobalSecondaryIndex
            {
                IndexName             = "Range-Hash-index",
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                },
                Projection = new Projection
                {
                    ProjectionType = ProjectionType.ALL
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "RANGE",
                        KeyType       = KeyType.HASH
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "HASH",
                        KeyType       = KeyType.RANGE
                    }
                }
            };

            var createTableRequest = new CreateTableRequest(
                tableName: "AdjacencyListDemo",
                keySchema: new List <KeySchemaElement>
            {
                new KeySchemaElement
                {
                    AttributeName = "HASH",
                    KeyType       = KeyType.HASH
                },
                new KeySchemaElement
                {
                    AttributeName = "RANGE",
                    KeyType       = KeyType.RANGE
                }
            },
                attributeDefinitions: new List <AttributeDefinition>
            {
                new AttributeDefinition
                {
                    AttributeName = "HASH",
                    AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition
                {
                    AttributeName = "RANGE",
                    AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition
                {
                    AttributeName = "Type",
                    AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition
                {
                    AttributeName = "Data",
                    AttributeType = ScalarAttributeType.S
                }
            },
                provisionedThroughput: new ProvisionedThroughput
            {
                ReadCapacityUnits  = 1,
                WriteCapacityUnits = 1
            }
                )
            {
                GlobalSecondaryIndexes = new List <GlobalSecondaryIndex> {
                    typeDataIndex, rangeHashIndex
                }
            };


            var result = dbClient.CreateTableAsync(createTableRequest).Result;
        }
Exemplo n.º 21
0
        private void CreateMemoriesTable(AmazonDynamoDBClient client)
        {
            var provisionedThroughput = new ProvisionedThroughput(1, 1);

            var attributeDefinition = new List <AttributeDefinition>
            {
                new AttributeDefinition
                {
                    AttributeName = "Id",
                    AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition
                {
                    AttributeName = "BabyId",
                    AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition
                {
                    AttributeName = "Description",
                    AttributeType = ScalarAttributeType.S
                }
            };

            var keySchema = new List <KeySchemaElement>()
            {
                new KeySchemaElement
                {
                    AttributeName = "Id",
                    KeyType       = KeyType.HASH
                }
            };

            var babyIdIndex = new GlobalSecondaryIndex()
            {
                IndexName             = "BabyIdIndex",
                ProvisionedThroughput = provisionedThroughput,
                Projection            = new Projection {
                    ProjectionType = ProjectionType.ALL
                },
                KeySchema =
                {
                    new KeySchemaElement
                    {
                        AttributeName = "BabyId",
                        KeyType       = KeyType.HASH
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "Description",
                        KeyType       = KeyType.RANGE
                    }
                }
            };


            // Build a 'CreateTableRequest' for the new table

            var createRequest = new CreateTableRequest
            {
                TableName              = BabyMemoryConstants.MemoriesTableName,
                AttributeDefinitions   = attributeDefinition,
                ProvisionedThroughput  = provisionedThroughput,
                KeySchema              = keySchema,
                GlobalSecondaryIndexes = { babyIdIndex }
            };

            try
            {
                var createResponse = client.CreateTable(createRequest);
                Console.WriteLine("\n Table Created " + createResponse.TableDescription);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: failed to create the new table; " + ex.Message);
            }
        }
Exemplo n.º 22
0
    private IEnumerable <CreateTableRequest> ReturnCreateTableRequests(IEnumerable <Type> types, string tablePrefix, IEnumerable <string> tableNames)
    {
        List <CreateTableRequest> tRequests = new List <CreateTableRequest>();

        foreach (var tbl in types)
        {
            var tblName = tablePrefix + tbl.GetCustomAttribute <DynamoDBTableAttribute>().TableName;
            if (!tableNames.Contains(tblName))
            {
                Projection projection = new Projection()
                {
                    ProjectionType = "INCLUDE"
                };
                List <string> nonKeyAttributes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBPropertyAttribute>() != null).Select(s => s.Name).ToList();
                projection.NonKeyAttributes = nonKeyAttributes;

                CreateTableRequest reqCreateTable = new CreateTableRequest();
                reqCreateTable.TableName             = tblName;
                reqCreateTable.ProvisionedThroughput = new ProvisionedThroughput();
                reqCreateTable.ProvisionedThroughput.ReadCapacityUnits  = 1;
                reqCreateTable.ProvisionedThroughput.WriteCapacityUnits = 1;

                var hashKeyProperty = tbl.GetProperties().FirstOrDefault(p => p.GetCustomAttribute <DynamoDBHashKeyAttribute>() != null);

                if (hashKeyProperty != null)
                {
                    string hashKeyName = hashKeyProperty.Name;
                    if (hashKeyProperty.GetCustomAttribute <DynamoDBHashKeyAttribute>().AttributeName != null)
                    {
                        hashKeyName = hashKeyProperty.GetCustomAttribute <DynamoDBHashKeyAttribute>().AttributeName;
                    }

                    var hashKeySchema = new KeySchemaElement()
                    {
                        AttributeName = hashKeyName, KeyType = KeyType.HASH
                    };
                    reqCreateTable.KeySchema.Add(hashKeySchema);

                    reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(hashKeyProperty, hashKeyName));

                    // adding range property if exists
                    var rangeyProperty = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBRangeKeyAttribute>() != null).FirstOrDefault();

                    if (rangeyProperty != null)
                    {
                        string rangeKeyName = rangeyProperty.Name;
                        if (rangeyProperty.GetCustomAttribute <DynamoDBRangeKeyAttribute>().AttributeName != null)
                        {
                            rangeKeyName = rangeyProperty.GetCustomAttribute <DynamoDBRangeKeyAttribute>().AttributeName;
                        }

                        reqCreateTable.KeySchema.Add(new KeySchemaElement()
                        {
                            AttributeName = rangeKeyName, KeyType = KeyType.RANGE
                        });
                        reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(rangeyProperty, rangeKeyName));

                        // adding local secondary indexes
                        var localIndexes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBLocalSecondaryIndexRangeKeyAttribute>() != null);
                        foreach (var indexProperty in localIndexes)
                        {
                            // usually only one index is assigned
                            string indexName = indexProperty.GetCustomAttribute <DynamoDBLocalSecondaryIndexRangeKeyAttribute>().IndexNames.First();
                            List <KeySchemaElement> indexKeySchema = new List <KeySchemaElement>();
                            indexKeySchema.Add(hashKeySchema);
                            indexKeySchema.Add(new KeySchemaElement()
                            {
                                AttributeName = indexProperty.Name, KeyType = KeyType.RANGE
                            });

                            reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(indexProperty, indexProperty.Name));

                            LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex()
                            {
                                IndexName  = indexName,
                                KeySchema  = indexKeySchema,
                                Projection = projection
                            };

                            reqCreateTable.LocalSecondaryIndexes.Add(localSecondaryIndex);
                        }
                    }

                    // adding global secondary indexes
                    var globalIndexes = tbl.GetProperties().Where(p => p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>() != null);
                    if (globalIndexes.Count() > 0)
                    {
                        foreach (var globalIndexProperty in globalIndexes)
                        {
                            var globalKeySchema = new List <KeySchemaElement>();

                            string globalHashKeyName = globalIndexProperty.Name;
                            if (globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().AttributeName != null)
                            {
                                globalHashKeyName = globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().AttributeName;
                            }

                            var globalHashKeySchema = new KeySchemaElement()
                            {
                                AttributeName = globalHashKeyName, KeyType = KeyType.HASH
                            };
                            globalKeySchema.Add(globalHashKeySchema);
                            reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(globalIndexProperty, globalHashKeyName));

                            string thisGlobalIndexName = globalIndexProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexHashKeyAttribute>().IndexNames.First();

                            // find the range keys related to this index
                            var globalRangeProperty = tbl.GetProperties().SingleOrDefault(p => p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>() != null && p.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().IndexNames.Contains(thisGlobalIndexName));
                            if (globalRangeProperty != null)
                            {
                                string globalRangeKeyName = globalRangeProperty.Name;
                                if (globalRangeProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().AttributeName != null)
                                {
                                    globalRangeKeyName = globalRangeProperty.GetCustomAttribute <DynamoDBGlobalSecondaryIndexRangeKeyAttribute>().AttributeName;
                                }

                                var globalRangeKeySchema = new KeySchemaElement()
                                {
                                    AttributeName = globalRangeKeyName, KeyType = KeyType.RANGE
                                };
                                globalKeySchema.Add(globalRangeKeySchema);
                                reqCreateTable.AttributeDefinitions.Add(ReturnAttributeDefinition(globalRangeProperty, globalRangeKeyName));
                            }

                            GlobalSecondaryIndex globalSecondaryIndex = new GlobalSecondaryIndex()
                            {
                                IndexName             = thisGlobalIndexName,
                                KeySchema             = globalKeySchema,
                                Projection            = projection,
                                ProvisionedThroughput = new ProvisionedThroughput(1, 1)
                            };

                            reqCreateTable.GlobalSecondaryIndexes.Add(globalSecondaryIndex);
                        }
                    }

                    tRequests.Add(reqCreateTable);
                }
            }
        }

        return(tRequests);
    }
Exemplo n.º 23
0
        private static void ExtractGlobalSecondaryIndexDefinitionFromExpression
        (
            Expression <Func <TEntity, GlobalSecondaryIndexDefinition> > indexDefExp,
            out GlobalSecondaryIndex indexDefinition,
            out IDictionary <string, Type> keyFields
        )
        {
            var memberInitExp = indexDefExp.Body as MemberInitExpression;

            if
            (
                (memberInitExp == null)
                ||
                (memberInitExp.Type != typeof(GlobalSecondaryIndexDefinition))
            )
            {
                throw new InvalidOperationException(string.Format("The provided expression {0} is invalid", indexDefExp));
            }

            indexDefinition = new GlobalSecondaryIndex
            {
                // all fields are always projected!
                Projection = new Projection {
                    ProjectionType = "ALL"
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 5,
                    WriteCapacityUnits = 5
                }
            };
            var keyFieldList = new Dictionary <string, Type>();

            // detaching global secondary index parameters from the expression
            foreach (var propAssignmentExp in memberInitExp.Bindings.Cast <MemberAssignment>().Where(exp => exp.BindingType == MemberBindingType.Assignment))
            {
                var keyPropertyExp = propAssignmentExp.Expression as MemberExpression;
                if (keyPropertyExp == null)
                {
                    // there might be a boxing operation
                    var convertExp = propAssignmentExp.Expression as UnaryExpression;
                    if (convertExp != null)
                    {
                        keyPropertyExp = (MemberExpression)convertExp.Operand;
                    }
                }

                switch (propAssignmentExp.Member.Name)
                {
                case "HashKeyField":
                {
                    if (keyPropertyExp == null)
                    {
                        throw new InvalidOperationException("HashKeyField expression is of wrong type");
                    }

                    indexDefinition.KeySchema.Add(new KeySchemaElement {
                            KeyType = "HASH", AttributeName = keyPropertyExp.Member.Name
                        });

                    keyFieldList[keyPropertyExp.Member.Name] = ((PropertyInfo)keyPropertyExp.Member).PropertyType;
                }
                break;

                case "RangeKeyField":
                {
                    if (keyPropertyExp == null)
                    {
                        throw new InvalidOperationException("RangeKeyField expression is of wrong type");
                    }

                    indexDefinition.KeySchema.Add(new KeySchemaElement {
                            KeyType = "RANGE", AttributeName = keyPropertyExp.Member.Name
                        });

                    keyFieldList[keyPropertyExp.Member.Name] = ((PropertyInfo)keyPropertyExp.Member).PropertyType;
                }
                break;

                case "ReadCapacityUnits":
                {
                    var capacityUnits = Expression.Lambda(propAssignmentExp.Expression).Compile().DynamicInvoke();
                    indexDefinition.ProvisionedThroughput.ReadCapacityUnits = (long)capacityUnits;
                }
                break;

                case "WriteCapacityUnits":
                {
                    var capacityUnits = Expression.Lambda(propAssignmentExp.Expression).Compile().DynamicInvoke();
                    indexDefinition.ProvisionedThroughput.WriteCapacityUnits = (long)capacityUnits;
                }
                break;
                }
            }

            indexDefinition.IndexName =
                indexDefinition.KeySchema.Aggregate(string.Empty, (current, keySchemaElement) => current + keySchemaElement.AttributeName)
                +
                "Index";

            keyFields = keyFieldList;
        }
Exemplo n.º 24
0
        public void Test6GlobalSeconaryIndex()
        {
            var tableName = "GameHistory";

            DeleteTable(tableName);

            var gsiSeaonalKeyPlayer = new GlobalSecondaryIndex {
                IndexName             = "SeasonalKeyPlayerIndex",
                ProvisionedThroughput = new ProvisionedThroughput {
                    ReadCapacityUnits  = 5,
                    WriteCapacityUnits = 1
                },
                Projection = new Projection {
                    ProjectionType = "ALL"
                },
                KeySchema = new List <KeySchemaElement> {
                    { new KeySchemaElement {
                          AttributeName = "SeasonId", KeyType = "HASH"
                      } },
                    { new KeySchemaElement {
                          AttributeName = "KeyPlayerId", KeyType = "RANGE"
                      } }
                }
            };

            var createTableRequest = new CreateTableRequest {
                TableName = tableName,
                KeySchema = new List <KeySchemaElement> {
                    new KeySchemaElement("GameId", KeyType.HASH),
                    new KeySchemaElement("GameType", KeyType.RANGE)
                },
                AttributeDefinitions = new List <AttributeDefinition> {
                    new AttributeDefinition("GameId", ScalarAttributeType.N),
                    new AttributeDefinition("GameType", ScalarAttributeType.N),
                    new AttributeDefinition("SeasonId", ScalarAttributeType.N),
                    new AttributeDefinition("KeyPlayerId", ScalarAttributeType.N)
                },
                ProvisionedThroughput  = new ProvisionedThroughput(1, 1),
                GlobalSecondaryIndexes = { gsiSeaonalKeyPlayer }
            };

            var response = Client.CreateTable(createTableRequest);

            Assert.AreEqual(response.HttpStatusCode, HttpStatusCode.OK);

            var queryRequest = new QueryRequest {
                TableName = tableName,
                IndexName = gsiSeaonalKeyPlayer.IndexName,
                KeyConditionExpression    = "SeasonId = :season_id and KeyPlayerId = :player_id",
                ExpressionAttributeValues = new Dictionary <string, AttributeValue> {
                    { ":season_id", new AttributeValue {
                          N = "1"
                      } },
                    { ":player_id", new AttributeValue {
                          N = "1234"
                      } }
                },
                ScanIndexForward = true
            };

            var queryResponse = Client.Query(queryRequest);
        }
Exemplo n.º 25
0
        private async Task TableCreateSecondaryIndex(string tableName, List <AttributeDefinition> attributes, GlobalSecondaryIndex secondaryIndex)
        {
            await ddbClient.UpdateTableAsync(new UpdateTableRequest
            {
                TableName = tableName,
                GlobalSecondaryIndexUpdates = new List <GlobalSecondaryIndexUpdate>
                {
                    new GlobalSecondaryIndexUpdate
                    {
                        Create = new CreateGlobalSecondaryIndexAction()
                        {
                            IndexName             = secondaryIndex.IndexName,
                            Projection            = secondaryIndex.Projection,
                            ProvisionedThroughput = provisionedThroughput,
                            KeySchema             = secondaryIndex.KeySchema
                        }
                    }
                },
                AttributeDefinitions = attributes
            });

            // Adding a GSI to a table is an eventually consistent operation and we might miss the table UPDATING status if we query the table status imediatelly after the table update call.
            // Creating a GSI takes significantly longer than 1 second and therefore this delay does not add time to the total duration of this method.
            await Task.Delay(1000);

            // When adding a GSI, the table briefly changes its status to UPDATING. The GSI creation process usually takes longer.
            // For this reason, we will wait for both the table and the index to become ACTIVE before marking the operation as complete.
            await TableWaitOnStatusAsync(tableName, TableStatus.UPDATING, TableStatus.ACTIVE);
            await TableIndexWaitOnStatusAsync(tableName, secondaryIndex.IndexName, IndexStatus.CREATING, IndexStatus.ACTIVE);
        }
Exemplo n.º 26
0
        public static void CreateInfectionsTableWithIndex(AmazonDynamoDBClient dynamoDBClient, string InfectionsTableName, List <AttributeDefinition> attributeDefinitions, List <KeySchemaElement> tableKeySchema, GlobalSecondaryIndex gsi)
        {
            Debug.WriteLine(String.Format("RUNNING SOLUTION CODE: {0}! Follow the steps in the lab guide to replace this method with your own implementation.\n", "CreateInfectionsTableWithIndex"));
            CreateTableRequest request = new CreateTableRequest
            {
                TableName             = InfectionsTableName,
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 5L,
                    WriteCapacityUnits = 10L
                },
                AttributeDefinitions   = attributeDefinitions,
                KeySchema              = tableKeySchema,
                GlobalSecondaryIndexes = { gsi },
            };

            dynamoDBClient.CreateTable(request);
        }
Exemplo n.º 27
0
        private static void CreateInfectionsTable()
        {
            // Create an instance of a GlobalSecondaryIndex class
            GlobalSecondaryIndex gsi = new GlobalSecondaryIndex
            {
                IndexName             = CityDateIndexName,
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 5L,
                    WriteCapacityUnits = 5L
                },
                Projection = new Projection {
                    ProjectionType = "ALL"
                }
            };

            var indexKeySchema = new List <KeySchemaElement>
            {
                { new KeySchemaElement {
                      AttributeName = "City", KeyType = "HASH"
                  } },
                { new KeySchemaElement {
                      AttributeName = "Date", KeyType = "RANGE"
                  } },
            };

            gsi.KeySchema = indexKeySchema;

            // Create attribute definitions for PatientId, City, Date.
            var attributeDefinitions = new List <AttributeDefinition>()
            {
                {
                    new AttributeDefinition {
                        AttributeName = "PatientID",
                        AttributeType = "S",
                    }
                },
                {
                    new AttributeDefinition {
                        AttributeName = "City",
                        AttributeType = "S",
                    }
                },
                {
                    new AttributeDefinition()
                    {
                        AttributeName = "Date",
                        AttributeType = "S",
                    }
                },
            };
            // Table key schema
            var tableKeySchema = new List <KeySchemaElement>()
            {
                {
                    new KeySchemaElement {
                        AttributeName = "PatientID",
                        KeyType       = "HASH"
                    }
                }
            };

            Debug.WriteLine("Creating DynamoDB table.");
            CreateInfectionsTableWithIndex(attributeDefinitions, tableKeySchema, gsi);

            string status = null;

            do
            {
                status = GetTableStatus();
            } while (status != TableStatus.ACTIVE);

            Debug.WriteLine("Table successfully created.");
        }
Exemplo n.º 28
0
        private static async Task CreateTableAndIndex(IAmazonDynamoDB client)
        {
            ListTablesResponse listTablesResponse = await client.ListTablesAsync();

            if (listTablesResponse.TableNames.Contains(_tableName))
            {
                Console.WriteLine("Table already exists!. Deleting Table...");
                await client.DeleteTableAsync(_tableName);

                await Task.Delay(10000);
            }

            CreateTableRequest request = new CreateTableRequest();

            var attributeDefinitions = new[] {
                new AttributeDefinition {
                    AttributeName = "PatientID",
                    AttributeType = ScalarAttributeType.S,
                },
                new AttributeDefinition {
                    AttributeName = "City",
                    AttributeType = ScalarAttributeType.S,
                },
                new AttributeDefinition {
                    AttributeName = "Date",
                    AttributeType = ScalarAttributeType.S,
                }
            }.ToList();

            var gsi = new GlobalSecondaryIndex();

            gsi.IndexName  = _indexName;
            gsi.Projection = new Projection {
                ProjectionType = "ALL"
            };
            gsi.ProvisionedThroughput =
                new ProvisionedThroughput
            {
                ReadCapacityUnits  = 5L,
                WriteCapacityUnits = 5L
            };

            gsi.KeySchema = new[] {
                new KeySchemaElement("City", KeyType.HASH),
                new KeySchemaElement("Date", KeyType.RANGE)
            }.ToList();

            request.TableName            = _tableName;
            request.AttributeDefinitions = attributeDefinitions;
            request.KeySchema            = new[] {
                new KeySchemaElement("PatientID", KeyType.HASH)
            }.ToList();

            request.GlobalSecondaryIndexes = new[] {
                gsi
            }.ToList();

            request.ProvisionedThroughput =
                new ProvisionedThroughput
            {
                ReadCapacityUnits  = 5L,
                WriteCapacityUnits = 5L
            };

            CreateTableResponse response = await client.CreateTableAsync(request);

            if (response.HttpStatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine("Table creation failed!");
            }

            bool tableCreated = false;

            Console.Write("Creating table....");

            while (tableCreated == false)
            {
                await Task.Delay(500);

                DescribeTableResponse describeResponse = await client.DescribeTableAsync(_tableName);

                tableCreated = describeResponse.Table.TableStatus == TableStatus.ACTIVE;
                Console.Write("..");
            }

            Console.WriteLine();
        }