forked from sgarciac/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.go
390 lines (316 loc) · 8.52 KB
/
copy.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
package main
import (
"cloud.google.com/go/firestore"
"context"
"fmt"
"github.com/urfave/cli"
"google.golang.org/api/iterator"
"log"
"strings"
"sync/atomic"
"time"
)
type PathType int
const (
DocumentPath PathType = 0
CollectionPath PathType = 1
)
func (p PathType) String() string {
switch p {
case DocumentPath: return "Document"
case CollectionPath: return "Collection"
default: return "UNKNOWN"
}
}
type CopyOption struct {
merge bool
overwrite bool
}
func pathType(p string) PathType {
return PathType(len(strings.Split(strings.Trim(p, "/"), "/")) % 2)
}
func copyCommandAction(c *cli.Context) error {
argsLength := len(c.Args())
if argsLength != 2 {
return cli.NewExitError("Wrong number of arguments", 85)
}
sourceCollectionOrDocumentPath := strings.Trim(c.Args().Get(0), "/")
targetCollectionOrDocumentPath := strings.Trim(c.Args().Get(1), "/")
merge := c.Bool("merge")
overwrite := c.Bool("overwrite")
sc := c.String("src-credentials")
dc := c.String("dest-credentials")
sp := c.String("src-projectid")
dp := c.String("dest-projectid")
if sc == "" {
sc = credentials
}
if dc == "" {
dc = credentials
}
if sp == "" {
sp = projectId
}
if dp == "" {
dp = projectId
}
option := CopyOption{
merge: merge,
overwrite: overwrite,
}
sType := pathType(sourceCollectionOrDocumentPath)
tType := pathType(targetCollectionOrDocumentPath)
if sType != tType {
return cli.NewExitError(fmt.Sprintf("Can't copy from %s to %s", sType.String(), tType.String()), 87)
}
sourceClient, err := createClientWithProjectId(sc, sp)
if err != nil {
return cliClientError(err)
}
targetClient, err := createClientWithProjectId(dc, dp)
if err != nil {
return cliClientError(err)
}
if sType == CollectionPath {
log.Println("copying collection")
copyCollection(
sourceClient.Collection(sourceCollectionOrDocumentPath),
targetClient.Collection(targetCollectionOrDocumentPath),
option,
)
}
if sType == DocumentPath {
log.Println("copying document")
copyDocument(
sourceClient.Doc(sourceCollectionOrDocumentPath),
targetClient.Doc(targetCollectionOrDocumentPath),
option,
)
}
log.Println("Done")
return nil
}
func copyDocument(source, target *firestore.DocumentRef, option CopyOption) {
client := NewCopyClient(200, option)
client.Run(
NewDocumentCopyJob(source, target),
NewCollectionIterationJob(source, target),
)
}
func copyCollection(source, target *firestore.CollectionRef, option CopyOption) {
client := NewCopyClient(200, option)
client.Run(NewDocumentIterationJob(source, target))
}
type CopyClient struct {
jobQueue []CopyJob
workerQueue []chan CopyJob
jobChannel chan CopyJob
workerChannel chan chan CopyJob
WorkerCount int
Option CopyOption
}
func NewCopyClient(workerCount int, option CopyOption) CopyClient {
return CopyClient{
jobChannel: make(chan CopyJob),
workerChannel: make(chan chan CopyJob),
WorkerCount: workerCount,
Option: option,
}
}
func (client *CopyClient) Run(seeds... CopyJob) {
out := make(chan []CopyJob)
go func() {
// submit initial jobs
for _, j := range seeds {
client.submitJob(j)
}
for {
// read jobs from workers
jobs := <-out
for _, j := range jobs {
client.submitJob(j)
}
}
}()
// create works to handle jobs
client.createWorkers(out)
// match workers with jobs
done := client.scheduleJobs()
<- done
}
func (client *CopyClient) submitJob(job CopyJob) {
client.jobChannel <- job
}
func (client *CopyClient) workerReady(w chan CopyJob) {
client.workerChannel <- w
}
func (client *CopyClient) workerInput() chan CopyJob {
return make(chan CopyJob)
}
func (client *CopyClient) createWorkers(workerOutput chan []CopyJob) {
for i:=0; i < client.WorkerCount; i++ {
in := client.workerInput()
go func(in chan CopyJob) {
for {
client.workerReady(in)
j := <-in
w := CopyWorker{
Overwrite: client.Option.overwrite,
Merge: client.Option.merge,
}
jobs, err := w.handleJob(j)
if err != nil {
continue
}
workerOutput <- jobs
}
}(in)
}
}
func (client *CopyClient) scheduleJobs() <- chan struct{} {
done := make(chan struct{})
go func() {
j := <- client.jobChannel
client.jobQueue = append(client.jobQueue, j)
for {
var activeJob CopyJob
var activeWorker chan CopyJob
if len(client.jobQueue) > 0 && len(client.workerQueue) > 0 {
activeJob = client.jobQueue[0]
activeWorker = client.workerQueue[0]
}
select {
case jj := <- client.jobChannel:
client.jobQueue = append(client.jobQueue, jj)
case w := <- client.workerChannel:
client.workerQueue = append(client.workerQueue, w)
case activeWorker <- activeJob:
client.jobQueue = client.jobQueue[1:]
client.workerQueue = client.workerQueue[1:]
case <- time.After(time.Second * 2):
// no more jobs
if len(client.jobQueue) == 0 && len(client.workerQueue) == client.WorkerCount {
done <- struct{}{}
return
}
}
}
}()
return done
}
var ops int32
type CopyWorker struct {
Overwrite bool
Merge bool
}
func (w *CopyWorker) handleJob(j CopyJob) ([]CopyJob, error) {
var result []CopyJob
if j.name == "iterateCollection" {
return w.handleCollectionIterationJob(j)
}
if j.name == "iterateDocument" {
return w.handleDocumentIterationJob(j)
}
if j.name == "copyDocument" {
return w.handleCopyDocumentJob(j)
}
return result, nil
}
func (w *CopyWorker) handleCollectionIterationJob(j CopyJob) ([]CopyJob, error) {
var result []CopyJob
if next, err := j.collectionIterator.Next(); err != nil {
if err == iterator.Done {
return result, nil
}
return result, err
} else {
result = append(result,
j,
NewDocumentIterationJob(next, j.targetDocumentRef.Collection(next.ID)),
)
return result, nil
}
}
func (w *CopyWorker) handleDocumentIterationJob(j CopyJob) ([]CopyJob, error) {
var result []CopyJob
if next, err := j.documentRefIterator.Next(); err != nil {
if err == iterator.Done {
return result, nil
}
return result, err
} else {
result = append(result,
j,
NewCollectionIterationJob(next, j.targetCollectionRef.Doc(next.ID)),
NewDocumentCopyJob(next, j.targetCollectionRef.Doc(next.ID)),
)
return result, nil
}
}
func (w *CopyWorker) handleCopyDocumentJob(j CopyJob) ([]CopyJob, error) {
var result []CopyJob
targetDoc, err := j.targetDocumentRef.Get(context.Background())
if targetDoc == nil {
if err != nil {
log.Printf("get document %s error: %s\n", j.targetDocumentRef.Path, err)
return result, err
}
return result, nil
}
if targetDoc.Exists() && !w.Overwrite {
log.Printf("skipped document %s, because it already exists. use --overwrite to overwrite \n", j.targetDocumentRef.Path)
return result, nil
}
sourceDoc, err := j.documentRef.Get(context.Background())
if sourceDoc == nil {
if err != nil {
log.Printf("get document %s error: %s\n", j.documentRef.Path, err)
return result, err
}
return result, nil
}
var options []firestore.SetOption
if w.Merge {
options = append(options, firestore.MergeAll)
}
atomic.AddInt32(&ops, 1)
if sourceDoc.Exists() {
log.Printf("set document: %d, %s \n", ops, j.targetDocumentRef.Path)
_, err = j.targetDocumentRef.Set(context.Background(), sourceDoc.Data(), options...)
if err != nil {
log.Printf("copy document %s to %s error: %s\n", j.documentRef.Path, j.targetDocumentRef.Path, err)
return result, err
}
}
return result, nil
}
type CopyJob struct {
// iterateCollection iterateDocument copyDocument
name string
documentRef *firestore.DocumentRef
collectionIterator *firestore.CollectionIterator
targetDocumentRef *firestore.DocumentRef
documentRefIterator *firestore.DocumentRefIterator
targetCollectionRef *firestore.CollectionRef
}
func NewCollectionIterationJob(documentRef *firestore.DocumentRef, targetDocumentRef *firestore.DocumentRef) CopyJob {
return CopyJob{
name: "iterateCollection",
collectionIterator: documentRef.Collections(context.Background()),
targetDocumentRef: targetDocumentRef,
}
}
func NewDocumentIterationJob(collectionRef *firestore.CollectionRef, targetCollectionRef *firestore.CollectionRef) CopyJob {
return CopyJob{
name: "iterateDocument",
documentRefIterator: collectionRef.DocumentRefs(context.Background()),
targetCollectionRef: targetCollectionRef,
}
}
func NewDocumentCopyJob(documentRef *firestore.DocumentRef, targetDocumentRef *firestore.DocumentRef) CopyJob {
return CopyJob{
name: "copyDocument",
documentRef: documentRef,
targetDocumentRef: targetDocumentRef,
}
}