Example #1
0
        private static IEnumerable <T> QueryFanout <T>(IDataProvider provider, string sql, dynamic param, IDbTransaction transaction, bool buffered, int?commandTimeout, CommandType?commandType, bool isAsync
                                                       , string federationName = null)
        {
            IEnumerable <T> result           = new ConcurrentBag <T>();
            var             operationResults = new ConcurrentBag <MemberOperation>();

            //resolve to the federation so we can get the members
            var        federatedDB = new FederatedDatabase(provider.Builder.ConnectionString);
            Federation targetFederation;

            if (!federatedDB.Federations.ContainsKey(federationName))
            {
                throw new ArgumentException("Federation does not exist", "federationName");
            }

            targetFederation = federatedDB.Federations[federationName];

            Parallel.ForEach(targetFederation.Members.Select(x => x.Value), new ParallelOptions()
            {
                MaxDegreeOfParallelism = IDbExtensions.DEFAULT_MAXTHREADS
            }, currentMember =>
            {
                Exception processingException = null;

                try
                {
                    using (var conn = provider.Builder.GetConnection())
                    {
                        conn.Open();
                        conn.UseFederationMember(currentMember);
                        IEnumerable <T> t = ExecuteQuery <T>(conn, sql, param, transaction, buffered, commandTimeout, commandType, isAsync);
                        result            = result.Concat(t);
                        conn.Close();
                    }
                }
                catch (Exception ex)
                {
                    processingException = ex;
                }
                finally
                {
                    operationResults.Add(new MemberOperation()
                    {
                        Member    = currentMember,
                        Exception = processingException
                    });
                }
            });

            // if any of the memberoperation instances have an exception then we throw
            if (operationResults.Any(x => x.Exception != null))
            {
                throw new FederationException()
                      {
                          Operations = operationResults.ToList()
                      };
            }

            return(result);
        }
Example #2
0
 private JsonSerializerSettings CreateSettings()
 {
     return(new JsonSerializerSettings()
     {
         ContractResolver = new ElasticContractResolver(this._settings),
         NullValueHandling = NullValueHandling.Ignore,
         DefaultValueHandling = DefaultValueHandling.Include,
         Converters = _defaultConverters.Concat(_extraConverters).ToList()
     });
 }
Example #3
0
        private IEnumerable <BcrLine> RunBcr(IEnumerable <Report> reports)
        {
            var bag = new ConcurrentBag <BcrLine>();

            var extraReportsToRun = new ConcurrentBag <Report>();

            Parallel.ForEach(
                reports,
                new ParallelOptions {
                MaxDegreeOfParallelism = 3
            },
                t =>
            {
                try
                {
                    var bcrLines = RunBcr(t);
                    foreach (var line in bcrLines)
                    {
                        bag.Add(line);
                    }
                }
                catch (Exception)
                {
                    if (t.ShouldFallBack)
                    {
                        var fallbackReports = t.FallbackReports().ToList();
                        _log.Info(
                            string.Format(
                                "Error getting BCR for {0}. Will fallback to {1}:{2}",
                                t.Parameter,
                                string.Join(
                                    Environment.NewLine,
                                    fallbackReports.Select(x => x.Parameter).ToArray()),
                                Environment.NewLine));
                        fallbackReports.ForEach(r => extraReportsToRun.Add(r));
                    }
                }
            });

            if (extraReportsToRun.Any())
            {
                return(bag.Concat(RunBcr(extraReportsToRun)));
            }

            return(bag);
        }
