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

Code before:
	})

	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 {
	if m == nil {
		return false
	}
	_, ok := m.fields.Load(name)
	return ok

Review comment (suggestion):
I think this nested `if else` can reduce to this (note the "not loaded" `!loaded`):

```
	var loaded bool
	if f, loaded = m.fields.LoadOrStore(newField.Name, newField); loaded && f.Type != typ {
		return f, false, ErrFieldTypeConflict
	} else {
		return f, !loaded, nil
	}
```

i believe including `loaded` in the `if` isn't needed either: just `f.Type != typ`


```
	var loaded bool
	if f, loaded = m.fields.LoadOrStore(newField.Name, newField); f.Type != typ {
		return f, !loaded, ErrFieldTypeConflict
	} else {
		return f, !loaded, nil
	}
```


Code after:
	})

	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 {
		return false
	}
	_, ok := m.fields.Load(name)
	return ok
}