<commit_before>/*
 * Copyright 2010 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.ning.metrics.goodwill.store;

import org.apache.thrift.protocol.TType;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Describe a TField
 */
public class ThriftField
{
    private String name;
    private byte type;
    private Integer position;
    private String sqlType;
    private Integer sqlLength;
    private Integer sqlScale;
    private Integer sqlPrecision;
    private String description;

    /* Keys for the JSON representation */
    public static final String JSON_THRIFT_FIELD_NAME = "name";
    public static final String JSON_THRIFT_FIELD_TYPE = "type";
    public static final String JSON_THRIFT_FIELD_POSITION = "position";
    public static final String JSON_THRIFT_FIELD_DESCRIPTION = "description";
    // When storing the thrifts in a database/datawarehouse, use these fields to describe
    // the associated sql type and length.
    // Given the large range of data types (Netezza, Oracle, MySQL, ...), no enforcement
    // is performed (plain Strings).
    public static final String JSON_THRIFT_FIELD_SQL_KEY = "sql";
    public static final String JSON_THRIFT_FIELD_SQL_TYPE = "type";
    public static final String JSON_THRIFT_FIELD_SQL_LENGTH = "length";
    public static final String JSON_THRIFT_FIELD_SQL_SCALE = "scale";
    public static final String JSON_THRIFT_FIELD_SQL_PRECISION = "precision";


    /* Human readable representation of Thrift internal types */
    private static final String TTYPE_STRING = "string";
    private static final String TTYPE_I64 = "i64";
    private static final String TTYPE_I32 = "i32";
    private static final String TTYPE_I16 = "i16";
    private static final String TTYPE_BYTE = "i8";
    private static final String TTYPE_BOOL = "bool";
    private static final String TTYPE_DOUBLE = "double";

    public ThriftField(
            String name,
            String typeString,
            Integer position
    )
    {
        this(name, typeString, position, null, null, null, null, null);
    }

    public ThriftField(
            String name,
            String typeString,
            Integer position,
            String description
    )
    {
        this(name, typeString, position, description, null, null, null, null);
    }

    public ThriftField(
            String name,
            String typeString,
            Integer position,
            String sqlType,
            Integer sqlLength
    )
    {
        this(name, typeString, position, null, sqlType, sqlLength, null, null);
    }

    public ThriftField(
            String name,
            String typeString,
            Integer position,
            String description,
            String sqlType,
            Integer sqlLength,
            Integer sqlScale,
            Integer sqlPrecision
    )
    {
        if (name == null) {
            throw new IllegalArgumentException("ThriftField name can't be null");
        }
        this.name = name;

        this.type = ttypeFromString(typeString);

        if (position == null) {
            throw new IllegalArgumentException("ThriftField position can't be null");
        }

        if ((sqlType == null || sqlType.equals("string")) && (sqlScale != null || sqlPrecision != null)) {
            throw new IllegalArgumentException("Strings cannot have a scale or precision");
        }


        this.position = position;

        // Optional fields
        this.description = description;
        this.sqlType = sqlType;
        this.sqlLength = sqlLength;
        this.sqlScale = sqlScale;
        this.sqlPrecision = sqlPrecision;
    }

    public ThriftField(
            JSONObject thriftItemJSONObject
    ) throws JSONException
    {
        this(

                thriftItemJSONObject.getString(JSON_THRIFT_FIELD_NAME),

                thriftItemJSONObject.getString(JSON_THRIFT_FIELD_TYPE),

                thriftItemJSONObject.getInt(JSON_THRIFT_FIELD_POSITION),

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_DESCRIPTION) ?
                        thriftItemJSONObject.getString(JSON_THRIFT_FIELD_DESCRIPTION) : null,

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_SQL_KEY) ?
                        (thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).has(JSON_THRIFT_FIELD_SQL_TYPE) ?
                                thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).getString(JSON_THRIFT_FIELD_SQL_TYPE) : null) : null,

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_SQL_KEY) ?
                        (thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).has(JSON_THRIFT_FIELD_SQL_LENGTH) ?
                                thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).getInt(JSON_THRIFT_FIELD_SQL_LENGTH) : null) : null,

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_SQL_KEY) ?
                        (thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).has(JSON_THRIFT_FIELD_SQL_SCALE) ?
                                thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).getInt(JSON_THRIFT_FIELD_SQL_SCALE) : null) : null,

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_SQL_KEY) ?
                        (thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).has(JSON_THRIFT_FIELD_SQL_PRECISION) ?
                                thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).getInt(JSON_THRIFT_FIELD_SQL_PRECISION) : null) : null
        );
    }

    /**
     * Lookup a TType associated with a human readable string
     *<commit_msg>store: fix ThriftField constructor<commit_after>/*
 * Copyright 2010 Ning, Inc.
 *
 * Ning licenses this file to you under the Apache License, version 2.0
 * (the "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.ning.metrics.goodwill.store;

import org.apache.thrift.protocol.TType;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Describe a TField
 */
