Beispiel #1
0
        public async Task UpdateAsync()
        {
            if (eventStore is MongoEventStore mongoEventStore)
            {
                var collection = mongoEventStore.RawCollection;

                var filter = Builders <BsonDocument> .Filter;

                await collection.Find(new BsonDocument()).ForEachAsync(async commit =>
                {
                    foreach (BsonDocument @event in commit["Events"].AsBsonArray)
                    {
                        var meta = JObject.Parse(@event["Metadata"].AsString);
                        var data = JObject.Parse(@event["Payload"].AsString);

                        if (data.TryGetValue("appId", out var appId))
                        {
                            meta[SquidexHeaders.AppId] = NamedId <Guid> .Parse(appId.ToString(), Guid.TryParse).Id;
                        }

                        @event.Remove("EventId");
                        @event["Metadata"] = meta.ToBson();
                    }

                    await collection.ReplaceOneAsync(filter.Eq("_id", commit["_id"].AsString), commit);
                });
            }
        }
Beispiel #2
0
        protected override NamedId <Guid> ReadValue(JsonReader reader, Type objectType, JsonSerializer serializer)
        {
            if (reader.TokenType != JsonToken.String)
            {
                throw new JsonException($"Expected String, but got {reader.TokenType}.");
            }

            try
            {
                return(NamedId <Guid> .Parse(reader.Value.ToString(), Guid.TryParse));
            }
            catch (ArgumentException ex)
            {
                throw new JsonException(ex.Message);
            }
        }
Beispiel #3
0
        public async Task UpdateAsync(
            CancellationToken ct)
        {
            if (eventStore is MongoEventStore mongoEventStore)
            {
                var collection = mongoEventStore.RawCollection;

                var filter = Builders <BsonDocument> .Filter;

                var updates = Builders <BsonDocument> .Update;

                var writes       = new List <WriteModel <BsonDocument> >();
                var writeOptions = new BulkWriteOptions
                {
                    IsOrdered = false
                };

                async Task WriteAsync(WriteModel <BsonDocument>?model, bool force)
                {
                    if (model != null)
                    {
                        writes.Add(model);
                    }

                    if (writes.Count == 1000 || (force && writes.Count > 0))
                    {
                        await collection.BulkWriteAsync(writes, writeOptions, ct);

                        writes.Clear();
                    }
                }

                await collection.Find(new BsonDocument()).ForEachAsync(async commit =>
                {
                    UpdateDefinition <BsonDocument>?update = null;

                    var index = 0;

                    foreach (BsonDocument @event in commit["Events"].AsBsonArray)
                    {
                        var data = BsonDocument.Parse(@event["Payload"].AsString);

                        if (data.TryGetValue("appId", out var appIdValue))
                        {
                            var appId = NamedId <Guid> .Parse(appIdValue.AsString, Guid.TryParse).Id.ToString();

                            var eventUpdate = updates.Set($"Events.{index}.Metadata.AppId", appId);

                            if (update != null)
                            {
                                update = updates.Combine(update, eventUpdate);
                            }
                            else
                            {
                                update = eventUpdate;
                            }
                        }

                        index++;
                    }

                    if (update != null)
                    {
                        var write = new UpdateOneModel <BsonDocument>(filter.Eq("_id", commit["_id"].AsString), update);

                        await WriteAsync(write, false);
                    }
                }, ct);

                await WriteAsync(null, true);
            }
        }