github.com/influxdata/influxdb/v2@v2.7.6/measurement.go (about)

     1  package influxdb
     2  
     3  import "fmt"
     4  
     5  // Length of components of measurement names.
     6  const (
     7  	OrgIDLength       = 8
     8  	BucketIDLength    = 8
     9  	MeasurementLength = OrgIDLength + BucketIDLength
    10  )
    11  
    12  // ReadMeasurement reads the provided measurement name and returns an Org ID and
    13  // bucket ID. It returns an error if the provided name has an invalid length.
    14  //
    15  // ReadMeasurement does not allocate, and instead returns sub-slices of name,
    16  // so callers should be careful about subsequent mutations to the provided name
    17  // slice.
    18  func ReadMeasurement(name []byte) (orgID, bucketID []byte, err error) {
    19  	if len(name) != MeasurementLength {
    20  		return nil, nil, fmt.Errorf("measurement %v has invalid length (%d)", name, len(name))
    21  	}
    22  	return name[:OrgIDLength], name[len(name)-BucketIDLength:], nil
    23  }
    24  
    25  // CreateMeasurement returns 16 bytes that represent a measurement.
    26  //
    27  // If either org or bucket are short then an error is returned, otherwise the
    28  // first 8 bytes of each are combined and returned.
    29  func CreateMeasurement(org, bucket []byte) ([]byte, error) {
    30  	if len(org) < OrgIDLength {
    31  		return nil, fmt.Errorf("org %v has invalid length (%d)", org, len(org))
    32  	} else if len(bucket) < BucketIDLength {
    33  		return nil, fmt.Errorf("bucket %v has invalid length (%d)", bucket, len(bucket))
    34  	}
    35  
    36  	name := make([]byte, 0, MeasurementLength)
    37  	name = append(name, org[:OrgIDLength]...)
    38  	return append(name, bucket[:BucketIDLength]...), nil
    39  }