-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Bsonify.java
258 lines (238 loc) · 9.17 KB
/
Bsonify.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
package com.youtube.vitess.vtgate.rpcclient.gorpc;
import com.google.common.primitives.Ints;
import com.google.common.primitives.UnsignedLong;
import com.youtube.vitess.vtgate.BatchQuery;
import com.youtube.vitess.vtgate.BatchQueryResponse;
import com.youtube.vitess.vtgate.BindVariable;
import com.youtube.vitess.vtgate.Field;
import com.youtube.vitess.vtgate.Field.Flag;
import com.youtube.vitess.vtgate.KeyRange;
import com.youtube.vitess.vtgate.KeyspaceId;
import com.youtube.vitess.vtgate.Query;
import com.youtube.vitess.vtgate.Query.QueryBuilder;
import com.youtube.vitess.vtgate.QueryResponse;
import com.youtube.vitess.vtgate.QueryResult;
import com.youtube.vitess.vtgate.Row;
import com.youtube.vitess.vtgate.Row.Cell;
import com.youtube.vitess.vtgate.SplitQueryRequest;
import com.youtube.vitess.vtgate.SplitQueryResponse;
import org.apache.commons.codec.binary.Hex;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.bson.types.BasicBSONList;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class Bsonify {
public static BSONObject queryToBson(Query query) {
BSONObject b = new BasicBSONObject();
b.put("Sql", query.getSql());
b.put("Keyspace", query.getKeyspace());
b.put("TabletType", query.getTabletType());
if (query.getBindVars() != null) {
b.put("BindVariables", bindVarsToBSON(query.getBindVars()));
}
if (query.getKeyspaceIds() != null) {
b.put("KeyspaceIds", query.getKeyspaceIds());
} else {
b.put("KeyRanges", query.getKeyRanges());
}
if (query.getSession() != null) {
b.put("Session", query.getSession());
}
return b;
}
public static BSONObject bindVarsToBSON(List<BindVariable> bindVars) {
BSONObject bindVariables = new BasicBSONObject();
for (BindVariable b : bindVars) {
if (b.getType().equals(BindVariable.Type.NULL)) {
bindVariables.put(b.getName(), null);
}
if (b.getType().equals(BindVariable.Type.LONG)) {
bindVariables.put(b.getName(), b.getLongVal());
}
if (b.getType().equals(BindVariable.Type.UNSIGNED_LONG)) {
bindVariables.put(b.getName(), b.getULongVal());
}
if (b.getType().equals(BindVariable.Type.DOUBLE)) {
bindVariables.put(b.getName(), b.getDoubleVal());
}
if (b.getType().equals(BindVariable.Type.BYTE_ARRAY)) {
bindVariables.put(b.getName(), b.getByteArrayVal());
}
}
return bindVariables;
}
public static BSONObject batchQueryToBson(BatchQuery batchQuery) {
BSONObject b = new BasicBSONObject();
List<Map<String, Object>> queries = new LinkedList<>();
Iterator<String> sqlIter = batchQuery.getSqls().iterator();
Iterator<List<BindVariable>> bvIter = batchQuery.getBindVarsList().iterator();
while (sqlIter.hasNext()) {
Map<String, Object> query = new HashMap<>();
query.put("Sql", sqlIter.next());
List<BindVariable> bindVars = bvIter.next();
if (bindVars != null) {
query.put("BindVariables", bindVarsToBSON(bindVars));
}
queries.add(query);
}
b.put("Queries", queries);
b.put("Keyspace", batchQuery.getKeyspace());
b.put("TabletType", batchQuery.getTabletType());
b.put("KeyspaceIds", batchQuery.getKeyspaceIds());
if (batchQuery.getSession() != null) {
b.put("Session", batchQuery.getSession());
}
return b;
}
public static QueryResponse bsonToQueryResponse(BSONObject reply) {
String error = null;
if (reply.containsField("Error")) {
byte[] err = (byte[]) reply.get("Error");
if (err.length > 0) {
error = new String(err);
}
}
QueryResult queryResult = null;
BSONObject result = (BSONObject) reply.get("Result");
if (result != null) {
queryResult = bsonToQueryResult(result, null);
}
Object session = null;
if (reply.containsField("Session")) {
session = reply.get("Session");
}
return new QueryResponse(queryResult, session, error);
}
public static BatchQueryResponse bsonToBatchQueryResponse(BSONObject reply) {
String error = null;
if (reply.containsField("Error")) {
byte[] err = (byte[]) reply.get("Error");
if (err.length > 0) {
error = new String(err);
}
}
Object session = null;
if (reply.containsField("Session")) {
session = reply.get("Session");
}
List<QueryResult> qrs = new LinkedList<>();
BasicBSONList results = (BasicBSONList) reply.get("List");
for (Object result : results) {
qrs.add(bsonToQueryResult((BSONObject) result, null));
}
return new BatchQueryResponse(qrs, session, error);
}
public static QueryResult bsonToQueryResult(BSONObject result, List<Field> fields) {
if (fields == null) {
fields = bsonToFields(result);
}
List<Row> rows = bsonToRows(result, fields);
long rowsAffected = (long) result.get("RowsAffected");
long lastRowId = (long) result.get("InsertId");
return new QueryResult(rows, fields, rowsAffected, lastRowId);
}
public static List<Field> bsonToFields(BSONObject result) {
List<Field> fieldList = new LinkedList<>();
BasicBSONList fields = (BasicBSONList) result.get("Fields");
for (Object field : fields) {
BSONObject fieldBson = (BSONObject) field;
String fieldName = new String((byte[]) fieldBson.get("Name"));
int mysqlType = Ints.checkedCast((Long) fieldBson.get("Type"));
long mysqlFlags = Flag.VT_ZEROVALUE_FLAG.mysqlFlag;
Object flags = fieldBson.get("Flags");
if (flags != null) {
mysqlFlags = (Long) flags;
}
fieldList.add(Field.newFieldFromMysql(fieldName, mysqlType, mysqlFlags));
}
return fieldList;
}
public static List<Row> bsonToRows(BSONObject result, List<Field> fields) {
List<Row> rowList = new LinkedList<>();
BasicBSONList rows = (BasicBSONList) result.get("Rows");
for (Object row : rows) {
LinkedList<Cell> cells = new LinkedList<>();
BasicBSONList cols = (BasicBSONList) row;
Iterator<Field> fieldsIter = fields.iterator();
for (Object col : cols) {
byte[] val = col != null ? (byte[]) col : null;
Field field = fieldsIter.next();
cells.add(field.convertValueToCell(val));
}
rowList.add(new Row(cells));
}
return rowList;
}
public static BSONObject splitQueryRequestToBson(SplitQueryRequest request) {
BSONObject query = new BasicBSONObject();
query.put("Sql", request.getSql());
BSONObject b = new BasicBSONObject();
b.put("Keyspace", request.getKeyspace());
b.put("SplitColumn", request.getSplitColumn());
b.put("Query", query);
b.put("SplitCount", request.getSplitCount());
return b;
}
public static SplitQueryResponse bsonToSplitQueryResponse(BSONObject reply) {
String error = null;
if (reply.containsField("Error")) {
byte[] err = (byte[]) reply.get("Error");
if (err.length > 0) {
error = new String(err);
}
}
BasicBSONList result = (BasicBSONList) reply.get("Splits");
Map<Query, Long> queries = new HashMap<>();
for (Object split : result) {
BSONObject splitObj = (BasicBSONObject) split;
BSONObject query = (BasicBSONObject) (splitObj.get("Query"));
String sql = new String((byte[]) query.get("Sql"));
BSONObject bindVars = (BasicBSONObject) query.get("BindVariables");
List<BindVariable> bindVariables = new LinkedList<>();
for (String key : bindVars.keySet()) {
BindVariable bv = null;
BSONObject val = (BasicBSONObject) bindVars.get(key);
if (val == null) {
bv = BindVariable.forNull(key);
}
int type = (int)val.get("Type");
if (type == 3 /*unsigned long*/) {
bv = BindVariable.forULong(key, (UnsignedLong) val.get("ValueUint"));
}
if (type == 2 /*long*/) {
bv = BindVariable.forLong(key, (Long) val.get("ValueInt"));
}
if (type == 4 /*double*/) {
bv = BindVariable.forDouble(key, (Double) val.get("ValueFloat"));
}
if (type == 1 /*byte[]*/) {
bv = BindVariable.forBytes(key, (byte[]) val.get("ValueBytes"));
}
if (bv == null) {
throw new RuntimeException("invalid bind variable type: " + val.getClass());
}
bindVariables.add(bv);
}
BSONObject keyrangePart = (BasicBSONObject)splitObj.get("KeyRangePart");
String keyspace = new String((byte[]) keyrangePart.get("Keyspace"));
List<KeyRange> keyranges = new ArrayList<>();
for (Object o : (List<?>) keyrangePart.get("KeyRanges")) {
BSONObject keyrange = (BasicBSONObject) o;
String start = Hex.encodeHexString((byte[]) keyrange.get("Start"));
String end = Hex.encodeHexString((byte[]) keyrange.get("End"));
KeyRange kr = new KeyRange(KeyspaceId.valueOf(start), KeyspaceId.valueOf(end));
keyranges.add(kr);
}
Query q = new QueryBuilder(sql, keyspace, "rdonly").setKeyRanges(keyranges)
.setBindVars(bindVariables).setStreaming(true).build();
long size = (long) splitObj.get("Size");
queries.put(q, size);
}
return new SplitQueryResponse(queries, error);
}
}