Search in sources :

Example 21 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class ProtocolDataReader method readNext.

public Object readNext() {
    try {
        Set<String> keys = null;
        String schemaName = null;
        String catalogName = null;
        String[] parsedOldData = null;
        long bytesRead = 0;
        Table table = null;
        while (tokens != null || csvReader.readRecord()) {
            lineNumber++;
            context.put(CTX_LINE_NUMBER, lineNumber);
            if (tokens == null) {
                tokens = csvReader.getValues();
            }
            bytesRead += logDebugAndCountBytes(tokens);
            Statistics stats = null;
            if (batch != null) {
                stats = statistics.get(batch);
                stats.increment(DataReaderStatistics.READ_BYTE_COUNT, bytesRead);
                bytesRead = 0;
            }
            if (table != null && !(tokens[0].equals(CsvConstants.TABLE) || tokens[0].equals(CsvConstants.KEYS) || tokens[0].equals(CsvConstants.COLUMNS))) {
                return table;
            }
            if (stats != null && (tokens[0].equals(CsvConstants.INSERT) || tokens[0].equals(CsvConstants.UPDATE) || tokens[0].equals(CsvConstants.DELETE))) {
                stats.increment(DataReaderStatistics.READ_RECORD_COUNT, 1);
            }
            if (tokens[0].equals(CsvConstants.INSERT)) {
                CsvData data = new CsvData();
                data.setNoBinaryOldData(noBinaryOldData);
                data.setDataEventType(DataEventType.INSERT);
                data.putParsedData(CsvData.ROW_DATA, CollectionUtils.copyOfRange(tokens, 1, tokens.length));
                tokens = null;
                return data;
            } else if (tokens[0].equals(CsvConstants.OLD)) {
                parsedOldData = CollectionUtils.copyOfRange(tokens, 1, tokens.length);
            } else if (tokens[0].equals(CsvConstants.UPDATE)) {
                CsvData data = new CsvData();
                data.setNoBinaryOldData(noBinaryOldData);
                data.setDataEventType(DataEventType.UPDATE);
                int columnCount = context.getLastParsedTable().getColumnCount();
                if (tokens.length <= columnCount) {
                    String msg = String.format("Invalid state while parsing csv data.  " + "The number of columns (%d) reported for table '%s' doesn't match up with the token count (%d) data: %s", columnCount, context.getLastParsedTable().getFullyQualifiedTableName(), tokens.length, ArrayUtils.toString(tokens));
                    throw new IllegalStateException(msg);
                }
                data.putParsedData(CsvData.ROW_DATA, CollectionUtils.copyOfRange(tokens, 1, columnCount + 1));
                data.putParsedData(CsvData.PK_DATA, CollectionUtils.copyOfRange(tokens, columnCount + 1, tokens.length));
                data.putParsedData(CsvData.OLD_DATA, parsedOldData);
                tokens = null;
                return data;
            } else if (tokens[0].equals(CsvConstants.DELETE)) {
                CsvData data = new CsvData();
                data.setNoBinaryOldData(noBinaryOldData);
                data.setDataEventType(DataEventType.DELETE);
                data.putParsedData(CsvData.PK_DATA, CollectionUtils.copyOfRange(tokens, 1, tokens.length));
                data.putParsedData(CsvData.OLD_DATA, parsedOldData);
                tokens = null;
                return data;
            } else if (tokens[0].equals(CsvConstants.BATCH) || tokens[0].equals(CsvConstants.RETRY)) {
                Batch batch = new Batch(batchType, Long.parseLong(tokens[1]), channelId, binaryEncoding, sourceNodeId, targetNodeId, false);
                statistics.put(batch, new DataReaderStatistics());
                tokens = null;
                return batch;
            } else if (tokens[0].equals(CsvConstants.NO_BINARY_OLD_DATA)) {
                if (tokens.length > 1) {
                    noBinaryOldData = Boolean.parseBoolean(tokens[1]);
                }
            } else if (tokens[0].equals(CsvConstants.NODEID)) {
                this.sourceNodeId = tokens[1];
            } else if (tokens[0].equals(CsvConstants.BINARY)) {
                this.binaryEncoding = BinaryEncoding.valueOf(tokens[1]);
            } else if (tokens[0].equals(CsvConstants.CHANNEL)) {
                this.channelId = tokens[1];
            } else if (tokens[0].equals(CsvConstants.SCHEMA)) {
                schemaName = tokens.length == 1 || StringUtils.isBlank(tokens[1]) ? null : tokens[1];
            } else if (tokens[0].equals(CsvConstants.CATALOG)) {
                catalogName = tokens.length == 1 || StringUtils.isBlank(tokens[1]) ? null : tokens[1];
            } else if (tokens[0].equals(CsvConstants.TABLE)) {
                String tableName = tokens[1];
                table = context.getParsedTables().get(Table.getFullyQualifiedTableName(catalogName, schemaName, tableName));
                if (table != null) {
                    context.setLastParsedTable(table);
                } else {
                    table = new Table(catalogName, schemaName, tableName);
                    context.setLastParsedTable(table);
                }
            } else if (tokens[0].equals(CsvConstants.KEYS)) {
                if (keys == null) {
                    keys = new HashSet<String>(tokens.length);
                }
                for (int i = 1; i < tokens.length; i++) {
                    keys.add(tokens[i]);
                }
            } else if (tokens[0].equals(CsvConstants.COLUMNS)) {
                table.removeAllColumns();
                for (int i = 1; i < tokens.length; i++) {
                    Column column = new Column(tokens[i], keys != null && keys.contains(tokens[i]));
                    table.addColumn(column);
                }
                context.getParsedTables().put(table.getFullyQualifiedTableName(), table);
            } else if (tokens[0].equals(CsvConstants.COMMIT)) {
                if (batch != null) {
                    batch.setComplete(true);
                }
                tokens = null;
                return null;
            } else if (tokens[0].equals(CsvConstants.SQL)) {
                CsvData data = new CsvData();
                data.setNoBinaryOldData(noBinaryOldData);
                data.setDataEventType(DataEventType.SQL);
                data.putParsedData(CsvData.ROW_DATA, new String[] { tokens[1] });
                tokens = null;
                return data;
            } else if (tokens[0].equals(CsvConstants.BSH)) {
                CsvData data = new CsvData();
                data.setNoBinaryOldData(noBinaryOldData);
                data.setDataEventType(DataEventType.BSH);
                data.putParsedData(CsvData.ROW_DATA, new String[] { tokens[1] });
                tokens = null;
                return data;
            } else if (tokens[0].equals(CsvConstants.CREATE)) {
                CsvData data = new CsvData();
                data.setNoBinaryOldData(noBinaryOldData);
                data.setDataEventType(DataEventType.CREATE);
                data.putParsedData(CsvData.ROW_DATA, new String[] { tokens[1] });
                tokens = null;
                return data;
            } else if (tokens[0].equals(CsvConstants.IGNORE)) {
                if (batch != null) {
                    batch.setIgnored(true);
                }
            } else {
                log.info("Unable to handle unknown csv values: " + Arrays.toString(tokens));
            }
            tokens = null;
        }
    } catch (IOException ex) {
        throw new IoException(ex);
    }
    return null;
}
Also used : Table(org.jumpmind.db.model.Table) IOException(java.io.IOException) Statistics(org.jumpmind.util.Statistics) CsvData(org.jumpmind.symmetric.io.data.CsvData) Batch(org.jumpmind.symmetric.io.data.Batch) Column(org.jumpmind.db.model.Column) IoException(org.jumpmind.exception.IoException)

