Exemple #1
0
        internal Key MakeKey(IDictionary <string, DynamoDBEntry> doc)
        {
            Key key = new Key();

            foreach (var kvp in Keys)
            {
                string         keyName     = kvp.Key;
                KeyDescription description = kvp.Value;
                DynamoDBEntry  value;
                if (!doc.TryGetValue(keyName, out value))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Document does not contain value for key {0}", keyName));
                }
                value = value.ToConvertedEntry(Conversion);
                if (StoreAsEpoch.Contains(keyName))
                {
                    value = Document.DateTimeToEpochSeconds(value, keyName);
                }

                Primitive primitive = value.AsPrimitive();
                if (primitive == null)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Key attribute {0} must be a Primitive type", keyName));
                }
                if (primitive.Type != description.Type)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Key attribute {0} must be of type {1}", keyName, description.Type));
                }

                key[keyName] = primitive.ConvertToAttributeValue(new DynamoDBEntry.AttributeConversionConfig(Conversion));
            }
            return(key);
        }
Exemple #2
0
        internal Key MakeKey(Primitive hashKey, Primitive rangeKey)
        {
            Key newKey = new Key();

            if (HashKeys.Count != 1)
            {
                throw new InvalidOperationException("Must have one hash key defined for the table " + TableName);
            }
            string         hashKeyName        = HashKeys[0];
            KeyDescription hashKeyDescription = Keys[hashKeyName];

            if (this.StoreAsEpoch.Contains(hashKeyName))
            {
                hashKey = KeyDateTimeToEpochSeconds(hashKey, hashKeyName);
            }

            if (hashKeyDescription.Type != hashKey.Type)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                  "Schema for table {0}, hash key {1}, is inconsistent with specified hash key value.", TableName, hashKeyName));
            }

            var hashKeyAttributeValue = hashKey.ConvertToAttributeValue(new DynamoDBEntry.AttributeConversionConfig(Conversion));

            newKey[hashKeyName] = hashKeyAttributeValue;

            if ((rangeKey == null) != (RangeKeys.Count == 0))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                  "Schema for table {0}, range key {1}, is inconsistent with specified range key value.", TableName, hashKeyName));
            }
            else if (rangeKey != null)
            {
                string         rangeKeyName        = RangeKeys[0];
                KeyDescription rangeKeyDescription = Keys[rangeKeyName];

                if (this.StoreAsEpoch.Contains(rangeKeyName))
                {
                    rangeKey = KeyDateTimeToEpochSeconds(rangeKey, rangeKeyName);
                }

                if (rangeKeyDescription.Type != rangeKey.Type)
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
                                                                      "Schema for table {0}, range key {1}, is inconsistent with specified range key value.", TableName, hashKeyName));
                }
                var rangeKeyAttributeValue = rangeKey.ConvertToAttributeValue(new DynamoDBEntry.AttributeConversionConfig(Conversion));
                newKey[rangeKeyName] = rangeKeyAttributeValue;
            }

            return(newKey);
        }
Exemple #3
0
        private void LoadTableInfo()
        {
            ClearTableData();

            var              tableInfoCache = SdkCache.GetCache <string, TableDescription>(DDBClient, TableInfoCacheIdentifier, StringComparer.Ordinal);
            bool             staleCacheData;
            TableDescription table = tableInfoCache.GetValue(TableName, this.DescribeTable, out staleCacheData);

            if (staleCacheData)
            {
                var logger = Logger.GetLogger(typeof(Table));
                logger.InfoFormat("Description for table [{0}] loaded from SDK Cache", TableName);
            }

            foreach (var key in table.KeySchema)
            {
                string keyName = key.AttributeName;
                AttributeDefinition attributeDefinition = table.AttributeDefinitions
                                                          .FirstOrDefault(a => string.Equals(a.AttributeName, keyName, StringComparison.Ordinal));
                if (attributeDefinition == null)
                {
                    throw new InvalidOperationException("No attribute definition found for key " + key.AttributeName);
                }
                KeyDescription keyDescription = new KeyDescription
                {
                    IsHash = string.Equals(key.KeyType, "HASH", StringComparison.OrdinalIgnoreCase),
                    Type   = GetType(attributeDefinition.AttributeType)
                };
                if (keyDescription.IsHash)
                {
                    HashKeys.Add(keyName);
                }
                else
                {
                    RangeKeys.Add(keyName);
                }
                Keys[keyName] = keyDescription;
            }

            if (table.LocalSecondaryIndexes != null)
            {
                foreach (var index in table.LocalSecondaryIndexes)
                {
                    LocalSecondaryIndexes[index.IndexName] = index;
                    LocalSecondaryIndexNames.Add(index.IndexName);
                }
            }

            if (table.GlobalSecondaryIndexes != null)
            {
                foreach (var index in table.GlobalSecondaryIndexes)
                {
                    GlobalSecondaryIndexes[index.IndexName] = index;
                    GlobalSecondaryIndexNames.Add(index.IndexName);
                }
            }

            foreach (var attribute in table.AttributeDefinitions)
            {
                Attributes.Add(attribute);
            }
        }
        private void GetTableInfo()
        {
            DescribeTableRequest req = new DescribeTableRequest
            {
                TableName = TableName
            };

            req.BeforeRequestEvent += new RequestEventHandler(this.UserAgentRequestEventHandlerSync);
            DescribeTableResult info = this.DDBClient.DescribeTable(req);

            if (info.Table == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Table name {0} does not exist", TableName));
            }

            Keys.Clear();
            HashKeys.Clear();
            RangeKeys.Clear();
            TableDescription table = info.Table;

            foreach (var key in table.KeySchema)
            {
                string keyName = key.AttributeName;
                AttributeDefinition attributeDefinition = table.AttributeDefinitions
                                                          .FirstOrDefault(a => string.Equals(a.AttributeName, keyName, StringComparison.Ordinal));
                if (attributeDefinition == null)
                {
                    throw new InvalidOperationException("No attribute definition found for key " + key.AttributeName);
                }
                KeyDescription keyDescription = new KeyDescription
                {
                    IsHash = string.Equals(key.KeyType, "HASH", StringComparison.OrdinalIgnoreCase),
                    Type   = GetType(attributeDefinition.AttributeType)
                };
                if (keyDescription.IsHash)
                {
                    HashKeys.Add(keyName);
                }
                else
                {
                    RangeKeys.Add(keyName);
                }
                Keys[keyName] = keyDescription;
            }

            LocalSecondaryIndexes.Clear();
            LocalSecondaryIndexNames.Clear();
            if (table.LocalSecondaryIndexes != null)
            {
                foreach (var index in table.LocalSecondaryIndexes)
                {
                    LocalSecondaryIndexes[index.IndexName] = index;
                    LocalSecondaryIndexNames.Add(index.IndexName);
                }
            }

            GlobalSecondaryIndexes.Clear();
            GlobalSecondaryIndexNames.Clear();
            if (table.GlobalSecondaryIndexes != null)
            {
                foreach (var index in table.GlobalSecondaryIndexes)
                {
                    GlobalSecondaryIndexes[index.IndexName] = index;
                    GlobalSecondaryIndexNames.Add(index.IndexName);
                }
            }

            Attributes.Clear();
            foreach (var attribute in table.AttributeDefinitions)
            {
                Attributes.Add(attribute);
            }

            keyNames = Keys.Keys.ToArray();
        }