Repository: influxdata/influxdb
File: tsdb/shard.go (Go)

Code before:
func (m *MeasurementFields) FieldKeys() []string {
	var a []string
	m.fields.Range(func(k string, _ *Field) bool {
		a = append(a, k)
		return true
	})

	sort.Strings(a)
	return a
}

// bytes estimates the memory footprint of this MeasurementFields, in bytes.
func (m *MeasurementFields) bytes() int {
	var b int
	b += int(unsafe.Sizeof(m.fields))
	m.fields.Range(func(k string, v *Field) bool {
		b += int(unsafe.Sizeof(k)) + len(k)
		b += int(unsafe.Sizeof(v)+unsafe.Sizeof(*v)) + len(v.Name)
		return true
	})
	return b
}

// CreateFieldIfNotExists creates a new field with the given name and type.
// Returns an error if the field already exists with a different type.
func (m *MeasurementFields) CreateFieldIfNotExists(name string, typ influxql.DataType) (*Field, bool, error) {
	newField := &Field{
		Name: name,
		Type: typ,
	}
	if f, loaded := m.fields.LoadOrStore(newField.Name, newField); loaded {
		if f.Type != typ {
			return f, false, ErrFieldTypeConflict
		}
		return f, false, nil
	} else {
		return f, true, nil
	}
}

func (m *MeasurementFields) FieldN() int {
	return m.fields.Len()
}

// Field returns the field for name, or nil if there is no field for name.
func (m *MeasurementFields) Field(name string) *Field {
	f, _ := m.fields.Load(name)
	return f
}

func (m *MeasurementFields) HasField(name string) bool {

Review comment (suggestion):
I was momentarily confused by the `bool` - it is true if the field is created, so it is the opposite of `loaded`. 

I suggest naming the return values. `(f *Field, created bool, err error)`

also I point out `CreateFieldIfNotExists` currently can only return a `ErrFieldTypeConflict` so it could get the same treatment that `ValidateAndCreateFields` to return a specific error type - your pick. 

Code after:
func (m *MeasurementFields) FieldKeys() []string {
	var a []string
	m.fields.Range(func(k string, _ *Field) bool {
		a = append(a, k)
		return true
	})

	sort.Strings(a)
	return a
}

// bytes estimates the memory footprint of this MeasurementFields, in bytes.
func (m *MeasurementFields) bytes() int {
	var b int
	b += int(unsafe.Sizeof(m.fields))
	m.fields.Range(func(k string, v *Field) bool {
		b += int(unsafe.Sizeof(k)) + len(k)
		b += int(unsafe.Sizeof(v)+unsafe.Sizeof(*v)) + len(v.Name)
		return true
	})
	return b
}

// CreateFieldIfNotExists creates a new field with the given name and type.
// Returns an error if the field already exists with a different type.
func (m *MeasurementFields) CreateFieldIfNotExists(name string, typ influxql.DataType) (f *Field, created bool, err error) {
	newField := &Field{
		Name: name,
		Type: typ,
	}
	var loaded bool
	if f, loaded = m.fields.LoadOrStore(newField.Name, newField); f.Type != typ {
		// This implies the field existed as a different type already.
		return f, false, ErrFieldTypeConflict
	} else {
		return f, !loaded, nil
	}
}

func (m *MeasurementFields) FieldN() int {
	return m.fields.Len()
}

// Field returns the field for name, or nil if there is no field for name.
func (m *MeasurementFields) Field(name string) *Field {
	f, _ := m.fields.Load(name)
	return f
}

func (m *MeasurementFields) HasField(name string) bool {
	if m == nil {