static AttributeValue ConvertFromAnnotation(DynamoDbScoreAnnotationV1 annotation) { var a = new Dictionary <string, AttributeValue>() { [DynamoDbScorePropertyNames.DataPropertyNames.AnnotationsPropertyNames.Id] = new AttributeValue() { N = annotation.Id.ToString(), } }; if (annotation.ContentHash != null) { a[DynamoDbScorePropertyNames.DataPropertyNames.AnnotationsPropertyNames.ContentHash] = new AttributeValue(annotation.ContentHash); } if (a.Count == 0) { return(null); } return(new AttributeValue() { M = a }); }
public async Task ReplaceAnnotationsAsync(Guid ownerId, Guid scoreId, List <PatchScoreAnnotation> annotations) { if (annotations.Count == 0) { throw new ArgumentException(nameof(annotations)); } var(data, oldHash) = await GetAsync(_dynamoDbClient, ScoreTableName, ownerId, scoreId); data.Annotations ??= new List <DynamoDbScoreAnnotationV1>(); // Key id, Value index var annotationIndices = new Dictionary <long, int>(); foreach (var(ann, index) in data.Annotations.Select((x, index) => (x, index))) { annotationIndices[ann.Id] = index; } var replacingAnnotations = new List <(DynamoDbScoreAnnotationV1 data, int targetIndex)>(); var existedAnnData = new HashSet <string>(); data.Annotations.ForEach(x => existedAnnData.Add(x.ContentHash)); var addAnnData = new Dictionary <string, PatchScoreAnnotation>(); foreach (var ann in annotations) { var id = ann.TargetAnnotationId; if (!annotationIndices.TryGetValue(id, out var index)) { throw new InvalidOperationException(); } var hash = DynamoDbScoreDataUtils.CalcHash(DynamoDbScoreDataUtils.AnnotationPrefix, ann.Content); if (!existedAnnData.Contains(hash)) { addAnnData[hash] = ann; } var a = new DynamoDbScoreAnnotationV1() { Id = id, ContentHash = hash, }; replacingAnnotations.Add((a, index)); data.Annotations[index] = a; } var removeAnnData = existedAnnData.ToHashSet(); foreach (var annotation in data.Annotations) { if (removeAnnData.Contains(annotation.ContentHash)) { removeAnnData.Remove(annotation.ContentHash); } } var newHash = data.CalcDataHash(); var now = ScoreDatabaseUtils.UnixTimeMillisecondsNow(); await UpdateAsync(_dynamoDbClient, ScoreTableName, ownerId, scoreId, replacingAnnotations, newHash, oldHash, now); await AddAnnListAsync(_dynamoDbClient, ScoreDataTableName, ownerId, scoreId, addAnnData); await RemoveAnnListAsync(_dynamoDbClient, ScoreDataTableName, ownerId, scoreId, removeAnnData);