Esempio n. 1
0
        public long GetId()
        {
            var ix = new long[1];

            _storage.UpdateSingletonEnforcingNew <DomainIdentityVector>(t => t.Reserve(ix));
            return(ix[0]);
        }
        void VerifyNonAtomic(NuclearStorage storage)
        {
            storage.AddOrUpdateEntity(1, new Entity());
            storage.GetEntity<Entity>(1);
            storage.UpdateEntity<Entity>(1, e => e.Do());
            storage.TryDeleteEntity<Entity>(1);

            storage.AddOrUpdateSingleton(() => new Entity(), e => e.Do());
            storage.UpdateSingletonEnforcingNew<Entity>(e => e.Do());
            storage.GetSingleton<Entity>();
            storage.TryDeleteSingleton<Entity>();
            storage.UpdateSingletonEnforcingNew<Entity>(e => e.Do());

            //storage.UpdateOrAddEntity<Entity>(1, e => e.Do());
            //storage.TryDelete<Entity>(1);

            //storage.SaveSingleton(new Entity());
            //storage.GetSingleton<Entity>();
            //storage.UpdateSingleton<Entity>(e => e.Do());
            //storage.TryDeleteSingleton<Entity>();
        }
Esempio n. 3
0
            public void Consume(Message message)
            {
                if (message.Fail)
                {
                    throw new InvalidOperationException("too much in one transaction");
                }


                new TransactionTester()
                {
                    OnCommit = () => _storage.UpdateSingletonEnforcingNew <Data>(i => i.Count += 1)
                };
            }
Esempio n. 4
0
        public void DispatchMessage(ImmutableEnvelope message)
        {
            // We either accept commands, events or control messages.


            // if tis is control message
            var controls = message
                           .GetAllAttributes()
                           .Where(ia => ia.Key.StartsWith("router-"))
                           .ToArray();

            if (controls.Length > 0)
            {
                if (message.Items.Length > 0)
                {
                    throw new InvalidOperationException("Router control message should not have any content");
                }

                _storage.UpdateSingletonEnforcingNew <RouteTable>(rt => UpgradeRouterTable(controls, rt));
                foreach (var attribute in controls)
                {
                    Console.WriteLine("  route {0}: {1}", attribute.Key, attribute.Value);
                }
                ReloadRouteMap();
                return;
            }

            // replace with your logic to detect commands.
            if (message.Items.All(m => m.Content is IPS_SampleCommand))
            {
                _queueFactory.GetWriteQueue("commands").PutMessage(_streamer.SaveEnvelopeData(message));
                return;
            }
            if (message.Items.All(m => m.Content is IPS_SampleEvent))
            {
                PublishEvent(message);
                return;
            }

            throw new InvalidOperationException(
                      "This is not command, event or control message. Please handle this case.");
        }
        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;
                }
            });
        }