示例#1
0
文件: shard.go 项目: rwarren/influxdb
// SeriesKeys returns a list of series in in all shards in a. If a series
// exists in multiple shards in a, all instances will be combined into a single
// Series by calling Combine on it.
func (a Shards) SeriesKeys(opt influxql.IteratorOptions) (influxql.SeriesList, error) {
	if influxql.Sources(opt.Sources).HasSystemSource() {
		// Only support a single system source.
		if len(opt.Sources) > 1 {
			return nil, errors.New("cannot select from multiple system sources")
		}
		// Meta queries don't need to know the series name and always have a single string.
		return []influxql.Series{{Aux: []influxql.DataType{influxql.String}}}, nil
	}

	seriesMap := make(map[string]influxql.Series)
	for _, sh := range a {
		series, err := sh.SeriesKeys(opt)
		if err != nil {
			return nil, err
		}

		for _, s := range series {
			cur, ok := seriesMap[s.ID()]
			if ok {
				cur.Combine(&s)
			} else {
				seriesMap[s.ID()] = s
			}
		}
	}

	seriesList := make([]influxql.Series, 0, len(seriesMap))
	for _, s := range seriesMap {
		seriesList = append(seriesList, s)
	}
	sort.Sort(influxql.SeriesList(seriesList))
	return influxql.SeriesList(seriesList), nil
}
示例#2
0
func (ic *IteratorCreator) SeriesKeys(opt influxql.IteratorOptions) (influxql.SeriesList, error) {
	if ic.SeriesKeysFn != nil {
		return ic.SeriesKeysFn(opt)
	}

	itr, err := ic.CreateIterator(opt)
	if err != nil {
		return nil, err
	}

	seriesMap := make(map[string]influxql.Series)
	switch itr := itr.(type) {
	case influxql.FloatIterator:
		for p := itr.Next(); p != nil; p = itr.Next() {
			s := influxql.Series{Name: p.Name, Tags: p.Tags, Aux: influxql.InspectDataTypes(p.Aux)}
			if series, ok := seriesMap[s.ID()]; ok {
				s.Combine(&series)
			}
			seriesMap[s.ID()] = s
		}
	case influxql.IntegerIterator:
		for p := itr.Next(); p != nil; p = itr.Next() {
			s := influxql.Series{Name: p.Name, Tags: p.Tags, Aux: influxql.InspectDataTypes(p.Aux)}
			if series, ok := seriesMap[s.ID()]; ok {
				s.Combine(&series)
			}
			seriesMap[s.ID()] = s
		}
	case influxql.StringIterator:
		for p := itr.Next(); p != nil; p = itr.Next() {
			s := influxql.Series{Name: p.Name, Tags: p.Tags, Aux: influxql.InspectDataTypes(p.Aux)}
			if series, ok := seriesMap[s.ID()]; ok {
				s.Combine(&series)
			}
			seriesMap[s.ID()] = s
		}
	case influxql.BooleanIterator:
		for p := itr.Next(); p != nil; p = itr.Next() {
			s := influxql.Series{Name: p.Name, Tags: p.Tags, Aux: influxql.InspectDataTypes(p.Aux)}
			if series, ok := seriesMap[s.ID()]; ok {
				s.Combine(&series)
			}
			seriesMap[s.ID()] = s
		}
	}

	seriesList := make([]influxql.Series, 0, len(seriesMap))
	for _, s := range seriesMap {
		seriesList = append(seriesList, s)
	}
	return influxql.SeriesList(seriesList), nil
}
示例#3
0
// Ensure series list can be marshaled into and out of a binary format.
func TestSeriesList_MarshalBinary(t *testing.T) {
	a := []influxql.Series{
		{Name: "cpu", Tags: ParseTags("foo=bar"), Aux: []influxql.DataType{influxql.Float, influxql.String}},
		{Name: "mem", Aux: []influxql.DataType{influxql.Integer}},
		{Name: "disk"},
		{},
	}

	// Marshal to binary.
	buf, err := influxql.SeriesList(a).MarshalBinary()
	if err != nil {
		t.Fatal(err)
	}

	// Unmarshal back to an object.
	var other influxql.SeriesList
	if err := other.UnmarshalBinary(buf); err != nil {
		t.Fatal(err)
	} else if !reflect.DeepEqual(other, influxql.SeriesList(a)) {
		t.Fatalf("unexpected series list: %s", spew.Sdump(other))
	}
}