Search in sources :

Example 11 with DataEventType

use of org.jumpmind.symmetric.io.data.DataEventType in project symmetric-ds by JumpMind.

the class PostgresBulkDatabaseWriter method write.

public void write(CsvData data) {
    statistics.get(batch).increment(DataWriterStatisticConstants.STATEMENTCOUNT);
    statistics.get(batch).increment(DataWriterStatisticConstants.LINENUMBER);
    statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
    DataEventType dataEventType = data.getDataEventType();
    if (targetTable != null || dataEventType.equals(DataEventType.CREATE)) {
        needsBinaryConversion = false;
        if (!batch.getBinaryEncoding().equals(BinaryEncoding.NONE) && targetTable != null) {
            for (Column column : targetTable.getColumns()) {
                if (column.isOfBinaryType()) {
                    needsBinaryConversion = true;
                    break;
                }
            }
        }
        switch(dataEventType) {
            case INSERT:
                startCopy();
                try {
                    String[] parsedData = data.getParsedData(CsvData.ROW_DATA);
                    if (needsBinaryConversion) {
                        Column[] columns = targetTable.getColumns();
                        for (int i = 0; i < columns.length; i++) {
                            if (columns[i].isOfBinaryType() && parsedData[i] != null) {
                                if (batch.getBinaryEncoding().equals(BinaryEncoding.HEX)) {
                                    parsedData[i] = encode(Hex.decodeHex(parsedData[i].toCharArray()));
                                } else if (batch.getBinaryEncoding().equals(BinaryEncoding.BASE64)) {
                                    parsedData[i] = encode(Base64.decodeBase64(parsedData[i].getBytes()));
                                }
                            }
                        }
                    }
                    String formattedData = CsvUtils.escapeCsvData(parsedData, '\n', '\'', CsvWriter.ESCAPE_MODE_DOUBLED);
                    byte[] dataToLoad = formattedData.getBytes();
                    copyIn.writeToCopy(dataToLoad, 0, dataToLoad.length);
                    loadedRows++;
                } catch (Exception ex) {
                    throw getPlatform().getSqlTemplate().translate(ex);
                }
                break;
            case UPDATE:
            case DELETE:
            default:
                endCopy();
                super.write(data);
                break;
        }
        if (loadedRows >= maxRowsBeforeFlush) {
            flush();
            loadedRows = 0;
        }
    }
    statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
}
Also used : Column(org.jumpmind.db.model.Column) DataEventType(org.jumpmind.symmetric.io.data.DataEventType) SQLException(java.sql.SQLException)

Example 12 with DataEventType

use of org.jumpmind.symmetric.io.data.DataEventType in project symmetric-ds by JumpMind.

the class OracleBulkDatabaseWriter method write.

public void write(CsvData data) {
    DataEventType dataEventType = data.getDataEventType();
    if (lastEventType != null && !lastEventType.equals(dataEventType)) {
        flush();
    }
    lastEventType = dataEventType;
    boolean requiresFlush = false;
    switch(dataEventType) {
        case INSERT:
            statistics.get(batch).increment(DataWriterStatisticConstants.STATEMENTCOUNT);
            statistics.get(batch).increment(DataWriterStatisticConstants.LINENUMBER);
            if (filterBefore(data)) {
                Object[] rowData = platform.getObjectValues(batch.getBinaryEncoding(), getRowData(data, CsvData.ROW_DATA), targetTable.getColumns(), false, writerSettings.isFitToColumn());
                rowData = convertObjectValues(rowData, targetTable.getColumns());
                for (int i = 0; i < rowData.length; i++) {
                    List<Object> columnList = null;
                    if (rowArrays.size() > i) {
                        columnList = rowArrays.get(i);
                    } else {
                        columnList = new ArrayList<Object>();
                        rowArrays.add(columnList);
                    }
                    columnList.add(rowData[i]);
                    if (columnList.size() >= maxRowsBeforeFlush) {
                        requiresFlush = true;
                    }
                }
                uncommittedCount++;
            }
            break;
        case UPDATE:
            flush();
            super.write(data);
            break;
        case DELETE:
            flush();
            super.write(data);
            break;
        default:
            flush();
            super.write(data);
            break;
    }
    if (requiresFlush) {
        flush();
    }
    checkForEarlyCommit();
}
Also used : DataEventType(org.jumpmind.symmetric.io.data.DataEventType)

Example 13 with DataEventType

