diff --git a/history.go b/history.go index 0bd4c6e..028643d 100644 --- a/history.go +++ b/history.go @@ -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") diff --git a/main.go b/main.go index 4f465ce..cdb571a 100644 --- a/main.go +++ b/main.go @@ -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