/
AbstractDatabaseWriter.java
560 lines (481 loc) · 21.4 KB
/
AbstractDatabaseWriter.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jumpmind.symmetric.io.data.writer;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.sql.SqlException;
import org.jumpmind.exception.ParseException;
import org.jumpmind.symmetric.io.IoConstants;
import org.jumpmind.symmetric.io.data.Batch;
import org.jumpmind.symmetric.io.data.CsvData;
import org.jumpmind.symmetric.io.data.DataContext;
import org.jumpmind.symmetric.io.data.DataEventType;
import org.jumpmind.symmetric.io.data.IDataWriter;
import org.jumpmind.symmetric.io.data.writer.Conflict.DetectConflict;
import org.jumpmind.util.Statistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import bsh.EvalError;
import bsh.Interpreter;
abstract public class AbstractDatabaseWriter implements IDataWriter {
protected final static Logger log = LoggerFactory.getLogger(AbstractDatabaseWriter.class);
public static final String CONFLICT_ERROR = "DatabaseWriter.ConflictError";
public static enum LoadStatus {
SUCCESS, CONFLICT
};
protected boolean lastUseConflictDetection = true;
protected boolean lastApplyChangesOnly = false;
protected Table sourceTable;
protected Table targetTable;
protected Map<String, Table> targetTables = new HashMap<String, Table>();
protected CsvData lastData;
protected Batch batch;
protected DataContext context;
protected long uncommittedCount = 0;
protected DatabaseWriterSettings writerSettings;
protected Map<Batch, Statistics> statistics = new HashMap<Batch, Statistics>();
protected IDatabaseWriterConflictResolver conflictResolver;
protected Set<String> missingTables = new HashSet<String>();
public AbstractDatabaseWriter() {
this(null, null);
}
public AbstractDatabaseWriter(DatabaseWriterSettings settings) {
this(null, settings);
}
public AbstractDatabaseWriter(
IDatabaseWriterConflictResolver conflictResolver, DatabaseWriterSettings settings) {
this.conflictResolver = conflictResolver == null ? new DefaultDatabaseWriterConflictResolver()
: conflictResolver;
this.writerSettings = settings == null ? new DatabaseWriterSettings() : settings;
}
public void open(DataContext context) {
this.context = context;
}
public void start(Batch batch) {
this.batch = batch;
this.statistics.put(batch, new Statistics());
this.statistics.get(batch).set(DataWriterStatisticConstants.STARTTIME, new Date().getTime());
}
public boolean start(Table table) {
if (table == null) {
throw new NullPointerException("Cannot load a null table");
}
this.lastData = null;
this.sourceTable = table;
this.targetTable = lookupTableAtTarget(this.sourceTable);
this.sourceTable.copyColumnTypesFrom(this.targetTable);
if (this.targetTable==null && hasFilterThatHandlesMissingTable(table)) {
this.targetTable = table;
}
/* The first data that requires a target table should fail because the table will not be found */
return true;
}
public void write(CsvData data) {
context.remove(AbstractDatabaseWriter.CONFLICT_ERROR);
/* If the startTable has been called and the targetTable is required then check
* to see if the writer has been configured to ignore this data event
*/
if (sourceTable != null && targetTable == null &&
data.requiresTable() && (writerSettings.isIgnoreMissingTables()
|| batch.getBatchId() == IoConstants.IGNORE_TABLES_BATCH)
&& !batch.getChannelId().equals("config")) {
String qualifiedName = sourceTable.getFullyQualifiedTableName();
if (!missingTables.contains(qualifiedName)) {
log.warn("Did not find the {} table in the target database", qualifiedName);
missingTables.add(qualifiedName);
}
} else {
context.put(CONFLICT_ERROR, null);
if (data.requiresTable() && sourceTable != null
&& targetTable == null && data.getDataEventType() != DataEventType.SQL ) {
Table lastTable = context.getLastParsedTable();
if (lastTable != null
&& lastTable.getFullyQualifiedTableNameLowerCase().equals(sourceTable.getFullyQualifiedTableNameLowerCase())) {
// if we cross batches and the table isn't specified, then
// use the last table we used
start(lastTable);
}
}
if (targetTable != null || !data.requiresTable()
|| (targetTable == null && data.getDataEventType() == DataEventType.SQL)) {
try {
statistics.get(batch).increment(DataWriterStatisticConstants.STATEMENTCOUNT);
statistics.get(batch).increment(DataWriterStatisticConstants.LINENUMBER);
if (filterBefore(data)) {
switch (data.getDataEventType()) {
case UPDATE:
case INSERT:
if (sourceTable.getColumnCount() != data.getParsedData(CsvData.ROW_DATA).length) {
throw new ParseException(String.format("The (%s) table's column count (%d) does not match the data's column count (%d)", sourceTable.getName(), sourceTable.getColumnCount(), data.getParsedData(CsvData.ROW_DATA).length));
}
break;
case DELETE:
if (sourceTable.getPrimaryKeyColumnCount() != data.getParsedData(CsvData.PK_DATA).length) {
throw new ParseException(String.format("The (%s) table's pk column count (%d) does not match the data's pk column count (%d)", sourceTable.getName(), sourceTable.getPrimaryKeyColumnCount(), data.getParsedData(CsvData.PK_DATA).length));
}
break;
default:
break;
}
LoadStatus loadStatus = LoadStatus.SUCCESS;
switch (data.getDataEventType()) {
case UPDATE:
loadStatus = update(data, writerSettings.isApplyChangesOnly(), true);
break;
case INSERT:
loadStatus = insert(data);
break;
case DELETE:
loadStatus = delete(data, true);
break;
case BSH:
script(data);
break;
case SQL:
sql(data);
break;
case CREATE:
create(data);
break;
default:
break;
}
if (loadStatus == LoadStatus.CONFLICT) {
if (conflictResolver != null) {
conflictResolver.needsResolved(this, data, loadStatus);
} else {
throw new ConflictException(data, targetTable, false,
writerSettings.pickConflict(targetTable, batch),
(Exception) context.get(AbstractDatabaseWriter.CONFLICT_ERROR));
}
}
uncommittedCount++;
lastData = data;
filterAfter(data);
checkForEarlyCommit();
}
} catch (IgnoreBatchException ex) {
rollback();
throw ex;
} catch (RuntimeException ex) {
if (filterError(data, ex)) {
if (!(ex instanceof SqlException)) {
/*
* SQL exceptions should have already been logged
*/
logFailureDetails(ex, data, false);
}
throw ex;
} else {
uncommittedCount++;
statistics.get(batch).increment(DataWriterStatisticConstants.IGNORECOUNT);
checkForEarlyCommit();
}
}
} else {
if (sourceTable != null) {
// If the source table was found but the target table is
// still unknown throw an exception
throw new SqlException(String.format("Could not find the target table %s",
sourceTable.getFullyQualifiedTableName()));
} else {
throw new SqlException("The target table was not specified");
}
}
}
}
protected void checkForEarlyCommit() {
if (uncommittedCount >= writerSettings.getMaxRowsBeforeCommit()) {
commit(true);
long sleep = writerSettings.getCommitSleepInterval();
if (sleep > 0) {
/*
* Chances are if SymmetricDS is configured to commit early in a
* batch we want to give other threads a chance to do work and
* access the database. This was added to support H2 clients
* that are loading big batches while an application is doing
* work.
*/
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
log.warn("{}", e.getMessage());
}
}
}
}
protected void commit(boolean earlyCommit) {
uncommittedCount = 0;
}
protected void rollback() {
uncommittedCount = 0;
}
protected boolean filterError(CsvData data, Exception ex) {
boolean process = true;
List<IDatabaseWriterErrorHandler> filters = this.writerSettings
.getDatabaseWriterErrorHandlers();
if (filters != null) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.FILTERMILLIS);
for (IDatabaseWriterErrorHandler filter : filters) {
process &= filter.handleError(context, targetTable, data, ex);
}
} finally {
statistics.get(batch).stopTimer(DataWriterStatisticConstants.FILTERMILLIS);
}
}
return process;
}
protected boolean filterBefore(CsvData data) {
boolean process = true;
List<IDatabaseWriterFilter> filters = this.writerSettings.getDatabaseWriterFilters();
if (filters != null) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.FILTERMILLIS);
for (IDatabaseWriterFilter filter : filters) {
process &= filter.beforeWrite(this.context, this.sourceTable, data);
}
// re-lookup target table in case the source table has changed
Table oldTargetTable = targetTable;
if (this.sourceTable!=null) {
targetTable = lookupTableAtTarget(this.sourceTable);
}
if (targetTable!=null && !targetTable.equals(oldTargetTable)) {
targetTableWasChangedByFilter(oldTargetTable);
}
} finally {
statistics.get(batch).stopTimer(DataWriterStatisticConstants.FILTERMILLIS);
}
}
return process;
}
protected void targetTableWasChangedByFilter(Table oldTargetTable) {
}
protected void notifyFiltersEarlyCommit() {
List<IDatabaseWriterFilter> filters = this.writerSettings.getDatabaseWriterFilters();
if (filters != null) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.FILTERMILLIS);
for (IDatabaseWriterFilter filter : filters) {
filter.earlyCommit(context);
}
} finally {
statistics.get(batch).stopTimer(DataWriterStatisticConstants.FILTERMILLIS);
}
}
}
protected void notifyFiltersBatchComplete() {
List<IDatabaseWriterFilter> filters = this.writerSettings.getDatabaseWriterFilters();
if (filters != null) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.FILTERMILLIS);
for (IDatabaseWriterFilter filter : filters) {
filter.batchComplete(context);
}
} finally {
statistics.get(batch).stopTimer(DataWriterStatisticConstants.FILTERMILLIS);
}
}
}
protected void notifyFiltersBatchCommitted() {
List<IDatabaseWriterFilter> filters = this.writerSettings.getDatabaseWriterFilters();
if (filters != null) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.FILTERMILLIS);
for (IDatabaseWriterFilter filter : filters) {
filter.batchCommitted(context);
}
} finally {
statistics.get(batch).stopTimer(DataWriterStatisticConstants.FILTERMILLIS);
}
}
}
protected void notifyFiltersBatchRolledback() {
List<IDatabaseWriterFilter> filters = this.writerSettings.getDatabaseWriterFilters();
if (filters != null) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.FILTERMILLIS);
for (IDatabaseWriterFilter filter : filters) {
filter.batchRolledback(context);
}
} finally {
statistics.get(batch).stopTimer(DataWriterStatisticConstants.FILTERMILLIS);
}
}
}
protected void filterAfter(CsvData data) {
List<IDatabaseWriterFilter> filters = this.writerSettings.getDatabaseWriterFilters();
if (filters != null) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.FILTERMILLIS);
for (IDatabaseWriterFilter filter : filters) {
filter.afterWrite(this.context, this.sourceTable, data);
}
} finally {
statistics.get(batch).stopTimer(DataWriterStatisticConstants.FILTERMILLIS);
}
}
}
protected abstract LoadStatus insert(CsvData data);
protected abstract LoadStatus delete(CsvData data, boolean useConflictDetection);
protected abstract LoadStatus update(CsvData data, boolean applyChangesOnly, boolean useConflictDetection);
protected abstract boolean create(CsvData data);
protected abstract boolean sql(CsvData data);
protected abstract void logFailureDetails(Throwable e, CsvData data, boolean logLastDmlDetails);
protected String[] getRowData(CsvData data, String dataType) {
String[] targetValues = new String[targetTable.getColumnCount()];
String[] targetColumnNames = targetTable.getColumnNames();
String[] originalValues = data.getParsedData(dataType);
String[] sourceColumnNames = sourceTable.getColumnNames();
if (originalValues != null) {
for (int i = 0; i < sourceColumnNames.length && i < originalValues.length; i++) {
for (int t = 0; t < targetColumnNames.length; t++) {
if (sourceColumnNames[i].equalsIgnoreCase(targetColumnNames[t])) {
targetValues[t] = originalValues[i];
break;
}
}
}
return targetValues;
} else {
return null;
}
}
protected void bindVariables(Map<String, Object> variables) {
variables.put("SOURCE_NODE_ID", batch.getSourceNodeId());
variables.put("TARGET_NODE_ID", batch.getTargetNodeId());
variables.put("log", log);
variables.putAll(context.getContext());
}
protected boolean script(CsvData data) {
try {
statistics.get(batch).startTimer(DataWriterStatisticConstants.LOADMILLIS);
String script = data.getParsedData(CsvData.ROW_DATA)[0];
Map<String, Object> variables = new HashMap<String, Object>();
bindVariables(variables);
Interpreter interpreter = new Interpreter();
if (variables != null) {
for (String variableName : variables.keySet()) {
interpreter.set(variableName, variables.get(variableName));
}
}
if (log.isDebugEnabled()) {
log.debug("About to run: {}", script);
}
interpreter.eval(script);
statistics.get(batch).increment(DataWriterStatisticConstants.SCRIPTCOUNT);
} catch (EvalError e) {
throw new RuntimeException(e);
}
return true;
}
protected Map<String, String> getLookupDataMap(CsvData data, Conflict conflict) {
Map<String, String> keyData = null;
if (data.getDataEventType() == DataEventType.INSERT) {
keyData = data.toColumnNameValuePairs(sourceTable.getColumnNames(), CsvData.ROW_DATA);
} else if (conflict.getDetectType() != DetectConflict.USE_PK_DATA) {
keyData = data.toColumnNameValuePairs(sourceTable.getColumnNames(), CsvData.OLD_DATA);
}
if (keyData == null || keyData.size() == 0) {
keyData = data.toKeyColumnValuePairs(sourceTable);
}
return keyData;
}
protected String getPkDataFor(CsvData data, Column column) {
String[] values = data.getParsedData(CsvData.PK_DATA);
if (values != null) {
Column[] columns = targetTable.getColumns();
int index = -1;
for (Column column2 : columns) {
if (column2.isPrimaryKey()) {
index++;
}
if (column2.equals(column)) {
return values[index];
}
}
} else {
return data.getParsedData(CsvData.ROW_DATA)[targetTable.getColumnIndex(column)];
}
return null;
}
public void end(Table table) {
}
public void end(Batch batch, boolean inError) {
this.lastData = null;
if (batch.isIgnored()) {
getStatistics().get(batch).increment(DataWriterStatisticConstants.IGNORECOUNT);
}
if (!inError) {
notifyFiltersBatchComplete();
commit(false);
} else {
rollback();
}
}
public void close() {
}
protected boolean hasFilterThatHandlesMissingTable(Table table) {
if (writerSettings.getDatabaseWriterFilters() != null) {
for (IDatabaseWriterFilter filter : writerSettings.getDatabaseWriterFilters()) {
if (filter.handlesMissingTable(context, table)) {
return true;
}
}
}
return false;
}
protected void allowInsertIntoAutoIncrementColumns(boolean value, Table table) {
}
protected Table lookupTableAtTarget(Table sourceTable) {
return sourceTable;
}
public Batch getBatch() {
return batch;
}
public DataContext getContext() {
return context;
}
public IDatabaseWriterConflictResolver getConflictResolver() {
return conflictResolver;
}
public void setConflictResolver(IDatabaseWriterConflictResolver conflictResolver) {
this.conflictResolver = conflictResolver;
}
public Table getTargetTable() {
return targetTable;
}
public Table getSourceTable() {
return sourceTable;
}
public Map<Batch, Statistics> getStatistics() {
return statistics;
}
public DatabaseWriterSettings getWriterSettings() {
return writerSettings;
}
}