view src/main/java/nl/cwi/monetdb/mcl/parser/MCLParser.java @ 322:0fcf338ce0b4

Optimized parse method of TupleLineParser by creating less helper objects and replacing method calls by direct operations on variables. We now only create a StringBuilder object uesc when it is needed (when an escaped character is detected in the tuple line). In most tuple data lines no escape characters are used and thus the StringBuilder object was not used/needed. Also increased the default capacity of StringBuilder uesc such that it is not enlarged so often. Also made StringBuilder object uesc now part of the TupleLineParser object, such that it can be reused by many parse() calls, again reducing the number of created StringBuilder objects.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Wed, 11 Sep 2019 17:18:00 +0200 (2019-09-11)
parents bb273e9c7e09
children 54137aeb1f92
line wrap: on
line source
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
 */

package nl.cwi.monetdb.mcl.parser;


/**
 * Interface for parsers in MCL.  The parser family in MCL is set up as
 * a reusable object.  This allows the same parser to be used again for
 * the same type of work.  While this is a very unnatural solution in
 * the Java language, it prevents many object creations on a low level
 * of the protocol.  This favours performance.
 * 
 * A typical parser has a method parse() which takes a String, and the
 * methods hasNext() and next() to retrieve the values that were
 * extracted by the parser.  Parser specific methods may be available to
 * perform common tasks.
 *
 * @author Fabian Groffen
 */
public abstract class MCLParser {
	/** The String values found while parsing.  Public, you may touch it. */
	public final String values[];
	protected int colnr = 0;

	/**
	 * Creates an MCLParser targetted at a given number of field values.
	 * The lines parsed by an instance of this MCLParser should have
	 * exactly capacity field values.
	 *
	 * @param capacity the number of field values to expect
	 */
	protected MCLParser(final int capacity) {
		values = new String[capacity];
	}

	/**
	 * Parse the given string, and populate the internal field array
	 * to allow for next() and hasNext() calls.
	 *
	 * @param source the String containing the line to parse
	 * @return value
	 * @throws MCLParseException if source cannot be (fully) parsed by
	 * this parser
	 * @see #next()
	 * @see #hasNext()
	 */
	abstract public int parse(final String source) throws MCLParseException;

	/**
	 * Repositions the internal field offset to the start, such that the
	 * next call to next() will return the first field again.
	 */
	final public void reset() {
		colnr = 0;
	}

	/**
	 * Returns whether the next call to next() or nextInt() succeeds.
	 *
	 * @return true if the next call to next() or nextInt() is bound to
	 * succeed
	 * @see #next()
	 */
	final public boolean hasNext() {
		return colnr < values.length;
	}

	/**
	 * Returns the current field value, and advances the field counter
	 * to the next value.  This method may fail with a RuntimeError if
	 * the current field counter is out of bounds.  Call hasNext() to
	 * determine if the call to next() will succeed.
	 *
	 * @return the current field value
	 * @see #hasNext()
	 */
	final public String next() {
		return values[colnr++];
	}
}