seaweedfs/weed/mq/schema/schema_builder.go

60 lines
2.4 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 (
2024-05-02 23:59:22 +08:00
TypeBoolean = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_BOOL}}
TypeInt32 = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_INT32}}
2024-05-21 02:03:56 +08:00
TypeInt64 = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_INT64}}
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}}
2024-04-19 13:27:18 +08:00
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
}
// RecordTypeBegin creates a new RecordTypeBuilder, it should be followed by a series of WithField methods and RecordTypeEnd
2024-04-29 04:00:52 +08:00
func RecordTypeBegin() *RecordTypeBuilder {
2024-04-19 13:27:18 +08:00
return &RecordTypeBuilder{recordType: &schema_pb.RecordType{}}
}
// RecordTypeEnd finishes the building of a RecordValue
2024-04-29 04:00:52 +08:00
func (rtb *RecordTypeBuilder) RecordTypeEnd() *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
}
// NewRecordTypeBuilder creates a new RecordTypeBuilder from an existing RecordType, it should be followed by a series of WithField methods and RecordTypeEnd
func NewRecordTypeBuilder(recordType *schema_pb.RecordType) (rtb *RecordTypeBuilder) {
return &RecordTypeBuilder{recordType: recordType}
}
2024-05-02 23:32:15 +08:00
func (rtb *RecordTypeBuilder) WithField(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-05-02 23:32:15 +08:00
func (rtb *RecordTypeBuilder) WithRecordField(name string, recordType *schema_pb.RecordType) *RecordTypeBuilder {
2024-04-19 13:27:18 +08:00
rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
Name: name,
2024-04-29 04:00:52 +08:00
Type: &schema_pb.Type{Kind: &schema_pb.Type_RecordType{RecordType: recordType}},
2024-04-19 13:27:18 +08:00
})
return rtb
}
2024-04-29 03:40:00 +08:00
func ListOf(elementType *schema_pb.Type) *schema_pb.Type {
2024-04-29 03:23:54 +08:00
return &schema_pb.Type{Kind: &schema_pb.Type_ListType{ListType: &schema_pb.ListType{ElementType: elementType}}}
}