-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.go
171 lines (151 loc) · 3.57 KB
/
seed.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
package main
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"time"
"database/sql"
"encoding/json"
"io/ioutil"
"path/filepath"
"sync/atomic"
_ "github.com/go-sql-driver/mysql"
"golang.org/x/sync/errgroup"
)
var rowBatchSize = 30
var numWorkers = 30
const wirelessFtpDir = "wirelessftp.fcc.gov/pub/uls/complete/%s/%s.dat"
// stub db.Exec for unit testing which queries are called
type execType = func(query string, args ...interface{}) (sql.Result, error)
func main() {
start := time.Now()
// Open a connection with no database so we can create one
db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:3306)/")
if err != nil {
panic(err)
}
if err := initTables(db.Exec); err != nil {
panic(err)
}
// Open a connection with the newly created Database
db, err = sql.Open("mysql", "root:root@/WirelessPA")
if err != nil {
panic(err)
}
defer db.Close()
if err := seedData(db.Exec); err != nil {
panic(err)
}
fmt.Println("Finished... took " + time.Since(start).String())
}
func initTables(exec execType) error {
fmt.Println("Initializing Database Tables")
path, err := filepath.Abs("init.sql")
if err != nil {
return err
}
c, err := ioutil.ReadFile(path)
if err != nil {
return err
}
queries := strings.Split(string(c), ";")
for _, query := range queries[:len(queries)-1] {
_, err = exec(strings.TrimSpace(query) + ";")
if err != nil {
return err
}
}
return nil
}
func seedData(exec execType) error {
path, err := filepath.Abs("config.json")
if err != nil {
return err
}
config, _ := ioutil.ReadFile(path)
var files map[string][]string
err = json.Unmarshal(config, &files)
if err != nil {
return err
}
for dir, dats := range files {
for _, dat := range dats {
if err := uploadDatFile(
fmt.Sprintf(wirelessFtpDir, dir, dat), dat, exec,
); err != nil {
return err
}
}
}
return nil
}
func uploadDatFile(filePath, table string, exec execType) error {
path, err := filepath.Abs(filePath)
if err != nil {
return err
}
fmt.Println("Importing data from " + path)
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
rows := make(chan string)
g, _ := errgroup.WithContext(context.TODO())
// atomic bool to sync when we're done
var finished int32
// fanning out consumers to run insert queries
for i := 0; i < numWorkers; i++ {
g.Go(func() error {
if err := queryBuilder(table, rows, &finished, exec); err != nil {
return err
}
return nil
})
}
// open the pipeline
g.Go(func() error {
defer close(rows)
for scanner.Scan() {
rows <- scanner.Text()
}
return scanner.Err()
})
return g.Wait()
}
func queryBuilder(table string, rows <-chan string, finished *int32, exec execType) error {
for atomic.LoadInt32(finished) == 0 {
q := buildInsertQuery(table, rows)
if q == "" {
atomic.StoreInt32(finished, 1)
return nil
}
if _, err := exec(q); err != nil {
fmt.Println("Error executing batch insert query:", err)
return err
}
}
return nil
}
func buildInsertQuery(table string, rows <-chan string) string {
insertLine := fmt.Sprintf("insert ignore into %s values ", table)
values := ""
rowCount := 0
for row := range rows {
row = strings.ReplaceAll(row, "'", "\\'")
values += fmt.Sprintf("('%s'), ", strings.ReplaceAll(row, "|", "', '"))
if rowCount >= rowBatchSize-1 {
values = values[:len(values)-2] // remove trailing comma and space
return insertLine + values
}
rowCount++
}
if rowCount == 0 {
return ""
}
values = values[:len(values)-2] // remove trailing comma and space
return insertLine + values
}