-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
182 lines (174 loc) · 4.03 KB
/
import.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
package dsio
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"strconv"
"time"
"cloud.google.com/go/datastore"
)
// Import imports DataStore entities from the export file.
func Import(r io.Reader, ds *datastore.Client) (err error) {
outCh := make(chan Entity, 10)
errCh := make(chan error, 1)
go func() {
defer close(errCh)
var keys []*datastore.Key
var rows []datastore.PropertyList
batchSize := 200
for rec := range outCh {
keys = append(keys, rec.Key)
rows = append(rows, rec.Properties)
if len(rows) >= batchSize {
_, err := ds.PutMulti(context.Background(), keys, rows)
if err != nil {
errCh <- err
return
}
keys, rows = nil, nil
}
}
if len(rows) > 0 {
_, err := ds.PutMulti(context.Background(), keys, rows)
if err != nil {
errCh <- err
}
}
}()
err = ImportFile(r, outCh, errCh)
return
}
// ImportFile reads an export file, writing DataStore entities to outCh.
// Closes outCh to signal EOF.
// Waits until errCh is closed.
// Returns the first encountered error.
func ImportFile(r io.Reader, outCh chan Entity, errCh chan error) (err error) {
rbuf := bufio.NewScanner(r)
rbuf.Buffer(make([]byte, 32768), 1024*1024*1024)
inCh := make(chan []byte, 10)
ierrCh := make(chan error, 1)
go Unmarshal(inCh, outCh, ierrCh)
outer:
for rbuf.Scan() {
select {
case inCh <- append([]byte{}, rbuf.Bytes()...): // make a copy
case err = <-errCh:
break outer
case err = <-ierrCh:
break outer
}
}
close(inCh)
go func() {
for range outCh {
// drain outCh
}
}()
err2 := <-ierrCh // catch possible error at last line
err3 := <-errCh // wait for completion
if err == nil {
err = err2
}
if err == nil {
err = err3
}
return
}
// Unmarshal processes export file lines, writing DataStore entities to outCh and any error to errCh.
// Closes outCh and errCh before returning.
func Unmarshal(inCh <-chan []byte, outCh chan<- Entity, errCh chan<- error) {
defer close(errCh)
defer close(outCh)
fieldtypes := make(map[string]string)
fields := make(map[int]jsonField)
e := struct {
jsonRowReader
jsonFields
}{}
linenr := 0
for b := range inCh {
linenr++
if len(e.Row) < len(fields) {
e.Row = make([]valueWrapper, len(fields))
for i, f := range fields {
e.Row[i].typ = f.Type
}
}
e.Fields = nil
e.Key = ""
for i := range e.Row {
e.Row[i].value = nil
}
err := json.Unmarshal(b, &e)
if err != nil {
errCh <- fmt.Errorf("line %d JSON Unmarshal error: %v. Line: %v", linenr, err, string(b))
return
}
if len(e.Fields) > 0 {
for i, f := range e.Fields {
f.idx = i + e.FieldsFrom
fields[f.idx] = f
fieldtypes[f.Name] = f.Type
}
e.Row = nil
}
if e.Key == "" || len(e.Row) == 0 {
continue
}
rec := Entity{Key: UnmarshalKey(e.Key)}
for i, v := range e.Row {
if v.value == nil {
continue
}
f := fields[i]
p := datastore.Property{
Name: f.Name,
Value: v.value,
NoIndex: f.NoIndex,
}
rec.Properties = append(rec.Properties, p)
}
outCh <- rec
}
}
type jsonRowReader struct {
Key string `json:"k"`
Row []valueWrapper `json:"d"`
}
type valueWrapper struct { // implements json.Unmarshaler
typ string
value interface{}
}
func (v *valueWrapper) UnmarshalJSON(b []byte) (err error) {
if len(b) == 4 && string(b) == "null" {
v.value = nil
return nil
}
switch v.typ {
case "bool":
v.value, err = strconv.ParseBool(string(b))
case "int64":
v.value, err = strconv.ParseInt(string(b), 10, 64)
case "float64":
v.value, err = strconv.ParseFloat(string(b), 64)
case "string":
v.value, err = strconv.Unquote(string(b))
case "time.Time":
if len(b) > 2 && b[0] == '"' {
v.value, err = time.Parse("2006-01-02T15:04:05.000Z", string(b[1:len(b)-1]))
}
case "[]uint8":
if len(b) > 2 && b[0] == '"' {
v.value, err = base64.RawStdEncoding.DecodeString(string(b[1 : len(b)-1]))
}
default:
err = fmt.Errorf("Unsupported data type '%s'", v.typ)
}
if err != nil {
err = fmt.Errorf("Unable to unmarshal '%v' as %v", string(b), v.typ)
}
return
}