<commit_before>﻿/* @csharp.file.header */

/*  _________        _____ __________________        _____
 *  __  ____/___________(_)______  /__  ____/______ ____(_)_______
 *  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
 *  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
 *  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/
 */

namespace GridGain.Client.Impl.Portable
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.IO;
    using GridGain.Client.Impl.Portable;
    using GridGain.Client.Portable;
    
    /**
     * <summary>Portable object implementation.</summary>
     */ 
    internal class GridClientPortableObjectImpl : IGridClientPortableObject
    {
        /** Empty fields collection. */
        private static readonly IDictionary<int, int> EMPTY_FIELDS = 
            new GridClientPortableReadOnlyDictionary<int, int>(new Dictionary<int, int>());

        /** Marshaller. */
        private readonly GridClientPortableMarshaller marsh;
        
        /** User type. */
        private readonly bool userType;

        /** Type ID. */
        private readonly int typeId;

        /** Hash code. */
        private readonly int hashCode;

        /** Raw data of this portable object. */
        private readonly byte[] data;

        /** Offset in data array. */
        private readonly int offset;

        /** Data length. */
        private readonly int len;

        /** Raw data offset. */
        private readonly int rawDataOffset;

        /** Fields. */
        private readonly IDictionary<int, int> fields;

        /**
         * <summary>Constructor.</summary>
         * <param name="marsh">Marshaller.</param>
         * <param name="data">Data bytes.</param>
         * <param name="offset">Offset.</param>
         * <param name="len">Length.</param>
         * <param name="userType">User type flag.</param>
         * <param name="typeId">Type ID.</param>
         * <param name="hashCode">Hash code.</param>
         * <param name="rawDataOffset">Raw data offset.</param>
         * <param name="fields">Fields.</param>
         */
        public GridClientPortableObjectImpl(GridClientPortableMarshaller marsh, byte[] data, int offset,
            int len, bool userType, int typeId, int hashCode, int rawDataOffset, IDictionary<int, int> fields)
        {
            this.marsh = marsh;

            this.data = data;
            this.offset = offset;
            this.len = len;

            this.userType = userType;
            this.typeId = typeId;
            this.hashCode = hashCode;            
            this.rawDataOffset = rawDataOffset;

            this.fields = fields == null ? EMPTY_FIELDS : new GridClientPortableReadOnlyDictionary<int, int>(fields);
        }

        /** <inheritdoc /> */
        public int HashCode()
        {
            return hashCode;
        }

        /** <inheritdoc /> */
        public bool IsUserType()
        {
            return userType;
        }

        /** <inheritdoc /> */
        public int TypeId()
        {
            return typeId;
        }        
       
        /** <inheritdoc /> */
        public F Field<F>(string fieldName)
        {
            throw new NotImplementedException();
        }

        /** <inheritdoc /> */
        public T Deserialize<T>()
        {
            return new GridClientPortableReadContext(marsh, marsh.IdToDescriptor, Stream()).Deserialize<T>(this);
        }

        /** <inheritdoc /> */
        public IGridClientPortableObject Copy(IDictionary<string, object> fields)
        {
            throw new System.NotImplementedException();
        }
        
        /**
         * <summary>Offset.</summary>
         */ 
        public int Offset
        {
            get { return offset; }
        }

        /**
         * <summary>Raw data offset.</summary><commit_msg># gg-8535: WIP.<commit_after>﻿/* @csharp.file.header */

/*  _________        _____ __________________        _____
 *  __  ____/___________(_)______  /__  ____/______ ____(_)_______
 *  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
 *  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ / _  /  _  / / /
 *  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/
 */

namespace GridGain.Client.Impl.Portable
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.IO;
    using GridGain.Client.Impl.Portable;
    using GridGain.Client.Portable;

    using PU = GridGain.Client.Impl.Portable.GridClientPortableUilts;
    
    /**
     * <summary>Portable object implementation.</summary>
     */ 
    internal class GridClientPortableObjectImpl : IGridClientPortableObject
    {
        /** Empty fields collection. */
        private static readonly IDictionary<int, int> EMPTY_FIELDS = 
            new GridClientPortableReadOnlyDictionary<int, int>(new Dictionary<int, int>());

        /** Marshaller. */
        private readonly GridClientPortableMarshaller marsh;
        
        /** User type. */
        private readonly bool userType;

        /** Type ID. */
        private readonly int typeId;

        /** Hash code. */
        private readonly int hashCode;

        /** Raw data of this portable object. */
        private readonly byte[] data;

        /** Offset in data array. */
        private readonly int offset;

        /** Data length. */
        private readonly int len;

        /** Raw data offset. */
        private readonly int rawDataOffset;

        /** Fields. */
        private readonly IDictionary<int, int> fields;

        /**
         * <summary>Constructor.</summary>
         * <param name="marsh">Marshaller.</param>
         * <param name="data">Data bytes.</param>
         * <param name="offset">Offset.</param>
         * <param name="len">Length.</param>
         * <param name="userType">User type flag.</param>
         * <param name="typeId">Type ID.</param>
         * <param name="hashCode">Hash code.</param>
         * <param name="rawDataOffset">Raw data offset.</param>
         * <param name="fields">Fields.</param>
         */
        public GridClientPortableObjectImpl(GridClientPortableMarshaller marsh, byte[] data, int offset,
            int len, bool userType, int typeId, int hashCode, int rawDataOffset, IDictionary<int, int> fields)
        {
            this.marsh = marsh;

            this.data = data;
            this.offset = offset;
            this.len = len;

            this.userType = userType;
            this.typeId = typeId;
            this.hashCode = hashCode;            
            this.rawDataOffset = rawDataOffset;

            this.fields = fields == null ? EMPTY_FIELDS : new GridClientPortableReadOnlyDictionary<int, int>(fields);
        }

        /** <inheritdoc /> */
        public int HashCode()
        {
            return hashCode;
        }

        /** <inheritdoc /> */
        public bool IsUserType()
        {
            return userType;
        }

        /** <inheritdoc /> */
        public int TypeId()
        {
            return typeId;
        }        
       
        /** <inheritdoc /> */
        public T Field<T>(string fieldName)
        {
            if (userType) {
                GridClientPortableTypeDescriptor desc;

                if (marsh.IdToDescriptor.TryGetValue(PU.TypeKey(true, typeId), out desc))
                {
                    int? fieldIdRef = desc.Mapper.FieldId(typeId, fieldName);

                    int fieldId = fieldIdRef.HasValue ? fieldIdRef.Value : (PU.StringHashCode(fieldName.ToLower()));

                    int pos;

                    return fields.TryGetValue(fieldId, out pos) ? Field0<T>(pos) : default(T);
                }
                else
                    throw new GridClientPortableException("Unknown user type: " + typeId);
                    
            }
            else {
                throw new GridClientPortableException("Cannot get field by name on system type: " + typeId);
            }
        }

        /**
         * <summary>Gets field value on the given.</summary>
         * <param name="pos">Position.</param>
         * <returns>Field value.</returns>
         */ 
        private T Field0<T>(int pos)
        {
            throw new NotImplementedException();
        }
             
        /** <inheritdoc /> */
        public T Deserialize<T>()
        {
            return new GridClientPortableReadContext(marsh, marsh.IdToDescriptor, Stream()).Deserialize<T>(this);
        }

        /** <inheritdoc /> */
        public IGridClientPortableObject Copy(IDictionary<string, object> fields)
        {
            throw new NotImplementedException();
        }
        
        /**
         * <summary>Offset.</summary>
         */ 
        public int Offset
        {
            get { return offset; }
        }

        /**
         * <summary>Raw data offset.</summary>
{'fraction_non_alphanumeric': 0.12008879023307437, 'fraction_numerical': 0.0006659267480577136, 'mean_word_length': 1.6111272095044915, 'pattern_counts': {'":': 0, '<': 95, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7063604', 'n_tokens_mistral': 2531, 'n_tokens_neox': 2447, 'n_words': 503}