modestly better search, with parameters

This commit is contained in:
Ted Unangst 2019-10-15 23:27:30 -04:00
parent d405d02e6f
commit fcd4170bb2
5 changed files with 59 additions and 14 deletions

View File

@ -52,6 +52,8 @@ func butwhatabout(name string) (*WhatAbout, error) {
return user, nil return user, nil
} }
var honkerinvalidator cache.Invalidator
func gethonkers(userid int64) []*Honker { func gethonkers(userid int64) []*Honker {
rows, err := stmtHonkers.Query(userid) rows, err := stmtHonkers.Query(userid)
if err != nil { if err != nil {
@ -195,8 +197,35 @@ func gethonksbyconvoy(userid int64, convoy string) []*Honk {
return honks return honks
} }
func gethonksbysearch(userid int64, q string) []*Honk { func gethonksbysearch(userid int64, q string) []*Honk {
q = "%" + q + "%" honker := ""
rows, err := stmtHonksBySearch.Query(userid, q, userid) withhonker := 0
site := ""
withsite := 0
terms := strings.Split(q, " ")
q = "%"
for _, t := range terms {
if strings.HasPrefix(t, "site:") {
site = t[5:]
site = "%" + site + "%"
withsite = 1
continue
}
if strings.HasPrefix(t, "honker:") {
honker = t[7:]
xid := fullname(honker, userid)
if xid != "" {
honker = xid
}
withhonker = 1
continue
}
if q[0] != '%' {
q += " "
}
q += t
}
q += "%"
rows, err := stmtHonksBySearch.Query(userid, withsite, site, withhonker, honker, honker, q, userid)
honks := getsomehonks(rows, err) honks := getsomehonks(rows, err)
return honks return honks
} }
@ -647,7 +676,7 @@ func prepareStatements(db *sql.DB) {
stmtHonksByHonker = preparetodie(db, selecthonks+"join honkers on (honkers.xid = honks.honker or honkers.xid = honks.oonker) where honks.userid = ? and honkers.name = ?"+butnotthose+limit) stmtHonksByHonker = preparetodie(db, selecthonks+"join honkers on (honkers.xid = honks.honker or honkers.xid = honks.oonker) where honks.userid = ? and honkers.name = ?"+butnotthose+limit)
stmtHonksByXonker = preparetodie(db, selecthonks+" where honks.userid = ? and (honker = ? or oonker = ?)"+butnotthose+limit) stmtHonksByXonker = preparetodie(db, selecthonks+" where honks.userid = ? and (honker = ? or oonker = ?)"+butnotthose+limit)
stmtHonksByCombo = preparetodie(db, selecthonks+"join honkers on honkers.xid = honks.honker where honks.userid = ? and honkers.combos like ?"+butnotthose+limit) stmtHonksByCombo = preparetodie(db, selecthonks+"join honkers on honkers.xid = honks.honker where honks.userid = ? and honkers.combos like ?"+butnotthose+limit)
stmtHonksBySearch = preparetodie(db, selecthonks+"where honks.userid = ? and noise like ?"+butnotthose+limit) stmtHonksBySearch = preparetodie(db, selecthonks+"where honks.userid = ? and (? = 0 or xid like ?) and (? = 0 or honks.honker = ? or honks.oonker = ?) and noise like ?"+butnotthose+limit)
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)

27
fun.go
View File

@ -350,10 +350,9 @@ func quickrename(s string, userid int64) string {
m = m[:len(m)-1] m = m[:len(m)-1]
} }
row := stmtOneHonker.QueryRow(m, userid) xid := fullname(m, userid)
var xid string
err := row.Scan(&xid) if xid != "" {
if err == nil {
_, name := handles(xid) _, name := handles(xid)
if name != "" { if name != "" {
nonstop = true nonstop = true
@ -373,7 +372,7 @@ var shortnames = cache.New(cache.Options{Filler: func(userid int64) (map[string]
m[h.XID] = h.Name m[h.XID] = h.Name
} }
return m, true return m, true
}}) }, Invalidator: &honkerinvalidator})
func shortname(userid int64, xid string) string { func shortname(userid int64, xid string) string {
var m map[string]string var m map[string]string
@ -384,6 +383,24 @@ func shortname(userid int64, xid string) string {
return "" return ""
} }
var fullnames = cache.New(cache.Options{Filler: func(userid int64) (map[string]string, bool) {
honkers := gethonkers(userid)
m := make(map[string]string)
for _, h := range honkers {
m[h.Name] = h.XID
}
return m, true
}, Invalidator: &honkerinvalidator})
func fullname(name string, userid int64) string {
var m map[string]string
ok := fullnames.Get(userid, &m)
if ok {
return m[name]
}
return ""
}
func mentionize(s string) string { func mentionize(s string) string {
s = re_mentions.ReplaceAllStringFunc(s, func(m string) string { s = re_mentions.ReplaceAllStringFunc(s, func(m string) string {
where := gofish(m) where := gofish(m)

2
go.mod
View File

@ -7,7 +7,7 @@ require (
golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4 golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 golang.org/x/net v0.0.0-20190620200207-3b0461eec859
humungus.tedunangst.com/r/go-sqlite3 v1.1.3 humungus.tedunangst.com/r/go-sqlite3 v1.1.3
humungus.tedunangst.com/r/webs v0.6.10 humungus.tedunangst.com/r/webs v0.6.11
) )
go 1.11 go 1.11

4
go.sum
View File

@ -21,5 +21,5 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
humungus.tedunangst.com/r/go-sqlite3 v1.1.3 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9ga8k38geUI= humungus.tedunangst.com/r/go-sqlite3 v1.1.3 h1:G2N4wzDS0NbuvrZtQJhh4F+3X+s7BF8b9ga8k38geUI=
humungus.tedunangst.com/r/go-sqlite3 v1.1.3/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M= humungus.tedunangst.com/r/go-sqlite3 v1.1.3/go.mod h1:FtEEmQM7U2Ey1TuEEOyY1BmphTZnmiEjPsNLEAkpf/M=
humungus.tedunangst.com/r/webs v0.6.10 h1:Qe4QW/7us8szAwL68zoIihfkaqsy/7Ys00bfdSPH5/g= humungus.tedunangst.com/r/webs v0.6.11 h1:go1OtvHRYveCPtH28vajw1CbdL9whtJyG0+DKn6aQcI=
humungus.tedunangst.com/r/webs v0.6.10/go.mod h1:Ho+nmafD/aUWF7LnH+Yl2/b0ob7f2pCkXm4onteWvLE= humungus.tedunangst.com/r/webs v0.6.11/go.mod h1:OXm4OzXMNIw95czKlkM51DPFKzgo+jrQ58+pINVWmWE=

5
web.go
View File

@ -1296,7 +1296,7 @@ var combocache = cache.New(cache.Options{Filler: func(userid int64) ([]string, b
combos = oneofakind(combos) combos = oneofakind(combos)
sort.Strings(combos) sort.Strings(combos)
return combos, true return combos, true
}}) }, Invalidator: &honkerinvalidator})
func showcombos(w http.ResponseWriter, r *http.Request) { func showcombos(w http.ResponseWriter, r *http.Request) {
userinfo := login.GetUserInfo(r) userinfo := login.GetUserInfo(r)
@ -1318,8 +1318,7 @@ func submithonker(w http.ResponseWriter, r *http.Request) {
combos = " " + combos + " " combos = " " + combos + " "
honkerid, _ := strconv.ParseInt(r.FormValue("honkerid"), 10, 0) honkerid, _ := strconv.ParseInt(r.FormValue("honkerid"), 10, 0)
defer combocache.Clear(u.UserID) defer honkerinvalidator.Clear(u.UserID)
defer shortnames.Clear(u.UserID)
if honkerid > 0 { if honkerid > 0 {
goodbye := r.FormValue("goodbye") goodbye := r.FormValue("goodbye")