Ejemplo n.º 1
0
        /// <summary>
        /// Recupera os metadados do tipo pelo nome informado.
        /// </summary>
        /// <param name="fullName">Nome informado</param>
        /// <returns>Metadados do tipo</returns>
        private Data.Schema.ITypeMetadata GetTypeMetadata(string fullName)
        {
            if (string.IsNullOrEmpty(fullName))
            {
                throw new ArgumentException(ResourceMessageFormatter.Create(() => Properties.Resources.ArgumentException_EntityFullNameIsEmpty).Format());
            }
            var typeMetadata = _typeSchema.GetTypeMetadata(fullName);

            if (typeMetadata == null)
            {
                throw new InvalidOperationException(ResourceMessageFormatter.Create(() => Properties.Resources.InvalidOperationException_TypeMetadataNotFoundByFullName, fullName).Format());
            }
            return(typeMetadata);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Recarrega os dados dos tipos informados para o cache.
        /// </summary>
        /// <param name="typeNames"></param>
        /// <returns></returns>
        public RealoadDataCacheResult Reload(Colosoft.Reflection.TypeName[] typeNames)
        {
            if (CacheLoaderConfiguration == null)
            {
                throw new InvalidOperationException("CacheLoaderConfiguration undefined.");
            }
            var loaderProvider = CacheLoaderConfiguration.Provider;

            Colosoft.Caching.ICacheLoader cacheLoader;
            try
            {
                var cacheLoaderType = Type.GetType(string.Format("{0}, {1}", loaderProvider.ClassName, loaderProvider.AssemblyName));
                cacheLoader = (Colosoft.Caching.ICacheLoader)Activator.CreateInstance(cacheLoaderType);
            }
            catch (InvalidCastException ex)
            {
                throw new Colosoft.Caching.Exceptions.ConfigurationException(ResourceMessageFormatter.Create(() => Properties.Resources.ConfigurationException_ICacheLoaderNotImplemented).Format(), ex);
            }
            catch (Exception ex)
            {
                throw new Colosoft.Caching.Exceptions.ConfigurationException(ex.Message, ex);
            }
            var resultErrors  = new List <RealoadDataCacheResult.Error>();
            var typesMetadata = new List <ITypeMetadata>();

            foreach (var i in typeNames)
            {
                var metadata = _typeSchema.GetTypeMetadata(i.FullName);
                if (metadata == null)
                {
                    resultErrors.Add(new RealoadDataCacheResult.Error {
                        Type      = i,
                        Exception = new DetailsException(ResourceMessageFormatter.Create(() => Properties.Resources.DataCacheManager_Reload_TypeNotFound, i.FullName))
                    });
                }
                else
                {
                    typesMetadata.Add(metadata);
                }
            }
            var observerManager = Colosoft.Query.RecordObserverManager.Instance;

            for (var i = 0; i < typesMetadata.Count; i++)
            {
                var metadata = typesMetadata[i];
                var typeName = metadata.GetTypeName();
                Colosoft.Caching.Queries.QueryResultSet queryResult = null;
                var queryInfo = new Colosoft.Query.QueryInfo()
                {
                    Entities = new Query.EntityInfo[] {
                        new Query.EntityInfo(typeName.FullName)
                    }
                };
                try
                {
                    queryResult = _cache.Search(string.Format("select {0}.{1}", metadata.Namespace, metadata.Name), null);
                }
                catch (Exception ex)
                {
                    resultErrors.Add(new RealoadDataCacheResult.Error {
                        Type      = Colosoft.Data.Schema.TypeSchemaExtensions.GetTypeName(metadata),
                        Exception = new DetailsException(ResourceMessageFormatter.Create(() => Properties.Resources.DataCacheManager_Reload_QueryItemsToDelete, metadata.FullName), ex)
                    });
                    typesMetadata.RemoveAt(i--);
                    continue;
                }
                var recordKeys = new Dictionary <string, Colosoft.Query.RecordKey>();
                using (var recordEnumerator = new Colosoft.Caching.Queries.QueryResultSetRecordEnumerator(_typeSchema, Cache, queryResult, queryInfo))
                {
                    while (recordEnumerator.MoveNext())
                    {
                        recordKeys.Add(recordEnumerator.CurrentKey is string?(string)recordEnumerator.CurrentKey: (recordEnumerator.CurrentKey ?? "").ToString(), Colosoft.Query.RecordKeyFactory.Instance.Create(typeName, recordEnumerator.Current));
                    }
                }
                var cacheLoaderParameters = new System.Collections.Hashtable();
                cacheLoaderParameters.Add("sourceContext", _sourceContext);
                cacheLoaderParameters.Add("manager", this);
                cacheLoaderParameters.Add("logger", _logger);
                cacheLoaderParameters.Add("typeMetadata", null);
                cacheLoaderParameters.Add("cacheLoaderObserver", _cacheLoaderObserver);
                cacheLoaderParameters.Add("typesMetadata", new List <ITypeMetadata> {
                    metadata
                });
                if (cacheLoader is DataCacheLoader)
                {
                    ((DataCacheLoader)cacheLoader).Observer.OnTotalProgressChanged(new System.ComponentModel.ProgressChangedEventArgs(i * 100 / typesMetadata.Count, null));
                }
                cacheLoader.Init(cacheLoaderParameters);
                using (var startupLoader = new Colosoft.Caching.Loaders.CacheStartupLoader(new System.Collections.Hashtable(), Cache, _logger))
                {
                    var isNewEntry = false;
                    startupLoader.Initialize(cacheLoader);
                    startupLoader.InsertingEntry += (sender, e) => {
                        var key = e.Key is string?(string)e.Key : (e.Key ?? "").ToString();
                        if (recordKeys.Remove(key))
                        {
                            isNewEntry = false;
                            Cache.Remove(key, new Colosoft.Caching.OperationContext(Colosoft.Caching.OperationContextFieldName.OperationType, Colosoft.Caching.OperationContextOperationType.CacheOperation));
                        }
                        else
                        {
                            isNewEntry = true;
                        }
                    };
                    startupLoader.InsertedEntry += (sender, e) => {
                        if (e.Value is Colosoft.Query.IRecord)
                        {
                            var record    = e.Value as Colosoft.Query.IRecord;
                            var recordKey = Colosoft.Query.RecordKeyFactory.Instance.Create(typeName, record);
                            if (!isNewEntry)
                            {
                                var notifier = observerManager.GetRecordChangedNotifier(typeName, recordKey);
                                if (notifier.IsValid)
                                {
                                    notifier.Notify(record);
                                }
                            }
                            else
                            {
                                observerManager.NotifyRecordsInserted(typeName, new Query.IRecord[] {
                                    record
                                });
                            }
                        }
                    };
                    startupLoader.LoadCache();
                }
                foreach (var key in recordKeys)
                {
                    try
                    {
                        Cache.Remove(key.Key, new Colosoft.Caching.OperationContext(Colosoft.Caching.OperationContextFieldName.OperationType, Colosoft.Caching.OperationContextOperationType.CacheOperation));
                    }
                    catch (Exception ex)
                    {
                        resultErrors.Add(new RealoadDataCacheResult.Error {
                            Type      = Colosoft.Data.Schema.TypeSchemaExtensions.GetTypeName(metadata),
                            Exception = new DetailsException(ResourceMessageFormatter.Create(() => Properties.Resources.DataCacheManager_Reload_ClearItems, metadata.FullName), ex)
                        });
                        typesMetadata.RemoveAt(i--);
                        break;
                    }
                    observerManager.NotifyRecordDeleted(typeName, new Colosoft.Query.RecordKey[] {
                        key.Value
                    });
                }
            }
            return(new RealoadDataCacheResult {
                Success = resultErrors.Count == 0,
                Errors = resultErrors.ToArray()
            });
        }