/** <inheritdoc /> */
        public void PutBinaryTypes(ICollection <BinaryType> types)
        {
            Debug.Assert(types != null);

            foreach (var binaryType in types)
            {
                var type = binaryType;  // Access to modified closure.

                _socket.DoOutInOp <object>(ClientOp.BinaryTypePut,
                                           s => BinaryProcessor.WriteBinaryType(_marsh.StartMarshal(s), type), null);
            }
        }
Exemple #2
0
        /** <inheritDoc /> */
        public T ToBinary <T>(object obj)
        {
            if (obj is IBinaryObject)
            {
                return((T)obj);
            }

            using (var stream = new BinaryHeapStream(1024))
            {
                // Serialize.
                BinaryWriter writer = _marsh.StartMarshal(stream);

                try
                {
                    writer.Write(obj);
                }
                finally
                {
                    // Save metadata.
                    _marsh.FinishMarshal(writer);
                }

                // Deserialize.
                stream.Seek(0, SeekOrigin.Begin);

                return(_marsh.Unmarshal <T>(stream, BinaryMode.ForceBinary));
            }
        }
        private static int GetComplexTypeHashCode <T>(T val, Marshaller marsh, IDictionary <int, int> affinityKeyFieldIds)
        {
            using (var stream = new BinaryHeapStream(128))
            {
                var writer = marsh.StartMarshal(stream);

                int?hashCode = null;

                writer.OnObjectWritten += (header, obj) =>
                {
                    if (affinityKeyFieldIds != null && affinityKeyFieldIds.ContainsKey(header.TypeId))
                    {
                        var err = string.Format(
                            "Affinity keys are not supported. Object '{0}' has an affinity key.", obj);

                        throw new IgniteException(err);
                    }

                    // In case of composite objects we need the last hash code.
                    hashCode = header.HashCode;
                };

                writer.Write(val);

                if (hashCode != null)
                {
                    // ReSharper disable once PossibleInvalidOperationException (false detection).
                    return(hashCode.Value);
                }

                throw new IgniteException(string.Format("Failed to compute hash code for object '{0}'", val));
            }
        }