Search in sources :

Example 71 with EOFException

use of java.io.EOFException in project phoenix by apache.

the class PhoenixConnection method executeStatements.

public int executeStatements(Reader reader, List<Object> binds, PrintStream out) throws IOException, SQLException {
    int bindsOffset = 0;
    int nStatements = 0;
    PhoenixStatementParser parser = new PhoenixStatementParser(reader);
    try {
        while (true) {
            PhoenixPreparedStatement stmt = null;
            try {
                stmt = new PhoenixPreparedStatement(this, parser);
                ParameterMetaData paramMetaData = stmt.getParameterMetaData();
                for (int i = 0; i < paramMetaData.getParameterCount(); i++) {
                    stmt.setObject(i + 1, binds.get(bindsOffset + i));
                }
                long start = System.currentTimeMillis();
                boolean isQuery = stmt.execute();
                if (isQuery) {
                    ResultSet rs = stmt.getResultSet();
                    if (!rs.next()) {
                        if (out != null) {
                            out.println("no rows selected");
                        }
                    } else {
                        int columnCount = 0;
                        if (out != null) {
                            ResultSetMetaData md = rs.getMetaData();
                            columnCount = md.getColumnCount();
                            for (int i = 1; i <= columnCount; i++) {
                                int displayWidth = md.getColumnDisplaySize(i);
                                String label = md.getColumnLabel(i);
                                if (md.isSigned(i)) {
                                    out.print(displayWidth < label.length() ? label.substring(0, displayWidth) : Strings.padStart(label, displayWidth, ' '));
                                    out.print(' ');
                                } else {
                                    out.print(displayWidth < label.length() ? label.substring(0, displayWidth) : Strings.padEnd(md.getColumnLabel(i), displayWidth, ' '));
                                    out.print(' ');
                                }
                            }
                            out.println();
                            for (int i = 1; i <= columnCount; i++) {
                                int displayWidth = md.getColumnDisplaySize(i);
                                out.print(Strings.padStart("", displayWidth, '-'));
                                out.print(' ');
                            }
                            out.println();
                        }
                        do {
                            if (out != null) {
                                ResultSetMetaData md = rs.getMetaData();
                                for (int i = 1; i <= columnCount; i++) {
                                    int displayWidth = md.getColumnDisplaySize(i);
                                    String value = rs.getString(i);
                                    String valueString = value == null ? QueryConstants.NULL_DISPLAY_TEXT : value;
                                    if (md.isSigned(i)) {
                                        out.print(Strings.padStart(valueString, displayWidth, ' '));
                                    } else {
                                        out.print(Strings.padEnd(valueString, displayWidth, ' '));
                                    }
                                    out.print(' ');
                                }
                                out.println();
                            }
                        } while (rs.next());
                    }
                } else if (out != null) {
                    int updateCount = stmt.getUpdateCount();
                    if (updateCount >= 0) {
                        out.println((updateCount == 0 ? "no" : updateCount) + (updateCount == 1 ? " row " : " rows ") + stmt.getUpdateOperation().toString());
                    }
                }
                bindsOffset += paramMetaData.getParameterCount();
                double elapsedDuration = ((System.currentTimeMillis() - start) / 1000.0);
                out.println("Time: " + elapsedDuration + " sec(s)\n");
                nStatements++;
            } finally {
                if (stmt != null) {
                    stmt.close();
                }
            }
        }
    } catch (EOFException e) {
    }
    return nStatements;
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) PhoenixStatementParser(org.apache.phoenix.jdbc.PhoenixStatement.PhoenixStatementParser) ResultSet(java.sql.ResultSet) EOFException(java.io.EOFException) Savepoint(java.sql.Savepoint) ParameterMetaData(java.sql.ParameterMetaData)

Example 72 with EOFException

use of java.io.EOFException in project lucene-solr by apache.

the class JavabinLoader method parseAndLoadDocs.

