Ejemplo n.º 1
0
        public async Task Save(
            Foo streamDocument,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            var state            = streamDocument.GetState();
            var json             = JsonConvert.SerializeObject(state, _serializerSettings);
            var newStreamMessage = new NewStreamMessage(Guid.NewGuid(), "FooDocState", json);

            var originalVersion = streamDocument.OriginalVersion;

            if (originalVersion < 0)
            {
                await _streamStore.AppendToStream(
                    streamDocument.Id,
                    ExpectedVersion.NoStream,
                    newStreamMessage,
                    cancellationToken);

                await _streamStore.SetStreamMetadata(
                    streamDocument.Id,
                    maxCount : _maxCount,
                    cancellationToken : cancellationToken);
            }
            else
            {
                await _streamStore.AppendToStream(
                    streamDocument.Id,
                    originalVersion,
                    newStreamMessage,
                    cancellationToken);
            }
        }
Ejemplo n.º 2
0
Archivo: Remote.cs Proyecto: zedr0n/ZES
        private async Task <FastForwardResult> FastForward(IStreamStore from, IStreamStore to, string branchId)
        {
            var fastForwardResult = new FastForwardResult();
            var page = await from.ListStreams();

            while (page.StreamIds.Length > 0)
            {
                foreach (var s in page.StreamIds.Where(x => x.StartsWith(branchId)))
                {
                    var localPosition = await from.LastPosition(s);

                    var remotePosition = await to.LastPosition(s);

                    if (localPosition == remotePosition)
                    {
                        continue;
                    }

                    var eventPage = await from.ReadStreamForwards(s, Math.Max(remotePosition + 1, 0), Configuration.BatchSize);

                    var appendMessages = new List <NewStreamMessage>();

                    if (eventPage.Messages.Length == 0 && localPosition >= ExpectedVersion.EmptyStream)
                    {
                        await to.AppendToStream(s, remotePosition, appendMessages.ToArray());
                    }

                    var metadata = await from.GetStreamMetadata(s);

                    await to.SetStreamMetadata(s, ExpectedVersion.Any, metadata.MaxAge, metadata.MaxCount, metadata.MetadataJson);

                    while (eventPage.Messages.Length > 0)
                    {
                        appendMessages.Clear();
                        foreach (var m in eventPage.Messages)
                        {
                            var payload = await m.GetJsonData();

                            var message = new NewStreamMessage(m.MessageId, m.Type, payload, m.JsonMetadata);
                            appendMessages.Add(message);
                        }

                        var result = await to.AppendToStream(s, remotePosition, appendMessages.ToArray());

                        fastForwardResult.NumberOfMessages += result.CurrentVersion - Math.Max(remotePosition, ExpectedVersion.EmptyStream);
                        eventPage = await eventPage.ReadNext();
                    }

                    fastForwardResult.NumberOfStreams++;
                }

                page = await page.Next();
            }

            fastForwardResult.ResultStatus = Status.Success;

            return(fastForwardResult);
        }
        public async Task <Unit> Invoke(IStreamStore streamStore, CancellationToken ct)
        {
            await streamStore.SetStreamMetadata(
                StreamId,
                ExpectedVersion,
                MaxAge,
                MaxCount,
                MetadataJson,
                ct);

            return(Unit.Instance);
        }
        public async Task <WebHooks> Load(CancellationToken cancellationToken)
        {
            var page = await _streamStore.ReadStreamBackwards(_name, StreamVersion.End, 1, cancellationToken);

            if (page.Status == PageReadStatus.StreamNotFound)
            {
                await _streamStore.SetStreamMetadata(_name, maxCount : 1, cancellationToken : cancellationToken);

                return(new WebHooks(_getUtcNow, _maxWebHookCount));
            }

            var json    = await page.Messages[0].GetJsonData(cancellationToken);
            var memento = JsonConvert.DeserializeObject <WebHooksMemento>(json);

            return(new WebHooks(memento, _getUtcNow, _maxWebHookCount));
        }
Ejemplo n.º 5
0
        private static void Write(IStreamStore streamStore, int messageCount, int streamCount)
        {
            var streams = Enumerable.Range(0, streamCount).Select(_ => $"test-{Guid.NewGuid():n}").ToList();

            IList <Func <Task> > tasks = new List <Func <Task> >();;

            for (int i = 0; i < streams.Count; i++)
            {
                var streamId = streams[i];
                var messages = GenerateMessages(messageCount);
                foreach (var message in messages)
                {
                    tasks.Add(() => streamStore.AppendToStream(streamId, ExpectedVersion.Any, new[] { message }));
                }

                var oneMonthInSeconds = 2629743;
                tasks.Add(() => streamStore.SetStreamMetadata(streamId, ExpectedVersion.Any, oneMonthInSeconds, 100, $@"{{ ""foo"": ""baz"" }}"));
            }
            tasks.Shuffle();
            Task.Run(() => Task.WhenAll(tasks.Select(t => t())));
        }