-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprotobuf.go
548 lines (495 loc) · 13.3 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
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
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
durability string
overwrite bool
atomic bool
returnValues 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 javascriptKind:
termType = p.Term_JAVASCRIPT
if len(arguments) == 2 {
options["timeout"] = arguments[1]
arguments = arguments[:1]
}
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]}
}
if ctx.useOutdated {
options["use_outdated"] = ctx.useOutdated
}
case betweenKind:
termType = p.Term_BETWEEN
if len(arguments) == 4 {
// last argument is an index
options["index"] = arguments[3]
arguments = arguments[:3]
}
case reduceKind:
termType = p.Term_REDUCE
options["base"] = arguments[2]
arguments = arguments[:2]
case groupedMapReduceKind:
termType = p.Term_GROUPED_MAP_REDUCE
options["base"] = arguments[4]
arguments = arguments[:4]
case eqJoinKind:
termType = p.Term_EQ_JOIN
options["index"] = arguments[3]
arguments = arguments[:3]
case updateKind, deleteKind, replaceKind, insertKind:
if ctx.durability != "" {
options["durability"] = ctx.durability
}
if ctx.returnValues {
options["return_vals"] = true
}
switch e.kind {
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 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
}
if spec.Durability != "" {
options["durability"] = spec.Durability
}
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 getAllKind:
termType = p.Term_GET_ALL
options["index"] = arguments[len(arguments)-1]
arguments = arguments[:len(arguments)-1]
case funcKind:
return ctx.toFuncTerm(arguments[0], arguments[1].(int))
// 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])
case durabilityKind:
ctx.durability = e.args[1].(string)
return ctx.toTerm(e.args[0])
case returnValuesKind:
ctx.returnValues = true
return ctx.toTerm(e.args[0])
case jsonKind:
termType = p.Term_JSON
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 groupByKind:
termType = p.Term_GROUPBY
case innerJoinKind:
termType = p.Term_INNER_JOIN
case outerJoinKind:
termType = p.Term_OUTER_JOIN
case zipKind:
termType = p.Term_ZIP
case coerceToKind:
termType = p.Term_COERCE_TO
case typeOfKind:
termType = p.Term_TYPEOF
case infoKind:
termType = p.Term_INFO
case keysKind:
termType = p.Term_KEYS
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 prependKind:
termType = p.Term_PREPEND
case insertAtKind:
termType = p.Term_INSERT_AT
case spliceAtKind:
termType = p.Term_SPLICE_AT
case deleteAtKind:
termType = p.Term_DELETE_AT
case changeAtKind:
termType = p.Term_CHANGE_AT
case differenceKind:
termType = p.Term_DIFFERENCE
case indexesOfKind:
termType = p.Term_INDEXES_OF
case isEmptyKind:
termType = p.Term_IS_EMPTY
case setInsertKind:
termType = p.Term_SET_INSERT
case setUnionKind:
termType = p.Term_SET_UNION
case setDifferenceKind:
termType = p.Term_SET_DIFFERENCE
case setIntersectionKind:
termType = p.Term_SET_INTERSECTION
case containsKind:
termType = p.Term_CONTAINS
case sliceKind:
termType = p.Term_SLICE
case skipKind:
termType = p.Term_SKIP
case limitKind:
termType = p.Term_LIMIT
case sampleKind:
termType = p.Term_SAMPLE
case matchKind:
termType = p.Term_MATCH
case getFieldKind:
termType = p.Term_GET_FIELD
case hasFieldsKind:
termType = p.Term_HAS_FIELDS
case withFieldsKind:
termType = p.Term_WITH_FIELDS
case pluckKind:
termType = p.Term_PLUCK
case withoutKind:
termType = p.Term_WITHOUT
case mergeKind:
termType = p.Term_MERGE
case indexCreateKind:
termType = p.Term_INDEX_CREATE
case indexListKind:
termType = p.Term_INDEX_LIST
case indexDropKind:
termType = p.Term_INDEX_DROP
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 databaseCreateKind:
termType = p.Term_DB_CREATE
case databaseDropKind:
termType = p.Term_DB_DROP
case databaseListKind:
termType = p.Term_DB_LIST
case errorKind:
termType = p.Term_ERROR
case implicitVariableKind:
termType = p.Term_IMPLICIT_VAR
case databaseKind:
termType = p.Term_DB
case variableKind:
termType = p.Term_VAR
case ascendingKind:
termType = p.Term_ASC
case descendingKind:
termType = p.Term_DESC
case defaultKind:
termType = p.Term_DEFAULT
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 containsImplicitVariable(term *p.Term) bool {
if *term.Type == p.Term_IMPLICIT_VAR {
return true
}
for _, arg := range term.Args {
if containsImplicitVariable(arg) {
return true
}
}
for _, optarg := range term.Optargs {
if containsImplicitVariable(optarg.Val) {
return true
}
}
return false
}
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)
// the user may pass in a Map with r.Row elements, such as:
// r.Table("heroes").Filter(r.Map{"durability": r.Row.Attr("speed")})
// these have to be sent to the server as a function, but it looks a lot like a
// literal or other expression, so in order to determine if we should send it
// to the server as a function, we just check for the use of r.Row
// if we just convert all literals to functions, something like:
// r.Expr(r.List{"a", "b", "b", "a"}).IndexesOf("a")
// won't work
term := ctx.toTerm(e)
if e.kind == javascriptKind || (e.kind == literalKind && !containsImplicitVariable(term)) {
return term
}
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 := paramsToTerm(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 := paramsToTerm(params)
funcTerm := ctx.toTerm(outValue.Interface())
return &p.Term{
Type: p.Term_FUNC.Enum(),
Args: []*p.Term{paramsTerm, funcTerm},
}
}
func paramsToTerm(params []int64) *p.Term {
terms := []*p.Term{}
for _, param := range params {
num := float64(param)
term := &p.Term{
Type: p.Term_DATUM.Enum(),
Datum: &p.Datum{
Type: p.Datum_R_NUM.Enum(),
RNum: &num,
},
}
terms = append(terms, term)
}
return &p.Term{
Type: p.Term_MAKE_ARRAY.Enum(),
Args: terms,
}
}
func (ctx context) literalToTerm(literal interface{}) *p.Term {
value := reflect.ValueOf(literal)
if value.Kind() == reflect.Map {
return &p.Term{
Type: p.Term_MAKE_OBJ.Enum(),
Optargs: ctx.mapToAssocPairs(literal),
}
}
term, err := datumMarshal(literal)
if err != nil {
panic(err)
}
return term
}
// 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
}