-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.go
140 lines (109 loc) · 3.04 KB
/
export.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
package main
import (
"context"
"errors"
"fmt"
"sync"
"cloud.google.com/go/firestore"
"golang.org/x/sync/errgroup"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type exporter struct {
cli *firestore.Client
goroutinesLimitCh chan struct{}
}
func newExporter(ctx context.Context, projectID, pathToCreds string, maxGoroutinesLimit int) (*exporter, error) {
cli, err := firestore.NewClient(ctx, projectID, option.WithCredentialsFile(pathToCreds))
if err != nil {
return nil, fmt.Errorf("[exporter] failed to create firestore client: %w", err)
}
return &exporter{
cli: cli,
goroutinesLimitCh: make(chan struct{}, maxGoroutinesLimit),
}, nil
}
func (e *exporter) exportAll(ctx context.Context) (map[string]any, error) {
return e.exportCollections(ctx, e.cli.Collections(ctx))
}
func (e *exporter) exportCollections(ctx context.Context, collectionsIter *firestore.CollectionIterator) (map[string]any, error) {
res := make(map[string]any, 0)
resMu := &sync.Mutex{}
eg, ctx := errgroup.WithContext(ctx)
loop:
for {
collRef, err := collectionsIter.Next()
switch {
case errors.Is(err, iterator.Done):
break loop
case err != nil:
return nil, fmt.Errorf("[exporter] failed to iterate over all collections: %w", err)
}
e.goroutinesLimitCh <- struct{}{}
eg.Go(func() error {
defer func() {
<-e.goroutinesLimitCh
}()
resMu.Lock()
res[collRef.ID], err = e.exportCollection(ctx, collRef)
resMu.Unlock()
if err != nil {
return fmt.Errorf("[exporter] failed to export collection %s: %w", collRef.Path, err)
}
return nil
})
}
err := eg.Wait()
if err != nil {
return nil, err
}
return res, nil
}
func (e *exporter) exportCollection(ctx context.Context, collRef *firestore.CollectionRef) (map[string]any, error) {
iter := collRef.DocumentRefs(ctx)
res := make(map[string]any, 0)
resMu := &sync.Mutex{}
eg, ctx := errgroup.WithContext(ctx)
loop:
for {
docRef, err := iter.Next()
switch {
case errors.Is(err, iterator.Done):
break loop
case err != nil:
return nil, fmt.Errorf("[exporter] failed to iterate over documents in collection %s: %w", collRef.Path, err)
}
e.goroutinesLimitCh <- struct{}{}
eg.Go(func() error {
defer func() {
<-e.goroutinesLimitCh
}()
subColls, err := e.exportCollections(ctx, docRef.Collections(ctx))
if err != nil {
return fmt.Errorf("[exporter] failed to export sub-collections for document %s: %w", docRef.Path, err)
}
docData := make(map[string]any, 0)
for key, val := range subColls {
docData[key] = val
}
doc, err := docRef.Get(ctx)
if err != nil && status.Code(err) != codes.NotFound {
return fmt.Errorf("[exporter] failed to get document data %s: %w", docRef.Path, err)
}
for key, val := range doc.Data() {
docData[key] = val
}
resMu.Lock()
res[docRef.ID] = docData
resMu.Unlock()
return nil
})
}
err := eg.Wait()
if err != nil {
return nil, err
}
return res, nil
}