forked from christopherhesse/rethinkgo
/
protobuf.go
438 lines (386 loc) · 10.4 KB
/
protobuf.go
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
package rethinkgo
// // Convert Exp trees and queries into protocol buffer form.
// // Functions in this file will panic on failure, the caller is expected to
// // recover().
import (
"code.google.com/p/goprotobuf/proto"
"fmt"
p "github.com/christopherhesse/rethinkgo/ql2"
"reflect"
"runtime"
"sync/atomic"
)
// context stores some state that is required when converting Expressions to
// protocol buffers, and has to be passed by value throughout.
type context struct {
databaseName string
useOutdated bool
overwrite bool
atomic bool
}
// toTerm converts an arbitrary object to a Term, within the context that toTerm
// was called on.
func (ctx context) toTerm(o interface{}) *p.Term {
e := Expr(o)
var termType p.Term_TermType
arguments := e.args
options := map[string]interface{}{}
switch e.kind {
case literalKind:
return ctx.literalToTerm(e.args[0])
case variableKind:
termType = p.Term_VAR
case javascriptKind:
termType = p.Term_JAVASCRIPT
case errorKind:
termType = p.Term_ERROR
case implicitVariableKind:
termType = p.Term_IMPLICIT_VAR
case databaseKind:
termType = p.Term_DB
case tableKind:
termType = p.Term_TABLE
// first arg to table must be the database
if len(arguments) == 1 {
dbExpr := naryOperator(databaseKind, ctx.databaseName)
arguments = []interface{}{dbExpr, arguments[0]}
}
options["use_outdated"] = ctx.useOutdated
case getKind:
termType = p.Term_GET
case equalityKind:
termType = p.Term_EQ
case inequalityKind:
termType = p.Term_NE
case lessThanKind:
termType = p.Term_LT
case lessThanOrEqualKind:
termType = p.Term_LE
case greaterThanKind:
termType = p.Term_GT
case greaterThanOrEqualKind:
termType = p.Term_GE
case logicalNotKind:
termType = p.Term_NOT
case addKind:
termType = p.Term_ADD
case subtractKind:
termType = p.Term_SUB
case multiplyKind:
termType = p.Term_MUL
case divideKind:
termType = p.Term_DIV
case moduloKind:
termType = p.Term_MOD
case appendKind:
termType = p.Term_APPEND
case sliceKind:
termType = p.Term_SLICE
case skipKind:
termType = p.Term_SKIP
case limitKind:
termType = p.Term_LIMIT
case getAttributeKind:
termType = p.Term_GETATTR
case containsKind:
termType = p.Term_CONTAINS
case pluckKind:
termType = p.Term_PLUCK
case withoutKind:
termType = p.Term_WITHOUT
case mergeKind:
termType = p.Term_MERGE
case betweenKind:
termType = p.Term_BETWEEN
options["left_bound"] = arguments[1]
options["right_bound"] = arguments[2]
arguments = arguments[:1]
case reduceKind:
termType = p.Term_REDUCE
options["base"] = arguments[2]
arguments = arguments[:2]
case mapKind:
termType = p.Term_MAP
case filterKind:
termType = p.Term_FILTER
case concatMapKind:
termType = p.Term_CONCATMAP
case orderByKind:
termType = p.Term_ORDERBY
case distinctKind:
termType = p.Term_DISTINCT
case countKind:
termType = p.Term_COUNT
case unionKind:
termType = p.Term_UNION
case nthKind:
termType = p.Term_NTH
case groupedMapReduceKind:
termType = p.Term_GROUPED_MAP_REDUCE
options["base"] = arguments[4]
arguments = arguments[:4]
case groupByKind:
termType = p.Term_GROUPBY
case innerJoinKind:
termType = p.Term_INNER_JOIN
case outerJoinKind:
termType = p.Term_OUTER_JOIN
case eqJoinKind:
termType = p.Term_EQ_JOIN
case zipKind:
termType = p.Term_ZIP
case coerceToKind:
termType = p.Term_COERCE_TO
case typeOfKind:
termType = p.Term_TYPEOF
case updateKind:
termType = p.Term_UPDATE
options["non_atomic"] = !ctx.atomic
case deleteKind:
termType = p.Term_DELETE
case replaceKind:
termType = p.Term_REPLACE
options["non_atomic"] = !ctx.atomic
case insertKind:
termType = p.Term_INSERT
options["upsert"] = ctx.overwrite
case databaseCreateKind:
termType = p.Term_DB_CREATE
case databaseDropKind:
termType = p.Term_DB_DROP
case databaseListKind:
termType = p.Term_DB_LIST
case tableCreateKind:
termType = p.Term_TABLE_CREATE
// last argument is the table spec
spec := arguments[len(arguments)-1].(TableSpec)
arguments = arguments[:len(arguments)-1]
if len(arguments) == 0 {
// just spec, need to add database
dbExpr := naryOperator(databaseKind, ctx.databaseName)
arguments = append(arguments, dbExpr)
}
arguments = append(arguments, spec.Name)
if spec.Datacenter != "" {
options["datacenter"] = spec.Datacenter
}
if spec.PrimaryKey != "" {
options["primary_key"] = spec.PrimaryKey
}
if spec.CacheSize != 0 {
options["cache_size"] = spec.CacheSize
}
case tableDropKind:
termType = p.Term_TABLE_DROP
if len(arguments) == 1 {
// no database specified, use the session database
dbExpr := naryOperator(databaseKind, ctx.databaseName)
arguments = []interface{}{dbExpr, arguments[0]}
}
case tableListKind:
termType = p.Term_TABLE_LIST
if len(arguments) == 0 {
// no database specified, use the session database
dbExpr := naryOperator(databaseKind, ctx.databaseName)
arguments = append(arguments, dbExpr)
}
case funcallKind:
termType = p.Term_FUNCALL
case branchKind:
termType = p.Term_BRANCH
case anyKind:
termType = p.Term_ANY
case allKind:
termType = p.Term_ALL
case forEachKind:
termType = p.Term_FOREACH
case funcKind:
return ctx.toFuncTerm(arguments[0], arguments[1].(int))
case ascendingKind:
termType = p.Term_ASC
case descendingKind:
termType = p.Term_DESC
// special made-up kind to set options on the query
case upsertKind:
ctx.overwrite = e.args[1].(bool)
return ctx.toTerm(e.args[0])
case atomicKind:
ctx.atomic = e.args[1].(bool)
return ctx.toTerm(e.args[0])
case useOutdatedKind:
ctx.useOutdated = e.args[1].(bool)
return ctx.toTerm(e.args[0])
default:
panic("invalid term kind")
}
args := []*p.Term{}
for _, arg := range arguments {
args = append(args, ctx.toTerm(arg))
}
var optargs []*p.Term_AssocPair
for key, value := range options {
optarg := &p.Term_AssocPair{
Key: proto.String(key),
Val: ctx.toTerm(value),
}
optargs = append(optargs, optarg)
}
return &p.Term{
Type: termType.Enum(),
Args: args,
Optargs: optargs,
}
}
var variableCounter int64 = 0
func nextVariableNumber() int64 {
return atomic.AddInt64(&variableCounter, 1)
}
func (ctx context) toFuncTerm(f interface{}, requiredArgs int) *p.Term {
if reflect.ValueOf(f).Kind() == reflect.Func {
return ctx.compileGoFunc(f, requiredArgs)
}
e := Expr(f)
if e.kind == javascriptKind {
return ctx.toTerm(e)
}
return ctx.compileExpressionFunc(e, requiredArgs)
}
func (ctx context) compileExpressionFunc(e Exp, requiredArgs int) *p.Term {
// an expression that takes no args, e.g. Row.Attr("name")
params := []int64{}
for requiredArgs > 0 {
params = append(params, nextVariableNumber())
requiredArgs--
}
paramsTerm := ctx.toTerm(params)
funcTerm := ctx.toTerm(e)
return &p.Term{
Type: p.Term_FUNC.Enum(),
Args: []*p.Term{paramsTerm, funcTerm},
}
}
func (ctx context) compileGoFunc(f interface{}, requiredArgs int) *p.Term {
// presumably if we're here, the user has supplied a go func to be
// converted to an expression
value := reflect.ValueOf(f)
valueType := value.Type()
if requiredArgs != -1 && valueType.NumIn() != requiredArgs {
panic("Function expression has incorrect number of arguments")
}
// check input types and generate the variables to pass to the function
// the args have generated names because when the function is serialized,
// the server can't figure out which variable is which in a closure
var params []int64
var args []reflect.Value
for i := 0; i < valueType.NumIn(); i++ {
number := nextVariableNumber()
e := naryOperator(variableKind, number)
args = append(args, reflect.ValueOf(e))
params = append(params, number)
// make sure all input arguments are of type Exp
if !valueType.In(i).AssignableTo(reflect.TypeOf(Exp{})) {
panic("Function argument is not of type Exp")
}
}
if valueType.NumOut() != 1 {
panic("Function does not have a single return value")
}
outValue := value.Call(args)[0]
paramsTerm := ctx.toTerm(params)
funcTerm := ctx.toTerm(outValue.Interface())
return &p.Term{
Type: p.Term_FUNC.Enum(),
Args: []*p.Term{paramsTerm, funcTerm},
}
}
func (ctx context) literalToTerm(literal interface{}) *p.Term {
value := reflect.ValueOf(literal)
var datum *p.Datum
switch value.Kind() {
case reflect.Array, reflect.Slice:
terms := []*p.Term{}
for _, arg := range toArray(literal) {
terms = append(terms, ctx.toTerm(arg))
}
return &p.Term{
Type: p.Term_MAKE_ARRAY.Enum(),
Args: terms,
}
case reflect.Map:
return &p.Term{
Type: p.Term_MAKE_OBJ.Enum(),
Optargs: ctx.mapToAssocPairs(literal),
}
}
datum, err := datumMarshal(literal)
if err != nil {
panic(err)
}
return &p.Term{
Type: p.Term_DATUM.Enum(),
Datum: datum,
}
}
// toArray and toObject seem overly complicated, like maybe some sort
// of assignment assertion would be enough
func toArray(a interface{}) []interface{} {
array := []interface{}{}
arrayValue := reflect.ValueOf(a)
for i := 0; i < arrayValue.Len(); i++ {
value := arrayValue.Index(i).Interface()
array = append(array, value)
}
return array
}
func toObject(m interface{}) map[string]interface{} {
object := map[string]interface{}{}
mapValue := reflect.ValueOf(m)
mapType := mapValue.Type()
keyType := mapType.Key()
if keyType.Kind() != reflect.String {
panic("string keys only in maps")
}
for _, keyValue := range mapValue.MapKeys() {
key := keyValue.String()
valueValue := mapValue.MapIndex(keyValue)
value := valueValue.Interface()
object[key] = value
}
return object
}
func (ctx context) mapToAssocPairs(m interface{}) (pairs []*p.Term_AssocPair) {
for key, value := range toObject(m) {
pair := &p.Term_AssocPair{
Key: proto.String(key),
Val: ctx.toTerm(value),
}
pairs = append(pairs, pair)
}
return pairs
}
func (e Exp) toProtobuf(ctx context) *p.Query {
return &p.Query{
Type: p.Query_START.Enum(),
Query: ctx.toTerm(e),
}
}
// buildProtobuf converts a query to a protobuf and catches any panics raised
// by the toProtobuf() functions.
func (ctx context) buildProtobuf(query Exp) (queryProto *p.Query, err error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
err = fmt.Errorf("rethinkdb: %v", r)
}
}()
queryProto = query.toProtobuf(ctx)
return
}
// Check compiles a query for sending to the server, but does not send it.
// There is one .Check() method for each query type.
func (e Exp) Check(s *Session) error {
_, err := s.getContext().buildProtobuf(e)
return err
}