Last active
August 29, 2015 14:02
-
-
Save ian-plosker/5ff1fa0e506bea9f7451 to your computer and use it in GitHub Desktop.
This file contains 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" | |
"encoding/json" | |
"flag" | |
"github.com/orchestrate-io/gorc" | |
"io" | |
"log" | |
"os" | |
"path/filepath" | |
"strings" | |
"sync" | |
) | |
var ( | |
apiKey = flag.String("key", "550002c1-63fa-428b-a04a-8d35e64d05b7", "the api key") | |
retries = flag.Int("retries", 3, "the number retry attempts") | |
workerCount = flag.Int("workers", 20, "the number of worker procs") | |
primaryKey = flag.String("primaryKey", "_id", "the primary key") | |
wg sync.WaitGroup | |
reqs = make(chan Request, 100) | |
c *gorc.Client | |
) | |
type Request struct { | |
collection string | |
value string | |
respChan chan Response | |
} | |
type Response struct { | |
bytes int | |
err error | |
} | |
type IndexingConflictError struct { | |
orchestrateError *gorc.OrchestrateError | |
} | |
func (e IndexingConflictError) Error() string { | |
return e.orchestrateError.Error() | |
} | |
func main() { | |
flag.Parse() | |
c = gorc.NewClient(*apiKey) | |
startRequestHandlerPool() | |
for _, file := range flag.Args() { | |
wg.Add(1) | |
go func(file string) { | |
importFile(file) | |
}(file) | |
} | |
wg.Wait() | |
close(reqs) | |
} | |
func startRequestHandlerPool() { | |
for i := 0; i < *workerCount; i++ { | |
go handleRequests(reqs) | |
} | |
} | |
func importFile(filename string) { | |
file, err := os.Open(filename) | |
if err != nil { | |
log.Printf("Error: %v\n", err) | |
return | |
} | |
defer file.Close() | |
collection := filepath.Base(filename) | |
stats, _ := file.Stat() | |
fileSize := stats.Size() | |
log.Printf("Importing %v to %v ...", filename, collection) | |
reader := bufio.NewReaderSize(file, 1024*1024) | |
var resps = make(chan Response, 100) | |
go handleResponses(filename, fileSize, resps) | |
for err == nil { | |
var line string | |
line, err = reader.ReadString('\n') | |
reqs <- Request{collection, line, resps} | |
} | |
if err != nil && err != io.EOF { | |
log.Panicf("Scanner error: %v\n", err) | |
} | |
} | |
func handleRequests(reqs chan Request) { | |
for req := range reqs { | |
decoder := json.NewDecoder(strings.NewReader(req.value)) | |
json := new(map[string]interface{}) | |
err := decoder.Decode(json) | |
if err == nil { | |
var key string | |
if *primaryKey == "_id" { | |
key = (*json)["_id"].(map[string]interface{})["$oid"].(string) | |
} else { | |
key = (*json)[*primaryKey].(string) | |
} | |
for try := 1; try <= *retries; try++ { | |
if _, err = c.Put(req.collection, key, json); err != nil { | |
switch e := err.(type) { | |
case *gorc.OrchestrateError: | |
if e.StatusCode == 409 { | |
err = IndexingConflictError{err.(*gorc.OrchestrateError)} | |
} | |
default: | |
log.Printf("Error %v/%v (attempt %v of %v): %v\n", req.collection, key, try, *retries, err) | |
continue | |
} | |
} | |
if try > 1 { | |
log.Printf("%v/%v successful after %v attempts", req.collection, key, try) | |
} | |
break | |
} | |
} | |
req.respChan <- Response{len(req.value), err} | |
} | |
} | |
func handleResponses(filename string, fileSize int64, resps chan Response) { | |
var indexingConflicts, importCount, errorCount int | |
var bytesImported int64 | |
for resp := range resps { | |
switch err := resp.err.(type) { | |
case IndexingConflictError: | |
indexingConflicts++ | |
case gorc.OrchestrateError: | |
errorCount++ | |
log.Printf("Error: %v", err.Error()) | |
} | |
importCount++ | |
bytesImported += int64(resp.bytes) | |
if importCount%1000 == 0 { | |
percentComplete := float64(bytesImported) / float64(fileSize) * 100 | |
log.Printf("Progress imported %v items from %v (%.3f%% complete)", importCount, filename, percentComplete) | |
} | |
if bytesImported == fileSize { | |
close(resps) | |
} | |
} | |
log.Printf("Done importing %v items from %v (with %v indexing conflicts and %v errors)", importCount, filename, indexingConflicts, errorCount) | |
wg.Done() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment