-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupsert.go
81 lines (65 loc) · 1.67 KB
/
upsert.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
package dsmock
import (
"context"
"fmt"
"math"
"google.golang.org/appengine"
"google.golang.org/appengine/datastore"
)
const (
// MaxBatchSize The number of entities per one multi upsert operation
MaxBatchSize = 500
)
// Upsert entities form yaml file to datastore
func Upsert(ctx context.Context, filename string) error {
batchSize := MaxBatchSize
// Parser
parser := NewYAMLParser()
// Read from file
if err := parser.ReadFile(filename); err != nil {
return err
}
// Parse
dsEntities, err := parser.Parse(ctx)
if err != nil {
return err
}
// Upsert to datastore
allPage := int(math.Ceil(float64(len(*dsEntities)) / float64(batchSize)))
for page := 0; page < allPage; page++ {
from := page * batchSize
to := (page + 1) * batchSize
if to > len(*dsEntities) {
to = len(*dsEntities)
}
// Upsert multi entities
keys, src := getKeysValues(ctx, dsEntities, from, to)
if _, err := datastore.PutMulti(ctx, keys, src); err != nil {
if me, ok := err.(appengine.MultiError); ok {
for i, e := range me {
if e != nil {
return fmt.Errorf("Upsert error(entity No.%v): %v\n", i+1, e)
}
}
} else {
return fmt.Errorf("Upsert error: %v\n", err)
}
} else {
// core.Infof("%d entities ware upserted successfully.\n", len(keys))
}
}
return nil
}
func getKeysValues(ctx context.Context, dsEntities *[]datastore.Entity, from, to int) (keys []*datastore.Key, values []interface{}) {
// Prepare entities
for _, e := range (*dsEntities)[from:to] {
k := KeyToString(e.Key)
if k == `""` {
k = "(auto)"
}
keys = append(keys, e.Key)
props := datastore.PropertyList(e.Properties)
values = append(values, &props)
}
return
}