Search in sources :

Example 1 with IoException

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

the class XmlDataReader method readNext.

protected void readNext() {
    try {
        Map<String, String> rowData = new LinkedHashMap<String, String>();
        String columnName = null;
        CsvData data = null;
        Table table = null;
        String catalog = null;
        String schema = null;
        int eventType = parser.next();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch(eventType) {
                case XmlPullParser.TEXT:
                    if (columnName != null) {
                        rowData.put(columnName, parser.getText());
                        columnName = null;
                    }
                    break;
                case XmlPullParser.START_TAG:
                    String name = parser.getName();
                    if ("row".equalsIgnoreCase(name)) {
                        data = new CsvData();
                        if (table != null) {
                            table.removeAllColumns();
                        }
                        data.setDataEventType(DataEventType.INSERT);
                    } else if ("field".equalsIgnoreCase(name)) {
                        boolean nullValue = false;
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if ("name".equalsIgnoreCase(attributeName)) {
                                columnName = attributeValue;
                            } else if ("xsi:nil".equalsIgnoreCase(attributeName)) {
                                nullValue = true;
                            }
                        }
                        if (nullValue) {
                            rowData.put(columnName, null);
                            columnName = null;
                        }
                    } else if ("table_data".equalsIgnoreCase(name)) {
                        Batch batch = new Batch();
                        batch.setBinaryEncoding(BinaryEncoding.BASE64);
                        next.add(batch);
                        table = new Table();
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if ("name".equalsIgnoreCase(attributeName)) {
                                table.setName(attributeValue);
                            }
                        }
                        next.add(table);
                    } else if ("table".equalsIgnoreCase(name)) {
                        Batch batch = new Batch();
                        batch.setBinaryEncoding(BinaryEncoding.BASE64);
                        next.add(batch);
                        table = DatabaseXmlUtil.nextTable(parser);
                        next.add(table);
                        Database db = new Database();
                        db.setName("dbimport");
                        db.setCatalog(catalog);
                        db.setSchema(schema);
                        db.addTable(table);
                        String xml = DatabaseXmlUtil.toXml(db);
                        data = new CsvData(DataEventType.CREATE);
                        data.putCsvData(CsvData.ROW_DATA, CsvUtils.escapeCsvData(xml));
                        next.add(data);
                    } else if ("database".equalsIgnoreCase(name)) {
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
                            String attributeName = parser.getAttributeName(i);
                            String attributeValue = parser.getAttributeValue(i);
                            if ("catalog".equalsIgnoreCase(attributeName)) {
                                catalog = attributeValue;
                            } else if ("schema".equalsIgnoreCase(attributeName)) {
                                schema = attributeValue;
                            }
                        }
                    }
                    break;
                case XmlPullParser.END_TAG:
                    name = parser.getName();
                    if ("row".equalsIgnoreCase(name)) {
                        String[] columnNames = rowData.keySet().toArray(new String[rowData.keySet().size()]);
                        for (String colName : columnNames) {
                            table.addColumn(new Column(colName));
                        }
                        String[] columnValues = rowData.values().toArray(new String[rowData.values().size()]);
                        data.putParsedData(CsvData.ROW_DATA, columnValues);
                        if (this.table == null || !this.table.equals(table)) {
                            next.add(table);
                        }
                        next.add(data);
                        rowData = new LinkedHashMap<String, String>();
                    } else if ("table_data".equalsIgnoreCase(name)) {
                        if (batch != null) {
                            batch.setComplete(true);
                        }
                    } else if ("field".equalsIgnoreCase(name)) {
                        columnName = null;
                    }
                    break;
            }
            eventType = parser.next();
        }
    } catch (IOException ex) {
        throw new IoException(ex);
    } catch (XmlPullParserException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Table(org.jumpmind.db.model.Table) IOException(java.io.IOException) CsvData(org.jumpmind.symmetric.io.data.CsvData) LinkedHashMap(java.util.LinkedHashMap) Batch(org.jumpmind.symmetric.io.data.Batch) Column(org.jumpmind.db.model.Column) Database(org.jumpmind.db.model.Database) IoException(org.jumpmind.exception.IoException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 2 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 3 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 4 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 5 with IoException

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

the class FtpDataWriter method println.

protected void println(String... data) {
    FileInfo fileInfo = fileInfoByTable.get(table.getFullyQualifiedTableName());
    if (fileInfo != null) {
        try {
            StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < data.length; i++) {
                if (i != 0) {
                    buffer.append(",");
                }
                buffer.append(data[i]);
            }
            buffer.append("\n");
            fileInfo.outputFileWriter.write(buffer.toString());
            long byteCount = buffer.length();
            statistics.get(batch).increment(DataWriterStatisticConstants.BYTECOUNT, byteCount);
        } catch (IOException e) {
            throw new IoException(e);
        }
    }
}
Also used : IoException(org.jumpmind.exception.IoException) IOException(java.io.IOException)

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