-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate.go
More file actions
158 lines (125 loc) · 3.72 KB
/
populate.go
File metadata and controls
158 lines (125 loc) · 3.72 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
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
package peerdb
import (
"context"
"os"
"os/signal"
"path/filepath"
"slices"
"syscall"
"github.com/rs/zerolog"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/go/x"
"gitlab.com/peerdb/peerdb/base"
"gitlab.com/peerdb/peerdb/document"
"gitlab.com/peerdb/peerdb/indexer"
"gitlab.com/peerdb/peerdb/transform"
)
func (c *PopulateCommand) populateSite(ctx context.Context, site Site) errors.E {
logger := zerolog.Ctx(ctx)
logger.Info().Str("index", site.Index).Str("schema", site.Schema).Msg("populating")
// We set fallback context values which are used to set application name on PostgreSQL connections.
ctx = WithFallbackDBContext(ctx, site.Schema, "populate")
documents, transformed, errE := base.GenerateCoreDocuments(ctx, nil)
if errE != nil {
return errE
}
return c.PopulateSite(ctx, site, documents, transformed)
}
// PopulateSite populates the given site with provided documents.
func (c *PopulateCommand) PopulateSite(ctx context.Context, site Site, documents []any, transformed []*document.D) errors.E {
logger := zerolog.Ctx(ctx)
if ctx.Err() != nil {
return errors.WithStack(ctx.Err())
}
if c.SaveDir != "" {
logger.Info().Str("path", c.SaveDir).Msg("saving structs as files into a directory")
errE := x.SaveJSONToDir(ctx, c.SaveDir, documents, func(doc any) (string, errors.E) {
id, errE := transform.ExtractDocumentID(doc)
if errE != nil {
return "", errE
}
p := slices.Clone(id)
for i := range len(id) - 1 {
p = append(p, x.SafeFilename(id[i]))
}
p = append(p, x.SafeFilename(id[len(id)-1])+".json")
return filepath.Join(p...), nil
})
if errE != nil {
return errE
}
logger.Info().Int("count", len(documents)).Msg("saved all structs")
if ctx.Err() != nil {
return errors.WithStack(ctx.Err())
}
}
if c.OutputDir != "" {
logger.Info().Str("path", c.OutputDir).Msg("saving documents as files into a directory")
errE := x.SaveJSONToDir(ctx, c.OutputDir, transformed, func(doc *document.D) (string, errors.E) {
return doc.ID.String(), nil
})
if errE != nil {
return errE
}
logger.Info().Int("count", len(transformed)).Msg("saved all documents")
if ctx.Err() != nil {
return errors.WithStack(ctx.Err())
}
}
if c.DryRun {
logger.Info().Msg("dry run, not inserting documents into the database")
return nil
}
count := x.NewCounter(0)
size := x.NewCounter(int64(len(transformed)))
progress := indexer.Progress(logger.With().Logger(), "indexing", nil)
ticker := x.NewTicker(ctx, count, size, indexer.ProgressPrintRate)
defer ticker.Stop()
go func() {
for p := range ticker.C {
progress(ctx, p)
}
}()
errE := site.PopulateAndStart(ctx, transformed, func(doc *document.D) {
count.Increment()
logger.Debug().Str("doc", doc.ID.String()).Msg("saving document")
}, nil, count, size)
if errE != nil {
return errE
}
logger.Info().
Str("index", site.Index).Str("schema", site.Schema).
Int64("count", count.Count()).
Int64("total", size.Count()).
Msg("indexing done")
return nil
}
// Run executes the populate command to populate database with documents.
func (c *PopulateCommand) Run(globals *Globals) errors.E {
// We stop the server gracefully on ctrl-c and TERM signal.
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
ctx = globals.Logger.WithContext(ctx)
InitSites(globals)
ctx, cancel := context.WithCancel(ctx)
if !c.DryRun {
onShutdown, errE := Init(ctx, globals)
if onShutdown != nil {
defer onShutdown()
}
defer cancel()
if errE != nil {
return errE
}
} else {
defer cancel()
}
for _, site := range globals.Sites {
errE := c.populateSite(ctx, site)
if errE != nil {
return errE
}
}
globals.Logger.Info().Msg("populate done")
return nil
}