示例#1
0
        public async Task ShouldGetEntityWithSnapshot()
        {
            var snapshot = Fake <ISnapshot>();

            A.CallTo(() => snapshot.Payload).Returns(new FakeState()
            {
                ThrowAbandon = true
            });
            A.CallTo(() => Snapstore.GetSnapshot <FakeEntity>(A <string> .Ignored, A <Id> .Ignored, A <Id[]> .Ignored)).Returns(Task.FromResult(snapshot));

            var entity = await Sut.Get <FakeEntity, FakeState>("test", "test", null).ConfigureAwait(false);

            entity.State.ThrowAbandon.Should().BeTrue();
        }
示例#2
0
        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);
        }
示例#3
0
        public async Task <TEntity> Get <TEntity, TState>(string bucket, Id id, IEntity parent) where TEntity : IEntity <TState> where TState : class, IState, new()
        {
            var uow = (Configuration.Settings.LocalContainer.Value ?? Configuration.Settings.Container).Resolve <Aggregates.UnitOfWork.IDomain>();

            var factory = EntityFactory.For <TEntity>();

            var parents = getParents(parent);
            // Todo: pass parent instead of Id[]?
            var snapshot = await _snapstore.GetSnapshot <TEntity>(bucket, id, parents?.Select(x => x.StreamId).ToArray()).ConfigureAwait(false);

            var events = await _eventstore.GetEvents <TEntity>(bucket, id, parents?.Select(x => x.StreamId).ToArray(), start : snapshot?.Version).ConfigureAwait(false);

            var entity = factory.Create(bucket, id, parents, events.Select(x => x.Event as IEvent).ToArray(), snapshot?.Payload);


            (entity as INeedDomainUow).Uow             = uow;
            (entity as INeedEventFactory).EventFactory = _factory;
            (entity as INeedStore).Store                = _eventstore;
            (entity as INeedStore).OobWriter            = _oobstore;
            (entity as INeedVersionRegistrar).Registrar = _registrar;
            (entity as INeedChildTracking).Tracker      = _childTracker;

            Logger.DebugEvent("Get", "[{EntityId:l}] bucket [{Bucket:l}] entity [{EntityType:l}] version {Version}", id, bucket, typeof(TEntity).FullName, entity.Version);

            return(entity);
        }
示例#4
0
        /// <summary>
        /// Gets the most recent snapshot for the specified <paramref name="streamId"/> and <paramref name="maximumVersion"/>.
        /// </summary>
        /// <param name="type">The snapshot type.</param>
        /// <param name="streamId">The unique stream identifier.</param>
        /// <param name="maximumVersion">The maximum snapshot version.</param>
        public Snapshot GetSnapshot(Type type, Guid streamId, Int32 maximumVersion)
        {
            var result = snapshotStore.GetSnapshot(type, streamId, maximumVersion);

            statistics.IncrementQueryCount();

            return(result);
        }
示例#5
0
        private async Task <T> GetUntracked(string bucket, string streamId)
        {
            ISnapshot snapshot = null;

            if (typeof(ISnapshotting).IsAssignableFrom(typeof(T)))
            {
                snapshot = await _snapstore.GetSnapshot <T>(bucket, streamId).ConfigureAwait(false);
            }

            var stream = await _store.GetStream <T>(bucket, streamId, snapshot).ConfigureAwait(false);

            // Get requires the stream exists
            if (stream == null || stream.StreamVersion == -1)
            {
                throw new NotFoundException($"Aggregate stream [{streamId}] in bucket [{bucket}] type {typeof(T).FullName} not found");
            }

            return(await GetUntracked(stream).ConfigureAwait(false));
        }
示例#6
0
        private async Task <T> GetUntracked(string bucket, string streamId)
        {
            Logger.Write(LogLevel.Debug, () => $"Retreiving aggregate id [{streamId}] in bucket [{bucket}] for type {typeof(T).FullName} in store");
            ISnapshot snapshot = null;

            if (typeof(ISnapshotting).IsAssignableFrom(typeof(T)))
            {
                snapshot = await _snapstore.GetSnapshot <T>(bucket, streamId).ConfigureAwait(false);

                Logger.Write(LogLevel.Debug, () =>
                {
                    if (snapshot != null)
                    {
                        return($"Retreived snapshot for aggregate id [{streamId}] version {snapshot.Version}");
                    }
                    return($"No snapshot found for aggregate id [{streamId}]");
                });
            }

            var stream = await _store.GetStream <T>(bucket, streamId, snapshot).ConfigureAwait(false);

            return(await GetUntracked(stream).ConfigureAwait(false));
        }
示例#7
0
 protected Task <ISnapshot> GetSnapshot(String bucket, String streamId)
 {
     return(_snapstore.GetSnapshot(bucket, streamId));
 }
示例#8
0
 public StoreEntities()
 {
     Snapstore = Fake <IStoreSnapshots>();
     A.CallTo(() => Snapstore.GetSnapshot <FakeEntity>(A <string> .Ignored, A <Id> .Ignored, A <Id[]> .Ignored)).Returns(Task.FromResult((ISnapshot)null));
     Inject(Snapstore);
 }