Created
January 3, 2018 09:37
-
-
Save tanji/8b801ebbb1cb8510cacbf05f6d1e6069 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"strings" | |
"sync" | |
"github.com/jmoiron/sqlx" | |
) | |
const maxPacketSize = 16777216 | |
func readDump(path string) { | |
file, err := os.Open(path) | |
if err != nil { | |
log.Println(err) | |
} | |
log.Println("Opened file", path, "for reading") | |
defer file.Close() | |
wg := new(sync.WaitGroup) | |
rows := make(chan string) | |
for w := 1; w <= 4; w++ { | |
wg.Add(1) | |
go insertRow(rows, wg, w) | |
} | |
go func() { | |
r := bufio.NewReaderSize(file, maxPacketSize) | |
for { | |
line, err := r.ReadString(10) | |
if err == io.EOF { | |
break | |
} | |
rows <- line | |
} | |
close(rows) | |
}() | |
// Wait for goroutines to finish | |
wg.Wait() | |
} | |
func insertRow(rows <-chan string, wg *sync.WaitGroup, w int) { | |
defer wg.Done() | |
db, err := sqlx.Connect("mysql", dsn) | |
if err != nil { | |
log.Println("Error connecting to database: ", err) | |
} | |
defer db.Close() | |
for row := range rows { | |
if strings.HasPrefix(row, "INSERT INTO") { | |
var srow string | |
if len(row) > 100 { | |
srow = row[:100] | |
} else { | |
srow = row | |
} | |
// This is our row to insert | |
fmt.Println(fmt.Sprintf("[%d]", w), srow) | |
_, err := db.Exec(row) | |
if err != nil { | |
log.Println("Error executing query: ", err) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment