1
0
miroir de https://github.com/quitesimpleorg/hs9001.git synchronisé 2024-11-25 03:32:36 +01:00

bash-ctrlr: Improve reverse bash search performance

Remove hack that only starts searching when two chars have been entered

Improve performance by adding LIMIT clause instead.
Cette révision appartient à :
Albert S. 2021-08-19 21:30:04 +02:00
Parent 1a6c75cea9
révision 3a6a1b2aa9
2 fichiers modifiés avec 26 ajouts et 15 suppressions

Voir le fichier

@ -12,16 +12,15 @@ type history struct {
}
func (h *history) GetHistoryByPrefix(prefix string) (ph []string) {
/* Hack for performance reasons */
if len(prefix) < 2 {
return
}
opts := searchopts{}
opts.order = "DESC"
o := "DESC"
opts.order = &o
lim := 100
opts.limit = &lim
cmdqry := prefix + "%"
opts.command = &cmdqry
results := search(h.conn, opts)
for e := results.Front(); e != nil; e = e.Next() {
for e := results.Back(); e != nil; e = e.Prev() {
entry, ok := e.Value.(*HistoryEntry)
if !ok {
log.Panic("Failed to retrieve entries")
@ -31,16 +30,15 @@ func (h *history) GetHistoryByPrefix(prefix string) (ph []string) {
return
}
func (h *history) GetHistoryByPattern(pattern string) (ph []string, pos []int) {
/* Hack for performance reasons */
if len(pattern) < 2 {
return
}
opts := searchopts{}
opts.order = "DESC"
o := "DESC"
opts.order = &o
lim := 100
opts.limit = &lim
cmdqry := "%" + pattern + "%"
opts.command = &cmdqry
results := search(h.conn, opts)
for e := results.Front(); e != nil; e = e.Next() {
for e := results.Back(); e != nil; e = e.Prev() {
entry, ok := e.Value.(*HistoryEntry)
if !ok {
log.Panic("Failed to retrieve entries")

19
main.go
Voir le fichier

@ -157,7 +157,8 @@ type searchopts struct {
after *time.Time
before *time.Time
retval *int
order string
order *string
limit *int
}
func search(conn *sql.DB, opts searchopts) list.List {
@ -188,7 +189,18 @@ func search(conn *sql.DB, opts searchopts) list.List {
args = append(args, opts.retval)
}
sb.WriteString("ORDER BY timestamp ")
sb.WriteString("ASC ")
if opts.order != nil {
sb.WriteString(*opts.order)
sb.WriteRune(' ')
} else {
sb.WriteString("ASC ")
}
if opts.limit != nil {
sb.WriteString("LIMIT ")
sb.WriteString(strconv.Itoa(*opts.limit))
sb.WriteRune(' ')
}
queryStmt := sb.String()
@ -353,7 +365,8 @@ func main() {
q := strings.Join(args, " ")
opts := searchopts{}
opts.order = "ASC"
o := "ASC"
opts.order = &o
if q != "" {
cmd := "%" + q + "%"
opts.command = &cmd