/// <summary> /// Gets descriptor for a type id. /// </summary> /// <param name="userType">User type flag.</param> /// <param name="typeId">Type id.</param> /// <returns>Descriptor.</returns> public IBinaryTypeDescriptor GetDescriptor(bool userType, int typeId) { IBinaryTypeDescriptor desc; var typeKey = BinaryUtils.TypeKey(userType, typeId); if (_idToDesc.TryGetValue(typeKey, out desc)) { return(desc); } if (!userType) { return(null); } var meta = GetBinaryType(typeId); if (meta != BinaryType.Empty) { desc = new BinaryFullTypeDescriptor(null, meta.TypeId, meta.TypeName, true, null, null, null, false, meta.AffinityKeyFieldName, meta.IsEnum, null); _idToDesc.GetOrAdd(typeKey, _ => desc); return(desc); } return(new BinarySurrogateTypeDescriptor(_cfg, typeId)); }
/// <summary> /// Registers the type. /// </summary> /// <param name="type">The type.</param> /// <param name="desc">Existing descriptor.</param> private BinaryFullTypeDescriptor RegisterType(Type type, BinaryFullTypeDescriptor desc) { Debug.Assert(type != null); var typeName = GetTypeName(type); var typeId = GetTypeId(typeName, _cfg.IdMapper); var registered = _ignite != null && _ignite.BinaryProcessor.RegisterType(typeId, typeName, RegisterSameJavaType); return(AddUserType(type, typeId, typeName, registered, desc)); }
/// <summary> /// Registers the type. /// </summary> /// <param name="type">The type.</param> /// <param name="desc">Existing descriptor.</param> private BinaryFullTypeDescriptor RegisterType(Type type, BinaryFullTypeDescriptor desc) { Debug.Assert(type != null); var typeName = BinaryUtils.GetTypeName(type); var typeId = BinaryUtils.TypeId(typeName, _cfg.DefaultNameMapper, _cfg.DefaultIdMapper); var registered = _ignite != null && _ignite.BinaryProcessor.RegisterType(typeId, type); return(AddUserType(type, typeId, typeName, registered, desc)); }
/// <summary> /// Gets descriptor for a type id. /// </summary> /// <param name="userType">User type flag.</param> /// <param name="typeId">Type id.</param> /// <param name="requiresType"> /// If set to true, resulting descriptor must have Type property populated. /// <para /> /// When working in binary mode, we don't need Type. And there is no Type at all in some cases. /// So we should not attempt to call BinaryProcessor right away. /// Only when we really deserialize the value, requiresType is set to true /// and we attempt to resolve the type by all means. /// </param> /// <returns> /// Descriptor. /// </returns> public IBinaryTypeDescriptor GetDescriptor(bool userType, int typeId, bool requiresType = false) { BinaryFullTypeDescriptor desc; var typeKey = BinaryUtils.TypeKey(userType, typeId); if (_idToDesc.TryGetValue(typeKey, out desc) && (!requiresType || desc.Type != null)) { return(desc); } if (!userType) { return(null); } if (requiresType && _ignite != null) { // Check marshaller context for dynamically registered type. var typeName = _ignite.BinaryProcessor.GetTypeName(typeId); if (typeName != null) { var type = new TypeResolver().ResolveType(typeName, nameMapper: _cfg.NameMapper ?? GetDefaultNameMapper()); if (type != null) { return(AddUserType(type, typeId, GetTypeName(type), true, desc)); } } } var meta = GetBinaryType(typeId); if (meta != BinaryType.Empty) { desc = new BinaryFullTypeDescriptor(null, meta.TypeId, meta.TypeName, true, null, null, null, false, meta.AffinityKeyFieldName, meta.IsEnum); _idToDesc.GetOrAdd(typeKey, _ => desc); return(desc); } return(new BinarySurrogateTypeDescriptor(_cfg, typeId, null)); }
/// <summary> /// Initializes a new instance of the <see cref="BinaryFullTypeDescriptor"/> class, /// copying values from specified descriptor. /// </summary> /// <param name="desc">The descriptor to copy from.</param> /// <param name="type">Type.</param> /// <param name="serializer">Serializer.</param> /// <param name="isRegistered">Registered flag.</param> public BinaryFullTypeDescriptor(BinaryFullTypeDescriptor desc, Type type, IBinarySerializerInternal serializer, bool isRegistered) { _type = type; _typeId = desc._typeId; _typeName = desc._typeName; _userType = desc._userType; _nameMapper = desc._nameMapper; _idMapper = desc._idMapper; _serializer = serializer; _keepDeserialized = desc._keepDeserialized; _affKeyFieldName = desc._affKeyFieldName; _isEnum = desc._isEnum; _isRegistered = isRegistered; _schema = desc._schema; _writerTypeStruct = desc._writerTypeStruct; _readerTypeStructure = desc._readerTypeStructure; }
/// <summary> /// Add type. /// </summary> /// <param name="type">Type.</param> /// <param name="typeId">Type ID.</param> /// <param name="typeName">Type name.</param> /// <param name="userType">User type flag.</param> /// <param name="keepDeserialized">Whether to cache deserialized value in IBinaryObject</param> /// <param name="nameMapper">Name mapper.</param> /// <param name="idMapper">ID mapper.</param> /// <param name="serializer">Serializer.</param> /// <param name="affKeyFieldName">Affinity key field name.</param> /// <param name="isEnum">Enum flag.</param> /// <param name="comparer">Comparer.</param> private void AddType(Type type, int typeId, string typeName, bool userType, bool keepDeserialized, IBinaryNameMapper nameMapper, IBinaryIdMapper idMapper, IBinarySerializerInternal serializer, string affKeyFieldName, bool isEnum, IEqualityComparer <IBinaryObject> comparer) { long typeKey = BinaryUtils.TypeKey(userType, typeId); IBinaryTypeDescriptor conflictingType; if (_idToDesc.TryGetValue(typeKey, out conflictingType)) { var type1 = conflictingType.Type != null ? conflictingType.Type.AssemblyQualifiedName : conflictingType.TypeName; var type2 = type != null ? type.AssemblyQualifiedName : typeName; throw new BinaryObjectException(string.Format("Conflicting type IDs [type1='{0}', " + "type2='{1}', typeId={2}]", type1, type2, typeId)); } if (userType && _typeNameToDesc.ContainsKey(typeName)) { throw new BinaryObjectException("Conflicting type name: " + typeName); } var descriptor = new BinaryFullTypeDescriptor(type, typeId, typeName, userType, nameMapper, idMapper, serializer, keepDeserialized, affKeyFieldName, isEnum, comparer); if (type != null) { _typeToDesc[type] = descriptor; } if (userType) { _typeNameToDesc[typeName] = descriptor; } _idToDesc.GetOrAdd(typeKey, _ => descriptor); }
/// <summary> /// Add type. /// </summary> /// <param name="type">Type.</param> /// <param name="typeId">Type ID.</param> /// <param name="typeName">Type name.</param> /// <param name="userType">User type flag.</param> /// <param name="keepDeserialized">Whether to cache deserialized value in IBinaryObject</param> /// <param name="nameMapper">Name mapper.</param> /// <param name="idMapper">ID mapper.</param> /// <param name="serializer">Serializer.</param> /// <param name="affKeyFieldName">Affinity key field name.</param> /// <param name="isEnum">Enum flag.</param> private BinaryFullTypeDescriptor AddType(Type type, int typeId, string typeName, bool userType, bool keepDeserialized, IBinaryNameMapper nameMapper, IBinaryIdMapper idMapper, IBinarySerializerInternal serializer, string affKeyFieldName, bool isEnum) { long typeKey = BinaryUtils.TypeKey(userType, typeId); BinaryFullTypeDescriptor conflictingType; if (_idToDesc.TryGetValue(typeKey, out conflictingType)) { var type1 = conflictingType.Type != null ? conflictingType.Type.AssemblyQualifiedName : conflictingType.TypeName; var type2 = type != null ? type.AssemblyQualifiedName : typeName; ThrowConflictingTypeError(type1, type2, typeId); } if (userType && _typeNameToDesc.ContainsKey(typeName)) { throw new BinaryObjectException("Conflicting type name: " + typeName); } var descriptor = new BinaryFullTypeDescriptor(type, typeId, typeName, userType, nameMapper, idMapper, serializer, keepDeserialized, affKeyFieldName, isEnum); if (type != null) { _typeToDesc.GetOrAdd(type, x => descriptor); } if (userType) { _typeNameToDesc.GetOrAdd(typeName, x => descriptor); } _idToDesc.GetOrAdd(typeKey, _ => descriptor); return(descriptor); }
/// <summary> /// Add user type. /// </summary> /// <param name="type">The type.</param> /// <param name="typeId">The type id.</param> /// <param name="typeName">Name of the type.</param> /// <param name="registered">Registered flag.</param> /// <param name="desc">Existing descriptor.</param> /// <returns>Descriptor.</returns> private BinaryFullTypeDescriptor AddUserType(Type type, int typeId, string typeName, bool registered, BinaryFullTypeDescriptor desc) { Debug.Assert(type != null); Debug.Assert(typeName != null); var ser = GetSerializer(_cfg, null, type, typeId, null, null, _log); desc = desc == null ? new BinaryFullTypeDescriptor(type, typeId, typeName, true, _cfg.NameMapper, _cfg.IdMapper, ser, false, AffinityKeyMappedAttribute.GetFieldNameFromAttribute(type), BinaryUtils.IsIgniteEnum(type), registered) : new BinaryFullTypeDescriptor(desc, type, ser, registered); if (RegistrationDisabled) { return(desc); } var typeKey = BinaryUtils.TypeKey(true, typeId); var desc0 = _idToDesc.GetOrAdd(typeKey, x => desc); if (desc0.Type != null && desc0.TypeName != typeName) { ThrowConflictingTypeError(type, desc0.Type, typeId); } desc0 = _typeNameToDesc.GetOrAdd(typeName, x => desc); if (desc0.Type != null && desc0.TypeName != typeName) { ThrowConflictingTypeError(type, desc0.Type, typeId); } ValidateRegistration(type); _typeToDesc.Set(type, desc); return(desc); }
/// <summary> /// Add type. /// </summary> /// <param name="type">Type.</param> /// <param name="typeId">Type ID.</param> /// <param name="typeName">Type name.</param> /// <param name="userType">User type flag.</param> /// <param name="keepDeserialized">Whether to cache deserialized value in IBinaryObject</param> /// <param name="nameMapper">Name mapper.</param> /// <param name="idMapper">ID mapper.</param> /// <param name="serializer">Serializer.</param> /// <param name="affKeyFieldName">Affinity key field name.</param> /// <param name="isEnum">Enum flag.</param> private BinaryFullTypeDescriptor AddType(Type type, int typeId, string typeName, bool userType, bool keepDeserialized, IBinaryNameMapper nameMapper, IBinaryIdMapper idMapper, IBinarySerializerInternal serializer, string affKeyFieldName, bool isEnum) { Debug.Assert(!string.IsNullOrEmpty(typeName)); long typeKey = BinaryUtils.TypeKey(userType, typeId); BinaryFullTypeDescriptor conflictingType; if (_idToDesc.TryGetValue(typeKey, out conflictingType) && conflictingType.TypeName != typeName) { ThrowConflictingTypeError(typeName, conflictingType.TypeName, typeId); } var descriptor = new BinaryFullTypeDescriptor(type, typeId, typeName, userType, nameMapper, idMapper, serializer, keepDeserialized, affKeyFieldName, isEnum); if (RegistrationDisabled) { return(descriptor); } if (type != null) { ValidateRegistration(type); _typeToDesc.Set(type, descriptor); } if (userType) { _typeNameToDesc.Set(typeName, descriptor); } _idToDesc.Set(typeKey, descriptor); return(descriptor); }
/// <summary> /// Add user type. /// </summary> /// <param name="type">The type.</param> /// <param name="typeId">The type id.</param> /// <param name="typeName">Name of the type.</param> /// <param name="registered">Registered flag.</param> /// <param name="desc">Existing descriptor.</param> /// <returns>Descriptor.</returns> private BinaryFullTypeDescriptor AddUserType(Type type, int typeId, string typeName, bool registered, BinaryFullTypeDescriptor desc) { Debug.Assert(type != null); Debug.Assert(typeName != null); var ser = GetSerializer(_cfg, null, type, typeId, null, null, _log); desc = desc == null ? new BinaryFullTypeDescriptor(type, typeId, typeName, true, _cfg.DefaultNameMapper, _cfg.DefaultIdMapper, ser, false, null, type.IsEnum, null, registered) : new BinaryFullTypeDescriptor(desc, type, ser, registered); if (RegistrationDisabled) { return(desc); } var typeKey = BinaryUtils.TypeKey(true, typeId); var desc0 = _idToDesc.GetOrAdd(typeKey, x => desc); if (desc0.Type != null && desc0.Type.FullName != type.FullName) { ThrowConflictingTypeError(type, desc0.Type, typeId); } desc0 = _typeNameToDesc.GetOrAdd(typeName, x => desc); if (desc0.Type != null && desc0.Type.FullName != type.FullName) { ThrowConflictingTypeError(type, desc0.Type, typeId); } _typeToDesc.Set(type, desc); return(desc); }