-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.go
250 lines (239 loc) · 6.47 KB
/
datasets.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
package eprinttools
import (
"bufio"
"fmt"
"log"
"os"
"path"
"strconv"
"strings"
"time"
// Caltech Library Packages
"github.com/caltechlibrary/dataset/v2"
)
//
//
// The "datasets" creates the pairtree datasets based on the
// EPrinttools settings.json and previously run harvests.
//
// generateDataset creates a dataset from the harvested repository.
func generateDataset(cfg *Config, repoName string, dsn string, projectDir string, keyList string, verbose bool) error {
repoCfg, ok := cfg.Repositories[repoName]
if !ok {
return fmt.Errorf("%s not found in configuration", repoName)
}
cName := path.Join(projectDir, repoCfg.DefaultCollection) + ".ds"
if _, err := os.Stat(cName); err == nil {
os.RemoveAll(cName)
}
// NOTE: We're creating a pairtree dataset collection so it can be downloaded as a ZIP
c, err := dataset.Init(cName, dsn)
if err != nil {
return err
}
c.Close()
c, err = dataset.Open(cName)
if err != nil {
return err
}
defer c.Close()
if keyList == "" {
// Get all the rows from the harvested repository, then
// write the JSON source to the dataset.
var (
cntStmt string
stmt string
tot int
)
if repoCfg.PublicOnly {
cntStmt = fmt.Sprintf("SELECT COUNT(*) FROM %s WHERE is_public IS TRUE", repoName)
stmt = fmt.Sprintf("SELECT id, src FROM %s WHERE is_public IS TRUE", repoName)
} else {
cntStmt = fmt.Sprintf("SELECT COUNT(*) FROM %s", repoName)
stmt = fmt.Sprintf("SELECT id, src FROM %s", repoName)
}
rows, err := cfg.Jdb.Query(cntStmt)
if err != nil {
return err
}
if rows.Next() {
if err := rows.Scan(&tot); err != nil {
return err
}
}
rows.Close()
modValue := calcModValue(tot)
rows, err = cfg.Jdb.Query(stmt)
if err != nil {
return err
}
defer rows.Close()
i := 0
t0 := time.Now()
for rows.Next() {
var (
eprintid int
src []byte
)
if err := rows.Scan(&eprintid, &src); err != nil {
log.Printf("failed to read row in %q, %s", repoName, err)
} else {
key := fmt.Sprintf("%d", eprintid)
if err := c.CreateJSON(key, src); err != nil {
log.Printf("failed to create %q in %q, %s", key, cName, err)
}
i += 1
if verbose && ((i % modValue) == 0) {
log.Printf("%s %d records added (%s)", path.Base(cName), i, progress(t0, i, tot))
}
}
}
if err := rows.Err(); err != nil {
log.Printf("row error %s", err)
return err
}
if verbose {
log.Printf("dataset %s created in %v", cName, time.Since(t0).Truncate(time.Second))
}
} else {
// Get key list rows from the harvested repository, then
// write the JSON source to the dataset.
var (
stmt string
tot int
ids []int
)
fp, err := os.Open(keyList)
if err != nil {
return err
}
defer fp.Close()
scanner := bufio.NewScanner(fp)
for scanner.Scan() {
tok := scanner.Text()
if tok != "" {
id, err := strconv.Atoi(strings.TrimSpace(tok))
if err != nil {
log.Printf("warning %q is not a valid eprintid", tok)
} else {
ids = append(ids, id)
}
}
}
if err := scanner.Err(); err != nil {
return err
}
tot = len(ids)
if verbose {
log.Printf("Retrieved %d keys from %q", tot, keyList)
}
modValue := calcModValue(tot)
stmt = fmt.Sprintf("SELECT id, src FROM %s WHERE id = ?", repoName)
t0 := time.Now()
for i, id := range ids {
rows, err := cfg.Jdb.Query(stmt, id)
if err != nil {
log.Printf("WARNING query failed for %d, line %d, %q", id, i, err)
continue
}
defer rows.Close()
for rows.Next() {
var (
eprintid int
src []byte
)
if err := rows.Scan(&eprintid, &src); err != nil {
log.Printf("WARNING failed to read row in %q, id %d, line %d, %s", repoName, id, i, err)
} else {
key := fmt.Sprintf("%d", eprintid)
if err := c.CreateJSON(key, src); err != nil {
log.Printf("failed to create %q in %q, %s", key, cName, err)
}
}
}
if err := rows.Err(); err != nil {
log.Printf("WARNING row error for id %d, line %d, %s", id, i, err)
}
if verbose && ((i % modValue) == 0) {
log.Printf("%s %d records added (%s)", path.Base(cName), i, progress(t0, i, tot))
}
}
if verbose {
log.Printf("dataset %s created in %v", cName, time.Since(t0).Truncate(time.Second))
}
}
return nil
}
// RunDataset will use the eprinttools settings.jons config file
// and a repository ID (e.g. caltechauthors) and render a
// dataset collection based on the previously harvested contents.
func RunDataset(cfgName string, repoName string, dsn string, keyList string, verbose bool) error {
t0 := time.Now()
appName := path.Base(os.Args[0])
// Read in the configuration for this harvester instance.
cfg, err := LoadConfig(cfgName)
if err != nil {
return err
}
if cfg == nil {
return fmt.Errorf("could not create a configuration object")
}
if err := OpenJSONStore(cfg); err != nil {
return err
}
defer cfg.Jdb.Close()
if cfg.ProjectDir != "" && cfg.ProjectDir != "." {
if _, err := os.Stat(cfg.ProjectDir); os.IsNotExist(err) {
if err := os.MkdirAll(cfg.ProjectDir, 0775); err != nil {
log.Printf("%s", err)
os.Exit(1)
}
}
}
if verbose {
log.Printf("%s (%s) started %v", appName, repoName, time.Now().Sub(t0).Truncate(time.Second))
}
if err := generateDataset(cfg, repoName, dsn, cfg.ProjectDir, keyList, verbose); err != nil {
return err
}
if verbose {
log.Printf("%s (%s) run time %v", appName, repoName, time.Since(t0).Truncate(time.Second))
}
return nil
}
// RunDatasets will use the eprinttools settings.jons config file
// and reader dataset collections based on the contents.
func RunDatasets(cfgName string, dsn string, keyList string, verbose bool) error {
t0 := time.Now()
appName := path.Base(os.Args[0])
// Read in the configuration for this harvester instance.
cfg, err := LoadConfig(cfgName)
if err != nil {
return err
}
if cfg == nil {
return fmt.Errorf("could not create a configuration object")
}
if err := OpenJSONStore(cfg); err != nil {
return err
}
defer cfg.Jdb.Close()
if _, err := os.Stat(cfg.ProjectDir); os.IsNotExist(err) {
if err := os.MkdirAll(cfg.ProjectDir, 0775); err != nil {
log.Printf("%s", err)
os.Exit(1)
}
}
if verbose {
log.Printf("%s started %v", appName, time.Now().Sub(t0).Truncate(time.Second))
}
for repoName := range cfg.Repositories {
if err := generateDataset(cfg, repoName, dsn, cfg.ProjectDir, keyList, verbose); err != nil {
return err
}
}
if verbose {
log.Printf("%s run time %v", appName, time.Since(t0).Truncate(time.Second))
}
return nil
}