public void verify_snapshot_check_version()
        {
            var id       = new TestEventUnfolderId(1);
            var unfolder = new TestProjector();
            var memento  = (EventUnfolderMemento)unfolder.GetSnapshot();

            memento.Signature = "OTHER"; //change the version of the memento.
            Snapshot s = new Snapshot(id, 2, memento);

            _persister.Load("", 1, "").ReturnsForAnyArgs(s);
            sut.GetById <TestProjector, TestAggregateQueryModel>(id, 200);
            _eventStore.Received().OpenStream("BLAH", id, 0, 200);
        }
Esempio n. 2
0
        public void verify_persistence_honor_database_version_when_threshold_passed()
        {
            var sampleAggregateId = new SampleAggregateId(_counter++);
            var aggregate         = TestAggregateFactory.Create <SampleAggregate, SampleAggregate.State>(new SampleAggregate.State(), sampleAggregateId);
            var memento           = new AggregateSnapshot <SampleAggregate.State>()
            {
                Id = sampleAggregateId, Version = 30, State = new SampleAggregate.State()
            };
            var snapshot = new Snapshot("Jarvis", sampleAggregateId, 30, memento);

            _persister.Load(null, 0, null).ReturnsForAnyArgs(snapshot);

            //Simulate the flow of a repository, first operation load the snapshot
            _sut.Load(sampleAggregateId, Int32.MaxValue, typeof(SampleAggregate));

            //then the repository restored the snapshot
            ((ISnapshotable)aggregate).Restore(memento);

            //now iterate
            _persister.DidNotReceiveWithAnyArgs().Persist(null, null);

            for (int i = 0; i < NumberOfCommitsBeforeSnapshot; i++)
            {
                aggregate.Touch();
            }
            _sut.Snapshot(aggregate, "Jarvis", NumberOfCommitsBeforeSnapshot);
            //should be persisted, becauase it is loaded at version 30, but then another snapshotthreshold count of event were raised
            _persister.ReceivedWithAnyArgs().Persist(null, aggregate.GetType().FullName);
        }
        public void verify_snapshot_usage()
        {
            var id = new TestEventUnfolderId(Interlocked.Increment(ref progressive));

            using (var stream = _eventStore.OpenStream("BLAH", id.AsString(), 0, Int32.MaxValue))
            {
                CommitOneValue(stream, 1);
                CommitOneValue(stream, 2);
            }

            var      unfolder = sut.GetById <TestProjector, TestAggregateQueryModel>(id, 1);
            var      memento  = unfolder.GetSnapshot();
            Snapshot s        = new Snapshot(id.AsString(), 2, memento);

            using (var stream = _eventStore.OpenStream("BLAH", id.AsString(), 0, Int32.MaxValue))
            {
                CommitOneValue(stream, 3);
            }
            _persister.Load(null, 1, null).ReturnsForAnyArgs(s);

            unfolder = sut.GetById <TestProjector, TestAggregateQueryModel>(id, Int32.MaxValue);
            var rm = unfolder.GetProjection();

            Assert.That(rm.Sum, Is.EqualTo(6));
            Assert.That(rm.ListOfInt, Is.EquivalentTo(new[] { 1, 2, 3 }));
        }
        public TProjector GetById <TProjector, TViewModel>(IIdentity id, Int32 to)
            where TProjector : Projector <TViewModel>
            where TViewModel : BaseAggregateQueryModel, new()
        {
            var snapshotTypeName = typeof(TProjector).Name;
            var snapshot         = _snapshotPersister.Load(id.AsString(), to, snapshotTypeName);
            var projector        = _factory.Create <TViewModel>(typeof(TProjector), id);

            Int32      startsFromEvent = 0;
            IMementoEx memento         = snapshot == null ? null : (IMementoEx)snapshot.Payload;

            if (memento != null)
            {
                if (!projector.ValidateMemento(memento))
                {
                    _snapshotPersister.Clear(id.AsString(), memento.Version, snapshotTypeName);
                }
                else
                {
                    projector.Restore(memento);
                    startsFromEvent = snapshot.StreamRevision;
                }
            }

            using (var events = _eventStore.OpenStream(projector.BucketId, id.AsString(), startsFromEvent, to))
            {
                Int32 eventCount = 0;
                foreach (var evt in events.CommittedEvents)
                {
                    projector.ApplyEvent((DomainEvent)evt.Body);
                    eventCount++;
                }

                if (projector.ShouldSnapshot(eventCount))
                {
                    memento  = projector.GetSnapshot();
                    snapshot = new Snapshot(id.AsString(), events.StreamRevision, memento);
                    _snapshotPersister.Persist(snapshot, snapshotTypeName);
                }
            }

            return((TProjector)projector);
        }
        public ISnapshot Load(string streamId, int upToVersion, Type aggregateType)
        {
            //Verify in cache without bothering persistence layer.
            var snapshot = _cache.Get(streamId) as ISnapshot;

            //if is in cache and version is not greater than version requested you can return
            if (_cacheEnabled && snapshot != null && snapshot.StreamRevision <= upToVersion)
            {
                CacheHitCounter.Increment();
                return(snapshot);
            }
            CacheMissCounter.Increment();

            //Try load from persistence medium
            var persistedSnapshot = _persister.Load(streamId, upToVersion, aggregateType.FullName);

            if (_cacheEnabled && persistedSnapshot != null && upToVersion == Int32.MaxValue)
            {
                _cache.Set(streamId, persistedSnapshot, _standardCachePolicy);
            }
            return(persistedSnapshot);
        }