-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQueryBot.java
More file actions
153 lines (136 loc) · 4.84 KB
/
QueryBot.java
File metadata and controls
153 lines (136 loc) · 4.84 KB
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
/**
* Class for executing queries against an Oracle DB
*
* @author Brad Mann brad.mann@marklogic.com
* @author Steven Brockman steven.brockman@marklogic.com
*/
package com.marklogic.rowbot;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Properties;
import java.util.Iterator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.json.JSONObject;
import com.marklogic.xcc.Session;
class QueryBot implements Runnable {
static final double NANO_TIME_DIV = 1000000000.0;
private JSONObject queryObject;
protected ExecutorService insertPool;
private RowBot rowBot;
private Session session;
private Connection connection;
private JSONObject connectionObj;
protected static final int INSERT_QUEUE_SIZE = 1000000;
protected LinkedBlockingQueue<Runnable> insertQueue;
protected static final List<Runnable> activeTasks = Collections.synchronizedList(new ArrayList<Runnable>());
public QueryBot(JSONObject connectionObj, JSONObject queryObject, RowBot rowBot) {
this.queryObject = queryObject;
this.rowBot = rowBot;
this.connectionObj = connectionObj;
this.insertQueue = new LinkedBlockingQueue<Runnable>(INSERT_QUEUE_SIZE);
this.insertPool = new ThreadPoolExecutor(rowBot.numInsertThreads, rowBot.numInsertThreads, 0L, TimeUnit.MILLISECONDS, insertQueue);
}
protected void queryComplete(boolean success, String queryId, String query, int resultCount, double queryTime, String message) {
String uri = rowBot.uriPrefix + rowBot.timestamp + "/" + queryId + "/rowbot-query-report.json";
JSONObject results = new JSONObject();
results.put("success", success);
results.put("queryId", queryId);
results.put("queryTime", queryTime);
results.put("query", query);
results.put("totalRows", resultCount);
if (message != null) {
results.put("message", message);
}
rowBot.insertJSONDoc(this.session, uri, results.toString(), null, new String[] {"rowbot-query-report"});
synchronized(rowBot.queryReportUris) {
rowBot.queryReportUris.add(uri);
}
}
public void run() {
activeTasks.add(this);
String connectionString = connectionObj.getString("connectionString");
this.session = rowBot.contentSource.newSession();
try {
Properties props = new Properties();
JSONObject propsObject = connectionObj.getJSONObject("properties");
Iterator<String> keys = propsObject.keys();
while (keys.hasNext()) {
String key = keys.next();
props.put(key, propsObject.getString(key));
}
this.connection = DriverManager.getConnection(connectionString, props);
} catch (SQLException e) {
this.queryComplete(false, this.queryObject.getString("queryId"), this.queryObject.getString("query"), 0, 0, e.getMessage());
return;
}
double queryTime = 0;
long queryStart = System.nanoTime();
int rowNum = 0;
boolean success = true;
String successMsg = null;
try {
String query = this.queryObject.getString("query");
try (
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(query);)
{
ResultSetMetaData rsm = rs.getMetaData();
int numColumns = rsm.getColumnCount();
while (rs.next()) {
rowNum++;
Map<String, Object> rowMap = new HashMap<>();
for (int i = 1; i <= numColumns; i++) {
if (rsm.getColumnType(i) == Types.BLOB) {
rowMap.put(rsm.getColumnName(i), rs.getBlob(i).getBinaryStream());
} else {
rowMap.put(rsm.getColumnName(i), rs.getString(i));
}
}
Runnable insertThread = new InsertBot(rowMap, queryObject, rowNum, this.rowBot);
insertPool.execute(insertThread);
}
} catch (Exception e) {
System.out.println("Error with Query: " + query);
e.printStackTrace();
success = false;
successMsg = e.toString();
}
//signals pool to not accept more tasks.
insertPool.shutdown();
while (!insertPool.isTerminated()) {
Thread.sleep(500);
}
long queryEnd = System.nanoTime();
queryTime = (queryEnd - queryStart) / QueryBot.NANO_TIME_DIV;
} catch (Exception e) {
e.printStackTrace();
success = false;
successMsg = e.toString();
} finally {
this.queryComplete(success, this.queryObject.getString("queryId"), this.queryObject.getString("query"), rowNum, queryTime, successMsg);
// NOTE: connections are now closed in RowBot at the END.
session.close();
try {
if (this.connection != null && this.connection.isValid(4)) {
this.connection.close();
}
} catch (SQLException e) {
// swallow
}
activeTasks.remove(this);
}
}
}