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.

59 lines
2.4 KiB

8 months ago
7 months ago
8 months ago
8 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_BOOL}}
  8. TypeInt32 = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_INT32}}
  9. TypeInt64 = &schema_pb.Type{Kind: &schema_pb.Type_ScalarType{schema_pb.ScalarType_INT64}}
  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. // RecordTypeBegin creates a new RecordTypeBuilder, it should be followed by a series of WithField methods and RecordTypeEnd
  19. func RecordTypeBegin() *RecordTypeBuilder {
  20. return &RecordTypeBuilder{recordType: &schema_pb.RecordType{}}
  21. }
  22. // RecordTypeEnd finishes the building of a RecordValue
  23. func (rtb *RecordTypeBuilder) RecordTypeEnd() *schema_pb.RecordType {
  24. // be consistent with parquet.node.go `func (g Group) Fields() []Field`
  25. sort.Slice(rtb.recordType.Fields, func(i, j int) bool {
  26. return rtb.recordType.Fields[i].Name < rtb.recordType.Fields[j].Name
  27. })
  28. return rtb.recordType
  29. }
  30. // NewRecordTypeBuilder creates a new RecordTypeBuilder from an existing RecordType, it should be followed by a series of WithField methods and RecordTypeEnd
  31. func NewRecordTypeBuilder(recordType *schema_pb.RecordType) (rtb *RecordTypeBuilder) {
  32. return &RecordTypeBuilder{recordType: recordType}
  33. }
  34. func (rtb *RecordTypeBuilder) WithField(name string, scalarType *schema_pb.Type) *RecordTypeBuilder {
  35. rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
  36. Name: name,
  37. Type: scalarType,
  38. })
  39. return rtb
  40. }
  41. func (rtb *RecordTypeBuilder) WithRecordField(name string, recordType *schema_pb.RecordType) *RecordTypeBuilder {
  42. rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
  43. Name: name,
  44. Type: &schema_pb.Type{Kind: &schema_pb.Type_RecordType{RecordType: recordType}},
  45. })
  46. return rtb
  47. }
  48. func ListOf(elementType *schema_pb.Type) *schema_pb.Type {
  49. return &schema_pb.Type{Kind: &schema_pb.Type_ListType{ListType: &schema_pb.ListType{ElementType: elementType}}}
  50. }