Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
use strings.HasPrefix and other minor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
replay committed Dec 15, 2017
1 parent a50882b commit d75149a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 51 deletions.
24 changes: 7 additions & 17 deletions idx/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,20 +474,20 @@ func (m *MemoryIdx) TagDetails(orgId int, key, filter string, from int64) (map[s
// tagPrefix: the string to be completed
// expressions: tagdb expressions in the same format as graphite uses
// from: only tags will be returned that have at least one metric
// with a LastUpdate above from
// with a LastUpdate >= from
// limit: the maximum number of results to return, the results will
// always be sorted alphabetically for consistency between
// consecutive queries and the limit is applied after sorting
//
func (m *MemoryIdx) AutoCompleteTags(orgId int, tagPrefix string, expressions []string, from int64, limit uint) ([]string, error) {
res := make([]string, 0)
var res []string

// only if expressions are specified we need to build a tag query.
// otherwise, the generation of the result set is much simpler
if len(expressions) > 0 {
// incorporate the tag prefix into the tag query expressions
if len(tagPrefix) > 0 {
expressions = append(expressions, fmt.Sprintf("__tag^=%s", tagPrefix))
expressions = append(expressions, "__tag^="+tagPrefix)
}

query, err := NewTagQuery(expressions, from)
Expand Down Expand Up @@ -524,7 +524,7 @@ func (m *MemoryIdx) AutoCompleteTags(orgId int, tagPrefix string, expressions []

tagsSorted := make([]string, 0, len(tags))
for tag := range tags {
if len(tagPrefix) > 0 && (len(tagPrefix) > len(tag) || tag[:len(tagPrefix)] != tagPrefix) {
if !strings.HasPrefix(tag, tagPrefix) {
continue
}

Expand Down Expand Up @@ -616,6 +616,7 @@ func (m *MemoryIdx) AutoCompleteTagValues(orgId int, tag, valPrefix string, expr

ids := query.Run(tags, m.DefById)
valueMap := make(map[string]struct{})
prefix := tag + "="
for id := range ids {
var ok bool
var def *idx.Archive
Expand All @@ -632,18 +633,7 @@ func (m *MemoryIdx) AutoCompleteTagValues(orgId int, tag, valPrefix string, expr
valueMap[def.Name] = struct{}{}
} else {
for _, t := range def.Tags {
// tag + "=" + at least one character as value so "+ 2"
if len(tag)+2 >= len(t) {
continue
}

// after the tag there must be a "=" sign
if t[len(tag)] != 61 {
continue
}

// check if tag matches
if t[:len(tag)] != tag {
if !strings.HasPrefix(t, prefix) {
continue
}

Expand Down Expand Up @@ -673,7 +663,7 @@ func (m *MemoryIdx) AutoCompleteTagValues(orgId int, tag, valPrefix string, expr

res = make([]string, 0, len(vals))
for val := range vals {
if len(valPrefix) > 0 && (len(valPrefix) > len(val) || val[:len(valPrefix)] != valPrefix) {
if !strings.HasPrefix(val, valPrefix) {
continue
}

Expand Down
47 changes: 13 additions & 34 deletions idx/memory/tag_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (q *TagQuery) getInitialByPrefix(expr kv, idCh chan idx.MetricID, stopCh ch

VALUES:
for v, ids := range q.index[expr.key] {
if len(v) < len(expr.value) || v[:len(expr.value)] != expr.value {
if !strings.HasPrefix(v, expr.value) {
continue
}

Expand Down Expand Up @@ -406,11 +406,7 @@ func (q *TagQuery) getInitialByTagPrefix(idCh chan idx.MetricID, stopCh chan str

TAGS:
for tag, values := range q.index {
if len(tag) < len(q.tagPrefix) {
continue
}

if tag[:len(q.tagPrefix)] != q.tagPrefix {
if !strings.HasPrefix(tag, q.tagPrefix) {
continue
}

Expand Down Expand Up @@ -544,13 +540,10 @@ EXPRS:
}
}
}
for _, tag := range def.Tags {
// length of key doesn't match
if len(tag) <= len(e.key)+1 || tag[len(e.key)] != 61 {
continue
}

if e.key != tag[:len(e.key)] {
prefix := e.key + "="
for _, tag := range def.Tags {
if !strings.HasPrefix(tag, prefix) {
continue
}

Expand Down Expand Up @@ -662,22 +655,18 @@ EXPRS:
// - key is "name"
// - def.Name is long enough so it might match the prefix
// - the prefix matches with def.Name
if e.key == "name" &&
len(def.Name) >= len(e.value) &&
def.Name[:len(e.value)] == e.value {
if e.key == "name" && strings.HasPrefix(def.Name, e.value) {
continue EXPRS
}

prefix := e.key + "=" + e.value
for _, tag := range def.Tags {
// continue if any of these match:
// - length of tag is too short, so this can't be a match
// - the position where we expect the = is not a =
// - the key does not match
// - the prefix value does not match
if len(tag) < len(e.key)+len(e.value)+1 ||
tag[len(e.key)] != 61 ||
tag[:len(e.key)] != e.key ||
tag[len(e.key)+1:len(e.key)+len(e.value)+1] != e.value {
if !strings.HasPrefix(tag, prefix) {
continue
}
continue EXPRS
Expand All @@ -689,16 +678,12 @@ EXPRS:

// testByTagPrefix filters a given metric by matching prefixes against its tags
func (q *TagQuery) testByTagPrefix(def *idx.Archive) bool {
if len(q.tagPrefix) <= 4 && q.tagPrefix == "name"[:len(q.tagPrefix)] {
if strings.HasPrefix("name", q.tagPrefix) {
return true
}

for _, tag := range def.Tags {
if len(tag) < len(q.tagPrefix) {
continue
}

if tag[:len(q.tagPrefix)] == q.tagPrefix {
if strings.HasPrefix(tag, q.tagPrefix) {
return true
}
}
Expand Down Expand Up @@ -822,10 +807,7 @@ func (q *TagQuery) getMaxTagCount() int {

if q.filterTagBy == PREFIX_TAG && len(q.tagPrefix) > 0 {
for tag := range q.index {
if len(tag) < len(q.tagPrefix) {
continue
}
if tag[:len(q.tagPrefix)] != q.tagPrefix {
if !strings.HasPrefix(tag, q.tagPrefix) {
continue
}
maxTagCount++
Expand Down Expand Up @@ -883,10 +865,7 @@ IDS:
}

if q.filterTagBy == PREFIX_TAG {
if len(key) < len(q.tagPrefix) {
continue
}
if key[:len(q.tagPrefix)] != q.tagPrefix {
if !strings.HasPrefix(key, q.tagPrefix) {
continue
}
} else if q.filterTagBy == MATCH_TAG {
Expand Down Expand Up @@ -945,7 +924,7 @@ func (q *TagQuery) tagFilterMatchesName() bool {
matchName := false

if q.filterTagBy == PREFIX_TAG || q.startWith == PREFIX_TAG {
if len(q.tagPrefix) <= 4 && "name"[:len(q.tagPrefix)] == q.tagPrefix {
if strings.HasPrefix("name", q.tagPrefix) {
matchName = true
}
} else if q.filterTagBy == MATCH_TAG || q.startWith == MATCH_TAG {
Expand Down

0 comments on commit d75149a

Please sign in to comment.