schema_builder.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. func RecordTypeBegin() *RecordTypeBuilder {
  19. return &RecordTypeBuilder{recordType: &schema_pb.RecordType{}}
  20. }
  21. func (rtb *RecordTypeBuilder) RecordTypeEnd() *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) WithField(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) WithRecordField(name string, recordType *schema_pb.RecordType) *RecordTypeBuilder {
  36. rtb.recordType.Fields = append(rtb.recordType.Fields, &schema_pb.Field{
  37. Name: name,
  38. Type: &schema_pb.Type{Kind: &schema_pb.Type_RecordType{RecordType: recordType}},
  39. })
  40. return rtb
  41. }
  42. func ListOf(elementType *schema_pb.Type) *schema_pb.Type {
  43. return &schema_pb.Type{Kind: &schema_pb.Type_ListType{ListType: &schema_pb.ListType{ElementType: elementType}}}
  44. }