-
Notifications
You must be signed in to change notification settings - Fork 0
/
monkeywrench.go
605 lines (538 loc) · 19.3 KB
/
monkeywrench.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package monkeywrench
import (
"context"
"fmt"
"reflect"
"google.golang.org/api/option"
"cloud.google.com/go/spanner"
)
const (
// FqParentPattern - The pattern to build the fully qualified Cloud Spanner
// parent name.
FqParentPattern = "projects/%s/instances/%s"
// FqDbPattern - The pattern to build the fully qualified Cloud Spanner
// database name.
FqDbPattern = "projects/%s/instances/%s/databases/%s"
)
// MonkeyWrench - Wrapper for Cloud Spanner.
type MonkeyWrench struct {
Context context.Context
Project string
Instance string
Db string
Opts []option.ClientOption
Client *spanner.Client
}
// CreateClient - Create a new Spanner client.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) CreateClient(sessionPoolConfig spanner.SessionPoolConfig) error {
// Build the fully qualified db name.
fqDb := fmt.Sprintf(FqDbPattern, m.Project, m.Instance, m.Db)
// Create the client.
spannerClient, err := spanner.NewClientWithConfig(m.Context, fqDb, spanner.ClientConfig{
SessionPoolConfig: sessionPoolConfig,
}, m.Opts...)
if err != nil {
return err
}
// Set the client.
m.Client = spannerClient
return nil
}
// Insert - Insert a row into a table.
//
// The supplied must match the names of the columns.
//
// Params:
// table string - The name of the table to insert into.
// cols []string - The columns to insert data into.
// vals interface{} - The data to import.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) Insert(table string, cols []string, vals []interface{}) error {
return m.applyGenericMutations(table, cols, [][]interface{}{vals}, spanner.Insert)
}
// InsertMulti - Insert multiple rows into a table.
//
// The slice of values supplied must match the names of the columns.
//
// Params:
// table string - The name of the table to insert into.
// cols []string - The columns to insert data into.
// sourceData [][]interface{} - A slice of data to import.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertMulti(table string, cols []string, sourceData [][]interface{}) error {
return m.applyGenericMutations(table, cols, sourceData, spanner.Insert)
}
// InsertOrUpdate - Insert or update a row into a table.
//
// The slice of values supplied must match the names of the columns.
//
// Params:
// table string - The name of the table to insert into.
// cols []string - The columns to insert data into.
// sourceData [][]interface{} - The values to import.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertOrUpdate(table string, cols []string, vals []interface{}) error {
return m.applyGenericMutations(table, cols, [][]interface{}{vals}, spanner.InsertOrUpdate)
}
// InsertOrUpdateMulti - Insert or update multiple rows into a table.
//
// The slice of values supplied must match the names of the columns.
//
// Params:
// table string - The name of the table to insert into.
// cols []string - The columns to insert data into.
// sourceData [][]interface{} - The values to import.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertOrUpdateMulti(table string, cols []string, sourceData [][]interface{}) error {
return m.applyGenericMutations(table, cols, sourceData, spanner.InsertOrUpdate)
}
// Update - Update a row in a table.
//
// The supplied must match the names of the columns.
//
// Params:
// table string - The name of the table to update.
// cols []string - The columns to update.
// vals interface{} - The data to update.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) Update(table string, cols []string, vals []interface{}) error {
return m.applyGenericMutations(table, cols, [][]interface{}{vals}, spanner.Update)
}
// UpdateMulti - Update multiple rows in a table.
//
// The slice of values supplied must match the names of the columns.
//
// Params:
// table string - The name of the table to update
// cols []string - The columns to update.
// sourceData [][]interface{} - A slice of data to update.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) UpdateMulti(table string, cols []string, sourceData [][]interface{}) error {
return m.applyGenericMutations(table, cols, sourceData, spanner.Update)
}
// InsertMap - Insert a row, based on a map, into a table.
//
// Params:
// table string - The name of the table to insert into.
// sourceData map[string]interface{} - The map of col => value data to
// insert into the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertMap(table string, sourceData map[string]interface{}) error {
return m.applyMapMutations(table, []map[string]interface{}{sourceData}, spanner.InsertMap)
}
// InsertMapMulti - Insert multiple rows, based on maps, into a table.
//
// Params:
// table string - The name of the table to insert into.
// sourceData []map[string]interface{} - Nested map of col => value data to
// insert into the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertMapMulti(table string, sourceData []map[string]interface{}) error {
return m.applyMapMutations(table, sourceData, spanner.InsertMap)
}
// InsertOrUpdateMap - Insert or update a row, based on a map, into
// a table.
//
// Params:
// table string - The name of the table to insert into.
// sourceData map[string]interface{} - The map of col => value data to
// insert into the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertOrUpdateMap(table string, sourceData map[string]interface{}) error {
return m.applyMapMutations(table, []map[string]interface{}{sourceData}, spanner.InsertOrUpdateMap)
}
// InsertOrUpdateMapMulti - Insert or update multiple rows, based on maps, into
// a table.
//
// Params:
// table string - The name of the table to insert into.
// sourceData []map[string]interface{} - Nested map of col => value data to
// insert into the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertOrUpdateMapMulti(table string, sourceData []map[string]interface{}) error {
return m.applyMapMutations(table, sourceData, spanner.InsertOrUpdateMap)
}
// UpdateMap - Update a row, based on a map, in a table.
//
// Params:
// table string - The name of the table to update.
// sourceData map[string]interface{} - The map of col => value data to
// update in the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) UpdateMap(table string, sourceData map[string]interface{}) error {
return m.applyMapMutations(table, []map[string]interface{}{sourceData}, spanner.UpdateMap)
}
// UpdateMapMulti - Update multiple rows, based on maps, in a table.
//
// Params:
// table string - The name of the table to update.
// sourceData []map[string]interface{} - Nested map of col => value data to
// update in the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) UpdateMapMulti(table string, sourceData []map[string]interface{}) error {
return m.applyMapMutations(table, sourceData, spanner.UpdateMap)
}
// InsertStruct - Insert a row, based on a struct, into a table.
//
// Params:
// table string - The name of the table to insert into.
// sourceData interface - The data struct to insert into the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertStruct(table string, sourceData interface{}) error {
return m.applyStructMutations(table, []interface{}{sourceData}, spanner.InsertStruct)
}
// InsertStructMulti - Insert multiple rows, based on a struct, into a table.
//
// Params:
// table string - The name of the table to insert into.
// sourceData interface - The data struct to insert into the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertStructMulti(table string, sourceData interface{}) error {
return m.applyStructMutations(table, sourceData, spanner.InsertStruct)
}
// InsertOrUpdateStruct - Insert or update a row, based on a struct, into a
// table.
//
// Params:
// table string - The name of the table to insert into.
// sourceData interface - The data struct to insert into the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertOrUpdateStruct(table string, sourceData interface{}) error {
return m.applyStructMutations(table, []interface{}{sourceData}, spanner.InsertOrUpdateStruct)
}
// InsertOrUpdateStructMulti - Insert or update multiple rows, based on a
// struct, into a table.
//
// Params:
// table string - The name of the table to insert into.
// sourceData interface - The data struct to insert into the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) InsertOrUpdateStructMulti(table string, sourceData interface{}) error {
return m.applyStructMutations(table, sourceData, spanner.InsertOrUpdateStruct)
}
// UpdateStruct - Update a row, based on a struct, into a table.
//
// Params:
// table string - The name of the table to update.
// sourceData interface - The data struct to update in the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) UpdateStruct(table string, sourceData interface{}) error {
return m.applyStructMutations(table, []interface{}{sourceData}, spanner.UpdateStruct)
}
// UpdateStructMulti - Update multiple rows, based on a struct, in a table.
//
// Params:
// table string - The name of the table to update.
// sourceData interface - The data struct to update in the table.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) UpdateStructMulti(table string, sourceData interface{}) error {
return m.applyStructMutations(table, sourceData, spanner.UpdateStruct)
}
// Delete - Delete a row from a table by key.
//
// Params:
// table string - The table to delete from.
// key spanner.Key - The key to delete.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) Delete(table string, key spanner.Key) error {
return m.DeleteMulti(table, []spanner.Key{key})
}
// DeleteMulti - Delete multiple rows from a table by key.
//
// Params:
// table string - The table to delete from.
// keys []spanner.Key - The list of keys to delete.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) DeleteMulti(table string, keys []spanner.Key) error {
// Create a mutation for each value set we have.
mutations := make([]*spanner.Mutation, 0, len(keys))
for _, key := range keys {
mutations = append(mutations, spanner.Delete(table, key))
}
// Apply the mutations.
err := m.applyMutations(mutations)
if err != nil {
return err
}
return nil
}
// DeleteKeyRange - Delete a range of rows by key.
//
// Params:
// table string - The table to delete rows from.
// startKey interface{} - The starting value of the range.
// endKey interface{} - The ending value of the range.
// rangeKind spanner.KeyRangeKind - The kind of range (includes keys or not)
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) DeleteKeyRange(table string, startKey, endKey spanner.Key, rangeKind spanner.KeyRangeKind) error {
// Create the mutation.
mutation := spanner.Delete(table, spanner.KeyRange{
Start: startKey,
End: endKey,
Kind: rangeKind,
})
// Apply the mutations.
err := m.applyMutations([]*spanner.Mutation{mutation})
if err != nil {
return err
}
return nil
}
// Query - Executes a query against Cloud Spanner.
func (m *MonkeyWrench) Query(statement string, params ...map[string]interface{}) ([]*spanner.Row, error) {
return m.QueryCtx(m.Context, statement, params...)
}
// QueryCtx is the same as Query but allows passing your own cancellable context
func (m *MonkeyWrench) QueryCtx(ctx context.Context, statement string, params ...map[string]interface{}) ([]*spanner.Row, error) {
// Prepare the raw statement.
stmt := spanner.NewStatement(statement)
// Add any parameters we've been given.
for _, param := range params {
for key, value := range param {
stmt.Params[key] = value
}
}
// Execute the query.
iter := m.Client.Single().Query(ctx, stmt)
return getResultSlice(iter)
}
// Read - Read multiple rows from Cloud Spanner.
//
// Params:
// table string - Name of the table to read rows from.
// index string - Name of the index to use from the table.
// keys []spanner.KeySet - Slice of keys for the rows to read. Passing an empty
// slice will cause all rows to be returned.
// columns []string - List of columns to read for each row.
//
// Return:
// []*spanner.Row - A list of all rows returned by the query.
// error - An error if it occurred.
func (m *MonkeyWrench) Read(table string, keys []spanner.KeySet, columns []string) ([]*spanner.Row, error) {
// Default to all keys.
var spannerKeys = spanner.AllKeys()
// If we have some specified keys, use those instead.
if len(keys) > 0 {
spannerKeys = spanner.KeySets(keys...)
}
// Execute the query.
iter := m.Client.Single().Read(m.Context, table, spannerKeys, columns)
return getResultSlice(iter)
}
// ReadUsingIndex - Read multiple rows from Cloud Spanner using an index.
//
// Params:
// table string - Name of the table to read rows from.
// index string - Name of the index to use from the table.
// keys []spanner.KeySet - List of keys for the rows to read. Passing an empty
// slice will cause all rows to be returned.
// columns []string - List of columns to read for each row.
//
// Return:
// []*spanner.Row - A list of all rows returned by the query.
// error - An error if it occurred.
func (m *MonkeyWrench) ReadUsingIndex(table, index string, keys []spanner.KeySet, columns []string) ([]*spanner.Row, error) {
// Default to all keys.
var spannerKeys = spanner.AllKeys()
// If we have some specified keys, use those instead.
if len(keys) > 0 {
spannerKeys = spanner.KeySets(keys...)
}
// Execute the query.
iter := m.Client.Single().ReadUsingIndex(m.Context, table, index, spannerKeys, columns)
return getResultSlice(iter)
}
// ReadToStruct - Read a row from Spanner table to a struct.
//
// Params:
// table string - Name of the table to read from.
// key spanner.Key - The key for the row to read.
// dst interface - Destination struct.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) ReadToStruct(table string, key spanner.Key, dst interface{}) error {
// Get the value of the destination parameter.
dstValue := reflect.Indirect(reflect.ValueOf(dst))
// Check we were passed a valid data type.
dataType := dstValue.Type().Kind()
if dataType != reflect.Struct {
return fmt.Errorf("Unsupported data type: %s", dataType.String())
}
// The columns to read.
cols, err := GetColsFromStruct(dst)
if err != nil {
return fmt.Errorf("Could not get columns from struct. Reason: %s", err)
}
// Perform the read.
rows, err := m.Read(table, []spanner.KeySet{key}, cols)
if err != nil {
return err
}
// Decode the row onto the struct.
for _, row := range rows {
row.ToStruct(dst)
}
return nil
}
// applyGenericMutations - Apply a set of generic mutations.
//
// This function is intended to generate and apply mutations for generic data
// based on key => value.
//
// Params:
// table string - The name of the table to insert into.
// cols []string - The columns to insert data into.
// sourceData [][]interface{} - The data to import.
// generator func(table string, cols []string, vals []interface{}) *spanner.Mutation - The callback to generate mutations.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) applyGenericMutations(table string, cols []string, sourceData [][]interface{}, generator func(table string, cols []string, vals []interface{}) *spanner.Mutation) error {
// Get the values from the passed source data.
vals := reflect.ValueOf(sourceData)
// Check the type of data we were passed is vald.
dataKind := vals.Type().Kind()
if dataKind != reflect.Slice && dataKind != reflect.Array {
return fmt.Errorf("Unsupported type: %s", dataKind.String())
}
// Create a mutation for each value set we have.
mutations := make([]*spanner.Mutation, 0, vals.Len())
for _, value := range sourceData {
mutations = append(mutations, generator(table, cols, value))
}
// Apply the mutations.
err := m.applyMutations(mutations)
if err != nil {
return err
}
return nil
}
// applyMapMutations - Apply a set of map mutations.
//
// This function is intended to generate and apply mutations based on maps.
//
// Params:
// table string - The name of the table to insert into.
// cols []string - The columns to insert data into.
// sourceData interface{} - The data to import.
// generator func(table string, data map[string]interface{}) *spanner.Mutation - The callback to generate mutations.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) applyMapMutations(table string, sourceData []map[string]interface{}, generator func(table string, data map[string]interface{}) *spanner.Mutation) error {
// Get the values from the passed source data.
vals := reflect.ValueOf(sourceData)
// Check the type of data we were passed is valid.
dataKind := vals.Type().Kind()
if dataKind != reflect.Slice && dataKind != reflect.Array {
return fmt.Errorf("Unsupported type: %s", dataKind.String())
}
// Create a mutation for each value set we have.
mutations := make([]*spanner.Mutation, 0, vals.Len())
for _, value := range sourceData {
mutations = append(mutations, generator(table, value))
}
// Apply the mutations.
err := m.applyMutations(mutations)
if err != nil {
return err
}
return nil
}
// applyStructMutations - Apply a set of structured mutations.
//
// This function is intended to generate and apply mutations based on structs.
//
// Params:
// table string - The name of the table to insert into.
// cols []string - The columns to insert data into.
// sourceData interface{} - The data to import.
// generator func(table string, data interface{}) (*spanner.Mutation, error) - The callback to generate mutations.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) applyStructMutations(table string, sourceData interface{}, generator func(table string, data interface{}) (*spanner.Mutation, error)) error {
// Get the values from the passed source data.
vals := reflect.Indirect(reflect.ValueOf(sourceData))
// Check the type of data we were passed is vald.
dataKind := vals.Type().Kind()
if dataKind != reflect.Slice && dataKind != reflect.Array {
return fmt.Errorf("Unsupported type: %s", dataKind.String())
}
// Create a mutation for each value set we have.
mutations := make([]*spanner.Mutation, 0, vals.Len())
for i := 0; i < vals.Len(); i++ {
value := vals.Index(i)
mutation, err := generator(table, value.Interface())
if err != nil {
return err
}
mutations = append(mutations, mutation)
}
// Apply the mutations.
err := m.applyMutations(mutations)
if err != nil {
return err
}
return nil
}
// applyMutations - Apply a set of mutations to Cloud Spanner
//
// Params:
// mutations []*spanner.Mutation - The mutations to apply.
//
// Return:
// error - An error if it occurred.
func (m *MonkeyWrench) applyMutations(mutations []*spanner.Mutation) error {
_, err := m.Client.Apply(m.Context, mutations)
if err != nil {
return err
}
return nil
}