BTDB.EventStoreLayer.TypeSerializers.MergeDescriptor C# (CSharp) Method

MergeDescriptor() public method

public MergeDescriptor ( ITypeDescriptor descriptor ) : ITypeDescriptor
descriptor ITypeDescriptor
return ITypeDescriptor
        public ITypeDescriptor MergeDescriptor(ITypeDescriptor descriptor)
        {
            foreach (var existingTypeDescriptor in _descriptorSet)
            {
                if (descriptor.Equals(existingTypeDescriptor.Key))
                {
                    return existingTypeDescriptor.Key;
                }
            }
            _descriptorSet.GetOrAdd(descriptor, true);
            return descriptor;
        }

Usage Example

Example #1
0
        public void LoadTypeDescriptors(AbstractBufferedReader reader)
        {
            var typeId      = reader.ReadVUInt32();
            var firstTypeId = typeId;

            while (typeId != 0)
            {
                if (typeId < firstTypeId)
                {
                    firstTypeId = typeId;
                }
                var             typeCategory = (TypeCategory)reader.ReadUInt8();
                ITypeDescriptor descriptor;
                switch (typeCategory)
                {
                case TypeCategory.BuildIn:
                    throw new ArgumentOutOfRangeException();

                case TypeCategory.Class:
                    descriptor = new ObjectTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.List:
                    descriptor = new ListTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Dictionary:
                    descriptor = new DictionaryTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                case TypeCategory.Enum:
                    descriptor = new EnumTypeDescriptor(_typeSerializers, reader);
                    break;

                case TypeCategory.Nullable:
                    descriptor = new NullableTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
                while (typeId >= _id2DescriptorMap.Count)
                {
                    _id2DescriptorMap.Add(null);
                }
                if (_id2DescriptorMap[(int)typeId] == null)
                {
                    _id2DescriptorMap[(int)typeId] = new InfoForType {
                        Id = (int)typeId, Descriptor = descriptor
                    }
                }
                ;
                typeId = reader.ReadVUInt32();
            }
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d =>
                {
                    var placeHolderDescriptor = d as PlaceHolderDescriptor;
                    return(placeHolderDescriptor != null ? _id2DescriptorMap[(int)placeHolderDescriptor.TypeId].Descriptor : d);
                });
            }
            // This additional cycle is needed to fill names of recursive structures
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d => d);
            }
            for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++)
            {
                var infoForType = _id2DescriptorMap[(int)i];
                var descriptor  = _typeSerializers.MergeDescriptor(infoForType.Descriptor);
                infoForType.Descriptor             = descriptor;
                _typeOrDescriptor2Info[descriptor] = infoForType;
            }
        }

        ITypeDescriptor NestedDescriptorReader(AbstractBufferedReader reader)
        {
            var typeId = reader.ReadVUInt32();

            if (typeId < _id2DescriptorMap.Count)
            {
                var infoForType = _id2DescriptorMap[(int)typeId];
                if (infoForType != null)
                {
                    return(infoForType.Descriptor);
                }
            }
            return(new PlaceHolderDescriptor(typeId));
        }