From 69a235a3d1c8189d21febfa48cd18e2be5ef85d6 Mon Sep 17 00:00:00 2001 From: Ted Unangst Date: Sun, 22 Sep 2019 17:00:14 -0400 Subject: [PATCH] don't store exploded onts in honks --- database.go | 36 ++++++++++++++++++++++++++++-------- schema.sql | 3 ++- upgradedb.go | 13 +++---------- util.go | 2 +- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/database.go b/database.go index 2f3de19..42eadee 100644 --- a/database.go +++ b/database.go @@ -181,9 +181,9 @@ type RowLike interface { func scanhonk(row RowLike) *Honk { h := new(Honk) - var dt, aud, onts string + var dt, aud string err := row.Scan(&h.ID, &h.UserID, &h.Username, &h.What, &h.Honker, &h.Oonker, &h.XID, &h.RID, - &dt, &h.URL, &aud, &h.Noise, &h.Precis, &h.Format, &h.Convoy, &h.Whofore, &h.Flags, &onts) + &dt, &h.URL, &aud, &h.Noise, &h.Precis, &h.Format, &h.Convoy, &h.Whofore, &h.Flags) if err != nil { if err != sql.ErrNoRows { log.Printf("error scanning honk: %s", err) @@ -193,9 +193,6 @@ func scanhonk(row RowLike) *Honk { h.Date, _ = time.Parse(dbtimeformat, dt) h.Audience = strings.Split(aud, " ") h.Public = !keepitquiet(h.Audience) - if len(onts) > 0 { - h.Onts = strings.Split(onts, " ") - } return h } @@ -207,6 +204,7 @@ func donksforhonks(honks []*Honk) { ids = append(ids, fmt.Sprintf("%d", h.ID)) hmap[h.ID] = h } + // grab donks q := fmt.Sprintf("select honkid, donks.fileid, xid, name, description, url, media, local from donks join files on donks.fileid = files.fileid where honkid in (%s)", strings.Join(ids, ",")) rows, err := db.Query(q) if err != nil { @@ -225,6 +223,28 @@ func donksforhonks(honks []*Honk) { h := hmap[hid] h.Donks = append(h.Donks, &d) } + rows.Close() + + // grab onts + q = fmt.Sprintf("select honkid, ontology from onts where honkid in (%s)", strings.Join(ids, ",")) + rows, err = db.Query(q) + if err != nil { + log.Printf("error querying onts: %s", err) + return + } + defer rows.Close() + for rows.Next() { + var hid int64 + var o string + err = rows.Scan(&hid, &o) + if err != nil { + log.Printf("error scanning donk: %s", err) + continue + } + h := hmap[hid] + h.Onts = append(h.Onts, o) + } + rows.Close() } func savehonk(h *Honk) error { @@ -233,7 +253,7 @@ func savehonk(h *Honk) error { res, err := stmtSaveHonk.Exec(h.UserID, h.What, h.Honker, h.XID, h.RID, dt, h.URL, aud, h.Noise, h.Convoy, h.Whofore, h.Format, h.Precis, - h.Oonker, h.Flags, strings.Join(h.Onts, " ")) + h.Oonker, h.Flags) if err != nil { log.Printf("err saving honk: %s", err) return err @@ -341,7 +361,7 @@ func prepareStatements(db *sql.DB) { stmtOneHonker = preparetodie(db, "select xid from honkers where name = ? and userid = ?") stmtDubbers = preparetodie(db, "select honkerid, userid, name, xid, flavor from honkers where userid = ? and flavor = 'dub'") - selecthonks := "select honks.honkid, honks.userid, username, what, honker, oonker, honks.xid, rid, dt, url, audience, noise, precis, format, convoy, whofore, flags, onts from honks join users on honks.userid = users.userid " + selecthonks := "select honks.honkid, honks.userid, username, what, honker, oonker, honks.xid, rid, dt, url, audience, noise, precis, format, convoy, whofore, flags from honks join users on honks.userid = users.userid " limit := " order by honks.honkid desc limit 250" butnotthose := " and convoy not in (select name from zonkers where userid = ? and wherefore = 'zonvoy' order by zonkerid desc limit 100)" stmtOneXonk = preparetodie(db, selecthonks+"where honks.userid = ? and xid = ?") @@ -358,7 +378,7 @@ func prepareStatements(db *sql.DB) { 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 (?, ?, ?)") - stmtSaveHonk = preparetodie(db, "insert into honks (userid, what, honker, xid, rid, dt, url, audience, noise, convoy, whofore, format, precis, oonker, flags, onts) 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 = ?") stmtUpdateHonk = preparetodie(db, "update honks set precis = ?, noise = ?, format = ?, dt = ? where honkid = ?") stmtSaveOnt = preparetodie(db, "insert into onts (ontology, honkid) values (?, ?)") diff --git a/schema.sql b/schema.sql index f59ec6f..952f8be 100644 --- a/schema.sql +++ b/schema.sql @@ -1,5 +1,5 @@ -create table honks (honkid integer primary key, userid integer, what text, honker text, xid text, rid text, dt text, url text, audience text, noise text, convoy text, whofore integer, format text, precis text, oonker text, flags integer, onts text); +create table honks (honkid integer primary key, userid integer, what text, honker text, xid text, rid text, dt text, url text, audience text, noise text, convoy text, whofore integer, format text, precis text, oonker text, flags integer); create table donks (honkid integer, fileid integer); create table files(fileid integer primary key, xid text, name text, description text, url text, media text, local integer, content blob); create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, combos text); @@ -19,6 +19,7 @@ create index idx_zonkersname on zonkers(name); create index idx_filesxid on files(xid); create index idx_filesurl on files(url); create index idx_ontology on onts(ontology); +create index idx_onthonkid on onts(honkid); create table config (key text, value text); diff --git a/upgradedb.go b/upgradedb.go index 5e81c2b..7e242e2 100644 --- a/upgradedb.go +++ b/upgradedb.go @@ -122,13 +122,11 @@ func upgradedb() { doordie(db, "update config set value = 14 where key = 'dbversion'") fallthrough case 14: - doordie(db, "alter table honks add column onts text") doordie(db, "create table onts (ontology text, honkid integer)") doordie(db, "create index idx_ontology on onts(ontology)") doordie(db, "update config set value = 15 where key = 'dbversion'") fallthrough case 15: - doordie(db, "update honks set onts = ''") doordie(db, "delete from onts") ontmap := make(map[int64][]string) rows, err := db.Query("select honkid, noise from honks") @@ -157,19 +155,11 @@ func upgradedb() { if err != nil { log.Fatalf("can't begin: %s", err) } - stmtHonk, err := tx.Prepare("update honks set onts = ? where honkid = ?") - if err != nil { - log.Fatal(err) - } stmtOnts, err := tx.Prepare("insert into onts (ontology, honkid) values (?, ?)") if err != nil { log.Fatal(err) } for honkid, onts := range ontmap { - _, err = stmtHonk.Exec(strings.Join(onts, " "), honkid) - if err != nil { - log.Fatal(err) - } for _, o := range onts { _, err = stmtOnts.Exec(strings.ToLower(o), honkid) if err != nil { @@ -193,6 +183,9 @@ func upgradedb() { doordie(db, "update config set value = 18 where key = 'dbversion'") fallthrough case 18: + doordie(db, "create index idx_onthonkid on onts(honkid)") + doordie(db, "update config set value = 19 where key = 'dbversion'") + case 19: default: log.Fatalf("can't upgrade unknown version %d", dbversion) } diff --git a/util.go b/util.go index e02ad0e..b3625a8 100644 --- a/util.go +++ b/util.go @@ -72,7 +72,7 @@ var dbtimeformat = "2006-01-02 15:04:05" var alreadyopendb *sql.DB var dbname = "honk.db" var stmtConfig *sql.Stmt -var myVersion = 18 +var myVersion = 19 func initdb() { schema, err := ioutil.ReadFile("schema.sql")