Return-Path: X-Original-To: apmail-hawq-commits-archive@minotaur.apache.org Delivered-To: apmail-hawq-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D518B17EA7 for ; Sat, 19 Sep 2015 00:36:18 +0000 (UTC) Received: (qmail 7872 invoked by uid 500); 19 Sep 2015 00:36:18 -0000 Delivered-To: apmail-hawq-commits-archive@hawq.apache.org Received: (qmail 7833 invoked by uid 500); 19 Sep 2015 00:36:18 -0000 Mailing-List: contact commits-help@hawq.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@hawq.incubator.apache.org Delivered-To: mailing list commits@hawq.incubator.apache.org Received: (qmail 7823 invoked by uid 99); 19 Sep 2015 00:36:18 -0000 Received: from Unknown (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 19 Sep 2015 00:36:18 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 0D854C0B8F for ; Sat, 19 Sep 2015 00:36:18 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.774 X-Spam-Level: * X-Spam-Status: No, score=1.774 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.006] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id ST_mskQ5kjwT for ; Sat, 19 Sep 2015 00:36:02 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id 0C3352307B for ; Sat, 19 Sep 2015 00:35:46 +0000 (UTC) Received: (qmail 3684 invoked by uid 99); 19 Sep 2015 00:35:45 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 19 Sep 2015 00:35:45 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D2E08E07E9; Sat, 19 Sep 2015 00:35:45 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: rvs@apache.org To: commits@hawq.incubator.apache.org Date: Sat, 19 Sep 2015 00:36:24 -0000 Message-Id: In-Reply-To: <1419874d5448421596d056c57b3abfc4@git.apache.org> References: <1419874d5448421596d056c57b3abfc4@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [41/51] [partial] incubator-hawq git commit: SGA import http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/8b26974c/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/HAWQRecord.java ---------------------------------------------------------------------- diff --git a/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/HAWQRecord.java b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/HAWQRecord.java new file mode 100644 index 0000000..27a975a --- /dev/null +++ b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/HAWQRecord.java @@ -0,0 +1,1802 @@ +package com.pivotal.hawq.mapreduce; + +import com.pivotal.hawq.mapreduce.datatype.*; +import com.pivotal.hawq.mapreduce.schema.HAWQField; +import com.pivotal.hawq.mapreduce.schema.HAWQPrimitiveField; +import com.pivotal.hawq.mapreduce.schema.HAWQSchema; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.Array; +import java.sql.Date; +import java.sql.Time; +import java.sql.Timestamp; + +/** + * Store schema and values for a record in database. + *

+ * User can use getXXX method in input format. + */ +public class HAWQRecord { + + protected HAWQSchema schema; + protected Object[] values; + + public HAWQRecord(HAWQSchema schema) { + this.schema = schema; + this.values = new Object[schema.getFieldCount()]; + } + + protected void checkFieldIndex(int fieldIndex) throws HAWQException { + if (fieldIndex < 1 || fieldIndex > values.length) + throw new HAWQException(String.format("index out of range [%d, %d]", 1, values.length)); + } + + /** + * Get schema of this record + * + * @return the schema in this record + */ + public HAWQSchema getSchema() { + return schema; + } + + /** + * Retrieves the value of the designated column in the current record as a + * boolean in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is false + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public boolean getBoolean(String fieldName) throws HAWQException { + return getBoolean(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * boolean in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is false + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public boolean getBoolean(int fieldIndex) throws HAWQException { + return toBoolean(getString(fieldIndex)); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBoolean(String fieldName, boolean newvalue) + throws HAWQException { + setBoolean(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBoolean(int fieldIndex, boolean newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * byte in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public byte getByte(String fieldName) throws HAWQException { + return getByte(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * byte in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public byte getByte(int fieldIndex) throws HAWQException { + return toByte(getString(fieldIndex)); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setByte(String fieldName, byte newvalue) throws HAWQException { + setByte(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setByte(int fieldIndex, byte newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * byte[] in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public byte[] getBytes(String fieldName) throws HAWQException { + return getBytes(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * byte[] in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public byte[] getBytes(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (byte[]) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBytes(String fieldName, byte[] newvalue) + throws HAWQException { + setBytes(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBytes(int fieldIndex, byte[] newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * double in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public double getDouble(String fieldName) throws HAWQException { + return getDouble(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * double in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public double getDouble(int fieldIndex) throws HAWQException { + return toDouble(getString(fieldIndex)); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setDouble(String fieldName, double newvalue) + throws HAWQException { + setDouble(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setDouble(int fieldIndex, double newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * float in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public float getFloat(String fieldName) throws HAWQException { + return getFloat(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * float in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public float getFloat(int fieldIndex) throws HAWQException { + return toFloat(getString(fieldIndex)); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setFloat(String fieldName, float newvalue) throws HAWQException { + setFloat(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setFloat(int fieldIndex, float newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * int in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public int getInt(String fieldName) throws HAWQException { + return getInt(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * int in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public int getInt(int fieldIndex) throws HAWQException { + return toInt(getString(fieldIndex)); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setInt(String fieldName, int newvalue) throws HAWQException { + setInt(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setInt(int fieldIndex, int newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * long in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public long getLong(String fieldName) throws HAWQException { + return getLong(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * long in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public long getLong(int fieldIndex) throws HAWQException { + return toLong(getString(fieldIndex)); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setLong(String fieldName, long newvalue) throws HAWQException { + setLong(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setLong(int fieldIndex, long newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * short in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public short getShort(String fieldName) throws HAWQException { + return getShort(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * short in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public short getShort(int fieldIndex) throws HAWQException { + return toShort(getString(fieldIndex)); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setShort(String fieldName, short newvalue) throws HAWQException { + setShort(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setShort(int fieldIndex, short newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * String in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public String getString(String fieldName) throws HAWQException { + return getString(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * String in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public String getString(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = this.values[fieldIndex - 1]; + return val == null ? null : val.toString(); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setString(String fieldName, String newvalue) + throws HAWQException { + setString(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setString(int fieldIndex, String newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * char in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public char getChar(String fieldName) throws HAWQException { + return getChar(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * char in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is 0 + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public char getChar(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + HAWQField field = schema.getField(fieldIndex); + + if (!field.isPrimitive()) + throw new HAWQException("cannot use of getChar on group field: " + field.getName()); + + HAWQPrimitiveField.PrimitiveType fieldType = field.asPrimitive().getType(); + if (fieldType != HAWQPrimitiveField.PrimitiveType.BPCHAR + && fieldType != HAWQPrimitiveField.PrimitiveType.VARCHAR + && fieldType != HAWQPrimitiveField.PrimitiveType.TEXT) { + throw new HAWQException("cannot use of getChar on field type: " + fieldType); + } + + String s = getString(fieldIndex); + if (s == null) return 0; + if (s.length() == 1) return s.charAt(0); + throw new HAWQException("Bad value for type char : " + s); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setChar(String fieldName, char newvalue) throws HAWQException { + setChar(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setChar(int fieldIndex, char newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = String.valueOf(newvalue); // convert char to string + } + + /** + * Retrieves whether the value of the designated column in the current + * record is null. + * + * @param fieldName the name of the field + * @return whether the value is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public boolean isNull(String fieldName) throws HAWQException { + return isNull(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves whether the value of the designated column in the current + * record is null. + * + * @param fieldIndex the index of the field + * @return whether the value is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public boolean isNull(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + return values[fieldIndex - 1] == null; + } + + /** + * Set the value of the designated column in the current record to null. + * + * @param fieldName the name of the field + * @throws HAWQException if fieldIndex is not valid + */ + public void setNull(String fieldName) throws HAWQException { + setNull(schema.getFieldIndex(fieldName)); + } + + /** + * Set the value of the designated column in the current record to null. + * + * @param fieldIndex the index of the field + * @throws HAWQException if fieldIndex is not valid + */ + public void setNull(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = null; + } + + /** + * Retrieves the value of the designated column in the current record as a + * java.sql.Timestamp in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public Timestamp getTimestamp(String fieldName) throws HAWQException { + return getTimestamp(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * java.sql.Timestamp in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public Timestamp getTimestamp(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (Timestamp) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setTimestamp(String fieldName, Timestamp newvalue) + throws HAWQException { + setTimestamp(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setTimestamp(int fieldIndex, Timestamp newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * java.sql.Time in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public Time getTime(String fieldName) throws HAWQException { + return getTime(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * java.sql.Time in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public Time getTime(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (Time) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setTime(String fieldName, Time newvalue) throws HAWQException { + setTime(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setTime(int fieldIndex, Time newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * java.sql.Date in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public Date getDate(String fieldName) throws HAWQException { + return getDate(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * java.sql.Date in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public Date getDate(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (Date) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setDate(String fieldName, Date newvalue) throws HAWQException { + setDate(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setDate(int fieldIndex, Date newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * BigDecimal in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public BigDecimal getBigDecimal(String fieldName) throws HAWQException { + return getBigDecimal(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * BigDecimal in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public BigDecimal getBigDecimal(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (BigDecimal) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBigDecimal(String fieldName, BigDecimal newvalue) + throws HAWQException { + setBigDecimal(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBigDecimal(int fieldIndex, BigDecimal newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * Array in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public Array getArray(String fieldName) throws HAWQException { + return getArray(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * Array in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public Array getArray(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQArray) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setArray(String fieldName, Array newvalue) throws HAWQException { + setArray(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setArray(int fieldIndex, Array newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + if (!(newvalue instanceof HAWQArray)) { + throw new HAWQException("only support HAWQArray instance"); + } + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQRecord in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQRecord getField(String fieldName) throws HAWQException { + return getField(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQRecord in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQRecord getField(int fieldIndex) throws HAWQException { + throw new UnsupportedOperationException("Not supported yet."); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setField(String fieldName, HAWQRecord newvalue) + throws HAWQException { + setField(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setField(int fieldIndex, HAWQRecord newvalue) + throws HAWQException { + throw new UnsupportedOperationException("Not supported yet."); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQBox in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQBox getBox(String fieldName) throws HAWQException { + return getBox(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQBox in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQBox getBox(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQBox) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBox(String fieldName, HAWQBox newvalue) throws HAWQException { + setBox(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBox(int fieldIndex, HAWQBox newvalue) throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQCircle in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQCircle getCircle(String fieldName) throws HAWQException { + return getCircle(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQCircle in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQCircle getCircle(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQCircle) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setCircle(String fieldName, HAWQCircle newvalue) + throws HAWQException { + setCircle(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setCircle(int fieldIndex, HAWQCircle newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQInterval in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQInterval getInterval(String fieldName) throws HAWQException { + return getInterval(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQInterval in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQInterval getInterval(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQInterval) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setInterval(String fieldName, HAWQInterval newvalue) + throws HAWQException { + setInterval(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setInterval(int fieldIndex, HAWQInterval newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQLseg in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQLseg getLseg(String fieldName) throws HAWQException { + return getLseg(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQLseg in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQLseg getLseg(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQLseg) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setLseg(String fieldName, HAWQLseg newvalue) + throws HAWQException { + setLseg(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setLseg(int fieldIndex, HAWQLseg newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQPath in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQPath getPath(String fieldName) throws HAWQException { + return getPath(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQPath in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQPath getPath(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQPath) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setPath(String fieldName, HAWQPath newvalue) + throws HAWQException { + setPath(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setPath(int fieldIndex, HAWQPath newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQPoint in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQPoint getPoint(String fieldName) throws HAWQException { + return getPoint(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQPoint in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQPoint getPoint(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQPoint) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setPoint(String fieldName, HAWQPoint newvalue) + throws HAWQException { + setPoint(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setPoint(int fieldIndex, HAWQPoint newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQPolygon in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQPolygon getPolygon(String fieldName) throws HAWQException { + return getPolygon(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQPolygon in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQPolygon getPolygon(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQPolygon) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setPolygon(String fieldName, HAWQPolygon newvalue) + throws HAWQException { + setPolygon(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setPolygon(int fieldIndex, HAWQPolygon newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQMacaddr in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQMacaddr getMacaddr(String fieldName) throws HAWQException { + return getMacaddr(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQMacaddr in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQMacaddr getMacaddr(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQMacaddr) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setMacaddr(String fieldName, HAWQMacaddr newvalue) + throws HAWQException { + setMacaddr(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setMacaddr(int fieldIndex, HAWQMacaddr newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQInet in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQInet getInet(String fieldName) throws HAWQException { + return getInet(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQInet in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQInet getInet(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQInet) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setInet(String fieldName, HAWQInet newvalue) + throws HAWQException { + setInet(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setInet(int fieldIndex, HAWQInet newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQCidr in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQCidr getCidr(String fieldName) throws HAWQException { + return getCidr(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQCidr in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQCidr getCidr(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQCidr) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setCidr(String fieldName, HAWQCidr newvalue) + throws HAWQException { + setCidr(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setCidr(int fieldIndex, HAWQCidr newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQVarbit in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQVarbit getVarbit(String fieldName) throws HAWQException { + return getVarbit(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQVarbit in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQVarbit getVarbit(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + Object val = values[fieldIndex - 1]; + return val == null ? null : (HAWQVarbit) val; + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setVarbit(String fieldName, HAWQVarbit newvalue) + throws HAWQException { + setVarbit(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setVarbit(int fieldIndex, HAWQVarbit newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQVarbit in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public HAWQVarbit getBit(String fieldName) throws HAWQException { + return getBit(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * HAWQVarbit in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public HAWQVarbit getBit(int fieldIndex) throws HAWQException { + return getVarbit(fieldIndex); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldName the name of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBit(String fieldName, HAWQVarbit newvalue) + throws HAWQException { + setBit(schema.getFieldIndex(fieldName), newvalue); + } + + /** + * Set the value of the designated column in the current record to new + * value. + * + * @param fieldIndex the index of the field + * @param newvalue new value to set + * @throws HAWQException if fieldIndex is not valid + */ + public void setBit(int fieldIndex, HAWQVarbit newvalue) + throws HAWQException { + this.checkFieldIndex(fieldIndex); + this.values[fieldIndex - 1] = newvalue; + } + + /** + * Retrieves the value of the designated column in the current record as a + * Object in the Java programming language. + * + * @param fieldName the name of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldName is not valid; if fail to get value from file + */ + public Object getObject(String fieldName) throws HAWQException { + return getObject(schema.getFieldIndex(fieldName)); + } + + /** + * Retrieves the value of the designated column in the current record as a + * Object in the Java programming language. + * + * @param fieldIndex the index of the field + * @return the field value; if the value is SQL NULL, the + * value returned is null + * @throws HAWQException if fieldIndex is not valid; if fail to get value from file + */ + public Object getObject(int fieldIndex) throws HAWQException { + this.checkFieldIndex(fieldIndex); + return values[fieldIndex - 1]; + } + + /** + * Reset the record to initial state + */ + public void reset() { + for (int i = 0; i < values.length; i++) { + values[i] = null; + } + } + + //----------------- Formatting Methods ------------------- + protected static boolean toBoolean(String s) { + if (s != null) { + s = s.trim(); + + if (s.equalsIgnoreCase("t") || s.equalsIgnoreCase("true") || s.equals("1")) + return true; + + if (s.equalsIgnoreCase("f") || s.equalsIgnoreCase("false") || s.equals("0")) + return false; + + try { + if (Double.valueOf(s).doubleValue() == 1) + return true; + } catch (NumberFormatException e) { + } + } + return false; // SQL NULL + } + + private static final BigInteger BYTEMAX = new BigInteger(Byte.toString(Byte.MAX_VALUE)); + private static final BigInteger BYTEMIN = new BigInteger(Byte.toString(Byte.MIN_VALUE)); + + protected static byte toByte(String s) throws HAWQException { + if (s != null) { + s = s.trim(); + if (s.length() == 0) + return 0; + try { + // try the optimal parse + return Byte.parseByte(s); + } catch (NumberFormatException e) { + // didn't work, assume the column is not a byte + try { + BigDecimal n = new BigDecimal(s); + BigInteger i = n.toBigInteger(); + + int gt = i.compareTo(BYTEMAX); + int lt = i.compareTo(BYTEMIN); + + if (gt > 0 || lt < 0) { + throw new HAWQException("Bad value for type byte : " + s); + } + return i.byteValue(); + } catch (NumberFormatException ex) { + throw new HAWQException("Bad value for type byte : " + s); + } + } + } + return 0; // SQL NULL + } + + private static final BigInteger SHORTMAX = new BigInteger(Short.toString(Short.MAX_VALUE)); + private static final BigInteger SHORTMIN = new BigInteger(Short.toString(Short.MIN_VALUE)); + + protected static short toShort(String s) throws HAWQException { + if (s != null) { + s = s.trim(); + try { + return Short.parseShort(s); + } catch (NumberFormatException e) { + try { + BigDecimal n = new BigDecimal(s); + BigInteger i = n.toBigInteger(); + int gt = i.compareTo(SHORTMAX); + int lt = i.compareTo(SHORTMIN); + + if (gt > 0 || lt < 0) { + throw new HAWQException("Bad value for type short : " + s); + } + return i.shortValue(); + + } catch (NumberFormatException ne) { + throw new HAWQException("Bad value for type short : " + s); + } + } + } + return 0; // SQL NULL + } + + private static final BigInteger INTMAX = new BigInteger(Integer.toString(Integer.MAX_VALUE)); + private static final BigInteger INTMIN = new BigInteger(Integer.toString(Integer.MIN_VALUE)); + + protected static int toInt(String s) throws HAWQException { + if (s != null) { + try { + s = s.trim(); + return Integer.parseInt(s); + } catch (NumberFormatException e) { + try { + BigDecimal n = new BigDecimal(s); + BigInteger i = n.toBigInteger(); + + int gt = i.compareTo(INTMAX); + int lt = i.compareTo(INTMIN); + + if (gt > 0 || lt < 0) { + throw new HAWQException("Bad value for type int : " + s); + } + return i.intValue(); + + } catch (NumberFormatException ne) { + throw new HAWQException("Bad value for type int : " + s); + } + } + } + return 0; // SQL NULL + } + + private final static BigInteger LONGMAX = new BigInteger(Long.toString(Long.MAX_VALUE)); + private final static BigInteger LONGMIN = new BigInteger(Long.toString(Long.MIN_VALUE)); + + protected static long toLong(String s) throws HAWQException { + if (s != null) { + try { + s = s.trim(); + return Long.parseLong(s); + } catch (NumberFormatException e) { + try { + BigDecimal n = new BigDecimal(s); + BigInteger i = n.toBigInteger(); + int gt = i.compareTo(LONGMAX); + int lt = i.compareTo(LONGMIN); + + if (gt > 0 || lt < 0) { + throw new HAWQException("Bad value for type long : " + s); + } + return i.longValue(); + } catch (NumberFormatException ne) { + throw new HAWQException("Bad value for type long : " + s); + } + } + } + return 0; // SQL NULL + } + + protected static float toFloat(String s) throws HAWQException { + if (s != null) { + try { + s = s.trim(); + return Float.parseFloat(s); + } catch (NumberFormatException e) { + throw new HAWQException("Bad value for type float : " + s); + } + } + return 0; // SQL NULL + } + + protected static double toDouble(String s) throws HAWQException { + if (s != null) { + try { + s = s.trim(); + return Double.parseDouble(s); + } catch (NumberFormatException e) { + throw new HAWQException("Bad value for type double : " + s); + } + } + return 0; // SQL NULL + } +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/8b26974c/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/conf/HAWQConfiguration.java ---------------------------------------------------------------------- diff --git a/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/conf/HAWQConfiguration.java b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/conf/HAWQConfiguration.java new file mode 100644 index 0000000..6b517c6 --- /dev/null +++ b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/conf/HAWQConfiguration.java @@ -0,0 +1,101 @@ +package com.pivotal.hawq.mapreduce.conf; + +import com.pivotal.hawq.mapreduce.schema.HAWQSchema; +import org.apache.hadoop.conf.Configuration; + +/** + * A container for configuration property names for jobs with HAWQ input/output. + *

+ * The job can be configured using the static methods in this class. + * Alternatively, the properties can be set in the configuration with proper + * values. + */ +public final class HAWQConfiguration { + + /** + * The schema of the table + */ + public static final String TABLE_SCHEMA_PROPERTY = "mapreduce.hawq.table.schema"; + + /** + * The encoding of the table + */ + public static final String TABLE_ENCODING_PROPERTY = "mapreduce.hawq.table.encoding"; + + /* + * GOSQL-1047 + * + * Save version of database + */ + /** + * The version of the database + */ + public static final String DATABASE_VERSION_PROPERTY = "mapreduce.hawq.database.version"; + + /** + * Get the schema of the table from configuration + * + * @param conf the configuration + * @return schema of the table + */ + public static HAWQSchema getInputTableSchema(Configuration conf) { + return HAWQSchema.fromString(conf.get(TABLE_SCHEMA_PROPERTY, null)); + } + + /** + * Set the schema of the table into configuration + * + * @param conf the configuration + * @param schema schema of the table + */ + public static void setInputTableSchema(Configuration conf, HAWQSchema schema) { + conf.setStrings(TABLE_SCHEMA_PROPERTY, schema.toString()); + } + + /** + * Get the encoding of the table from configuration + * + * @param conf the configuration + * @return encoding of the table + */ + public static String getInputTableEncoding(Configuration conf) { + return conf.get(TABLE_ENCODING_PROPERTY, "UTF8"); + } + + /** + * Set the encoding of the table into configuration + * + * @param conf the configuration + * @param encoding encoding of the table + */ + public static void setInputTableEncoding(Configuration conf, String encoding) { + conf.set(TABLE_ENCODING_PROPERTY, encoding); + } + + /* + * GOSQL-1047 + * + * Two functions below supply get/set method for version of database + */ + + /** + * Get version of database from configuration + * + * @param conf The configuration + * @return version of database + */ + public static String getDatabaseVersion(Configuration conf) { + return conf.get(HAWQConfiguration.DATABASE_VERSION_PROPERTY); + } + + /** + * Set version of database into configuration + * + * @param conf the configuration + * @param version version of database + */ + public static void setDatabaseVersion(Configuration conf, String version) { + conf.set(HAWQConfiguration.DATABASE_VERSION_PROPERTY, version); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/8b26974c/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQArray.java ---------------------------------------------------------------------- diff --git a/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQArray.java b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQArray.java new file mode 100644 index 0000000..51fa714 --- /dev/null +++ b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQArray.java @@ -0,0 +1,200 @@ +package com.pivotal.hawq.mapreduce.datatype; + +import java.sql.Array; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; + +import com.pivotal.hawq.mapreduce.schema.HAWQPrimitiveField; + +/** + * Store value of array (_int2, _int4, etc.) in database + */ +public final class HAWQArray implements Array +{ + + StringBuffer buffer = new StringBuffer(); + private int baseType = -1; + private String baseTypeName = ""; + private Object array = null; + + /** + * @param baseType + * type oid, e.g. 23 means int4 + * @param baseTypeName + * type name, e.g. int4 + * @param startOfEachDime + * @param numOfEachDime + * @param datas + */ + public HAWQArray(int baseType, + HAWQPrimitiveField.PrimitiveType baseTypeName, + int[] startOfEachDime, int[] numOfEachDime, Object datas) + { + String separator = ","; + if (baseTypeName == HAWQPrimitiveField.PrimitiveType.BOX) + separator = ";"; + + boolean startAllOne = true; + for (int i = 0; i < startOfEachDime.length; ++i) + { + if (startOfEachDime[i] != 1) + { + startAllOne = false; + break; + } + } + if (!startAllOne) + { + for (int j = 0; j < startOfEachDime.length; ++j) + { + buffer.append('[').append(startOfEachDime[j]).append(':') + .append((startOfEachDime[j] + numOfEachDime[j] - 1)) + .append(']'); + } + buffer.append('='); + } + + Object allDatas[] = null; + switch (startOfEachDime.length) + { + case 1: + this.array = datas; + buffer.append('{'); + allDatas = (Object[]) datas; + for (int i = 0; i < allDatas.length; ++i) + { + buffer.append(allDatas[i]); + if (i != allDatas.length - 1) + buffer.append(separator); + else + buffer.append('}'); + } + break; + case 2: + Object newDatas2[][] = new Object[numOfEachDime[0]][numOfEachDime[1]]; + buffer.append('{'); + allDatas = (Object[]) datas; + for (int i = 0; i < newDatas2.length; ++i) + { + buffer.append('{'); + for (int j = 0; j < newDatas2[i].length; ++j) + { + newDatas2[i][j] = allDatas[i * newDatas2[i].length + j]; + buffer.append(newDatas2[i][j]); + if (j != newDatas2[i].length - 1) + buffer.append(separator); + else + buffer.append('}'); + } + if (i != newDatas2.length - 1) + buffer.append(','); + else + buffer.append('}'); + } + array = newDatas2; + break; + case 3: + Object newDatas3[][][] = new Object[numOfEachDime[0]][numOfEachDime[1]][numOfEachDime[2]]; + buffer.append('{'); + allDatas = (Object[]) datas; + for (int i = 0; i < newDatas3.length; ++i) + { + buffer.append('{'); + for (int j = 0; j < newDatas3[i].length; ++j) + { + buffer.append('{'); + for (int k = 0; k < newDatas3[i][j].length; ++k) + { + newDatas3[i][j][k] = allDatas[i * newDatas3[i].length + * newDatas3[i][j].length + j + * newDatas3[i][j].length + k]; + buffer.append(newDatas3[i][j][k]); + if (k != newDatas3[i][j].length - 1) + buffer.append(separator); + else + buffer.append('}'); + } + if (j != newDatas3[i].length - 1) + buffer.append(','); + else + buffer.append('}'); + } + if (i != newDatas3.length - 1) + buffer.append(','); + else + buffer.append('}'); + } + array = newDatas3; + break; + } + + this.baseType = baseType; + this.baseTypeName = baseTypeName.toString().toLowerCase(); + } + + @Override + public String toString() + { + return buffer.toString(); + } + + public void free() throws SQLException + { + array = null; + } + + public Object getArray() throws SQLException + { + return array; + } + + public Object getArray(Map> arg0) throws SQLException + { + throw new UnsupportedOperationException(); + } + + public Object getArray(long arg0, int arg1) throws SQLException + { + throw new UnsupportedOperationException(); + } + + public Object getArray(long arg0, int arg1, Map> arg2) + throws SQLException + { + throw new UnsupportedOperationException(); + } + + public int getBaseType() throws SQLException + { + return baseType; + } + + public String getBaseTypeName() throws SQLException + { + return baseTypeName; + } + + public ResultSet getResultSet() throws SQLException + { + throw new UnsupportedOperationException(); + } + + public ResultSet getResultSet(Map> arg0) + throws SQLException + { + throw new UnsupportedOperationException(); + } + + public ResultSet getResultSet(long arg0, int arg1) throws SQLException + { + throw new UnsupportedOperationException(); + } + + public ResultSet getResultSet(long arg0, int arg1, + Map> arg2) throws SQLException + { + throw new UnsupportedOperationException(); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/8b26974c/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQBox.java ---------------------------------------------------------------------- diff --git a/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQBox.java b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQBox.java new file mode 100644 index 0000000..aac4d61 --- /dev/null +++ b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQBox.java @@ -0,0 +1,116 @@ +package com.pivotal.hawq.mapreduce.datatype; + +import com.pivotal.hawq.mapreduce.HAWQException; + +/** + * Store value of box in database + */ +public class HAWQBox +{ + private HAWQPoint point1; + private HAWQPoint point2; + + /** + * Initialize a box from string + * + * @param value + * the value that varbit init from. Should be like this: + * (1.2,1.3),(2.2,2.3) + * @throws HAWQException + */ + public HAWQBox(String value) throws HAWQException + { + String[] pointStrs = value.split(","); + + if (pointStrs.length != 4) + throw new HAWQException("Cannot convert " + value + " to HAWQBox"); + + String pointStr1 = pointStrs[0] + "," + pointStrs[1]; + String pointStr2 = pointStrs[2] + "," + pointStrs[3]; + + try + { + init(new HAWQPoint(pointStr1), new HAWQPoint(pointStr2)); + } + catch (HAWQException e) + { + throw new HAWQException("Cannot convert " + value + " to HAWQBox"); + } + } + + /** + * Initialize a box by coordinates + * + * @param x1 + * abscissa of first vertex on same diagonal + * @param y1 + * ordinate of first vertex on same diagonal + * @param x2 + * abscissa of second vertex on same diagonal + * @param y2 + * ordinate of second vertex on same diagonal + */ + public HAWQBox(double x1, double y1, double x2, double y2) + { + init(new HAWQPoint(x1, y1), new HAWQPoint(x2, y2)); + } + + /** + * Initialize a box by vertexes + * + * @param point1 + * first vertex on same diagonal + * @param point2 + * second vertex on same diagonal + */ + public HAWQBox(HAWQPoint point1, HAWQPoint point2) + { + init(point1, point2); + } + + private void init(HAWQPoint point1, HAWQPoint point2) + { + this.point1 = point1; + this.point2 = point2; + } + + /** + * Get first vertex + * + * @return first vertex + */ + public HAWQPoint getPoint1() + { + return point1; + } + + /** + * Get second vertex + * + * @return second vertex + */ + public HAWQPoint getPoint2() + { + return point2; + } + + @Override + public boolean equals(Object obj) + { + if (obj instanceof HAWQBox) + { + HAWQBox other = (HAWQBox) obj; + return point1.equals(other.getPoint1()) + && point2.equals(other.getPoint2()); + } + return false; + } + + @Override + public String toString() + { + StringBuffer buffer = new StringBuffer(); + buffer.append(point1.toString()).append(',').append(point2.toString()); + return buffer.toString(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/8b26974c/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQCidr.java ---------------------------------------------------------------------- diff --git a/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQCidr.java b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQCidr.java new file mode 100644 index 0000000..c6fcda5 --- /dev/null +++ b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQCidr.java @@ -0,0 +1,74 @@ +package com.pivotal.hawq.mapreduce.datatype; + +import com.pivotal.hawq.mapreduce.HAWQException; + +/** + * Store value of cidr in database + */ +public class HAWQCidr extends HAWQInet +{ + /** + * Initialize an cidr from byte array + * + * @param inetType + * ipv4 or ipv6 + * @param bytes + * byte array that cidr init from + * @param mask + * net mask + * @throws HAWQException + * length of bytes is not enough to init cidr or inetType is + * invalid + */ + public HAWQCidr(InetType inetType, byte[] bytes, short mask) + throws HAWQException + { + this(inetType, bytes, 0, mask); + } + + /** + * Initialize an cidr from byte array + * + * @param inetType + * ipv4 or ipv6 + * @param bytes + * byte array that cidr init from + * @param offset + * offset in byte array + * @param mask + * net mask + * @throws HAWQException + * length of bytes is not enough to init cidr or inetType is + * invalid + */ + public HAWQCidr(InetType inetType, byte[] bytes, int offset, short mask) + throws HAWQException + { + super(inetType, bytes, offset, mask); + } + + @Override + public boolean equals(Object obj) + { + if (obj instanceof HAWQCidr) + { + HAWQInet other = (HAWQInet) obj; + return super.equals(other); + } + return false; + } + + @Override + public String toString() + { + String temp = super.toString(); + if (temp.indexOf('/') == -1) + { + if (getInetType() == InetType.IPV4) + return temp + "/32"; + else + return temp + "/128"; + } + return temp; + } +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/8b26974c/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQCircle.java ---------------------------------------------------------------------- diff --git a/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQCircle.java b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQCircle.java new file mode 100644 index 0000000..918ee0e --- /dev/null +++ b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQCircle.java @@ -0,0 +1,131 @@ +package com.pivotal.hawq.mapreduce.datatype; + +import com.pivotal.hawq.mapreduce.HAWQException; + +/** + * Store value of circle in database + */ +public class HAWQCircle +{ + private HAWQPoint center; + private double radius; + + /** + * Initialize a circle from string + * + * @param value + * the value that circle init from. Should be like this: + * <(1.2,1.3),2.2> + * @throws HAWQException + * when this value is not correct for circle + */ + public HAWQCircle(String value) throws HAWQException + { + if (value.startsWith("<") && value.endsWith(">")) + { + String[] pointStrs = value.substring(1, value.length() - 1).split( + ","); + + if (pointStrs.length != 3) + throw new HAWQException("Cannot convert " + value + + " to HAWQCircle"); + + String pointStr = pointStrs[0] + "," + pointStrs[1]; + + try + { + init(new HAWQPoint(pointStr), Double.parseDouble(pointStrs[2])); + } + catch (Exception e) + { + throw new HAWQException("Cannot convert " + value + + " to HAWQCircle"); + } + } + else + { + throw new HAWQException("Cannot convert " + value + + " to HAWQCircle"); + } + } + + /** + * Initialize a box by coordinates + * + * @param centerX + * abscissa of center + * @param centerY + * ordinate of center + * @param radius + * radius of this circle + */ + public HAWQCircle(double centerX, double centerY, double radius) + { + init(new HAWQPoint(centerX, centerY), radius); + } + + /** + * Initialize a box by center point and radius + * + * @param center + * center point of this circle + * @param radius + * radius of this circle + */ + public HAWQCircle(HAWQPoint center, double radius) + { + init(center, radius); + } + + private void init(HAWQPoint center, double radius) + { + this.center = center; + this.radius = radius; + } + + /** + * Get center of this circle + * + * @return circle center + */ + public HAWQPoint getCenter() + { + return center; + } + + /** + * Get radius of this circle + * + * @return circle radius + */ + public double getRadius() + { + return radius; + } + + @Override + public boolean equals(Object obj) + { + if (obj instanceof HAWQCircle) + { + HAWQCircle other = (HAWQCircle) obj; + return radius == other.getRadius() + && center.equals(other.getCenter()); + } + return false; + } + + @Override + public String toString() + { + StringBuffer buffer = new StringBuffer(); + buffer.append('<').append(center).append(',').append(radius) + .append('>'); + /* + * GPSQL-936 + * + * Remove useless ".0" for float/double + */ + return buffer.toString().replace(".0", ""); + } +} http://git-wip-us.apache.org/repos/asf/incubator-hawq/blob/8b26974c/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQInet.java ---------------------------------------------------------------------- diff --git a/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQInet.java b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQInet.java new file mode 100644 index 0000000..12cac26 --- /dev/null +++ b/contrib/hawq-hadoop/hawq-mapreduce-common/src/main/java/com/pivotal/hawq/mapreduce/datatype/HAWQInet.java @@ -0,0 +1,287 @@ +package com.pivotal.hawq.mapreduce.datatype; + +import com.pivotal.hawq.mapreduce.HAWQException; + +/** + * Store value of inet in database + */ +public class HAWQInet +{ + public static enum InetType + { + IPV4, IPV6 + } + + private short mask; + private int[] ipv6_value; + private int[] ipv4_value; + private InetType inetType; + private String value = null; + + /** + * Initialize an inet from string + * + * @param value + * the value that inet init from + * @throws HAWQException + * when this value is not correct for inet + */ + public HAWQInet(String value) throws HAWQException + { + try + { + int offset = value.indexOf('.'); + int maskOffset = value.indexOf('/'); + String valueWithoutMask = value; + if (maskOffset != -1) + { + mask = Short.parseShort(value.substring(maskOffset + 1)); + valueWithoutMask = value.substring(0, maskOffset); + } + + if (offset != -1) + { + if (maskOffset == -1) + mask = 32; + inetType = InetType.IPV4; + ipv4_value = new int[4]; + int dot1 = valueWithoutMask.indexOf('.'); + int dot2 = valueWithoutMask.indexOf('.', dot1 + 1); + int dot3 = valueWithoutMask.indexOf('.', dot2 + 1); + ipv4_value[0] = Integer.parseInt(valueWithoutMask.substring(0, + dot1)); + ipv4_value[1] = Integer.parseInt(valueWithoutMask.substring( + dot1 + 1, dot2)); + ipv4_value[2] = Integer.parseInt(valueWithoutMask.substring( + dot2 + 1, dot3)); + ipv4_value[3] = Integer.parseInt(valueWithoutMask + .substring(dot3 + 1)); + + if (mask == 32) + this.value = valueWithoutMask; + else + this.value = value; + } + else + { + if (maskOffset == -1) + mask = 128; + inetType = InetType.IPV6; + ipv6_value = new int[8]; + for (int i = 0; i < 8; ++i) + ipv6_value[i] = 0; + + int indexOfMiddleZero = valueWithoutMask.indexOf("::"); + if (indexOfMiddleZero == -1) + { + String[] values = valueWithoutMask.split(":"); + for (int i = 0; i < 8; ++i) + ipv6_value[i] = Integer.parseInt(values[i], 16); + } + else + { + String[] valueFirstPart = valueWithoutMask.substring(0, + indexOfMiddleZero).split(":"); + String[] valueSecondPart = valueWithoutMask.substring( + indexOfMiddleZero + 2).split(":"); + if (valueFirstPart.length != 1 + || !valueFirstPart[0].equals("")) + for (int i = 0; i < valueFirstPart.length; ++i) + ipv6_value[i] = Integer.parseInt(valueFirstPart[i], + 16); + if (valueSecondPart.length != 1 + || !valueSecondPart[0].equals("")) + for (int i = 0; i < valueSecondPart.length; ++i) + ipv6_value[7 - i] = Integer.parseInt( + valueSecondPart[i], 16); + } + if (mask == 128) + this.value = valueWithoutMask; + else + this.value = value; + } + } + catch (Exception e) + { + throw new HAWQException("Cannot convert " + value + " to HAWQInet"); + } + } + + /** + * Initialize an inet from byte array + * + * @param inetType + * ipv4 or ipv6 + * @param bytes + * byte array that inet init from + * @param mask + * net mask + * @throws HAWQException + * length of bytes is not enough to init inet or inetType is + * invalid + */ + public HAWQInet(InetType inetType, byte[] bytes, short mask) + throws HAWQException + { + this(inetType, bytes, 0, mask); + } + + /** + * Initialize an inet from byte array + * + * @param inetType + * ipv4 or ipv6 + * @param bytes + * byte array that inet init from + * @param offset + * offset in byte array + * @param mask + * net mask + * @throws HAWQException + * length of bytes is not enough to init inet or inetType is + * invalid + */ + public HAWQInet(InetType inetType, byte[] bytes, int offset, short mask) + throws HAWQException + { + this.inetType = inetType; + this.mask = mask; + switch (this.inetType) + { + case IPV6: + ipv6_value = new int[8]; + try + { + for (int i = 0; i < 8; ++i) + { + ipv6_value[i] = (((int) bytes[offset + 2 * i] & 0xFF) << 8) + | (((int) bytes[offset + 2 * i + 1]) & 0xFF); + } + } + catch (ArrayIndexOutOfBoundsException e) + { + throw new HAWQException("Need at least 16 bytes: offset is " + + offset + " while length of bytes is " + bytes.length); + } + break; + case IPV4: + ipv4_value = new int[4]; + try + { + ipv4_value[0] = (int) bytes[offset] & 0xFF; + ipv4_value[1] = (int) bytes[offset + 1] & 0xFF; + ipv4_value[2] = (int) bytes[offset + 2] & 0xFF; + ipv4_value[3] = (int) bytes[offset + 3] & 0xFF; + } + catch (ArrayIndexOutOfBoundsException e) + { + throw new HAWQException("Need at least 4 bytes: offset is " + + offset + " while length of bytes is " + bytes.length); + } + break; + default: + throw new HAWQException("Wrong inet type"); + } + + } + + /** + * Get type of this inet(ipv4/ipv6) + * + * @return type of this inet + */ + public InetType getInetType() + { + return inetType; + } + + /** + * Get net mask of this inet + * + * @return net mask + */ + public short getMask() + { + return mask; + } + + @Override + public boolean equals(Object obj) + { + if (obj instanceof HAWQInet) + { + HAWQInet other = (HAWQInet) obj; + if (inetType != other.getInetType()) + return false; + if (mask != other.getMask()) + return false; + if (!toString().equals(other.toString())) + return false; + return true; + } + return false; + } + + @Override + public String toString() + { + if (value != null) + return value; + + StringBuffer buffer = new StringBuffer(); + switch (inetType) + { + case IPV6: + boolean hasContinuousZero = false; + for (int i = 0; i < 8; ++i) + { + buffer.append(Integer.toHexString(ipv6_value[i])); + if (i != 7) + { + buffer.append(':'); + if (ipv6_value[i] == 0 && ipv6_value[i + 1] == 0) + hasContinuousZero = true; + } + } + if (mask != 128) + buffer.append('/').append(mask); + + if (hasContinuousZero) + { + StringBuffer temp = new StringBuffer(); + int offset = -1, length = -1; + for (int i = 7; i >= 2; --i) + { + temp.delete(0, buffer.length()); + for (int j = 0; j < i; ++j) + { + if (j != 0) + temp.append(':'); + temp.append('0'); + } + offset = buffer.indexOf(temp.toString()); + if (offset != -1) + { + length = temp.length(); + break; + } + } + if (length != 13) + buffer.replace(offset, offset + length, ""); + else + buffer.replace(offset, offset + length, ":"); + } + break; + case IPV4: + buffer.append(ipv4_value[0]).append('.').append(ipv4_value[1]) + .append('.').append(ipv4_value[2]).append('.') + .append(ipv4_value[3]); + if (mask != 32) + buffer.append('/').append(mask); + break; + default: + return null; + } + return buffer.toString(); + } +}