Exemplo n.º 1
0
		public override IEnumerable<SlideBlock> BuildUp(BuildUpContext context, IImmutableSet<string> filesInProgress)
		{
			if (filesInProgress.Contains(File))
				throw new Exception("Cyclic dependency");

			var xmlStream = new StringReader(context.FileSystem.GetContent(File));
			var serializer = new XmlSerializer(typeof(SlideBlock[]));
			var slideBlocks = (SlideBlock[])serializer.Deserialize(xmlStream);
			var newInProgress = filesInProgress.Add(File);
			return slideBlocks.SelectMany(b => b.BuildUp(context, newInProgress));
		}
Exemplo n.º 2
0
 public Item Bet(Address bettor)
 {
     return(Bettors.Contains(bettor)
         ? this
         : new Item(Stake, Bettors.Add(bettor)));
 }
Exemplo n.º 3
0
        private void HandleCoordinatorMessage(PersistentShardCoordinator.ICoordinatorMessage message)
        {
            if (message is PersistentShardCoordinator.HostShard)
            {
                var shard = ((PersistentShardCoordinator.HostShard)message).Shard;
                Log.Debug("Host shard [{0}]", shard);
                RegionByShard = RegionByShard.SetItem(shard, Self);
                UpdateRegionShards(Self, shard);

                // Start the shard, if already started this does nothing
                GetShard(shard);

                Sender.Tell(new PersistentShardCoordinator.ShardStarted(shard));
            }
            else if (message is PersistentShardCoordinator.ShardHome)
            {
                var home = (PersistentShardCoordinator.ShardHome)message;
                Log.Debug("Shard [{0}] located at [{1}]", home.Shard, home.Ref);
                IActorRef region;

                if (RegionByShard.TryGetValue(home.Shard, out region))
                {
                    if (region.Equals(Self) && !home.Ref.Equals(Self))
                    {
                        // should not happen, inconsistency between ShardRegion and PersistentShardCoordinator
                        throw new IllegalStateException(string.Format("Unexpected change of shard [{0}] from self to [{1}]", home.Shard, home.Ref));
                    }
                }

                RegionByShard = RegionByShard.SetItem(home.Shard, home.Ref);
                UpdateRegionShards(home.Ref, home.Shard);

                if (!home.Ref.Equals(Self))
                {
                    Context.Watch(home.Ref);
                }

                if (home.Ref.Equals(Self))
                {
                    var shardRef = GetShard(home.Shard);
                    if (!Equals(shardRef, ActorRefs.Nobody))
                    {
                        DeliverBufferedMessage(home.Shard, shardRef);
                    }
                }
                else
                {
                    DeliverBufferedMessage(home.Shard, home.Ref);
                }
            }
            else if (message is PersistentShardCoordinator.RegisterAck)
            {
                _coordinator = ((PersistentShardCoordinator.RegisterAck)message).Coordinator;
                Context.Watch(_coordinator);
                RequestShardBufferHomes();
            }
            else if (message is PersistentShardCoordinator.BeginHandOff)
            {
                var shard = ((PersistentShardCoordinator.BeginHandOff)message).Shard;
                Log.Debug("Begin hand off shard [{0}]", shard);
                IActorRef regionRef;
                if (RegionByShard.TryGetValue(shard, out regionRef))
                {
                    IImmutableSet <ShardId> updatedShards;
                    if (!Regions.TryGetValue(regionRef, out updatedShards))
                    {
                        updatedShards = ImmutableHashSet <ShardId> .Empty;
                    }

                    updatedShards = updatedShards.Remove(shard);
                    if (updatedShards.Count == 0)
                    {
                        Regions = Regions.Remove(regionRef);
                    }
                    else
                    {
                        Regions = Regions.SetItem(regionRef, updatedShards);
                    }

                    RegionByShard = RegionByShard.Remove(shard);
                }

                Sender.Tell(new PersistentShardCoordinator.BeginHandOffAck(shard));
            }
            else if (message is PersistentShardCoordinator.HandOff)
            {
                var shard = ((PersistentShardCoordinator.HandOff)message).Shard;
                Log.Debug("Hand off shard [{0}]", shard);

                // must drop requests that came in between the BeginHandOff and now,
                // because they might be forwarded from other regions and there
                // is a risk or message re-ordering otherwise
                if (ShardBuffers.ContainsKey(shard))
                {
                    ShardBuffers             = ShardBuffers.Remove(shard);
                    _loggedFullBufferWarning = false;
                }

                IActorRef actorRef;
                if (Shards.TryGetValue(shard, out actorRef))
                {
                    HandingOff = HandingOff.Add(actorRef);
                    actorRef.Forward(message);
                }
                else
                {
                    Sender.Tell(new PersistentShardCoordinator.ShardStopped(shard));
                }
            }
            else
            {
                Unhandled(message);
            }
        }
            /// <summary>
            /// TBD
            /// </summary>
            /// <param name="e">TBD</param>
            /// <exception cref="ArgumentException">TBD</exception>
            /// <returns>TBD</returns>
            public State Updated(IDomainEvent e)
            {
                if (e is ShardRegionRegistered)
                {
                    var message = e as ShardRegionRegistered;
                    if (Regions.ContainsKey(message.Region))
                    {
                        throw new ArgumentException($"Region {message.Region} is already registered", nameof(e));
                    }

                    return(Copy(regions: Regions.SetItem(message.Region, ImmutableList <ShardId> .Empty)));
                }

                if (e is ShardRegionProxyRegistered)
                {
                    var message = e as ShardRegionProxyRegistered;
                    if (RegionProxies.Contains(message.RegionProxy))
                    {
                        throw new ArgumentException($"Region proxy {message.RegionProxy} is already registered", nameof(e));
                    }

                    return(Copy(regionProxies: RegionProxies.Add(message.RegionProxy)));
                }

                if (e is ShardRegionTerminated)
                {
                    var message = e as ShardRegionTerminated;
                    if (!Regions.TryGetValue(message.Region, out var shardRegions))
                    {
                        throw new ArgumentException($"Terminated region {message.Region} not registered", nameof(e));
                    }

                    var newUnallocatedShards = RememberEntities ? UnallocatedShards.Union(shardRegions) : UnallocatedShards;
                    return(Copy(
                               regions: Regions.Remove(message.Region),
                               shards: Shards.RemoveRange(shardRegions),
                               unallocatedShards: newUnallocatedShards));
                }

                if (e is ShardRegionProxyTerminated)
                {
                    var message = e as ShardRegionProxyTerminated;
                    if (!RegionProxies.Contains(message.RegionProxy))
                    {
                        throw new ArgumentException($"Terminated region proxy {message.RegionProxy} not registered", nameof(e));
                    }

                    return(Copy(regionProxies: RegionProxies.Remove(message.RegionProxy)));
                }

                if (e is ShardHomeAllocated)
                {
                    var message = e as ShardHomeAllocated;
                    if (!Regions.TryGetValue(message.Region, out var shardRegions))
                    {
                        throw new ArgumentException($"Region {message.Region} not registered", nameof(e));
                    }
                    if (Shards.ContainsKey(message.Shard))
                    {
                        throw new ArgumentException($"Shard {message.Shard} is already allocated", nameof(e));
                    }

                    var newUnallocatedShards = RememberEntities ? UnallocatedShards.Remove(message.Shard) : UnallocatedShards;
                    return(Copy(
                               shards: Shards.SetItem(message.Shard, message.Region),
                               regions: Regions.SetItem(message.Region, shardRegions.Add(message.Shard)),
                               unallocatedShards: newUnallocatedShards));
                }

                if (e is ShardHomeDeallocated)
                {
                    var message = e as ShardHomeDeallocated;
                    if (!Shards.TryGetValue(message.Shard, out var region))
                    {
                        throw new ArgumentException($"Shard {message.Shard} not allocated", nameof(e));
                    }
                    if (!Regions.TryGetValue(region, out var shardRegions))
                    {
                        throw new ArgumentException($"Region {region} for shard {message.Shard} not registered", nameof(e));
                    }

                    var newUnallocatedShards = RememberEntities ? UnallocatedShards.Add(message.Shard) : UnallocatedShards;
                    return(Copy(
                               shards: Shards.Remove(message.Shard),
                               regions: Regions.SetItem(region, shardRegions.Where(s => s != message.Shard).ToImmutableList()),
                               unallocatedShards: newUnallocatedShards));
                }

                return(this);
            }
