public async Task <T> Get <T>(string bucket, string stream) where T : class
        {
            var streamName = $"{_streamGen(typeof(T), bucket + ".POCO", stream)}";

            Logger.Write(LogLevel.Debug, () => $"Getting stream [{streamName}]");

            if (_shouldCache)
            {
                var cached = _cache.Retreive(streamName) as T;
                if (cached != null)
                {
                    HitMeter.Mark();
                    Logger.Write(LogLevel.Debug, () => $"Found poco [{stream}] bucket [{bucket}] in cache");
                    // An easy way to make a deep copy
                    return(JsonConvert.DeserializeObject <T>(JsonConvert.SerializeObject(cached)));
                }
                MissMeter.Mark();
            }

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

            if (read == null || !read.Any())
            {
                return(null);
            }

            var @event = read.Single();

            if (_shouldCache)
            {
                _cache.Cache(streamName, @event.Event);
            }
            return(@event.Event as T);
        }
        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);
        }
Exemple #3
0
        public async Task <ISnapshot> GetSnapshot <T>(string bucket, string streamId) where T : class, IEventSource
        {
            var streamName = $"{_streamGen(typeof(T), bucket + ".SNAP", streamId)}";

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

            if (_shouldCache)
            {
                var cached = _cache.Retreive(streamName) as ISnapshot;
                if (cached != null)
                {
                    HitMeter.Mark();
                    Logger.Write(LogLevel.Debug, () => $"Found snapshot [{streamName}] in cache");
                    return(cached);
                }
                MissMeter.Mark();
            }
            Logger.Write(LogLevel.Debug, () => $"Reading snapshot for stream [{streamName}] from store");


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

            if (read == null || !read.Any())
            {
                return(null);
            }


            var @event   = read.Single();
            var snapshot = new Snapshot
            {
                EntityType = @event.Descriptor.EntityType,
                Bucket     = bucket,
                Stream     = streamId,
                Timestamp  = @event.Descriptor.Timestamp,
                Version    = @event.Descriptor.Version,
                Payload    = @event.Event
            };

            if (_shouldCache)
            {
                _cache.Cache(streamName, snapshot);
            }
            return(snapshot);
        }
Exemple #4
0
        public Task Cache <T>(IEventStream stream) where T : class, IEventSource
        {
            if (!_shouldCache)
            {
                return(Task.CompletedTask);
            }

            var streamName = _streamGen(typeof(T), stream.Bucket, stream.StreamId);

            _cache.Cache(streamName, stream.Clone());
            return(Task.CompletedTask);
        }