diff --git a/exporter/configuration/configuration.go b/exporter/configuration/configuration.go new file mode 100644 index 0000000..8f2da30 --- /dev/null +++ b/exporter/configuration/configuration.go @@ -0,0 +1,7 @@ +package configuration + +type Configuration interface { + GetName() string + GetFields() []Field + AddField(field Field) +} diff --git a/exporter/configuration/field.go b/exporter/configuration/field.go new file mode 100644 index 0000000..947efe3 --- /dev/null +++ b/exporter/configuration/field.go @@ -0,0 +1,7 @@ +package configuration + +type Field interface { + GetID() int + GetName() string + GetType() FieldType +} diff --git a/exporter/configuration/field_type.go b/exporter/configuration/field_type.go new file mode 100644 index 0000000..ca94e36 --- /dev/null +++ b/exporter/configuration/field_type.go @@ -0,0 +1,36 @@ +package configuration + +import "fmt" + +const ( + FieldTypeString = FieldType(iota) + FieldTypeInt + FieldTypeInt64 + FieldTypeFloat32 + FieldTypeFloat64 + FieldTypeByte + FieldTypeBool +) + +type FieldType byte + +func (slf FieldType) String() string { + switch slf { + case FieldTypeString: + return "string" + case FieldTypeInt: + return "int" + case FieldTypeInt64: + return "int64" + case FieldTypeFloat32: + return "float32" + case FieldTypeFloat64: + return "float64" + case FieldTypeByte: + return "byte" + case FieldTypeBool: + return "bool" + } + + panic(fmt.Errorf("not support field type %v", byte(slf))) +} diff --git a/exporter/configuration/golang/configuration.go b/exporter/configuration/golang/configuration.go new file mode 100644 index 0000000..f70e133 --- /dev/null +++ b/exporter/configuration/golang/configuration.go @@ -0,0 +1,30 @@ +package golang + +import ( + "minotaur/exporter/configuration" + "minotaur/utils/hash" +) + +func NewConfiguration(name string) *Configuration { + return &Configuration{ + name: name, + fields: hash.NewSortMap[int, configuration.Field](), + } +} + +type Configuration struct { + name string + fields *hash.SortMap[int, configuration.Field] +} + +func (slf *Configuration) GetName() string { + return slf.name +} + +func (slf *Configuration) GetFields() []configuration.Field { + return slf.fields.ToSliceSort() +} + +func (slf *Configuration) AddField(field configuration.Field) { + slf.fields.Set(field.GetID(), field) +} diff --git a/exporter/configuration/golang/field.go b/exporter/configuration/golang/field.go new file mode 100644 index 0000000..e8de6b9 --- /dev/null +++ b/exporter/configuration/golang/field.go @@ -0,0 +1,30 @@ +package golang + +import ( + "minotaur/exporter/configuration" +) + +func NewField(name string, fieldType configuration.FieldType) *Field { + return &Field{ + name: name, + fieldType: fieldType, + } +} + +type Field struct { + id int + name string + fieldType configuration.FieldType +} + +func (slf *Field) GetID() int { + return slf.id +} + +func (slf *Field) GetName() string { + return slf.name +} + +func (slf *Field) GetType() configuration.FieldType { + return slf.fieldType +} diff --git a/utils/hash/srot_map.go b/utils/hash/srot_map.go index 51f70d6..9df2975 100644 --- a/utils/hash/srot_map.go +++ b/utils/hash/srot_map.go @@ -4,6 +4,14 @@ import ( "sort" ) +func NewSortMap[K comparable, V any]() *SortMap[K, V] { + return &SortMap[K, V]{ + m: map[K]V{}, + s: map[int]K{}, + r: map[K]int{}, + } +} + // SortMap 有序的 map 实现 type SortMap[K comparable, V any] struct { i int @@ -13,16 +21,6 @@ type SortMap[K comparable, V any] struct { } func (slf *SortMap[K, V]) Set(key K, value V) { - if slf.m == nil { - slf.m = make(map[K]V) - } - if slf.s == nil { - slf.s = make(map[int]K) - } - if slf.r == nil { - slf.r = make(map[K]int) - } - if i, exist := slf.r[key]; exist { slf.s[i] = key slf.m[key] = value @@ -42,12 +40,12 @@ func (slf *SortMap[K, V]) Del(key K) { } } -func (slf SortMap[K, V]) Get(key K) V { +func (slf *SortMap[K, V]) Get(key K) V { v := slf.m[key] return v } -func (slf SortMap[K, V]) For(handle func(key K, value V) bool) { +func (slf *SortMap[K, V]) For(handle func(key K, value V) bool) { for k, v := range slf.m { if !handle(k, v) { break @@ -55,7 +53,7 @@ func (slf SortMap[K, V]) For(handle func(key K, value V) bool) { } } -func (slf SortMap[K, V]) ForSort(handle func(key K, value V) bool) { +func (slf *SortMap[K, V]) ForSort(handle func(key K, value V) bool) { var indexes []int for i, _ := range slf.s { indexes = append(indexes, i) @@ -69,7 +67,7 @@ func (slf SortMap[K, V]) ForSort(handle func(key K, value V) bool) { } } -func (slf SortMap[K, V]) ToMap() map[K]V { +func (slf *SortMap[K, V]) ToMap() map[K]V { var m = make(map[K]V) for k, v := range slf.m { m[k] = v @@ -77,7 +75,7 @@ func (slf SortMap[K, V]) ToMap() map[K]V { return m } -func (slf SortMap[K, V]) ToSlice() []V { +func (slf *SortMap[K, V]) ToSlice() []V { var s = make([]V, 0, len(slf.m)) for _, v := range slf.m { s = append(s, v) @@ -85,7 +83,21 @@ func (slf SortMap[K, V]) ToSlice() []V { return s } -func (slf SortMap[K, V]) KeyToSlice() []K { +func (slf *SortMap[K, V]) ToSliceSort() []V { + var indexes []int + for i, _ := range slf.s { + indexes = append(indexes, i) + } + sort.Ints(indexes) + var result []V + for _, i := range indexes { + k := slf.s[i] + result = append(result, slf.m[k]) + } + return result +} + +func (slf *SortMap[K, V]) KeyToSlice() []K { var s = make([]K, 0, len(slf.m)) for k := range slf.m { s = append(s, k)