Ejemplo n.º 1
0
        /// <summary>
        /// Try writing object as special builder type.
        /// </summary>
        /// <param name="obj">Object.</param>
        /// <returns>True if object was written, false otherwise.</returns>
        private bool WriteBuilderSpecials <T>(T obj)
        {
            if (_builder != null)
            {
                // Special case for binary object during build.
                BinaryObject portObj = obj as BinaryObject;

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

                    return(true);
                }

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

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

                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Set new builder.
        /// </summary>
        /// <param name="builder">Builder.</param>
        /// <returns>Previous builder.</returns>
        internal BinaryObjectBuilder SetBuilder(BinaryObjectBuilder builder)
        {
            BinaryObjectBuilder ret = _builder;

            _builder = builder;

            return(ret);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets field value on the given object.
        /// </summary>
        /// <param name="pos">Position.</param>
        /// <param name="builder">Builder.</param>
        /// <returns>Field value.</returns>
        public T GetField <T>(int pos, BinaryObjectBuilder builder)
        {
            IBinaryStream stream = new BinaryHeapStream(_data);

            stream.Seek(pos + _offset, SeekOrigin.Begin);

            return(_marsh.Unmarshal <T>(stream, BinaryMode.ForceBinary, builder));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="binary">Binary.</param>
        /// <param name="parent">Parent builder.</param>
        /// <param name="obj">Initial binary object.</param>
        /// <param name="desc">Type descriptor.</param>
        public BinaryObjectBuilder(Binary binary, BinaryObjectBuilder parent,
                                   BinaryObject obj, IBinaryTypeDescriptor desc)
        {
            Debug.Assert(binary != null);
            Debug.Assert(desc != null);

            _binary = binary;
            _parent = parent ?? this;
            _desc   = desc;

            _obj = obj ?? BinaryFromDescriptor(desc);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="marsh">Marshaller.</param>
        /// <param name="stream">Input stream.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="builder">Builder.</param>
        public BinaryReader
            (Marshaller marsh,
            IBinaryStream stream,
            BinaryMode mode,
            BinaryObjectBuilder builder)
        {
            _marsh   = marsh;
            _mode    = mode;
            _builder = builder;

            Stream = stream;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="marsh">Marshaller.</param>
        /// <param name="descs">Descriptors.</param>
        /// <param name="stream">Input stream.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="builder">Builder.</param>
        public BinaryReader
            (Marshaller marsh,
            IDictionary <long, IBinaryTypeDescriptor> descs,
            IBinaryStream stream,
            BinaryMode mode,
            BinaryObjectBuilder builder)
        {
            _marsh   = marsh;
            _descs   = descs;
            _mode    = mode;
            _builder = builder;

            Stream = stream;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="igniteBinary">Binary.</param>
        /// <param name="parent">Parent builder.</param>
        /// <param name="obj">Initial binary object.</param>
        /// <param name="desc">Type descriptor.</param>
        public BinaryObjectBuilder(IgniteBinary igniteBinary, BinaryObjectBuilder parent,
                                   BinaryObject obj, IBinaryTypeDescriptor desc)
        {
            Debug.Assert(igniteBinary != null);
            Debug.Assert(obj != null);
            Debug.Assert(desc != null);

            _igniteBinary = igniteBinary;
            _parent       = parent ?? this;
            _obj          = obj;
            _desc         = desc;

            _hashCode = obj.GetHashCode();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Process child builder.
        /// </summary>
        /// <param name="outStream">Output stream.</param>
        /// <param name="builder">Builder.</param>
        internal void ProcessBuilder(IBinaryStream outStream, BinaryObjectBuilder builder)
        {
            BinaryHeapStream inStream = new BinaryHeapStream(builder._obj.Data);

            inStream.Seek(builder._obj.Offset, SeekOrigin.Begin);

            // Builder parent context might be null only in one case: if we never met this group of
            // builders before. In this case we set context to their parent and track it. Context
            // cleanup will be performed at the very end of build process.
            if (builder._parent._ctx == null || builder._parent._ctx.Closed)
            {
                builder._parent._ctx = new Context(_parent._ctx);
            }

            builder.Mutate(inStream, outStream as BinaryHeapStream, builder._desc,
                           builder._hashCode, builder._vals);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="binary">Binary.</param>
        /// <param name="parent">Parent builder.</param>
        /// <param name="obj">Initial binary object.</param>
        /// <param name="desc">Type descriptor.</param>
        public BinaryObjectBuilder(Binary binary, BinaryObjectBuilder parent,
                                   BinaryObject obj, IBinaryTypeDescriptor desc)
        {
            Debug.Assert(binary != null);
            Debug.Assert(desc != null);

            _binary = binary;
            _parent = parent ?? this;
            _desc   = desc;

            if (obj != null)
            {
                _obj      = obj;
                _hashCode = obj.GetHashCode();
            }
            else
            {
                _obj = BinaryFromDescriptor(desc);
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Internal builder creation routine.
 /// </summary>
 /// <param name="parent">Parent builder.</param>
 /// <param name="obj">binary object.</param>
 /// <param name="desc">Type descriptor.</param>
 /// <returns>Builder.</returns>
 private BinaryObjectBuilder Builder0(BinaryObjectBuilder parent, BinaryObject obj,
                                      IBinaryTypeDescriptor desc)
 {
     return(new BinaryObjectBuilder(this, parent, obj, desc));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Unmarshal object.
 /// </summary>
 /// <param name="stream">Stream over underlying byte array with correct position.</param>
 /// <param name="mode">The mode.</param>
 /// <param name="builder">Builder.</param>
 /// <returns>
 /// Object.
 /// </returns>
 public T Unmarshal <T>(IBinaryStream stream, BinaryMode mode, BinaryObjectBuilder builder)
 {
     return(new BinaryReader(this, stream, mode, builder).Deserialize <T>());
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Mutate binary object.
        /// </summary>
        /// <param name="inStream">Input stream with initial object.</param>
        /// <param name="outStream">Output stream.</param>
        /// <param name="desc">Type descriptor.</param>
        /// <param name="vals">Values.</param>
        private void Mutate(
            BinaryHeapStream inStream,
            BinaryHeapStream outStream,
            IBinaryTypeDescriptor desc,
            IDictionary <string, BinaryBuilderField> vals)
        {
            // Set correct builder to writer frame.
            BinaryObjectBuilder oldBuilder = _parent._ctx.Writer.SetBuilder(_parent);

            int streamPos = inStream.Position;

            try
            {
                // Prepare fields.
                IBinaryTypeHandler metaHnd = _binary.Marshaller.GetBinaryTypeHandler(desc);

                IDictionary <int, BinaryBuilderField> vals0;

                if (vals == null || vals.Count == 0)
                {
                    vals0 = EmptyVals;
                }
                else
                {
                    vals0 = new Dictionary <int, BinaryBuilderField>(vals.Count);

                    foreach (KeyValuePair <string, BinaryBuilderField> valEntry in vals)
                    {
                        int fieldId = BinaryUtils.FieldId(desc.TypeId, valEntry.Key, desc.NameMapper, desc.IdMapper);

                        if (vals0.ContainsKey(fieldId))
                        {
                            throw new IgniteException("Collision in field ID detected (change field name or " +
                                                      "define custom ID mapper) [fieldName=" + valEntry.Key + ", fieldId=" + fieldId + ']');
                        }

                        vals0[fieldId] = valEntry.Value;

                        // Write metadata if: 1) it is enabled for type; 2) type is not null (i.e. it is neither
                        // remove marker, nor a field read through "GetField" method.
                        if (metaHnd != null && valEntry.Value.Type != null)
                        {
                            metaHnd.OnFieldWrite(fieldId, valEntry.Key, valEntry.Value.TypeId);
                        }
                    }
                }

                // Actual processing.
                Mutate0(_parent._ctx, inStream, outStream, true, vals0);

                // 3. Handle metadata.
                if (metaHnd != null)
                {
                    IDictionary <string, BinaryField> meta = metaHnd.OnObjectWriteFinished();

                    if (meta != null)
                    {
                        _parent._ctx.Writer.SaveMetadata(desc, meta);
                    }
                }
            }
            finally
            {
                // Restore builder frame.
                _parent._ctx.Writer.SetBuilder(oldBuilder);

                inStream.Seek(streamPos, SeekOrigin.Begin);
            }
        }