2020-10-19 12:33:20 +00:00
|
|
|
package main
|
|
|
|
|
2020-10-21 12:35:04 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2020-11-08 01:29:36 +00:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2020-10-21 12:35:04 +00:00
|
|
|
)
|
|
|
|
|
2020-11-08 21:34:29 +00:00
|
|
|
const insertMessageQuery = "INSERT INTO messages(author, depth, kind, prev, signature, mhash) VALUES(?1, ?2, ?3, ?4, ?5, ?6)"
|
2020-11-08 01:29:36 +00:00
|
|
|
const insertBodyItemQuery = "INSERT INTO body_items(parent, key, value, rank) VALUES(?1, ?2, ?3, ?4)"
|
|
|
|
|
2020-10-28 13:06:56 +00:00
|
|
|
func ingestOneMessage(msg pigeonMessage, blobIndex map[string]bool) {
|
|
|
|
if getPeerStatus(msg.author) == following {
|
2020-11-08 01:29:36 +00:00
|
|
|
tx, err1 := getDB().Begin()
|
|
|
|
check(err1, "ingestOneMessage: Can't open DB: %s", err1)
|
2020-11-08 19:24:35 +00:00
|
|
|
mhash := formatMessage(msg).mhash
|
2020-11-08 15:39:17 +00:00
|
|
|
results, err2 := tx.Exec(insertMessageQuery,
|
|
|
|
msg.author,
|
|
|
|
msg.depth,
|
|
|
|
msg.kind,
|
|
|
|
msg.prev,
|
|
|
|
msg.signature,
|
|
|
|
mhash)
|
|
|
|
rollbackCheck(tx, err2, "Failed to save message %s. %s", msg.signature, err2)
|
2020-11-08 01:29:36 +00:00
|
|
|
parent, err3 := results.LastInsertId()
|
|
|
|
rollbackCheck(tx, err3, "Failed to get last ID for message %s", msg.signature)
|
|
|
|
|
|
|
|
for rank, pair := range msg.body {
|
|
|
|
_, err4 := tx.Exec(insertBodyItemQuery, parent, pair.key, pair.value, rank)
|
|
|
|
if err4 != nil {
|
|
|
|
fmt.Printf("%s", err4)
|
|
|
|
}
|
|
|
|
rollbackCheck(tx, err4, "Failed to insert body item %d of %s", rank, msg.signature)
|
|
|
|
}
|
|
|
|
err5 := tx.Commit()
|
|
|
|
check(err5, "Failed to commit message %s", msg.signature)
|
2020-10-28 13:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ingestManyMessages takes an array of Pigeon messages
|
2020-10-21 12:35:04 +00:00
|
|
|
and adds them to the local database, assuming that they are
|
2020-11-08 01:29:36 +00:00
|
|
|
messages of interest and assuming that they do not already
|
|
|
|
exist in the database. */
|
2020-10-28 13:06:56 +00:00
|
|
|
func ingestManyMessages(outp parserOutput) {
|
|
|
|
for _, message := range outp.messages {
|
|
|
|
ingestOneMessage(message, outp.blobIndex)
|
2020-10-21 12:35:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 12:33:20 +00:00
|
|
|
|
2020-11-08 01:29:36 +00:00
|
|
|
func ingestBlobs(p string, blobIndex map[string]bool) {
|
|
|
|
dir, _ := path.Split(p)
|
|
|
|
wildcard := path.Join(dir, "*.blb")
|
|
|
|
blobPaths, err1 := filepath.Glob(wildcard)
|
|
|
|
check(err1, "Blob wildcard failure %s", wildcard)
|
|
|
|
for _, blobPath := range blobPaths {
|
|
|
|
mhash, data := getMhashForFile(blobPath)
|
|
|
|
|
|
|
|
if blobIndex[mhash] {
|
|
|
|
addBlob(mhash, data)
|
|
|
|
blobIndex[mhash] = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:33:20 +00:00
|
|
|
func importBundle(path string) error {
|
|
|
|
// Get messages.pgn file
|
2020-10-21 12:35:04 +00:00
|
|
|
dat, err1 := ioutil.ReadFile(path)
|
2020-10-28 13:06:56 +00:00
|
|
|
check(err1, "Problem opening bundle %s. Error: %s", path, err1)
|
|
|
|
outp, err2 := parseMessage(string(dat))
|
2020-10-21 12:35:04 +00:00
|
|
|
check(err2, "Failed to parse %s. Error: %s", path, err2)
|
2020-10-28 13:06:56 +00:00
|
|
|
ingestManyMessages(outp)
|
2020-11-08 01:29:36 +00:00
|
|
|
ingestBlobs(path, outp.blobIndex)
|
|
|
|
return nil
|
2020-10-19 12:33:20 +00:00
|
|
|
}
|