-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsutil.go
509 lines (488 loc) · 12.2 KB
/
dsutil.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
package main
import (
"bufio"
"context"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
_ "net/http/pprof"
"os"
"os/user"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"cloud.google.com/go/datastore"
"github.com/rustyx/dsutil/dsio"
"google.golang.org/api/iterator"
)
var (
project = flag.String("project", "", "Google Cloud project name (deduced if not provided)")
kind = flag.String("kind", "", "DataStore table name (required for 'export')")
filter = flag.String("filter", "", "Filter field name (optional)")
from = flag.String("from", "", "Filter >= value (optional)")
to = flag.String("to", "", "Filter < value (optional)")
eq = flag.String("eq", "", "Filter = value (optional)")
order = flag.String("order", "", "Order by field name, use '-' prefix for descending order (optional)")
limit = flag.Int("limit", 0, "Max number of records to export (optional)")
skipdefault = flag.Bool("skipdefault", false, "skip default values (in 'convert' command)")
// httpPort = flag.Int("pprof", 0, "pprof listen port (e.g. 8080)") // for debugging
)
func main() {
flag.Parse()
// if *httpPort > 0 {
// go func() { log.Fatal(http.ListenAndServe(fmt.Sprintf("localhost:%d", *httpPort), nil)) }()
// }
if len(flag.Args()) == 0 {
printUsageAndDie("Missing command argument\n")
}
cmd := flag.Args()[0]
switch cmd {
case "export":
cmdExport()
return
case "import":
cmdImport()
return
case "delete":
cmdDelete()
return
case "set":
cmdSet()
return
case "convert":
cmdConvert()
return
case "test":
cmdTest()
default:
printUsageAndDie("Invalid command argument\n")
}
}
func printUsageAndDie(msg string) {
fmt.Println(msg + `Usage: dsutil [options] command <args>
command:
export <filename> - export records from DataStore
import <filename>... - import records into DataStore
delete - delete records from DataStore
set <field> <type> <value> - update records in DataStore (type is: string, int, double)
convert <in> <out> - convert exported records from JSON to Go object notation
Note: <filename> ending with ".gz" will be automatically g(un)zipped
`)
flag.PrintDefaults()
os.Exit(1)
}
func ensureRequiredArguments() {
cmd := ""
if len(flag.Args()) > 0 {
cmd = flag.Args()[0]
}
if *project == "" {
*project = deduceProjectID()
}
switch {
case *project == "" && cmd != "convert":
printUsageAndDie("Missing required option -project\n")
case *kind == "" && cmd == "export":
printUsageAndDie("Missing required option -kind\n")
case len(flag.Args()) < 2 && (cmd == "export" || cmd == "import"):
printUsageAndDie("Missing required argument <filename>\n")
case len(flag.Args()) > 2 && cmd == "export":
printUsageAndDie("Too many arguments for export command\n")
case simpleFilter(*filter) && *from == "" && *to == "" && *eq == "":
printUsageAndDie("Missing option -from, -to or -eq for -filter\n")
case *filter == "" && *from != "":
printUsageAndDie("Missing option -filter for -from\n")
case *filter == "" && *to != "":
printUsageAndDie("Missing option -filter for -to\n")
}
}
var filterExprRe = regexp.MustCompile(`\b(\w+)\s*(?:(>=?|<=?|!?=)\s*(\S+))?\b`)
func simpleFilter(s string) bool {
m := filterExprRe.FindAllStringSubmatch(s, -1)
if m == nil || len(m) > 1 {
return false
}
return m[0][2] == "" && m[0][3] == ""
}
func cmdExport() {
ensureRequiredArguments()
outfile, err := dsio.OpenForWriting(flag.Args()[1])
check(err, flag.Args()[1])
defer outfile.Close()
ds := connectDS()
defer ds.Close()
q := datastore.NewQuery(*kind)
if simpleFilter(*filter) {
if *from != "" {
q = q.Filter(fmt.Sprintf("%s>=", *filter), *from)
}
if *to != "" {
q = q.Filter(fmt.Sprintf("%s<", *filter), *to)
}
if *eq != "" {
q = q.Filter(fmt.Sprintf("%s=", *filter), *eq)
}
} else {
m := filterExprRe.FindAllStringSubmatch(*filter, -1)
for _, flt := range m {
q = q.Filter(flt[1]+flt[2], flt[3])
}
}
if *order != "" {
if *order == "1" {
*order = *filter
} else if *order == "-1" {
*order = "-" + *filter
}
q = q.Order(*order)
}
if *limit != 0 {
q = q.Limit(*limit)
}
it := ds.Run(context.Background(), q)
err = dsio.Export(it, outfile)
check(err, "ds.Export")
}
func cmdImport() {
ensureRequiredArguments()
for _, ff := range flag.Args()[1:] {
filenames, err := filepath.Glob(ff)
check(err, "Glob")
for _, f := range filenames {
log.Printf("Importing file %s", f)
importFile(f)
}
}
log.Printf("Done")
}
func importFile(filename string) {
infile, err := dsio.OpenForReading(filename)
check(err, filename)
defer infile.Close()
ds := connectDS()
defer ds.Close()
err = dsio.Import(infile, ds)
check(err, "ds.Import")
}
func check(err error, msg string) {
if err != nil {
log.Fatalf("%s: %v", msg, err)
}
}
func cmdSet() {
ensureRequiredArguments()
ds := connectDS()
defer ds.Close()
if len(flag.Args()) != 4 {
printUsageAndDie("'set' requires 3 arguments: FieldName, type and Value\n")
}
if *kind == "" {
printUsageAndDie("Missing required option -kind\n")
}
var err error
key, valueType, valueStr := flag.Args()[1], flag.Args()[2], flag.Args()[3]
var value interface{}
switch valueType {
case "string":
value = valueStr
case "int":
value, err = strconv.Atoi(valueStr)
check(err, "Atoi")
case "double":
value, err = strconv.ParseFloat(valueStr, 64)
check(err, "ParseFloat")
default:
check(errors.New("Invalid type "+valueType), "parse type")
}
log.Printf("Updating %s, setting %s=%v", *kind, key, value)
q := datastore.NewQuery(*kind)
if *from != "" {
log.Printf("where %s >= %v", *filter, *from)
q = q.Filter(fmt.Sprintf("%s>=", *filter), *from)
}
if *to != "" {
log.Printf("where %s < %v", *filter, *to)
q = q.Filter(fmt.Sprintf("%s<", *filter), *to)
}
if *eq != "" {
log.Printf("where %s = %v", *filter, *eq)
q = q.Filter(fmt.Sprintf("%s=", *filter), *eq)
}
it := ds.Run(context.Background(), q)
n := 0
for {
rec := dsio.Entity{}
rec.Key, err = it.Next(&rec.Properties)
if err == iterator.Done {
break
}
check(err, "ds.Next")
found := false
for i, p := range rec.Properties {
if p.Name == key {
rec.Properties[i].Value = value
found = true
break
}
}
if !found {
rec.Properties = append(rec.Properties, datastore.Property{Name: key, Value: value})
}
// log.Printf("Updating %v", rec.Key)
_, err = ds.Put(context.Background(), rec.Key, &rec.Properties)
check(err, "ds.Put")
n++
}
log.Printf("Updated %v", n)
}
func cmdDelete() {
ensureRequiredArguments()
ds := connectDS()
defer ds.Close()
if *filter != "" && len(flag.Args()) > 1 {
printUsageAndDie("'delete' supports -filter OR input file(s), not both\n")
}
if len(flag.Args()) > 1 {
for _, ff := range flag.Args()[1:] {
filenames, err := filepath.Glob(ff)
check(err, "Glob")
for _, f := range filenames {
deleteFromFile(f, ds)
}
}
return
}
if *kind == "" {
printUsageAndDie("Missing required option -kind\n")
}
log.Printf("Deleting entities from %s", *kind)
q := datastore.NewQuery(*kind)
if *from != "" {
log.Printf("where %s >= %v", *filter, *from)
q = q.Filter(fmt.Sprintf("%s>=", *filter), *from)
}
if *to != "" {
log.Printf("where %s < %v", *filter, *to)
q = q.Filter(fmt.Sprintf("%s<", *filter), *to)
}
if *eq != "" {
log.Printf("where %s = %v", *filter, *eq)
q = q.Filter(fmt.Sprintf("%s=", *filter), *eq)
}
if *order != "" {
if *order == "1" {
*order = *filter
} else if *order == "-1" {
*order = "-" + *filter
}
q = q.Order(*order)
}
if *limit != 0 {
q = q.Limit(*limit)
}
q.KeysOnly()
it := ds.Run(context.Background(), q)
n := 0
var keys []*datastore.Key
for {
key, err := it.Next(nil)
if err == iterator.Done {
break
}
check(err, "ds.Next")
log.Printf("Deleting %v", key)
keys = append(keys, key)
n++
if len(keys) >= 200 {
err = ds.DeleteMulti(context.Background(), keys)
check(err, "ds.DeleteMulti")
keys = nil
}
}
if len(keys) > 0 {
err := ds.DeleteMulti(context.Background(), keys)
check(err, "ds.DeleteMulti")
}
log.Printf("Deleted %v", n)
}
func deleteFromFile(filename string, ds *datastore.Client) {
infile, err := dsio.OpenForReading(filename)
check(err, filename)
defer infile.Close()
log.Printf("Deleting entities from file %s", filename)
outCh := make(chan dsio.Entity, 10)
errCh := make(chan error, 1)
go func() {
defer close(errCh)
var keys []*datastore.Key
batchSize := 200
for rec := range outCh {
log.Printf("Deleting %v", rec.Key)
keys = append(keys, rec.Key)
if len(keys) >= batchSize {
err = ds.DeleteMulti(context.Background(), keys)
if err != nil {
errCh <- err
return
}
keys = nil
}
}
if len(keys) > 0 {
err = ds.DeleteMulti(context.Background(), keys)
if err != nil {
errCh <- err
}
}
}()
err = dsio.ImportFile(infile, outCh, errCh)
check(err, "ds.Delete")
}
func cmdConvert() {
ensureRequiredArguments()
if len(flag.Args()) != 3 {
printUsageAndDie("convert arguments should be <in> <out>\n")
}
in, err := dsio.OpenForReading(flag.Args()[1])
check(err, flag.Args()[1])
defer in.Close()
out, err := dsio.OpenForWriting(flag.Args()[2])
check(err, flag.Args()[2])
defer out.Close()
rbuf := bufio.NewScanner(in)
rbuf.Buffer(make([]byte, 32768), 1024*1024*1024)
wbuf := bufio.NewWriterSize(out, 32768)
defer wbuf.Flush()
inCh := make(chan []byte, 10)
outCh := make(chan dsio.Entity, 10)
errCh := make(chan error, 1)
werrCh := make(chan error, 1)
go dsio.Unmarshal(inCh, outCh, errCh)
go func() {
defer close(werrCh)
for rec := range outCh {
_, _ = wbuf.WriteString("{")
i := 0
for _, p := range rec.Properties {
if *skipdefault && (p.Value == "" || p.Value == int64(0) || p.Value == float64(0.0) || p.Value == false) {
continue
}
if i > 0 {
_, _ = wbuf.WriteString(",")
}
i++
value := p.Value
switch v := value.(type) {
case string:
value = strconv.Quote(v)
case time.Time:
value = v.Format(`"2006-01-02T15:04:05.000Z"`)
// case []byte: // TODO
}
_, err = wbuf.WriteString(fmt.Sprintf("%s:%v", p.Name, value))
if err != nil {
werrCh <- err
}
}
_, _ = wbuf.WriteString("},\n")
}
}()
outer:
for rbuf.Scan() {
select {
case inCh <- append([]byte{}, rbuf.Bytes()...): // make a copy
case err = <-errCh:
break outer
}
}
close(inCh)
check(err, "parse")
check(<-errCh, "parse")
check(<-werrCh, "write")
}
func cmdTest() {
ensureRequiredArguments()
ds := connectDS()
defer ds.Close()
key := datastore.IDKey("Test", 0, nil)
dt, err := time.Parse("20060102-15:04:05.000", "20060102-15:04:05.012")
check(err, "parse time")
ent := struct {
Str string
Bool bool
Int int
Int32 int32
Flt32 float32
Flt float64
Dt time.Time
Bin []byte
}{
"Test str",
true,
123,
int32(123),
float32(123.12),
123.12,
dt,
[]byte{1, 2, 3},
}
key, err = ds.Put(context.Background(), key, &ent)
check(err, "ds put")
log.Printf("put=%v,%v", key, &ent)
err = ds.Get(context.Background(), key, &ent)
check(err, "ds get")
log.Printf("get=%v", &ent)
}
func connectDS() *datastore.Client {
host := os.Getenv("DATASTORE_EMULATOR_HOST")
if host != "" {
log.Printf("DATASTORE_EMULATOR_HOST=%q", host)
}
ds, err := datastore.NewClient(context.Background(), *project)
check(err, "DataStore")
return ds
}
func deduceProjectID() (res string) {
if os.Getenv("DATASTORE_EMULATOR_HOST") != "" {
res = os.Getenv("DATASTORE_PROJECT_ID")
}
if res == "" {
active := strings.Trim(getConfigFile("gcloud/active_config"), " \t\r\n")
if active == "" {
active = "default"
}
cfg := getConfigFile("gcloud/configurations/config_" + active)
re := regexp.MustCompile(`(?s)\[core\].*?\bproject = (\S+)`)
m := re.FindStringSubmatch(cfg)
if m != nil {
res = m[1]
}
}
if res != "" {
log.Printf("Using DataStore project %v", res)
}
return
}
func getConfigFile(s string) string {
var b []byte
if runtime.GOOS == "windows" {
b, _ = ioutil.ReadFile(filepath.Join(os.Getenv("APPDATA"), s))
} else {
b, _ = ioutil.ReadFile(filepath.Join(guessUnixHomeDir(), ".config", s))
}
return string(b)
}
func guessUnixHomeDir() string {
if v := os.Getenv("HOME"); v != "" {
return v
}
if u, err := user.Current(); err == nil {
return u.HomeDir
}
return ""
}