Example #1
0
 public void SetUp()
 {
     streamId = Guid.NewGuid().ToString();
     store    = TestEventStore.Create();
     store.Populate(streamId);
     stream = NEventStoreStream.ByAggregate(store, streamId);
 }
        public async Task One_stream_can_transparently_delegate_to_another()
        {
            var upstream = NEventStoreStream.AllEvents(store);

            store.WriteEvents(i => new AccountOpened(), 100);
            var projection = new Projection <int, string>();

            var dependentStream = Stream.Create <int, string>(
                async q =>
            {
                var mapped = upstream.Map(e => new[] { e.Count() });
                var batch  = await mapped.Fetch(q);
                return(batch);
            });

            var catchup = StreamCatchup.Create(dependentStream, batchSize: 50);

            FetchAndSaveProjection <Projection <int, string> > manageProjection = async(id, aggregate) =>
            {
                await aggregate(projection);
            };

            catchup.Subscribe(async(p, b) =>
            {
                p.Value += b.Sum();
                return(p);
            }, manageProjection);

            await catchup.RunSingleBatch();

            Console.WriteLine(projection.ToLogString());

            projection.Value.Should().Be(50);
            projection.CursorPosition.Should().Be("50");
        }
        public void SetUp()
        {
            // populate the event store
            store = TestEventStore.Create();

            streamId = Guid.NewGuid().ToString();

            store.WriteEvents(streamId);

            stream = NEventStoreStream.ByAggregate(store, streamId).DomainEvents();
        }
Example #4
0
        public async Task A_partitioned_stream_can_be_mapped_at_query_time()
        {
            for (var i = 1; i <= 9; i++)
            {
                store.WriteEvents(
                    streamId: Guid.NewGuid().ToString(),
                    howMany: 10,
                    bucketId: i.ToString());
            }

            var partitionedStream = Stream.Partitioned <EventMessage, int, string>(async(q, p) =>
            {
                var bucketId          = ((IStreamQueryValuePartition <string>)p).Value;
                var streamsToSnapshot = store.Advanced.GetStreamsToSnapshot(bucketId, 0);
                var streamId          = streamsToSnapshot.Select(s => s.StreamId).Single();
                var stream            = NEventStoreStream.ByAggregate(store, streamId, bucketId);
                var batch             = await stream.CreateQuery(q.Cursor, q.BatchSize).NextBatch();
                return(batch);
            }).Trace();

            var domainEvents = partitionedStream.Map(es => es.Select(e => e.Body).OfType <IDomainEvent>());

            // catch up
            var catchup = domainEvents.DistributeAmong(Enumerable.Range(1, 10)
                                                       .Select(i => Partition.ByValue(i.ToString())),
                                                       batchSize: 2);

            var receivedEvents = new ConcurrentBag <IDomainEvent>();

            catchup.Subscribe(async b =>
            {
                foreach (var e in b)
                {
                    receivedEvents.Add(e);
                }
            });

            await catchup.RunUntilCaughtUp().Timeout();

            receivedEvents.Count().Should().Be(90);
        }
        public async Task One_stream_can_use_anothers_cursor_to_limit_how_far_ahead_it_looks()
        {
            var aggregateId = Guid.NewGuid().ToString();

            Console.WriteLine(new { aggregateId });
            var upstream = NEventStoreStream.AggregateIds(store);

            store.WriteEvents(i => new AccountOpened
            {
                AggregateId = aggregateId
            }, 50);

            var streamPerAggregate = upstream.IntoMany(
                async(streamId, fromCursor, toCursor) =>
            {
                var stream = NEventStoreStream.AllEvents(store);

                var cursor = Cursor.New(fromCursor);
                var events = await stream.CreateQuery(cursor, int.Parse(toCursor)).NextBatch();

                var s = events.Select(e => e.Body)
                        .Cast <IDomainEvent>()
                        .GroupBy(e => e.AggregateId)
                        .Select(group => @group.AsStream(cursorPosition: i => i.CheckpointToken));

                return(s);
            });

            var streams = await streamPerAggregate.CreateQuery(batchSize : 25).NextBatch();

            var count = 0;

            foreach (var stream in streams.SelectMany(s => s))
            {
                count++;
                var batch = await stream.CreateQuery().NextBatch();

                batch.Count.Should().Be(25);
            }
        }
        public IStream <IStream <IDomainEvent, int>, string> StreamPerAggregate()
        {
            return(StreamUpdates()
                   .IntoMany(
                       async(streamUpdate, fromCursor, toCursor) =>
            {
                var allEvents = NEventStoreStream.AllEvents(store);

                var cursor = Cursor.New(fromCursor);
                var batch = await allEvents.CreateQuery(cursor, int.Parse(toCursor))
                            .NextBatch();

                var aggregate = batch.Select(e => e.Body)
                                .Cast <IDomainEvent>()
                                .Where(e => e.AggregateId == streamUpdate.StreamId);

                IStream <IDomainEvent, int> stream = aggregate.AsStream(
                    id: streamUpdate.StreamId,
                    cursorPosition: e => e.StreamRevision);

                return stream;
            }));
        }
 public IStream <IDomainEvent, int> Open(string streamId)
 {
     return(NEventStoreStream.ByAggregate(store, streamId).DomainEvents());
 }