Search in sources :

Example 11 with Statistics

use of org.jumpmind.util.Statistics in project symmetric-ds by JumpMind.

the class TransformWriter method write.

public void write(CsvData data) {
    DataEventType eventType = data.getDataEventType();
    if (activeTransforms != null && activeTransforms.size() > 0 && isTransformable(eventType)) {
        if (data.requiresTable() && sourceTable == null && context.getLastParsedTable() != null) {
            // if we cross batches and the table isn't specified, then
            // use the last table we used
            start(context.getLastParsedTable());
        }
        long ts = System.currentTimeMillis();
        Map<String, String> sourceValues = data.toColumnNameValuePairs(this.sourceTable.getColumnNames(), CsvData.ROW_DATA);
        Map<String, String> oldSourceValues = null;
        if (data.contains(CsvData.OLD_DATA)) {
            oldSourceValues = data.toColumnNameValuePairs(this.sourceTable.getColumnNames(), CsvData.OLD_DATA);
        }
        Map<String, String> sourceKeyValues = null;
        if (data.contains(CsvData.PK_DATA)) {
            sourceKeyValues = data.toKeyColumnValuePairs(this.sourceTable);
        }
        if (eventType == DataEventType.DELETE) {
            sourceValues = oldSourceValues;
            if (sourceValues == null || sourceValues.size() == 0) {
                sourceValues = sourceKeyValues;
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("{} transformation(s) started because of {} on {}.  The original row data was: {}", new Object[] { activeTransforms.size(), eventType.toString(), this.sourceTable.getFullyQualifiedTableName(), sourceValues });
        }
        List<TransformedData> dataThatHasBeenTransformed = new ArrayList<TransformedData>();
        TransformTable[] transformTables = activeTransforms.toArray(new TransformTable[activeTransforms.size()]);
        if (eventType == DataEventType.DELETE) {
            CollectionUtils.reverseArray(transformTables);
        }
        for (TransformTable transformation : transformTables) {
            if (eventType == DataEventType.INSERT && transformation.isUpdateFirst()) {
                eventType = DataEventType.UPDATE;
            }
            dataThatHasBeenTransformed.addAll(transform(eventType, context, transformation, sourceKeyValues, oldSourceValues, sourceValues));
        }
        for (TransformedData transformedData : dataThatHasBeenTransformed) {
            Table transformedTable = transformedData.buildTargetTable();
            CsvData csvData = transformedData.buildTargetCsvData();
            long transformTimeInMs = System.currentTimeMillis() - ts;
            boolean processData = true;
            if (lastTransformedTable == null || !lastTransformedTable.equals(transformedTable)) {
                if (lastTransformedTable != null) {
                    this.nestedWriter.end(lastTransformedTable);
                }
                processData = this.nestedWriter.start(transformedTable);
                if (!processData) {
                    lastTransformedTable = null;
                } else {
                    lastTransformedTable = transformedTable;
                }
            }
            if (processData || !csvData.requiresTable()) {
                this.nestedWriter.write(csvData);
            }
            Statistics stats = this.nestedWriter.getStatistics().get(batch);
            if (stats != null) {
                stats.increment(DataWriterStatisticConstants.TRANSFORMMILLIS, transformTimeInMs);
            }
            ts = System.currentTimeMillis();
        }
    } else {
        if (sourceTable != null) {
            super.start(sourceTable);
        }
        super.write(data);
        if (sourceTable != null) {
            super.end(sourceTable);
        }
    }
}
Also used : TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable) Table(org.jumpmind.db.model.Table) TransformedData(org.jumpmind.symmetric.io.data.transform.TransformedData) ArrayList(java.util.ArrayList) DataEventType(org.jumpmind.symmetric.io.data.DataEventType) TransformTable(org.jumpmind.symmetric.io.data.transform.TransformTable) Statistics(org.jumpmind.util.Statistics) CsvData(org.jumpmind.symmetric.io.data.CsvData)

Example 12 with Statistics

use of org.jumpmind.util.Statistics in project symmetric-ds by JumpMind.

the class AbstractProtocolDataWriter method start.

