package edu.hawaii.ics.yucheng; import java.util.ArrayList; /** * A class that contains DDL commands. It is a derived class of * Java.util.ArrayList. The initial items in the list are parsed from the * 'ddlfile' statements file. * * @author Cheng Jade * @assignment ICS 421 Assignment 2-2 * @date Feb 29, 2010 * @bugs None */ public class CSVNode extends ArrayList<String> { /** The serialized version id. */ private static final long serialVersionUID = 1L; public CSVNode(final String[] line){ assert null != line; for (final String field : line) this.add(field); } // return the corresponding node this csv should be inserted to, 1 bounded. public int partitionTo(Configuration configuration, ArrayList<String> columnNames) throws ProgramException{ int indexOfPartitionColumn = -1; String partitionColumn = configuration.getPartitionColumn(); for (int i = 0; i < columnNames.size(); i++) { if (columnNames.get(i).equalsIgnoreCase(partitionColumn)) { indexOfPartitionColumn = i; break; } } if (indexOfPartitionColumn == -1) throw new ProgramException("Mismatch partition table name and the dtables"); String partitionValue = this.get(indexOfPartitionColumn); try { int intPartitionValue = Integer.parseInt(partitionValue); if (configuration.getPartitionMethod().equalsIgnoreCase("range")) { for (int i = 0; i < configuration.nodeListsize(); i++) { int param1 = Integer.parseInt(configuration.getPartition(i).param1); int param2 = Integer.parseInt(configuration.getPartition(i).param2); if (intPartitionValue > param1 && intPartitionValue <= param2) return i + 1; } throw new ProgramException("Partition value out of range"); } if (configuration.getPartitionMethod().equalsIgnoreCase("hash")) { int param1 = Integer.parseInt(configuration.getPartition(0).param1); return (intPartitionValue % param1) + 1; } throw new ProgramException("Support only range and hash partition methods."); } catch (NumberFormatException e) { throw new ProgramException("Support only numeric partion values"); } } /** * Returns a readable version of the contents of the DDL command list. * * @return A readable version of the contents of the DDL command list. */ @Override public String toString() { // If the directory is empty, return a special note. if (this.size() == 0) return "The CSV Node is empty.\n"; // Loop over each item, and add it to the string builder. final StringBuilder builder = new StringBuilder(); final int size = this.size(); for (int i = 0; i < size; i++) { builder.append("Column "); builder.append(i + 1); builder.append(": "); builder.append(this.get(i)); builder.append("\n"); } // Return the string created above. return builder.toString(); } /** * The entry point for a test for this class. * * @param args * The command line arguments. */ public static void main(final String[] args) { assert null != args; CSVNode node = new CSVNode(new String[] { "cheng", "jade", "123" }); System.out.println(node); // Exit cleanly while debugging from Eclipse. System.exit(0); } }