public class ThriftField
{
    private String name;
    private byte type;
    private Integer position;
    private String sqlType;
    private Integer sqlLength;
    private Integer sqlScale;
    private Integer sqlPrecision;
    private String description;

    /* Keys for the JSON representation */
    public static final String JSON_THRIFT_FIELD_NAME = "name";
    public static final String JSON_THRIFT_FIELD_TYPE = "type";
    public static final String JSON_THRIFT_FIELD_POSITION = "position";
    public static final String JSON_THRIFT_FIELD_DESCRIPTION = "description";
    // When storing the thrifts in a database/datawarehouse, use these fields to describe
    // the associated sql type and length.
    // Given the large range of data types (Netezza, Oracle, MySQL, ...), no enforcement
    // is performed (plain Strings).
    public static final String JSON_THRIFT_FIELD_SQL_KEY = "sql";
    public static final String JSON_THRIFT_FIELD_SQL_TYPE = "type";
    public static final String JSON_THRIFT_FIELD_SQL_LENGTH = "length";
    public static final String JSON_THRIFT_FIELD_SQL_SCALE = "scale";
    public static final String JSON_THRIFT_FIELD_SQL_PRECISION = "precision";


    /* Human readable representation of Thrift internal types */
    private static final String TTYPE_STRING = "string";
    private static final String TTYPE_I64 = "i64";
    private static final String TTYPE_I32 = "i32";
    private static final String TTYPE_I16 = "i16";
    private static final String TTYPE_BYTE = "i8";
    private static final String TTYPE_BOOL = "bool";
    private static final String TTYPE_DOUBLE = "double";

    public ThriftField(
            String name,
            String typeString,
            Integer position
    )
    {
        this(name, typeString, position, null, null, null, null, null);
    }

    public ThriftField(
            String name,
            String typeString,
            Integer position,
            String description
    )
    {
        this(name, typeString, position, description, null, null, null, null);
    }

    public ThriftField(
            String name,
            String typeString,
            Integer position,
            String sqlType,
            Integer sqlLength
    )
    {
        this(name, typeString, position, null, sqlType, sqlLength, null, null);
    }

    public ThriftField(
            String name,
            String typeString,
            Integer position,
            String description,
            String sqlType,
            Integer sqlLength,
            Integer sqlScale,
            Integer sqlPrecision
    )
    {
        if (name == null) {
            throw new IllegalArgumentException("ThriftField name can't be null");
        }
        this.name = name;

        this.type = ttypeFromString(typeString);

        if (position == null) {
            throw new IllegalArgumentException("ThriftField position can't be null");
        }

        if ((sqlType == null || sqlType.equals("string")) && (sqlScale != null || sqlPrecision != null)) {
            throw new IllegalArgumentException("Strings cannot have a scale or precision");
        }


        this.position = position;

        // Optional fields
        this.description = description;
        this.sqlType = sqlType;
        this.sqlLength = sqlLength;
        this.sqlScale = sqlScale;
        this.sqlPrecision = sqlPrecision;
    }

    public ThriftField(
            JSONObject thriftItemJSONObject
    ) throws JSONException
    {
        this(

                thriftItemJSONObject.getString(JSON_THRIFT_FIELD_NAME),

                thriftItemJSONObject.getString(JSON_THRIFT_FIELD_TYPE),

                thriftItemJSONObject.getInt(JSON_THRIFT_FIELD_POSITION),

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_DESCRIPTION) ?
                        thriftItemJSONObject.getString(JSON_THRIFT_FIELD_DESCRIPTION) : null,

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_SQL_KEY) ?
                        (!thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).isNull(JSON_THRIFT_FIELD_SQL_TYPE) ?
                                thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).getString(JSON_THRIFT_FIELD_SQL_TYPE) : null) : null,

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_SQL_KEY) ?
                        (!thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).isNull(JSON_THRIFT_FIELD_SQL_LENGTH) ?
                                thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).getInt(JSON_THRIFT_FIELD_SQL_LENGTH) : null) : null,

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_SQL_KEY) ?
                        (!thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).isNull(JSON_THRIFT_FIELD_SQL_SCALE) ?
                                thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).getInt(JSON_THRIFT_FIELD_SQL_SCALE) : null) : null,

                thriftItemJSONObject.has(JSON_THRIFT_FIELD_SQL_KEY) ?
                        (!thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).isNull(JSON_THRIFT_FIELD_SQL_PRECISION) ?
                                thriftItemJSONObject.getJSONObject(JSON_THRIFT_FIELD_SQL_KEY).getInt(JSON_THRIFT_FIELD_SQL_PRECISION) : null) : null
        );
    }

    /**
     * Lookup a TType associated with a human readable string
     *
{'fraction_non_alphanumeric': 0.07081285748811729, 'fraction_numerical': 0.0033835495045516795, 'mean_word_length': 2.6298245614035087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '7659153', 'n_tokens_mistral': 3493, 'n_tokens_neox': 3378, 'n_words': 879}