public async Task <ISnapshot> GetSnapshot <T>(string bucket, string streamId) where T : class, IEventSource
        {
            var streamName = _streamGen(typeof(T), StreamTypes.Snapshot, bucket, streamId);

            Logger.Write(LogLevel.Debug, () => $"Getting snapshot for stream [{streamName}]");
            {
                var snapshot = await _snapshots.Retreive(streamName).ConfigureAwait(false);

                if (snapshot != null)
                {
                    HitMeter.Mark();
                    Logger.Write(LogLevel.Debug,
                                 () => $"Found snapshot [{streamName}] version {snapshot.Version} from subscription");
                    return(snapshot);
                }
            }

            // Check store directly (this might be a new instance which hasn't caught up to snapshot stream yet

            Logger.Write(LogLevel.Debug, () => $"Checking for snapshot for stream [{streamName}] in store");

            var read = await _store.GetEventsBackwards(streamName, StreamPosition.End, 1).ConfigureAwait(false);

            if (read != null && read.Any())
            {
                var @event   = read.Single();
                var snapshot = new Snapshot
                {
                    EntityType = @event.Descriptor.EntityType,
                    Bucket     = bucket,
                    StreamId   = streamId,
                    Timestamp  = @event.Descriptor.Timestamp,
                    Version    = @event.Descriptor.Version,
                    Payload    = @event.Event
                };
                HitMeter.Mark();
                Logger.Write(LogLevel.Debug, () => $"Found snapshot [{streamName}] version {snapshot.Version} from store");
                return(snapshot);
            }

            MissMeter.Mark();
            Logger.Write(LogLevel.Debug, () => $"Snapshot not found for stream [{streamName}]");
            return(null);
        }
Exemple #2
0
        public async Task <ISnapshot> GetSnapshot <T>(string bucket, Id streamId, Id[] parents) where T : IEntity
        {
            var streamName = _streamGen(_registrar.GetVersionedName(typeof(T)), StreamTypes.Snapshot, bucket, streamId, parents);

            Logger.DebugEvent("Get", "[{Stream:l}]", streamName);
            if (_snapshots != null)
            {
                var snapshot = await _snapshots.Retreive(streamName).ConfigureAwait(false);

                if (snapshot != null)
                {
                    _metrics.Mark("Snapshot Cache Hits", Unit.Items);
                    Logger.DebugEvent("Cached", "[{Stream:l}] version {Version}", streamName, snapshot.Version);
                    return(snapshot);
                }
            }
            _metrics.Mark("Snapshot Cache Misses", Unit.Items);

            // Check store directly (this might be a new instance which hasn't caught up to snapshot stream yet


            var read = await _store.GetEventsBackwards(streamName, StreamPosition.End, 1).ConfigureAwait(false);

            if (read != null && read.Any())
            {
                var @event   = read.Single();
                var snapshot = new Snapshot
                {
                    EntityType = @event.Descriptor.EntityType,
                    Bucket     = bucket,
                    StreamId   = streamId,
                    Timestamp  = @event.Descriptor.Timestamp,
                    Version    = @event.Descriptor.Version,
                    Payload    = @event.Event as IState
                };
                Logger.DebugEvent("Read", "[{Stream:l}] version {Version}", streamName, snapshot.Version);
                return(snapshot);
            }

            Logger.DebugEvent("NotFound", "[{Stream:l}]", streamName);
            return(null);
        }