AdGuardHome/internal/querylog/searchparams.go
Ainar Garipov a572876775 Pull request: all: fix lint and naming issues
Merge in DNS/adguard-home from 2276-fix-lint to master

Updates #2276.

Squashed commit of the following:

commit 433f44cc7b674a20ed60a9d29466ba888b3ef66e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Dec 7 14:14:28 2020 +0300

    querylog: improve code and documentation

commit 851df97d2a87de5e7180a502055ee6f1a6defdca
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Dec 4 20:36:32 2020 +0300

    all: fix lint and naming issues
2020-12-07 14:32:06 +03:00

57 lines
1.5 KiB
Go

package querylog
import "time"
// searchParams represent the search query sent by the client
type searchParams struct {
// searchCriteria - list of search criteria that we use to get filter results
searchCriteria []searchCriteria
// olderThen - return entries that are older than this value
// if not set - disregard it and return any value
olderThan time.Time
offset int // offset for the search
limit int // limit the number of records returned
maxFileScanEntries int // maximum log entries to scan in query log files. if 0 - no limit
}
// newSearchParams - creates an empty instance of searchParams
func newSearchParams() *searchParams {
return &searchParams{
// default max log entries to return
limit: 500,
// by default, we scan up to 50k entries at once
maxFileScanEntries: 50000,
}
}
// quickMatchesGetDataParams - quickly checks if the line matches the searchParams
// this method does not guarantee anything and the reason is to do a quick check
// without deserializing anything
func (s *searchParams) quickMatch(line string) bool {
for _, c := range s.searchCriteria {
if !c.quickMatch(line) {
return false
}
}
return true
}
// match - checks if the logEntry matches the searchParams
func (s *searchParams) match(entry *logEntry) bool {
if !s.olderThan.IsZero() && entry.Time.UnixNano() >= s.olderThan.UnixNano() {
// Ignore entries newer than what was requested
return false
}
for _, c := range s.searchCriteria {
if !c.match(entry) {
return false
}
}
return true
}