save honkmeta in a generic table since it seems we're gonna have plenty of it

This commit is contained in:
Ted Unangst 2019-10-02 17:10:01 -04:00
parent f96b70cb3e
commit 1595e1f9de
5 changed files with 104 additions and 24 deletions

View File

@ -22,6 +22,8 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"bytes"
"encoding/json"
"humungus.tedunangst.com/r/webs/login" "humungus.tedunangst.com/r/webs/login"
) )
@ -250,24 +252,36 @@ func donksforhonks(honks []*Honk) {
h.Onts = append(h.Onts, o) h.Onts = append(h.Onts, o)
} }
rows.Close() rows.Close()
// grab places // grab meta
q = fmt.Sprintf("select honkid, name, latitude, longitude, url from places where honkid in (%s)", strings.Join(ids, ",")) q = fmt.Sprintf("select honkid, genus, json from honkmeta where honkid in (%s)", strings.Join(ids, ","))
rows, err = db.Query(q) rows, err = db.Query(q)
if err != nil { if err != nil {
log.Printf("error querying places: %s", err) log.Printf("error querying honkmeta: %s", err)
return return
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var hid int64 var hid int64
p := new(Place) var genus, j string
err = rows.Scan(&hid, &p.Name, &p.Latitude, &p.Longitude, &p.Url) err = rows.Scan(&hid, &genus, &j)
if err != nil { if err != nil {
log.Printf("error scanning place: %s", err) log.Printf("error scanning honkmeta: %s", err)
continue continue
} }
h := hmap[hid] h := hmap[hid]
switch genus {
case "place":
p := new(Place)
err = unjsonify(j, p)
if err != nil {
log.Printf("error parsing place: %s", err)
continue
}
h.Place = p h.Place = p
case "oldrev":
default:
log.Printf("unknown meta genus: %s", genus)
}
} }
rows.Close() rows.Close()
} }
@ -317,7 +331,10 @@ func saveextras(h *Honk) error {
} }
} }
if p := h.Place; p != nil { if p := h.Place; p != nil {
_, err := stmtSavePlace.Exec(h.ID, p.Name, p.Latitude, p.Longitude, p.Url) j, err := jsonify(p)
if err != nil {
_, err = stmtSaveMeta.Exec(h.ID, "genus", j)
}
if err != nil { if err != nil {
log.Printf("error saving place: %s", err) log.Printf("error saving place: %s", err)
return err return err
@ -336,7 +353,7 @@ func deleteextras(honkid int64) {
if err != nil { if err != nil {
log.Printf("error deleting: %s", err) log.Printf("error deleting: %s", err)
} }
_, err = stmtDeletePlace.Exec(honkid) _, err = stmtDeleteMeta.Exec(honkid)
if err != nil { if err != nil {
log.Printf("error deleting: %s", err) log.Printf("error deleting: %s", err)
} }
@ -350,19 +367,41 @@ func deletehonk(honkid int64) {
} }
} }
func jsonify(what interface{}) (string, error) {
var buf bytes.Buffer
e := json.NewEncoder(&buf)
e.SetEscapeHTML(false)
e.SetIndent("", "")
err := e.Encode(what)
return buf.String(), err
}
func unjsonify(s string, dest interface{}) error {
d := json.NewDecoder(strings.NewReader(s))
err := d.Decode(dest)
return err
}
func updatehonk(h *Honk) { func updatehonk(h *Honk) {
old := getxonk(h.UserID, h.XID) old := getxonk(h.UserID, h.XID)
_, err := stmtSaveOld.Exec(old.ID, old.Precis, old.Noise) oldrev := OldRevision{Precis: old.Precis, Noise: old.Noise}
if err != nil {
log.Printf("error saving old: %s", err)
return
}
deleteextras(h.ID) deleteextras(h.ID)
dt := h.Date.UTC().Format(dbtimeformat) dt := h.Date.UTC().Format(dbtimeformat)
stmtUpdateHonk.Exec(h.Precis, h.Noise, h.Format, dt, h.ID) stmtUpdateHonk.Exec(h.Precis, h.Noise, h.Format, dt, h.ID)
saveextras(h) saveextras(h)
j, err := jsonify(&oldrev)
if err != nil {
log.Printf("error jsonify oldrev: %s", err)
return
}
_, err = stmtSaveMeta.Exec(old.ID, "oldrev", j)
if err != nil {
log.Printf("error saving oldrev: %s", err)
return
}
} }
func cleanupdb(arg string) { func cleanupdb(arg string) {
@ -384,7 +423,7 @@ func cleanupdb(arg string) {
doordie(db, "delete from honks where "+where, sqlargs...) doordie(db, "delete from honks where "+where, sqlargs...)
doordie(db, "delete from donks where honkid not in (select honkid from honks)") doordie(db, "delete from donks where honkid not in (select honkid from honks)")
doordie(db, "delete from onts where honkid not in (select honkid from honks)") doordie(db, "delete from onts where honkid not in (select honkid from honks)")
doordie(db, "delete from places where honkid not in (select honkid from honks)") doordie(db, "delete from honkmeta where honkid not in (select honkid from honks)")
doordie(db, "delete from filemeta where fileid not in (select fileid from donks)") doordie(db, "delete from filemeta where fileid not in (select fileid from donks)")
for _, u := range allusers() { for _, u := range allusers() {
@ -442,7 +481,7 @@ var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover, stmtOneHonker
var stmtThumbBiters, stmtDeleteHonk, stmtDeleteDonks, stmtDeleteOnts, stmtSaveZonker *sql.Stmt var stmtThumbBiters, stmtDeleteHonk, stmtDeleteDonks, stmtDeleteOnts, stmtSaveZonker *sql.Stmt
var stmtGetZonkers, stmtRecentHonkers, stmtGetXonker, stmtSaveXonker, stmtDeleteXonker *sql.Stmt var stmtGetZonkers, stmtRecentHonkers, stmtGetXonker, stmtSaveXonker, stmtDeleteXonker *sql.Stmt
var stmtSelectOnts, stmtSaveOnt, stmtUpdateFlags, stmtClearFlags *sql.Stmt var stmtSelectOnts, stmtSaveOnt, stmtUpdateFlags, stmtClearFlags *sql.Stmt
var stmtSavePlace, stmtDeletePlace, stmtHonksForUserFirstClass, stmtSaveOld, stmtUpdateHonk *sql.Stmt var stmtHonksForUserFirstClass, stmtSaveMeta, stmtDeleteMeta, stmtUpdateHonk *sql.Stmt
func preparetodie(db *sql.DB, s string) *sql.Stmt { func preparetodie(db *sql.DB, s string) *sql.Stmt {
stmt, err := db.Prepare(s) stmt, err := db.Prepare(s)
@ -477,12 +516,11 @@ func prepareStatements(db *sql.DB) {
stmtHonksByConvoy = preparetodie(db, selecthonks+"where (honks.userid = ? or (? = -1 and whofore = 2)) and convoy = ?"+limit) stmtHonksByConvoy = preparetodie(db, selecthonks+"where (honks.userid = ? or (? = -1 and whofore = 2)) and convoy = ?"+limit)
stmtHonksByOntology = preparetodie(db, selecthonks+"join onts on honks.honkid = onts.honkid where onts.ontology = ? and (honks.userid = ? or (? = -1 and honks.whofore = 2))"+limit) stmtHonksByOntology = preparetodie(db, selecthonks+"join onts on honks.honkid = onts.honkid where onts.ontology = ? and (honks.userid = ? or (? = -1 and honks.whofore = 2))"+limit)
stmtSaveOld = preparetodie(db, "insert into forsaken (honkid, precis, noise) values (?, ?, ?)") stmtSaveMeta = preparetodie(db, "insert into honkmeta (honkid, genus, json) values (?, ?, ?)")
stmtDeleteMeta = preparetodie(db, "delete from honkmeta where honkid = ?")
stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy, whofore, format, precis, oonker, flags) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)") stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy, whofore, format, precis, oonker, flags) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
stmtDeleteHonk = preparetodie(db, "delete from honks where honkid = ?") stmtDeleteHonk = preparetodie(db, "delete from honks where honkid = ?")
stmtUpdateHonk = preparetodie(db, "update honks set precis = ?, noise = ?, format = ?, dt = ? where honkid = ?") stmtUpdateHonk = preparetodie(db, "update honks set precis = ?, noise = ?, format = ?, dt = ? where honkid = ?")
stmtSavePlace = preparetodie(db, "insert into places (honkid, name, latitude, longitude, url) values (?, ?, ?, ?, ?)")
stmtDeletePlace = preparetodie(db, "delete from places where honkid = ?")
stmtSaveOnt = preparetodie(db, "insert into onts (ontology, honkid) values (?, ?)") stmtSaveOnt = preparetodie(db, "insert into onts (ontology, honkid) values (?, ?)")
stmtDeleteOnts = preparetodie(db, "delete from onts where honkid = ?") stmtDeleteOnts = preparetodie(db, "delete from onts where honkid = ?")
stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)") stmtSaveDonk = preparetodie(db, "insert into donks (honkid, fileid) values (?, ?)")

View File

@ -64,6 +64,11 @@ type Honk struct {
Place *Place Place *Place
} }
type OldRevision struct {
Precis string
Noise string
}
const ( const (
flagIsAcked = 1 flagIsAcked = 1
flagIsBonked = 2 flagIsBonked = 2

View File

@ -7,8 +7,7 @@ create table xonkers (xonkerid integer primary key, name text, info text, flavor
create table zonkers (zonkerid integer primary key, userid integer, name text, wherefore text); create table zonkers (zonkerid integer primary key, userid integer, name text, wherefore text);
create table doovers(dooverid integer primary key, dt text, tries integer, username text, rcpt text, msg blob); create table doovers(dooverid integer primary key, dt text, tries integer, username text, rcpt text, msg blob);
create table onts (ontology text, honkid integer); create table onts (ontology text, honkid integer);
create table forsaken (honkid integer, precis text, noise text); create table honkmeta (honkid integer, genus text, json text);
create table places (honkid integer, name text, latitude real, longitude real, url text);
create index idx_honksxid on honks(xid); create index idx_honksxid on honks(xid);
create index idx_honksconvoy on honks(convoy); create index idx_honksconvoy on honks(convoy);
@ -21,7 +20,7 @@ create index idx_filesxid on filemeta(xid);
create index idx_filesurl on filemeta(url); create index idx_filesurl on filemeta(url);
create index idx_ontology on onts(ontology); create index idx_ontology on onts(ontology);
create index idx_onthonkid on onts(honkid); create index idx_onthonkid on onts(honkid);
create index idx_placehonkid on places(honkid); create index idx_honkmetaid on honkmeta(honkid);
create table config (key text, value text); create table config (key text, value text);

View File

@ -27,7 +27,7 @@ import (
func doordie(db *sql.DB, s string, args ...interface{}) { func doordie(db *sql.DB, s string, args ...interface{}) {
_, err := db.Exec(s, args...) _, err := db.Exec(s, args...)
if err != nil { if err != nil {
log.Fatal(err) log.Fatalf("can't run %s: %s", s, err)
} }
} }
@ -239,6 +239,44 @@ func upgradedb() {
doordie(db, "update config set value = 22 where key = 'dbversion'") doordie(db, "update config set value = 22 where key = 'dbversion'")
fallthrough fallthrough
case 22: case 22:
doordie(db, "create table honkmeta (honkid integer, genus text, json text)")
doordie(db, "create index idx_honkmetaid on honkmeta(honkid)")
doordie(db, "drop table forsaken") // don't bother saving this one
rows, err := db.Query("select honkid, name, latitude, longitude, url from places")
if err != nil {
log.Fatal(err)
}
places := make(map[int64]*Place)
for rows.Next() {
var honkid int64
p := new(Place)
err = rows.Scan(&honkid, &p.Name, &p.Latitude, &p.Longitude, &p.Url)
if err != nil {
log.Fatal(err)
}
places[honkid] = p
}
rows.Close()
tx, err := db.Begin()
if err != nil {
log.Fatalf("can't begin: %s", err)
}
for honkid, p := range places {
j, err := jsonify(p)
_, err = tx.Exec("insert into honkmeta (honkid, genus, json) values (?, ?, ?)",
honkid, "place", j)
if err != nil {
log.Fatal(err)
}
}
err = tx.Commit()
if err != nil {
log.Fatalf("can't commit: %s", err)
}
doordie(db, "update config set value = 23 where key = 'dbversion'")
fallthrough
case 23:
default: default:
log.Fatalf("can't upgrade unknown version %d", dbversion) log.Fatalf("can't upgrade unknown version %d", dbversion)
} }

View File

@ -73,7 +73,7 @@ var alreadyopendb *sql.DB
var dbname = "honk.db" var dbname = "honk.db"
var blobdbname = "blob.db" var blobdbname = "blob.db"
var stmtConfig *sql.Stmt var stmtConfig *sql.Stmt
var myVersion = 22 var myVersion = 23
func initdb() { func initdb() {
schema, err := ioutil.ReadFile("schema.sql") schema, err := ioutil.ReadFile("schema.sql")