package nxm.ucl.prim;


import nxm.sys.lib.Parser;
import nxm.sys.lib.Primitive;
import nxm.sys.lib.Table;

/** Tests speed of inserting values with unique keys into a table.
    Run with "nxm.ucl.prim.tableinsert RES=tbl MODE=KV COUNT=1000"
    @author  J. Czechowski IV
    @since   NeXtMidas 2.1.1
 */
public class tableinsert extends Primitive {
  private String name;   // Results name
  private Table  table;  // The table
  private int    count;  // Num to add
  private String mode;   // Table mode to use.

  /** default constructor */
  public tableinsert () {}

  public int open() {
    name  = MA.getU("RES");
    count = MA.getL("COUNT");
    mode  = MA.getS("MODE");
    table = null;

    if (!mode.equals("NONE")) {
      int modeID = Parser.find(Table.tableModesList, mode, -1);
      table = new Table(mode);
    } else {
      table = new Table();
    }

    return NORMAL;
  }

  public int process() {
    if (mode.equals("NONE")) {
      for(int i = 0; i < count; i++) {
        table.put("ROW"+i, i);
      }
    } else {
      for(int i = 0; i < count; i++) {
        String key = table.getUniqueKey("ROW");
        table.put(key, i);
      }
    }

    return FINISH;
  }

  public int close() {
    MR.put(name, table);
    return NORMAL;
  }
}