Example 22 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class CsvTableDataReader method init.

@Override
protected void init() {
    try {
        this.csvReader = CsvUtils.getCsvReader(reader);
        this.csvReader.setUseComments(true);
        this.csvReader.readHeaders();
        String[] columnNames = this.csvReader.getHeaders();
        for (String columnName : columnNames) {
            table.addColumn(new Column(columnName));
        }
    } catch (IOException e) {
        throw new IoException(e);
    }
}
Also used : Column(org.jumpmind.db.model.Column) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

Example 23 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class RestService method getSnapshot.

/**
     * Takes a snapshot for the specified engine and streams it to the client.
     */
@ApiOperation(value = "Take a diagnostic snapshot for the specified engine")
@RequestMapping(value = "engine/{engine}/snapshot", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final void getSnapshot(@PathVariable("engine") String engineName, HttpServletResponse resp) {
    BufferedInputStream bis = null;
    try {
        ISymmetricEngine engine = getSymmetricEngine(engineName);
        File file = engine.snapshot();
        resp.setHeader("Content-Disposition", String.format("attachment; filename=%s", file.getName()));
        bis = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(bis, resp.getOutputStream());
    } catch (IOException e) {
        throw new IoException(e);
    } finally {
        IOUtils.closeQuietly(bis);
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ISymmetricEngine(org.jumpmind.symmetric.ISymmetricEngine) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) FileInputStream(java.io.FileInputStream) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 24 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class SymmetricPushClient method open.

public void open() {
    try {
        transport = new HttpOutgoingTransport(new URL(buildUrl()), 30000, true, 0, -1, null, null, false, -1, false);
        writer = new ProtocolDataWriter(nodeId, transport.openWriter(), false);
        writer.start(batch);
    } catch (Exception ex) {
        throw new IoException(ex);
    }
}
Also used : ProtocolDataWriter(org.jumpmind.symmetric.io.data.writer.ProtocolDataWriter) HttpOutgoingTransport(org.jumpmind.symmetric.transport.http.HttpOutgoingTransport) IoException(org.jumpmind.exception.IoException) URL(java.net.URL) IOException(java.io.IOException) IoException(org.jumpmind.exception.IoException)

Example 25 with IoException

use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.

the class StagedResource method getReader.

public synchronized BufferedReader getReader() {
    Thread thread = Thread.currentThread();
    BufferedReader reader = readers != null ? readers.get(thread) : null;
    if (reader == null) {
        if (file != null && file.exists()) {
            try {
                reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), IoConstants.ENCODING));
                createReadersMap();
                readers.put(thread, reader);
            } catch (IOException ex) {
                throw new IoException(ex);
            }
        } else if (memoryBuffer != null && memoryBuffer.length() > 0) {
            reader = new BufferedReader(new StringReader(memoryBuffer.toString()));
            createReadersMap();
            readers.put(thread, reader);
        } else {
            throw new IllegalStateException("There is no content to read.  Memory buffer was empty and " + file.getAbsolutePath() + " was not found.");
        }
    }
    return reader;
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Aggregations

IoException (org.jumpmind.exception.IoException)48 IOException (java.io.IOException)41 File (java.io.File)8 Table (org.jumpmind.db.model.Table)8 BufferedReader (java.io.BufferedReader)6 Column (org.jumpmind.db.model.Column)6 BufferedWriter (java.io.BufferedWriter)5 FileInputStream (java.io.FileInputStream)5 FileOutputStream (java.io.FileOutputStream)4 FileWriter (java.io.FileWriter)4 OutputStreamWriter (java.io.OutputStreamWriter)4 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)4 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 SymmetricException (org.jumpmind.symmetric.SymmetricException)3 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)3 ProcessInfoKey (org.jumpmind.symmetric.model.ProcessInfoKey)3 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)2 BufferedInputStream (java.io.BufferedInputStream)2