Beispiel #1
0
        private static void TestTuple(Xtensive.Tuples.Tuple tuple)
        {
            Assert.IsFalse(tuple.GetFieldState(0).IsAvailable());

            try {
                tuple.GetFieldState(0).IsNull();
                throw new AssertionException("Value is not available. No one knows if it is null or not.");
            } catch (InvalidOperationException) {
            }

            tuple.SetValue(0, 1);
            Assert.IsTrue(tuple.GetFieldState(0).IsAvailable());
            Assert.IsFalse(tuple.GetFieldState(0).IsNull());
            Assert.IsTrue(tuple.GetFieldState(0).HasValue());
            Assert.AreEqual(1, tuple.GetValue(0));
            Assert.AreEqual(1, tuple.GetValue <int>(0));
            Assert.AreEqual(new int?(1), tuple.GetValue <int?>(0));

            tuple.SetValue(0, null);
            Assert.IsTrue(tuple.GetFieldState(0).IsAvailable());
            Assert.IsTrue(tuple.GetFieldState(0).IsNull());
            Assert.IsFalse(tuple.GetFieldState(0).HasValue());
            Assert.AreEqual(null, tuple.GetValue(0));
            Assert.AreEqual(null, tuple.GetValue <int?>(0));

            tuple.SetValue <int?>(0, null);
            Assert.IsTrue(tuple.GetFieldState(0).IsAvailable());
            Assert.IsTrue(tuple.GetFieldState(0).IsNull());
            Assert.IsFalse(tuple.GetFieldState(0).HasValue());
            Assert.AreEqual(null, tuple.GetValue(0));
            Assert.AreEqual(null, tuple.GetValue <int?>(0));
            Assert.AreEqual(null, tuple.GetValueOrDefault(0));
            Assert.AreEqual(null, tuple.GetValueOrDefault <int?>(0));

            try {
                tuple.GetValue(1);
                throw new AssertionException("Value should not be available.");
            } catch (InvalidOperationException) {
            }

            try {
                tuple.GetValue <string>(2);
                throw new AssertionException("Value should not be available.");
            } catch (InvalidOperationException) {
            }

            try {
                tuple.GetValue <byte>(0);
                throw new AssertionException("Null reference or Invalid cast exception should be thrown.");
            }
            catch (NullReferenceException) {}
            catch (InvalidCastException) {}

            Assert.IsTrue(tuple.Equals(tuple));
        }
Beispiel #2
0
        private static void InternalSetValue <T>(Xtensive.Tuples.Tuple tuple, int fieldIndex, Random random)
        {
            IInstanceGenerator <T> generator = InstanceGeneratorProvider.Default.GetInstanceGenerator <T>();
            T instance = generator.GetInstance(random);

            tuple.SetValue(fieldIndex, instance);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes the specified <see cref="Tuple"/> with default values.
        /// </summary>
        /// <param name="target">Tuple to initialize.</param>
        /// <param name="nullableMap"><see cref="BitArray"/> instance that flags that field should have null value.</param>
        /// <exception cref="ArgumentException">Tuple descriptor field count is not equal to <paramref name="nullableMap"/> count.</exception>
        public static void Initialize(this Tuple target, BitArray nullableMap)
        {
            var descriptor = target.Descriptor;

            if (descriptor.Count != nullableMap.Count)
            {
                throw new ArgumentException(String.Format(Strings.ExInvalidFieldMapSizeExpectedX, descriptor.Count));
            }

            for (int i = 0; i < target.Count; i++)
            {
                if (nullableMap[i])
                {
                    target.SetFieldState(i, TupleFieldState.Available | TupleFieldState.Null);
                }
                else
                {
                    var fieldType = descriptor[i];
                    if (fieldType.IsValueType)
                    {
                        target.SetValue(i, Activator.CreateInstance(fieldType));
                    }
                    else
                    {
                        target.SetFieldState(i, TupleFieldState.Available);
                    }
                }
            }
        }
Beispiel #4
0
 /// <inheritdoc />
 public override void SetValue(int fieldIndex, object fieldValue)
 {
     if (difference == null)
     {
         difference = origin.CreateNew();
     }
     difference.SetValue(fieldIndex, fieldValue);
 }
        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 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);
                }
        }
 private void FillLocalCollectionField(object item, Tuple tuple, Expression expression)
 {
     if (item == null)
     {
         return;
     }
     // LocalCollectionExpression
     if (expression is LocalCollectionExpression)
     {
         var itemExpression = (LocalCollectionExpression)expression;
         foreach (var field in itemExpression.Fields)
         {
             var    propertyInfo = field.Key as PropertyInfo;
             object value        = propertyInfo == null
     ? ((FieldInfo)field.Key).GetValue(item)
     : propertyInfo.GetValue(item, BindingFlags.InvokeMethod, null, null, null);
             if (value != null)
             {
                 FillLocalCollectionField(value, tuple, (Expression)field.Value);
             }
         }
     }
     else if (expression is ColumnExpression)
     {
         var columnExpression = (ColumnExpression)expression;
         tuple.SetValue(columnExpression.Mapping.Offset, item);
     }
     else if (expression is StructureExpression)
     {
         var structureExpression = (StructureExpression)expression;
         var structure           = (Structure)item;
         var typeInfo            = structureExpression.PersistentType;
         var tupleDescriptor     = typeInfo.TupleDescriptor;
         var tupleSegment        = new Segment <int>(0, tupleDescriptor.Count);
         var structureTuple      = structure.Tuple.GetSegment(tupleSegment);
         structureTuple.CopyTo(tuple, 0, structureExpression.Mapping.Offset, structureTuple.Count);
     }
     else if (expression is EntityExpression)
     {
         var entityExpression = (EntityExpression)expression;
         var entity           = (Entity)item;
         var keyTuple         = entity.Key.Value;
         keyTuple.CopyTo(tuple, 0, entityExpression.Key.Mapping.Offset, keyTuple.Count);
     }
     else if (expression is KeyExpression)
     {
         var keyExpression = (KeyExpression)expression;
         var key           = (Key)item;
         var keyTuple      = key.Value;
         keyTuple.CopyTo(tuple, 0, keyExpression.Mapping.Offset, keyTuple.Count);
     }
     else
     {
         throw new NotSupportedException();
     }
 }
