// addToIndexFromKey will pull the measurement name, series key, and field name from a composite key and add it to the // database index and measurement fields func (e *Engine) addToIndexFromKey(shardID uint64, key []byte, fieldType influxql.DataType, index *tsdb.DatabaseIndex) error { seriesKey, field := SeriesAndFieldFromCompositeKey(key) measurement := tsdb.MeasurementFromSeriesKey(string(seriesKey)) m := index.CreateMeasurementIndexIfNotExists(measurement) m.SetFieldName(field) mf := e.measurementFields[measurement] if mf == nil { mf = tsdb.NewMeasurementFields() e.measurementFields[measurement] = mf } if err := mf.CreateFieldIfNotExists(field, fieldType, false); err != nil { return err } // Have we already indexed this series? ss := index.SeriesBytes(seriesKey) if ss != nil { // Add this shard to the existing series ss.AssignShard(shardID) return nil } // ignore error because ParseKey returns "missing fields" and we don't have // fields (in line protocol format) in the series key _, tags, _ := models.ParseKey(seriesKey) s := tsdb.NewSeries(string(seriesKey), tags) index.CreateSeriesIndexIfNotExists(measurement, s) s.AssignShard(shardID) return nil }
// addToIndexFromKey will pull the measurement name, series key, and field name from a composite key and add it to the // database index and measurement fields func (e *Engine) addToIndexFromKey(key string, fieldType influxql.DataType, index *tsdb.DatabaseIndex) error { seriesKey, field := seriesAndFieldFromCompositeKey(key) measurement := tsdb.MeasurementFromSeriesKey(seriesKey) m := index.CreateMeasurementIndexIfNotExists(measurement) m.SetFieldName(field) mf := e.measurementFields[measurement] if mf == nil { mf = tsdb.NewMeasurementFields() e.measurementFields[measurement] = mf } if err := mf.CreateFieldIfNotExists(field, fieldType, false); err != nil { return err } _, tags, err := models.ParseKey(seriesKey) if err == nil { return err } s := tsdb.NewSeries(seriesKey, tags) s.InitializeShards() index.CreateSeriesIndexIfNotExists(measurement, s) return nil }
// MeasurementFields returns the measurement fields for a measurement. func (e *Engine) MeasurementFields(measurement string) *tsdb.MeasurementFields { e.mu.RLock() m := e.measurementFields[measurement] e.mu.RUnlock() if m != nil { return m } e.mu.Lock() m = e.measurementFields[measurement] if m == nil { m = tsdb.NewMeasurementFields() e.measurementFields[measurement] = m } e.mu.Unlock() return m }