Esempio n. 1
0
func (q *TQuery) parseStr(f TQueryField, selector string) (*TQuery, error) {
	min, max := f.GetLimit()
	newset := bitset.New(uint(max - min + 1))

	if selector == "*" {
		return q, nil
	}

	if patternRegular.FindString(selector) != "" {
		// patterns like */2, */3
		// interval becomes 2 or 3 in these cases
		interval, err := strconv.ParseInt(selector[2:], 10, 32)
		if err != nil {
			return nil, err
		}

		for i := min; i <= max; i += int(interval) {
			newset.Set(uint(f.Index(i)))
		}
	} else if patternRanges.FindString(selector) != "" {
		ranges := strings.Split(selector, ",")
		for _, r := range ranges {
			values := patternRange.FindStringSubmatch(r)
			if values != nil {
				// range
				minRange, _ := strconv.ParseInt(values[1], 10, 32)
				maxRange, _ := strconv.ParseInt(values[2], 10, 32)
				if int(minRange) < min || int(minRange) > max {
					return nil, fmt.Errorf("Min out of range %d. expected [%d, %d]", minRange, min, max)
				}
				if int(maxRange) < min || int(maxRange) > max {
					return nil, fmt.Errorf("Max out of range %d. expected [%d, %d]", minRange, min, max)
				}

				for i := minRange; i <= maxRange; i++ {
					newset.Set(uint(f.Index(int(i))))
				}
			} else {
				// single value
				val, _ := strconv.ParseInt(r, 10, 32)
				if int(val) < min || int(val) > max {
					return nil, fmt.Errorf("Out of range %d. expected [%d, %d]", val, min, max)
				}

				newset.Set(uint(f.Index(int(val))))
			}
		}
	} else {
		return nil, fmt.Errorf("Not supported selector %s", selector)
	}

	q.fields[f] = q.fields[f].Intersection(newset)
	return q, nil
}
Esempio n. 2
0
func newQuery() *TQuery {
	q := new(TQuery)
	q.fields = make([]*bitset.BitSet, FIELD_END)

	for f := TQueryField(0); f < FIELD_END; f++ {
		min, max := f.GetLimit()
		set := bitset.New(uint(max - min + 1))
		set.SetAll()
		q.fields[f] = set
	}

	q.minT = nil

	return q
}
Esempio n. 3
0
func (q *TQuery) intersection(f TQueryField, data ...int) (*TQuery, error) {
	min, max := f.GetLimit()
	newset := bitset.New(uint(max - min + 1))

	for _, datum := range data {
		if datum > max || datum < min {
			return nil, fmt.Errorf("%s should be between %d and %d. Given: %d", f.Name(), min, max, datum)
		}
		newset.Set(uint(f.Index(datum)))
	}

	q.fields[f] = q.fields[f].Intersection(newset)
	if q.fields[f].Count() == 0 {
		return nil, ErrEmpty
	}

	return q, nil
}