private void parseAndLoadDocs(final SolrQueryRequest req, SolrQueryResponse rsp, InputStream stream, final UpdateRequestProcessor processor) throws IOException {
    UpdateRequest update = null;
    JavaBinUpdateRequestCodec.StreamingUpdateHandler handler = new JavaBinUpdateRequestCodec.StreamingUpdateHandler() {

        private AddUpdateCommand addCmd = null;

        @Override
        public void update(SolrInputDocument document, UpdateRequest updateRequest, Integer commitWithin, Boolean overwrite) {
            if (document == null) {
                // Perhaps commit from the parameters
                try {
                    RequestHandlerUtils.handleCommit(req, processor, updateRequest.getParams(), false);
                    RequestHandlerUtils.handleRollback(req, processor, updateRequest.getParams(), false);
                } catch (IOException e) {
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "ERROR handling commit/rollback");
                }
                return;
            }
            if (addCmd == null) {
                addCmd = getAddCommand(req, updateRequest.getParams());
            }
            addCmd.solrDoc = document;
            if (commitWithin != null) {
                addCmd.commitWithin = commitWithin;
            }
            if (overwrite != null) {
                addCmd.overwrite = overwrite;
            }
            if (updateRequest.isLastDocInBatch()) {
                // this is a hint to downstream code that indicates we've sent the last doc in a batch
                addCmd.isLastDocInBatch = true;
            }
            try {
                processor.processAdd(addCmd);
                addCmd.clear();
            } catch (IOException e) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "ERROR adding document " + document, e);
            }
        }
    };
    FastInputStream in = FastInputStream.wrap(stream);
    for (; ; ) {
        try {
            update = new JavaBinUpdateRequestCodec().unmarshal(in, handler);
        } catch (EOFException e) {
            // this is expected
            break;
        }
        if (update.getDeleteByIdMap() != null || update.getDeleteQuery() != null) {
            delete(req, update, processor);
        }
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) EOFException(java.io.EOFException) IOException(java.io.IOException) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) JavaBinUpdateRequestCodec(org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec) SolrException(org.apache.solr.common.SolrException) FastInputStream(org.apache.solr.common.util.FastInputStream)

Example 73 with EOFException

use of java.io.EOFException in project lucene-solr by apache.

the class TestMockDirectoryWrapper method testCorruptOnCloseIsWorking.

private void testCorruptOnCloseIsWorking(Directory dir) throws Exception {
    dir = new PreventCloseDirectoryWrapper(dir);
    try (MockDirectoryWrapper wrapped = new MockDirectoryWrapper(random(), dir)) {
        // otherwise MDW sometimes randomly leaves the file intact and we'll see false test failures:
        wrapped.alwaysCorrupt = true;
        // MDW will only try to corrupt things if it sees an index:
        RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
        iw.addDocument(new Document());
        iw.close();
        // not sync'd!
        try (IndexOutput out = wrapped.createOutput("foo", IOContext.DEFAULT)) {
            for (int i = 0; i < 100; i++) {
                out.writeInt(i);
            }
        }
    // MDW.close now corrupts our unsync'd file (foo):
    }
    boolean changed = false;
    IndexInput in = null;
    try {
        in = dir.openInput("foo", IOContext.DEFAULT);
    } catch (NoSuchFileException | FileNotFoundException fnfe) {
        // ok
        changed = true;
    }
    if (in != null) {
        for (int i = 0; i < 100; i++) {
            int x;
            try {
                x = in.readInt();
            } catch (EOFException eofe) {
                changed = true;
                break;
            }
            if (x != i) {
                changed = true;
                break;
            }
        }
        in.close();
    }
    assertTrue("MockDirectoryWrapper on dir=" + dir + " failed to corrupt an unsync'd file", changed);
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) EOFException(java.io.EOFException) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 74 with EOFException

use of java.io.EOFException in project lucene-solr by apache.

the class HttpSolrCall method writeResponse.

