less awkard use of xonkers table

This commit is contained in:
Ted Unangst 2019-06-03 00:15:06 -04:00
parent 156a4029a2
commit f29f31d32c
6 changed files with 24 additions and 17 deletions

View File

@ -344,9 +344,9 @@ func getboxes(ident string) (*Box, error) {
return b, nil return b, nil
} }
row := stmtGetBoxes.QueryRow(ident) var info string
b = &Box{} row := stmtGetXonker.QueryRow(ident, "boxes")
err := row.Scan(&b.In, &b.Out, &b.Shared) err := row.Scan(&info)
if err != nil { if err != nil {
j, err := GetJunk(ident) j, err := GetJunk(ident)
if err != nil { if err != nil {
@ -357,12 +357,17 @@ func getboxes(ident string) (*Box, error) {
sbox, _ := jsonfindstring(j, []string{"endpoints", "sharedInbox"}) sbox, _ := jsonfindstring(j, []string{"endpoints", "sharedInbox"})
b = &Box{In: inbox, Out: outbox, Shared: sbox} b = &Box{In: inbox, Out: outbox, Shared: sbox}
if inbox != "" { if inbox != "" {
_, err = stmtSaveBoxes.Exec(ident, inbox, outbox, sbox, "") m := strings.Join([]string{inbox, outbox, sbox}, " ")
_, err = stmtSaveXonker.Exec(ident, m, "boxes")
if err != nil { if err != nil {
log.Printf("error saving boxes: %s", err) log.Printf("error saving boxes: %s", err)
} }
} }
} else {
m := strings.Split(info, " ")
b = &Box{In: m[0], Out: m[1], Shared: m[2]}
} }
boxlock.Lock() boxlock.Lock()
boxofboxes[ident] = b boxofboxes[ident] = b
boxlock.Unlock() boxlock.Unlock()
@ -889,8 +894,7 @@ func gofish(name string) string {
if ok { if ok {
return ref return ref
} }
db := opendatabase() row := stmtGetXonker.QueryRow(name, "fishname")
row := db.QueryRow("select ibox from xonkers where xid = ?", name)
var href string var href string
err := row.Scan(&href) err := row.Scan(&href)
if err == nil { if err == nil {
@ -914,8 +918,7 @@ func gofish(name string) string {
rel, _ := jsongetstring(l, "rel") rel, _ := jsongetstring(l, "rel")
t, _ := jsongetstring(l, "type") t, _ := jsongetstring(l, "type")
if rel == "self" && friendorfoe(t) { if rel == "self" && friendorfoe(t) {
db.Exec("insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)", stmtSaveXonker.Exec(name, href, "fishname")
name, href, "", "", "")
handlock.Lock() handlock.Lock()
handfull[name] = href handfull[name] = href
handlock.Unlock() handlock.Unlock()

5
fun.go
View File

@ -394,8 +394,7 @@ func zaggy(keyname string) (key *rsa.PublicKey) {
if key != nil { if key != nil {
return return
} }
db := opendatabase() row := stmtGetXonker.QueryRow(keyname, "pubkey")
row := db.QueryRow("select pubkey from xonkers where xid = ?", keyname)
var data string var data string
err := row.Scan(&data) err := row.Scan(&data)
if err != nil { if err != nil {
@ -421,7 +420,7 @@ func zaggy(keyname string) (key *rsa.PublicKey) {
log.Printf("error decoding %s pubkey: %s", keyname, err) log.Printf("error decoding %s pubkey: %s", keyname, err)
return return
} }
_, err = stmtSaveBoxes.Exec(keyname, "", "", "", data) _, err = stmtSaveXonker.Exec(keyname, data, "pubkey")
if err != nil { if err != nil {
log.Printf("error saving key: %s", err) log.Printf("error saving key: %s", err)
} }

View File

@ -1354,7 +1354,7 @@ var stmtHonksByHonker, stmtSaveHonk, stmtFileData, stmtWhatAbout *sql.Stmt
var stmtFindXonk, stmtSaveDonk, stmtFindFile, stmtSaveFile *sql.Stmt var stmtFindXonk, stmtSaveDonk, stmtFindFile, stmtSaveFile *sql.Stmt
var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover *sql.Stmt var stmtAddDoover, stmtGetDoovers, stmtLoadDoover, stmtZapDoover *sql.Stmt
var stmtHasHonker, stmtThumbBiters, stmtZonkIt, stmtZonkDonks, stmtSaveZonker *sql.Stmt var stmtHasHonker, stmtThumbBiters, stmtZonkIt, stmtZonkDonks, stmtSaveZonker *sql.Stmt
var stmtGetBoxes, stmtSaveBoxes *sql.Stmt var stmtGetXonker, stmtSaveXonker *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)
@ -1400,8 +1400,8 @@ func prepareStatements(db *sql.DB) {
stmtZapDoover = preparetodie(db, "delete from doovers where dooverid = ?") stmtZapDoover = preparetodie(db, "delete from doovers where dooverid = ?")
stmtThumbBiters = preparetodie(db, "select userid, name, wherefore from zonkers where (wherefore = 'zonker' or wherefore = 'zurl' or wherefore = 'zword')") stmtThumbBiters = preparetodie(db, "select userid, name, wherefore from zonkers where (wherefore = 'zonker' or wherefore = 'zurl' or wherefore = 'zword')")
stmtSaveZonker = preparetodie(db, "insert into zonkers (userid, name, wherefore) values (?, ?, ?)") stmtSaveZonker = preparetodie(db, "insert into zonkers (userid, name, wherefore) values (?, ?, ?)")
stmtGetBoxes = preparetodie(db, "select ibox, obox, sbox from xonkers where xid = ?") stmtGetXonker = preparetodie(db, "select info from xonkers where name = ? and flavor = ?")
stmtSaveBoxes = preparetodie(db, "insert into xonkers (xid, ibox, obox, sbox, pubkey) values (?, ?, ?, ?, ?)") stmtSaveXonker = preparetodie(db, "insert into xonkers (name, info, flavor) values (?, ?, ?)")
} }
func ElaborateUnitTests() { func ElaborateUnitTests() {

View File

@ -3,7 +3,7 @@ create table honks (honkid integer primary key, userid integer, what text, honke
create table donks (honkid integer, fileid integer); create table donks (honkid integer, fileid integer);
create table files(fileid integer primary key, xid text, name text, url text, media text, local integer, content blob); create table files(fileid integer primary key, xid text, name 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, pubkey text); create table honkers (honkerid integer primary key, userid integer, name text, xid text, flavor text, combos text, pubkey text);
create table xonkers (xonkerid integer primary key, xid text, ibox text, obox text, sbox text, pubkey text); create table xonkers (xonkerid integer primary key, name text, info text, flavor text);
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);
@ -11,7 +11,7 @@ create index idx_honksxid on honks(xid);
create index idx_honksconvoy on honks(convoy); create index idx_honksconvoy on honks(convoy);
create index idx_honkshonker on honks(honker); create index idx_honkshonker on honks(honker);
create index idx_honkerxid on honkers(xid); create index idx_honkerxid on honkers(xid);
create index idx_xonkerxid on xonkers(xid); create index idx_xonkername on xonkers(name);
create index idx_zonkersname on zonkers(name); create index idx_zonkersname on zonkers(name);
create index idx_filesxid on files(xid); create index idx_filesxid on files(xid);
create index idx_filesurl on files(url); create index idx_filesurl on files(url);

View File

@ -93,6 +93,11 @@ func upgradedb() {
doordie(db, "update files set local = 1") doordie(db, "update files set local = 1")
doordie(db, "update config set value = 9 where key = 'dbversion'") doordie(db, "update config set value = 9 where key = 'dbversion'")
case 9: case 9:
doordie(db, "drop table xonkers")
doordie(db, "create table xonkers (xonkerid integer primary key, name text, info text, flavor text)")
doordie(db, "create index idx_xonkername on xonkers(name)")
doordie(db, "update config set value = 10 where key = 'dbversion'")
case 10:
default: default:
log.Fatalf("can't upgrade unknown version %d", dbversion) log.Fatalf("can't upgrade unknown version %d", dbversion)
} }

View File

@ -71,7 +71,7 @@ var dbtimeformat = "2006-01-02 15:04:05"
var alreadyopendb *sql.DB var alreadyopendb *sql.DB
var dbname = "honk.db" var dbname = "honk.db"
var stmtConfig *sql.Stmt var stmtConfig *sql.Stmt
var myVersion = 9 var myVersion = 10
func initdb() { func initdb() {
schema, err := ioutil.ReadFile("schema.sql") schema, err := ioutil.ReadFile("schema.sql")