use of org.jumpmind.symmetric.io.data.DataEventType in project symmetric-ds by JumpMind.

the class FileSyncZipDataWriter method write.

public void write(CsvData data) {
    DataEventType eventType = data.getDataEventType();
    if (eventType == DataEventType.INSERT || eventType == DataEventType.UPDATE) {
        Map<String, String> columnData = data.toColumnNameValuePairs(snapshotTable.getColumnNames(), CsvData.ROW_DATA);
        Map<String, String> oldColumnData = data.toColumnNameValuePairs(snapshotTable.getColumnNames(), CsvData.OLD_DATA);
        FileSnapshot snapshot = new FileSnapshot();
        snapshot.setTriggerId(columnData.get("TRIGGER_ID"));
        snapshot.setRouterId(columnData.get("ROUTER_ID"));
        snapshot.setFileModifiedTime(Long.parseLong(columnData.get("FILE_MODIFIED_TIME")));
        snapshot.setCrc32Checksum(Long.parseLong(columnData.get("CRC32_CHECKSUM")));
        String oldChecksum = oldColumnData.get("CRC32_CHECKSUM");
        if (StringUtils.isNotBlank(oldChecksum)) {
            snapshot.setOldCrc32Checksum(Long.parseLong(oldChecksum));
        }
        snapshot.setFileSize(Long.parseLong(columnData.get("FILE_SIZE")));
        snapshot.setLastUpdateBy(columnData.get("LAST_UPDATE_BY"));
        snapshot.setFileName(columnData.get("FILE_NAME"));
        snapshot.setRelativeDir(columnData.get("RELATIVE_DIR"));
        snapshot.setLastEventType(LastEventType.fromCode(columnData.get("LAST_EVENT_TYPE")));
        snapshotEvents.add(snapshot);
    } else if (eventType == DataEventType.RELOAD) {
        String targetNodeId = context.getBatch().getTargetNodeId();
        Node targetNode = nodeService.findNode(targetNodeId);
        List<FileTriggerRouter> fileTriggerRouters = fileSyncService.getFileTriggerRoutersForCurrentNode();
        for (FileTriggerRouter fileTriggerRouter : fileTriggerRouters) {
            if (fileTriggerRouter.isEnabled() && fileTriggerRouter.isInitialLoadEnabled() && fileTriggerRouter.getRouter().getNodeGroupLink().getTargetNodeGroupId().equals(targetNode.getNodeGroupId())) {
                DirectorySnapshot directorySnapshot = fileSyncService.getDirectorySnapshot(fileTriggerRouter);
                snapshotEvents.addAll(directorySnapshot);
            }
        }
    }
}
Also used : FileSnapshot(org.jumpmind.symmetric.model.FileSnapshot) FileTriggerRouter(org.jumpmind.symmetric.model.FileTriggerRouter) Node(org.jumpmind.symmetric.model.Node) DataEventType(org.jumpmind.symmetric.io.data.DataEventType) ArrayList(java.util.ArrayList) List(java.util.List)

Example 14 with DataEventType

use of org.jumpmind.symmetric.io.data.DataEventType in project symmetric-ds by JumpMind.

the class AuditTableDataRouter method routeToNodes.

public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
    DataEventType eventType = dataMetaData.getData().getDataEventType();
    if (eventType == DataEventType.INSERT || eventType == DataEventType.UPDATE || eventType == DataEventType.DELETE) {
        IParameterService parameterService = engine.getParameterService();
        IDatabasePlatform platform = engine.getDatabasePlatform();
        TriggerHistory triggerHistory = dataMetaData.getTriggerHistory();
        Table table = dataMetaData.getTable().copyAndFilterColumns(triggerHistory.getParsedColumnNames(), triggerHistory.getParsedPkColumnNames(), true);
        String tableName = table.getFullyQualifiedTableName();
        Table auditTable = auditTables.get(tableName);
        if (auditTable == null) {
            auditTable = toAuditTable(table);
            if (parameterService.is(ParameterConstants.AUTO_CONFIGURE_DATABASE)) {
                platform.alterTables(true, auditTable);
            }
            auditTable = platform.getTableFromCache(auditTable.getCatalog(), auditTable.getSchema(), auditTable.getName(), false);
            auditTables.put(tableName, auditTable);
        }
        DatabaseInfo dbInfo = platform.getDatabaseInfo();
        String auditTableName = auditTable.getQualifiedTableName(dbInfo.getDelimiterToken(), dbInfo.getCatalogSeparator(), dbInfo.getSchemaSeparator());
        ISqlTemplate template = platform.getSqlTemplate();
        Map<String, Object> values = null;
        if (eventType != DataEventType.DELETE) {
            values = new HashMap<String, Object>(getNewDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
        } else {
            values = new HashMap<String, Object>(getOldDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
        }
        Long sequence = (Long) context.get(auditTableName);
        if (sequence == null) {
            sequence = 1l + template.queryForLong(String.format("select max(%s) from %s", auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), auditTableName));
        } else {
            sequence = 1l + sequence;
        }
        context.put(auditTable.getName(), sequence);
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), sequence);
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_TIME).getName(), new Date());
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_EVENT).getName(), eventType.getCode());
        DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, auditTable, null);
        int[] types = statement.getTypes();
        Object[] args = statement.getValueArray(values);
        String sql = statement.getSql();
        template.update(sql, args, types);
    }
    return null;
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) DataEventType(org.jumpmind.symmetric.io.data.DataEventType) IParameterService(org.jumpmind.symmetric.service.IParameterService) Date(java.util.Date) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) DmlStatement(org.jumpmind.db.sql.DmlStatement)

