Skip to content

Commit

Permalink
feat: Add MeasurementNames method to MeasurementFieldSet (#23173)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcharlton authored Mar 15, 2022
1 parent 7d310c2 commit 4e08604
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tsdb/shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,20 @@ func (fs *MeasurementFieldSet) Bytes() int {
return b
}

// MeasurementNames returns the names of all of the measurements in the field set in
// lexographical order.
func (fs *MeasurementFieldSet) MeasurementNames() []string {
fs.mu.RLock()
defer fs.mu.RUnlock()

names := make([]string, 0, len(fs.fields))
for name := range fs.fields {
names = append(names, name)
}
sort.Strings(names)
return names
}

// Fields returns fields for a measurement by name.
func (fs *MeasurementFieldSet) Fields(name []byte) *MeasurementFields {
fs.mu.RLock()
Expand Down
19 changes: 19 additions & 0 deletions tsdb/shard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
_ "github.com/influxdata/influxdb/v2/tsdb/engine"
_ "github.com/influxdata/influxdb/v2/tsdb/index"
"github.com/influxdata/influxql"
"github.com/stretchr/testify/assert"
)

func TestShardWriteAndIndex(t *testing.T) {
Expand Down Expand Up @@ -1705,6 +1706,24 @@ func TestMeasurementFieldSet_ConcurrentSave(t *testing.T) {
}
}

func TestMeasurementFieldSet_MeasurementNames(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "fields.idx")
mf, err := tsdb.NewMeasurementFieldSet(path)
if err != nil {
t.Fatalf("NewMeasurementFieldSet error: %v", err)
}
defer mf.Close()

mf.CreateFieldsIfNotExists([]byte("cpu"))
mf.CreateFieldsIfNotExists([]byte("memory"))
mf.CreateFieldsIfNotExists([]byte("disk_usage"))

exp := []string{"cpu", "disk_usage", "memory"}
got := mf.MeasurementNames()
assert.Equal(t, exp, got)
}

func testFieldMaker(t *testing.T, wg *sync.WaitGroup, mf *tsdb.MeasurementFieldSet, measurement string, fieldNames []string) {
defer wg.Done()
fields := mf.CreateFieldsIfNotExists([]byte(measurement))
Expand Down

0 comments on commit 4e08604

Please sign in to comment.