Esempio n. 1
0
        private void SaveEventsInternal(IEnumerable<DomainEvent> events, ITapeStream stream, int version)
        {
            var array = events.ToArray();

            foreach (var @event in array)
            {
                @event.Version = ++version;

                byte[] bytes;
                using (var buffer = new MemoryStream())
                {
                    var descriptor = new EventDescriptor(@event);
                    serializer.Serialize(descriptor, buffer);
                    bytes = buffer.ToArray();
                }
                stream.TryAppend(bytes, TapeAppendCondition.None);
            }

            sender.SendBatch(array);
        }
Esempio n. 2
0
        public void Can_continue_after_recreation()
        {
            foreach (var b in _batch)
            {
                _stream.TryAppend(b);
            }

            var version = _stream.GetCurrentVersion();

            _stream = null;
            FreeResources();

            _stream = InitializeAndGetTapeStorage();

            _stream.TryAppend(_batch[0]);

            var readings = _stream.ReadRecords(version, 1).ToArray();

            Assert.AreEqual(1, readings.Length, "Number of records mismatch");
            Assert.Greater(readings[0].Version, version, "Version mismatch");
            CollectionAssert.AreEqual(_batch[0], readings[0].Data, "Data mismatch");
        }
Esempio n. 3
0
        public void Can_continue_after_recreation()
        {
            foreach (var b in _batch)
            {
                _stream.TryAppend(b);
            }

            var version = _stream.GetCurrentVersion();

            _stream = null;
            FreeResources();

            _stream = InitializeAndGetTapeStorage();

            _stream.TryAppend(_batch[0]);

            var readings = _stream.ReadRecords(version, 1).ToArray();

            Assert.AreEqual(1, readings.Length, "Number of records mismatch");
            Assert.Greater(readings[0].Version, version, "Version mismatch");
            CollectionAssert.AreEqual(_batch[0], readings[0].Data, "Data mismatch");
        }
        static void Observe(ITapeStream tapes, RedirectToDynamicEvent wire)
        {
            var date  = DateTime.MinValue;
            var watch = Stopwatch.StartNew();

            foreach (var record in tapes.ReadRecords(0, int.MaxValue))
            {
                var env = Streamer.ReadAsEnvelopeData(record.Data);
                if (date.Month != env.CreatedOnUtc.Month)
                {
                    date = env.CreatedOnUtc;
                    SystemObserver.Notify("Observing {0:yyyy-MM-dd} {1}", date, Math.Round(watch.Elapsed.TotalSeconds, 2));
                    watch.Restart();
                }
                foreach (var item in env.Items)
                {
                    var e = item.Content as IEvent;
                    if (e != null)
                    {
                        wire.InvokeEvent(e);
                    }
                }
            }
        }
 public SimpleDispatcher(AggregateFactory aggregateFactory, IEnvelopeStreamer serializer, ITapeStream tapeStream)
 {
     _aggregateFactory = aggregateFactory;
     _serializer = serializer;
     _tapeStream = tapeStream;
 }
Esempio n. 6
0
 public void TestSetUp()
 {
     PrepareEnvironment();
     _stream = InitializeAndGetTapeStorage();
 }
 public LocalEventStore(ITapeStream remote, FileTapeStream cache)
 {
     IsSyncAvailable = remote != null;
     _remote         = remote;
     _cache          = cache;
 }
Esempio n. 8
0
 public void TestSetUp()
 {
     PrepareEnvironment();
     _stream = InitializeAndGetTapeStorage();
 }
        public static void Rebuild(IDocumentStore targetContainer, ITapeStream stream)
        {
            var strategy = targetContainer.Strategy;
            var memory   = new MemoryStorageConfig();

            var memoryContainer = memory.CreateNuclear(strategy).Container;
            var tracked         = new ProjectionInspectingContainer(memoryContainer);

            var projections = new List <object>();

            projections.AddRange(DomainBoundedContext.Projections(tracked));
            projections.AddRange(ClientBoundedContext.Projections(tracked));
            //projections.AddRange(ApiOpsBoundedContext.Projections(tracked));

            if (tracked.Buckets.Count != projections.Count())
            {
                throw new InvalidOperationException("Count mismatch");
            }

            var storage = new NuclearStorage(targetContainer);
            var hashes  = storage.GetSingletonOrNew <ProjectionHash>().Entries;

            var memoryProjections = projections.Select((projection, i) =>
            {
                var bucketName = tracked.Buckets[i];
                var viewType   = tracked.Views[i];

                var projectionHash = GetClassHash(projection.GetType()) + "\r\n" + GetClassHash(viewType);

                bool needsRebuild = !hashes.ContainsKey(bucketName) || hashes[bucketName] != projectionHash;
                return(new
                {
                    bucketName,
                    projection,
                    hash = projectionHash,
                    needsRebuild
                });
            }).ToArray();

            foreach (var memoryProjection in memoryProjections)
            {
                if (memoryProjection.needsRebuild)
                {
                    SystemObserver.Notify("[warn] {0} needs rebuild", memoryProjection.bucketName);
                }
                else
                {
                    SystemObserver.Notify("[good] {0} is up-to-date", memoryProjection.bucketName);
                }
            }


            var needRebuild = memoryProjections.Where(x => x.needsRebuild).ToArray();

            if (needRebuild.Length == 0)
            {
                return;
            }



            var watch = Stopwatch.StartNew();

            var wire = new RedirectToDynamicEvent();

            needRebuild.ForEach(x => wire.WireToWhen(x.projection));


            var handlersWatch = Stopwatch.StartNew();

            Observe(stream, wire);
            var timeTotal      = watch.Elapsed.TotalSeconds;
            var handlerTicks   = handlersWatch.ElapsedTicks;
            var timeInHandlers = Math.Round(TimeSpan.FromTicks(handlerTicks).TotalSeconds, 1);

            Console.WriteLine("Total Elapsed: {0}sec ({1}sec in handlers)", Math.Round(timeTotal, 0), timeInHandlers);


            // delete projections that were rebuilt
            var bucketNames = needRebuild.Select(x => x.bucketName).ToArray();

            foreach (var name in bucketNames)
            {
                targetContainer.Reset(name);

                var contents = memoryContainer.EnumerateContents(name);
                targetContainer.WriteContents(name, contents);
            }

            var allBuckets = new HashSet <string>(memoryProjections.Select(p => p.bucketName));
            var obsolete   = hashes.Keys.Where(s => !allBuckets.Contains(s)).ToArray();

            foreach (var name in obsolete)
            {
                SystemObserver.Notify("[warn] {0} is obsolete", name);
                targetContainer.Reset(name);
            }
            storage.UpdateSingletonEnforcingNew <ProjectionHash>(x =>
            {
                x.Entries.Clear();

                foreach (var prj in memoryProjections)
                {
                    x.Entries[prj.bucketName] = prj.hash;
                }
            });
        }
 public SimpleDispatcher(AggregateFactory aggregateFactory, IEnvelopeStreamer serializer, ITapeStream tapeStream)
 {
     _aggregateFactory = aggregateFactory;
     _serializer       = serializer;
     _tapeStream       = tapeStream;
 }