Exemplo n.º 5
0
        public Task Merge([NotNull] DocumentInfo document, [NotNull] IEnumerable <string> indexWords)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }
            if (indexWords == null)
            {
                throw new ArgumentNullException("indexWords");
            }

            var  sourceWords        = new SortedSet <string>(indexWords, wordComparer).ToList();
            bool hasWords           = sourceWords.Any();
            var  singleDocumentList = ImmutableHashSet.Create(document);

            // Merge join sorted word list with wordIndex list
            lock (lockIndex)
            {
                IImmutableSet <DocumentInfo> oldDocuments = state.documents;
                IImmutableSet <DocumentInfo> newDocuments;
                bool isNewDocument;
                if (hasWords)
                {
                    newDocuments  = oldDocuments.Add(document);
                    isNewDocument = newDocuments != oldDocuments; // new if was added to document set
                }
                else
                {
                    newDocuments  = oldDocuments.Remove(document);
                    isNewDocument = newDocuments == oldDocuments; // new if wasn't contained before in document set
                    if (isNewDocument)
                    {
                        return(CompletedTask.Instance);
                    }
                }

                var oldIndex  = state.wordIndex;
                var newIndex  = new InternalSortedList <string, IImmutableSet <DocumentInfo> >(this.wordComparer, oldIndex.Count + sourceWords.Count);
                int idxSource = 0;
                int idxTarget = 0;

                while (true)
                {
                    int compareResult = 0;
                    if (idxTarget >= oldIndex.Count)
                    {
                        compareResult = -1;
                    }
                    if (idxSource >= sourceWords.Count)
                    {
                        if (compareResult != 0)
                        {
                            break;
                        }
                        compareResult = 1;
                    }
                    if (compareResult == 0)
                    {
                        compareResult = wordComparer.Compare(sourceWords[idxSource], oldIndex[idxTarget].Key);
                    }

                    if (compareResult < 0)
                    {
                        // add and include
                        newIndex.AddSorted(sourceWords[idxSource], singleDocumentList);
                        idxSource += 1;
                        //idxTarget += 1;
                    }
                    else if (compareResult > 0)
                    {
                        var item = oldIndex[idxTarget];
                        if (!isNewDocument)
                        {
                            var newValue = item.Value.Remove(document);
                            if (newValue.Count > 0)
                            {
                                newIndex.AddSorted(item.Key, newValue);
                            }
                        }
                        else
                        {
                            newIndex.AddSorted(item.Key, item.Value);
                        }
                        idxTarget += 1;
                    }
                    else
                    {
                        var item = oldIndex[idxTarget];
                        newIndex.AddSorted(item.Key, item.Value.Add(document));
                        idxSource += 1;
                        idxTarget += 1;
                    }
                }
                this.state = new State(newIndex, newDocuments);
            }
            return(CompletedTask.Instance);
        }
