public async Task <IEventStream> GetStream <T>(string bucket, Id streamId, IEnumerable <Id> parents = null) where T : class, IEventSource { parents = parents ?? new Id[] { }; var streamName = _streamGen(typeof(T), StreamTypes.Domain, bucket, streamId, parents); Logger.Write(LogLevel.Debug, () => $"Retreiving stream [{streamId}] in bucket [{bucket}] for type {typeof(T).FullName}"); var cached = _cache.Retreive(streamName) as IImmutableEventStream; if (cached != null) { HitMeter.Mark(); Logger.Write(LogLevel.Debug, () => $"Found stream [{streamName}] in cache"); return(new EventStream <T>(cached)); } MissMeter.Mark(); while (await CheckFrozen <T>(bucket, streamId, parents).ConfigureAwait(false)) { Logger.Write(LogLevel.Info, () => $"Stream [{streamId}] in bucket [{bucket}] is frozen - waiting"); await Task.Delay(100).ConfigureAwait(false); } Logger.Write(LogLevel.Debug, () => $"Stream [{streamId}] in bucket [{bucket}] not in cache - reading from store"); ISnapshot snapshot = null; if (typeof(ISnapshotting).IsAssignableFrom(typeof(T))) { snapshot = await _snapstore.GetSnapshot <T>(bucket, streamId, parents).ConfigureAwait(false); Logger.Write(LogLevel.Debug, () => { if (snapshot != null) { return($"Retreived snapshot for entity id [{streamId}] bucket [{bucket}] version {snapshot.Version}"); } return($"No snapshot found for entity id [{streamId}] bucket [{bucket}]"); }); } var events = await _store.GetEvents(streamName, start : snapshot?.Version + 1).ConfigureAwait(false); var oobMetadata = await _store.GetMetadata(streamName, OobMetadataKey).ConfigureAwait(false); IEnumerable <OobDefinition> oobs = null; if (!string.IsNullOrEmpty(oobMetadata)) { oobs = JsonConvert.DeserializeObject <IEnumerable <OobDefinition> >(oobMetadata); } var eventstream = new EventStream <T>(bucket, streamId, parents, oobs, events, snapshot); _cache.Cache(streamName, eventstream.Clone()); Logger.Write(LogLevel.Debug, () => $"Stream [{streamId}] in bucket [{bucket}] read - version is {eventstream.CommitVersion}"); return(eventstream); }
public async Task <IEventStream> GetStream <T>(String bucket, String stream, Int32?start = null) where T : class, IEntity { Logger.DebugFormat("Getting stream '{0}' in bucket '{1}'", stream, bucket); var streamId = String.Format("{0}.{1}", bucket, stream); var events = new List <ResolvedEvent>(); var readSize = _nsbSettings.Get <Int32>("ReadSize"); if (_shouldCache) { var cached = _cache.Retreive(streamId); if (cached != null) { return(cached); } } var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All, Binder = new EventSerializationBinder(_mapper), ContractResolver = new EventContractResolver(_mapper) }; StreamEventsSlice current; var sliceStart = start ?? StreamPosition.Start; do { current = await _client.ReadStreamEventsForwardAsync(streamId, sliceStart, readSize, false); events.AddRange(current.Events); sliceStart = current.NextEventNumber; } while (!current.IsEndOfStream); var translatedEvents = events.Select(e => { var descriptor = e.Event.Metadata.Deserialize(settings); var data = e.Event.Data.Deserialize(e.Event.EventType, settings); return(new Internal.WritableEvent { Descriptor = descriptor, Event = data, EventId = e.Event.EventId }); }); var eventstream = new Internal.EventStream <T>(_builder, this, _snapshots, bucket, stream, current.LastEventNumber, translatedEvents); if (_shouldCache) { _cache.Cache(streamId, eventstream.Clone()); } return(eventstream); }