func GetDB() (*sql.DB, error) { p, err := ds.DefaultCachePath() if err != nil { return nil, err } err = mkDir(p) var version string if err != nil { return nil, err } fp := path.Join(p, fileName) mp := path.Join(p, metaName) if exists(fp) && exists(mp) { b, err := ioutil.ReadFile(mp) if err != nil { return nil, err } version = strings.Trim(string(b[:]), "\n\r") } err = updateDB(version, p) if err != nil { return nil, err } return sql.Open("sqlite3", fp) }
func main() { db, err := GetDB() if err != nil { log.Fatal(err) } games, err := queryDB(db, query) if err != nil { log.Fatal(err) } p, err := ds.DefaultCachePath() if err != nil { log.Fatal(err) } os.RemoveAll(path.Join(p, dbName)) if err != nil { log.Fatal(err) } ldb, err := leveldb.OpenFile(path.Join(p, dbName), nil) if err != nil { log.Fatal(err) } defer ldb.Close() for _, g := range games { b, err := marshalGame(g) if err != nil { log.Fatal(err) } batch := new(leveldb.Batch) i := []byte(g.RomID) h := []byte(g.Hash) hn := []byte(g.Hash + "-name") fn := []byte(strings.ToLower(g.FileName)) batch.Put(i, b) batch.Put(h, i) batch.Put(fn, i) batch.Put(hn, fn) err = ldb.Write(batch, nil) if err != nil { log.Fatal(err) } } }
func main() { bioRE := regexp.MustCompile(`- (CAST|CONTRIBUTE|PORTS|SCORING|SERIES|STAFF|TECHNICAL|TRIVIA|UPDATES) -`) f, err := os.Open("history.dat") if err != nil { log.Fatal(err) } defer f.Close() scanner := New(f) p, err := ds.DefaultCachePath() if err != nil { log.Fatal(err) } os.RemoveAll(filepath.Join(p, dbName)) if err != nil { log.Fatal(err) } ldb, err := leveldb.OpenFile(filepath.Join(p, dbName), nil) if err != nil { log.Fatal(err) } defer ldb.Close() var count int for { e, err := scanner.Scan() if err != nil { if err == io.EOF { break } log.Fatal(err) } rID := make([]byte, 4) for { _, err = rand.Read(rID) if err != nil { log.Fatal("error:", err) } ok, err := ldb.Has(rID, nil) if err != nil { log.Fatal(err) } if !ok { break } } batch := new(leveldb.Batch) i := []byte(e.Name) bparts := bioRE.Split(e.Bio, 2) d := []byte(bparts[0]) batch.Put(i, rID) for _, cs := range e.Clones { c := []byte(cs) batch.Put(c, rID) } batch.Put(rID, d) err = ldb.Write(batch, nil) if err != nil { log.Fatal(err) } count++ } fmt.Printf("Found: %d\n", count) }