Mercurial > hg > monetdb-java
changeset 497:aed7f32e029a onclient
Refactor LineType to be an enum
Makes it easier to catch them all when changing things.
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Fri, 13 Aug 2021 10:26:56 +0200 (2021-08-13) |
parents | c8f3950eec93 |
children | 4cfe4991ee63 |
files | src/main/java/org/monetdb/jdbc/MonetConnection.java src/main/java/org/monetdb/mcl/io/BufferedMCLReader.java src/main/java/org/monetdb/mcl/io/LineType.java src/main/java/org/monetdb/mcl/net/MapiSocket.java src/main/java/org/monetdb/merovingian/Control.java src/main/java/org/monetdb/util/SQLRestore.java |
diffstat | 6 files changed, 64 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/org/monetdb/jdbc/MonetConnection.java +++ b/src/main/java/org/monetdb/jdbc/MonetConnection.java @@ -35,6 +35,7 @@ import java.util.concurrent.Executor; import org.monetdb.mcl.io.BufferedMCLReader; import org.monetdb.mcl.io.BufferedMCLWriter; +import org.monetdb.mcl.io.LineType; import org.monetdb.mcl.net.HandshakeOptions; import org.monetdb.mcl.net.MapiSocket; import org.monetdb.mcl.parser.HeaderLineParser; @@ -2100,7 +2101,7 @@ public class MonetConnection * @return a non-null String if the line is invalid, * or additional lines are not allowed. */ - public abstract String addLine(String line, int linetype); + public abstract String addLine(String line, LineType linetype); /** * Returns whether this Response expects more lines to be added @@ -2259,12 +2260,12 @@ public class MonetConnection */ // {{{ addLine @Override - public String addLine(final String tmpLine, final int linetype) { + public String addLine(final String tmpLine, final LineType linetype) { if (isSet[LENS] && isSet[TYPES] && isSet[TABLES] && isSet[NAMES]) { return resultBlocks[0].addLine(tmpLine, linetype); } - if (linetype != BufferedMCLReader.HEADER) + if (linetype != LineType.HEADER) return "header expected, got: " + tmpLine; // depending on the name of the header, we continue @@ -2634,8 +2635,8 @@ public class MonetConnection * or additional lines are not allowed. */ @Override - public String addLine(final String line, final int linetype) { - if (linetype != BufferedMCLReader.RESULT) + public String addLine(final String line, final LineType linetype) { + if (linetype != LineType.RESULT) return "protocol violation: unexpected line in data block: " + line; // add to the backing array data[++pos] = line; @@ -2725,7 +2726,7 @@ public class MonetConnection } @Override - public String addLine(final String line, final int linetype) { + public String addLine(final String line, final LineType linetype) { return "Header lines are not supported for an UpdateResponse"; } @@ -2762,7 +2763,7 @@ public class MonetConnection public final int state = Statement.SUCCESS_NO_INFO; @Override - public String addLine(final String line, final int linetype) { + public String addLine(final String line, final LineType linetype) { return "Header lines are not supported for a SchemaResponse"; } @@ -2990,12 +2991,12 @@ public class MonetConnection // go for new results String tmpLine = in.readLine(); - int linetype = in.getLineType(); + LineType linetype = in.getLineType(); Response res = null; - while (linetype != BufferedMCLReader.PROMPT) { + while (linetype != LineType.PROMPT) { // each response should start with a start of header (or error) switch (linetype) { - case BufferedMCLReader.SOHEADER: + case SOHEADER: // make the response object, and fill it try { switch (sohp.parse(tmpLine)) { @@ -3100,7 +3101,7 @@ public class MonetConnection tmpLine = in.readLine(); linetype = in.getLineType(); break; - case BufferedMCLReader.INFO: + case INFO: addWarning(tmpLine.substring(1), "01000"); // read the next line (can be prompt, new result, error, etc.) // before we start the loop over @@ -3111,7 +3112,7 @@ public class MonetConnection // we have something we don't expect/understand, let's make it an error message tmpLine = "!M0M10!protocol violation, unexpected line: " + tmpLine; // don't break; fall through... - case BufferedMCLReader.ERROR: + case ERROR: // read everything till the prompt (should be // error) we don't know if we ignore some // garbage here... but the log should reveal that
--- a/src/main/java/org/monetdb/mcl/io/BufferedMCLReader.java +++ b/src/main/java/org/monetdb/mcl/io/BufferedMCLReader.java @@ -14,6 +14,8 @@ import java.io.InputStream; import java.io.Reader; import java.io.UnsupportedEncodingException; +import static org.monetdb.mcl.io.LineType.*; + /** * Read text from a character-input stream, buffering characters so as * to provide a means for efficient reading of characters, arrays and @@ -41,27 +43,9 @@ import java.io.UnsupportedEncodingExcept * @see org.monetdb.mcl.io.BufferedMCLWriter */ public final class BufferedMCLReader extends BufferedReader { - /** "there is currently no line", or the the type is unknown is represented by UNKNOWN */ - public final static int UNKNOWN = 0; - /** a line starting with ! indicates ERROR */ - public final static int ERROR = '!'; - /** a line starting with % indicates HEADER */ - public final static int HEADER = '%'; - /** a line starting with [ indicates RESULT */ - public final static int RESULT = '['; - /** a line which matches the pattern of prompt1 is a PROMPT */ - public final static int PROMPT = '.'; - /** a line which matches the pattern of prompt2 is a MORE */ - public final static int MORE = ','; - /** a line starting with & indicates the start of a header block */ - public final static int SOHEADER = '&'; - /** a line starting with ^ indicates REDIRECT */ - public final static int REDIRECT = '^'; - /** a line starting with # indicates INFO */ - public final static int INFO = '#'; /** The type of the last line read */ - private int lineType = UNKNOWN; + private LineType lineType = UNKNOWN; /** * Create a buffering character-input stream that uses a @@ -162,7 +146,7 @@ public final class BufferedMCLReader ext * following constants: UNKNOWN, HEADER, ERROR, PROMPT, MORE, * RESULT, SOHEADER, REDIRECT, INFO */ - public int getLineType() { + public LineType getLineType() { return lineType; }
new file mode 100644 --- /dev/null +++ b/src/main/java/org/monetdb/mcl/io/LineType.java @@ -0,0 +1,32 @@ +package org.monetdb.mcl.io; + +public enum LineType { + /** "there is currently no line", or the the type is unknown is represented by UNKNOWN */ + UNKNOWN(0), + /** a line starting with ! indicates ERROR */ + ERROR('!'), + /** a line starting with % indicates HEADER */ + HEADER('%'), + /** a line starting with [ indicates RESULT */ + RESULT('['), + /** a line which matches the pattern of prompt1 is a PROMPT */ + PROMPT('.'), + /** a line which matches the pattern of prompt2 is a MORE */ + MORE(','), + /** a line starting with & indicates the start of a header block */ + SOHEADER('&'), + /** a line starting with ^ indicates REDIRECT */ + REDIRECT('^'), + /** a line starting with # indicates INFO */ + INFO('#'); + + private final byte chr; + + LineType(int chr) { + this.chr = (byte) chr; + } + + public byte chr() { + return this.chr; + } +}
--- a/src/main/java/org/monetdb/mcl/net/MapiSocket.java +++ b/src/main/java/org/monetdb/mcl/net/MapiSocket.java @@ -31,6 +31,7 @@ import java.util.List; import org.monetdb.mcl.MCLException; import org.monetdb.mcl.io.BufferedMCLReader; import org.monetdb.mcl.io.BufferedMCLWriter; +import org.monetdb.mcl.io.LineType; import org.monetdb.mcl.parser.MCLParseException; /** @@ -293,7 +294,7 @@ public class MapiSocket { /* cannot (yet final ArrayList<String> redirects = new ArrayList<String>(); final List<String> warns = new ArrayList<String>(); String err = "", tmp; - int lineType; + LineType lineType; do { tmp = reader.readLine(); if (tmp == null) @@ -301,14 +302,14 @@ public class MapiSocket { /* cannot (yet con.getInetAddress().getHostName() + ":" + con.getPort() + ": End of stream reached"); lineType = reader.getLineType(); - if (lineType == BufferedMCLReader.ERROR) { + if (lineType == LineType.ERROR) { err += "\n" + tmp.substring(7); - } else if (lineType == BufferedMCLReader.INFO) { + } else if (lineType == LineType.INFO) { warns.add(tmp.substring(1)); - } else if (lineType == BufferedMCLReader.REDIRECT) { + } else if (lineType == LineType.REDIRECT) { redirects.add(tmp.substring(1)); } - } while (lineType != BufferedMCLReader.PROMPT); + } while (lineType != LineType.PROMPT); if (err.length() > 0) { close(); @@ -984,7 +985,7 @@ public class MapiSocket { /* cannot (yet block[blockLen++] = '\n'; } // insert 'fake' flush - block[blockLen++] = BufferedMCLReader.PROMPT; + block[blockLen++] = LineType.PROMPT.chr(); block[blockLen++] = '\n'; if (debug) log("RD ", "inserting prompt", true);
--- a/src/main/java/org/monetdb/merovingian/Control.java +++ b/src/main/java/org/monetdb/merovingian/Control.java @@ -207,17 +207,17 @@ public class Control { mout.writeLine(database + " " + command + "\n"); ArrayList<String> l = new ArrayList<String>(); String tmpLine = min.readLine(); - int linetype = min.getLineType(); - if (linetype == BufferedMCLReader.ERROR) + LineType linetype = min.getLineType(); + if (linetype == LineType.ERROR) throw new MerovingianException(tmpLine.substring(6)); - if (linetype != BufferedMCLReader.RESULT) + if (linetype != LineType.RESULT) throw new MerovingianException("unexpected line: " + tmpLine); if (!tmpLine.substring(1).equals(RESPONSE_OK)) throw new MerovingianException(tmpLine.substring(1)); tmpLine = min.readLine(); linetype = min.getLineType(); - while (linetype != BufferedMCLReader.PROMPT) { - if (linetype != BufferedMCLReader.RESULT) + while (linetype != LineType.PROMPT) { + if (linetype != LineType.RESULT) throw new MerovingianException("unexpected line: " + tmpLine);
--- a/src/main/java/org/monetdb/util/SQLRestore.java +++ b/src/main/java/org/monetdb/util/SQLRestore.java @@ -15,6 +15,7 @@ import java.util.concurrent.atomic.Atomi import org.monetdb.mcl.io.BufferedMCLReader; import org.monetdb.mcl.io.BufferedMCLWriter; +import org.monetdb.mcl.io.LineType; import org.monetdb.mcl.net.MapiSocket; /** @@ -53,9 +54,9 @@ public final class SQLRestore { final String line = _is.readLine(); if (line == null) break; - final int result = _is.getLineType(); + final LineType result = _is.getLineType(); switch (result) { - case BufferedMCLReader.ERROR: + case ERROR: _errorMessage = line; _errorState.set(true); return;