コード例 #1
0
        public async Task <IEventStream> GetStream <T>(string bucket, string streamId, ISnapshot snapshot = null) where T : class, IEventSource
        {
            var streamName = _streamGen(typeof(T), bucket, streamId);

            Logger.Write(LogLevel.Debug, () => $"Retreiving stream [{streamId}] in bucket [{bucket}]");

            if (_shouldCache)
            {
                var cached = _cache.Retreive(streamName) as EventStream <T>;
                if (cached != null && cached.CommitVersion >= (snapshot?.Version + 1))
                {
                    HitMeter.Mark();
                    Logger.Write(LogLevel.Debug, () => $"Found stream [{streamName}] in cache");
                    return(new EventStream <T>(cached, Builder, this, snapshot));
                }
                MissMeter.Mark();
            }

            while (await CheckFrozen <T>(bucket, streamId).ConfigureAwait(false))
            {
                Logger.Write(LogLevel.Debug, () => $"Stream [{streamName}] is frozen - waiting");
                Thread.Sleep(100);
            }

            var events = await _store.GetEvents(streamName, start : snapshot?.Version + 1).ConfigureAwait(false);

            var eventstream = new EventStream <T>(Builder, this, bucket, streamId, events, snapshot);

            if (_shouldCache)
            {
                await Cache <T>(eventstream).ConfigureAwait(false);
            }

            return(eventstream);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
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);
        }