Exemplo n.º 6
0
            /// <summary>
            /// TBD
            /// </summary>
            /// <param name="e">TBD</param>
            /// <exception cref="ArgumentException">TBD</exception>
            /// <returns>TBD</returns>
            public State Updated(IDomainEvent e)
            {
                if (e is ShardRegionRegistered)
                {
                    var message = e as ShardRegionRegistered;
                    if (Regions.ContainsKey(message.Region))
                    {
                        throw new ArgumentException($"Region {message.Region} is already registered", nameof(e));
                    }

                    return(Copy(regions: Regions.SetItem(message.Region, ImmutableList <ShardId> .Empty)));
                }
                else if (e is ShardRegionProxyRegistered)
                {
                    var message = e as ShardRegionProxyRegistered;
                    if (RegionProxies.Contains(message.RegionProxy))
                    {
                        throw new ArgumentException($"Region proxy {message.RegionProxy} is already registered", nameof(e));
                    }

                    return(Copy(regionProxies: RegionProxies.Add(message.RegionProxy)));
                }
                else if (e is ShardRegionTerminated)
                {
                    IImmutableList <ShardId> shardRegions;
                    var message = e as ShardRegionTerminated;
                    if (!Regions.TryGetValue(message.Region, out shardRegions))
                    {
                        throw new ArgumentException($"Region {message.Region} not registered", nameof(e));
                    }

                    return(Copy(
                               regions: Regions.Remove(message.Region),
                               shards: Shards.RemoveRange(shardRegions),
                               unallocatedShards: shardRegions.Aggregate(UnallocatedShards, (set, shard) => set.Add(shard))));
                }
                else if (e is ShardRegionProxyTerminated)
                {
                    var message = e as ShardRegionProxyTerminated;
                    if (!RegionProxies.Contains(message.RegionProxy))
                    {
                        throw new ArgumentException($"Region proxy {message.RegionProxy} not registered", nameof(e));
                    }

                    return(Copy(regionProxies: RegionProxies.Remove(message.RegionProxy)));
                }
                else if (e is ShardHomeAllocated)
                {
                    IImmutableList <ShardId> shardRegions;
                    var message = e as ShardHomeAllocated;
                    if (!Regions.TryGetValue(message.Region, out shardRegions))
                    {
                        throw new ArgumentException($"Region {message.Region} not registered", nameof(e));
                    }
                    if (Shards.ContainsKey(message.Shard))
                    {
                        throw new ArgumentException($"Shard {message.Shard} is already allocated", nameof(e));
                    }

                    return(Copy(
                               shards: Shards.SetItem(message.Shard, message.Region),
                               regions: Regions.SetItem(message.Region, shardRegions.Add(message.Shard)),
                               unallocatedShards: UnallocatedShards.Remove(message.Shard)));
                }
                else if (e is ShardHomeDeallocated)
                {
                    IActorRef region;
                    IImmutableList <ShardId> shardRegions;
                    var message = e as ShardHomeDeallocated;
                    if (!Shards.TryGetValue(message.Shard, out region))
                    {
                        throw new ArgumentException($"Shard {message.Shard} not allocated", nameof(e));
                    }
                    if (!Regions.TryGetValue(region, out shardRegions))
                    {
                        throw new ArgumentException($"Region {region} for shard {message.Shard} not registered", nameof(e));
                    }

                    return(Copy(
                               shards: Shards.Remove(message.Shard),
                               regions: Regions.SetItem(region, shardRegions.Where(s => s != message.Shard).ToImmutableList()),
                               unallocatedShards: UnallocatedShards.Add(message.Shard)));
                }
                else
                {
                    return(this);
                }
            }
