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)); }
public static int ExtractTypeId(TypeInfo type, TypeIdRegistry typeIdRegistry, Tuple tuple, int typeIdIndex, out TypeReferenceAccuracy accuracy) { accuracy = TypeReferenceAccuracy.Hierarchy; if (type.IsInterface) { accuracy = TypeReferenceAccuracy.BaseType; } if (typeIdIndex < 0) { if (type.IsLeaf) { accuracy = TypeReferenceAccuracy.ExactType; } return(typeIdRegistry.GetTypeId(type)); } accuracy = TypeReferenceAccuracy.ExactType; TupleFieldState state; var value = tuple.GetValue <int>(typeIdIndex, out state); if (type.IsLeaf) { return(state.HasValue() ? typeIdRegistry.GetTypeId(type) : TypeInfo.NoTypeId); } // Hack here: 0 (default) = TypeInfo.NoTypeId return(value); }
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 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); }
// Constructors /// <summary> /// Initializes new instance of this type. /// </summary> /// <param name="source">The tuple to create the fast read-only tuple from.</param> public FastReadOnlyTuple(Tuple source) { descriptor = source.Descriptor; int count = descriptor.Count; values = new object[count]; states = new TupleFieldState[count]; for (int i = 0; i < count; i++) { TupleFieldState state; values[i] = source.GetValue(i, out state); states[i] = state; } }
/// <inheritdoc/> public override object GetValue(int fieldIndex, out TupleFieldState fieldState) { return(InnerTuple.GetValue(fieldIndex, out fieldState)); }