Example #4
0
        public async Task UpdateClientDependenciesAsync(List <Client> clientList, bool isActiveProgress = false)
        {
            int loadUnit = 500;
            ConcurrentBag <Contact> contactList = new ConcurrentBag <Contact>();
            ConcurrentBag <Address> addressList = new ConcurrentBag <Address>();

            // saving the clients
            List <Client> savedClientList = LoadClient(clientList);

            for (int i = 0; i < (savedClientList.Count() / loadUnit) || loadUnit >= savedClientList.Count() && i == 0; i++)
            {
                ConcurrentBag <Address> addressFoundList = new ConcurrentBag <Address>(await _gateWayClient.GetAddressDataByClientListAsync(savedClientList.Skip(i * loadUnit).Take(loadUnit).ToList()));
                addressList = new ConcurrentBag <Address>(addressList.Concat(new ConcurrentBag <Address>(addressFoundList)));

                ConcurrentBag <Contact> contactFoundList = new ConcurrentBag <Contact>(await _gateWayClient.GetContactDataByClientListAsync(savedClientList.Skip(i * loadUnit).Take(loadUnit).ToList()));
                contactList = new ConcurrentBag <Contact>(contactList.Concat(new ConcurrentBag <Contact>(contactFoundList)));
            }

            // saving the addresses into local database
            List <Address> savedAddressList = LoadAddress(addressList.ToList());

            // saving the contacts into the local database
            List <Contact> savedContactList = LoadContact(contactList.ToList());
        }
        /// <summary>
        /// Post process the types:
        ///  - If UserType has static members in more than one module, split it into multiple user types.
        ///  - Find parent type/namespace.
        /// </summary>
        /// <param name="userTypes">The list of user types.</param>
        /// <param name="symbolNamespaces">The symbol namespaces.</param>
        /// <returns>Newly generated user types.</returns>
        internal IEnumerable<UserType> ProcessTypes(IEnumerable<UserType> userTypes, Dictionary<Symbol, string> symbolNamespaces)
        {
            ConcurrentBag<UserType> newTypes = new ConcurrentBag<UserType>();

            // Split user types that have static members in more than one module
            Parallel.ForEach(Partitioner.Create(userTypes), (userType) =>
            {
                if (!userType.ExportStaticFields)
                    return;

                Symbol[] symbols = GlobalCache.GetSymbolStaticFieldsSymbols(userType.Symbol).ToArray();

                if (symbols.Length == 1)
                    return;

                bool foundSameNamespace = false;

                foreach (var symbol in symbols)
                {
                    string nameSpace = symbol.Module.Namespace;

                    if (userType.Namespace != nameSpace)
                        newTypes.Add(new UserType(symbol, null, nameSpace) { ExportDynamicFields = false });
                    else
                        foundSameNamespace = true;
                }

                userType.ExportStaticFields = foundSameNamespace;
            });

            // Find parent type/namespace
            Dictionary<string, UserType> namespaceTypes = new Dictionary<string, UserType>();

            foreach (UserType userType in userTypes)
            {
                Symbol symbol = userType.Symbol;

                if (symbol.Tag != SymTagEnum.SymTagUDT && symbol.Tag != SymTagEnum.SymTagEnum)
                    continue;

                string symbolName = symbol.Name;
                List<string> namespaces = symbol.Namespaces;

                if (namespaces.Count == 1)
                {
                    // Class is not defined in namespace nor in type.
                    continue;
                }

                StringBuilder currentNamespaceSB = new StringBuilder();
                UserType previousNamespaceUserType = null;

                for (int i = 0; i < namespaces.Count - 1; i++)
                {
                    if (i > 0)
                        currentNamespaceSB.Append("::");
                    currentNamespaceSB.Append(namespaces[i]);

                    string currentNamespace = currentNamespaceSB.ToString();
                    UserType namespaceUserType;

                    if (!namespaceTypes.TryGetValue(currentNamespace, out namespaceUserType))
                        namespaceUserType = GlobalCache.GetUserType(currentNamespace, symbol.Module);

                    // Put type under exported template type (TODO: Remove this when template types start checking subtypes)
                    var templateType = namespaceUserType as TemplateUserType;

                    if (templateType != null)
                        namespaceUserType = templateType.TemplateType;
                    if (namespaceUserType == null)
                    {
                        namespaceUserType = new NamespaceUserType(new string[] { namespaces[i] }, previousNamespaceUserType == null ? symbolNamespaces[symbol] : null);
                        if (previousNamespaceUserType != null)
                            namespaceUserType.UpdateDeclaredInType(previousNamespaceUserType);
                        namespaceTypes.Add(currentNamespace, namespaceUserType);
                        newTypes.Add(namespaceUserType);
                    }

                    previousNamespaceUserType = namespaceUserType;
                }

                userType.UpdateDeclaredInType(previousNamespaceUserType);
            }

            // Update Class Name if it has duplicate with the namespace it is declared in
            foreach (UserType userType in newTypes.Concat(userTypes))
            {
                userType.ClassName = userType.OriginalClassName;
                if (userType.DeclaredInType != null && userType.OriginalClassName == userType.DeclaredInType.ClassName)
                {
                    userType.ClassName += "_";
                }

                TemplateUserType templateUserType = userType as TemplateUserType;

                if (templateUserType != null)
                {
                    foreach (UserType specializedUserType in templateUserType.SpecializedTypes)
                    {
                        specializedUserType.ClassName = userType.ClassName;
                    }
                }
            }

            // Remove duplicate types from exported template types (TODO: Remove this when template types start checking subtypes)
            foreach (UserType userType in userTypes)
            {
                TemplateUserType templateType = userType as TemplateUserType;

                if (templateType == null)
                    continue;

                HashSet<string> uniqueTypes = new HashSet<string>();

                foreach (var innerType in templateType.InnerTypes.ToArray())
                {
                    string className;

                    if (!(innerType is NamespaceUserType))
                        className = innerType.ClassName;
                    else
                        className = innerType.Namespace;
                    if (uniqueTypes.Contains(className))
                        templateType.InnerTypes.Remove(innerType);
                    else
                        uniqueTypes.Add(className);
                }
            }

            // Find all derived classes
            foreach (UserType userType in userTypes)
            {
                // We are doing this only for UDTs
                if (userType is EnumUserType || userType is GlobalsUserType || userType is NamespaceUserType)
                    continue;

                // For template user types, we want to remember all specializations
                TemplateUserType templateUserType = userType as TemplateUserType;

                if (templateUserType != null)
                {
                    foreach (UserType specializedUserType in templateUserType.SpecializedTypes)
                    {
                        AddDerivedClassToBaseClasses(specializedUserType);
                    }
                }
                else
                {
                    AddDerivedClassToBaseClasses(userType);
                }
            }

            // Merge namespaces when possible
            foreach (UserType userType in newTypes)
            {
                NamespaceUserType nameSpace = userType as NamespaceUserType;

                if (nameSpace == null)
                {
                    continue;
                }

                nameSpace.MergeIfPossible();
            }

            // Remove empty namespaces after merge
            List<UserType> removedUserTypes = new List<UserType>();

            foreach (UserType userType in newTypes)
            {
                NamespaceUserType nameSpace = userType as NamespaceUserType;

                if (nameSpace == null)
                {
                    continue;
                }

                if (nameSpace.InnerTypes.Count == 0)
                {
                    removedUserTypes.Add(nameSpace);
                }
            }

            return newTypes.Except(removedUserTypes);
        }