Example 15 with DataEventType

use of org.jumpmind.symmetric.io.data.DataEventType in project symmetric-ds by JumpMind.

the class AbstractDataRouter method getDataObjectMap.

protected Map<String, Object> getDataObjectMap(DataMetaData dataMetaData, ISymmetricDialect symmetricDialect, boolean upperCase) {
    Map<String, Object> data = null;
    DataEventType dml = dataMetaData.getData().getDataEventType();
    switch(dml) {
        case UPDATE:
            data = new LinkedCaseInsensitiveMap<Object>(dataMetaData.getTable().getColumnCount() * 2);
            data.putAll(getNewDataAsObject(null, dataMetaData, symmetricDialect, upperCase));
            data.putAll(getOldDataAsObject(OLD_, dataMetaData, symmetricDialect, upperCase));
            break;
        case INSERT:
            data = new LinkedCaseInsensitiveMap<Object>(dataMetaData.getTable().getColumnCount() * 2);
            data.putAll(getNewDataAsObject(null, dataMetaData, symmetricDialect, upperCase));
            Map<String, Object> map = getNullData(OLD_, dataMetaData);
            data.putAll(map);
            break;
        case DELETE:
            data = new LinkedCaseInsensitiveMap<Object>(dataMetaData.getTable().getColumnCount() * 2);
            data.putAll(getOldDataAsObject(null, dataMetaData, symmetricDialect, upperCase));
            data.putAll(getOldDataAsObject(OLD_, dataMetaData, symmetricDialect, upperCase));
            if (data.size() == 0) {
                data.putAll(getPkDataAsObject(dataMetaData, symmetricDialect));
            }
            break;
        default:
            break;
    }
    if (data != null && data.size() == 0) {
        data.putAll(getPkDataAsString(dataMetaData, symmetricDialect));
    }
    if (StringUtils.isNotBlank(dataMetaData.getData().getExternalData())) {
        if (data == null) {
            data = new LinkedCaseInsensitiveMap<Object>(1);
        }
        data.put("EXTERNAL_DATA", dataMetaData.getData().getExternalData());
    } else if (data != null) {
        data.put("EXTERNAL_DATA", null);
    }
    return data;
}
Also used : DataEventType(org.jumpmind.symmetric.io.data.DataEventType)

Aggregations

DataEventType (org.jumpmind.symmetric.io.data.DataEventType)16 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)3 Column (org.jumpmind.db.model.Column)3 Table (org.jumpmind.db.model.Table)2 CsvData (org.jumpmind.symmetric.io.data.CsvData)2 IgnoreColumnException (org.jumpmind.symmetric.io.data.transform.IgnoreColumnException)2 IgnoreRowException (org.jumpmind.symmetric.io.data.transform.IgnoreRowException)2 NewAndOldValue (org.jumpmind.symmetric.io.data.transform.NewAndOldValue)2 TransformColumn (org.jumpmind.symmetric.io.data.transform.TransformColumn)2 IncludeOnType (org.jumpmind.symmetric.io.data.transform.TransformColumn.IncludeOnType)2 TransformedData (org.jumpmind.symmetric.io.data.transform.TransformedData)2 Statistics (org.jumpmind.util.Statistics)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonServiceException (com.amazonaws.AmazonServiceException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Date (java.util.Date)1