private void RseQueryTest(int count) { using (var session = Domain.OpenSession()) using (session.Activate()) { using (var ts = session.OpenTransaction()) { TestHelper.CollectGarbage(); using (warmup ? null : new Measurement("RSE query", count)) { for (int i = 0; i < count; i++) { var pKey = new Parameter <Tuple>(); var rs = Domain.Model.Types[typeof(Simplest)].Indexes.PrimaryIndex.GetQuery().Seek(() => pKey.Value); using (new ParameterContext().Activate()) { pKey.Value = Tuple.Create((long)(i % instanceCount)); var es = rs.GetRecordSet(session).ToEntities <Simplest>(0); foreach (var o in es) { // Doing nothing, just enumerate } } } ts.Complete(); } } } }
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy, int[] keyIndexes) { return(new Key <T1, T2, T3>(nodeId, type, accuracy, tuple.GetValueOrDefault <T1>(keyIndexes[0]), tuple.GetValueOrDefault <T2>(keyIndexes[1]), tuple.GetValueOrDefault <T3>(keyIndexes[2]))); }
public static Key Create(string nodeId, TypeInfo type, Tuple tuple, TypeReferenceAccuracy accuracy) { return(new Key <T1, T2, T3>(nodeId, type, accuracy, tuple.GetValueOrDefault <T1>(0), tuple.GetValueOrDefault <T2>(1), tuple.GetValueOrDefault <T3>(2))); }
/// <exception cref="InvalidOperationException"> /// Attempt to associate non-null <paramref name="tuple"/> with <paramref name="key"/> of unknown type. /// </exception> internal EntityState UpdateStateInCache(Key key, Tuple tuple, bool isStale) { var result = EntityStateCache[key, true]; if (result == null) { if (!key.HasExactType && tuple != null) { throw Exceptions.InternalError( Strings.ExCannotAssociateNonEmptyEntityStateWithKeyOfUnknownType, OrmLog.Instance); } result = AddEntityStateToCache(key, tuple, isStale); SystemEvents.NotifyEntityMaterialized(result); Events.NotifyEntityMaterialized(result); } else { if (!result.Key.HasExactType && key.HasExactType) { EntityStateCache.RemoveKey(result.Key); result = AddEntityStateToCache(key, tuple, result.IsStale); } result.Update(tuple); result.IsStale = isStale; if (IsDebugEventLoggingEnabled) { OrmLog.Debug(Strings.LogSessionXUpdatingCacheY, this, result); } } return(result); }
public void CombinedTest() { int minLength = 30; int maxLength = 200; int iterationCount = 1000; var rnd = RandomManager.CreateRandom(); var generator = InstanceGeneratorProvider.Default.GetInstanceGenerator <int>(); for (int i = 0; i < iterationCount; i++) { int count = rnd.Next(maxLength - minLength); // Preparing data for test var fields = new Type[count]; var data = new int[count]; for (int j = 0; j < count; j++) { data[j] = generator.GetInstance(rnd); fields[j] = typeof(int); } { // Testing writes (untyped) var tuple = Tuple.Create(fields); for (int j = 0; j < count; j++) { tuple.SetValue(j, (object)data[j]); } // Testing reads (untyped) for (int j = 0; j < count; j++) { Assert.AreEqual(data[j], tuple.GetValue(j)); } // Testing serialization var tuple2 = Cloner.Clone(tuple); Assert.AreEqual(tuple, tuple2); Assert.AreEqual(tuple.Descriptor, tuple2.Descriptor); // Testing reads (untyped)); for (int j = 0; j < count; j++) { Assert.AreEqual(data[j], tuple2.GetValue(j)); } } { // Testing writes (typed) var tuple = Tuple.Create(fields); for (int j = 0; j < count; j++) { tuple.SetValue(j, data[j]); } // Testing reads (typed) for (int j = 0; j < count; j++) { Assert.AreEqual(data[j], tuple.GetValue <int>(j)); } } } }
private EntityState AddEntityStateToCache(Key key, Tuple tuple, bool isStale) { var result = new EntityState(this, key, tuple, isStale) { PersistenceState = PersistenceState.Synchronized }; EntityStateCache.Add(result); OrmLog.Debug(Strings.LogSessionXCachingY, this, result); return(result); }
public void ConfusingParameterTest() { var parameter = new Parameter <Tuple>("ConfusingParameter"); var parameterContext = new ParameterContext(); parameterContext.SetValue(parameter, Tuple.Create(false)); TestQuery( () => from o in Session.Demand().Query.All <MyEntity>() where o.HasStupidName == parameterContext.GetValue(parameter).GetValueOrDefault <bool>(0) select o ); }
public void SimpleTest() { var tracks = Session.Demand().Query.All <Track>().Take(10).ToList(); var ids = tracks.Select(supplier => (Tuple)Tuple.Create(supplier.TrackId)); var trackRs = Domain.Model.Types[typeof(Track)].Indexes.PrimaryIndex.GetQuery(); var inRs = trackRs.Include(() => ids, "columnName", new[] { 0 }); var inIndex = inRs.Header.Columns.Count - 1; var whereRs = inRs.Filter(tuple => tuple.GetValueOrDefault <bool>(inIndex)); var result = whereRs.GetRecordSet(Session.Current).ToList(); Assert.AreEqual(0, whereRs.GetRecordSet(Session.Current).Select(t => t.GetValue <int>(0)).Except(tracks.Select(s => s.TrackId)).Count()); }
public void ConfusingParameterTest() { var parameter = new Parameter <Tuple>(); using (new ParameterContext().Activate()) { parameter.Value = Tuple.Create(false); TestQuery( () => from o in Session.Demand().Query.All <MyEntity>() where o.HasStupidName == parameter.Value.GetValueOrDefault <bool>(0) select o ); } }
/// <summary> /// Extracts the foreign key from the specified <see cref="Tuple"/>. /// </summary> /// <param name="tuple">The tuple.</param> /// <param name="type">The type.</param> public Tuple ExtractForeignKey(TypeInfo type, Tuple tuple) { // foreignKeyExtractor can be null if OwnerType is interface if (foreignKeyExtractor != null) { return(foreignKeyExtractor.Apply(TupleTransformType.TransformedTuple, tuple)); } if (OwnerType.IsInterface) { var field = type.FieldMap[OwnerField]; return(field.ExtractValue(tuple)); } throw new InvalidOperationException(Strings.ExCanNotExtractForeignKey); }
internal EntityState CreateEntityState(Key key, bool failIfStateIsAlreadyBound) { // Checking for deleted entity with the same key var result = EntityStateCache[key, false]; EnforceChangeRegistrySizeLimit(); // Must be done before new entity registration // If type is unknown, we consider tuple is null, // so its Entity is considered as non-existing Tuple tuple = null; if (key.HasExactType) { // A tuple with all the fields set to default values rather then N/A var typeInfo = key.TypeInfo; tuple = typeInfo.CreateEntityTuple(key.Value, StorageNode.TypeIdRegistry[typeInfo]); } if (result == null) { result = new EntityState(this, key, tuple) { PersistenceState = PersistenceState.New }; EntityStateCache.Add(result); } else { if (result.Entity != null && !result.Entity.IsRemoved && failIfStateIsAlreadyBound) { throw new UniqueConstraintViolationException(string.Format(Strings.ExEntityWithKeyXAlreadyExists, key)); } result.Key = key; result.Tuple = tuple; result.PersistenceState = PersistenceState.New; } if (IsDebugEventLoggingEnabled) { OrmLog.Debug(Strings.LogSessionXCachingY, this, result); } return(result); }
public void ResolveKeyTest() { using (var session = Domain.OpenSession()) using (var t = session.OpenTransaction()) { var descriptor = Domain.Model.Types[typeof(Test)].Hierarchy.Key.TupleDescriptor; Tuple tuple = Tuple.Create(descriptor); tuple.SetValue(0, " , "); tuple.SetValue <Byte>(1, 1); tuple.SetValue <SByte>(2, -1); tuple.SetValue(3, DateTime.Now); Key k1 = Key.Create <Test>(Domain, tuple); var stringValue = k1.Format(); var k2 = Key.Parse(Domain, stringValue); Assert.AreEqual(k1, k2); t.Complete(); } }
public Entity Materialize(int entityIndex, int typeIdIndex, TypeInfo type, Pair <int>[] entityColumns, Tuple tuple) { var result = entities[entityIndex]; if (result != null) { return(result); } TypeReferenceAccuracy accuracy; int typeId = EntityDataReader.ExtractTypeId(type, typeIdRegistry, tuple, typeIdIndex, out accuracy); if (typeId == TypeInfo.NoTypeId) { return(null); } bool canCache = accuracy == TypeReferenceAccuracy.ExactType; var materializationInfo = MaterializationContext.GetTypeMapping(entityIndex, type, typeId, entityColumns); Key key; var keyIndexes = materializationInfo.KeyIndexes; if (!KeyFactory.IsValidKeyTuple(tuple, keyIndexes)) { return(null); } if (keyIndexes.Length <= WellKnown.MaxGenericKeyLength) { key = KeyFactory.Materialize(Session.Domain, Session.StorageNodeId, materializationInfo.Type, tuple, accuracy, canCache, keyIndexes); } else { var keyTuple = materializationInfo.KeyTransform.Apply(TupleTransformType.TransformedTuple, tuple); key = KeyFactory.Materialize(Session.Domain, Session.StorageNodeId, materializationInfo.Type, keyTuple, accuracy, canCache, null); } if (accuracy == TypeReferenceAccuracy.ExactType) { var entityTuple = materializationInfo.Transform.Apply(TupleTransformType.Tuple, tuple); var entityState = Session.Handler.UpdateState(key, entityTuple); result = entityState.Entity; } else { result = Session.Query.SingleOrDefault(key); } entities[entityIndex] = result; return(result); }
// Constructors // ReSharper disable MemberCanBeProtected.Global public SubQuery(ProjectionExpression projectionExpression, TranslatedQuery query, Parameter <Tuple> parameter, Tuple tuple, ItemMaterializationContext context) // ReSharper restore MemberCanBeProtected.Global { this.provider = context.Session.Query.Provider; var tupleParameterBindings = new Dictionary <Parameter <Tuple>, Tuple>(projectionExpression.TupleParameterBindings); var currentTranslatedQuery = query; var outerParameterContext = context.ParameterContext; var parameterContext = new ParameterContext(outerParameterContext); // Gather Parameter<Tuple> values from current ParameterScope for future use. outerParameterContext.SetValue(parameter, tuple); foreach (var tupleParameter in currentTranslatedQuery.TupleParameters) { var value = outerParameterContext.GetValue(tupleParameter); tupleParameterBindings[tupleParameter] = value; parameterContext.SetValue(tupleParameter, value); } this.projectionExpression = new ProjectionExpression( projectionExpression.Type, projectionExpression.ItemProjector, tupleParameterBindings, projectionExpression.ResultAccessMethod); var translatedQuery = new TranslatedQuery( query.DataSource, query.Materializer, query.ResultAccessMethod, tupleParameterBindings, EnumerableUtils <Parameter <Tuple> > .Empty); delayedQuery = new DelayedQuery <TElement>(context.Session, translatedQuery, parameterContext); context.Session.RegisterUserDefinedDelayedQuery(delayedQuery.Task); context.MaterializationContext.MaterializationQueue.Enqueue(MaterializeSelf); }
public static Structure CreateStructure(Session session, Type type, Tuple tuple) { var activator = DetachedStructureActivators.GetOrAdd(type, GetActivator <Session, Tuple, Structure>); return(activator.Invoke(session, tuple)); }
// Constructors // ReSharper disable MemberCanBeProtected.Global public SubQuery(ProjectionExpression projectionExpression, TranslatedQuery query, Parameter <Tuple> parameter, Tuple tuple, ItemMaterializationContext context) // ReSharper restore MemberCanBeProtected.Global { this.provider = context.Session.Query.Provider; var tupleParameterBindings = new Dictionary <Parameter <Tuple>, Tuple>(projectionExpression.TupleParameterBindings); var currentTranslatedQuery = ((TranslatedQuery <IEnumerable <TElement> >)query); // Gather Parameter<Tuple> values from current ParameterScope for future use. parameter.Value = tuple; foreach (var tupleParameter in currentTranslatedQuery.TupleParameters) { var value = tupleParameter.Value; tupleParameterBindings[tupleParameter] = value; } var parameterContext = new ParameterContext(); using (parameterContext.Activate()) foreach (var tupleParameter in currentTranslatedQuery.TupleParameters) { tupleParameter.Value = tupleParameter.Value; } this.projectionExpression = new ProjectionExpression( projectionExpression.Type, projectionExpression.ItemProjector, tupleParameterBindings, projectionExpression.ResultType); var translatedQuery = new TranslatedQuery <IEnumerable <TElement> >( query.DataSource, (Func <IEnumerable <Tuple>, Session, Dictionary <Parameter <Tuple>, Tuple>, ParameterContext, IEnumerable <TElement> >)query.UntypedMaterializer, tupleParameterBindings, EnumerableUtils <Parameter <Tuple> > .Empty); delayedSequence = new DelayedSequence <TElement>(context.Session, translatedQuery, parameterContext); context.Session.RegisterUserDefinedDelayedQuery(delayedSequence.Task); context.MaterializationContext.MaterializationQueue.Enqueue(MaterializeSelf); }
public static CompilableProvider Seek(this CompilableProvider source, Tuple key) { return(new SeekProvider(source, key)); }
internal virtual EntityState UpdateState(Key key, Tuple tuple) { return(Session.UpdateStateInCache(key, tuple)); }
/// <summary> /// Extracts the field value from the specified <see cref="Tuple"/>. /// </summary> /// <param name="tuple">The tuple to extract value from.</param> /// <returns><see cref="Tuple"/> instance with the extracted value.</returns> public Tuple ExtractValue(Tuple tuple) { return(valueExtractor.Apply(TupleTransformType.TransformedTuple, tuple)); }
public Grouping(ProjectionExpression projectionExpression, TranslatedQuery translatedQuery, Parameter <Tuple> parameter, Tuple tuple, TKey key, ItemMaterializationContext context) : base(projectionExpression, translatedQuery, parameter, tuple, context) { Key = key; }
internal EntityState UpdateStateInCache(Key key, Tuple tuple) { return(UpdateStateInCache(key, tuple, false)); }
// Constructors internal LongKey(string nodeId, TypeInfo type, TypeReferenceAccuracy accuracy, Tuple value) : base(nodeId, type, accuracy, value) { }