Esempio n. 1
0
        /** <inheritDoc /> */
        public IPortableBuilder Builder(Type type)
        {
            IgniteArgumentCheck.NotNull(type, "type");

            IPortableTypeDescriptor desc = _marsh.Descriptor(type);

            if (desc == null)
            {
                throw new IgniteException("Type is not portable (add it to PortableConfiguration): " +
                                          type.FullName);
            }

            return(Builder0(null, PortableFromDescriptor(desc), desc));
        }
Esempio n. 2
0
        /// <summary>
        /// Internal deserialization routine.
        /// </summary>
        /// <param name="mode">The mode.</param>
        /// <returns>
        /// Deserialized object.
        /// </returns>
        private T Deserialize <T>(PortableMode mode)
        {
            if (_deserialized == null)
            {
                IPortableStream stream = new PortableHeapStream(_data);

                stream.Seek(_offset, SeekOrigin.Begin);

                T res = _marsh.Unmarshal <T>(stream, mode);

                IPortableTypeDescriptor desc = _marsh.Descriptor(true, _typeId);

                if (!desc.KeepDeserialized)
                {
                    return(res);
                }

                _deserialized = res;
            }

            return((T)_deserialized);
        }
Esempio n. 3
0
        internal void Write <T>(T obj, object handler)
        {
            // Apply detach mode if needed.
            PortableHandleDictionary <object, long> oldHnds = null;

            bool resetDetach = false;

            if (_detach)
            {
                _detach     = false;
                _detachMode = true;
                resetDetach = true;

                oldHnds = _hnds;

                _hnds = null;
            }

            try
            {
                // Write null.
                if (obj == null)
                {
                    _stream.WriteByte(PortableUtils.HdrNull);

                    return;
                }

                if (_builder != null)
                {
                    // Special case for portable object during build.
                    PortableUserObject portObj = obj as PortableUserObject;

                    if (portObj != null)
                    {
                        if (!WriteHandle(_stream.Position, portObj))
                        {
                            _builder.ProcessPortable(_stream, portObj);
                        }

                        return;
                    }

                    // Special case for builder during build.
                    PortableBuilderImpl portBuilder = obj as PortableBuilderImpl;

                    if (portBuilder != null)
                    {
                        if (!WriteHandle(_stream.Position, portBuilder))
                        {
                            _builder.ProcessBuilder(_stream, portBuilder);
                        }

                        return;
                    }
                }

                // Try writting as well-known type.
                if (InvokeHandler(handler, handler as PortableSystemWriteDelegate, obj))
                {
                    return;
                }

                Type type = obj.GetType();

                IPortableTypeDescriptor desc = _marsh.Descriptor(type);

                object typedHandler;
                PortableSystemWriteDelegate untypedHandler;

                if (desc == null)
                {
                    typedHandler   = null;
                    untypedHandler = PortableSystemHandlers.WriteHandler(type);
                }
                else
                {
                    typedHandler   = desc.TypedHandler;
                    untypedHandler = desc.UntypedHandler;
                }

                if (InvokeHandler(typedHandler, untypedHandler, obj))
                {
                    return;
                }

                if (desc == null)
                {
                    if (!type.IsSerializable)
                    {
                        // If neither handler, nor descriptor exist, and not serializable, this is an exception.
                        throw new PortableException("Unsupported object type [type=" + type +
                                                    ", object=" + obj + ']');
                    }

                    Write(new SerializableObjectHolder(obj));

                    return;
                }

                int pos = _stream.Position;

                // Dealing with handles.
                if (!(desc.Serializer is IPortableSystemTypeSerializer) && WriteHandle(pos, obj))
                {
                    return;
                }

                // Write header.
                _stream.WriteByte(PortableUtils.HdrFull);

                _stream.WriteBool(desc.UserType);
                _stream.WriteInt(desc.TypeId);
                _stream.WriteInt(obj.GetHashCode());

                // Skip length as it is not known in the first place.
                _stream.Seek(8, SeekOrigin.Current);

                // Preserve old frame.
                int oldTypeId = _curTypeId;
                IPortableNameMapper      oldConverter = _curConverter;
                IPortableIdMapper        oldMapper    = _curMapper;
                IPortableMetadataHandler oldMetaHnd   = _curMetaHnd;
                bool oldRaw    = _curRaw;
                long oldRawPos = _curRawPos;

                // Push new frame.
                _curTypeId    = desc.TypeId;
                _curConverter = desc.NameConverter;
                _curMapper    = desc.Mapper;
                _curMetaHnd   = desc.MetadataEnabled ? _marsh.MetadataHandler(desc) : null;
                _curRaw       = false;
                _curRawPos    = 0;

                // Write object fields.
                desc.Serializer.WritePortable(obj, this);

                // Calculate and write length.
                int retPos = _stream.Position;

                _stream.Seek(pos + 10, SeekOrigin.Begin);

                int len = retPos - pos;

                _stream.WriteInt(len);

                if (_curRawPos != 0)
                {
                    // When set, it is difference between object head and raw position.
                    _stream.WriteInt((int)(_curRawPos - pos));
                }
                else
                {
                    // When no set, it is equal to object length.
                    _stream.WriteInt(len);
                }

                _stream.Seek(retPos, SeekOrigin.Begin);

                // 13. Collect metadata.
                if (_curMetaHnd != null)
                {
                    IDictionary <string, int> meta = _curMetaHnd.OnObjectWriteFinished();

                    if (meta != null)
                    {
                        SaveMetadata(_curTypeId, desc.TypeName, desc.AffinityKeyFieldName, meta);
                    }
                }

                // Restore old frame.
                _curTypeId    = oldTypeId;
                _curConverter = oldConverter;
                _curMapper    = oldMapper;
                _curMetaHnd   = oldMetaHnd;
                _curRaw       = oldRaw;
                _curRawPos    = oldRawPos;
            }
            finally
            {
                // Restore handles if needed.
                if (resetDetach)
                {
                    // Add newly recorded handles without overriding already existing ones.
                    if (_hnds != null)
                    {
                        if (oldHnds == null)
                        {
                            oldHnds = _hnds;
                        }
                        else
                        {
                            oldHnds.Merge(_hnds);
                        }
                    }

                    _hnds = oldHnds;

                    _detachMode = false;
                }
            }
        }