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;
}
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);
}
}
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);
}
}
use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.
the class SymmetricPushClient method open.
public void open() {
try {
transport = new HttpOutgoingTransport(new URL(buildUrl()), 30000, true, 0, -1, null, null, false, -1, false);
writer = new ProtocolDataWriter(nodeId, transport.openWriter(), false);
writer.start(batch);
} catch (Exception ex) {
throw new IoException(ex);
}
}
use of org.jumpmind.exception.IoException in project symmetric-ds by JumpMind.
the class StagedResource method getReader.
public synchronized BufferedReader getReader() {
Thread thread = Thread.currentThread();
BufferedReader reader = readers != null ? readers.get(thread) : null;
if (reader == null) {
if (file != null && file.exists()) {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), IoConstants.ENCODING));
createReadersMap();
readers.put(thread, reader);
} catch (IOException ex) {
throw new IoException(ex);
}
} else if (memoryBuffer != null && memoryBuffer.length() > 0) {
reader = new BufferedReader(new StringReader(memoryBuffer.toString()));
createReadersMap();
readers.put(thread, reader);
} else {
throw new IllegalStateException("There is no content to read. Memory buffer was empty and " + file.getAbsolutePath() + " was not found.");
}
}
return reader;
}
Aggregations