<commit_before>package water.api;

import water.H2O;
import water.H2O.H2OCountedCompleter;
import water.Iced;
import water.api.RequestServer.Route;
import water.util.Log;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public abstract class Handler<I extends Iced, S extends Schema<I,S>> extends H2OCountedCompleter {
  protected Handler( ) { super(); }
  protected Handler( Handler completer ) { super(completer); }

  private long _t_start, _t_stop; // Start/Stop time in ms for the serve() call

  /** Dumb Version-&gt;Schema mapping */
  abstract protected S schema(int version); // TODO: should be static
  abstract protected int min_ver();         // TODO: should be static
  abstract protected int max_ver();         // TODO: should be static

  // Invoke the handler with parameters.  Can throw any exception the called handler can throw.
  final Schema handle(int version, Route route, Properties parms) throws Exception {

    if( !(min_ver() <= version && version <= max_ver()) ) // Version check!
      return new HttpErrorV1(new IllegalArgumentException("Version "+version+" is not in range V"+min_ver()+"-V"+max_ver()));

    // Make a version-specific Schema; primitive-parse the URL into the Schema,
    // then fill the Iced from the versioned Schema.
    S schema = schema(version);
    if (null == schema)
      throw H2O.fail("Failed to find a schema for version: " + version + " in: " + this.getClass());

    // Fill a Schema from the request params
    schema = schema.fillFromParms(parms);
    if (null == schema)
      throw H2O.fail("fillFromParms returned a null schema for version: " + version + " in: " + this.getClass() + " with params: " + parms);

    // Fill an impl object from the schema
    final I i = schema.createImpl();  // NOTE: it's ok to get a null implementation object
                                      // (as long as handler_method knows what to do with it).

    // Run the Handler in the Nano Thread (nano does not grok CPS!)
    _t_start = System.currentTimeMillis();
    Schema result = null;
    try { result = (Schema)route._handler_method.invoke(this, version, i); }
    // Exception throws out of the invoked method turn into InvocationTargetException
    // rather uselessly.  Peel out the original exception & throw it.
    catch( InvocationTargetException ite ) {
      Throwable t = ite.getCause();
      if( t instanceof RuntimeException ) throw (RuntimeException)t;
      if( t instanceof Error ) throw (Error)t;
      throw new RuntimeException(t);
    }
    _t_stop  = System.currentTimeMillis();

    // Version-specific unwind from the Iced back into the Schema
    return result;
  }

  protected StringBuffer markdown(Handler handler, int version, StringBuffer docs, String filename) {
    // TODO: version handling
    StringBuffer sb = new StringBuffer();
    Path path = Paths.get(filename);
    try {
      sb.append(Files.readAllBytes(path));
    }
    catch (IOException e) {
      Log.warn("Caught IOException trying to read doc file: ", path);
    }<commit_msg>Default behavior of Handlers is that compute2 does nothing.  We want well-named handler methods.<commit_after>package water.api;

import water.H2O;
import water.H2O.H2OCountedCompleter;
import water.Iced;
import water.api.RequestServer.Route;
import water.util.Log;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public abstract class Handler<I extends Iced, S extends Schema<I,S>> extends H2OCountedCompleter {
  protected Handler( ) { super(); }
  protected Handler( Handler completer ) { super(completer); }

  private long _t_start, _t_stop; // Start/Stop time in ms for the serve() call

  /** Dumb Version-&gt;Schema mapping */
  abstract protected S schema(int version); // TODO: should be static
  abstract protected int min_ver();         // TODO: should be static
  abstract protected int max_ver();         // TODO: should be static

  // Invoke the handler with parameters.  Can throw any exception the called handler can throw.
  final Schema handle(int version, Route route, Properties parms) throws Exception {

    if( !(min_ver() <= version && version <= max_ver()) ) // Version check!
      return new HttpErrorV1(new IllegalArgumentException("Version "+version+" is not in range V"+min_ver()+"-V"+max_ver()));

    // Make a version-specific Schema; primitive-parse the URL into the Schema,
    // then fill the Iced from the versioned Schema.
    S schema = schema(version);
    if (null == schema)
      throw H2O.fail("Failed to find a schema for version: " + version + " in: " + this.getClass());

    // Fill a Schema from the request params
    schema = schema.fillFromParms(parms);
    if (null == schema)
      throw H2O.fail("fillFromParms returned a null schema for version: " + version + " in: " + this.getClass() + " with params: " + parms);

    // Fill an impl object from the schema
    final I i = schema.createImpl();  // NOTE: it's ok to get a null implementation object
                                      // (as long as handler_method knows what to do with it).

    // Run the Handler in the Nano Thread (nano does not grok CPS!)
    _t_start = System.currentTimeMillis();
    Schema result = null;
    try { result = (Schema)route._handler_method.invoke(this, version, i); }
    // Exception throws out of the invoked method turn into InvocationTargetException
    // rather uselessly.  Peel out the original exception & throw it.
    catch( InvocationTargetException ite ) {
      Throwable t = ite.getCause();
      if( t instanceof RuntimeException ) throw (RuntimeException)t;
      if( t instanceof Error ) throw (Error)t;
      throw new RuntimeException(t);
    }
    _t_stop  = System.currentTimeMillis();

    // Version-specific unwind from the Iced back into the Schema
    return result;
  }

  @Override public void compute2() {
    throw H2O.unimpl();
  }

  protected StringBuffer markdown(Handler handler, int version, StringBuffer docs, String filename) {
    // TODO: version handling
    StringBuffer sb = new StringBuffer();
    Path path = Paths.get(filename);
    try {
      sb.append(Files.readAllBytes(path));
    }
    catch (IOException e) {
      Log.warn("Caught IOException trying to read doc file: ", path);
    }
{'fraction_non_alphanumeric': 0.10185611290075165, 'fraction_numerical': 0.0026077619266758707, 'mean_word_length': 3.2782152230971127, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5991778', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1805, 'n_words': 665}