Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
536 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
symmetric/symmetric-io/src/main/java/org/jumpmind/symmetric/io/FileIoResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.jumpmind.symmetric.io; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.InputStream; | ||
|
||
import org.jumpmind.exception.IoException; | ||
|
||
public class FileIoResource implements IoResource { | ||
|
||
File file; | ||
|
||
public FileIoResource(File file) { | ||
this.file = file; | ||
} | ||
|
||
public InputStream open() { | ||
try { | ||
return new FileInputStream(file); | ||
} catch (FileNotFoundException e) { | ||
throw new IoException(e); | ||
} | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
symmetric/symmetric-io/src/main/java/org/jumpmind/symmetric/io/IoResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.jumpmind.symmetric.io; | ||
|
||
import java.io.InputStream; | ||
|
||
public interface IoResource { | ||
|
||
public InputStream open(); | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
symmetric/symmetric-io/src/main/java/org/jumpmind/symmetric/io/MemoryIoResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.jumpmind.symmetric.io; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.InputStream; | ||
|
||
public class MemoryIoResource implements IoResource { | ||
|
||
byte[] buffer; | ||
|
||
public MemoryIoResource(byte[] buffer) { | ||
this.buffer = buffer; | ||
} | ||
|
||
public InputStream open() { | ||
return new ByteArrayInputStream(buffer); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
.../symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/reader/CapturedDataReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.jumpmind.symmetric.io.data.reader; | ||
|
||
public class CapturedDataReader { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...ric/symmetric-io/src/main/java/org/jumpmind/symmetric/io/data/reader/TableDataReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.jumpmind.symmetric.io.data.reader; | ||
|
||
public class TableDataReader { | ||
|
||
} |
181 changes: 181 additions & 0 deletions
181
...mmetric-io/src/main/java/org/jumpmind/symmetric/io/data/writer/AbstractCsvDataWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package org.jumpmind.symmetric.io.data.writer; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.jumpmind.db.BinaryEncoding; | ||
import org.jumpmind.db.model.Column; | ||
import org.jumpmind.db.model.Table; | ||
import org.jumpmind.symmetric.io.data.Batch; | ||
import org.jumpmind.symmetric.io.data.CsvConstants; | ||
import org.jumpmind.symmetric.io.data.CsvData; | ||
import org.jumpmind.symmetric.io.data.DataContext; | ||
import org.jumpmind.symmetric.io.data.IDataReader; | ||
import org.jumpmind.symmetric.io.data.IDataWriter; | ||
import org.jumpmind.util.Statistics; | ||
|
||
abstract public class AbstractCsvDataWriter implements IDataWriter { | ||
|
||
protected DataContext<? extends IDataReader, ? extends IDataWriter> context; | ||
|
||
protected Batch batch; | ||
|
||
protected Table table; | ||
|
||
protected Set<Table> processedTables = new HashSet<Table>(); | ||
|
||
protected String delimiter = ","; | ||
|
||
protected boolean flushNodeId = true; | ||
|
||
protected Map<Batch, Statistics> statistics = new HashMap<Batch, Statistics>(); | ||
|
||
protected List<ICsvDataWriterListener> listeners; | ||
|
||
public AbstractCsvDataWriter(List<ICsvDataWriterListener> listeners) { | ||
this.listeners = listeners; | ||
} | ||
|
||
public <R extends IDataReader, W extends IDataWriter> void open(DataContext<R, W> context) { | ||
this.context = context; | ||
} | ||
|
||
public void close() { | ||
} | ||
|
||
public void start(Batch batch) { | ||
this.statistics.put(batch, new Statistics()); | ||
this.batch = batch; | ||
|
||
if (listeners != null) { | ||
for (ICsvDataWriterListener listener : listeners) { | ||
listener.start(batch); | ||
} | ||
} | ||
|
||
if (flushNodeId) { | ||
String sourceNodeId = batch.getSourceNodeId(); | ||
if (StringUtils.isNotBlank(sourceNodeId)) { | ||
println(CsvConstants.NODEID, sourceNodeId); | ||
} | ||
BinaryEncoding binaryEncoding = batch.getBinaryEncoding(); | ||
if (binaryEncoding != null) { | ||
println(CsvConstants.BINARY, binaryEncoding.name()); | ||
} | ||
flushNodeId = false; | ||
} | ||
if (StringUtils.isNotBlank(batch.getChannelId())) { | ||
println(CsvConstants.CHANNEL, batch.getChannelId()); | ||
} | ||
println(CsvConstants.BATCH, Long.toString(batch.getBatchId())); | ||
} | ||
|
||
public boolean start(Table table) { | ||
this.table = table; | ||
String catalogName = table.getCatalog(); | ||
println(CsvConstants.CATALOG, StringUtils.isNotBlank(catalogName) ? catalogName : ""); | ||
String schemaName = table.getSchema(); | ||
println(CsvConstants.SCHEMA, StringUtils.isNotBlank(schemaName) ? schemaName : ""); | ||
println(CsvConstants.TABLE, table.getName()); | ||
if (!processedTables.contains(table)) { | ||
println(CsvConstants.KEYS, table.getPrimaryKeyColumns()); | ||
println(CsvConstants.COLUMNS, table.getColumns()); | ||
} | ||
this.processedTables.add(table); | ||
return true; | ||
} | ||
|
||
public void write(CsvData data) { | ||
switch (data.getDataEventType()) { | ||
case INSERT: | ||
println(CsvConstants.INSERT, data.getCsvData(CsvData.ROW_DATA)); | ||
break; | ||
|
||
case UPDATE: | ||
String oldData = data.getCsvData(CsvData.OLD_DATA); | ||
if (StringUtils.isNotBlank(oldData)) { | ||
println(CsvConstants.OLD, oldData); | ||
} | ||
println(CsvConstants.UPDATE, data.getCsvData(CsvData.ROW_DATA), | ||
data.getCsvData(CsvData.PK_DATA)); | ||
break; | ||
|
||
case DELETE: | ||
oldData = data.getCsvData(CsvData.OLD_DATA); | ||
if (StringUtils.isNotBlank(oldData)) { | ||
println(CsvConstants.OLD, oldData); | ||
} | ||
println(CsvConstants.DELETE, data.getCsvData(CsvData.PK_DATA)); | ||
break; | ||
|
||
case SQL: | ||
println(CsvConstants.SQL, data.getCsvData(CsvData.ROW_DATA)); | ||
break; | ||
} | ||
} | ||
|
||
public void end(Table table) { | ||
} | ||
|
||
final public void end(Batch batch, boolean inError) { | ||
println(CsvConstants.COMMIT, Long.toString(batch.getBatchId())); | ||
|
||
endBatch(batch); | ||
|
||
if (listeners != null) { | ||
for (ICsvDataWriterListener listener : listeners) { | ||
notifyEndBatch(batch, listener); | ||
} | ||
} | ||
} | ||
|
||
abstract protected void endBatch(Batch batch); | ||
|
||
abstract protected void notifyEndBatch(Batch batch, ICsvDataWriterListener listener); | ||
|
||
protected int println(String key, List<Column> columns) { | ||
return println(key, columns.toArray(new Column[columns.size()])); | ||
} | ||
|
||
protected int println(String key, Column[] columns) { | ||
StringBuilder buffer = new StringBuilder(key); | ||
for (int i = 0; i < columns.length; i++) { | ||
buffer.append(delimiter); | ||
buffer.append(columns[i].getName()); | ||
} | ||
println(buffer.toString()); | ||
return buffer.length(); | ||
} | ||
|
||
abstract protected void print(Batch batch, String data); | ||
|
||
protected int println(String... data) { | ||
StringBuilder buffer = new StringBuilder(); | ||
for (int i = 0; i < data.length; i++) { | ||
if (i != 0) { | ||
buffer.append(delimiter); | ||
} | ||
buffer.append(data[i]); | ||
} | ||
buffer.append("\n"); | ||
print(batch, buffer.toString()); | ||
return buffer.length(); | ||
} | ||
|
||
public void setDelimiter(String delimiter) { | ||
this.delimiter = delimiter; | ||
} | ||
|
||
public String getDelimiter() { | ||
return delimiter; | ||
} | ||
|
||
public Map<Batch, Statistics> getStatistics() { | ||
return statistics; | ||
} | ||
|
||
} |
Oops, something went wrong.