static (string itemIdText, Guid itemId, long size) GetItemAndSizeAsync(Dictionary <string, AttributeValue> items) { var sizeValue = items[ScoreItemDatabasePropertyNames.Size]; var size = long.Parse(sizeValue.N, CultureInfo.InvariantCulture); try { var type = items[ScoreItemDatabasePropertyNames.Type]; if (type.S == ScoreItemDatabaseConstant.TypeImage) { var thumbnail = items[ScoreItemDatabasePropertyNames.Thumbnail]; var thumbnailSizeValue = thumbnail.M[ScoreItemDatabasePropertyNames.ThumbnailPropertyNames.Size]; size += long.Parse(thumbnailSizeValue.N, CultureInfo.InvariantCulture); } } catch (Exception ex) { throw; } var itemIdText = items[ScoreItemDatabasePropertyNames.ItemId].S; var item = itemIdText.Substring(ScoreItemDatabaseConstant.ScoreIdLength); var itemId = ScoreDatabaseUtils.ConvertToGuid(item); return(itemIdText, itemId, size); }
/// <summary> /// DynamoDB のアイテムを変換する /// </summary> /// <param name="item"></param> /// <returns></returns> /// <exception cref="InvalidOperationException"></exception> public static ScoreItemDatabaseItemDataBase ConvertFromDynamoDbValue(Dictionary <string, AttributeValue> item) { var ownerIdValue = item[ScoreItemDatabasePropertyNames.OwnerId]; var itemIdValue = item[ScoreItemDatabasePropertyNames.ItemId]; var sizeValue = item[ScoreItemDatabasePropertyNames.Size]; var atValue = item[ScoreItemDatabasePropertyNames.At]; var typeValue = item[ScoreItemDatabasePropertyNames.Type]; var objNameValue = item[ScoreItemDatabasePropertyNames.ObjName]; var totalSizeValue = item[ScoreItemDatabasePropertyNames.TotalSize]; ScoreItemDatabaseItemDataBase result = default; if (typeValue.S == ScoreItemDatabaseConstant.TypeImage) { var orgNameValue = item[ScoreItemDatabasePropertyNames.OrgName]; var thumbnailValue = item[ScoreItemDatabasePropertyNames.Thumbnail]; var thumbObjNameValue = thumbnailValue.M[ScoreItemDatabasePropertyNames.ThumbnailPropertyNames.ObjName]; var thumbSizeValue = thumbnailValue.M[ScoreItemDatabasePropertyNames.ThumbnailPropertyNames.Size]; result = new ScoreItemDatabaseItemDataImage { OrgName = orgNameValue.S, Thumbnail = new ScoreItemDatabaseItemDataImageThumbnail { ObjName = thumbObjNameValue.S, Size = long.Parse(thumbSizeValue.N) } }; } else { throw new InvalidOperationException(); } result.Size = long.Parse(sizeValue.N); result.TotalSize = long.Parse(totalSizeValue.N); result.OwnerId = ScoreItemDatabaseUtils.ConvertFromPartitionKey(ownerIdValue.S); var scoreBase64 = itemIdValue.S.Substring(0, ScoreItemDatabaseConstant.ScoreIdLength); var itemBase64 = itemIdValue.S.Substring(ScoreItemDatabaseConstant.ScoreIdLength); result.ScoreId = ScoreDatabaseUtils.ConvertToGuid(scoreBase64); result.ItemId = ScoreDatabaseUtils.ConvertToGuid(itemBase64); result.ObjName = objNameValue.S; return(result); }
public static ScoreData Create(DynamoDbScoreDataV1 data) { return(new ScoreData() { Title = data.Title, DescriptionHash = data.DescriptionHash, Pages = data.Page.Select(x => new ScorePage() { Id = x.Id, Page = x.Page, ItemId = ScoreDatabaseUtils.ConvertToGuid(x.ItemId), ObjectName = x.ObjectName, }).ToArray(), Annotations = data.Annotations.Select(x => new ScoreAnnotation() { Id = x.Id, ContentHash = x.ContentHash, }).ToArray(), }); }
static async Task <ScoreSnapshotSummary[]> GetAsync(IAmazonDynamoDB client, string tableName, Guid ownerId, Guid scoreId) { var partitionKey = ScoreDatabaseUtils.ConvertToPartitionKey(ownerId); var score = ScoreDatabaseUtils.ConvertToBase64(scoreId); var request = new QueryRequest() { TableName = tableName, ExpressionAttributeNames = new Dictionary <string, string>() { ["#owner"] = DynamoDbScorePropertyNames.PartitionKey, ["#score"] = DynamoDbScorePropertyNames.SortKey, ["#snapshotName"] = DynamoDbScorePropertyNames.SnapshotName, ["#createAt"] = DynamoDbScorePropertyNames.CreateAt, }, ExpressionAttributeValues = new Dictionary <string, AttributeValue>() { [":owner"] = new AttributeValue(partitionKey), [":snapPrefix"] = new AttributeValue(ScoreDatabaseConstant.ScoreIdSnapPrefix + score), }, KeyConditionExpression = "#owner = :owner and begins_with(#score, :snapPrefix)", ProjectionExpression = "#score, #snapshotName, #createAt", }; try { var response = await client.QueryAsync(request); var subStartIndex = (ScoreDatabaseConstant.ScoreIdSnapPrefix + score).Length; return(response.Items .Select(x => ( score: x[DynamoDbScorePropertyNames.SortKey].S, name: x[DynamoDbScorePropertyNames.SnapshotName].S, createAt: x[DynamoDbScorePropertyNames.CreateAt].S) ) .Select(x => new ScoreSnapshotSummary() { Id = ScoreDatabaseUtils.ConvertToGuid(x.score.Substring(subStartIndex)), Name = x.name, CreateAt = ScoreDatabaseUtils.ConvertFromUnixTimeMilli(x.createAt), } ) .ToArray()); } catch (InternalServerErrorException ex) { Console.WriteLine(ex.Message); throw; } catch (ProvisionedThroughputExceededException ex) { Console.WriteLine(ex.Message); throw; } catch (RequestLimitExceededException ex) { Console.WriteLine(ex.Message); throw; } catch (ResourceNotFoundException ex) { Console.WriteLine(ex.Message); throw; } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } }
/// <summary> /// 楽譜のアイテムデータのパーティションキーから UUID に変換する /// </summary> /// <param name="partitionKey"></param> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public static Guid ConvertFromPartitionKey(string partitionKey) => ScoreDatabaseUtils.ConvertToGuid( partitionKey.Substring(ScoreItemDatabaseConstant.PartitionKeyPrefix.Length));