public void start(Batch batch) {
    this.statistics.put(batch, new Statistics());
    this.batch = batch;
    if (listeners != null) {
        for (IProtocolDataWriterListener listener : listeners) {
            listener.start(context, batch);
        }
    }
    if (StringUtils.isBlank(sourceNodeId)) {
        sourceNodeId = batch.getSourceNodeId();
    }
    if (flushNodeId) {
        if (StringUtils.isNotBlank(sourceNodeId)) {
            println(CsvConstants.NODEID, sourceNodeId);
        }
        if (!backwardsCompatible) {
            printBinary();
        }
        flushNodeId = false;
    }
    if (!backwardsCompatible && StringUtils.isNotBlank(batch.getChannelId())) {
        println(CsvConstants.CHANNEL, batch.getChannelId());
    }
    println(CsvConstants.BATCH, Long.toString(batch.getBatchId()));
    if (backwardsCompatible) {
        printBinary();
    }
}
Also used : Statistics(org.jumpmind.util.Statistics)

Example 13 with Statistics

use of org.jumpmind.util.Statistics in project symmetric-ds by JumpMind.

the class AbstractWriterTest method writeData.

protected long writeData(IDataWriter writer, TableCsvData... datas) {
    this.lastDataWriterUsed = writer;
    DataContext context = new DataContext();
    writer.open(context);
    try {
        for (TableCsvData tableCsvData : datas) {
            Batch batch = new Batch(BatchType.LOAD, getNextBatchId(), "default", BinaryEncoding.BASE64, "00000", "00001", false);
            try {
                writer.start(batch);
                if (writer.start(tableCsvData.table)) {
                    for (CsvData d : tableCsvData.data) {
                        writer.write(d);
                    }
                    writer.end(tableCsvData.table);
                }
                writer.end(batch, false);
            } catch (IgnoreBatchException ex) {
                writer.end(batch, false);
            } catch (Exception ex) {
                writer.end(batch, true);
                if (!isErrorExpected()) {
                    if (ex instanceof RuntimeException) {
                        throw (RuntimeException) ex;
                    } else {
                        throw new RuntimeException(ex);
                    }
                }
            }
        }
    } finally {
        writer.close();
    }
    long statementCount = 0;
    Collection<Statistics> stats = writer.getStatistics().values();
    for (Statistics statistics : stats) {
        statementCount += statistics.get(DataWriterStatisticConstants.STATEMENTCOUNT);
    }
    return statementCount;
}
Also used : DataContext(org.jumpmind.symmetric.io.data.DataContext) Batch(org.jumpmind.symmetric.io.data.Batch) IgnoreBatchException(org.jumpmind.symmetric.io.data.writer.IgnoreBatchException) Statistics(org.jumpmind.util.Statistics) CsvData(org.jumpmind.symmetric.io.data.CsvData) IgnoreBatchException(org.jumpmind.symmetric.io.data.writer.IgnoreBatchException)

Example 14 with Statistics

use of org.jumpmind.util.Statistics in project symmetric-ds by JumpMind.

the class StructureDataWriter method start.

public void start(Batch batch) {
    List<String> payloadData = new ArrayList<String>();
    this.currentBatch = batch.getBatchId();
    this.statistics.put(batch, new Statistics());
    this.payloadMap.put(currentBatch, payloadData);
}
Also used : ArrayList(java.util.ArrayList) Statistics(org.jumpmind.util.Statistics)

Example 15 with Statistics

use of org.jumpmind.util.Statistics in project symmetric-ds by JumpMind.

the class AbstractDatabaseWriter method start.

public void start(Batch batch) {
    this.batch = batch;
    this.statistics.put(batch, new Statistics());
}
Also used : Statistics(org.jumpmind.util.Statistics)

Aggregations

Statistics (org.jumpmind.util.Statistics)16 CsvData (org.jumpmind.symmetric.io.data.CsvData)6 IoException (org.jumpmind.exception.IoException)3 DataContext (org.jumpmind.symmetric.io.data.DataContext)3 DetectConflict (org.jumpmind.symmetric.io.data.writer.Conflict.DetectConflict)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Table (org.jumpmind.db.model.Table)2 AbstractWriterTest (org.jumpmind.symmetric.io.AbstractWriterTest)2 Batch (org.jumpmind.symmetric.io.data.Batch)2 DataEventType (org.jumpmind.symmetric.io.data.DataEventType)2 DataProcessor (org.jumpmind.symmetric.io.data.DataProcessor)2 IDataReader (org.jumpmind.symmetric.io.data.IDataReader)2 DataReaderStatistics (org.jumpmind.symmetric.io.data.reader.DataReaderStatistics)2 ResolveConflict (org.jumpmind.symmetric.io.data.writer.Conflict.ResolveConflict)2 IgnoreBatchException (org.jumpmind.symmetric.io.data.writer.IgnoreBatchException)2 IStagedResource (org.jumpmind.symmetric.io.stage.IStagedResource)2 Node (org.jumpmind.symmetric.model.Node)2 Test (org.junit.Test)2