Beispiel #8
0
        private static void CopyValue(Tuple source, int sourceIndex, Tuple target, int targetIndex)
        {
            TupleFieldState fieldState;
            var             value = source.GetValue(sourceIndex, out fieldState);

            if (!fieldState.IsAvailable())
            {
                return;
            }
            target.SetValue(targetIndex, fieldState.IsAvailableAndNull() ? null : value);
        }
        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());
        }
        protected internal void SetReferenceKey(FieldInfo field, Key value)
        {
            Key oldValue = null;

            try {
                oldValue = GetReferenceKey(field);
                if (field.ReflectedType.IsInterface)
                {
                    field = TypeInfo.FieldMap[field];
                }
                SystemBeforeSetValue(field, value);
                if (!field.IsEntity)
                {
                    throw new InvalidOperationException(
                              String.Format(Strings.ExFieldIsNotAnEntityField, field.Name, field.ReflectedType.Name));
                }

                var types = Session.Domain.Model.Types;
                if (value == null)
                {
                    for (int i = 0; i < field.MappingInfo.Length; i++)
                    {
                        Tuple.SetValue(field.MappingInfo.Offset + i, null);
                    }
                    return;
                }
                if (!field.ValueType.IsAssignableFrom(value.TypeInfo.UnderlyingType))
                {
                    throw new InvalidOperationException(string.Format("Key of {0} type is not assignable to field of {1} type", value.TypeInfo.Name, field.ValueType.Name));
                }

                value.Value.CopyTo(Tuple, 0, field.MappingInfo.Offset, field.MappingInfo.Length);
                if (field.IsPrimaryKey)
                {
                    value.Value.CopyTo(((Entity)this).Key.Value, 0, field.MappingInfo.Offset, field.MappingInfo.Length);
                }
                SystemSetValue(field, oldValue, value);
                SystemSetValueCompleted(field, oldValue, value, null);
            }
            catch (Exception e) {
                SystemSetValueCompleted(field, oldValue, value, e);
                throw;
            }
        }