Exemplo n.º 7
0
        private Tuple <ImmutableList <Rule>, RecursionProblem> CheckForProblematicLeftRecursion(
            ImmutableList <Rule> context,
            IImmutableSet <Rule> visited,
            RecursionProblem problem)
        {
            var rule = context[context.Count - 1];

            // avoid infinite recursion
            if (visited.Contains(rule))
            {
                return(null);
            }

            var produced       = context[0].Produced;
            var newVisited     = visited.Add(rule);
            var currentProblem = problem;

            for (var i = 0; i < rule.Symbols.Count; ++i)
            {
                var symbol = rule.Symbols[i] as NonTerminal;
                if (symbol == null)
                {
                    break;
                }

                // found left recursion
                if (symbol == produced)
                {
                    if (currentProblem != RecursionProblem.None)
                    {
                        return(Tuple.Create(context, currentProblem));
                    }

                    // otherwise, we don't need to recurse but we still need to check hidden
                }
                else
                {
                    // otherwise, recurse on each rule
                    var isAlias = this.IsAliasOf(symbol, produced) || this.IsAliasOf(produced, symbol);
                    foreach (var symbolRule in this.rulesByProduced[symbol])
                    {
                        var result = this.CheckForProblematicLeftRecursion(
                            context.Add(symbolRule),
                            newVisited,
                            currentProblem | (isAlias ? RecursionProblem.None : RecursionProblem.Indirect)
                            );
                        if (result != null)
                        {
                            return(result);
                        }
                    }
                }

                // continue past the symbol if it's nullable. Once we do this we're in the realm of hidden left recursion
                if (!this.firstFollow.IsNullable(symbol))
                {
                    break;
                }
                currentProblem |= RecursionProblem.Hidden;
            }

            // if we reach here without returning, we found nothing
            return(null);
        }
