use of org.jumpmind.symmetric.io.data.CsvData in project symmetric-ds by JumpMind.
the class TransformedData method buildTargetCsvData.
public CsvData buildTargetCsvData() {
CsvData data = new CsvData(this.targetDmlType);
if (targetDmlType != DataEventType.DELETE) {
data.putParsedData(CsvData.ROW_DATA, getColumnValues());
}
if (targetDmlType == DataEventType.UPDATE || targetDmlType == DataEventType.DELETE) {
data.putParsedData(CsvData.OLD_DATA, getOldColumnValues());
data.putParsedData(CsvData.PK_DATA, getKeyValues());
}
data.putAttribute(getClass().getName(), this);
return data;
}
use of org.jumpmind.symmetric.io.data.CsvData in project symmetric-ds by JumpMind.
the class AbstractDatabaseWriterConflictResolver method needsResolved.
public void needsResolved(AbstractDatabaseWriter writer, CsvData data, LoadStatus loadStatus) {
DataEventType originalEventType = data.getDataEventType();
DatabaseWriterSettings writerSettings = writer.getWriterSettings();
Conflict conflict = writerSettings.pickConflict(writer.getTargetTable(), writer.getBatch());
Statistics statistics = writer.getStatistics().get(writer.getBatch());
long statementCount = statistics.get(DataWriterStatisticConstants.STATEMENTCOUNT);
long lineNumber = statistics.get(DataWriterStatisticConstants.LINENUMBER);
ResolvedData resolvedData = writerSettings.getResolvedData(statementCount);
logConflictHappened(conflict, data, writer, resolvedData, lineNumber);
switch(originalEventType) {
case INSERT:
if (resolvedData != null) {
attemptToResolve(resolvedData, data, writer, conflict);
} else {
switch(conflict.getResolveType()) {
case FALLBACK:
performFallbackToUpdate(writer, data, conflict, true);
break;
case NEWER_WINS:
if ((conflict.getDetectType() == DetectConflict.USE_TIMESTAMP && isTimestampNewer(conflict, writer, data)) || (conflict.getDetectType() == DetectConflict.USE_VERSION && isVersionNewer(conflict, writer, data))) {
performFallbackToUpdate(writer, data, conflict, true);
} else {
if (!conflict.isResolveRowOnly()) {
throw new IgnoreBatchException();
}
}
break;
case IGNORE:
ignore(writer, conflict);
break;
case MANUAL:
default:
attemptToResolve(resolvedData, data, writer, conflict);
break;
}
}
break;
case UPDATE:
if (resolvedData != null) {
attemptToResolve(resolvedData, data, writer, conflict);
} else {
switch(conflict.getResolveType()) {
case FALLBACK:
if (conflict.getDetectType() == DetectConflict.USE_PK_DATA) {
CsvData withoutOldData = data.copyWithoutOldData();
try {
// we already tried to update using the pk
performFallbackToInsert(writer, withoutOldData, conflict, true);
} catch (ConflictException ex) {
performFallbackToUpdate(writer, withoutOldData, conflict, true);
}
} else {
try {
performFallbackToUpdate(writer, data, conflict, true);
} catch (ConflictException ex) {
performFallbackToInsert(writer, data, conflict, true);
}
}
break;
case NEWER_WINS:
if ((conflict.getDetectType() == DetectConflict.USE_TIMESTAMP && isTimestampNewer(conflict, writer, data)) || (conflict.getDetectType() == DetectConflict.USE_VERSION && isVersionNewer(conflict, writer, data))) {
try {
performFallbackToUpdate(writer, data, conflict, false);
} catch (ConflictException ex) {
performFallbackToInsert(writer, data, conflict, true);
}
} else {
if (!conflict.isResolveRowOnly()) {
throw new IgnoreBatchException();
}
}
break;
case IGNORE:
ignore(writer, conflict);
break;
case MANUAL:
default:
attemptToResolve(resolvedData, data, writer, conflict);
break;
}
}
break;
case DELETE:
switch(conflict.getResolveType()) {
case FALLBACK:
LoadStatus status = LoadStatus.CONFLICT;
if (conflict.getDetectType() != DetectConflict.USE_PK_DATA) {
status = writer.delete(data, false);
}
if (status == LoadStatus.CONFLICT) {
writer.getStatistics().get(writer.getBatch()).increment(DataWriterStatisticConstants.MISSINGDELETECOUNT);
}
break;
case IGNORE:
ignore(writer, conflict);
break;
case NEWER_WINS:
// nothing to do ...
break;
case MANUAL:
default:
if (resolvedData != null) {
if (!resolvedData.isIgnoreRow()) {
writer.delete(data, false);
} else {
if (!conflict.isResolveRowOnly()) {
throw new IgnoreBatchException();
}
}
} else {
throw new ConflictException(data, writer.getTargetTable(), false, conflict, (Exception) writer.getContext().get(AbstractDatabaseWriter.CONFLICT_ERROR));
}
break;
}
break;
default:
break;
}
logConflictResolution(conflict, data, writer, resolvedData, lineNumber);
}
use of org.jumpmind.symmetric.io.data.CsvData in project symmetric-ds by JumpMind.
the class DatabaseWriterTest method testUpdateDetectOldDataManual.
@Test
public void testUpdateDetectOldDataManual() {
Conflict setting = new Conflict();
setting.setConflictId("unit.test");
setting.setDetectType(DetectConflict.USE_OLD_DATA);
setting.setResolveRowOnly(false);
setting.setResolveChangesOnly(false);
setting.setResolveType(ResolveConflict.MANUAL);
writerSettings.setDefaultConflictSetting(setting);
String origId = getNextId();
String[] originalValues = massageExpectectedResultsForDialect(new String[] { origId, "string2", "changed value", "char2", "char not null2", "2007-01-02 03:20:10.000", "2012-03-12 07:00:00.000", "0", "2", "67.89", "-0.0747663" });
CsvData data = new CsvData(DataEventType.INSERT, originalValues);
writeData(data, originalValues);
String[] oldData = CollectionUtils.copyOfRange(originalValues, 0, originalValues.length);
oldData[2] = "original value";
oldData = massageExpectectedResultsForDialect(oldData);
String[] newData = CollectionUtils.copyOfRange(originalValues, 0, originalValues.length);
newData[2] = "new value";
newData = massageExpectectedResultsForDialect(newData);
CsvData update = new CsvData(DataEventType.UPDATE);
update.putParsedData(CsvData.ROW_DATA, newData);
update.putParsedData(CsvData.OLD_DATA, oldData);
try {
writeData(update);
Assert.fail("Should have received a conflict exception");
} catch (ConflictException ex) {
Statistics stats = lastDataWriterUsed.getStatistics().values().iterator().next();
long statementNumber = stats.get(DataWriterStatisticConstants.STATEMENTCOUNT);
ResolvedData resolvedData = new ResolvedData(statementNumber, update.getCsvData(CsvData.ROW_DATA), false);
writerSettings.setResolvedData(resolvedData);
writeData(update);
Map<String, Object> row = queryForRow(origId);
Assert.assertNotNull(row);
Assert.assertEquals(newData[2], row.get("string_required_value"));
}
}
use of org.jumpmind.symmetric.io.data.CsvData in project symmetric-ds by JumpMind.
the class DatabaseWriterTest method testStringSpaces.
@Test
public void testStringSpaces() throws Exception {
String[] values = new String[TEST_COLUMNS.length];
values[0] = getNextId();
values[1] = " two spaces before";
if (!(platform instanceof AseDatabasePlatform)) {
values[2] = "two spaces after ";
}
values[3] = " one space before";
values[4] = "one space after ";
writeData(new CsvData(DataEventType.INSERT, values), values);
}
use of org.jumpmind.symmetric.io.data.CsvData in project symmetric-ds by JumpMind.
the class DatabaseWriterTest method testUpdateDetectTimestampNewerWins.
@Test
public void testUpdateDetectTimestampNewerWins() {
Conflict setting = new Conflict();
setting.setConflictId("unit.test");
setting.setDetectType(DetectConflict.USE_TIMESTAMP);
setting.setDetectExpression("time_value");
setting.setResolveRowOnly(true);
setting.setResolveChangesOnly(true);
setting.setResolveType(ResolveConflict.NEWER_WINS);
writerSettings.setDefaultConflictSetting(setting);
String id = getNextId();
String[] originalValues = massageExpectectedResultsForDialect(new String[] { id, "string2", "string not null2", "char2", "char not null2", "2007-01-02 03:20:10.000", "2012-03-12 07:00:00.000", "0", "47", "67.89", "-0.0747663" });
CsvData data = new CsvData(DataEventType.INSERT, originalValues);
writeData(data, originalValues);
String[] updateShouldNotBeApplied = CollectionUtils.copyOfRange(originalValues, 0, originalValues.length);
updateShouldNotBeApplied[2] = "updated string";
updateShouldNotBeApplied[6] = "2012-03-12 06:00:00.0";
data = new CsvData(DataEventType.UPDATE, massageExpectectedResultsForDialect(updateShouldNotBeApplied));
writeData(data, originalValues);
String[] updateShouldBeApplied = CollectionUtils.copyOfRange(originalValues, 0, originalValues.length);
updateShouldBeApplied[2] = "string3";
updateShouldBeApplied[6] = "2012-03-12 08:00:00.000";
data = new CsvData(DataEventType.UPDATE, massageExpectectedResultsForDialect(updateShouldBeApplied));
writeData(data, updateShouldBeApplied);
}
Aggregations