You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.9 KiB

9 months ago
9 months ago
9 months ago
  1. package schema
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  4. "sort"
  5. )
  6. var (
  7. TypeBoolean = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_BOOLEAN}}
  8. TypeInteger = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_INTEGER}}
  9. TypeLong = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_LONG}}
  10. TypeFloat = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_FLOAT}}
  11. TypeDouble = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_DOUBLE}}
  12. TypeBytes = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_BYTES}}
  13. TypeString = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_STRING}}
  14. )
  15. type RecordTypeBuilder struct {
  16. recordType *schema_pb.RecordType
  17. }
  18. func NewRecordTypeBuilder() *RecordTypeBuilder {
  19. return &RecordTypeBuilder{recordType: &schema_pb.RecordType{}}
  20. }
  21. func (rtb *RecordTypeBuilder) Build() *schema_pb.RecordType {
  22. // be consistent with parquet.node.go `func (g Group) Fields() []Field`
  23. sort.Slice(rtb.recordType.Fields, func(i, j int) bool {
  24. return rtb.recordType.Fields[i].Name < rtb.recordType.Fields[j].Name
  25. })
  26. return rtb.recordType
  27. }
  28. func (rtb *RecordTypeBuilder) addField(name string, scalarType *schema_pb.Type) *RecordTypeBuilder {
  29. rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
  30. Name: name,
  31. Type: scalarType,
  32. })
  33. return rtb
  34. }
  35. func (rtb *RecordTypeBuilder) AddBoolField(name string) *RecordTypeBuilder {
  36. return rtb.addField(name, TypeBoolean)
  37. }
  38. func (rtb *RecordTypeBuilder) AddIntegerField(name string) *RecordTypeBuilder {
  39. return rtb.addField(name, TypeInteger)
  40. }
  41. func (rtb *RecordTypeBuilder) AddLongField(name string) *RecordTypeBuilder {
  42. return rtb.addField(name, TypeLong)
  43. }
  44. func (rtb *RecordTypeBuilder) AddFloatField(name string) *RecordTypeBuilder {
  45. return rtb.addField(name, TypeFloat)
  46. }
  47. func (rtb *RecordTypeBuilder) AddDoubleField(name string) *RecordTypeBuilder {
  48. return rtb.addField(name, TypeDouble)
  49. }
  50. func (rtb *RecordTypeBuilder) AddBytesField(name string) *RecordTypeBuilder {
  51. return rtb.addField(name, TypeBytes)
  52. }
  53. func (rtb *RecordTypeBuilder) AddStringField(name string) *RecordTypeBuilder {
  54. return rtb.addField(name, TypeString)
  55. }
  56. func (rtb *RecordTypeBuilder) AddRecordField(name string, recordTypeBuilder *RecordTypeBuilder) *RecordTypeBuilder {
  57. rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
  58. Name: name,
  59. Type: &schema_pb.Type{Kind: &schema_pb.Type_RecordType{RecordType: recordTypeBuilder.Build()}},
  60. })
  61. return rtb
  62. }
  63. func (rtb *RecordTypeBuilder) AddListField(name string, elementType *schema_pb.Type) *RecordTypeBuilder {
  64. rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
  65. Name: name,
  66. Type: &schema_pb.Type{Kind: &schema_pb.Type_ListType{ListType: &schema_pb.ListType{ElementType: elementType}}},
  67. })
  68. return rtb
  69. }