Exemplo n.º 8
0
 public SetDomain <T> With(T elem)
 {
     return(new SetDomain <T> (set.Add(elem)));
 }
Exemplo n.º 9
0
        public static Block <PolymorphicAction <ActionBase> > MineGenesisBlock(
            IDictionary <string, string> tableSheets,
            GoldDistribution[] goldDistributions,
            PendingActivationState[] pendingActivationStates,
            AdminState adminState,
            AuthorizedMinersState authorizedMinersState = null,
            IImmutableSet <Address> activatedAccounts   = null,
            bool isActivateAdminAddress  = false,
            IEnumerable <string> credits = null,
            PrivateKey privateKey        = null,
            DateTimeOffset?timestamp     = null
            )
        {
            if (!tableSheets.TryGetValue(nameof(GameConfigSheet), out var csv))
            {
                throw new KeyNotFoundException(nameof(GameConfigSheet));
            }
            var gameConfigState     = new GameConfigState(csv);
            var redeemCodeListSheet = new RedeemCodeListSheet();

            redeemCodeListSheet.Set(tableSheets[nameof(RedeemCodeListSheet)]);

            if (privateKey is null)
            {
                privateKey = new PrivateKey();
            }

            var ncg = new Currency("NCG", 2, privateKey.ToAddress());

            activatedAccounts = activatedAccounts ?? ImmutableHashSet <Address> .Empty;
            var initialStatesAction = new InitializeStates
                                      (
                rankingState: new RankingState(),
                shopState: new ShopState(),
                tableSheets: (Dictionary <string, string>)tableSheets,
                gameConfigState: gameConfigState,
                redeemCodeState: new RedeemCodeState(redeemCodeListSheet),
                adminAddressState: adminState,
                activatedAccountsState: new ActivatedAccountsState(
                    isActivateAdminAddress
                    ? activatedAccounts.Add(adminState.AdminAddress)
                    : activatedAccounts),
                goldCurrencyState: new GoldCurrencyState(ncg),
                goldDistributions: goldDistributions,
                pendingActivationStates: pendingActivationStates,
                authorizedMinersState: authorizedMinersState,
                creditsState: credits is null ? null : new CreditsState(credits)
                                      );
            var actions = new PolymorphicAction <ActionBase>[]
            {
                initialStatesAction,
            };
            var blockAction = new BlockPolicySource(Log.Logger).GetPolicy().BlockAction;

            return
                (BlockChain <PolymorphicAction <ActionBase> > .MakeGenesisBlock(
                     HashAlgorithmType.Of <SHA256>(),
                     actions,
                     privateKey : privateKey,
                     blockAction : blockAction,
                     timestamp : timestamp));
        }
 public override ChildrenContainer ShallDie(ActorRef actor)
 {
     return(new TerminatingChildrenContainer(InternalChildren, _toDie.Add(actor), _reason));
 }