seaweedfs/weed/mq/schema/schema_builder.go

79 lines
2.9 KiB
Go
Raw Normal View History

2024-04-19 13:27:18 +08:00
package schema
import (
"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
2024-04-22 15:42:18 +08:00
"sort"
2024-04-19 13:27:18 +08:00
)
var (
TypeBoolean = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_BOOLEAN}}
TypeInteger = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_INTEGER}}
TypeLong = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_LONG}}
TypeFloat = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_FLOAT}}
TypeDouble = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_DOUBLE}}
TypeBytes = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_BYTES}}
TypeString = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_STRING}}
)
type RecordTypeBuilder struct {
recordType *schema_pb.RecordType
}
func NewRecordTypeBuilder() *RecordTypeBuilder {
return &RecordTypeBuilder{recordType: &schema_pb.RecordType{}}
}
func (rtb *RecordTypeBuilder) Build() *schema_pb.RecordType {
2024-04-22 15:42:18 +08:00
// be consistent with parquet.node.go `func (g Group) Fields() []Field`
sort.Slice(rtb.recordType.Fields, func(i, j int) bool {
return rtb.recordType.Fields[i].Name < rtb.recordType.Fields[j].Name
})
2024-04-19 13:27:18 +08:00
return rtb.recordType
}
2024-04-29 02:14:13 +08:00
func (rtb *RecordTypeBuilder) setField(name string, scalarType *schema_pb.Type) *RecordTypeBuilder {
2024-04-19 13:27:18 +08:00
rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
Name: name,
Type: scalarType,
})
return rtb
}
2024-04-28 14:44:11 +08:00
func (rtb *RecordTypeBuilder) SetBoolField(name string) *RecordTypeBuilder {
2024-04-29 02:14:13 +08:00
return rtb.setField(name, TypeBoolean)
2024-04-19 13:27:18 +08:00
}
2024-04-28 14:44:11 +08:00
func (rtb *RecordTypeBuilder) SetIntegerField(name string) *RecordTypeBuilder {
2024-04-29 02:14:13 +08:00
return rtb.setField(name, TypeInteger)
2024-04-19 13:27:18 +08:00
}
2024-04-28 14:44:11 +08:00
func (rtb *RecordTypeBuilder) SetLongField(name string) *RecordTypeBuilder {
2024-04-29 02:14:13 +08:00
return rtb.setField(name, TypeLong)
2024-04-19 13:27:18 +08:00
}
2024-04-28 14:44:11 +08:00
func (rtb *RecordTypeBuilder) SetFloatField(name string) *RecordTypeBuilder {
2024-04-29 02:14:13 +08:00
return rtb.setField(name, TypeFloat)
2024-04-19 13:27:18 +08:00
}
2024-04-28 14:44:11 +08:00
func (rtb *RecordTypeBuilder) SetDoubleField(name string) *RecordTypeBuilder {
2024-04-29 02:14:13 +08:00
return rtb.setField(name, TypeDouble)
2024-04-19 13:27:18 +08:00
}
2024-04-28 14:44:11 +08:00
func (rtb *RecordTypeBuilder) SetBytesField(name string) *RecordTypeBuilder {
2024-04-29 02:14:13 +08:00
return rtb.setField(name, TypeBytes)
2024-04-19 13:27:18 +08:00
}
2024-04-28 14:44:11 +08:00
func (rtb *RecordTypeBuilder) SetStringField(name string) *RecordTypeBuilder {
2024-04-29 02:14:13 +08:00
return rtb.setField(name, TypeString)
2024-04-19 13:27:18 +08:00
}
2024-04-28 14:44:11 +08:00
func (rtb *RecordTypeBuilder) SetRecordField(name string, recordTypeBuilder *RecordTypeBuilder) *RecordTypeBuilder {
2024-04-19 13:27:18 +08:00
rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
Name: name,
2024-04-24 16:32:02 +08:00
Type: &schema_pb.Type{Kind: &schema_pb.Type_RecordType{RecordType: recordTypeBuilder.Build()}},
2024-04-19 13:27:18 +08:00
})
return rtb
}
2024-04-28 14:44:11 +08:00
func (rtb *RecordTypeBuilder) SetListField(name string, elementType *schema_pb.Type) *RecordTypeBuilder {
2024-04-19 13:27:18 +08:00
rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
Name: name,
Type: &schema_pb.Type{Kind: &schema_pb.Type_ListType{ListType: &schema_pb.ListType{ElementType: elementType}}},
})
return rtb
}