Skip to content

Instantly share code, notes, and snippets.

@ian-plosker
Created March 5, 2014 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian-plosker/6acf0e94451d47a2d8bc to your computer and use it in GitHub Desktop.
Save ian-plosker/6acf0e94451d47a2d8bc to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"client"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"sync"
)
var (
c = client.NewClient("550002c1-63fa-428b-a04a-8d35e64d05b7")
w sync.WaitGroup
)
func main() {
fileGlob := os.Args[1] + "/*"
if files, err := filepath.Glob(fileGlob); err != nil {
panic(err)
} else {
for _, file := range files {
go importFile(file)
w.Add(1)
}
w.Wait()
}
}
func importFile(filename string) {
defer w.Done()
file, err := os.Open(filename)
if err != nil {
log.Printf("Error: %v\n", err)
return
}
defer file.Close()
log.Printf("Importing %v ...", filename)
reader := bufio.NewReaderSize(file, 1024*1024)
var indexingConflicts int64 = 0
for err == nil {
var line string
line, err = reader.ReadString('\n')
if err != nil {
break
}
decoder := json.NewDecoder(strings.NewReader(line))
json := new(map[string]interface{})
decoder.Decode(json)
if err := c.Put(filepath.Base(filename), fmt.Sprintf("%v", (*json)["_id"].(map[string]interface{})["$oid"]), json); err != nil && err.(*client.OrchestrateError).StatusCode != 409 {
log.Panicf("Error: %#v\n", err)
} else if err != nil && err.(*client.OrchestrateError).StatusCode == 409 {
indexingConflicts++
}
}
if err != nil && err != io.EOF {
log.Panicf("Scanner error: %v\n", err)
}
log.Printf("Done importing %v (with %v indexing conflicts)", filename, indexingConflicts)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment