/
CsvLoader.java
407 lines (350 loc) · 15.6 KB
/
CsvLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Eric Long <erilong@users.sourceforge.net>,
* Chris Henson <chenson42@users.sourceforge.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*/
package org.jumpmind.symmetric.load.csv;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jumpmind.symmetric.common.csv.CsvConstants;
import org.jumpmind.symmetric.db.BinaryEncoding;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.load.DataLoaderContext;
import org.jumpmind.symmetric.load.DataLoaderStatistics;
import org.jumpmind.symmetric.load.IColumnFilter;
import org.jumpmind.symmetric.load.IDataLoader;
import org.jumpmind.symmetric.load.IDataLoaderContext;
import org.jumpmind.symmetric.load.IDataLoaderFilter;
import org.jumpmind.symmetric.load.IDataLoaderStatistics;
import org.jumpmind.symmetric.load.TableTemplate;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.jdbc.core.JdbcTemplate;
import com.csvreader.CsvReader;
public class CsvLoader implements IDataLoader {
static final Log logger = LogFactory.getLog(CsvLoader.class);
protected JdbcTemplate jdbcTemplate;
protected IDbDialect dbDialect;
protected CsvReader csvReader;
protected DataLoaderContext context;
protected DataLoaderStatistics stats;
protected boolean enableFallbackInsert;
protected boolean enableFallbackUpdate;
protected boolean allowMissingDelete;
protected List<IDataLoaderFilter> filters;
protected Map<String, IColumnFilter> columnFilters;
public void open(BufferedReader reader) throws IOException {
csvReader = new CsvReader(reader);
csvReader.setEscapeMode(CsvReader.ESCAPE_MODE_BACKSLASH);
csvReader.setSafetySwitch(false);
context = new DataLoaderContext();
stats = new DataLoaderStatistics();
}
public void open(BufferedReader reader, List<IDataLoaderFilter> filters, Map<String, IColumnFilter> columnFilters)
throws IOException {
open(reader);
this.filters = filters;
this.columnFilters = columnFilters;
}
public boolean hasNext() throws IOException {
while (csvReader.readRecord()) {
String[] tokens = csvReader.getValues();
stats.incrementByteCount(csvReader.getRawRecord().length());
if (tokens[0].equals(CsvConstants.BATCH)) {
context.setBatchId(tokens[1]);
stats = new DataLoaderStatistics();
return true;
} else if (tokens[0].equals(CsvConstants.NODEID)) {
context.setNodeId(tokens[1]);
} else if (tokens[0].equals(CsvConstants.VERSION)) {
context.setVersion(tokens[1] + "." + tokens[2] + "." + tokens[3]);
} else if (isMetaTokenParsed(tokens)) {
continue;
} else {
throw new RuntimeException("Unexpected token '" + tokens[0] + "' while parsing for next batch");
}
}
return false;
}
public void skip() throws IOException {
context.setSkipping(true);
load();
// skipping is reset when a new batch_id is set
}
public void load() throws IOException {
BinaryEncoding encoding = BinaryEncoding.NONE;
while (csvReader.readRecord()) {
String[] tokens = csvReader.getValues();
stats.incrementLineCount();
stats.incrementByteCount(csvReader.getRawRecord().length());
if (tokens[0].equals(CsvConstants.INSERT)) {
if (!context.getTableTemplate().isIgnoreThisTable() && !context.isSkipping()) {
insert(tokens, encoding);
}
} else if (tokens[0].equals(CsvConstants.UPDATE)) {
if (!context.getTableTemplate().isIgnoreThisTable() && !context.isSkipping()) {
update(tokens, encoding);
}
} else if (tokens[0].equals(CsvConstants.DELETE)) {
if (!context.getTableTemplate().isIgnoreThisTable() && !context.isSkipping()) {
delete(tokens);
}
} else if (isMetaTokenParsed(tokens)) {
continue;
} else if (tokens[0].equals(CsvConstants.COMMIT)) {
break;
} else if (tokens[0].equals(CsvConstants.SQL)) {
if (!context.getTableTemplate().isIgnoreThisTable() && !context.isSkipping()) {
runSql(tokens[1]);
}
} else if (tokens[0].equals(CsvConstants.CREATE)) {
if (!context.isSkipping()) {
runDdl(tokens[1]);
}
} else if (tokens[0].equals(CsvConstants.BINARY)) {
try {
encoding = BinaryEncoding.valueOf(tokens[1]);
} catch (Exception ex) {
logger.warn("Unsupported binary encoding value of " + tokens[1]);
}
} else {
throw new RuntimeException("Unexpected token '" + tokens[0] + "' on line " + stats.getLineCount()
+ " of batch " + context.getBatchId());
}
}
}
protected boolean isMetaTokenParsed(String[] tokens) {
boolean isMetaTokenParsed = true;
if (tokens[0].equals(CsvConstants.TABLE)) {
setTable(tokens[1]);
} else if (tokens[0].equals(CsvConstants.KEYS)) {
context.setKeyNames((String[]) ArrayUtils.subarray(tokens, 1, tokens.length));
} else if (tokens[0].equals(CsvConstants.COLUMNS)) {
context.setColumnNames((String[]) ArrayUtils.subarray(tokens, 1, tokens.length));
} else {
isMetaTokenParsed = false;
}
return isMetaTokenParsed;
}
protected void setTable(String tableName) {
cleanupAfterDataLoad();
context.setTableName(tableName);
if (context.getTableTemplate() == null) {
context.setTableTemplate(new TableTemplate(jdbcTemplate, dbDialect, tableName,
this.columnFilters != null ? this.columnFilters.get(tableName) : null));
}
dbDialect.prepareTableForDataLoad(context.getTableTemplate().getTable());
}
protected void cleanupAfterDataLoad() {
if (context != null && context.getTableName() != null) {
dbDialect.cleanupAfterDataLoad(context.getTableTemplate().getTable());
}
}
protected int insert(String[] tokens, BinaryEncoding encoding) {
stats.incrementStatementCount();
String[] columnValues = parseColumns(tokens, 1);
int rows = 0;
if (filters != null) {
stats.startTimer();
for (IDataLoaderFilter filter : filters) {
filter.filterInsert(context, columnValues);
}
stats.incrementFilterMillis(stats.endTimer());
}
Object savepoint = null;
try {
stats.startTimer();
if (enableFallbackUpdate) {
savepoint = dbDialect.createSavepointForFallback();
}
rows = context.getTableTemplate().insert(columnValues, encoding);
dbDialect.releaseSavepoint(savepoint);
} catch (DataIntegrityViolationException e) {
// TODO: modify sql-error-codes.xml for unique constraint vs foreign key
if (enableFallbackUpdate) {
dbDialect.rollbackToSavepoint(savepoint);
if (logger.isDebugEnabled()) {
logger.debug("Unable to insert into " + context.getTableName() + ", updating instead: "
+ ArrayUtils.toString(tokens));
}
String keyValues[] = parseKeys(tokens, 1);
stats.incrementFallbackUpdateCount();
rows = context.getTableTemplate().update(columnValues, keyValues, encoding);
if (rows == 0) {
throw new RuntimeException("Unable to update " + context.getTableName() + ": "
+ ArrayUtils.toString(tokens), e);
}
} else {
// TODO: log the PK information as an ERROR level.
throw e;
}
} finally {
stats.incrementDatabaseMillis(stats.endTimer());
}
return rows;
}
protected int update(String[] tokens, BinaryEncoding encoding) {
stats.incrementStatementCount();
String columnValues[] = parseColumns(tokens, 1);
String keyValues[] = parseKeys(tokens, 1 + columnValues.length);
if (filters != null) {
stats.startTimer();
for (IDataLoaderFilter filter : filters) {
filter.filterUpdate(context, columnValues, keyValues);
}
stats.incrementFilterMillis(stats.endTimer());
}
stats.startTimer();
int rows = context.getTableTemplate().update(columnValues, keyValues, encoding);
if (rows == 0) {
if (enableFallbackInsert) {
if (logger.isDebugEnabled()) {
logger.debug("Unable to update " + context.getTableName() + ", inserting instead: "
+ ArrayUtils.toString(tokens));
}
stats.incrementFallbackInsertCount();
rows = context.getTableTemplate().insert(columnValues, encoding);
} else {
// TODO: log the PK information as an ERROR level.
stats.incrementDatabaseMillis(stats.endTimer());
throw new RuntimeException("Unable to update " + context.getTableName() + ": "
+ ArrayUtils.toString(tokens));
}
} else if (rows > 1) {
logger.warn("Too many rows (" + rows + ") updated for " + context.getTableName() + ": "
+ ArrayUtils.toString(tokens));
}
stats.incrementDatabaseMillis(stats.endTimer());
return rows;
}
protected int delete(String[] tokens) {
stats.incrementStatementCount();
String keyValues[] = parseKeys(tokens, 1);
if (filters != null) {
stats.startTimer();
for (IDataLoaderFilter filter : filters) {
filter.filterDelete(context, keyValues);
}
stats.incrementFilterMillis(stats.endTimer());
}
stats.startTimer();
int rows = context.getTableTemplate().delete(keyValues);
stats.incrementDatabaseMillis(stats.endTimer());
if (rows == 0) {
if (allowMissingDelete) {
logger
.warn("Delete of " + context.getTableName() + " affected no rows: "
+ ArrayUtils.toString(tokens));
stats.incrementMissingDeleteCount();
} else {
throw new RuntimeException("Delete of " + context.getTableName() + " affected no rows: "
+ ArrayUtils.toString(tokens));
}
}
return rows;
}
protected void runSql(String sql) {
stats.incrementStatementCount();
if (logger.isDebugEnabled()) {
logger.debug("Running SQL: " + sql);
}
jdbcTemplate.execute(sql);
}
protected void runDdl(String xml) {
stats.incrementStatementCount();
if (logger.isDebugEnabled()) {
logger.debug("Running DDL: " + xml);
}
dbDialect.createTables(xml);
context.getTableTemplate().resetMetaData();
// TODO Eric - why was this done here?
//dbDialect.prepareTableForDataLoad(context.getTableTemplate().getTable());
}
protected String[] parseKeys(String[] tokens, int startIndex) {
if (context.getTableTemplate().getKeyNames() == null) {
throw new RuntimeException("Key names were not specified for table "
+ context.getTableTemplate().getTableName());
}
int keyLength = context.getTableTemplate().getKeyNames().length;
return parseValues("key", tokens, startIndex, startIndex + keyLength);
}
protected String[] parseColumns(String[] tokens, int startIndex) {
if (context.getTableTemplate().getColumnNames() == null) {
throw new RuntimeException("Column names were not specified for table "
+ context.getTableTemplate().getTableName());
}
int columnLength = context.getTableTemplate().getColumnNames().length;
return parseValues("column", tokens, startIndex, startIndex + columnLength);
}
protected String[] parseValues(String name, String[] tokens, int startIndex, int endIndex) {
if (tokens.length < endIndex) {
throw new RuntimeException("Expected to have " + (endIndex - startIndex) + " " + name + " values for "
+ context.getTableTemplate().getTableName() + ": " + ArrayUtils.toString(tokens));
}
return (String[]) ArrayUtils.subarray(tokens, startIndex, endIndex);
}
public IDataLoader clone() {
CsvLoader dataLoader = new CsvLoader();
dataLoader.setJdbcTemplate(jdbcTemplate);
dataLoader.setDbDialect(dbDialect);
dataLoader.setEnableFallbackInsert(enableFallbackInsert);
dataLoader.setEnableFallbackUpdate(enableFallbackUpdate);
dataLoader.setAllowMissingDelete(allowMissingDelete);
return dataLoader;
}
public void close() {
cleanupAfterDataLoad();
if (csvReader != null) {
csvReader.close();
}
}
public IDataLoaderContext getContext() {
return context;
}
public IDataLoaderStatistics getStatistics() {
return stats;
}
public boolean isAllowMissingDelete() {
return allowMissingDelete;
}
public void setAllowMissingDelete(boolean allowMissingDelete) {
this.allowMissingDelete = allowMissingDelete;
}
public boolean isEnableFallbackInsert() {
return enableFallbackInsert;
}
public void setEnableFallbackInsert(boolean enableFallbackInsert) {
this.enableFallbackInsert = enableFallbackInsert;
}
public boolean isEnableFallbackUpdate() {
return enableFallbackUpdate;
}
public void setEnableFallbackUpdate(boolean enableFallbackUpdate) {
this.enableFallbackUpdate = enableFallbackUpdate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void setDbDialect(IDbDialect dbDialect) {
this.dbDialect = dbDialect;
}
}