-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollections_loader.go
More file actions
76 lines (63 loc) · 1.66 KB
/
collections_loader.go
File metadata and controls
76 lines (63 loc) · 1.66 KB
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
package fscli
import (
"fmt"
"os"
"sync"
"cloud.google.com/go/firestore"
"google.golang.org/api/iterator"
)
var (
baseDocToFetched *sync.Map
baseDocToCollections *sync.Map
)
const PAGE_SIZE = 100
func init() {
baseDocToFetched = new(sync.Map)
baseDocToCollections = new(sync.Map)
}
func shouldFetchCollections(baseDoc string) bool {
_, ok := baseDocToFetched.Load(baseDoc)
return !ok
}
func markCollectionsFetched(baseDoc string) {
baseDocToFetched.Store(baseDoc, true)
}
func unmarkCollectionsFetched(baseDoc string) {
baseDocToFetched.Delete(baseDoc)
}
func fetchCollections(baseDoc string, getCollectionsIterator func(baseDoc string) (*firestore.CollectionIterator, error)) {
if !shouldFetchCollections(baseDoc) {
return
}
markCollectionsFetched(baseDoc)
itr, err := getCollectionsIterator(baseDoc)
if err != nil {
unmarkCollectionsFetched(baseDoc)
return
}
collections := make([]string, 0)
p := iterator.NewPager(itr, PAGE_SIZE, "")
for {
var cols []*firestore.CollectionRef
pageToken, err := p.NextPage(&cols)
if err != nil {
fmt.Fprintf(os.Stderr, "\nERROR: %v\n", err)
unmarkCollectionsFetched(baseDoc)
return
}
collections = append(collections, getCollectionIds(cols)...)
baseDocToCollections.Store(baseDoc, collections)
if pageToken == "" {
break
}
}
baseDocToCollections.Store(baseDoc, collections)
}
func getCollections(baseDoc string, getCollectionsIterator func(baseDoc string) (*firestore.CollectionIterator, error)) []string {
go fetchCollections(baseDoc, getCollectionsIterator)
if collection, ok := baseDocToCollections.Load(baseDoc); !ok {
return []string{}
} else {
return collection.([]string)
}
}