Beispiel #11
0
        protected internal void SetReferenceKey(FieldInfo field, Key value)
        {
            if (Session.StorageNodeId != value.NodeId)
            {
                throw new ArgumentException(Strings.ExKeyBelongsToDifferentStorageNode, "value");
            }

            // KeyRemapper also uses this method during persist so we need to detect whether it is KeyRemapper
            var isPersisting = Session.IsPersisting;
            Key oldValue     = null;

            try {
                oldValue = GetReferenceKey(field);
                if (field.ReflectedType.IsInterface)
                {
                    field = TypeInfo.FieldMap[field];
                }

                SystemBeforeSetValue(field, value);
                if (!field.IsEntity)
                {
                    throw new InvalidOperationException(
                              string.Format(Strings.ExFieldIsNotAnEntityField, field.Name, field.ReflectedType.Name));
                }

                if (!isPersisting)
                {
                    SystemBeforeTupleChange();
                }

                var types = Session.Domain.Model.Types;
                if (value == null)
                {
                    for (var i = 0; i < field.MappingInfo.Length; i++)
                    {
                        Tuple.SetValue(field.MappingInfo.Offset + i, null);
                    }

                    if (!isPersisting)
                    {
                        SystemTupleChange();
                    }
                    return;
                }
                if (!field.ValueType.IsAssignableFrom(value.TypeInfo.UnderlyingType))
                {
                    throw new InvalidOperationException(string.Format("Key of {0} type is not assignable to field of {1} type", value.TypeInfo.Name, field.ValueType.Name));
                }

                if (value == oldValue && !isPersisting)
                {
                    return;
                }

                value.Value.CopyTo(Tuple, 0, field.MappingInfo.Offset, field.MappingInfo.Length);
                if (field.IsPrimaryKey)
                {
                    value.Value.CopyTo(((Entity)this).Key.Value, 0, field.MappingInfo.Offset, field.MappingInfo.Length);
                }

                if (!isPersisting)
                {
                    SystemTupleChange();
                }

                SystemSetValue(field, oldValue, value);
                SystemSetValueCompleted(field, oldValue, value, null);
            }
            catch (Exception e) {
                SystemSetValueCompleted(field, oldValue, value, e);
                throw;
            }
        }
Beispiel #12
0
 /// <inheritdoc/>
 public override void SetValue(int fieldIndex, object fieldValue)
 {
     InnerTuple.SetValue(fieldIndex, fieldValue);
 }
Beispiel #13
0
        public static Key Materialize(Domain domain, string nodeId,
                                      TypeInfo type, Tuple value, TypeReferenceAccuracy accuracy, bool canCache, int[] keyIndexes)
        {
            var hierarchy = type.Hierarchy;
            var keyInfo   = type.Key;

            if (keyIndexes == null)
            {
                if (value.Descriptor != keyInfo.TupleDescriptor)
                {
                    throw new ArgumentException(Strings.ExWrongKeyStructure);
                }
                if (accuracy == TypeReferenceAccuracy.ExactType)
                {
                    int typeIdColumnIndex = keyInfo.TypeIdColumnIndex;
                    if (typeIdColumnIndex >= 0 && !value.GetFieldState(typeIdColumnIndex).IsAvailable())
                    {
                        // Ensures TypeId is filled in into Keys of newly created Entities
                        value.SetValue(typeIdColumnIndex, type.TypeId);
                    }
                }
            }
            if (hierarchy != null && hierarchy.Root.IsLeaf)
            {
                accuracy = TypeReferenceAccuracy.ExactType;
                canCache = false; // No reason to cache
            }

            Key key;
            var isGenericKey = keyInfo.TupleDescriptor.Count <= WellKnown.MaxGenericKeyLength;

            if (isGenericKey)
            {
                key = CreateGenericKey(domain, nodeId, type, accuracy, value, keyIndexes);
            }
            else
            {
                if (keyIndexes != null)
                {
                    throw Exceptions.InternalError(Strings.ExKeyIndexesAreSpecifiedForNonGenericKey, OrmLog.Instance);
                }
                key = new LongKey(nodeId, type, accuracy, value);
            }
            if (!canCache)
            {
                return(key);
            }
            var keyCache = domain.KeyCache;

            lock (keyCache) {
                Key foundKey;
                if (keyCache.TryGetItem(key, true, out foundKey))
                {
                    key = foundKey;
                }
                else
                {
                    if (accuracy == TypeReferenceAccuracy.ExactType)
                    {
                        keyCache.Add(key);
                    }
                }
            }
            return(key);
        }
 /// <summary>
 /// Updates the field of <see cref="Tuple"/> with the specified index.
 /// </summary>
 /// <param name="fieldIndex">The field index.</param>
 /// <param name="value">The new field value</param>
 /// <returns><see langword="this" /></returns>
 public TupleUpdater SetValue(int fieldIndex, object value)
 {
     tuple.SetValue(fieldIndex, value);
     return(this);
 }