private void writeResponse(SolrQueryResponse solrRsp, QueryResponseWriter responseWriter, Method reqMethod) throws IOException {
    try {
        Object invalidStates = solrReq.getContext().get(CloudSolrClient.STATE_VERSION);
        // the response for each request
        if (invalidStates != null)
            solrRsp.add(CloudSolrClient.STATE_VERSION, invalidStates);
        // Now write it out
        final String ct = responseWriter.getContentType(solrReq, solrRsp);
        // don't call setContentType on null
        if (null != ct)
            response.setContentType(ct);
        if (solrRsp.getException() != null) {
            NamedList info = new SimpleOrderedMap();
            int code = ResponseUtils.getErrorInfo(solrRsp.getException(), info, log);
            solrRsp.add("error", info);
            response.setStatus(code);
        }
        if (Method.HEAD != reqMethod) {
            // Prevent close of container streams, see SOLR-8933
            OutputStream out = new CloseShieldOutputStream(response.getOutputStream());
            QueryResponseWriterUtil.writeQueryResponse(out, responseWriter, solrReq, solrRsp, ct);
        }
    //else http HEAD request, nothing to write out, waited this long just to get ContentType
    } catch (EOFException e) {
        log.info("Unable to write response, client closed connection or we are shutting down", e);
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) OutputStream(java.io.OutputStream) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream) EOFException(java.io.EOFException) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) CloseShieldOutputStream(org.apache.commons.io.output.CloseShieldOutputStream)

Example 75 with EOFException

use of java.io.EOFException in project lucene-solr by apache.

the class Node method readLocalFileMetaData.

/** Opens the specified file, reads its identifying information, including file length, full index header (includes the unique segment
   *  ID) and the full footer (includes checksum), and returns the resulting {@link FileMetaData}.
   *
   *  <p>This returns null, logging a message, if there are any problems (the file does not exist, is corrupt, truncated, etc.).</p> */
public FileMetaData readLocalFileMetaData(String fileName) throws IOException {
    Map<String, FileMetaData> cache = lastFileMetaData;
    FileMetaData result;
    if (cache != null) {
        // We may already have this file cached from the last NRT point:
        result = cache.get(fileName);
    } else {
        result = null;
    }
    if (result == null) {
        // Pull from the filesystem
        long checksum;
        long length;
        byte[] header;
        byte[] footer;
        try (IndexInput in = dir.openInput(fileName, IOContext.DEFAULT)) {
            try {
                length = in.length();
                header = CodecUtil.readIndexHeader(in);
                footer = CodecUtil.readFooter(in);
                checksum = CodecUtil.retrieveChecksum(in);
            } catch (EOFException | CorruptIndexException cie) {
                // to delete such unreferenced files, but virus checker can block that, leaving this bad file.
                if (VERBOSE_FILES) {
                    message("file " + fileName + ": will copy [existing file is corrupt]");
                }
                return null;
            }
            if (VERBOSE_FILES) {
                message("file " + fileName + " has length=" + bytesToString(length));
            }
        } catch (FileNotFoundException | NoSuchFileException e) {
            if (VERBOSE_FILES) {
                message("file " + fileName + ": will copy [file does not exist]");
            }
            return null;
        }
        // NOTE: checksum is redundant w/ footer, but we break it out separately because when the bits cross the wire we need direct access to
        // checksum when copying to catch bit flips:
        result = new FileMetaData(header, footer, length, checksum);
    }
    return result;
}
Also used : EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexInput(org.apache.lucene.store.IndexInput) CorruptIndexException(org.apache.lucene.index.CorruptIndexException)

Aggregations

EOFException (java.io.EOFException)1149 IOException (java.io.IOException)508 DataInputStream (java.io.DataInputStream)139 FileInputStream (java.io.FileInputStream)124 InputStream (java.io.InputStream)100 ByteArrayInputStream (java.io.ByteArrayInputStream)95 ArrayList (java.util.ArrayList)84 Test (org.junit.Test)84 ByteBuffer (java.nio.ByteBuffer)75 File (java.io.File)74 FileNotFoundException (java.io.FileNotFoundException)61 BufferedInputStream (java.io.BufferedInputStream)56 RandomAccessFile (java.io.RandomAccessFile)46 SocketTimeoutException (java.net.SocketTimeoutException)43 HashMap (java.util.HashMap)38 ByteArrayOutputStream (java.io.ByteArrayOutputStream)32 SocketException (java.net.SocketException)31 Map (java.util.Map)30 InterruptedIOException (java.io.InterruptedIOException)29 Path (org.apache.hadoop.fs.Path)29