public static Tuple DeserializeKeyFields(TypeInfo entityType, TypeIdRegistry registry, SerializationInfo info, StreamingContext context) { var keyTuple = Tuple.Create(entityType.Hierarchy.Key.TupleDescriptor); foreach (FieldInfo keyField in entityType.Fields.Where(f => f.IsPrimaryKey && f.Parent == null)) { if (keyField.IsTypeId) { keyTuple.SetValue(keyField.MappingInfo.Offset, registry[entityType]); } else { var value = info.GetValue(keyField.Name, keyField.ValueType); if (keyField.IsEntity) { var referencedEntity = (Entity)value; if (!IsInitialized(referencedEntity)) { DeserializationContext.Demand().InitializeEntity(referencedEntity); } var referencedTuple = referencedEntity.Key.Value; referencedTuple.CopyTo(keyTuple, keyField.MappingInfo.Offset); } else { keyTuple.SetValue(keyField.MappingInfo.Offset, value); } } } return(keyTuple); }
public static ITrackingItem CreateTrackingItem(Key key, TrackingItemState state) { var tuple = Tuple.Create(typeof(string)); var diff = new DifferentialTuple(tuple); return((ITrackingItem)Activator.CreateInstance(TrackingItemType, key, state, diff)); }
public override Tuple GenerateKey(KeyInfo keyInfo, Session session) { var result = Tuple.Create(counter.ToString()); counter++; return(result); }
public void RegisterRevivedItemWithDifferenceTupleTest() { using (var session = Domain.OpenSession()) using (var tx = session.OpenTransaction()) { var registry = session.EntityChangeRegistry; Assert.That(registry.Count, Is.EqualTo(0)); Assert.That(registry.GetItems(PersistenceState.New).Any(), Is.False); Assert.That(registry.GetItems(PersistenceState.Modified).Any(), Is.False); Assert.That(registry.GetItems(PersistenceState.Removed).Any(), Is.False); var dTuple = new DifferentialTuple( Tuple.Create <int, string>(0, null), Tuple.Create <int, string>(0, "here is some changes")); var state1 = CreateDummyState(session, 100, dTuple); state1.SetPersistenceState(PersistenceState.Removed); registry.Register(state1); Assert.That(registry.GetItems(PersistenceState.New).Any(), Is.False); Assert.That(registry.GetItems(PersistenceState.Modified).Any(), Is.False); Assert.That(registry.GetItems(PersistenceState.Removed).Count(), Is.EqualTo(1)); Assert.That(registry.GetItems(PersistenceState.Removed).All(i => i == state1)); Assert.That(registry.Count, Is.EqualTo(1)); state1.SetPersistenceState(PersistenceState.New); registry.Register(state1); Assert.That(state1.PersistenceState, Is.EqualTo(PersistenceState.Modified)); Assert.That(registry.GetItems(PersistenceState.New).Any(), Is.False); Assert.That(registry.GetItems(PersistenceState.Modified).Count(), Is.EqualTo(1)); Assert.That(registry.GetItems(PersistenceState.Modified).All(i => i == state1), Is.True); Assert.That(registry.GetItems(PersistenceState.Removed).Any(), Is.False); Assert.That(registry.Count, Is.EqualTo(1)); } }
public void GeneratorAdvancedTest() { var types = new[] { typeof(int), typeof(long), typeof(float), typeof(double), typeof(string), typeof(DateTime), typeof(TimeSpan), typeof(byte[]) }; var sizeRandomizer = new Random(); var typeRandomizer = new Random(); const int maxSize = 30; const int runCount = 10000; using (new Measurement("Create tuple with random descriptor", runCount)) for (int i = 0; i < runCount; i++) { var count = sizeRandomizer.Next(maxSize + 1); var tupleTypes = Enumerable.Repeat(0, count) .Select(_ => types[typeRandomizer.Next(types.Length)]) .ToArray(count); var descriptor = TupleDescriptor.Create(tupleTypes); var tuple = Tuple.Create(descriptor); } }
private void BuildConverter() { Type itemType = isKeyConverter ? entityTypestoredInKey : typeof(TItem); int index = 0; ParameterExpression parameterExpression = Expression.Parameter(itemType, "item"); IEnumerable <Type> types = EnumerableUtils <Type> .Empty; if (IsPersistableType(itemType)) { Expression = (Expression)BuildField(itemType, ref index, ref types); TupleDescriptor = TupleDescriptor.Create(types); } else { Xtensive.Collections.ISet <Type> processedTypes = new Set <Type>(); LocalCollectionExpression itemExpression = BuildLocalCollectionExpression(itemType, processedTypes, ref index, null, ref types); TupleDescriptor = TupleDescriptor.Create(types); Expression = itemExpression; } Func <TItem, Tuple> converter = delegate(TItem item) { RegularTuple tuple = Tuple.Create(TupleDescriptor); if (ReferenceEquals(item, null)) { return(tuple); } int offset = 0; FillLocalCollectionField(item, tuple, Expression); return(tuple); }; this.converter = converter; }
private void BuildConverter() { var itemType = isKeyConverter ? entityTypestoredInKey : typeof(TItem); var index = 0; var types = new TupleTypeCollection(); if (IsPersistableType(itemType)) { Expression = (Expression)BuildField(itemType, ref index, types); TupleDescriptor = TupleDescriptor.Create(types.ToArray(types.Count)); } else { var processedTypes = new HashSet <Type>(); var itemExpression = BuildLocalCollectionExpression(itemType, processedTypes, ref index, null, types); TupleDescriptor = TupleDescriptor.Create(types.ToArray(types.Count)); Expression = itemExpression; } converter = delegate(TItem item) { Tuple tuple = Tuple.Create(TupleDescriptor); if (ReferenceEquals(item, null)) { return(tuple); } FillLocalCollectionField(item, tuple, Expression); return(tuple); }; }
private void WriteTypes(IEnumerable <TypeMetadata> types) { var descriptor = CreateDescriptor(mapping.Type, mapping.IntMapping, mapping.TypeId, ParameterTransmissionType.Regular, mapping.StringMapping, mapping.TypeName, ParameterTransmissionType.Regular); executor.Overwrite(descriptor, types.Select(item => (Tuple)Tuple.Create(IntStringDescriptor, item.Id, item.Name))); }
private void WriteAssemblies(IEnumerable <AssemblyMetadata> assemblies) { var descriptor = CreateDescriptor(mapping.Assembly, mapping.StringMapping, mapping.AssemblyName, ParameterTransmissionType.Regular, mapping.StringMapping, mapping.AssemblyVersion, ParameterTransmissionType.Regular); executor.Overwrite(descriptor, assemblies.Select(item => (Tuple)Tuple.Create(StringStringDescriptor, item.Name, item.Version))); }
public override Tuple GenerateKey(KeyInfo keyInfo, Session session) { var sequence = sequenceProvider.GetSequence(keyInfo.Sequence, session); var keyValue = sequence.GetNextValue(keyInfo.Sequence, session); var keyTuple = Tuple.Create(keyInfo.TupleDescriptor); keyTuple.SetValue(0, keyValue); return(keyTuple); }
private void MapCompositeKey(OperationExecutionContext context) { // TODO: AY: Review this later var domain = context.Session.Domain; var nodeId = context.Session.StorageNodeId; var keyInfo = Key.TypeReference.Type.Key; var hierarchy = keyInfo.Hierarchy; if (hierarchy.Key.Fields.Count == 1 && !hierarchy.Key.Fields[0].IsEntity) { return; } var columnIndex = 0; var sourceTuple = Key.Value; var resultTuple = Tuple.Create(sourceTuple.Descriptor); var hasTemporaryComponentBeenFound = false; foreach (var keyField in hierarchy.Key.Fields) { if (keyField.Parent != null) { continue; } if (keyField.IsPrimitive) { resultTuple.SetValue(columnIndex, sourceTuple.GetValue(columnIndex)); columnIndex++; } else { var association = keyField.Associations.Last(); var componentKeyValue = Tuple.Create(association.TargetType.Key.TupleDescriptor); sourceTuple.CopyTo(componentKeyValue, columnIndex, keyField.MappingInfo.Length); var componentKey = Key.Create(domain, nodeId, association.TargetType.UnderlyingType, TypeReferenceAccuracy.BaseType, componentKeyValue); var componentKeyLength = componentKey.Value.Count; Key mappedKey; if (context.KeyMapping.TryGetValue(componentKey, out mappedKey)) { mappedKey.Value.CopyTo(resultTuple, 0, columnIndex, componentKeyLength); hasTemporaryComponentBeenFound = true; } else { componentKeyValue.CopyTo(resultTuple, 0, columnIndex, componentKeyLength); } columnIndex += componentKeyLength; } } if (hasTemporaryComponentBeenFound) { var result = Key.Create(domain, nodeId, Key.TypeReference.Type.UnderlyingType, TypeReferenceAccuracy.BaseType, resultTuple); context.AddKeyMapping(Key, result); } }
public void MainTest() { Xtensive.Tuples.Tuple source = Tuple.Create(1); MapTransform transform = new MapTransform(true, TupleDescriptor.Create <byte, int, string>(), new[] { -1, 0 }); Xtensive.Tuples.Tuple result = transform.Apply(TupleTransformType.TransformedTuple, source); Assert.AreEqual(TupleFieldState.Default, result.GetFieldState(0)); Assert.AreEqual(TupleFieldState.Available, result.GetFieldState(1)); Assert.AreEqual(TupleFieldState.Default, result.GetFieldState(2)); }
public Tuple Read(DbDataReader source) { var target = Tuple.Create(Descriptor); for (int i = 0; i < mappings.Length; i++) { var value = !source.IsDBNull(i) ? mappings[i].ReadValue(source, i) : null; target.SetValue(i, value); } return(target); }
/// <summary> /// Returns a <see cref="Tuple"/> with specified <paramref name="descriptor"/> /// parsed from the <paramref name="source"/> string. /// </summary> /// <param name="descriptor">The descriptor of <see cref="Tuple"/> to parse.</param> /// <param name="source">The string to parse.</param> /// <returns>A <see cref="Tuple"/> parsed from the <paramref name="source"/> string.</returns> /// <exception cref="System.InvalidOperationException"><paramref name="source"/> string /// can't be parsed to a <see cref="Tuple"/> with specified <paramref name="descriptor"/>.</exception> public static Tuple Parse(this TupleDescriptor descriptor, string source) { ArgumentValidator.EnsureArgumentNotNull(descriptor, "descriptor"); var target = Tuple.Create(descriptor); var count = target.Count; var sources = source.RevertibleSplit(Escape, Comma).ToArray(); for (int i = 0; i < count; i++) { ParseHandler.Get(descriptor[i]).Execute(sources, target, i); } return(target); }
private void WriteExtensions(IEnumerable <ExtensionMetadata> extensions) { var extensionTextTransmissionType = driver.ProviderInfo.Supports(ProviderFeatures.LargeObjects) ? ParameterTransmissionType.CharacterLob : ParameterTransmissionType.Regular; var descriptor = CreateDescriptor(mapping.Extension, mapping.StringMapping, mapping.ExtensionName, ParameterTransmissionType.Regular, mapping.StringMapping, mapping.ExtensionText, extensionTextTransmissionType, ProvideExtensionMetadataFilter); executor.Overwrite(descriptor, extensions.Select(item => (Tuple)Tuple.Create(StringStringDescriptor, item.Name, item.Value))); }
public override void Initialize(Domain ownerDomain, TupleDescriptor keyTupleDescriptor) { prototype = Tuple.Create(keyTupleDescriptor); var accessor = ownerDomain.Services.Demand <IStorageSequenceAccessor>(); if (ownerDomain.StorageProviderInfo.Supports(ProviderFeatures.TransactionalKeyGenerators)) { sequenceProvider = new SessionCachingSequenceProvider <TValue>(accessor); } else { sequenceProvider = new DomainCachingSequenceProvider <TValue>(accessor); } }
public void ProfileGuidFieldAccessTest() { const int iterationCount = 10000000; TupleDescriptor descriptor = TupleDescriptor.Create <Guid>(); Tuple tuple = Tuple.Create(descriptor); using (new Measurement("Tuple.SetValue", iterationCount)) for (int i = 0; i < iterationCount; i++) { tuple.SetValue(0, Guid.Empty); } using (new Measurement("Tuple.GetValue(_,_)", iterationCount)) for (int i = 0; i < iterationCount; i++) { TupleFieldState state; tuple.GetValue(0, out state); } using (new Measurement("Tuple.SetValue<T>", iterationCount)) for (int i = 0; i < iterationCount; i++) { tuple.SetValue(0, Guid.Empty); } using (new Measurement("Tuple.SetValue<T?>", iterationCount)) for (int i = 0; i < iterationCount; i++) { tuple.SetValue <Guid?>(0, Guid.Empty); } using (new Measurement("Tuple.GetValue<T>", iterationCount)) for (int i = 0; i < iterationCount; i++) { tuple.GetValue <Guid>(0); } using (new Measurement("Tuple.GetValue<T>(_,_)", iterationCount)) for (int i = 0; i < iterationCount; i++) { TupleFieldState state; tuple.GetValue <Guid>(0, out state); } using (new Measurement("Tuple.GetValue<T?>(_,_)", iterationCount)) for (int i = 0; i < iterationCount; i++) { TupleFieldState state; tuple.GetValue <Guid?>(0, out state); } }
public void ProfileIntFieldAccessTest() { const int iterationCount = 10000000; TupleDescriptor descriptor = TupleDescriptor.Create(shortFieldTypes); Tuple tuple = Tuple.Create(descriptor); using (new Measurement("Tuple.SetValue", iterationCount)) for (int i = 0; i < iterationCount; i++) { tuple.SetValue(0, (object)i); } using (new Measurement("Tuple.GetValue(_,_)", iterationCount)) for (int i = 0; i < iterationCount; i++) { TupleFieldState state; tuple.GetValue(0, out state); } using (new Measurement("Tuple.SetValue<T>", iterationCount)) for (int i = 0; i < iterationCount; i++) { tuple.SetValue(0, i); } using (new Measurement("Tuple.SetValue<T?>", iterationCount)) for (int i = 0; i < iterationCount; i++) { tuple.SetValue <int?>(0, i); } using (new Measurement("Tuple.GetValue<T>", iterationCount)) for (int i = 0; i < iterationCount; i++) { tuple.GetValue <int>(0); } using (new Measurement("Tuple.GetValue<T>(_,_)", iterationCount)) for (int i = 0; i < iterationCount; i++) { TupleFieldState state; tuple.GetValue <int>(0, out state); } using (new Measurement("Tuple.GetValue<T?>(_,_)", iterationCount)) for (int i = 0; i < iterationCount; i++) { TupleFieldState state; tuple.GetValue <int?>(0, out state); } }
public void FormatTest() { const int iterationCount = 1000000; var output = new List <string>(iterationCount); var descriptor = TupleDescriptor.Create(typicalFieldTypes); var tuple = Tuple.Create(descriptor); tuple.SetValue(0, 123L); tuple.SetValue(2, "hello"); using (new Measurement("Format", iterationCount)) for (int i = 0; i < iterationCount; i++) { var formatted = tuple.Format(); output.Add(formatted); } }
public void BasicTest() { var t = Tuple.Create(TupleDescriptor.Create(new [] { typeof(string), typeof(int), typeof(string), typeof(TimeSpan), typeof(string), typeof(string) })); t.SetValue(0, string.Empty); t.SetValue(2, "n\\a"); t.SetValue(3, new TimeSpan()); t.SetValue(4, null); t.SetValue(5, "null"); var s = t.Format(); var tt = Xtensive.Tuples.Tuple.Parse(t.Descriptor, s); Assert.AreEqual(t, tt); }
public void ParseTest() { const int iterationCount = 1000000; var output = new List <Tuple>(iterationCount); var descriptor = TupleDescriptor.Create(typicalFieldTypes); var tuple = Tuple.Create(descriptor); tuple.SetValue(0, 123L); tuple.SetValue(2, "hello"); var source = tuple.Format(); using (new Measurement("Parse", iterationCount)) for (int i = 0; i < iterationCount; i++) { var parsed = descriptor.Parse(source); output.Add(parsed); } }
public void GeneratorTest() { const int fieldCount = 10; const int iterationCount = 100; int iteration = 0; Random random = RandomManager.CreateRandom(SeedVariatorType.CallingMethod); using (new Measurement("Random Tuple generation", iterationCount)) { while (iteration++ < iterationCount) { var fieldTypes = new List <Type>(); for (int i = 0; i < fieldCount; i++) { fieldTypes.Add(allFieldTypes[random.Next(allFieldTypes.Length)]); } var d = TupleDescriptor.Create(fieldTypes.ToArray()); var tuple = Tuple.Create(d); } } }
public void Main() { Tuple t = Tuple.Create <int, string>(0, null); Assert.IsTrue(t.GetFieldState(0).IsAvailable()); Assert.IsFalse(t.GetFieldState(0).IsNull()); Assert.IsTrue(t.GetFieldState(1).IsAvailable()); Assert.IsTrue(t.GetFieldState(1).IsNull()); t.SetValue(0, null); t.SetValue(1, null); Assert.IsTrue(t.GetFieldState(0).IsAvailable()); Assert.IsTrue(t.GetFieldState(1).IsAvailable()); Assert.IsTrue(t.GetFieldState(0).IsNull()); Assert.IsTrue(t.GetFieldState(1).IsNull()); t.SetValue(0, new int?(32)); Assert.IsTrue(t.GetFieldState(0).IsAvailable()); Assert.IsFalse(t.GetFieldState(0).IsNull()); t.SetValue(0, (int?)null); Assert.IsTrue(t.GetFieldState(0).IsAvailable()); Assert.IsTrue(t.GetFieldState(0).IsNull()); }
public void DifferentialTest() { TupleDescriptor descriptor = TupleDescriptor.Create(fieldTypes); Xtensive.Tuples.Tuple t1 = Tuple.Create(descriptor); Xtensive.Tuples.Tuple t2 = t1.Clone(); PopulateData(fieldTypes, t2, t1); DifferentialTuple d = new DifferentialTuple(t1); AssertAreSame(t1, d); PopulateData(fieldTypes, t1, d); AssertAreSame(t1, d); DifferentialTuple c = (DifferentialTuple)d.Clone(); AssertAreSame(d, c); d.Reset(); AssertAreSame(t2, d); AssertAreSame(t1, c.ToRegular()); }
internal Tuple CreateTuple() { var descriptor = TypeReference.Type.Key.TupleDescriptor; return(Tuple.Create(descriptor)); }
public static Key Materialize(Domain domain, string nodeId, TypeInfo type, TypeReferenceAccuracy accuracy, params object[] values) { var keyInfo = type.Key; ArgumentValidator.EnsureArgumentIsInRange(values.Length, 1, keyInfo.TupleDescriptor.Count, "values"); var tuple = Tuple.Create(keyInfo.TupleDescriptor); int typeIdIndex = keyInfo.TypeIdColumnIndex; if (typeIdIndex >= 0) { tuple.SetValue(typeIdIndex, type.TypeId); } int tupleIndex = 0; if (tupleIndex == typeIdIndex) { tupleIndex++; } for (int valueIndex = 0; valueIndex < values.Length; valueIndex++) { var value = values[valueIndex]; ArgumentValidator.EnsureArgumentNotNull(value, String.Format("values[{0}]", valueIndex)); var entity = value as Entity; if (entity != null) { entity.EnsureNotRemoved(); value = entity.Key; } var key = value as Key; if (key != null) { if (key.TypeReference.Type.Hierarchy == type.Hierarchy) { typeIdIndex = -1; // Key must be fully copied in this case } for (int keyIndex = 0; keyIndex < key.Value.Count; keyIndex++) { tuple.SetValue(tupleIndex++, key.Value.GetValueOrDefault(keyIndex)); if (tupleIndex == typeIdIndex) { tupleIndex++; } } continue; } tuple.SetValue(tupleIndex++, value); if (tupleIndex == typeIdIndex) { tupleIndex++; } } if (tupleIndex != tuple.Count) { throw new ArgumentException(String.Format( Strings.ExSpecifiedValuesArentEnoughToCreateKeyForTypeX, type.Name)); } return(Materialize(domain, nodeId, type, tuple, accuracy, false, null)); }
private void BuildTuplePrototype() { // Building nullable map var nullabilityMap = new BitArray(TupleDescriptor.Count); int i = 0; foreach (var column in Columns) { nullabilityMap[i++] = column.IsNullable; } // fixing reference fields that are marked as not nullable foreach (var field in Fields.Where(f => f.IsEntity && !f.IsPrimaryKey && f.IsNullable == false)) { var segment = field.MappingInfo; for (int j = segment.Offset; j < segment.EndOffset; j++) { nullabilityMap[j] = true; } } // Building TuplePrototype var tuple = Tuple.Create(TupleDescriptor); tuple.Initialize(nullabilityMap); // Initializing defaults i = 0; foreach (var column in Columns) { if (column.DefaultValue != null) { try { tuple.SetValue(i, column.DefaultValue); } catch (Exception e) { OrmLog.Error(e, Strings.LogExErrorSettingDefaultValueXForColumnYInTypeZ, column.DefaultValue, column.Name, Name); } } i++; } // Aditional initialization for entities if (IsEntity) { // Setting type discriminator column if (Hierarchy.TypeDiscriminatorMap != null) { tuple.SetValue(Hierarchy.TypeDiscriminatorMap.Field.MappingInfo.Offset, typeDiscriminatorValue); } // Building primary key injector var fieldCount = TupleDescriptor.Count; var keyFieldCount = Key.TupleDescriptor.Count; var keyFieldMap = new Pair <int, int> [fieldCount]; for (i = 0; i < fieldCount; i++) { keyFieldMap[i] = new Pair <int, int>((i < keyFieldCount) ? 0 : 1, i); } primaryKeyInjector = new MapTransform(false, TupleDescriptor, keyFieldMap); } TuplePrototype = IsEntity ? tuple.ToFastReadOnly() : tuple; }
protected override Xtensive.Tuples.Tuple CreateTestTuple(TupleDescriptor descriptor) { return(new DifferentialTuple(Tuple.Create(descriptor))); }
public override Tuple GenerateKey(KeyInfo keyInfo, Session session) { return(Tuple.Create(counter++)); }
public override Tuple GenerateKey(KeyInfo keyInfo, Session session) { return(Tuple.Create(seed++, seed++)); }