Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TimeToLiveOptions"/> class.
 /// </summary>
 /// <remarks>
 /// The following table shows the initial property values for an instance of <see cref="TimeToLiveOptions"/>.
 /// <list type="table">
 ///     <listheader>
 ///         <term>Property</term>
 ///         <description>Initial Value</description>
 ///     </listheader>
 ///     <item>
 ///         <term><see cref="Specification"/></term>
 ///         <description>A new instance of <see cref="TimeToLiveSpecification"/> initialized with <see cref="TimeToLiveSpecification.Enabled"/> set to <c>true</c> and <see cref="TimeToLiveSpecification.AttributeName"/> set to <c>epoch_expires</c>.</description>
 ///     </item>
 /// </list>
 /// </remarks>
 public TimeToLiveOptions()
 {
     Specification = new TimeToLiveSpecification()
     {
         AttributeName = SuggestedAttributeName,
         Enabled       = true
     };
 }
Exemplo n.º 2
0
 public UpdateTimeToLiveRequest(string tableName, string attributeName, bool enabled)
 {
     TableName = tableName ?? throw new ArgumentNullException(nameof(tableName));
     TimeToLiveSpecification = new TimeToLiveSpecification(attributeName, enabled);
 }
Exemplo n.º 3
0
        public static async Task CreateTableIfDoesNotExist(AmazonDynamoDBClient client, string table, string hashKey,
                                                           bool isHashKeyInt = false, string sortKey = null, bool isSortKeyInt = false, string timeToLiveAttribute = null)
        {
            bool tableExists = false;

            // Check for whether the quotey_quote table exists
            try
            {
                await client.DescribeTableAsync(table);

                tableExists = true;
            }
            catch (ResourceNotFoundException) { }

            // Create table if does not exist
            if (!tableExists)
            {
                Console.WriteLine($"{table} does not exist will try to create one");

                // Table attributes
                List <AttributeDefinition> attributes = new List <AttributeDefinition>
                {
                    // Only the indexes
                    new AttributeDefinition(hashKey, isHashKeyInt? ScalarAttributeType.N: ScalarAttributeType.S)
                };
                // Add sort key if was not null
                if (!string.IsNullOrEmpty(sortKey))
                {
                    attributes.Add(new AttributeDefinition(sortKey, isSortKeyInt ? ScalarAttributeType.N: ScalarAttributeType.S));
                }

                // Key Schema
                List <KeySchemaElement> keySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement(hashKey, KeyType.HASH)
                };
                // Add sort key if was not null
                if (!string.IsNullOrEmpty(sortKey))
                {
                    keySchema.Add(new KeySchemaElement(sortKey, KeyType.RANGE));
                }

                // Table creation request
                CreateTableRequest req = new CreateTableRequest
                {
                    TableName            = table,
                    KeySchema            = keySchema,
                    AttributeDefinitions = attributes,
                    BillingMode          = BillingMode.PAY_PER_REQUEST,
                };

                CreateTableResponse res = await client.CreateTableAsync(req);

                if (res.HttpStatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new Exception($"Could not create table {table}");
                }

                Console.WriteLine($"{table} successfully created");

                // Adding time to live if exists
                // It was not possible at the time of this to add ttl with table creation
                if (!string.IsNullOrEmpty(timeToLiveAttribute))
                {
                    // Sleep waiting for the table to be setup in the system then try adding ttl if needed
                    // May need less than 10s but to be always sure it works
                    await Task.Delay(10000);

                    TimeToLiveSpecification ttl = new TimeToLiveSpecification
                    {
                        AttributeName = timeToLiveAttribute,
                        Enabled       = true
                    };

                    UpdateTimeToLiveRequest ttlRequest = new UpdateTimeToLiveRequest
                    {
                        TableName = table,
                        TimeToLiveSpecification = ttl
                    };

                    UpdateTimeToLiveResponse ttlResponse = await client.UpdateTimeToLiveAsync(ttlRequest);

                    if (ttlResponse.HttpStatusCode == System.Net.HttpStatusCode.OK)
                    {
                        Console.WriteLine("Successfully added time to live specification");
                    }
                    else
                    {
                        throw new Exception($"Could not add ttl for the table {table}");
                    }
                }
            }
        }