mirror of https://github.com/velour/catbase.git
104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"github.com/jmoiron/sqlx"
|
|
bh "github.com/timshannon/bolthold"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
type Babbler struct {
|
|
BabblerID uint64 `db:"id" boltholdKey:"BabblerID"`
|
|
Name string `db:"babbler"`
|
|
}
|
|
|
|
type BabblerWord struct {
|
|
WordID uint64 `db:"id" boltholdKey:"WordID"`
|
|
Word string `db:"word"`
|
|
}
|
|
|
|
type BabblerNode struct {
|
|
NodeID uint64 `db:"id" boltholdKey:"NodeID"`
|
|
BabblerID uint64 `db:"babblerId"`
|
|
WordID uint64 `db:"wordId"`
|
|
Root uint64 `db:"root"`
|
|
RootFrequency int64 `db:"rootFrequency"`
|
|
}
|
|
|
|
type BabblerArc struct {
|
|
ArcID uint64 `db:"id" boltholdKey:"ArcID"`
|
|
FromNodeID uint64 `db:"fromNodeId"`
|
|
ToNodeID uint64 `db:"toNodeId"`
|
|
Frequency int64 `db:"frequency"`
|
|
}
|
|
|
|
func migrateBabbler(db *sqlx.DB, store *bh.Store) error {
|
|
allBabblers := []Babbler{}
|
|
err := db.Select(&allBabblers, `select * from babblers`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t0 := time.Now()
|
|
for _, b := range allBabblers {
|
|
err := store.Insert(b.BabblerID, b)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
log.Printf("Migrated %d babblers took %v", len(allBabblers), time.Now().Sub(t0))
|
|
|
|
allNodes := []BabblerNode{}
|
|
err = db.Select(&allNodes, `select * from babblerNodes`)
|
|
if err != nil && err != sql.ErrNoRows {
|
|
return err
|
|
}
|
|
if err == sql.ErrNoRows || len(allNodes) == 0 {
|
|
log.Printf("Finished all windows")
|
|
return nil
|
|
}
|
|
log.Printf("Have %d nodes to insert", len(allNodes))
|
|
nodeMap := map[uint64]bool{}
|
|
for _, n := range allNodes {
|
|
if nodeMap[n.NodeID] {
|
|
log.Fatalf("Duplicate ID: %v: %#v", n.NodeID, n)
|
|
}
|
|
nodeMap[n.NodeID] = true
|
|
}
|
|
for _, b := range allNodes {
|
|
err := store.Insert(b.NodeID, b)
|
|
if err != nil {
|
|
log.Printf("error trying to insert node: %#v", b)
|
|
}
|
|
}
|
|
log.Printf("Migrated %d nodes", len(allNodes))
|
|
|
|
allWords := []BabblerWord{}
|
|
err = db.Select(&allWords, `select * from babblerWords`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, b := range allWords {
|
|
err := store.Insert(b.WordID, b)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
log.Printf("Migrated %d words", len(allWords))
|
|
|
|
allArcs := []BabblerArc{}
|
|
err = db.Select(&allArcs, `select * from babblerArcs`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, b := range allArcs {
|
|
err := store.Insert(b.ArcID, b)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
log.Printf("Migrated %d arcs", len(allArcs))
|
|
|
|
return nil
|
|
}
|