mirror of
https://github.com/quitesimpleorg/hs9001.git
synced 2024-11-22 08:47:50 +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.
This commit is contained in:
parent
1a6c75cea9
commit
3a6a1b2aa9
22
history.go
22
history.go
@ -12,16 +12,15 @@ type history struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *history) GetHistoryByPrefix(prefix string) (ph []string) {
|
func (h *history) GetHistoryByPrefix(prefix string) (ph []string) {
|
||||||
/* Hack for performance reasons */
|
|
||||||
if len(prefix) < 2 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
opts := searchopts{}
|
opts := searchopts{}
|
||||||
opts.order = "DESC"
|
o := "DESC"
|
||||||
|
opts.order = &o
|
||||||
|
lim := 100
|
||||||
|
opts.limit = &lim
|
||||||
cmdqry := prefix + "%"
|
cmdqry := prefix + "%"
|
||||||
opts.command = &cmdqry
|
opts.command = &cmdqry
|
||||||
results := search(h.conn, opts)
|
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)
|
entry, ok := e.Value.(*HistoryEntry)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Panic("Failed to retrieve entries")
|
log.Panic("Failed to retrieve entries")
|
||||||
@ -31,16 +30,15 @@ func (h *history) GetHistoryByPrefix(prefix string) (ph []string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
func (h *history) GetHistoryByPattern(pattern string) (ph []string, pos []int) {
|
func (h *history) GetHistoryByPattern(pattern string) (ph []string, pos []int) {
|
||||||
/* Hack for performance reasons */
|
|
||||||
if len(pattern) < 2 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
opts := searchopts{}
|
opts := searchopts{}
|
||||||
opts.order = "DESC"
|
o := "DESC"
|
||||||
|
opts.order = &o
|
||||||
|
lim := 100
|
||||||
|
opts.limit = &lim
|
||||||
cmdqry := "%" + pattern + "%"
|
cmdqry := "%" + pattern + "%"
|
||||||
opts.command = &cmdqry
|
opts.command = &cmdqry
|
||||||
results := search(h.conn, opts)
|
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)
|
entry, ok := e.Value.(*HistoryEntry)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Panic("Failed to retrieve entries")
|
log.Panic("Failed to retrieve entries")
|
||||||
|
19
main.go
19
main.go
@ -157,7 +157,8 @@ type searchopts struct {
|
|||||||
after *time.Time
|
after *time.Time
|
||||||
before *time.Time
|
before *time.Time
|
||||||
retval *int
|
retval *int
|
||||||
order string
|
order *string
|
||||||
|
limit *int
|
||||||
}
|
}
|
||||||
|
|
||||||
func search(conn *sql.DB, opts searchopts) list.List {
|
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)
|
args = append(args, opts.retval)
|
||||||
}
|
}
|
||||||
sb.WriteString("ORDER BY timestamp ")
|
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()
|
queryStmt := sb.String()
|
||||||
|
|
||||||
@ -353,7 +365,8 @@ func main() {
|
|||||||
q := strings.Join(args, " ")
|
q := strings.Join(args, " ")
|
||||||
|
|
||||||
opts := searchopts{}
|
opts := searchopts{}
|
||||||
opts.order = "ASC"
|
o := "ASC"
|
||||||
|
opts.order = &o
|
||||||
if q != "" {
|
if q != "" {
|
||||||
cmd := "%" + q + "%"
|
cmd := "%" + q + "%"
|
||||||
opts.command = &cmd
|
opts.command = &cmd
|
||||||
|
Loading…
Reference in New Issue
Block a user