/// <summary> /// Executa a ação para apagar os ados do registro no cache. /// </summary> /// <param name="action"></param> /// <returns></returns> private PersistenceActionResult ExecuteDeleteAction(PersistenceAction action) { var typeMetadata = _typeSchema.GetTypeMetadata(action.EntityFullName); if (typeMetadata == null) { return new PersistenceActionResult { Success = false, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.CachePersistenceExecuter_TypeMetadataNotFound, action.EntityFullName).Format(), } } ; var query = GetActionQuery(action, typeMetadata).CreateQueryInfo(); Queries.QueryResultSet queryResult = null; try { queryResult = ((CacheDataSource)_sourceContext.DataSource).ExecuteInCache(query); } catch (Exception ex) { return(new PersistenceActionResult { Success = false, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.CachePersistenceExecuter_ExecuteQueryInCacheError, Colosoft.Diagnostics.ExceptionFormatter.FormatException(ex, true)).Format() }); } var typeName = new Colosoft.Reflection.TypeName(action.EntityFullName); var keyGenerator = _keyFactory.Value.CreateGenerator(typeName); var recordKeys = new List <RecordKey>(); using (var recordEnumerator = new Colosoft.Caching.Queries.QueryResultSetRecordEnumerator(_typeSchema, Cache, queryResult, query)) { while (recordEnumerator.MoveNext()) { recordKeys.Add(RecordKeyFactory.Instance.Create(typeName, recordEnumerator.Current)); try { Cache.Remove(recordEnumerator.CurrentKey, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation)); } catch (Exception ex) { return(new PersistenceActionResult { Success = false, FailureMessage = ex.Message }); } } } return(new PersistenceActionResult { Success = true, Result = new DeleteActionResult(recordKeys) }); }
/// <summary> /// Inicializa a instancia. /// </summary> /// <param name="resultSet"></param> public void Initialize(QueryResultSet resultSet) { if (!_isInitialized) { this.Type = resultSet.Type; this.AggregateFunctionType = resultSet.AggregateFunctionType; this.AggregateFunctionResult = resultSet.AggregateFunctionResult; this.SearchKeysResult = resultSet.SearchKeysResult; this.SearchEntriesResult = resultSet.SearchEntriesResult; _isInitialized = true; } }
/// <summary> /// Construtor padrão. /// </summary> /// <param name="typeSchema">Esquema com os tipos.</param> /// <param name="cache">Instancia do cache.</param> /// <param name="result"></param> /// <param name="query"></param> public QueryResultSetRecordEnumerator(ITypeSchema typeSchema, Cache cache, QueryResultSet result, QueryInfo query) { typeSchema.Require("typeSchema").NotNull(); cache.Require("cache").NotNull(); result.Require("result").NotNull(); _typeSchema = typeSchema; _cache = cache; _result = result; _query = query; if (result is MultipleKeysQueryResultSet) { var resultSet = (MultipleKeysQueryResultSet)result; _multipleKeysEnumerator = resultSet.Result.Items.GetEnumerator(); } else { _keysEnumerator = result.SearchKeysResult.GetEnumerator(); } }
/// <summary> /// Executa a ação para atualizar os dados do registro no cache. /// </summary> /// <param name="action"></param> /// <returns></returns> private PersistenceActionResult ExecuteUpdateAction(PersistenceAction action) { var typeName = new Reflection.TypeName(action.EntityFullName); var typeMetadata = _typeSchema.GetTypeMetadata(action.EntityFullName); if (typeMetadata == null) { return new PersistenceActionResult { Success = false, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.CachePersistenceExecuter_TypeMetadataNotFound, action.EntityFullName).Format(), } } ; var query = GetActionQuery(action, typeMetadata).CreateQueryInfo(); Queries.QueryResultSet queryResult = null; try { queryResult = ((CacheDataSource)_sourceContext.DataSource).ExecuteInCache(query); } catch (Exception ex) { return(new PersistenceActionResult { Success = false, FailureMessage = ResourceMessageFormatter.Create(() => Properties.Resources.CachePersistenceExecuter_ExecuteQueryInCacheError, Colosoft.Diagnostics.ExceptionFormatter.FormatException(ex, true)).Format() }); } Colosoft.Query.Record.RecordDescriptor recordDescriptor = null; try { recordDescriptor = new Colosoft.Query.Record.RecordDescriptor(action.EntityFullName, GetRecordFields(action.EntityFullName, typeMetadata)); } catch (Exception ex) { return(new PersistenceActionResult { Success = false, FailureMessage = ex.Message }); } var keyGenerator = _keyFactory.Value.CreateGenerator(typeName); var typeInfoMap = Cache.GetTypeInfoMap(); var keysResult = queryResult.SearchKeysResult.ToArray(); if (keysResult.Length > 0) { foreach (var key in keysResult) { var record = CacheDataSource.CreateRecord(Cache, key, ref recordDescriptor, query); var recordValues = new object[recordDescriptor.Count]; record.GetValues(recordValues); foreach (var actionParameter in action.Parameters) { var indexOf = 0; for (; indexOf < recordDescriptor.Count; indexOf++) { if (StringComparer.InvariantCultureIgnoreCase.Equals(recordDescriptor[indexOf].Name, actionParameter.Name)) { break; } } if (indexOf < recordDescriptor.Count) { recordValues[indexOf] = actionParameter.Value; } } var newRecord = new CacheItemRecord(new Reflection.TypeName(action.EntityFullName), recordValues, recordDescriptor); var newKey = keyGenerator.GetKey(newRecord); try { var queryInfo = GetCacheEntryQueryInfo(Cache, typeInfoMap, newRecord); Cache.Remove(key, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation)); var insertResult = Cache.Insert(newKey, newRecord, null, null, new Colosoft.Caching.Policies.PriorityEvictionHint(CacheItemPriority.Normal), null, null, queryInfo, new BitSet(), null, 0, LockAccessType.IGNORE_LOCK, null, null, new OperationContext(OperationContextFieldName.OperationType, OperationContextOperationType.CacheOperation)); } catch (Exception ex) { return(new PersistenceActionResult { Success = false, FailureMessage = ex.Message }); } } } else { } return(new PersistenceActionResult { Success = true }); }
/// <summary> /// Compila o resultado. /// </summary> /// <param name="resultSet">Resultado que será usado na compilação.</param> public void Compile(QueryResultSet resultSet) { if (!_isInitialized) { Initialize(resultSet); } else { switch (this.Type) { case QueryType.SearchKeys: if (SearchKeysResult != null) { SearchKeysResult.AddRange(resultSet.SearchKeysResult); return; } this.SearchKeysResult = resultSet.SearchKeysResult; return; case QueryType.SearchEntries: if (this.SearchEntriesResult != null) { IDictionaryEnumerator enumerator = resultSet.SearchEntriesResult.GetEnumerator(); while (enumerator.MoveNext()) { SearchEntriesResult[enumerator.Key] = enumerator.Value; } return; } this.SearchEntriesResult = resultSet.SearchEntriesResult; return; case QueryType.AggregateFunction: decimal num; decimal num2; object obj2; object obj3; IComparable comparable; IComparable comparable2; switch (((AggregateFunctionType)this.AggregateFunctionResult.Key)) { case AggregateFunctionType.SUM: { obj2 = this.AggregateFunctionResult.Value; obj3 = resultSet.AggregateFunctionResult.Value; decimal?nullable = null; if ((obj2 == null) && (obj3 != null)) { nullable = new decimal?((decimal)obj3); } else if ((obj2 != null) && (obj3 == null)) { nullable = new decimal?((decimal)obj2); } else if ((obj2 != null) && (obj3 != null)) { num = (decimal)obj2; num2 = (decimal)obj3; nullable = new decimal?(num + num2); } if (nullable.HasValue) { this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.SUM, nullable); return; } this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.SUM, null); return; } case AggregateFunctionType.COUNT: { num = (decimal)this.AggregateFunctionResult.Value; num2 = (decimal)resultSet.AggregateFunctionResult.Value; decimal num3 = num + num2; this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.COUNT, num3); return; } case AggregateFunctionType.MIN: { comparable = (IComparable)this.AggregateFunctionResult.Value; comparable2 = (IComparable)resultSet.AggregateFunctionResult.Value; IComparable comparable3 = comparable; if ((comparable == null) && (comparable2 != null)) { comparable3 = comparable2; } else if ((comparable != null) && (comparable2 == null)) { comparable3 = comparable; } else if ((comparable == null) && (comparable2 == null)) { comparable3 = null; } else if (comparable2.CompareTo(comparable) < 0) { comparable3 = comparable2; } this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.MIN, comparable3); return; } case AggregateFunctionType.MAX: { comparable = (IComparable)this.AggregateFunctionResult.Value; comparable2 = (IComparable)resultSet.AggregateFunctionResult.Value; IComparable comparable4 = comparable; if ((comparable == null) && (comparable2 != null)) { comparable4 = comparable2; } else if ((comparable != null) && (comparable2 == null)) { comparable4 = comparable; } else if ((comparable == null) && (comparable2 == null)) { comparable4 = null; } else if (comparable2.CompareTo(comparable) > 0) { comparable4 = comparable2; } this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.MAX, comparable4); return; } case AggregateFunctionType.AVG: { obj2 = this.AggregateFunctionResult.Value; obj3 = resultSet.AggregateFunctionResult.Value; AverageResult result = null; if ((obj2 == null) && (obj3 != null)) { result = (AverageResult)obj3; } else if ((obj2 != null) && (obj3 == null)) { result = (AverageResult)obj2; } else if ((obj2 != null) && (obj3 != null)) { AverageResult result2 = (AverageResult)obj2; AverageResult result3 = (AverageResult)obj3; result = new AverageResult(); result.Sum = result2.Sum + result3.Sum; result.Count = result2.Count + result3.Count; } if (result != null) { this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.AVG, result); return; } this.AggregateFunctionResult = new DictionaryEntry(AggregateFunctionType.AVG, null); return; } } return; } } }