private static async Task<IEnumerable<EventStreamChange>> EventStreamChanges(Func<DisposableQueryable<StorableEvent>> getStorableEventsQueryable, IStreamQuery<long> streamQuery) { using (var events = getStorableEventsQueryable()) { var query = events.Queryable .Where(e => e.Id > streamQuery.Cursor.Position); var fetchedFromEventStore = query .OrderBy(e => e.Id) .Select(e => new { e.AggregateId, e.StreamName, e.Id }) .Take(streamQuery.BatchSize ?? 10) .GroupBy(e => e.AggregateId) .ToArray(); var eventStreamChanges = fetchedFromEventStore .Select(e => new EventStreamChange { AggregateId = e.Key, AggregateType = e.FirstOrDefault().StreamName, AbsoluteSequenceNumber = e.OrderByDescending(ee => ee.Id).FirstOrDefault().Id }) .OrderBy(e => e.AbsoluteSequenceNumber) .ToArray(); return eventStreamChanges; } }
private static async Task<IEnumerable<IEvent>> QueryAsync( Func<DisposableQueryable<StorableEvent>> getStorableEvents, IStreamQuery<long> q, EventStreamChange update, long fromCursor, long toCursor) { { using (var disposableQueryable = getStorableEvents()) { var query = disposableQueryable.Queryable .Where(e => e.AggregateId == update.AggregateId) .Where(e => e.Id > fromCursor && e.Id <= toCursor); query = query.OrderBy(e => e.Id) .Take(q.BatchSize ?? 1000); var events = query.ToArray(); foreach (var e in events) Trace.WriteLine(e.Id); return events.Select(e => e.ToDomainEvent()); } } }
private static async Task <IEnumerable <IEvent> > QueryAsync( Func <DisposableQueryable <StorableEvent> > getStorableEvents, IStreamQuery <long> q, EventStreamChange update, long fromCursor, long toCursor) { { using (var disposableQueryable = getStorableEvents()) { var query = disposableQueryable.Queryable .Where(e => e.AggregateId == update.AggregateId) .Where(e => e.Id > fromCursor && e.Id <= toCursor); query = query.OrderBy(e => e.Id) .Take(q.BatchSize ?? 1000); var events = query.ToArray(); foreach (var e in events) { Trace.WriteLine(e.Id); } return(events.Select(e => e.ToDomainEvent())); } } }
public override async Task <IStreamBatch <TData> > Fetch(IStreamQuery <TCursor> query) { var batch = await fetch(query, partition); AdvanceCursor(query, batch); return(batch); }
public IAsyncEnumerable <TQueryResult> StreamAsync <TQueryResult>(IStreamQuery <TQueryResult> query, CancellationToken cancellationToken = default) { var mediationStrategy = new SingleStreamHandlerMediationStrategy <IStreamQuery <TQueryResult>, TQueryResult>(cancellationToken); var findStrategy = new ActualTypeOrFirstAssignableTypeMessageResolveStrategy(); return(_messageMediator.Mediate(query, findStrategy, mediationStrategy)); }
public async Task <IStreamBatch <EventMessage> > Fetch(IStreamQuery <string> query) { var commits = store.Advanced.GetFrom(query.Cursor.Position); var batchSize = query.BatchSize ?? 100; var actualCount = 0; var events = new List <EventMessage>(); var cursorPosition = query.Cursor.Position; foreach (var commit in commits) { actualCount += commit.Events.Count; if (actualCount > batchSize) { break; } events.AddRange(commit.Events); foreach (var @event in commit.Events.Select(e => e.Body).OfType <IDomainEvent>()) { @event.StreamRevision = commit.StreamRevision; @event.CheckpointToken = commit.CheckpointToken; } cursorPosition = commit.CheckpointToken; } var batch = StreamBatch.Create(events, query.Cursor); if (batch.Count > 0) { query.Cursor.AdvanceTo(cursorPosition); } return(batch); }
public async Task Stream_Trace_default_behavior_can_be_overridden() { IStreamQuery <int> receivedSendQuery = null; IStreamQuery <int> receivedResultsQuery = null; IStreamBatch <int> receivedBatch = null; var stream = Enumerable.Range(1, 1000) .AsStream() .Trace(onSendQuery: q => { receivedSendQuery = q; }, onResults: (q, b) => { receivedResultsQuery = q; receivedBatch = b; }); var sentQuery = stream.CreateQuery(Cursor.New(15), batchSize: 3); await sentQuery.NextBatch(); receivedSendQuery.Should().BeSameAs(sentQuery); receivedResultsQuery.Should().BeSameAs(sentQuery); receivedBatch.Count().Should().Be(3); receivedBatch.Should().ContainInOrder(16, 17, 18); }
/// <summary> /// Fetches a batch of data from the stream. /// </summary> /// <param name="query">The query to apply to the stream.</param> /// <returns></returns> public async Task <IStreamBatch <EventMessage> > Fetch(IStreamQuery <int> query) { var minRevisionToFetch = Math.Max(query.Cursor.Position, 0); int maxRevisionToFetch; checked { maxRevisionToFetch = minRevisionToFetch + query.BatchSize ?? 100000; } var maxExistingRevision = store.Advanced .GetFrom(bucketId, streamId, minRevisionToFetch, int.MaxValue) .Select(c => c.StreamRevision) .LastOrDefault(); if (maxExistingRevision <= minRevisionToFetch) { return(query.Cursor.EmptyBatch <EventMessage, int>()); } var events = new List <EventMessage>(); checked { for (var i = minRevisionToFetch + 1; i <= maxRevisionToFetch; i++) { try { using (var stream = store.OpenStream(streamId: streamId, minRevision: i, maxRevision: i, bucketId: bucketId)) { if (stream.CommittedEvents.Count == 0) { break; } events.AddRange(stream.CommittedEvents .Select(e => { e.SetStreamRevision(stream.StreamRevision); return(e); })); } } catch (StreamNotFoundException) { break; } } } var batch = StreamBatch.Create(events, query.Cursor); if (batch.Count > 0) { query.Cursor.AdvanceTo(batch.Max(i => i.StreamRevision())); } return(batch); }
public Task <Stream> ExecuteAsync(IStreamQuery query, CancellationToken token = default(CancellationToken)) { return(this.ExecuteAsync((IQuery <Stream>)query, token)); }
public virtual Stream Execute(IStreamQuery stream) { return(RequestProviderFactory.GetSyncRequestProvider().Execute(stream)); }
public virtual Task <Stream> ExecuteAsync(IStreamQuery stream, CancellationToken token = default(CancellationToken)) { return(RequestProviderFactory.GetAsyncRequestProvider().ExecuteAsync(stream, token)); }
private static Maybe <Unit> AdvanceCursor(IStreamBatch <IEvent> batch, IStreamQuery <long> query, long toCursor) { return(batch.LastOrDefault() .IfNotNull() .ThenDo(e => query.Cursor.AdvanceTo(toCursor))); }
public Stream Execute(IStreamQuery query) { return(Execute((IQuery <Stream>)query)); }
private static Maybe<Unit> AdvanceCursor(IStreamBatch<IEvent> batch, IStreamQuery<long> query, long toCursor) { return batch.LastOrDefault() .IfNotNull() .ThenDo(e => query.Cursor.AdvanceTo(toCursor)); }
protected void AdvanceCursor(IStreamQuery <TCursor> query, IStreamBatch <TData> batch) { advanceCursor(query, batch); }
public abstract Task <IStreamBatch <TData> > Fetch(IStreamQuery <TCursor> query);
private static async Task <IEnumerable <EventStreamChange> > EventStreamChanges(Func <DisposableQueryable <StorableEvent> > getStorableEventsQueryable, IStreamQuery <long> streamQuery) { using (var events = getStorableEventsQueryable()) { var query = events.Queryable .Where(e => e.Id > streamQuery.Cursor.Position); var fetchedFromEventStore = query .OrderBy(e => e.Id) .Select(e => new { e.AggregateId, e.StreamName, e.Id }) .Take(streamQuery.BatchSize ?? 10) .GroupBy(e => e.AggregateId) .ToArray(); var eventStreamChanges = fetchedFromEventStore .Select(e => new EventStreamChange { AggregateId = e.Key, AggregateType = e.FirstOrDefault().StreamName, AbsoluteSequenceNumber = e.OrderByDescending(ee => ee.Id).FirstOrDefault().Id }) .OrderBy(e => e.AbsoluteSequenceNumber) .ToArray(); return(eventStreamChanges); } }