📄 simpleformatter.java
字号:
/* * @(#)$Id: SimpleFormatter.java,v 1.8 2004/07/02 23:59:21 huebsch Exp $ * * Copyright (c) 2001-2004 Regents of the University of California. * All rights reserved. * * This file is distributed under the terms in the attached BERKELEY-LICENSE * file. If you do not find these files, copies can be found by writing to: * Computer Science Division, Database Group, Universite of California, * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License * * Copyright (c) 2003-2004 Intel Corporation. All rights reserved. * * This file is distributed under the terms in the attached INTEL-LICENSE file. * If you do not find these files, copies can be found by writing to: * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300, * Berkeley, CA, 94704. Attention: Intel License Inquiry. */package pier.helpers.formatters;import java.net.InetSocketAddress;import java.nio.ByteBuffer;import java.util.Iterator;import pier.data.Tuple;import pier.data.TupleCollection;import pier.query.QueryPlan;import pier.query.QueryTag;import services.LocalNode;/** * Class SimpleFormatter * */public class SimpleFormatter implements Formatter { protected final static byte[] errorBytes = new byte[]{69, 114, 114, 111, 114}; protected final static ByteBuffer errorBuffer = ByteBuffer.allocate(errorBytes.length).put(errorBytes); protected final static String QUERY_TYPE_CHAR = "Q"; protected final static String MESSAGE_TYPE_CHAR = "M"; protected final static String ERROR_TYPE_CHAR = "E"; protected final static String RESULT_TYPE_CHAR = "T"; protected final static String DELIMITER = ","; protected final static String NEWLINE = "\n\r"; protected final static String NULL_VALUE = "\\N"; /** * Method convertStringToBuffer * * @param stringBuffer * @return */ protected ByteBuffer convertStringToBuffer(StringBuffer stringBuffer) { return LocalNode.myTCPRawMessenger.convertStringToBuffer( stringBuffer.toString()); } /** * Method generateLineHeader * * @param buffer * @param linePrefix * @param refnum * @param queryTag * @param source * @param time * @return */ protected StringBuffer generateLineHeader(StringBuffer buffer, String linePrefix, int refnum, QueryTag queryTag, InetSocketAddress source, long time) { if (buffer == null) { buffer = new StringBuffer(); } buffer.append(linePrefix); buffer.append(DELIMITER); if (time >= 0) { buffer.append(time); buffer.append(DELIMITER); } buffer.append(refnum); buffer.append(DELIMITER); if (queryTag != null) { buffer.append(queryTag); } else { buffer.append(NULL_VALUE); } buffer.append(DELIMITER); if (source != null) { buffer.append(source); } else { buffer.append(NULL_VALUE); } return buffer; } /** * Method convertTupleToStringBuffer * * @param buffer * @param tuple * @param refnum * @param queryTag * @param source * @param time * @return */ protected StringBuffer convertTupleToStringBuffer(StringBuffer buffer, Tuple tuple, int refnum, QueryTag queryTag, InetSocketAddress source, long time) { buffer = generateLineHeader(buffer, RESULT_TYPE_CHAR, refnum, queryTag, source, time); buffer.append(DELIMITER); if (tuple != null) { buffer.append(convertTupleToString(tuple)); } else { buffer.append(NULL_VALUE); } buffer.append(NEWLINE); return buffer; } /** * Method escapeString * * @param string * @return */ protected String escapeString(String string) { // Convert: \ -> \\ string = string.replaceAll("\\\\", "\\\\\\\\"); // Convert: DELIMITER -> \DELIMITER string = string.replaceAll(DELIMITER, "\\\\" + DELIMITER); // Convert: [backspace ASCII 8] -> \b string = string.replaceAll("\\u0008", "\\\\b"); // Convert: [form feed ASCII 12] -> \f string = string.replaceAll("\\f", "\\\\f"); // Convert: [newline ASCII 10] -> \n string = string.replaceAll("\\n", "\\\\n"); // Convert: [carriage return ASCII 13] -> \r string = string.replaceAll("\\r", "\\\\r"); // Convert: [tab ASCII 9] -> \t string = string.replaceAll("\\t", "\\\\t"); // Convert: [vertical tab ASCII 11] -> \v string = string.replaceAll("\\u000B", "\\\\v"); return string; } /** * Method convertTupleToString * * @param tuple * @return */ protected String convertTupleToString(Tuple tuple) { String string = new String(); string = string + tuple.getTableName(); for (int i = 0; i < tuple.numFields(); i++) { Object fieldValue = tuple.getValue(i); String valueString = (fieldValue == null) ? NULL_VALUE : escapeString(fieldValue.toString()); string = string + DELIMITER + valueString; } return string; } /** * Method formatTuple * * @param tuple * @param refnum * @param queryTag * @param source * @param time * @return */ public ByteBuffer formatTuple(Tuple tuple, int refnum, QueryTag queryTag, InetSocketAddress source, long time) { return convertStringToBuffer(convertTupleToStringBuffer(null, tuple, refnum, queryTag, source, time)); } /** * Method formatTuples * * @param tuples * @param refnum * @param queryTag * @param source * @param time * @return */ public ByteBuffer formatTuples(TupleCollection tuples, int refnum, QueryTag queryTag, InetSocketAddress source, long time) { StringBuffer buffer = new StringBuffer(); Iterator tupleIterator = tuples.iterator(); while (tupleIterator.hasNext()) { Tuple theTuple = (Tuple) tupleIterator.next(); convertTupleToStringBuffer(buffer, theTuple, refnum, queryTag, source, time); } return convertStringToBuffer(buffer); } /** * Method formatQuery * * @param theQuery * @param refnum * @param source * @param time * @return */ public ByteBuffer formatQuery(String theQuery, int refnum, InetSocketAddress source, long time) { StringBuffer buffer = generateLineHeader(null, QUERY_TYPE_CHAR, refnum, null, source, time); buffer.append(DELIMITER); buffer.append(escapeString(theQuery)); buffer.append(NEWLINE); return convertStringToBuffer(buffer); } /** * Method formatQuery * * @param thePlan * @param refnum * @param source * @param time * @return */ public ByteBuffer formatQuery(QueryPlan thePlan, int refnum, InetSocketAddress source, long time) { StringBuffer buffer = generateLineHeader(null, QUERY_TYPE_CHAR, refnum, null, source, time); buffer.append(DELIMITER); buffer.append(escapeString(thePlan.toString())); buffer.append(NEWLINE); return convertStringToBuffer(buffer); } /** * Method formatMessage * * @param message * @param refnum * @param queryTag * @param source * @param time * @return */ public ByteBuffer formatMessage(String message, int refnum, QueryTag queryTag, InetSocketAddress source, long time) { StringBuffer buffer = generateLineHeader(null, MESSAGE_TYPE_CHAR, refnum, null, source, time); buffer.append(DELIMITER); buffer.append(escapeString(message)); buffer.append(NEWLINE); return convertStringToBuffer(buffer); } /** * Method formatError * * @param errorMessage * @param refnum * @param queryTag * @param source * @param time * @return */ public ByteBuffer formatError(String errorMessage, int refnum, QueryTag queryTag, InetSocketAddress source, long time) { StringBuffer buffer = generateLineHeader(null, ERROR_TYPE_CHAR, refnum, null, source, time); buffer.append(DELIMITER); buffer.append(escapeString(errorMessage)); buffer.append(NEWLINE); return convertStringToBuffer(buffer); } /** * Method formatSchema * * @param tuple * @param refnum * @param queryTag * @param time * @return */ public ByteBuffer formatSchema(Tuple tuple, int refnum, QueryTag queryTag, long time) { return LocalNode.myTCPRawMessenger.convertStringToBuffer(""); } /** * Method messagePreamble * * * @param messageType * @param time * @return */ public ByteBuffer messagePreamble(int messageType, long time) { return LocalNode.myTCPRawMessenger.convertStringToBuffer(""); } /** * Method messagePostamble * * * @param messageType * @param time * @return */ public ByteBuffer messagePostamble(int messageType, long time) { return LocalNode.myTCPRawMessenger.convertStringToBuffer(""); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -