/
DataExtractorService.java
300 lines (251 loc) · 11.9 KB
/
DataExtractorService.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
/*
* SymmetricDS is an open source database synchronization solution.
*
* Copyright (C) Chris Henson <chenson42@users.sourceforge.net>,
* Eric Long <erilong@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.service.impl;
import java.io.BufferedWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.db.IDbDialect;
import org.jumpmind.symmetric.extract.DataExtractorContext;
import org.jumpmind.symmetric.extract.IDataExtractor;
import org.jumpmind.symmetric.model.BatchType;
import org.jumpmind.symmetric.model.Node;
import org.jumpmind.symmetric.model.Data;
import org.jumpmind.symmetric.model.DataEventType;
import org.jumpmind.symmetric.model.OutgoingBatch;
import org.jumpmind.symmetric.model.TriggerHistory;
import org.jumpmind.symmetric.model.Trigger;
import org.jumpmind.symmetric.service.IConfigurationService;
import org.jumpmind.symmetric.service.IDataExtractorService;
import org.jumpmind.symmetric.service.IExtractListener;
import org.jumpmind.symmetric.service.IOutgoingBatchService;
import org.jumpmind.symmetric.transport.IOutgoingTransport;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.JdbcUtils;
public class DataExtractorService implements IDataExtractorService {
private IOutgoingBatchService outgoingBatchService;
private IConfigurationService configurationService;
private IDataExtractor dataExtractor;
private IDbDialect dbDialect;
private JdbcTemplate jdbcTemplate;
private DataExtractorContext context;
private String tablePrefix;
private String selectEventDataToExtractSql;
public void extractClientIdentityFor(Node node, IOutgoingTransport transport) {
String tableName = tablePrefix + "_node_identity";
OutgoingBatch batch = new OutgoingBatch(node, Constants.CHANNEL_CONFIG,
BatchType.INITIAL_LOAD);
outgoingBatchService.insertOutgoingBatch(batch);
try {
BufferedWriter writer = transport.open();
DataExtractorContext ctxCopy = context.copy();
dataExtractor.init(writer, ctxCopy);
dataExtractor.begin(batch, writer);
TriggerHistory audit = new TriggerHistory(tableName, "node_id",
"node_id");
Data data = new Data(1, null, node.getNodeId(),
DataEventType.INSERT, tableName, null, audit);
dataExtractor.write(writer, data, ctxCopy);
dataExtractor.commit(batch, writer);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public OutgoingBatch extractInitialLoadFor(Node node, final Trigger trigger,
final IOutgoingTransport transport) {
OutgoingBatch batch = new OutgoingBatch(node, trigger.getChannelId(), BatchType.INITIAL_LOAD);
outgoingBatchService.insertOutgoingBatch(batch);
writeInitialLoad(node, trigger, transport, batch);
outgoingBatchService.markOutgoingBatchSent(batch);
return batch;
}
public void extractInitialLoadWithinBatchFor(Node node, final Trigger trigger,
final IOutgoingTransport transport) {
writeInitialLoad(node, trigger, transport, null);
}
protected void writeInitialLoad(Node node, final Trigger trigger,
final IOutgoingTransport transport, final OutgoingBatch batch) {
final String sql = dbDialect.createInitalLoadSqlFor(node, trigger);
final TriggerHistory audit = configurationService.getLatestHistoryRecordFor(trigger.getTriggerId());
jdbcTemplate.execute(new ConnectionCallback() {
public Object doInConnection(Connection conn) throws SQLException, DataAccessException {
try {
PreparedStatement st = conn.prepareStatement(sql,
java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
st.setFetchSize(dbDialect.getStreamingResultsFetchSize());
ResultSet rs = st.executeQuery();
final BufferedWriter writer = transport.open();
final DataExtractorContext ctxCopy = context.copy();
if (batch != null) {
dataExtractor.init(writer, ctxCopy);
dataExtractor.begin(batch, writer);
}
while (rs.next()) {
dataExtractor.write(writer, new Data(0, null, rs.getString(1), DataEventType.INSERT,
trigger.getSourceTableName(), null, audit), ctxCopy);
}
if (batch != null) {
dataExtractor.commit(batch, writer);
}
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(st);
return null;
} catch (Exception e) {
throw new RuntimeException("Error during SQL: " + sql, e);
}
}
});
}
public boolean extract(Node node, IOutgoingTransport transport)
throws Exception {
ExtractStreamHandler handler = new ExtractStreamHandler(transport);
return extract(node, handler);
}
/**
* Allow a handler callback to do the work so we can route the extracted data to
* other types of handlers for processing.
*/
public boolean extract(Node node, final IExtractListener handler)
throws Exception {
outgoingBatchService.buildOutgoingBatches(node.getNodeId());
List<OutgoingBatch> batches = outgoingBatchService
.getOutgoingBatches(node.getNodeId());
if (batches != null && batches.size() > 0) {
try {
handler.init();
for (final OutgoingBatch batch : batches) {
handler.startBatch(batch);
jdbcTemplate.execute(new ConnectionCallback() {
public Object doInConnection(Connection conn)
throws SQLException, DataAccessException {
PreparedStatement statement = conn
.prepareStatement(
selectEventDataToExtractSql,
java.sql.ResultSet.TYPE_FORWARD_ONLY,
java.sql.ResultSet.CONCUR_READ_ONLY);
statement.setFetchSize(dbDialect
.getStreamingResultsFetchSize());
statement.setString(1, batch.getNodeId());
statement.setString(2, batch.getBatchId());
ResultSet results = statement.executeQuery();
while (results.next()) {
try {
handler.dataExtracted(next(results));
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
JdbcUtils.closeResultSet(results);
JdbcUtils.closeStatement(statement);
return null;
}
});
handler.endBatch(batch);
// At this point, we've already sent the data to the client, so if
// updating the batch to 'sent' fails, all this means is that the batch
// will be sent to the client again. This is expected to happen so
// infrequently, that any ineffiencies associated with resending a batch
// are negligible.
outgoingBatchService.markOutgoingBatchSent(batch);
}
} finally {
handler.done();
}
return true;
} else {
return false;
}
}
private Data next(ResultSet results) throws SQLException {
long dataId = results.getLong(1);
String tableName = results.getString(2);
DataEventType eventType = DataEventType.getEventType(results
.getString(3));
String rowData = results.getString(4);
String pk = results.getString(5);
Date created = results.getDate(7);
TriggerHistory audit = configurationService.getHistoryRecordFor(results
.getInt(8));
return new Data(dataId, pk, rowData, eventType, tableName, created,
audit);
}
public void setOutgoingBatchService(
IOutgoingBatchService batchBuilderService) {
this.outgoingBatchService = batchBuilderService;
}
public void setDataExtractor(IDataExtractor dataExtractor) {
this.dataExtractor = dataExtractor;
}
public void setContext(DataExtractorContext context) {
this.context = context;
}
public void setDbDialect(IDbDialect dialect) {
this.dbDialect = dialect;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void setConfigurationService(
IConfigurationService configurationService) {
this.configurationService = configurationService;
}
public void setSelectEventDataToExtractSql(
String selectEventDataToExtractSql) {
this.selectEventDataToExtractSql = selectEventDataToExtractSql;
}
class ExtractStreamHandler implements IExtractListener {
IOutgoingTransport transport;
DataExtractorContext context;
BufferedWriter writer;
ExtractStreamHandler(IOutgoingTransport transport) throws Exception {
this.transport = transport;
}
public void dataExtracted(Data data) throws Exception {
dataExtractor.write(writer, data, context);
}
public void done() throws IOException {
}
public void endBatch(OutgoingBatch batch) throws Exception {
dataExtractor.commit(batch, writer);
}
public void init() throws Exception {
this.writer = transport.open();
this.context = DataExtractorService.this.context.copy();
dataExtractor.init(writer, context);
}
public void startBatch(OutgoingBatch batch) throws Exception {
context.setBatch(batch);
dataExtractor.begin(batch, writer);
}
}
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}
}